fix(auth): 优化授权流程防止重复跳转和请求
添加防抖机制防止短时间内重复跳转至授权页 修改授权失败提示为模态框并增加失败状态标记 更新跳转逻辑防止重复导航
Showing
3 changed files
with
47 additions
and
13 deletions
| ... | @@ -17,7 +17,16 @@ | ... | @@ -17,7 +17,16 @@ |
| 17 | import Taro, { useDidShow } from '@tarojs/taro' | 17 | import Taro, { useDidShow } from '@tarojs/taro' |
| 18 | import { silentAuth, returnToOriginalPage } from '@/utils/authRedirect' | 18 | import { silentAuth, returnToOriginalPage } from '@/utils/authRedirect' |
| 19 | 19 | ||
| 20 | +let last_try_at = 0 | ||
| 21 | +let has_shown_fail_modal = false | ||
| 22 | +let has_failed = false | ||
| 23 | + | ||
| 20 | useDidShow(() => { | 24 | useDidShow(() => { |
| 25 | + if (has_failed) return | ||
| 26 | + const now = Date.now() | ||
| 27 | + if (now - last_try_at < 1200) return | ||
| 28 | + last_try_at = now | ||
| 29 | + | ||
| 21 | /** | 30 | /** |
| 22 | * 尝试静默授权 | 31 | * 尝试静默授权 |
| 23 | * - 授权成功后回跳到来源页 | 32 | * - 授权成功后回跳到来源页 |
| ... | @@ -25,8 +34,16 @@ useDidShow(() => { | ... | @@ -25,8 +34,16 @@ useDidShow(() => { |
| 25 | */ | 34 | */ |
| 26 | silentAuth() | 35 | silentAuth() |
| 27 | .then(() => returnToOriginalPage()) | 36 | .then(() => returnToOriginalPage()) |
| 28 | - .catch((error) => { | 37 | + .catch(async (error) => { |
| 29 | - Taro.showToast({ title: error?.message || '授权失败', icon: 'none' }) | 38 | + has_failed = true |
| 39 | + if (has_shown_fail_modal) return | ||
| 40 | + has_shown_fail_modal = true | ||
| 41 | + await Taro.showModal({ | ||
| 42 | + title: '提示', | ||
| 43 | + content: error?.message || '授权失败,请稍后再尝试', | ||
| 44 | + showCancel: false, | ||
| 45 | + confirmText: '我知道了', | ||
| 46 | + }) | ||
| 30 | }) | 47 | }) |
| 31 | }) | 48 | }) |
| 32 | </script> | 49 | </script> | ... | ... |
| ... | @@ -2,6 +2,9 @@ import Taro from '@tarojs/taro' | ... | @@ -2,6 +2,9 @@ import Taro from '@tarojs/taro' |
| 2 | import { routerStore } from '@/stores/router' | 2 | import { routerStore } from '@/stores/router' |
| 3 | import BASE_URL, { REQUEST_DEFAULT_PARAMS } from './config' | 3 | import BASE_URL, { REQUEST_DEFAULT_PARAMS } from './config' |
| 4 | 4 | ||
| 5 | +let last_navigate_auth_at = 0 | ||
| 6 | +let navigating_to_auth = false | ||
| 7 | + | ||
| 5 | /** | 8 | /** |
| 6 | * 授权与回跳相关工具 | 9 | * 授权与回跳相关工具 |
| 7 | * - 统一管理:保存来源页、静默授权、跳转授权页、授权后回跳 | 10 | * - 统一管理:保存来源页、静默授权、跳转授权页、授权后回跳 |
| ... | @@ -229,22 +232,35 @@ export const silentAuth = async (on_success, on_error, options) => { | ... | @@ -229,22 +232,35 @@ export const silentAuth = async (on_success, on_error, options) => { |
| 229 | * @returns {void} 无返回值 | 232 | * @returns {void} 无返回值 |
| 230 | */ | 233 | */ |
| 231 | export const navigateToAuth = (return_path) => { | 234 | export const navigateToAuth = (return_path) => { |
| 235 | + const pages = Taro.getCurrentPages() | ||
| 236 | + const current_page = pages[pages.length - 1] | ||
| 237 | + const current_route = current_page?.route | ||
| 238 | + if (current_route === 'pages/auth/index') { | ||
| 239 | + return | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + const now = Date.now() | ||
| 243 | + if (navigating_to_auth) return | ||
| 244 | + if (now - last_navigate_auth_at < 1200) return | ||
| 245 | + | ||
| 246 | + last_navigate_auth_at = now | ||
| 247 | + navigating_to_auth = true | ||
| 248 | + | ||
| 232 | if (return_path) { | 249 | if (return_path) { |
| 233 | saveCurrentPagePath(return_path) | 250 | saveCurrentPagePath(return_path) |
| 234 | } else { | 251 | } else { |
| 235 | saveCurrentPagePath() | 252 | saveCurrentPagePath() |
| 236 | } | 253 | } |
| 237 | 254 | ||
| 238 | - const pages = Taro.getCurrentPages() | 255 | + const done = () => { |
| 239 | - const current_page = pages[pages.length - 1] | 256 | + setTimeout(() => { |
| 240 | - const current_route = current_page?.route | 257 | + navigating_to_auth = false |
| 241 | - if (current_route === 'pages/auth/index') { | 258 | + }, 300) |
| 242 | - return | ||
| 243 | } | 259 | } |
| 244 | 260 | ||
| 245 | - Taro.navigateTo({ url: '/pages/auth/index' }).catch(() => { | 261 | + Taro.navigateTo({ url: '/pages/auth/index' }) |
| 246 | - return Taro.redirectTo({ url: '/pages/auth/index' }) | 262 | + .catch(() => Taro.redirectTo({ url: '/pages/auth/index' })) |
| 247 | - }) | 263 | + .finally(done) |
| 248 | } | 264 | } |
| 249 | 265 | ||
| 250 | /** | 266 | /** |
| ... | @@ -324,12 +340,12 @@ export const handleSharePageAuth = async (options, callback) => { | ... | @@ -324,12 +340,12 @@ export const handleSharePageAuth = async (options, callback) => { |
| 324 | if (typeof callback === 'function') callback() | 340 | if (typeof callback === 'function') callback() |
| 325 | }, | 341 | }, |
| 326 | () => { | 342 | () => { |
| 327 | - Taro.navigateTo({ url: '/pages/auth/index' }) | 343 | + navigateToAuth() |
| 328 | } | 344 | } |
| 329 | ) | 345 | ) |
| 330 | return true | 346 | return true |
| 331 | } catch (error) { | 347 | } catch (error) { |
| 332 | - Taro.navigateTo({ url: '/pages/auth/index' }) | 348 | + navigateToAuth() |
| 333 | return false | 349 | return false |
| 334 | } | 350 | } |
| 335 | } | 351 | } | ... | ... |
| 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-13 20:49:09 | 4 | + * @LastEditTime: 2026-01-16 17:05:21 |
| 5 | * @FilePath: /xyxBooking-weapp/src/utils/config.js | 5 | * @FilePath: /xyxBooking-weapp/src/utils/config.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| 8 | // TAG:服务器环境配置 | 8 | // TAG:服务器环境配置 |
| 9 | const BASE_URL = process.env.NODE_ENV === 'production' | 9 | const BASE_URL = process.env.NODE_ENV === 'production' |
| 10 | ? 'https://oa.onwall.cn' | 10 | ? 'https://oa.onwall.cn' |
| 11 | + // ? 'https://oa-dev.onwall.cn' | ||
| 11 | : 'https://oa-dev.onwall.cn' | 12 | : 'https://oa-dev.onwall.cn' |
| 12 | 13 | ||
| 13 | /** | 14 | /** | ... | ... |
-
Please register or login to post a comment