feat(auth): 添加静默授权处理逻辑作为授权重定向的优先方案
当用户未授权时,优先尝试静默授权,仅在失败或异常时跳转授权页面
Showing
1 changed file
with
23 additions
and
3 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-01-25 10:00:00 | 2 | * @Date: 2025-01-25 10:00:00 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-12 12:24:03 | 4 | + * @LastEditTime: 2025-09-12 21:32:12 |
| 5 | * @FilePath: /lls_program/src/utils/authRedirect.js | 5 | * @FilePath: /lls_program/src/utils/authRedirect.js |
| 6 | * @Description: 授权重定向处理工具函数 | 6 | * @Description: 授权重定向处理工具函数 |
| 7 | */ | 7 | */ |
| ... | @@ -171,18 +171,38 @@ export const handleSharePageAuth = async (options, callback) => { | ... | @@ -171,18 +171,38 @@ export const handleSharePageAuth = async (options, callback) => { |
| 171 | const sessionid = wx.getStorageSync('sessionid') | 171 | const sessionid = wx.getStorageSync('sessionid') |
| 172 | 172 | ||
| 173 | if (!sessionid) { | 173 | if (!sessionid) { |
| 174 | - // 没有授权,需要先授权 | 174 | + // 没有授权,使用静默授权 |
| 175 | if (isFromShare(options)) { | 175 | if (isFromShare(options)) { |
| 176 | // 来自分享,保存当前页面路径用于授权后返回 | 176 | // 来自分享,保存当前页面路径用于授权后返回 |
| 177 | saveCurrentPagePath() | 177 | saveCurrentPagePath() |
| 178 | } | 178 | } |
| 179 | 179 | ||
| 180 | - // 跳转到授权页面 | 180 | + try { |
| 181 | + // 使用静默授权 | ||
| 182 | + await silentAuth( | ||
| 183 | + () => { | ||
| 184 | + // 静默授权成功 | ||
| 185 | + if (callback && typeof callback === 'function') { | ||
| 186 | + callback() | ||
| 187 | + } | ||
| 188 | + }, | ||
| 189 | + () => { | ||
| 190 | + // 静默授权失败时,仍然跳转到授权页面作为备选方案 | ||
| 191 | + Taro.navigateTo({ | ||
| 192 | + url: '/pages/auth/index' | ||
| 193 | + }) | ||
| 194 | + } | ||
| 195 | + ) | ||
| 196 | + return true | ||
| 197 | + } catch (error) { | ||
| 198 | + console.error('静默授权异常:', error) | ||
| 199 | + // 发生异常时,跳转到授权页面作为备选方案 | ||
| 181 | Taro.navigateTo({ | 200 | Taro.navigateTo({ |
| 182 | url: '/pages/auth/index' | 201 | url: '/pages/auth/index' |
| 183 | }) | 202 | }) |
| 184 | return false | 203 | return false |
| 185 | } | 204 | } |
| 205 | + } | ||
| 186 | 206 | ||
| 187 | // 已授权,执行回调 | 207 | // 已授权,执行回调 |
| 188 | if (callback && typeof callback === 'function') { | 208 | if (callback && typeof callback === 'function') { | ... | ... |
-
Please register or login to post a comment