feat: 接口对接完成
This commit is contained in:
+51
-10
@@ -1,4 +1,6 @@
|
||||
import { CustomRequestOptions } from '@/interceptors/request'
|
||||
import { loginRoute } from '@/interceptors/route'
|
||||
import { useUserStore } from '@/store'
|
||||
|
||||
export const http = <T>(options: CustomRequestOptions) => {
|
||||
// 1. 返回 Promise 对象
|
||||
@@ -10,23 +12,62 @@ export const http = <T>(options: CustomRequestOptions) => {
|
||||
responseType: 'json',
|
||||
// #endif
|
||||
// 响应成功
|
||||
success(res) {
|
||||
async success(res) {
|
||||
// 状态码 2xx,参考 axios 的设计
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
// 2.1 提取核心数据 res.data
|
||||
resolve(res.data as IResData<T>)
|
||||
if (res.header['content-type'].includes('application/json')) {
|
||||
// 2.1 提取核心数据 res.data
|
||||
const data = res.data as IResData<T>
|
||||
if (data.code === 1) {
|
||||
resolve(data as IResData<T>)
|
||||
} else if (data.code === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
!options.hideErrorToast &&
|
||||
(await uni.showModal({
|
||||
title: '提示',
|
||||
content: data.msg || '请求错误',
|
||||
showCancel: false,
|
||||
}))
|
||||
uni.reLaunch({
|
||||
url: loginRoute,
|
||||
})
|
||||
|
||||
reject(res)
|
||||
} else {
|
||||
!options.hideErrorToast &&
|
||||
(await uni.showModal({
|
||||
title: '提示',
|
||||
content: data.msg || '请求错误',
|
||||
showCancel: false,
|
||||
}))
|
||||
reject(res)
|
||||
}
|
||||
} else {
|
||||
resolve(res.data as any)
|
||||
}
|
||||
} else if (res.statusCode === 401) {
|
||||
// 401错误 -> 清理用户信息,跳转到登录页
|
||||
// userStore.clearUserInfo()
|
||||
// uni.navigateTo({ url: '/pages/login/login' })
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
!options.hideErrorToast &&
|
||||
(await uni.showModal({
|
||||
title: '提示',
|
||||
content: res.data.msg || '请求错误',
|
||||
showCancel: false,
|
||||
}))
|
||||
uni.reLaunch({
|
||||
url: loginRoute,
|
||||
})
|
||||
|
||||
reject(res)
|
||||
} else {
|
||||
// 其他错误 -> 根据后端错误信息轻提示
|
||||
!options.hideErrorToast &&
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: (res.data as IResData<T>).msg || '请求错误',
|
||||
})
|
||||
(await uni.showModal({
|
||||
title: '提示',
|
||||
content: (res.data as IResData<T>).msg || '请求错误',
|
||||
showCancel: false,
|
||||
}))
|
||||
reject(res)
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user