From dcb100e87df691a1eaebf4fae6214c35137880fe Mon Sep 17 00:00:00 2001
From: feige996 <1020102647@qq.com>
Date: Sun, 22 Jun 2025 16:47:41 +0800
Subject: [PATCH] =?UTF-8?q?feat(=E8=AF=B7=E6=B1=82):=20=E6=B7=BB=E5=8A=A0a?=
=?UTF-8?q?lova=E8=AF=B7=E6=B1=82=E6=BC=94=E7=A4=BA=E9=A1=B5=E9=9D=A2?=
=?UTF-8?q?=E5=8F=8A=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
新增alova请求演示页面,包含请求发送和重置功能
修改alova配置,添加请求拦截器和错误处理
---
src/pages.json | 8 +++++
src/pages/about/about.vue | 9 ++++++
src/pages/about/alova.vue | 60 ++++++++++++++++++++++++++++++++++++++
src/utils/request/alova.ts | 32 ++++++++++++++++++--
4 files changed, 107 insertions(+), 2 deletions(-)
create mode 100644 src/pages/about/alova.vue
diff --git a/src/pages.json b/src/pages.json
index 1fe326b..3c4f228 100644
--- a/src/pages.json
+++ b/src/pages.json
@@ -59,6 +59,14 @@
"style": {
"navigationBarTitleText": "关于"
}
+ },
+ {
+ "path": "pages/about/alova",
+ "type": "page",
+ "layout": "default",
+ "style": {
+ "navigationBarTitleText": "Alova 请求演示"
+ }
}
],
"subPackages": []
diff --git a/src/pages/about/about.vue b/src/pages/about/about.vue
index 71ca5fe..396e273 100644
--- a/src/pages/about/about.vue
+++ b/src/pages/about/about.vue
@@ -20,6 +20,12 @@ const { safeAreaInsets } = uni.getSystemInfoSync()
// }
// testOxlint('oxlint')
console.log('about')
+
+function gotoAlova() {
+ uni.navigateTo({
+ url: '/pages/about/alova',
+ })
+}
@@ -32,6 +38,9 @@ console.log('about')
+
diff --git a/src/pages/about/alova.vue b/src/pages/about/alova.vue
new file mode 100644
index 0000000..fada4e6
--- /dev/null
+++ b/src/pages/about/alova.vue
@@ -0,0 +1,60 @@
+
+{
+ layout: 'default',
+ style: {
+ navigationBarTitleText: 'Alova 请求演示',
+ },
+}
+
+
+
+
+
+
+
+ 发送请求
+
+
+
+ loading...
+
+
+
+ 请求数据如下
+
+
+ {{ JSON.stringify(data) }}
+
+
+
+
+ 重置数据
+
+
+
+
+
diff --git a/src/utils/request/alova.ts b/src/utils/request/alova.ts
index a0cbdde..77584d3 100644
--- a/src/utils/request/alova.ts
+++ b/src/utils/request/alova.ts
@@ -1,7 +1,35 @@
import AdapterUniapp from '@alova/adapter-uniapp'
import { createAlova } from 'alova'
-const http = createAlova({
- baseURL: import.meta.env.VITE_APP_PROXY_PREFIX,
+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
+ 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 || '请求错误')
+ }
+ },
})