| | |
| | | import myConfig from '@/assets/js/config' |
| | | import toolClass from '@/assets/js/toolClass.js' |
| | | import router from '@/router' |
| | | |
| | | // 创建 axios 实例 |
| | | const service = axios.create({ |
| | | baseURL: myConfig.requestCtx, |
| | | baseURL: myConfig.aiCtx, |
| | | timeout: myConfig.requestTimeOut, // 请求超时时间 |
| | | }) |
| | | |
| | |
| | | }, |
| | | ) |
| | | |
| | | // 响应拦截器 |
| | | service.interceptors.response.use( |
| | | (response) => { |
| | | // dataAxios 是 axios 返回数据中的 data |
| | | const dataAxios = response.data |
| | | if (typeof dataAxios.data === 'boolean') { |
| | | return dataAxios.data |
| | | } |
| | | if (response.config.responseType == 'blob') { |
| | | return dataAxios |
| | | } |
| | | const { success } = dataAxios |
| | | if (dataAxios.currentDate) { |
| | | sessionStorage.currentDate = new Date(dataAxios.currentDate).getTime() |
| | | } |
| | | // 根据 code 进行判断 |
| | | if (success) { |
| | | return dataAxios.data ? dataAxios.data : dataAxios |
| | | } else { |
| | | // 提示错误 |
| | | } |
| | | // 请求拦截器 |
| | | // app-6MPX5Fr97eK6BWHpy8g7vddd |
| | | // app-u5c1CREHe0IWw3vC0wW8j5Ys // 中药饮片 |
| | | // app-pLuWNhXPmI1uZSLmWw74Q05F // 小分子库 |
| | | service.interceptors.request.use( |
| | | (config) => { |
| | | const token = 'app-pLuWNhXPmI1uZSLmWw74Q05F' |
| | | if (token) config.headers['Authorization'] = `Bearer ${token}` |
| | | config.headers['Content-Type'] = 'application/json;charset=utf-8' |
| | | return config |
| | | }, |
| | | (error) => { |
| | | if ((error.response && error.response.status == 401) || error.code == 'ERR_NETWORK') { |
| | | localStorage.removeItem(myConfig.tokenKey) |
| | | localStorage.removeItem('jesk-userInfo') |
| | | localStorage.removeItem('alreadyElectronicBook') |
| | | localStorage.removeItem('alreadyPaperBook') |
| | | localStorage.removeItem('electronicBookList') |
| | | localStorage.removeItem('paperBookList') |
| | | sessionStorage.removeItem('cartNumber') |
| | | // router.replace({ |
| | | // path: '/home', |
| | | // query: { |
| | | // showLogin: '1' |
| | | // } |
| | | // }) |
| | | const url = window.location.hash.slice(1) |
| | | console.log(url) |
| | | if (url.includes('showLogin=1')) { |
| | | router.push(url) |
| | | } else { |
| | | if (url.includes('?')) { |
| | | router.push(url + '&showLogin=1') |
| | | } else { |
| | | router.push(url + '?showLogin=1') |
| | | } |
| | | } |
| | | } else { |
| | | if (error.response && error.response.data && error.response.data.error) { |
| | | console.error(error.response.data.error.msg) |
| | | } else { |
| | | console.error('请求发生错误') |
| | | } |
| | | } |
| | | return Promise.reject(error) |
| | | // 发送失败 |
| | | Promise.reject(error) |
| | | }, |
| | | ) |
| | | |
| | | // 响应拦截器 |
| | | // service.interceptors.response.use( |
| | | // (response) => { |
| | | // // dataAxios 是 axios 返回数据中的 data |
| | | // const dataAxios = response.data |
| | | // if (typeof dataAxios.data === 'boolean') { |
| | | // return dataAxios.data |
| | | // } |
| | | // if (response.config.responseType == 'blob') { |
| | | // return dataAxios |
| | | // } |
| | | // const { success } = dataAxios |
| | | // if (dataAxios.currentDate) { |
| | | // sessionStorage.currentDate = new Date(dataAxios.currentDate).getTime() |
| | | // } |
| | | // // 根据 code 进行判断 |
| | | // if (success) { |
| | | // return dataAxios.data ? dataAxios.data : dataAxios |
| | | // } else { |
| | | // // 提示错误 |
| | | // } |
| | | // }, |
| | | // (error) => { |
| | | // if ((error.response && error.response.status == 401) || error.code == 'ERR_NETWORK') { |
| | | // localStorage.removeItem(myConfig.tokenKey) |
| | | // localStorage.removeItem('jesk-userInfo') |
| | | // localStorage.removeItem('alreadyElectronicBook') |
| | | // localStorage.removeItem('alreadyPaperBook') |
| | | // localStorage.removeItem('electronicBookList') |
| | | // localStorage.removeItem('paperBookList') |
| | | // sessionStorage.removeItem('cartNumber') |
| | | // // router.replace({ |
| | | // // path: '/home', |
| | | // // query: { |
| | | // // showLogin: '1' |
| | | // // } |
| | | // // }) |
| | | // const url = window.location.hash.slice(1) |
| | | // console.log(url) |
| | | // if (url.includes('showLogin=1')) { |
| | | // router.push(url) |
| | | // } else { |
| | | // if (url.includes('?')) { |
| | | // router.push(url + '&showLogin=1') |
| | | // } else { |
| | | // router.push(url + '?showLogin=1') |
| | | // } |
| | | // } |
| | | // } else { |
| | | // if (error.response && error.response.data && error.response.data.error) { |
| | | // console.error(error.response.data.error.msg) |
| | | // } else { |
| | | // console.error('请求发生错误') |
| | | // } |
| | | // } |
| | | // return Promise.reject(error) |
| | | // }, |
| | | // ) |
| | | |
| | | export default service |