hookehuyr

refactor(auth): 优化授权逻辑和代码注释

移除重复的授权请求检查,简化401处理逻辑
添加详细的代码注释说明网络检查和预加载逻辑
1 /* 1 /*
2 * @Date: 2025-06-28 10:33:00 2 * @Date: 2025-06-28 10:33:00
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2026-01-07 20:54:02 4 + * @LastEditTime: 2026-01-08 19:36:25
5 * @FilePath: /xyxBooking-weapp/src/app.js 5 * @FilePath: /xyxBooking-weapp/src/app.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
...@@ -14,6 +14,11 @@ import Taro from '@tarojs/taro' ...@@ -14,6 +14,11 @@ import Taro from '@tarojs/taro'
14 import { qrcodeListAPI } from '@/api/index' 14 import { qrcodeListAPI } from '@/api/index'
15 import { formatDatetime } from '@/utils/tools' 15 import { formatDatetime } from '@/utils/tools'
16 16
17 +/**
18 + * 格式化支付记录,按 pay_id 分组,相同 pay_id 下的记录 sort 为 0,否则为 1
19 + * @param {*} data 支付记录数组
20 + * @returns 格式化后的支付记录数组
21 + */
17 const formatGroup = (data) => { 22 const formatGroup = (data) => {
18 let lastPayId = null; 23 let lastPayId = null;
19 for (let i = 0; i < data.length; i++) { 24 for (let i = 0; i < data.length; i++) {
...@@ -38,10 +43,17 @@ const App = createApp({ ...@@ -38,10 +43,17 @@ const App = createApp({
38 .join('&') 43 .join('&')
39 const full_path = query_string ? `${path}?${query_string}` : path 44 const full_path = query_string ? `${path}?${query_string}` : path
40 45
46 + // 保存当前页面路径,用于授权后跳转回原页面
41 if (full_path) { 47 if (full_path) {
42 saveCurrentPagePath(full_path) 48 saveCurrentPagePath(full_path)
43 } 49 }
44 50
51 + /**
52 + * 预加载二维码数据
53 + * - 仅在有网络连接时调用
54 + * - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA)
55 + * - 失败则移除缓存
56 + */
45 const preloadQrData = async () => { 57 const preloadQrData = async () => {
46 try { 58 try {
47 const { code, data } = await qrcodeListAPI(); 59 const { code, data } = await qrcodeListAPI();
...@@ -64,6 +76,12 @@ const App = createApp({ ...@@ -64,6 +76,12 @@ const App = createApp({
64 } 76 }
65 }; 77 };
66 78
79 + /**
80 + * 检查网络状态并预加载二维码数据
81 + * - 仅在有网络连接时调用
82 + * - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA)
83 + * - 失败则移除缓存
84 + */
67 const checkNetworkAndPreload = () => { 85 const checkNetworkAndPreload = () => {
68 Taro.getNetworkType({ 86 Taro.getNetworkType({
69 success: (res) => { 87 success: (res) => {
...@@ -75,12 +93,17 @@ const App = createApp({ ...@@ -75,12 +93,17 @@ const App = createApp({
75 }); 93 });
76 }; 94 };
77 95
96 + /**
97 + * 监听网络状态变化
98 + * - 网络连接时预加载二维码数据
99 + */
78 Taro.onNetworkStatusChange((res) => { 100 Taro.onNetworkStatusChange((res) => {
79 if (res.isConnected) { 101 if (res.isConnected) {
80 preloadQrData(); 102 preloadQrData();
81 } 103 }
82 }); 104 });
83 105
106 + // 初始检查网络状态并预加载二维码数据
84 checkNetworkAndPreload(); 107 checkNetworkAndPreload();
85 108
86 if (!needAuth()) return 109 if (!needAuth()) return
...@@ -88,9 +111,13 @@ const App = createApp({ ...@@ -88,9 +111,13 @@ const App = createApp({
88 if (path === 'pages/auth/index') return 111 if (path === 'pages/auth/index') return
89 112
90 try { 113 try {
114 + // 静默授权
91 await silentAuth() 115 await silentAuth()
116 + // 授权成功后检查网络状态并预加载二维码数据
92 checkNetworkAndPreload(); 117 checkNetworkAndPreload();
93 } catch (error) { 118 } catch (error) {
119 + console.error('静默授权失败:', error)
120 + // 授权失败后跳转到授权页,携带当前页路径作为回跳目标
94 navigateToAuth(full_path || undefined) 121 navigateToAuth(full_path || undefined)
95 } 122 }
96 }, 123 },
......
1 /* 1 /*
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2026-01-08 16:27:50 4 + * @LastEditTime: 2026-01-08 19:25:17
5 * @FilePath: /xyxBooking-weapp/src/utils/request.js 5 * @FilePath: /xyxBooking-weapp/src/utils/request.js
6 * @Description: 简单axios封装,后续按实际处理 6 * @Description: 简单axios封装,后续按实际处理
7 */ 7 */
...@@ -105,11 +105,10 @@ service.interceptors.response.use( ...@@ -105,11 +105,10 @@ service.interceptors.response.use(
105 if (res.code === 401) { 105 if (res.code === 401) {
106 const config = response?.config || {} 106 const config = response?.config || {}
107 /** 107 /**
108 - * 避免死循环/重复授权: 108 + * 避免死循环/重复重试:
109 - * - __is_auth_request:本次请求就是“换取会话”的授权请求,不应再次触发刷新
110 * - __is_retry:本次请求是 401 后的重试请求,如果仍 401,不再继续重试 109 * - __is_retry:本次请求是 401 后的重试请求,如果仍 401,不再继续重试
111 */ 110 */
112 - if (config.__is_auth_request || config.__is_retry) { 111 + if (config.__is_retry) {
113 return response 112 return response
114 } 113 }
115 114
......