Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hooke
/
xyxBooking-weapp
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
hookehuyr
2026-01-08 19:37:14 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e8c22fe80de6b7dee96afb61ac68e98bdc3e5692
e8c22fe8
1 parent
410e0e6e
refactor(auth): 优化授权逻辑和代码注释
移除重复的授权请求检查,简化401处理逻辑 添加详细的代码注释说明网络检查和预加载逻辑
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
5 deletions
src/app.js
src/utils/request.js
src/app.js
View file @
e8c22fe
/*
* @Date: 2025-06-28 10:33:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-01-0
7 20:54:02
* @LastEditTime: 2026-01-0
8 19:36:25
* @FilePath: /xyxBooking-weapp/src/app.js
* @Description: 文件描述
*/
...
...
@@ -14,6 +14,11 @@ import Taro from '@tarojs/taro'
import
{
qrcodeListAPI
}
from
'@/api/index'
import
{
formatDatetime
}
from
'@/utils/tools'
/**
* 格式化支付记录,按 pay_id 分组,相同 pay_id 下的记录 sort 为 0,否则为 1
* @param {*} data 支付记录数组
* @returns 格式化后的支付记录数组
*/
const
formatGroup
=
(
data
)
=>
{
let
lastPayId
=
null
;
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
...
...
@@ -38,10 +43,17 @@ const App = createApp({
.
join
(
'&'
)
const
full_path
=
query_string
?
`
${
path
}
?
${
query_string
}
`
:
path
// 保存当前页面路径,用于授权后跳转回原页面
if
(
full_path
)
{
saveCurrentPagePath
(
full_path
)
}
/**
* 预加载二维码数据
* - 仅在有网络连接时调用
* - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA)
* - 失败则移除缓存
*/
const
preloadQrData
=
async
()
=>
{
try
{
const
{
code
,
data
}
=
await
qrcodeListAPI
();
...
...
@@ -64,6 +76,12 @@ const App = createApp({
}
};
/**
* 检查网络状态并预加载二维码数据
* - 仅在有网络连接时调用
* - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA)
* - 失败则移除缓存
*/
const
checkNetworkAndPreload
=
()
=>
{
Taro
.
getNetworkType
({
success
:
(
res
)
=>
{
...
...
@@ -75,12 +93,17 @@ const App = createApp({
});
};
/**
* 监听网络状态变化
* - 网络连接时预加载二维码数据
*/
Taro
.
onNetworkStatusChange
((
res
)
=>
{
if
(
res
.
isConnected
)
{
preloadQrData
();
}
});
// 初始检查网络状态并预加载二维码数据
checkNetworkAndPreload
();
if
(
!
needAuth
())
return
...
...
@@ -88,9 +111,13 @@ const App = createApp({
if
(
path
===
'pages/auth/index'
)
return
try
{
// 静默授权
await
silentAuth
()
// 授权成功后检查网络状态并预加载二维码数据
checkNetworkAndPreload
();
}
catch
(
error
)
{
console
.
error
(
'静默授权失败:'
,
error
)
// 授权失败后跳转到授权页,携带当前页路径作为回跳目标
navigateToAuth
(
full_path
||
undefined
)
}
},
...
...
src/utils/request.js
View file @
e8c22fe
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-01-08 1
6:27:50
* @LastEditTime: 2026-01-08 1
9:25:17
* @FilePath: /xyxBooking-weapp/src/utils/request.js
* @Description: 简单axios封装,后续按实际处理
*/
...
...
@@ -105,11 +105,10 @@ service.interceptors.response.use(
if
(
res
.
code
===
401
)
{
const
config
=
response
?.
config
||
{}
/**
* 避免死循环/重复授权:
* - __is_auth_request:本次请求就是“换取会话”的授权请求,不应再次触发刷新
* 避免死循环/重复重试:
* - __is_retry:本次请求是 401 后的重试请求,如果仍 401,不再继续重试
*/
if
(
config
.
__is_
auth_request
||
config
.
__is_
retry
)
{
if
(
config
.
__is_retry
)
{
return
response
}
...
...
Please
register
or
login
to post a comment