2024-08-07 10:31:42 +08:00
|
|
|
|
/**
|
|
|
|
|
* Shopro-request
|
|
|
|
|
* @description api模块管理,loading配置,请求拦截,错误处理
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import Request from 'luch-request';
|
|
|
|
|
import { baseUrl, apiPath } from '@/sheep/config';
|
|
|
|
|
import $store from '@/sheep/store';
|
|
|
|
|
import $platform from '@/sheep/platform';
|
2024-08-07 21:40:27 +08:00
|
|
|
|
import { showAuthModal } from '@/sheep/hooks/useModal';
|
2024-08-07 10:31:42 +08:00
|
|
|
|
import AuthUtil from '@/sheep/api/member/auth';
|
|
|
|
|
|
|
|
|
|
const options = {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
// 显示操作成功消息 默认不显示
|
|
|
|
|
showSuccess: false,
|
|
|
|
|
// 成功提醒 默认使用后端返回值
|
|
|
|
|
successMsg: '',
|
|
|
|
|
// 显示失败消息 默认显示
|
|
|
|
|
showError: true,
|
|
|
|
|
// 失败提醒 默认使用后端返回信息
|
|
|
|
|
errorMsg: '',
|
|
|
|
|
// 显示请求时loading模态框 默认显示
|
|
|
|
|
showLoading: true,
|
|
|
|
|
// loading提醒文字
|
|
|
|
|
loadingMsg: '加载中',
|
|
|
|
|
// 需要授权才能请求 默认放开
|
|
|
|
|
auth: false,
|
|
|
|
|
// ...
|
2024-08-07 10:31:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Loading全局实例
|
|
|
|
|
let LoadingInstance = {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
target: null,
|
|
|
|
|
count: 0,
|
2024-08-07 10:31:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关闭loading
|
|
|
|
|
*/
|
|
|
|
|
function closeLoading() {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
if (LoadingInstance.count > 0) LoadingInstance.count--;
|
|
|
|
|
if (LoadingInstance.count === 0) uni.hideLoading();
|
2024-08-07 10:31:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description 请求基础配置 可直接使用访问自定义请求
|
|
|
|
|
*/
|
|
|
|
|
const http = new Request({
|
2024-08-07 21:40:27 +08:00
|
|
|
|
baseURL: baseUrl + apiPath,
|
|
|
|
|
timeout: 8000,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
header: {
|
|
|
|
|
Accept: 'text/json',
|
|
|
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
|
|
|
platform: $platform.name,
|
|
|
|
|
},
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
sslVerify: false,
|
|
|
|
|
// #endif
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
// 跨域请求时是否携带凭证(cookies)仅H5支持(HBuilderX 2.6.15+)
|
|
|
|
|
withCredentials: false,
|
|
|
|
|
// #endif
|
|
|
|
|
custom: options,
|
2024-08-07 10:31:42 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description 请求拦截器
|
|
|
|
|
*/
|
|
|
|
|
http.interceptors.request.use(
|
2024-08-07 21:40:27 +08:00
|
|
|
|
(config) => {
|
2024-08-07 10:31:42 +08:00
|
|
|
|
// 自定义处理【auth 授权】:必须登录的接口,则跳出 AuthModal 登录弹窗
|
2024-08-07 21:40:27 +08:00
|
|
|
|
if (config.custom.auth && !$store('user').isLogin) {
|
|
|
|
|
showAuthModal();
|
|
|
|
|
return Promise.reject();
|
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
// 自定义处理【loading 加载中】:如果需要显示 loading,则显示 loading
|
2024-08-07 21:40:27 +08:00
|
|
|
|
if (config.custom.showLoading) {
|
|
|
|
|
LoadingInstance.count++;
|
|
|
|
|
LoadingInstance.count === 1 &&
|
|
|
|
|
uni.showLoading({
|
|
|
|
|
title: config.custom.loadingMsg,
|
|
|
|
|
mask: true,
|
|
|
|
|
fail: () => {
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
// 增加 token 令牌、terminal 终端、tenant 租户的请求头
|
2024-08-07 21:40:27 +08:00
|
|
|
|
const token = getAccessToken();
|
|
|
|
|
if (token) {
|
2024-08-07 10:31:42 +08:00
|
|
|
|
config.header['Authorization'] = token;
|
|
|
|
|
}
|
2024-08-07 21:40:27 +08:00
|
|
|
|
// TODO 芋艿:特殊处理
|
|
|
|
|
config.header['Accept'] = '*/*';
|
2024-08-07 10:31:42 +08:00
|
|
|
|
config.header['tenant-id'] = '1';
|
|
|
|
|
config.header['terminal'] = '20';
|
|
|
|
|
// config.header['Authorization'] = 'Bearer test247';
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
},
|
2024-08-07 10:31:42 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description 响应拦截器
|
|
|
|
|
*/
|
|
|
|
|
http.interceptors.response.use(
|
2024-08-07 21:40:27 +08:00
|
|
|
|
(response) => {
|
|
|
|
|
// 约定:如果是 /auth/ 下的 URL 地址,并且返回了 accessToken 说明是登录相关的接口,则自动设置登陆令牌
|
|
|
|
|
if (response.config.url.indexOf('/member/auth/') >= 0 && response.data?.data?.accessToken) {
|
|
|
|
|
$store('user').setToken(response.data.data.accessToken, response.data.data.refreshToken);
|
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
// 自定处理【loading 加载中】:如果需要显示 loading,则关闭 loading
|
2024-08-07 21:40:27 +08:00
|
|
|
|
response.config.custom.showLoading && closeLoading();
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
// 自定义处理【error 错误提示】:如果需要显示错误提示,则显示错误提示
|
2024-08-07 21:40:27 +08:00
|
|
|
|
if (response.data.code !== 0) {
|
2024-08-07 10:31:42 +08:00
|
|
|
|
// 特殊:如果 401 错误码,则跳转到登录页 or 刷新令牌
|
|
|
|
|
if (response.data.code === 401) {
|
|
|
|
|
return refreshToken(response.config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 错误提示
|
2024-08-07 21:40:27 +08:00
|
|
|
|
if (response.config.custom.showError) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: response.data.msg || '服务器开小差啦,请稍后再试~',
|
|
|
|
|
icon: 'none',
|
|
|
|
|
mask: true,
|
|
|
|
|
});
|
2024-08-07 10:31:42 +08:00
|
|
|
|
}
|
2024-08-07 21:40:27 +08:00
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
2024-08-07 21:40:27 +08:00
|
|
|
|
// 自定义处理【showSuccess 成功提示】:如果需要显示成功提示,则显示成功提示
|
|
|
|
|
if (
|
|
|
|
|
response.config.custom.showSuccess &&
|
|
|
|
|
response.config.custom.successMsg !== '' &&
|
|
|
|
|
response.data.code === 0
|
|
|
|
|
) {
|
2024-08-07 10:31:42 +08:00
|
|
|
|
uni.showToast({
|
2024-08-07 21:40:27 +08:00
|
|
|
|
title: response.config.custom.successMsg,
|
|
|
|
|
icon: 'none',
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
// 返回结果:包括 code + data + msg
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return Promise.resolve(response.data);
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
const userStore = $store('user');
|
|
|
|
|
const isLogin = userStore.isLogin;
|
|
|
|
|
let errorMessage = '网络请求出错';
|
|
|
|
|
if (error !== undefined) {
|
|
|
|
|
switch (error.statusCode) {
|
|
|
|
|
case 400:
|
|
|
|
|
errorMessage = '请求错误';
|
|
|
|
|
break;
|
|
|
|
|
case 401:
|
2024-08-07 10:31:42 +08:00
|
|
|
|
errorMessage = isLogin ? '您的登陆已过期' : '请先登录';
|
|
|
|
|
// 正常情况下,后端不会返回 401 错误,所以这里不处理 handleAuthorized
|
|
|
|
|
break;
|
2024-08-07 21:40:27 +08:00
|
|
|
|
case 403:
|
|
|
|
|
errorMessage = '拒绝访问';
|
|
|
|
|
break;
|
|
|
|
|
case 404:
|
|
|
|
|
errorMessage = '请求出错';
|
|
|
|
|
break;
|
|
|
|
|
case 408:
|
|
|
|
|
errorMessage = '请求超时';
|
|
|
|
|
break;
|
|
|
|
|
case 429:
|
|
|
|
|
errorMessage = '请求频繁, 请稍后再访问';
|
|
|
|
|
break;
|
|
|
|
|
case 500:
|
|
|
|
|
errorMessage = '服务器开小差啦,请稍后再试~';
|
|
|
|
|
break;
|
|
|
|
|
case 501:
|
|
|
|
|
errorMessage = '服务未实现';
|
|
|
|
|
break;
|
|
|
|
|
case 502:
|
|
|
|
|
errorMessage = '网络错误';
|
|
|
|
|
break;
|
|
|
|
|
case 503:
|
|
|
|
|
errorMessage = '服务不可用';
|
|
|
|
|
break;
|
|
|
|
|
case 504:
|
|
|
|
|
errorMessage = '网络超时';
|
|
|
|
|
break;
|
|
|
|
|
case 505:
|
|
|
|
|
errorMessage = 'HTTP 版本不受支持';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (error.errMsg.includes('timeout')) errorMessage = '请求超时';
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
if (error.errMsg.includes('Network'))
|
|
|
|
|
errorMessage = window.navigator.onLine ? '服务器异常' : '请检查您的网络连接';
|
|
|
|
|
// #endif
|
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
2024-08-07 21:40:27 +08:00
|
|
|
|
if (error && error.config) {
|
|
|
|
|
if (error.config.custom.showError === false) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
title: error.data?.msg || errorMessage,
|
|
|
|
|
icon: 'none',
|
|
|
|
|
mask: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
error.config.custom.showLoading && closeLoading();
|
|
|
|
|
}
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return false;
|
|
|
|
|
},
|
2024-08-07 10:31:42 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Axios 无感知刷新令牌,参考 https://www.dashingdog.cn/article/11 与 https://segmentfault.com/a/1190000020210980 实现
|
2024-08-07 21:40:27 +08:00
|
|
|
|
let requestList = []; // 请求队列
|
|
|
|
|
let isRefreshToken = false; // 是否正在刷新中
|
2024-08-07 10:31:42 +08:00
|
|
|
|
const refreshToken = async (config) => {
|
|
|
|
|
// 如果当前已经是 refresh-token 的 URL 地址,并且还是 401 错误,说明是刷新令牌失败了,直接返回 Promise.reject(error)
|
|
|
|
|
if (config.url.indexOf('/member/auth/refresh-token') >= 0) {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return Promise.reject('error');
|
2024-08-07 10:31:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
|
|
|
|
|
if (!isRefreshToken) {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
isRefreshToken = true;
|
2024-08-07 10:31:42 +08:00
|
|
|
|
// 1. 如果获取不到刷新令牌,则只能执行登出操作
|
2024-08-07 21:40:27 +08:00
|
|
|
|
const refreshToken = getRefreshToken();
|
2024-08-07 10:31:42 +08:00
|
|
|
|
if (!refreshToken) {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return handleAuthorized();
|
2024-08-07 10:31:42 +08:00
|
|
|
|
}
|
|
|
|
|
// 2. 进行刷新访问令牌
|
|
|
|
|
try {
|
|
|
|
|
const refreshTokenResult = await AuthUtil.refreshToken(refreshToken);
|
|
|
|
|
if (refreshTokenResult.code !== 0) {
|
|
|
|
|
// 如果刷新不成功,直接抛出 e 触发 2.2 的逻辑
|
|
|
|
|
// noinspection ExceptionCaughtLocallyJS
|
|
|
|
|
throw new Error('刷新令牌失败');
|
|
|
|
|
}
|
|
|
|
|
// 2.1 刷新成功,则回放队列的请求 + 当前请求
|
2024-08-07 21:40:27 +08:00
|
|
|
|
config.header.Authorization = 'Bearer ' + getAccessToken();
|
2024-08-07 10:31:42 +08:00
|
|
|
|
requestList.forEach((cb) => {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
cb();
|
|
|
|
|
});
|
|
|
|
|
requestList = [];
|
|
|
|
|
return request(config);
|
2024-08-07 10:31:42 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
// 为什么需要 catch 异常呢?刷新失败时,请求因为 Promise.reject 触发异常。
|
|
|
|
|
// 2.2 刷新失败,只回放队列的请求
|
|
|
|
|
requestList.forEach((cb) => {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
cb();
|
|
|
|
|
});
|
2024-08-07 10:31:42 +08:00
|
|
|
|
// 提示是否要登出。即不回放当前请求!不然会形成递归
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return handleAuthorized();
|
2024-08-07 10:31:42 +08:00
|
|
|
|
} finally {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
requestList = [];
|
|
|
|
|
isRefreshToken = false;
|
2024-08-07 10:31:42 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 添加到队列,等待刷新获取到新的令牌
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
requestList.push(() => {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
config.header.Authorization = 'Bearer ' + getAccessToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
|
|
|
|
|
resolve(request(config));
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-08-07 10:31:42 +08:00
|
|
|
|
}
|
2024-08-07 21:40:27 +08:00
|
|
|
|
};
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理 401 未登录的错误
|
|
|
|
|
*/
|
|
|
|
|
const handleAuthorized = () => {
|
|
|
|
|
const userStore = $store('user');
|
|
|
|
|
userStore.logout(true);
|
|
|
|
|
showAuthModal();
|
|
|
|
|
// 登录超时
|
|
|
|
|
return Promise.reject({
|
|
|
|
|
code: 401,
|
2024-08-07 21:40:27 +08:00
|
|
|
|
msg: userStore.isLogin ? '您的登陆已过期' : '请先登录',
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
/** 获得访问令牌 */
|
|
|
|
|
const getAccessToken = () => {
|
|
|
|
|
return uni.getStorageSync('token');
|
2024-08-07 21:40:27 +08:00
|
|
|
|
};
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
/** 获得刷新令牌 */
|
|
|
|
|
const getRefreshToken = () => {
|
|
|
|
|
return uni.getStorageSync('refresh-token');
|
2024-08-07 21:40:27 +08:00
|
|
|
|
};
|
2024-08-07 10:31:42 +08:00
|
|
|
|
|
|
|
|
|
const request = (config) => {
|
2024-08-07 21:40:27 +08:00
|
|
|
|
return http.middleware(config);
|
2024-08-07 10:31:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-08-07 21:40:27 +08:00
|
|
|
|
export default request;
|