|
@@ -11,6 +11,8 @@ import { showAuthModal } from '@/hooks/useModal'
|
|
|
import { rewardEventTrackClick } from '@/api/integral'
|
|
|
import { getSuffixAfterPrefix } from '@/utils/prefixUrl'
|
|
|
import { encryptionFun } from '@/utils/openEncryption'
|
|
|
+import { useSystem } from '@/store/system'
|
|
|
+import { sendError } from '@/api/system'
|
|
|
|
|
|
import qs from 'qs'
|
|
|
|
|
@@ -25,6 +27,8 @@ const getEventList = () => {
|
|
|
}
|
|
|
getEventList()
|
|
|
|
|
|
+const errorData = []
|
|
|
+
|
|
|
const options = {
|
|
|
// 显示操作成功消息 默认不显示
|
|
|
showSuccess: false,
|
|
@@ -85,7 +89,7 @@ const http = new Request({
|
|
|
* @description 请求拦截器
|
|
|
*/
|
|
|
http.interceptors.request.use(
|
|
|
- (config) => {
|
|
|
+ async (config) => {
|
|
|
const useUserStore = userStore()
|
|
|
// 自定义处理【auth 授权】:必须登录的接口,则跳出 AuthModal 登录弹窗
|
|
|
if (config.custom.auth && !useUserStore.isLogin) {
|
|
@@ -133,12 +137,38 @@ http.interceptors.request.use(
|
|
|
|
|
|
// 开启参数加密
|
|
|
if (config.custom?.openEncryption) {
|
|
|
+ const { getTimeDifference, setTimeDifference, systemInfo } = useSystem()
|
|
|
const raw = config.url.split('?')[1]
|
|
|
- const body = {
|
|
|
- ...config.data,
|
|
|
- ...config.params
|
|
|
- }
|
|
|
- const header = encryptionFun({raw, body, appId: 'web_client', AppSecret: 'fa0fc0b5098b974b'})
|
|
|
+ const body = {
|
|
|
+ ...typeof config.data === 'string' ? JSON.parse(config.data) : config.data,
|
|
|
+ ...typeof config.params === 'string' ? JSON.parse(config.params) : config.params
|
|
|
+ }
|
|
|
+ if (!systemInfo?.timeDifference) {
|
|
|
+ const _difference = await getTimeDifference()
|
|
|
+ setTimeDifference(_difference)
|
|
|
+ }
|
|
|
+
|
|
|
+ const header = encryptionFun({
|
|
|
+ raw,
|
|
|
+ body,
|
|
|
+ appId: 'web_client',
|
|
|
+ AppSecret: 'fa0fc0b5098b974b',
|
|
|
+ timestamp: new Date().getTime() + (Number(systemInfo?.timeDifference) || 0),
|
|
|
+ })
|
|
|
+ const content = {
|
|
|
+ data: config.data,
|
|
|
+ params: config.params,
|
|
|
+ body,
|
|
|
+ raw,
|
|
|
+ config
|
|
|
+ }
|
|
|
+ // 记录请求备用-签名错误后重试
|
|
|
+
|
|
|
+ errorData.push({
|
|
|
+ time: header.timestamp,
|
|
|
+ url: config.url,
|
|
|
+ content
|
|
|
+ })
|
|
|
Object.assign(config.header, header)
|
|
|
}
|
|
|
|
|
@@ -187,6 +217,8 @@ http.interceptors.response.use(
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ const config = response.config
|
|
|
+
|
|
|
// 自定处理【loading 加载中】:如果需要显示 loading,则关闭 loading
|
|
|
response.config.custom.showLoading && closeLoading();
|
|
|
|
|
@@ -197,6 +229,23 @@ http.interceptors.response.use(
|
|
|
return handleRefreshToken(response.config);
|
|
|
}
|
|
|
|
|
|
+ // 签名错误
|
|
|
+ if (response.data.code === 440) {
|
|
|
+ // 更新服务器与用户时间差的值
|
|
|
+ const { setTimeDifference } = useSystem()
|
|
|
+ if (Number(response?.data?.msg)) setTimeDifference(response.data.msg - new Date().getTime())
|
|
|
+ // 请求重试
|
|
|
+ signErrRetry(response.config)
|
|
|
+ // 保存错误信息
|
|
|
+ const _index = errorData.findIndex(e => e.url === config.url && e.time === +config.header.timestamp)
|
|
|
+ if (_index > -1) {
|
|
|
+ const _d = errorData.splice(_index, 1)
|
|
|
+ const _item = _d[0]
|
|
|
+ sendError({ content: JSON.stringify(_item.content), mark: _item.time + '' })
|
|
|
+ }
|
|
|
+ response.config.custom.showError = false // 不弹错误弹窗
|
|
|
+ }
|
|
|
+
|
|
|
// 错误提示
|
|
|
if (response.config.custom.showError) {
|
|
|
if (response.data?.msg === '重复请求,请稍后重试') console.error('前台打印: 重复请求,请稍后重试')
|
|
@@ -258,6 +307,9 @@ http.interceptors.response.use(
|
|
|
case 429:
|
|
|
errorMessage = '请求频繁, 请稍后再访问';
|
|
|
break;
|
|
|
+ case 440:
|
|
|
+ errorMessage = '网络请求出错'; // 签名异常
|
|
|
+ break;
|
|
|
case 500:
|
|
|
errorMessage = '服务器开小差啦,请稍后再试~';
|
|
|
break;
|
|
@@ -374,6 +426,34 @@ const handleAuthorized = () => {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 处理440签名错误
|
|
|
+*/
|
|
|
+const maxRetries = 2 // 设置签名错误重试请求次数,超出次数弹出错误
|
|
|
+const retryDelay = 1000 // 请求延迟
|
|
|
+const signErrRetry = (config) => {
|
|
|
+ if (config) {
|
|
|
+ if (!config?.custom?.retryCount) {
|
|
|
+ config.custom.retryCount = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ // 超过重试次数
|
|
|
+ if (config.custom.retryCount >= maxRetries) {
|
|
|
+ uni.showToast({
|
|
|
+ title: '网络请求错误!',
|
|
|
+ icon: 'none',
|
|
|
+ mask: false,
|
|
|
+ });
|
|
|
+ return Promise.reject({ statusCode: 440 })
|
|
|
+ }
|
|
|
+ // 重试
|
|
|
+ config.custom.retryCount++
|
|
|
+ setTimeout(() => {
|
|
|
+ return request(config)
|
|
|
+ }, retryDelay)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 触发获取积分
|
|
|
const getIntegral = (url) => {
|
|
|
rewardEventTrackClick(url).then(() => {})
|
|
@@ -381,7 +461,7 @@ const getIntegral = (url) => {
|
|
|
|
|
|
/** 获得必填人才信息情况 */
|
|
|
export const showNecessaryInfoPopup = () => {
|
|
|
- return uni.getStorageSync('necessaryInfoReady') === 'fddeaddc47868b';
|
|
|
+ return uni.getStorageSync('necessaryInfoReady') === 'student';
|
|
|
}
|
|
|
|
|
|
/** 获得访问令牌 */
|