| | |
| | | // 创建 axios 实例 |
| | | const service = axios.create({ |
| | | baseURL: myConfig.requestCtx, |
| | | timeout: myConfig.requestTimeOut // 请求超时时间 |
| | | timeout: myConfig.requestTimeOut, // 请求超时时间 |
| | | }); |
| | | |
| | | // 请求拦截器 |
| | | service.interceptors.request.use( |
| | | config => { |
| | | (config) => { |
| | | // 在请求发送之前做一些处理 |
| | | // if (config.url !== "/api/account/loginByPassword") { |
| | | let token = toolClass.getCookie(myConfig.tokenKey); |
| | |
| | | // } |
| | | return config; |
| | | }, |
| | | error => { |
| | | (error) => { |
| | | // 发送失败 |
| | | Promise.reject(error); |
| | | } |
| | |
| | | |
| | | // 响应拦截器 |
| | | service.interceptors.response.use( |
| | | response => { |
| | | (response) => { |
| | | // dataAxios 是 axios 返回数据中的 data |
| | | const dataAxios = response.data; |
| | | |
| | | // 这个状态码是和后端约定的 |
| | | const { |
| | | success |
| | | } = dataAxios; |
| | | const { success } = dataAxios; |
| | | if (dataAxios.currentDate) { |
| | | sessionStorage.currentDate = new Date(dataAxios.currentDate).getTime(); |
| | | } |
| | | |
| | | // 根据 code 进行判断 |
| | | if (success) { |
| | | return dataAxios.data; |
| | |
| | | console.error(dataAxios.msg); |
| | | } |
| | | }, |
| | | error => { |
| | | (error) => { |
| | | if ((error.message = "Network Error")) { |
| | | let loginCode = WeChat.getUrlInfo("weChatLogin"); |
| | | if (loginCode.code) { |
| | | WeChat.login(loginCode.code, data => { |
| | | WeChat.login(loginCode.code, (data) => { |
| | | toolClass.setCookie(myConfig.tokenKey, data); |
| | | location.reload(); |
| | | }); |
| | | } else { |
| | | // toolClass.delCookie(myConfig.tokenKey); |
| | | // WeChat.getCode("weChatLogin"); |
| | | toolClass.delCookie(myConfig.tokenKey); |
| | | WeChat.getCode("weChatLogin"); |
| | | } |
| | | } else { |
| | | if (error.response && error.response.data && error.response.data.error) { |