简化state参数
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 1m7s

This commit is contained in:
kk
2026-07-20 17:45:44 +08:00
parent 558e1380db
commit e1f71fb220
2 changed files with 12 additions and 61 deletions
+8 -36
View File
@@ -38,28 +38,15 @@ export default {
code = urlParams.get('code') || urlParams.get('auth_code') || '';
}
// 解析 state 参数(OAuth 标准保证 state 原样回传,包含 action 和 device_id
if (!this.action || !this.deviceId) {
const stateRaw = options.state || '';
if (stateRaw) {
try {
const stateObj = JSON.parse(decodeURIComponent(stateRaw));
if (!this.action && stateObj.action) {
this.action = stateObj.action;
}
if (!this.deviceId && stateObj.device_id) {
this.deviceId = stateObj.device_id;
}
} catch (_) {
// state 解析失败,继续用 localStorage 兜底
}
// 解析 state 参数(OAuth 原样回传,"base" 或 "userinfo"
if (!this.action) {
const state = options.state || '';
if (state === 'userinfo') {
this.action = 'userinfo';
}
}
// 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发,localStorage 由跳转函数清理)
if (!this.action) {
this.action = localStorage.getItem('__auth_action') || '';
}
if (!this.deviceId) {
this.deviceId = localStorage.getItem('__auth_device_id') || '';
}
@@ -133,17 +120,11 @@ 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) : '',
state: 'base',
});
const data = res.data || {};
@@ -298,7 +279,6 @@ export default {
this.statusText = '正在获取用户信息...';
// 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
localStorage.setItem('__auth_action', 'userinfo');
if (this.deviceId) {
localStorage.setItem('__auth_device_id', this.deviceId);
}
@@ -311,17 +291,11 @@ 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),
state: 'userinfo',
});
const data = res.data || {};
@@ -367,12 +341,11 @@ export default {
}
uni.setStorageSync('__used_auth_code', code);
// 立即清除 URL 中的 code/action 参数,避免刷新重复处理
// 立即清除 URL 中的 code/state 参数,避免刷新重复处理
const url = new URL(window.location.href);
url.searchParams.delete('code');
url.searchParams.delete('auth_code');
url.searchParams.delete('state');
url.searchParams.delete('action');
window.history.replaceState({}, '', url.toString());
try {
@@ -422,7 +395,6 @@ export default {
? `/pages/index/index?device_id=${deviceId}&action=mine`
: '/pages/index/index?action=mine';
localStorage.removeItem('__auth_device_id');
localStorage.removeItem('__auth_action');
uni.removeStorageSync('__auth_callback_done');
uni.redirectTo({ url });
},