http.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // export const basePath='http://10.113.248.3:8086'
  19. export const basePath='http://43.143.198.30:8086'
  20. axios.defaults.baseURL = basePath;
  21. // axios.defaults.baseURL = "http://43.143.198.30:8086";
  22. //设置端口
  23. // var urlCode ='http://152.136.24.101:8997';
  24. // var urlCode ='http://43.143.198.30:8085';
  25. // var urlCode ='https://child.hhnao.com';
  26. var urlCode = "";
  27. //接口白名单
  28. var whiteList=[
  29. '/user/blogin','/user/getCode'
  30. ]
  31. // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8060/';
  32. // axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
  33. sessionStorage.setItem("codeImage", urlCode);
  34. axios.defaults.timeout = 30000; // 超时时间
  35. axios.defaults.headers.common["Content-Type"] =
  36. "application/JSON;charset=UTF-8";
  37. axios.defaults.withCredentials = true;
  38. // let loadingInstance
  39. axios.interceptors.request.use(
  40. function (config) {
  41. for(let i=0;i<whiteList.length;i++){
  42. if(whiteList[i]!==config.url){
  43. ////不在白名单内,需要判断用户是否在,不在的话存储到登录页
  44. if(oSessionStorage.getItem("userInfo")==''){
  45. router.replace({
  46. path: "/",
  47. // query: {redirect: router.currentRoute.fullPath}
  48. });
  49. }
  50. //在此判断需要 判断用户缓存是否存在
  51. // break
  52. }
  53. }
  54. //发送请求前判断token是存在
  55. // 在发送请求之前做些什么
  56. // loadingInstance = Loading.service({
  57. // lock: true,
  58. // text: '加载中',
  59. // spinner: 'el-icon-loading',
  60. // background: 'rgba(0, 0, 0, 0.7)'
  61. // });
  62. // eslint-disable-next-line
  63. // const userId = sessionStorage.getItem("userId");
  64. // config.headers['type'] = process.env.VUE_APP_VERSION;
  65. // if (userId) {
  66. // if (sessionStorage.getItem(`${userId}token`)) {
  67. // config.headers.Authorization = `Bearer ${sessionStorage.getItem(`${userId}token`)}`;
  68. // }
  69. // }
  70. config.headers.Authorization = oSessionStorage.getItem("token")
  71. ? `Bearer ${oSessionStorage.getItem("token")}`
  72. : "";
  73. console.log(config.headers.Authorization)
  74. return config;
  75. },
  76. function (error) {
  77. // 对请求错误做些什么
  78. return Promise.reject(error);
  79. }
  80. );
  81. // 添加响应拦截器
  82. axios.interceptors.response.use(
  83. function (response) {
  84. // 对响应数据做点什么
  85. // loadingInstance.close();
  86. //返回code若为401,则表示token有问题
  87. if (response.data.code === 401) {
  88. // 重定向到登录页
  89. router.replace({
  90. path: "/",
  91. // query: {redirect: router.currentRoute.fullPath}
  92. });
  93. // Toast.fail(response.data.msg);
  94. Message.error(response.data.msg);
  95. }
  96. return response;
  97. },
  98. function (error) {
  99. // 对响应错误做点什么
  100. // loadingInstance.close();
  101. return Promise.reject(error);
  102. }
  103. );
  104. const http = {
  105. get: (url, data, sCallBack) => {
  106. if (data) {
  107. data = Encrypt(JSON.stringify(data));
  108. }
  109. axios.get(url, { params: data }).then((res) => {
  110. res.data = JSON.parse(Decrypt(res.data));
  111. if (sCallBack) {
  112. sCallBack(res.data);
  113. }
  114. });
  115. },
  116. getImg: (url, data, sCallBack) => {
  117. if (data) {
  118. data = Encrypt(JSON.stringify(data));
  119. }
  120. axios({
  121. method: "get",
  122. url: url,
  123. responseType: "blob",
  124. params: data,
  125. headers: {
  126. Accept: "application/octet-stream",
  127. // "Content-Disposition":"attachment"
  128. },
  129. },{responseType:"arraybuffer"}).then((res) => {
  130. sCallBack(res);
  131. });
  132. },
  133. getDown: (url, data, sCallBack) => {
  134. if (data) {
  135. data = Encrypt(JSON.stringify(data));
  136. }
  137. axios({
  138. method: "get",
  139. url: url,
  140. responseType: "blob",
  141. params: data,
  142. headers: {
  143. // Accept: "application/octet-stream",
  144. "Content-Disposition": "attachment"
  145. },
  146. }).then((res) => {
  147. sCallBack(res);
  148. });
  149. },
  150. post: (url, data, sucessCallBack, errCallBack) => {
  151. if (data) {
  152. data = Encrypt(JSON.stringify(data));
  153. }
  154. axios.post(url, { data: data }).then((res) => {
  155. // res.status
  156. if (res) {
  157. res.data = JSON.parse(Decrypt(res.data));
  158. }
  159. if (res.status >= 200 && res.status < 300) {
  160. if (sucessCallBack) {
  161. sucessCallBack(res.data);
  162. }
  163. } else if (res.status === 401) {
  164. //token过期了
  165. } else {
  166. if (errCallBack) {
  167. errCallBack(res.data);
  168. } else {
  169. // Toast(res.data.msg);
  170. Message.error(res.data.msg);
  171. }
  172. }
  173. });
  174. },
  175. delete: (url, data, sucessCallBack, errCallBack) => {
  176. if (data) {
  177. data = Encrypt(JSON.stringify(data));
  178. }
  179. axios.delete(url, { data: data }).then((res) => {
  180. // res.status
  181. if (res) {
  182. res.data = JSON.parse(Decrypt(res.data));
  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. // if (res.data.code == 200) {
  199. // if (sucessCallBack) {
  200. // sucessCallBack(res.data);
  201. // }
  202. // } else if(res.data.code == 2001){
  203. // if (sucessCallBack) {
  204. // sucessCallBack(res.data);
  205. // }
  206. // }else {
  207. // if (errCallBack) {
  208. // errCallBack(res.data);
  209. // } else {
  210. // // Toast(res.data.msg);
  211. // Message.error(res.data.msg);
  212. // }
  213. // }
  214. });
  215. },
  216. };
  217. export default http;