You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
6.3 KiB
236 lines
6.3 KiB
import * as config from './config';
|
|
import cache from './cache';
|
|
import aes from "@/common/aes.js";
|
|
import store from "@/store/index.js";
|
|
|
|
|
|
// 用于存储正在进行的请求,以便在刷新 token 后重新发送
|
|
let requests = [];
|
|
|
|
let isRefreshingToken = false;
|
|
let refreshTokenPromise = null;
|
|
|
|
const refreshToken = () => {
|
|
if (!isRefreshingToken) {
|
|
debugger
|
|
isRefreshingToken = true;
|
|
const userInfo = uni.getStorageSync('userInfo');
|
|
const refreshTokenValue = userInfo.refreshToken;
|
|
if (!refreshTokenValue){
|
|
debugger
|
|
store.commit('setUserInfo', null)
|
|
uni.removeStorageSync('userInfo');
|
|
uni.showToast({
|
|
title: '用户失效,请重新登录',
|
|
icon: 'none',
|
|
mask: true
|
|
})
|
|
setTimeout(() => {
|
|
uni.reLaunch({
|
|
url: '/pages/index/index'
|
|
});
|
|
},1500)
|
|
}else{
|
|
debugger
|
|
return module.exports.post(config.refreshToken, { refreshToken: refreshTokenValue }, 'application/json', null, false, false)
|
|
.then(response => {
|
|
debugger
|
|
const newAccessToken = response.data.accessToken;
|
|
const newRefreshToken = response.data.refreshToken;
|
|
userInfo.accessToken = newAccessToken;
|
|
userInfo.refreshToken = newRefreshToken;
|
|
store.commit('setUserInfo', userInfo)
|
|
return newAccessToken;
|
|
})
|
|
.catch(error => {
|
|
debugger
|
|
isRefreshingToken = false;
|
|
return Promise.reject(error);
|
|
});
|
|
}
|
|
} else {
|
|
return refreshTokenPromise;
|
|
}
|
|
};
|
|
|
|
const requestInterceptor = function (config) {
|
|
debugger
|
|
const tok = store.state.userInfo.accessToken || uni.getStorageSync('userInfo').accessToken;
|
|
if (tok) {
|
|
config.header['Authorization'] = 'Bearer ' + tok;
|
|
}
|
|
// 添加其他通用预处理逻辑
|
|
if (config.data && typeof config.data === 'object') {
|
|
for (const key in config.data) {
|
|
if (key === 'oldFieldName') {
|
|
config.data['newFieldName'] = config.data[key];
|
|
delete config.data[key];
|
|
}
|
|
}
|
|
}
|
|
return config;
|
|
};
|
|
|
|
const responseInterceptor = function (response,config) {
|
|
console.log(response, config,'response接口结果')
|
|
debugger
|
|
const code = response[1].data.status || response[1].data.code;
|
|
if (code&&code === 401) {
|
|
let originalRequest = config;
|
|
console.log(originalRequest, 'originalRequestsssss');
|
|
return new Promise((resolve, reject) => {
|
|
// 先将当前请求添加到队列中
|
|
requests.push(() => {
|
|
originalRequest.header['Authorization'] = 'Bearer ' + store.state.userInfo.accessToken;
|
|
return uni.request(originalRequest).then((newResponse) => {
|
|
console.log(newResponse, 'newResponse');
|
|
// 如果新的响应还是 401,递归调用直到成功或出现其他错误
|
|
if (newResponse[1].data.status === 401 || newResponse[1].data.code === 401) {
|
|
return responseInterceptor(newResponse, originalRequest);
|
|
} else {
|
|
resolve(newResponse[1]);
|
|
}
|
|
}).catch(reject);
|
|
});
|
|
|
|
// 调用刷新 token 的接口
|
|
refreshToken().then(() => {
|
|
requests.forEach(cb => cb());
|
|
requests = [];
|
|
}).catch(reject);
|
|
});
|
|
}
|
|
return response[1];
|
|
};
|
|
|
|
module.exports = {
|
|
config: function (name) {
|
|
var info = null;
|
|
if (name) {
|
|
var name2 = name.split(".");
|
|
if (name2.length > 1) {
|
|
info = configdata[name2[0]][name2[1]] || null;
|
|
} else {
|
|
info = configdata[name] || null;
|
|
}
|
|
if (info == null) {
|
|
let web_config = cache.get("web_config");
|
|
if (web_config) {
|
|
if (name2.length > 1) {
|
|
info = web_config[name2[0]][name2[1]] || null;
|
|
} else {
|
|
info = web_config[name] || null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return info;
|
|
},
|
|
post: function (url, data, headerContentType, tentId, needAes, auth) {
|
|
console.log(url, data, headerContentType, tentId, needAes, 'post 请求参数');
|
|
|
|
if (needAes !== false) {
|
|
for (var key in data) {
|
|
data[key] = aes.aesMinEncrypt(data[key]);
|
|
}
|
|
}
|
|
|
|
headerContentType = headerContentType === "json" ? "application/json" : "application/x-www-form-urlencoded";
|
|
|
|
let headers = {
|
|
"content-type": headerContentType,
|
|
'Cache-Control': 'no-cache'
|
|
};
|
|
|
|
if (auth === true) {
|
|
const tok = store.state.userInfo.accessToken || uni.getStorageSync('userInfo').accessToken;
|
|
headers['Authorization'] = 'Bearer ' + tok;
|
|
}
|
|
|
|
if (tentId && tentId !== "" && tentId !== null && tentId !== undefined) {
|
|
headers['tenant-id'] = tentId;
|
|
}
|
|
|
|
return new Promise((succ, error) => {
|
|
let config = {
|
|
url: url,
|
|
data: headerContentType === "application/json" ? JSON.stringify(data) : data,
|
|
method: "POST",
|
|
header: headers,
|
|
xhrFields: {
|
|
withCredentials: true
|
|
}
|
|
};
|
|
config = requestInterceptor(config);
|
|
uni.request(config).then(result => {
|
|
const response = responseInterceptor(result, config);
|
|
if (response instanceof Promise) {
|
|
response.then(res => {
|
|
succ.call(self, res.data);
|
|
}).catch(err => {
|
|
console.error('POST 请求错误:', err);
|
|
error.call(self, err);
|
|
});
|
|
} else {
|
|
succ.call(self, response.data);
|
|
}
|
|
}).catch(e => {
|
|
console.error('POST 请求失败:', e);
|
|
error.call(self, e);
|
|
});
|
|
});
|
|
},
|
|
|
|
get: function (url, data, headerContentType, tentId, needAes, auth) {
|
|
console.log(url, data, headerContentType, tentId, needAes, 'get 请求参数');
|
|
|
|
if (needAes !== false) {
|
|
for (var key in data) {
|
|
data[key] = aes.aesMinEncrypt(data[key]);
|
|
}
|
|
}
|
|
|
|
headerContentType = headerContentType ? headerContentType : "application/x-www-form-urlencoded";
|
|
|
|
let headers = {
|
|
"content-type": headerContentType,
|
|
'Cache-Control': 'no-cache'
|
|
};
|
|
|
|
if (auth === true) {
|
|
// const tok = store.state.userInfo.accessToken || uni.getStorageSync('userInfo').accessToken;
|
|
const tok = uni.getStorageSync('userInfo').accessToken;
|
|
headers['Authorization'] = 'Bearer ' + tok;
|
|
}
|
|
|
|
if (tentId && tentId !== "" && tentId !== null && tentId !== undefined) {
|
|
headers['tenant-id'] = tentId;
|
|
}
|
|
|
|
return new Promise((succ, error) => {
|
|
let config = {
|
|
url: url,
|
|
data: data,
|
|
method: "GET",
|
|
header: headers
|
|
};
|
|
config = requestInterceptor(config);
|
|
uni.request(config).then(result => {
|
|
const response = responseInterceptor(result, config);
|
|
if (response instanceof Promise) {
|
|
response.then(res => {
|
|
succ.call(self, res.data);
|
|
}).catch(err => {
|
|
console.error('POST 请求错误:', err);
|
|
error.call(self, err);
|
|
});
|
|
} else {
|
|
succ.call(self, response.data);
|
|
}
|
|
}).catch(e => {
|
|
console.error('GET 请求失败:', e);
|
|
error.call(self, e);
|
|
});
|
|
});
|
|
}
|
|
}; |