http.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import axios from "axios";
  2. // import { Toast } from 'vant';
  3. import { Message } from "element-ui";
  4. import { Decrypt, Encrypt } from "./utils";
  5. import { oSessionStorage } from "./utils";
  6. import { router } from "@/router";
  7. // axios.defaults.baseURL = baseUrl;
  8. // axios.defaults.baseURL = 'http://10.113.248.4:8060';
  9. // axios.defaults.baseURL = 'http://49.232.26.44:8060';
  10. // axios.defaults.baseURL = 'https://child.jue-ming.com:8060/';
  11. // axios.defaults.baseURL = 'http://10.113.248.5:8070/';
  12. // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8070/';
  13. // axios.defaults.baseURL = 'https://gengnao.cn:8070/';
  14. // axios.defaults.baseURL = 'http://152.136.24.101:8071';
  15. // axios.defaults.baseURL = 'https://81.70.207.4:8070';
  16. // axios.defaults.baseURL = 'https://child.hhnao.com:8070';
  17. // axios.defaults.baseURL = 'https://child.hhnao.com:8070';
  18. //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx01d1a44906973cf2&redirect_uri=http%3A%2F%2F192.168.18.51%3A8085&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
  19. // export const basePath='http://10.113.248.3:8086'
  20. export const basePath = "http://10.113.248.4:8089";
  21. // const base_url = 'http://10.113.248.4:8090/'
  22. // export const basePath='http://43.143.198.30:8086'
  23. axios.defaults.baseURL = basePath;
  24. // axios.defaults.baseURL = "http://43.143.198.30:8086";
  25. //设置端口
  26. // var urlCode ='http://152.136.24.101:8997';
  27. // var urlCode ='http://43.143.198.30:8085';
  28. // var urlCode ='https://child.hhnao.com';
  29. var urlCode = "";
  30. //接口白名单
  31. var whiteList = ["/user/blogin", "/user/getCode"];
  32. //数据是否要加密
  33. var isMI = false;
  34. // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8060/';
  35. // axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
  36. sessionStorage.setItem("codeImage", urlCode);
  37. axios.defaults.timeout = 30000; // 超时时间
  38. axios.defaults.headers.common["Content-Type"] =
  39. "application/JSON;charset=UTF-8";
  40. axios.defaults.withCredentials = true;
  41. // let loadingInstance
  42. axios.interceptors.request.use(
  43. function (config) {
  44. for (let i = 0; i < whiteList.length; i++) {
  45. if (whiteList[i] !== config.url) {
  46. ////不在白名单内,需要判断用户是否在,不在的话存储到登录页
  47. if (oSessionStorage.getItem("userInfo") == "") {
  48. router.replace({
  49. path: "/",
  50. // query: {redirect: router.currentRoute.fullPath}
  51. });
  52. }
  53. //在此判断需要 判断用户缓存是否存在
  54. // break
  55. }
  56. }
  57. //发送请求前判断token是存在
  58. // 在发送请求之前做些什么
  59. // loadingInstance = Loading.service({
  60. // lock: true,
  61. // text: '加载中',
  62. // spinner: 'el-icon-loading',
  63. // background: 'rgba(0, 0, 0, 0.7)'
  64. // });
  65. // eslint-disable-next-line
  66. // const userId = sessionStorage.getItem("userId");
  67. // config.headers['type'] = process.env.VUE_APP_VERSION;
  68. // if (userId) {
  69. // if (sessionStorage.getItem(`${userId}token`)) {
  70. // config.headers.Authorization = `Bearer ${sessionStorage.getItem(`${userId}token`)}`;
  71. // }
  72. // }
  73. config.headers.Authorization = oSessionStorage.getItem("token")
  74. ? `Bearer ${oSessionStorage.getItem("token")}`
  75. : "";
  76. // console.log(config.headers.Authorization)
  77. return config;
  78. },
  79. function (error) {
  80. // 对请求错误做些什么
  81. return Promise.reject(error);
  82. }
  83. );
  84. // 添加响应拦截器
  85. axios.interceptors.response.use(
  86. function (response) {
  87. // 对响应数据做点什么
  88. // loadingInstance.close();
  89. //返回code若为401,则表示token有问题
  90. if (response.data.code === 401) {
  91. // 重定向到登录页
  92. router.replace({
  93. path: "/",
  94. // query: {redirect: router.currentRoute.fullPath}
  95. });
  96. // Toast.fail(response.data.msg);
  97. Message.error(response.data.msg);
  98. }
  99. return response;
  100. },
  101. function (error) {
  102. // 对响应错误做点什么
  103. // loadingInstance.close();
  104. return Promise.reject(error);
  105. }
  106. );
  107. const http = {
  108. get: (url, data, sCallBack) => {
  109. if (data) {
  110. if (isMI) {
  111. data = Encrypt(JSON.stringify(data));
  112. } else {
  113. data = JSON.stringify(data);
  114. }
  115. }
  116. axios.get(url, { params: data }).then((res) => {
  117. // res.data = JSON.parse(Decrypt(res.data));
  118. if (isMI) {
  119. res.data = JSON.parse(Decrypt(res.data));
  120. } else {
  121. // res.data = JSON.parse(res.data);
  122. }
  123. debugger
  124. if (sCallBack) {
  125. sCallBack(res.data);
  126. }
  127. });
  128. },
  129. getImg: (url, data, sCallBack) => {
  130. if (data) {
  131. data = Encrypt(JSON.stringify(data));
  132. }
  133. axios(
  134. {
  135. method: "get",
  136. url: url,
  137. responseType: "blob",
  138. params: data,
  139. headers: {
  140. Accept: "application/octet-stream",
  141. // "Content-Disposition":"attachment"
  142. },
  143. },
  144. { responseType: "arraybuffer" }
  145. ).then((res) => {
  146. sCallBack(res);
  147. });
  148. },
  149. getDown: (url, data, sCallBack) => {
  150. if (data) {
  151. data = Encrypt(JSON.stringify(data));
  152. }
  153. axios({
  154. method: "get",
  155. url: url,
  156. responseType: "blob",
  157. params: data,
  158. headers: {
  159. // Accept: "application/octet-stream",
  160. "Content-Disposition": "attachment",
  161. },
  162. }).then((res) => {
  163. sCallBack(res);
  164. });
  165. },
  166. post: (url, data, sucessCallBack, errCallBack) => {
  167. if (data) {
  168. if (isMI) {
  169. data = Encrypt(JSON.stringify(data));
  170. } else {
  171. // data = JSON.stringify(data);
  172. // data = data;
  173. }
  174. }
  175. axios.post(url, data ).then((res) => {
  176. // res.status
  177. if (res) {
  178. if (isMI) {
  179. res.data = JSON.parse(Decrypt(res.data));
  180. }else{
  181. res.data = JSON.parse(res.data);
  182. }
  183. }
  184. if (res.status >= 200 && res.status < 300) {
  185. if (sucessCallBack) {
  186. sucessCallBack(res.data);
  187. }
  188. } else if (res.status === 401) {
  189. //token过期了
  190. } else {
  191. if (errCallBack) {
  192. errCallBack(res.data);
  193. } else {
  194. // Toast(res.data.msg);
  195. Message.error(res.data.msg);
  196. }
  197. }
  198. });
  199. },
  200. delete: (url, data, sucessCallBack, errCallBack) => {
  201. if (data) {
  202. if(isMI){
  203. data = Encrypt(JSON.stringify(data));
  204. }else{
  205. data = JSON.stringify(data);
  206. }
  207. }
  208. axios.delete(url, { data: data }).then((res) => {
  209. // res.status
  210. if (res) {
  211. if(isMI){
  212. res.data = JSON.parse(Decrypt(res.data));
  213. }else{
  214. res.data = JSON.parse(res.data);
  215. }
  216. }
  217. if (res.status >= 200 && res.status < 300) {
  218. if (sucessCallBack) {
  219. sucessCallBack(res.data);
  220. }
  221. } else if (res.status === 401) {
  222. //token过期了
  223. } else {
  224. if (errCallBack) {
  225. errCallBack(res.data);
  226. } else {
  227. // Toast(res.data.msg);
  228. Message.error(res.data.msg);
  229. }
  230. }
  231. // if (res.data.code == 200) {
  232. // if (sucessCallBack) {
  233. // sucessCallBack(res.data);
  234. // }
  235. // } else if(res.data.code == 2001){
  236. // if (sucessCallBack) {
  237. // sucessCallBack(res.data);
  238. // }
  239. // }else {
  240. // if (errCallBack) {
  241. // errCallBack(res.data);
  242. // } else {
  243. // // Toast(res.data.msg);
  244. // Message.error(res.data.msg);
  245. // }
  246. // }
  247. });
  248. },
  249. };
  250. export default http;