erp-el-element/utils/request.js

190 lines
6.0 KiB
JavaScript
Raw Normal View History

2024-05-07 11:30:13 +08:00
/*
* @Author: your name
* @Date: 2020-09-08 18:06:28
* @LastEditTime: 2023-03-06 14:54:24
* @LastEditors: ckycjwds 1240973496@qq.com
* @Description: In User Settings Edit
* @FilePath: \aiguo_erp_vue\src\utils\request.js
*/
import axios from 'axios'
import { MessageBox, Message } from 'element-ui';
2024-05-07 11:51:58 +08:00
// import cancelClass from './cancelToken'
2024-05-07 11:30:13 +08:00
const service = axios.create({
2024-05-07 11:57:19 +08:00
baseURL: window.VUE_APP_BASE_API?window.VUE_APP_BASE_API:"http://dev-erp-api.xtkj99.com:7890", // api 的 base_url
2024-05-07 11:30:13 +08:00
timeout: 1000 * 60 * 2 // 接口超时
})
const pendingAjax = []
const fastClickMsg = '数据请求中,请稍后'
const CancelToken = axios.CancelToken;
let lastcancel=null;
const removePendingAjax = (config, type, c) => {
if (type.type == 1) {
if (config.data) {
config.data = JSON.stringify(config.data)
}
}
let url = config.url
if (!!config.data && config.data != '{}') {
url = config.url + '_' + config.data
}
// 数据请求中白名单
const whitelist = ['get_class_account_list', 'cate_brand_list_all', 'ai_model_v2', 'res_advanced_search', 'qrcode', 'account_class_list', 'search_accessories', 'zz_new_status', 'get_unread_msg', 'third_party_no', 'get_ws_auth_code', 'get_drafts_box', 'manual_pull_datas', 'get_unopened_module', 'sales_performance_detail', 'goal_warehouse', 'chx_product_details', 'get_oss_sign_aliyun']
const index = pendingAjax.findIndex(i => i === url)
const white = whitelist.some(res => {
if (url.indexOf(res) != -1) {
return true
}
})
if (white) { // 白名单内的接口可以重复请求
pendingAjax.splice(index, 1)
} else {
if (index > -1) {
c ? c(fastClickMsg + url) : pendingAjax.splice(index, 1)
} else {
c && pendingAjax.push(url)
}
}
}
// 请求拦截
service.interceptors.request.use(
config => {
// if (navigator.onLine) { //检测网络是否正常
let token=localStorage.getItem("token");
if (token) {
2024-05-07 11:51:58 +08:00
config.headers['token'] = token;
2024-05-07 11:30:13 +08:00
}
if(!lastcancel){
lastcancel= new CancelToken(c => {
const copyconfig = JSON.parse(JSON.stringify(config))
removePendingAjax(copyconfig, { type: 1 }, c)
});
}
config.cancelToken=lastcancel;
// config.cancelToken = new CancelToken(c => {
// const copyconfig = JSON.parse(JSON.stringify(config))
// removePendingAjax(copyconfig, { type: 1 }, c)
// })
return config
// }else{
// Message({
// duration:4000,
// message: '请检查网络状态',
// type: 'warning',
// showClose:true,
// })
// return
// }
},
error => {
return Promise.reject(error)
}
)
// 响应拦截
service.interceptors.response.use(
async response => {
const res = response.data
const copyconfig = JSON.parse(JSON.stringify(response.config))
copyconfig.url = response.config.url.replace(copyconfig.baseURL, '')
if (typeof (copyconfig.data) === undefined || copyconfig.data == {} || copyconfig.data == '' || typeof (copyconfig.data) === 'object') {
copyconfig.data = ''
}
removePendingAjax(copyconfig, { type: 2 })
lastcancel=null;
if (res.errcode == 1012) {
return res
} else if (res.errcode == 0 && res.msg != '' && res.msg != '成功' && res.msg != '获取成功') {
Message({
duration: 4000,
message: res.msg,
type: 'success',
showClose: true
})
return res
} else if (res.errcode == 105) { // 数据请求过快
Message({
duration: 1000,
message: res.msg,
type: 'warning',
showClose: true
})
2024-05-07 11:51:58 +08:00
} else if (res.errcode == 1002) {
// Message({
// duration:4000,
// message: res.msg,
// type: 'warning',
// showClose:true,
// })
// await store.dispatch('user/resetToken')
// 账号多地登陆 清空token 跳转到登陆页面
2024-05-07 11:30:13 +08:00
localStorage.clear();
2024-05-07 11:51:58 +08:00
sessionStorage.clear();
location.reload()
2024-05-07 11:30:13 +08:00
/* 这里是判断识别sn的接口地址 如果为真 就把content返回*/
} else if (res.errcode == 50086) {
return res
} else if (res.errcode != 0) {
Message({
duration: 4000,
message: res.msg,
type: 'warning',
showClose: true
})
return res
/* 这里是判断识别sn的接口地址 如果为真 就把content返回*/
} else if (response.config.url.indexOf('https://apiv1.aiguovip.com/api/query/identify_sn_imei') > -1) {
return res.content
} else {
return res
}
},
error => {
console.log(error)
if (error.message.indexOf('数据请求中') != -1) {
Message({
duration: 1000,
showClose: true,
message: '数据请求中,请稍后...',
type: 'warning'
})
} else if (error.message.indexOf('timeout') != -1) {
const copyconfig = JSON.parse(JSON.stringify(error.config))
copyconfig.url = error.config.url.replace(copyconfig.baseURL, '')
if (typeof (copyconfig.data) === undefined || copyconfig.data == {} || copyconfig.data == '' || typeof (copyconfig.data) === 'object') {
copyconfig.data = ''
}
removePendingAjax(copyconfig, { type: 2 })
Message({
duration: 4000,
showClose: true,
message: '接口请求超时',
type: 'error'
})
2024-05-07 11:57:19 +08:00
} else {
if(error.response&&error.response.config){
const copyconfig = JSON.parse(JSON.stringify(error.response.config))
copyconfig.url = copyconfig.url.replace(copyconfig.baseURL, '')
2024-05-07 11:30:13 +08:00
if (typeof (copyconfig.data) === undefined || copyconfig.data == {} || copyconfig.data == '' || typeof (copyconfig.data) === 'object') {
copyconfig.data = ''
}
removePendingAjax(copyconfig, { type: 2 })
}
Message({
duration: 4000,
showClose: true,
message: '接口请求超时',
type: 'error'
})
}
// return Promise.reject(error.response.data)
// return Promise.reject(error)
return error
}
)
2024-05-07 11:51:58 +08:00
export default service;