hookehuyr

fix(auth): 优化授权流程防止重复跳转和请求

添加防抖机制防止短时间内重复跳转至授权页
修改授权失败提示为模态框并增加失败状态标记
更新跳转逻辑防止重复导航
......@@ -17,7 +17,16 @@
import Taro, { useDidShow } from '@tarojs/taro'
import { silentAuth, returnToOriginalPage } from '@/utils/authRedirect'
let last_try_at = 0
let has_shown_fail_modal = false
let has_failed = false
useDidShow(() => {
if (has_failed) return
const now = Date.now()
if (now - last_try_at < 1200) return
last_try_at = now
/**
* 尝试静默授权
* - 授权成功后回跳到来源页
......@@ -25,8 +34,16 @@ useDidShow(() => {
*/
silentAuth()
.then(() => returnToOriginalPage())
.catch((error) => {
Taro.showToast({ title: error?.message || '授权失败', icon: 'none' })
.catch(async (error) => {
has_failed = true
if (has_shown_fail_modal) return
has_shown_fail_modal = true
await Taro.showModal({
title: '提示',
content: error?.message || '授权失败,请稍后再尝试',
showCancel: false,
confirmText: '我知道了',
})
})
})
</script>
......
......@@ -2,6 +2,9 @@ import Taro from '@tarojs/taro'
import { routerStore } from '@/stores/router'
import BASE_URL, { REQUEST_DEFAULT_PARAMS } from './config'
let last_navigate_auth_at = 0
let navigating_to_auth = false
/**
* 授权与回跳相关工具
* - 统一管理:保存来源页、静默授权、跳转授权页、授权后回跳
......@@ -229,22 +232,35 @@ export const silentAuth = async (on_success, on_error, options) => {
* @returns {void} 无返回值
*/
export const navigateToAuth = (return_path) => {
const pages = Taro.getCurrentPages()
const current_page = pages[pages.length - 1]
const current_route = current_page?.route
if (current_route === 'pages/auth/index') {
return
}
const now = Date.now()
if (navigating_to_auth) return
if (now - last_navigate_auth_at < 1200) return
last_navigate_auth_at = now
navigating_to_auth = true
if (return_path) {
saveCurrentPagePath(return_path)
} else {
saveCurrentPagePath()
}
const pages = Taro.getCurrentPages()
const current_page = pages[pages.length - 1]
const current_route = current_page?.route
if (current_route === 'pages/auth/index') {
return
const done = () => {
setTimeout(() => {
navigating_to_auth = false
}, 300)
}
Taro.navigateTo({ url: '/pages/auth/index' }).catch(() => {
return Taro.redirectTo({ url: '/pages/auth/index' })
})
Taro.navigateTo({ url: '/pages/auth/index' })
.catch(() => Taro.redirectTo({ url: '/pages/auth/index' }))
.finally(done)
}
/**
......@@ -324,12 +340,12 @@ export const handleSharePageAuth = async (options, callback) => {
if (typeof callback === 'function') callback()
},
() => {
Taro.navigateTo({ url: '/pages/auth/index' })
navigateToAuth()
}
)
return true
} catch (error) {
Taro.navigateTo({ url: '/pages/auth/index' })
navigateToAuth()
return false
}
}
......
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-01-13 20:49:09
* @LastEditTime: 2026-01-16 17:05:21
* @FilePath: /xyxBooking-weapp/src/utils/config.js
* @Description: 文件描述
*/
// TAG:服务器环境配置
const BASE_URL = process.env.NODE_ENV === 'production'
? 'https://oa.onwall.cn'
// ? 'https://oa-dev.onwall.cn'
: 'https://oa-dev.onwall.cn'
/**
......