fix(router): 优化微信授权流程和URL处理
移除restoreHashAfterOAuth调用,简化微信授权URL处理逻辑 统一使用完整URL进行微信授权和JSSDK配置
Showing
3 changed files
with
24 additions
and
27 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-03-20 19:53:12 | 2 | * @Date: 2025-03-20 19:53:12 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-12-09 16:43:21 | 4 | + * @LastEditTime: 2025-12-10 11:51:27 |
| 5 | * @FilePath: /mlaj/src/App.vue | 5 | * @FilePath: /mlaj/src/App.vue |
| 6 | * @Description: 入口文件 | 6 | * @Description: 入口文件 |
| 7 | --> | 7 | --> |
| ... | @@ -33,23 +33,20 @@ provideCart('single'); // 提供全局购物车状态,单一商品模式 | ... | @@ -33,23 +33,20 @@ provideCart('single'); // 提供全局购物车状态,单一商品模式 |
| 33 | * @returns {void} | 33 | * @returns {void} |
| 34 | */ | 34 | */ |
| 35 | const initWxConfig = async () => { | 35 | const initWxConfig = async () => { |
| 36 | - // 使用不带 hash 的完整 URL 参与签名,规避重复刷新问题 | 36 | + const raw_url = encodeURIComponent(location.pathname + location.hash); |
| 37 | - const sign_url = encodeURIComponent(window.location.href.split('#')[0]); | 37 | + try { |
| 38 | - try { | 38 | + const wxJs = await wxJsAPI({ url: raw_url }) |
| 39 | - const wxJs = await wxJsAPI({ url: sign_url }); | 39 | + wxJs.data.jsApiList = apiList |
| 40 | - wxJs.data.jsApiList = apiList; | 40 | + wx.config(wxJs.data) |
| 41 | - wx.config(wxJs.data); | 41 | + wx.ready(() => { |
| 42 | - wx.ready(() => { | 42 | + wx.showAllNonBaseMenuItem() |
| 43 | - // 微信 JSSDK 初始化完成,展示所有非基础菜单项 | 43 | + }) |
| 44 | - wx.showAllNonBaseMenuItem(); | 44 | + wx.error((err) => { |
| 45 | - }); | 45 | + console.warn('微信配置初始化失败:', err) |
| 46 | - wx.error((err) => { | 46 | + }) |
| 47 | - // 微信 JSSDK 初始化失败日志 | 47 | + } catch (error) { |
| 48 | - console.warn('微信配置初始化失败:', err); | 48 | + console.error('初始化微信配置失败:', error) |
| 49 | - }); | 49 | + } |
| 50 | - } catch (error) { | ||
| 51 | - console.error('初始化微信配置失败:', error); | ||
| 52 | - } | ||
| 53 | } | 50 | } |
| 54 | 51 | ||
| 55 | // 在非开发环境下初始化微信配置 | 52 | // 在非开发环境下初始化微信配置 |
| ... | @@ -92,7 +89,8 @@ onUnmounted(() => { | ... | @@ -92,7 +89,8 @@ onUnmounted(() => { |
| 92 | <div v-if="Component"> | 89 | <div v-if="Component"> |
| 93 | <component :is="Component" /> | 90 | <component :is="Component" /> |
| 94 | </div> | 91 | </div> |
| 95 | - <div v-else class="flex items-center justify-center h-screen bg-gradient-to-br from-green-50 via-teal-50 to-blue-50"> | 92 | + <div v-else |
| 93 | + class="flex items-center justify-center h-screen bg-gradient-to-br from-green-50 via-teal-50 to-blue-50"> | ||
| 96 | <div class="bg-white/20 backdrop-blur-md rounded-xl p-6 shadow-lg"> | 94 | <div class="bg-white/20 backdrop-blur-md rounded-xl p-6 shadow-lg"> |
| 97 | <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-green-500 mx-auto"></div> | 95 | <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-green-500 mx-auto"></div> |
| 98 | <p class="mt-4 text-gray-700">加载中...</p> | 96 | <p class="mt-4 text-gray-700">加载中...</p> |
| ... | @@ -110,10 +108,10 @@ onUnmounted(() => { | ... | @@ -110,10 +108,10 @@ onUnmounted(() => { |
| 110 | ol { | 108 | ol { |
| 111 | list-style-type: decimal; | 109 | list-style-type: decimal; |
| 112 | padding: 0.25rem 1rem; | 110 | padding: 0.25rem 1rem; |
| 111 | + | ||
| 113 | li { | 112 | li { |
| 114 | margin-bottom: 0.25rem; | 113 | margin-bottom: 0.25rem; |
| 115 | } | 114 | } |
| 116 | } | 115 | } |
| 117 | } | 116 | } |
| 118 | - | ||
| 119 | </style> | 117 | </style> | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-06-20 10:00:51 | 4 | + * @LastEditTime: 2025-12-10 11:50:37 |
| 5 | * @FilePath: /mlaj/src/main.js | 5 | * @FilePath: /mlaj/src/main.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| ... | @@ -57,6 +57,6 @@ function restoreHashAfterOAuth() { | ... | @@ -57,6 +57,6 @@ function restoreHashAfterOAuth() { |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | // 在安装路由前进行一次 hash 复原,确保初始路由正确 | 59 | // 在安装路由前进行一次 hash 复原,确保初始路由正确 |
| 60 | -restoreHashAfterOAuth() | 60 | +// restoreHashAfterOAuth() |
| 61 | app.use(router) | 61 | app.use(router) |
| 62 | app.mount('#app') | 62 | app.mount('#app') | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-12-09 16:47:55 | 4 | + * @LastEditTime: 2025-12-10 11:50:14 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| ... | @@ -82,10 +82,9 @@ export const startWxAuth = async () => { | ... | @@ -82,10 +82,9 @@ export const startWxAuth = async () => { |
| 82 | // 探测失败不影响授权流程,继续跳转 | 82 | // 探测失败不影响授权流程,继续跳转 |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | - // 跳转到微信授权地址(签名与回跳使用不含 hash 的完整 URL),并通过 ret_hash 参数保留授权前页面位置 | 85 | + // 跳转到微信授权地址 |
| 86 | - const base_url = encodeURIComponent(window.location.href.split('#')[0]); | 86 | + const raw_url = encodeURIComponent(location.href); |
| 87 | - const ret_hash = encodeURIComponent(window.location.hash || ''); | 87 | + const short_url = `/srv/?f=behalo&a=openid&res=${raw_url}`; |
| 88 | - const short_url = `/srv/?f=behalo&a=openid&res=${base_url}&ret_hash=${ret_hash}`; | ||
| 89 | location.href = short_url; | 88 | location.href = short_url; |
| 90 | } | 89 | } |
| 91 | 90 | ... | ... |
-
Please register or login to post a comment