http.js 8.8 KB

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