diff --git a/src/pages/auth/loading.vue b/src/pages/auth/loading.vue index 9585ad6..5bea222 100644 --- a/src/pages/auth/loading.vue +++ b/src/pages/auth/loading.vue @@ -118,28 +118,24 @@ export default { // 静默授权:微信用 snsapi_base,支付宝用 auth_base const scopes = platform === 'alipay' ? 'auth_base' : 'snsapi_base'; + + // 构建 state(包含 device_id,回调时原样回传) + const stateObj = {}; + if (this.deviceId) { + stateObj.device_id = this.deviceId; + } + const res = await gatewayGet('/api/v1/user/auth/url', { app_no: appNo, scopes: scopes, redirect_uri: redirectUri, + state: Object.keys(stateObj).length > 0 ? JSON.stringify(stateObj) : '', }); const data = res.data || {}; if (data.code === 0 && data.data && data.data.auth_url) { - // 通过 state 传递 device_id(OAuth 标准保证 state 原样回传) - let authUrl = data.data.auth_url; - if (this.deviceId) { - const stateVal = encodeURIComponent(JSON.stringify({ device_id: this.deviceId })); - try { - const urlObj = new URL(authUrl); - urlObj.searchParams.set('state', stateVal); - authUrl = urlObj.toString(); - } catch (_) { - authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal; - } - } - // 跳转微信授权页(静默,不弹窗) - window.location.href = authUrl; + // 后端已将 state 写入授权 URL,直接跳转 + window.location.href = data.data.auth_url; } else { uni.showToast({ title: data.message || '获取授权失败', icon: 'none' }); setTimeout(() => this.goToScan(), 1500); @@ -279,30 +275,24 @@ export default { // 用户信息授权:微信用 snsapi_userinfo,支付宝用 auth_user const scopes = platform === 'alipay' ? 'auth_user' : 'snsapi_userinfo'; + + // 构建 state(包含 action 和 device_id,回调时原样回传) + const stateObj = { action: 'userinfo' }; + if (this.deviceId) { + stateObj.device_id = this.deviceId; + } + const res = await gatewayGet('/api/v1/user/auth/url', { app_no: appNo, scopes: scopes, redirect_uri: redirectUri, + state: JSON.stringify(stateObj), }); const data = res.data || {}; if (data.code === 0 && data.data && data.data.auth_url) { - // 通过 state 传递 action 和 device_id(OAuth 标准保证 state 原样回传) - const stateObj = { action: 'userinfo' }; - if (this.deviceId) { - stateObj.device_id = this.deviceId; - } - const stateVal = encodeURIComponent(JSON.stringify(stateObj)); - let authUrl = data.data.auth_url; - try { - const urlObj = new URL(authUrl); - urlObj.searchParams.set('state', stateVal); - authUrl = urlObj.toString(); - } catch (_) { - authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal; - } - // 跳转微信授权页(弹窗确认) - window.location.href = authUrl; + // 后端已将 state 写入授权 URL,直接跳转 + window.location.href = data.data.auth_url; } else { uni.showToast({ title: data.message || '获取授权失败', icon: 'none' }); setTimeout(() => this.goToHomeWithAction(), 1500); diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 883f3f3..755cfc1 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -344,34 +344,28 @@ export default { window.location.origin.replace(/^http:/, 'https:') + window.location.pathname ); - // 获取非静默授权链接(会弹窗确认) + // 构建 state(包含 action 和 device_id,回调时原样回传) + const stateObj = { action: 'userinfo' }; + if (this.deviceId) { + stateObj.device_id = this.deviceId; + } + + // 用户信息授权:微信用 snsapi_userinfo,支付宝用 auth_user + const platform = uni.getStorageSync('platform') || 'wechat'; + const scopes = platform === 'alipay' ? 'auth_user' : 'snsapi_userinfo'; + const res = await gatewayGet('/api/v1/user/auth/url', { app_no: appNo, - scopes: 'snsapi_userinfo', + scopes: scopes, redirect_uri: redirectUri, + state: JSON.stringify(stateObj), }); const data = res.data || {}; if (data.code === 0 && data.data && data.data.auth_url) { - // 通过 state 传递 action 和 device_id(OAuth 标准保证 state 原样回传) - const stateObj = { action: 'userinfo' }; - if (this.deviceId) { - stateObj.device_id = this.deviceId; - } - const stateVal = encodeURIComponent(JSON.stringify(stateObj)); - // 解析 auth_url,安全地设置 state 参数(避免重复) - let authUrl = data.data.auth_url; - try { - const urlObj = new URL(authUrl); - urlObj.searchParams.set('state', stateVal); - authUrl = urlObj.toString(); - } catch (_) { - // URL 解析失败时直接拼接 - authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal; - } - console.log('[index] 跳转授权页:', authUrl); - // 跳转微信授权页(弹窗确认) - window.location.href = authUrl; + console.log('[index] 跳转授权页:', data.data.auth_url); + // 后端已将 state 写入授权 URL,直接跳转 + window.location.href = data.data.auth_url; } else { console.warn('[index] 获取授权链接失败:', data.message); }