196 lines
6.2 KiB
JavaScript
196 lines
6.2 KiB
JavaScript
|
/*
|
||
|
* @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'
|
||
|
import store from '@/store'
|
||
|
import { getToken } from '@/utils/auth'
|
||
|
// import cancelClass from './cancelToken'
|
||
|
const VUE_APP_BASE_API='http://ag2.xtkj99.com:20248'
|
||
|
const service = axios.create({
|
||
|
baseURL: VUE_APP_BASE_API,
|
||
|
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) { //检测网络是否正常
|
||
|
if (store.getters.token) {
|
||
|
config.headers['token'] = localStorage.getItem('token')
|
||
|
}
|
||
|
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
|
||
|
})
|
||
|
} else if (res.errcode == 1002) {
|
||
|
// Message({
|
||
|
// duration:4000,
|
||
|
// message: res.msg,
|
||
|
// type: 'warning',
|
||
|
// showClose:true,
|
||
|
// })
|
||
|
// await store.dispatch('user/resetToken')
|
||
|
// 账号多地登陆 清空token 跳转到登陆页面
|
||
|
await store.dispatch('user/resetToken').then(() => {
|
||
|
location.reload()
|
||
|
})
|
||
|
/* 这里是判断识别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.message,"requesterror");
|
||
|
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'
|
||
|
})
|
||
|
} else {
|
||
|
// console.log(Object.getOwnPropertyNames(error));
|
||
|
|
||
|
// console.log(error.stack);
|
||
|
// console.log(error.message);
|
||
|
let copyconfig=null;
|
||
|
if(error?.response?.config){
|
||
|
copyconfig = JSON.parse(JSON.stringify(error.response.config))
|
||
|
copyconfig.url = error.response.config.url.replace(copyconfig.baseURL, '')
|
||
|
}
|
||
|
|
||
|
if(!copyconfig||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
|
||
|
}
|
||
|
)
|
||
|
|
||
|
export default service
|