|
|
@@ -1,13 +1,18 @@
|
|
|
import { CustomRequestOptions } from '@/interceptors/request'
|
|
|
+import { useUserStore } from '@/store/user'
|
|
|
+import { useAppStore } from '@/store/app'
|
|
|
|
|
|
export const http = <T>(options: CustomRequestOptions) => {
|
|
|
+ const userStore = useUserStore()
|
|
|
+ const appStore = useAppStore()
|
|
|
+
|
|
|
// 1. 返回 Promise 对象
|
|
|
return new Promise<IResData<T>>((resolve, reject) => {
|
|
|
uni.request({
|
|
|
...options,
|
|
|
header: Object.assign(
|
|
|
{
|
|
|
- token: `${uni.getStorageSync('loginInfo')?.token}`,
|
|
|
+ token: `${appStore.appInfo.token}`,
|
|
|
},
|
|
|
options.header,
|
|
|
),
|
|
|
@@ -17,21 +22,53 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|
|
// #endif
|
|
|
// 响应成功
|
|
|
success(res) {
|
|
|
- // 状态码 2xx,参考 axios 的设计
|
|
|
+ const { Status, Message, Data } = res.data as any
|
|
|
+
|
|
|
+ // 2. http的状态码 状态码 2xx,参考 axios 的设计
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
- // 2.1 提取核心数据 res.data
|
|
|
- resolve(res.data as IResData<T>)
|
|
|
+ // 3. 业务状态码
|
|
|
+ switch (Status) {
|
|
|
+ case 0: // 请求成功
|
|
|
+ resolve(res.data as IResData<T>)
|
|
|
+ break
|
|
|
+ case -1: // 请求失败
|
|
|
+ uni.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ title: Message || '请求错误',
|
|
|
+ duration: 3000,
|
|
|
+ })
|
|
|
+ break
|
|
|
+ case -2: // token过期
|
|
|
+ // 判断是否已登录
|
|
|
+ if (appStore.isLogined) {
|
|
|
+ uni.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ title: Message || '请求错误',
|
|
|
+ duration: 3000,
|
|
|
+ success: () => {
|
|
|
+ userStore.clearUserInfo()
|
|
|
+ appStore.clearAppInfo()
|
|
|
+ uni.navigateTo({ url: '/pages-sub/auth/index' })
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
} else if (res.statusCode === 401) {
|
|
|
// 401错误 -> 清理用户信息,跳转到登录页
|
|
|
- // userStore.clearUserInfo()
|
|
|
- // uni.navigateTo({ url: '/pages/login/login' })
|
|
|
+ userStore.clearUserInfo()
|
|
|
+ appStore.clearAppInfo()
|
|
|
+ uni.navigateTo({ url: '/pages-sub/auth/index' })
|
|
|
reject(res)
|
|
|
} else {
|
|
|
// 其他错误 -> 根据后端错误信息轻提示
|
|
|
!options.hideErrorToast &&
|
|
|
uni.showToast({
|
|
|
icon: 'none',
|
|
|
- title: (res.data as IResData<T>).msg || '请求错误',
|
|
|
+ title: (res.data as IResData<T>).Message || '请求错误',
|
|
|
+ duration: 3000,
|
|
|
})
|
|
|
reject(res)
|
|
|
}
|
|
|
@@ -41,6 +78,7 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|
|
uni.showToast({
|
|
|
icon: 'none',
|
|
|
title: '网络错误,换个网络试试',
|
|
|
+ duration: 3000,
|
|
|
})
|
|
|
reject(err)
|
|
|
},
|