import axios from 'axios' import store from '../store' import util from "./util"; let instance = axios.create({ timeout: 600000, withCredentials: true, }) instance.interceptors.request.use(config => { store.commit('initWaterMarker') if (!config.withoutToken && store.state.token) { config.headers["Authorization"] = store.state.token } return config }, err => { console.error(err) }) instance.interceptors.response.use(res => { if (res.data) { if (res.data.code) { if (res.data.code == 0) { return res.data } else if (res.data.code == 401) { store.commit("logout"); let reg = new RegExp('.*code=(.+$)', "g") if (reg.test(location.search)) { let code = location.search.replace(reg, '$1') store.commit('bindAccount', { code, then: res => { store.commit("login", [res?.token_type, res?.access_token].join(" ").trim()) location.href = location.href.replace('code=' + code, '') } }) } else util.confirm("用户信息验证失效,是否要重新登录?").then(() => { store.commit('redirectCode') // let app = store.state.apps?.find(e => location.href.indexOf(e.path) > -1) // const goto = path => { // location.href = location.origin + "/pages/loading?" + path // } // if (app) { // goto(location.search + `&app=${app.key}`) // } else { // goto(location.search + `#error`) // } }).catch(() => 0) } else { console.error(res.data.msg || "请求失败!") return Promise.reject(res.data.msg) } } else { return res.data } } else { console.error("服务器异常,请联系管理员!") } }, err => { console.error(err) }) export default instance