| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import axios from "axios"
- import XEUtils from "xe-utils"
- import sysConfig from "@/config"
- import tool from "@/utils/tool"
- import router from "@/router"
- axios.defaults.baseURL = ""
- axios.defaults.timeout = sysConfig.TIMEOUT
- // HTTP request 拦截器
- axios.interceptors.request.use(
- (config) => {
- let token = tool.cookie.get("MES_TOKEN")
-
- if (token && XEUtils.startsWith(token, sysConfig.TOKEN_PREFIX)) {
- config.headers[sysConfig.TOKEN_NAME] = token
- }
- if (!sysConfig.REQUEST_CACHE && config.method == "get") {
- config.params = config.params || {}
- config.params["_"] = new Date().getTime()
- }
- Object.assign(config.headers, sysConfig.HEADERS)
- return config
- },
- (error) => {
- return Promise.reject(error)
- }
- )
- //FIX 多个API同时401时疯狂弹窗BUG
- let MessageBox_401_show = false
- // HTTP response 拦截器
- axios.interceptors.response.use(
- (response) => response,
- (error) => {
- if (error.response) {
- if (error.response.status == 404) {
- ElNotification.error({
- title: "请求错误",
- message: "Status:404,正在请求不存在的服务器记录!"
- })
- } else if (error.response.status == 503) {
- console.log(error.response.data.message)
- } else if (error.response.status == 401) {
- if (!MessageBox_401_show) {
- MessageBox_401_show = true
- ElMessageBox.confirm("当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。", "无权限访问", {
- customStyle: { "--el-messagebox-width": "480px" },
- type: "error",
- center: true,
- showClose: false,
- closeOnPressEscape: false,
- closeOnClickModal: false,
- confirmButtonText: "重新登录",
- showCancelButton: false,
- beforeClose: (action, instance, done) => {
- MessageBox_401_show = false
- done()
- }
- }).then(() => {
- tool.cookie.remove("MES_TOKEN")
- tool.data.remove("USER_INFO")
- router.replace({ path: "/login" })
- }).catch(() => {})
- }
- } else {
- ElNotification.error({
- title: "请求错误",
- message: error.response.data.message || `Status:${error.response.status},未知错误!`
- })
- }
- return Promise.reject(error.response)
- } else {
- ElNotification.error({
- title: "请求错误",
- message: "请求服务器无响应!"
- })
- return Promise.reject(error)
- }
- }
- )
- var http = {
- /** get 请求
- * @param {string} url 接口地址
- * @param {object} params 请求参数
- * @param {object} config 参数
- */
- get: function (url, params = {}, config = {}) {
- return new Promise((resolve, reject) => {
- axios({
- url,
- method: "get",
- params,
- ...config
- }).then(response => {
- resolve(response.data)
- }).catch(error => {
- reject(error)
- })
- })
- },
- /** post 请求
- * @param {string} url 接口地址
- * @param {object} data 请求参数
- * @param {object} config 参数
- * @param {object} params 路径请求参数
- */
- post: function (url, data = {}, config = {}, params = {}) {
- return new Promise((resolve, reject) => {
- let emptyField = []
- if (!XEUtils.isFormData(data)) {
- emptyField = XEUtils.keys(XEUtils.pick(data, val => val === "" || XEUtils.isNull(val) || XEUtils.isUndefined(val)))
-
- // 更新接口 置空
- url.endsWith("/update") && XEUtils.set(data, "emptyField", emptyField)
- }
-
- axios({
- url,
- method: "post",
- data: emptyField.length ? XEUtils.omit(data, emptyField) : data,
- ...config,
- params
- }).then(response => {
- resolve(response.data)
- }).catch(error => {
- reject(error)
- })
- })
- }
- }
- export default http
|