Files
aiot-uniapp/src/utils/request/alova.ts
feige996 dcb100e87d feat(请求): 添加alova请求演示页面及功能
新增alova请求演示页面,包含请求发送和重置功能
修改alova配置,添加请求拦截器和错误处理
2025-06-22 16:47:41 +08:00

36 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AdapterUniapp from '@alova/adapter-uniapp'
import { createAlova } from 'alova'
const baseURL = JSON.parse(__VITE_APP_PROXY__)
? import.meta.env.VITE_APP_PROXY_PREFIX
: import.meta.env.VITE_SERVER_BASEURL
export const http = createAlova({
baseURL,
...AdapterUniapp(),
async responded(res: UniApp.RequestSuccessCallbackResult, method) {
console.log('responded:', method, res)
// 请求成功的拦截器
// 状态码 2xx参考 axios 的设计
const resData = res.data as IResData<any>
if (res.statusCode >= 200 && res.statusCode < 300) {
// 2.1 提取核心数据 res.data
return resData.data
}
else if (res.statusCode === 401) {
// 401错误 -> 清理用户信息,跳转到登录页
// userStore.clearUserInfo()
// uni.navigateTo({ url: '/pages/login/login' })
console.log(res)
throw new Error(resData.msg || '401错误')
}
else {
uni.showToast({
icon: 'none',
title: (resData).msg || '请求错误',
})
throw new Error(resData.msg || '请求错误')
}
},
})