Showing
1 changed file
with
96 additions
and
91 deletions
| 1 | -import axios from 'axios' | 1 | +import axios from "axios"; |
| 2 | -import { Message, MessageBox } from 'element-ui' | 2 | +import { Message, MessageBox } from "element-ui"; |
| 3 | -import store from '@/store' | 3 | +import store from "@/store"; |
| 4 | -import { getToken, setToken } from '@/utils/auth' | 4 | +import { getToken, setToken } from "@/utils/auth"; |
| 5 | -import errorCode from '@/utils/errorCode' | 5 | +import errorCode from "@/utils/errorCode"; |
| 6 | -import settings from "@/settings" | 6 | +import settings from "@/settings"; |
| 7 | 7 | ||
| 8 | -axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' | 8 | +axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8"; |
| 9 | // 创建axios实例 | 9 | // 创建axios实例 |
| 10 | const service = axios.create({ | 10 | const service = axios.create({ |
| 11 | // axios中请求配置有baseURL选项,表示请求URL公共部分 | 11 | // axios中请求配置有baseURL选项,表示请求URL公共部分 |
| 12 | - baseURL: settings.baseURL, | 12 | + baseURL: settings.baseURL, //接口地址 |
| 13 | // 超时 | 13 | // 超时 |
| 14 | - timeout: 90000, | 14 | + timeout: 30000, |
| 15 | -}) | 15 | +}); |
| 16 | - | ||
| 17 | // request拦截器 | 16 | // request拦截器 |
| 18 | -service.interceptors.request.use(config => { | 17 | +service.interceptors.request.use( |
| 19 | - // 是否需要设置 token | 18 | + (config) => { |
| 20 | - const isToken = (config.headers || {}).isToken === false | 19 | + // 是否需要设置 token |
| 21 | - if (getToken() && !isToken) { | 20 | + const isToken = (config.headers || {}).isToken === false; |
| 22 | - config.headers['Authorization'] = decodeURIComponent(getToken()) // 让每个请求携带自定义token 请根据实际情况自行修改 | 21 | + if (getToken() && !isToken) { |
| 23 | - } | 22 | + config.headers["Authorization"] = getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改 |
| 24 | - // get请求映射params参数 | 23 | + } |
| 25 | - if (config.method === 'get' && config.params) { | 24 | + // get请求映射params参数 |
| 26 | - let url = config.url + '?'; | 25 | + if (config.method === "get" && config.params) { |
| 27 | - for (const propName of Object.keys(config.params)) { | 26 | + let url = config.url + "?"; |
| 28 | - const value = config.params[propName]; | 27 | + for (const propName of Object.keys(config.params)) { |
| 29 | - var part = encodeURIComponent(propName) + "="; | 28 | + const value = config.params[propName]; |
| 30 | - if (value !== null && typeof (value) !== "undefined") { | 29 | + var part = encodeURIComponent(propName) + "="; |
| 31 | - if (typeof value === 'object') { | 30 | + if (value !== null && typeof value !== "undefined") { |
| 32 | - for (const key of Object.keys(value)) { | 31 | + if (typeof value === "object") { |
| 33 | - let params = propName + '[' + key + ']'; | 32 | + for (const key of Object.keys(value)) { |
| 34 | - var subPart = encodeURIComponent(params) + "="; | 33 | + let params = propName + "[" + key + "]"; |
| 35 | - url += subPart + encodeURIComponent(value[key]) + "&"; | 34 | + var subPart = encodeURIComponent(params) + "="; |
| 35 | + url += subPart + encodeURIComponent(value[key]) + "&"; | ||
| 36 | + } | ||
| 37 | + } else { | ||
| 38 | + url += part + encodeURIComponent(value) + "&"; | ||
| 36 | } | 39 | } |
| 37 | - } else { | ||
| 38 | - url += part + encodeURIComponent(value) + "&"; | ||
| 39 | } | 40 | } |
| 40 | } | 41 | } |
| 42 | + url = url.slice(0, -1); | ||
| 43 | + config.params = {}; | ||
| 44 | + config.url = url; | ||
| 41 | } | 45 | } |
| 42 | - url = url.slice(0, -1); | 46 | + return config; |
| 43 | - config.params = {}; | 47 | + }, |
| 44 | - config.url = url; | 48 | + (error) => { |
| 49 | + console.log(error); | ||
| 50 | + Promise.reject(error); | ||
| 45 | } | 51 | } |
| 46 | - return config | 52 | +); |
| 47 | -}, error => { | ||
| 48 | - console.log(error) | ||
| 49 | - Promise.reject(error) | ||
| 50 | -}) | ||
| 51 | 53 | ||
| 52 | // 响应拦截器 | 54 | // 响应拦截器 |
| 53 | -service.interceptors.response.use(res => { | 55 | +service.interceptors.response.use( |
| 54 | - if (res.headers['token']) { | 56 | + (res) => { |
| 55 | - setToken(res.headers['token']) | 57 | + if (res.headers["token"]) { |
| 56 | - } | 58 | + setToken(decodeURI(res.headers["token"])); |
| 57 | - // 未设置状态码则默认成功状态 | ||
| 58 | - const code = res.data.code || 200; | ||
| 59 | - // 获取错误信息 | ||
| 60 | - const msg = errorCode[code] || res.data.msg || errorCode['default'] | ||
| 61 | - if (code === 301) { | ||
| 62 | - MessageBox.confirm('登录状态已过期,需重新登录!msg:' + msg, '系统提示', { | ||
| 63 | - closeOnClickModal: false, | ||
| 64 | - showCancelButton: false, | ||
| 65 | - confirmButtonText: '重新登录', | ||
| 66 | - type: 'warning' | ||
| 67 | } | 59 | } |
| 68 | - ).then(() => { | 60 | + // 未设置状态码则默认成功状态 |
| 69 | - store.dispatch('LogOut').then(() => { | 61 | + const code = res.data.code || 200; |
| 70 | - // location.href = '/'; | 62 | + // 获取错误信息 |
| 71 | - window.location.href = settings.loginURL;//wsso系统跳转 | 63 | + const msg = errorCode[code] || res.data.msg || errorCode["default"]; |
| 72 | - }) | 64 | + if (code === 301) { |
| 73 | - }); | 65 | + MessageBox.confirm("登录状态已过期,需重新登录", "系统提示", { |
| 74 | - } else if (code === 403) { | 66 | + closeOnClickModal: false, |
| 75 | - //无权限页面 | 67 | + showCancelButton: false, |
| 76 | - location.href = '/401'; | 68 | + confirmButtonText: "重新登录", |
| 77 | - } | 69 | + type: "warning", |
| 78 | - else if (code === 500) { | 70 | + }).then(() => { |
| 79 | - Message({ | 71 | + store.dispatch("LogOut").then(() => { |
| 80 | - message: msg, | 72 | + location.href = settings.routerBase + "/index"; |
| 81 | - type: 'error' | 73 | + }); |
| 82 | - }) | 74 | + }); |
| 83 | - return Promise.reject(new Error(msg)) | 75 | + } else if (code === 403) { |
| 84 | - } else if (code !== 200 && code !== 201 && code !== 202 && code != 203 && code != 603 && code != 402) { | 76 | + //无权限页面 |
| 85 | - Message({ | 77 | + location.href = "/401"; |
| 86 | - message: msg, | 78 | + } else if (code === 500) { |
| 87 | - type: 'error' | 79 | + Message({ |
| 88 | - }) | 80 | + message: msg, |
| 89 | - return Promise.reject('error') | 81 | + type: "error", |
| 90 | - } else { | 82 | + }); |
| 91 | - return res.data | 83 | + return Promise.reject(new Error(msg)); |
| 92 | - } | 84 | + } else if ( |
| 93 | -}, | 85 | + code !== 200 && |
| 94 | - error => { | 86 | + code !== 201 && |
| 95 | - console.log('err' + error) | 87 | + code !== 202 && |
| 88 | + code != 203 && | ||
| 89 | + code != 603 && | ||
| 90 | + code != 402 | ||
| 91 | + ) { | ||
| 92 | + Message({ | ||
| 93 | + message: msg, | ||
| 94 | + type: "error", | ||
| 95 | + }); | ||
| 96 | + return Promise.reject("error"); | ||
| 97 | + } else { | ||
| 98 | + return res.data; | ||
| 99 | + } | ||
| 100 | + }, | ||
| 101 | + (error) => { | ||
| 102 | + console.log("err" + error); | ||
| 96 | let { message } = error; | 103 | let { message } = error; |
| 97 | if (message == "Network Error") { | 104 | if (message == "Network Error") { |
| 98 | message = "网络连接异常"; | 105 | message = "网络连接异常"; |
| 99 | - } | 106 | + } else if (message.includes("timeout")) { |
| 100 | - else if (message.includes("timeout")) { | ||
| 101 | message = "系统接口请求超时"; | 107 | message = "系统接口请求超时"; |
| 102 | - } | 108 | + } else if (message.includes("Request failed with status code")) { |
| 103 | - else if (message.includes("Request failed with status code")) { | ||
| 104 | message = "系统接口" + message.substr(message.length - 3) + "异常"; | 109 | message = "系统接口" + message.substr(message.length - 3) + "异常"; |
| 105 | } | 110 | } |
| 106 | Message({ | 111 | Message({ |
| 107 | message: message, | 112 | message: message, |
| 108 | - type: 'error', | 113 | + type: "error", |
| 109 | - duration: 5 * 1000 | 114 | + duration: 5 * 1000, |
| 110 | - }) | 115 | + }); |
| 111 | - return Promise.reject(error) | 116 | + return Promise.reject(error); |
| 112 | } | 117 | } |
| 113 | -) | 118 | +); |
| 114 | 119 | ||
| 115 | -export default service | 120 | +export default service; | ... | ... |
-
Please register or login to post a comment