http.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // 少锋本地
  20. // export const basePath = "http://10.113.248.4:8089";
  21. //少锋本地 wifi
  22. // export const basePath = "http://172.28.50.2:8089";
  23. //30
  24. // export const basePath = "http://43.143.198.30:8089";
  25. //红朵服务器
  26. // export const basePath = "http://129.211.221.44:8089";
  27. //锦泓税务服务器
  28. export const basePath = "http://146.56.226.174:8089";
  29. // const base_url = 'http://43.143.198.30:8089/'
  30. // const base_url = 'http://10.113.248.4:8090/'
  31. // export const basePath='http://43.143.198.30:8086'
  32. axios.defaults.baseURL = basePath;
  33. // axios.defaults.baseURL = "http://43.143.198.30:8086";
  34. //设置端口
  35. // var urlCode ='http://152.136.24.101:8997';
  36. // var urlCode ='http://43.143.198.30:8085';
  37. // var urlCode ='https://child.hhnao.com';
  38. var urlCode = "";
  39. let time;
  40. //接口白名单
  41. var whiteList = ["/user/blogin", "/user/getCode"];
  42. //数据是否要加密
  43. var isMI = false;
  44. // axios.defaults.baseURL = 'http://cognitive.wistcm.com:8060/';
  45. // axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
  46. sessionStorage.setItem("codeImage", urlCode);
  47. axios.defaults.timeout = 30000000; // 超时时间
  48. axios.defaults.headers.common["Content-Type"] =
  49. "application/JSON;charset=UTF-8";
  50. axios.defaults.withCredentials = true;
  51. // let loadingInstance
  52. var noTime = false;
  53. axios.interceptors.request.use(
  54. function (config) {
  55. for (let i = 0; i < whiteList.length; i++) {
  56. if (whiteList[i] !== config.url) {
  57. ////不在白名单内,需要判断用户是否在,不在的话存储到登录页
  58. if (oSessionStorage.getItem("userInfo") == "") {
  59. router.replace({
  60. path: "/",
  61. // query: {redirect: router.currentRoute.fullPath}
  62. });
  63. }
  64. //在此判断需要 判断用户缓存是否存在
  65. // break
  66. }
  67. }
  68. //发送请求前判断token是存在
  69. // 在发送请求之前做些什么
  70. // loadingInstance = Loading.service({
  71. // lock: true,
  72. // text: '加载中',
  73. // spinner: 'el-icon-loading',
  74. // background: 'rgba(0, 0, 0, 0.7)'
  75. // });
  76. // eslint-disable-next-line
  77. // const userId = sessionStorage.getItem("userId");
  78. // config.headers['type'] = process.env.VUE_APP_VERSION;
  79. // if (userId) {
  80. // if (sessionStorage.getItem(`${userId}token`)) {
  81. // config.headers.Authorization = `Bearer ${sessionStorage.getItem(`${userId}token`)}`;
  82. // }
  83. // }
  84. config.headers.Authorization = oSessionStorage.getItem("token")
  85. ? `Bearer ${oSessionStorage.getItem("token")}`
  86. : "";
  87. // console.log(config.headers.Authorization)
  88. return config;
  89. },
  90. function (error) {
  91. // 对请求错误做些什么
  92. return Promise.reject(error);
  93. }
  94. );
  95. // 添加响应拦截器
  96. axios.interceptors.response.use(
  97. function (response) {
  98. // 对响应数据做点什么
  99. // loadingInstance.close();
  100. //返回code若为401,则表示token有问题
  101. if (response.data.code === 401) {
  102. //当401时清除缓存信息
  103. if (oSessionStorage.getItem("userInfo") !== "") {
  104. oSessionStorage.removeItem("userInfo");
  105. // Message.error(response.data.msg);
  106. //搞个节流函数
  107. let flagT = false;
  108. clearTimeout(time);
  109. time = setTimeout(() => {
  110. flagT = true;
  111. if (flagT) {
  112. Message.error(response.data.msg);
  113. }
  114. }, 500);
  115. // alert('1111')
  116. }
  117. // 重定向到登录页
  118. router.replace({
  119. path: "/",
  120. // query: {redirect: router.currentRoute.fullPath}
  121. });
  122. return;
  123. // Toast.fail(response.data.msg);
  124. // if(){
  125. // }
  126. }
  127. return response;
  128. },
  129. function (error) {
  130. // 对响应错误做点什么
  131. // loadingInstance.close();
  132. return Promise.reject(error);
  133. }
  134. );
  135. const http = {
  136. get: (url, data, sCallBack) => {
  137. if (data) {
  138. if (isMI) {
  139. data = Encrypt(JSON.stringify(data));
  140. } else {
  141. data = JSON.stringify(data);
  142. }
  143. }
  144. axios.get(url, { params: data }).then((res) => {
  145. // res.data = JSON.parse(Decrypt(res.data));
  146. if (isMI) {
  147. res.data = JSON.parse(Decrypt(res.data));
  148. } else {
  149. // res.data = JSON.parse(res.data);
  150. }
  151. if (sCallBack) {
  152. sCallBack(res.data);
  153. }
  154. });
  155. },
  156. getImg: (url, data, sCallBack) => {
  157. if (data) {
  158. data = Encrypt(JSON.stringify(data));
  159. }
  160. axios(
  161. {
  162. method: "get",
  163. url: url,
  164. responseType: "blob",
  165. params: data,
  166. headers: {
  167. Accept: "application/octet-stream",
  168. // "Content-Disposition":"attachment"
  169. },
  170. },
  171. {
  172. responseType: "arraybuffer",
  173. }
  174. ).then((res) => {
  175. sCallBack(res);
  176. });
  177. },
  178. getDown: (url, data, sCallBack) => {
  179. if (data) {
  180. data = Encrypt(JSON.stringify(data));
  181. }
  182. axios({
  183. method: "get",
  184. url: url,
  185. responseType: "blob",
  186. params: data,
  187. headers: {
  188. // Accept: "application/octet-stream",
  189. "Content-Disposition": "attachment",
  190. },
  191. }).then((res) => {
  192. sCallBack(res);
  193. });
  194. },
  195. postDown: (url, data, sCallBack) => {
  196. if (data) {
  197. if (isMI) {
  198. data = Encrypt(JSON.stringify(data));
  199. } else {
  200. // data = JSON.stringify(data);
  201. // data = data;
  202. }
  203. }
  204. axios({
  205. method: "post",
  206. url: url,
  207. responseType: "blob",
  208. params: data,
  209. headers: {
  210. // Accept: "application/octet-stream",
  211. "Content-Disposition": "attachment",
  212. },
  213. }).then((res) => {
  214. sCallBack(res);
  215. });
  216. },
  217. post: (url, data, sucessCallBack, errCallBack) => {
  218. if (data) {
  219. if (isMI) {
  220. data = Encrypt(JSON.stringify(data));
  221. } else {
  222. // data = JSON.stringify(data);
  223. // data = data;
  224. }
  225. }
  226. axios.post(url, data).then((res) => {
  227. // res.status
  228. if (res) {
  229. if (isMI) {
  230. res.data = JSON.parse(Decrypt(res.data));
  231. } else {
  232. // res.data = JSON.parse(res.data);
  233. }
  234. }
  235. if (res.status >= 200 && res.status < 300) {
  236. if (sucessCallBack) {
  237. sucessCallBack(res.data);
  238. }
  239. } else if (res.status === 401) {
  240. //token过期了
  241. } else {
  242. if (errCallBack) {
  243. errCallBack(res.data);
  244. } else {
  245. // Toast(res.data.msg);
  246. Message.error(res.data.msg);
  247. }
  248. }
  249. });
  250. },
  251. delete: (url, data, sucessCallBack, errCallBack) => {
  252. if (data) {
  253. if (isMI) {
  254. data = Encrypt(JSON.stringify(data));
  255. } else {
  256. data = JSON.stringify(data);
  257. }
  258. }
  259. axios.delete(url, { data: data }).then((res) => {
  260. // res.status
  261. if (res) {
  262. if (isMI) {
  263. res.data = JSON.parse(Decrypt(res.data));
  264. } else {
  265. res.data = JSON.parse(res.data);
  266. }
  267. }
  268. if (res.status >= 200 && res.status < 300) {
  269. if (sucessCallBack) {
  270. sucessCallBack(res.data);
  271. }
  272. } else if (res.status === 401) {
  273. //token过期了
  274. } else {
  275. if (errCallBack) {
  276. errCallBack(res.data);
  277. } else {
  278. // Toast(res.data.msg);
  279. Message.error(res.data.msg);
  280. }
  281. }
  282. // if (res.data.code == 200) {
  283. // if (sucessCallBack) {
  284. // sucessCallBack(res.data);
  285. // }
  286. // } else if(res.data.code == 2001){
  287. // if (sucessCallBack) {
  288. // sucessCallBack(res.data);
  289. // }
  290. // }else {
  291. // if (errCallBack) {
  292. // errCallBack(res.data);
  293. // } else {
  294. // // Toast(res.data.msg);
  295. // Message.error(res.data.msg);
  296. // }
  297. // }
  298. });
  299. },
  300. };
  301. export default http;