fix: 仅主 API 的 401 清除登录态,避免模块服务误登出
This commit is contained in:
@@ -23,7 +23,6 @@ export const useMpStore = defineStore({
|
|||||||
async fetchMpInfo() {
|
async fetchMpInfo() {
|
||||||
const info = await getMpInfo().catch((err => {
|
const info = await getMpInfo().catch((err => {
|
||||||
console.log(err.code,'fetchMpInfo()')
|
console.log(err.code,'fetchMpInfo()')
|
||||||
uni.clearStorageSync()
|
|
||||||
}));
|
}));
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return {};
|
return {};
|
||||||
|
|||||||
+11
-3
@@ -5,6 +5,14 @@ import {
|
|||||||
} from '@/config/setting';
|
} from '@/config/setting';
|
||||||
|
|
||||||
const http = new Request();
|
const http = new Request();
|
||||||
|
|
||||||
|
// 只有主 API(core) 的 401 才代表登录态失效;shop/booking 等独立服务有自己的令牌体系
|
||||||
|
function isCoreRequest(url?: string) {
|
||||||
|
if (!url) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return url.startsWith(SERVER_API_URL) || url.startsWith('/');
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @description 修改全局默认配置
|
* @description 修改全局默认配置
|
||||||
* @param {Function}
|
* @param {Function}
|
||||||
@@ -48,8 +56,8 @@ http.interceptors.response.use((response) => {
|
|||||||
// return response.data
|
// return response.data
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// token过期
|
// token过期(仅主 API)
|
||||||
if (response.data.code == 401) {
|
if (response.data.code == 401 && isCoreRequest(response.config.url)) {
|
||||||
uni.removeStorageSync('access_token')
|
uni.removeStorageSync('access_token')
|
||||||
uni.removeStorageSync('user_id')
|
uni.removeStorageSync('user_id')
|
||||||
uni.removeStorageSync('phone')
|
uni.removeStorageSync('phone')
|
||||||
@@ -57,7 +65,7 @@ http.interceptors.response.use((response) => {
|
|||||||
if (response.data.code == 1) {
|
if (response.data.code == 1) {
|
||||||
return Promise.reject(response.data)
|
return Promise.reject(response.data)
|
||||||
}
|
}
|
||||||
if (response.data.message === '请先登录') {
|
if (response.data.message === '请先登录' && isCoreRequest(response.config.url)) {
|
||||||
uni.removeStorageSync('access_token')
|
uni.removeStorageSync('access_token')
|
||||||
uni.removeStorageSync('user_id')
|
uni.removeStorageSync('user_id')
|
||||||
uni.removeStorageSync('phone')
|
uni.removeStorageSync('phone')
|
||||||
|
|||||||
Reference in New Issue
Block a user