对接接口
This commit is contained in:
@ -56,15 +56,26 @@ const alovaInstance = createAlova({
|
||||
}
|
||||
|
||||
const { config } = method
|
||||
console.log("🚀 ~ config.meta?.ignoreAuth:", config)
|
||||
|
||||
const ignoreAuth = !config.meta?.ignoreAuth
|
||||
console.log('ignoreAuth===>', ignoreAuth)
|
||||
// 处理认证信息 自行处理认证问题
|
||||
if (ignoreAuth) {
|
||||
const token = 'getToken()'
|
||||
// const token = 'getToken()'
|
||||
// if (!token) {
|
||||
// throw new Error('[请求错误]:未登录')
|
||||
// }
|
||||
// method.config.headers.token = token;
|
||||
|
||||
const userStore = useUserStore()
|
||||
const { token } = userStore.userInfo as unknown as IUserInfo
|
||||
console.log("🚀 ~ userStore.userInfo:", userStore.userInfo)
|
||||
console.log("🚀 ~ token:", token)
|
||||
if (!token) {
|
||||
throw new Error('[请求错误]:未登录')
|
||||
}
|
||||
// method.config.headers.token = token;
|
||||
method.config.headers.token = token;
|
||||
}
|
||||
|
||||
// 处理动态域名
|
||||
@ -102,12 +113,22 @@ const alovaInstance = createAlova({
|
||||
// }
|
||||
|
||||
// 处理业务逻辑错误
|
||||
const { code, message, data } = rawData as IResponse
|
||||
const { code, msg, data } = rawData as IResponse
|
||||
// if (code === ResultEnum.Unauthorized) {
|
||||
// if (config.meta?.toast !== false) {
|
||||
// toast.info(msg)
|
||||
// setTimeout(() => {
|
||||
// uni.navigateTo({ url: '/pages/login/mobile' })
|
||||
// })
|
||||
// }
|
||||
// throw new Error(`登录超时[${code}]:${msg}`)
|
||||
// }
|
||||
|
||||
if (code !== ResultEnum.Success) {
|
||||
if (config.meta?.toast !== false) {
|
||||
toast.warning(message)
|
||||
toast.warning(msg)
|
||||
}
|
||||
throw new Error(`请求错误[${code}]:${message}`)
|
||||
throw new Error(`请求错误[${code}]:${msg}`)
|
||||
}
|
||||
// 处理成功响应,返回业务数据
|
||||
return data
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
export enum ResultEnum {
|
||||
Success = 1, // 成功
|
||||
Error = 0, // 错误
|
||||
Unauthorized = 401, // 未授权
|
||||
Unauthorized = -1, // 未授权
|
||||
Forbidden = 403, // 禁止访问(原为forbidden)
|
||||
NotFound = 404, // 未找到(原为notFound)
|
||||
MethodNotAllowed = 405, // 方法不允许(原为methodNotAllowed)
|
||||
|
||||
@ -11,7 +11,7 @@ export type CustomRequestOptions = UniApp.RequestOptions & {
|
||||
export interface IResponse<T = any> {
|
||||
code: number | string
|
||||
data: T
|
||||
message: string
|
||||
msg: string
|
||||
status: string | number
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user