http.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.4:8089'
  20. // export const basePath = "http://43.143.198.30:8089";
  21. // const base_url = 'http://43.143.198.30:8089/'
  22. // const base_url = 'http://10.113.248.4:8090/'
  23. // export const basePath='http://43.143.198.30:8086'
  24. axios.defaults.baseURL = basePath;
  25. // axios.defaults.baseURL = "http://43.143.198.30:8086";
  26. //设置端口
  27. // var urlCode ='http://152.136.24.101:8997';
  28. // var urlCode ='http://43.143.198.30:8085';
  29. // var urlCode ='https://child.hhnao.com';
  30. var urlCode = "";
  31. //接口白名单
  32. var whiteList = ["/user/blogin", "/user/getCode"];
  33. //数据是否要加密
  34. var isMI = false;
  35. // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8060/';
  36. // axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
  37. sessionStorage.setItem("codeImage", urlCode);
  38. axios.defaults.timeout = 30000; // 超时时间
  39. axios.defaults.headers.common["Content-Type"] =
  40. "application/JSON;charset=UTF-8";
  41. axios.defaults.withCredentials = true;
  42. // let loadingInstance
  43. axios.interceptors.request.use(
  44. function (config) {
  45. for (let i = 0; i < whiteList.length; i++) {
  46. if (whiteList[i] !== config.url) {
  47. ////不在白名单内,需要判断用户是否在,不在的话存储到登录页
  48. if (oSessionStorage.getItem("userInfo") == "") {
  49. router.replace({
  50. path: "/",
  51. // query: {redirect: router.currentRoute.fullPath}
  52. });
  53. }
  54. //在此判断需要 判断用户缓存是否存在
  55. // break
  56. }
  57. }
  58. //发送请求前判断token是存在
  59. // 在发送请求之前做些什么
  60. // loadingInstance = Loading.service({
  61. // lock: true,
  62. // text: '加载中',
  63. // spinner: 'el-icon-loading',
  64. // background: 'rgba(0, 0, 0, 0.7)'
  65. // });
  66. // eslint-disable-next-line
  67. // const userId = sessionStorage.getItem("userId");
  68. // config.headers['type'] = process.env.VUE_APP_VERSION;
  69. // if (userId) {
  70. // if (sessionStorage.getItem(`${userId}token`)) {
  71. // config.headers.Authorization = `Bearer ${sessionStorage.getItem(`${userId}token`)}`;
  72. // }
  73. // }
  74. config.headers.Authorization = oSessionStorage.getItem("token")
  75. ? `Bearer ${oSessionStorage.getItem("token")}`
  76. : "";
  77. // console.log(config.headers.Authorization)
  78. return config;
  79. },
  80. function (error) {
  81. // 对请求错误做些什么
  82. return Promise.reject(error);
  83. }
  84. );
  85. // 添加响应拦截器
  86. axios.interceptors.response.use(
  87. function (response) {
  88. // 对响应数据做点什么
  89. // loadingInstance.close();
  90. //返回code若为401,则表示token有问题
  91. if (response.data.code === 401) {
  92. // 重定向到登录页
  93. router.replace({
  94. path: "/",
  95. // query: {redirect: router.currentRoute.fullPath}
  96. });
  97. // Toast.fail(response.data.msg);
  98. Message.error(response.data.msg);
  99. }
  100. return response;
  101. },
  102. function (error) {
  103. // 对响应错误做点什么
  104. // loadingInstance.close();
  105. return Promise.reject(error);
  106. }
  107. );
  108. const http = {
  109. get: (url, data, sCallBack) => {
  110. if (data) {
  111. if (isMI) {
  112. data = Encrypt(JSON.stringify(data));
  113. } else {
  114. data = JSON.stringify(data);
  115. }
  116. }
  117. axios.get(url, { params: data }).then((res) => {
  118. // res.data = JSON.parse(Decrypt(res.data));
  119. if (isMI) {
  120. res.data = JSON.parse(Decrypt(res.data));
  121. } else {
  122. // res.data = JSON.parse(res.data);
  123. }
  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. postDown: (url, data, sCallBack) => {
  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({
  176. method: "post",
  177. url: url,
  178. responseType: "blob",
  179. params: data,
  180. headers: {
  181. // Accept: "application/octet-stream",
  182. "Content-Disposition": "attachment",
  183. },
  184. }).then((res) => {
  185. sCallBack(res);
  186. });
  187. },
  188. post: (url, data, sucessCallBack, errCallBack) => {
  189. if (data) {
  190. if (isMI) {
  191. data = Encrypt(JSON.stringify(data));
  192. } else {
  193. // data = JSON.stringify(data);
  194. // data = data;
  195. }
  196. }
  197. axios.post(url, data ).then((res) => {
  198. // res.status
  199. if (res) {
  200. if (isMI) {
  201. res.data = JSON.parse(Decrypt(res.data));
  202. }else{
  203. // res.data = JSON.parse(res.data);
  204. }
  205. }
  206. if (res.status >= 200 && res.status < 300) {
  207. if (sucessCallBack) {
  208. sucessCallBack(res.data);
  209. }
  210. } else if (res.status === 401) {
  211. //token过期了
  212. } else {
  213. if (errCallBack) {
  214. errCallBack(res.data);
  215. } else {
  216. // Toast(res.data.msg);
  217. Message.error(res.data.msg);
  218. }
  219. }
  220. });
  221. },
  222. delete: (url, data, sucessCallBack, errCallBack) => {
  223. if (data) {
  224. if(isMI){
  225. data = Encrypt(JSON.stringify(data));
  226. }else{
  227. data = JSON.stringify(data);
  228. }
  229. }
  230. axios.delete(url, { data: data }).then((res) => {
  231. // res.status
  232. if (res) {
  233. if(isMI){
  234. res.data = JSON.parse(Decrypt(res.data));
  235. }else{
  236. res.data = JSON.parse(res.data);
  237. }
  238. }
  239. if (res.status >= 200 && res.status < 300) {
  240. if (sucessCallBack) {
  241. sucessCallBack(res.data);
  242. }
  243. } else if (res.status === 401) {
  244. //token过期了
  245. } else {
  246. if (errCallBack) {
  247. errCallBack(res.data);
  248. } else {
  249. // Toast(res.data.msg);
  250. Message.error(res.data.msg);
  251. }
  252. }
  253. // if (res.data.code == 200) {
  254. // if (sucessCallBack) {
  255. // sucessCallBack(res.data);
  256. // }
  257. // } else if(res.data.code == 2001){
  258. // if (sucessCallBack) {
  259. // sucessCallBack(res.data);
  260. // }
  261. // }else {
  262. // if (errCallBack) {
  263. // errCallBack(res.data);
  264. // } else {
  265. // // Toast(res.data.msg);
  266. // Message.error(res.data.msg);
  267. // }
  268. // }
  269. });
  270. },
  271. };
  272. export default http;