116 lines
2.9 KiB
JavaScript
116 lines
2.9 KiB
JavaScript
import coreRequest from './request'
|
||
import apiList from './apis.js'
|
||
import store from '@/core/store/index.js'
|
||
import { setToken,removeToken } from '../utils/auth';
|
||
import router from '@/core/router'
|
||
import { handleTokenExpired } from '@/core/utils/auth-guard'
|
||
|
||
const Request = new coreRequest();
|
||
export default function http(
|
||
url,
|
||
data = {},
|
||
toastBefore = '', // 请求前加载提示
|
||
toastAfter = true, // 请求后错误提示
|
||
responseType = 'text',
|
||
timeout = 0 // 自定义请求超时(ms),0 表示使用默认 20s;长轮询接口需传入大于服务端等待时长的值
|
||
) {
|
||
let api = getApiPath(url);
|
||
console.log(api);
|
||
const token = uni.getStorageSync('token');
|
||
console.log('[Request] token:', token);
|
||
if (api.auth && !token) {
|
||
store.dispatch('logout');
|
||
uni.hideLoading();
|
||
return Promise.reject({
|
||
msg: `暂未登录,已阻止此次API请求: '${api.url}'`
|
||
});
|
||
}
|
||
/* 请求之前拦截器 */
|
||
Request.interceptor.request((config, cancel) => {
|
||
token && Request.setConfig(config => {
|
||
config.header.authorization = `Bearer ${token}`
|
||
console.log('[Request] Authorization:', config.header.authorization);
|
||
})
|
||
if (toastBefore !== '') {
|
||
uni.showLoading({
|
||
title: toastBefore,
|
||
mask: true
|
||
});
|
||
}
|
||
return config
|
||
});
|
||
|
||
/* 请求之后拦截器 */
|
||
Request.interceptor.response((res) => {
|
||
const headers = res.header;
|
||
console.log(res);
|
||
const response = res.data;
|
||
const getHeaderValue = (headerMap, key) => {
|
||
if (!headerMap) {
|
||
return '';
|
||
}
|
||
const targetKey = Object.keys(headerMap).find(item => String(item).toLowerCase() === String(key).toLowerCase());
|
||
return targetKey ? headerMap[targetKey] : '';
|
||
};
|
||
const normalizeToken = (rawToken) => {
|
||
if (!rawToken) {
|
||
return '';
|
||
}
|
||
const text = String(rawToken).trim();
|
||
if (!text) {
|
||
return '';
|
||
}
|
||
return text.startsWith('Bearer ') ? text.substring(7).trim() : text;
|
||
};
|
||
const token =
|
||
getHeaderValue(headers, 'authorization') ||
|
||
getHeaderValue(headers, 'token') ||
|
||
getHeaderValue(headers, 'x-access-token') ||
|
||
getHeaderValue(headers, 'access-token');
|
||
const normalizedToken = normalizeToken(token);
|
||
if (normalizedToken) {
|
||
setToken(normalizedToken)
|
||
}
|
||
uni.hideLoading();
|
||
if (response?.flag === "failure") {
|
||
if (toastAfter) {
|
||
uni.showToast({
|
||
title: response.message || '请求出错,稍后重试',
|
||
icon: 'none',
|
||
duration: 1000,
|
||
mask: true
|
||
});
|
||
}
|
||
|
||
}
|
||
|
||
// token过期注销
|
||
if (res.statusCode === 401) {
|
||
handleTokenExpired(store)
|
||
return Promise.reject({
|
||
msg: '登录已过期,请重新登录'
|
||
})
|
||
}
|
||
return response
|
||
})
|
||
|
||
return Request.request({
|
||
url: api.url,
|
||
path: api.path,
|
||
data,
|
||
method: api.method,
|
||
responseType,
|
||
...(timeout ? { timeout } : {})
|
||
})
|
||
}
|
||
|
||
// 组装接口路径
|
||
function getApiPath(url) {
|
||
let apiArray = url.split(".");
|
||
let api = apiList;
|
||
apiArray.forEach(v => {
|
||
api = api[v];
|
||
});
|
||
return api;
|
||
}
|