|
|
|
import storage from '../common/utils/storage'
|
|
|
|
import {
|
|
|
|
removeToken
|
|
|
|
} from '@/common/utils/auth'
|
|
|
|
|
|
|
|
function service(options = {}) {
|
|
|
|
|
|
|
|
var token = storage.getStorage(storage.constant.token)
|
|
|
|
var tenantId = storage.getStorage('tenantId')
|
|
|
|
// if(getApp()!=undefined){
|
|
|
|
// tenantId = getApp().globalData.tenantId
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if(getApp()!=undefined){
|
|
|
|
// requestUrl = getApp().globalData.request_url
|
|
|
|
// }
|
|
|
|
var requestUrl = ""
|
|
|
|
requestUrl = import.meta.env.VITE_BASE_URL
|
|
|
|
// 获取当前页面路由
|
|
|
|
let pages = getCurrentPages(); // 获取所有页面栈的实例数组
|
|
|
|
let currentPage = pages[pages.length - 1]; // 获取最后一个页面的实例
|
|
|
|
let currentRoute = currentPage.route; // 获取当前页面的路由
|
|
|
|
options.header = {
|
|
|
|
"content-type": "application/json",
|
|
|
|
"Authorization": "Bearer " + token,
|
|
|
|
"dataType": "json",
|
|
|
|
"dataSource": "PDA",
|
|
|
|
"tenant-id": tenantId,
|
|
|
|
"Referer1":currentPage.route
|
|
|
|
};
|
|
|
|
options.timeout = 300000
|
|
|
|
|
|
|
|
options.url = requestUrl + options.url
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
options.success = (res) => {
|
|
|
|
if (res) {
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
if (res.data.code == 0) {
|
|
|
|
resolve(res.data);
|
|
|
|
} else {
|
|
|
|
if (res.data.code == 401) {
|
|
|
|
uni.clearStorageSync()
|
|
|
|
uni.removeStorageSync('overPackageRecord') // 删除直接翻包中缓存的来源库位
|
|
|
|
removeToken();
|
|
|
|
uni.reLaunch({
|
|
|
|
url: '/pages/login/index'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (res.data.code == 1002000008) {
|
|
|
|
uni.setStorageSync('username',options.data.username)
|
|
|
|
uni.reLaunch({
|
|
|
|
url: '/pages/setter/loginPwdUpdate'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (res.data.msg) {
|
|
|
|
if (res.data.msg.includes('get lock')) {
|
|
|
|
reject("系统异常:状态码["+res.statusCode +"]\n"+"数据库死锁")
|
|
|
|
} else {
|
|
|
|
reject("系统异常:状态码["+res.statusCode +"]\n"+res.data.msg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject("系统异常:状态码["+res.statusCode+"]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (res.data.msg) {
|
|
|
|
if (res.data.msg.includes('get lock')) {
|
|
|
|
reject("系统异常:状态码[" +res.statusCode+"]\n"+ "数据库死锁")
|
|
|
|
} else {
|
|
|
|
reject("系统异常:状态码[" +res.statusCode+"]\n"+ res.data.msg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject("系统异常:状态码["+res.statusCode+"]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (res.data.msg) {
|
|
|
|
if (res.data.msg.includes('get lock')) {
|
|
|
|
reject("系统异常:状态码["+res.statusCode +"]\n"+ "数据库死锁")
|
|
|
|
} else {
|
|
|
|
reject("系统异常:状态码["+res.statusCode +"]\n"+ res.data.msg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject("系统异常:状态码["+res.statusCode+"]")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
options.fail = (error) => {
|
|
|
|
var message = error.errMsg
|
|
|
|
if (message === 'Network Error') {
|
|
|
|
message = '接口连接异常'
|
|
|
|
} else if (message.includes('timeout')) {
|
|
|
|
message = '接口请求超时'
|
|
|
|
} else if (message.includes('Request failed with status code')) {
|
|
|
|
message = '接口' + message.substr(message.length - 3) + '异常'
|
|
|
|
} else if (message.includes('get lock')) {
|
|
|
|
message = '接口' + "数据库死锁"
|
|
|
|
}else if(message.includes('statusCode:-1')){
|
|
|
|
message = "当前设备无网络,请检查网络后重试"
|
|
|
|
}
|
|
|
|
reject("系统异常:" + message);
|
|
|
|
console.log("系统异常", message)
|
|
|
|
};
|
|
|
|
// #ifdef APP
|
|
|
|
uni.getNetworkType({
|
|
|
|
success: function(res) {
|
|
|
|
const networkType = res.networkType;
|
|
|
|
console.log("网络类型", networkType)
|
|
|
|
if (networkType == "none") {
|
|
|
|
reject("当前设备无网络,请检查网络后重试");
|
|
|
|
} else {
|
|
|
|
uni.request(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// #endif
|
|
|
|
// #ifdef H5
|
|
|
|
console.log("网s络H5", navigator.onLine)
|
|
|
|
if (navigator.onLine) {
|
|
|
|
uni.request(options);
|
|
|
|
} else {
|
|
|
|
reject("当前无网络");
|
|
|
|
}
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default service;
|