Compare commits

...

4 Commits

Author SHA1 Message Date
kk d326912bd3 redirectTo改为reLaunch
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 46s
2026-07-22 10:03:13 +08:00
kk be976f939f 简化state参数
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 46s
2026-07-20 17:55:22 +08:00
kk e1f71fb220 简化state参数
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 1m7s
2026-07-20 17:45:44 +08:00
kk 558e1380db 正在获取用户信息
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 47s
2026-07-20 17:00:41 +08:00
3 changed files with 17 additions and 70 deletions
+7 -26
View File
@@ -47,34 +47,15 @@
if (code) {
// 有 code → 进入授权 Loading 页处理
// state 中解析 device_id 和 actionOAuth 标准保证 state 原样回传)
let action = '';
let callbackDeviceId = deviceId || '';
try {
const stateStr = urlParams.get('state');
if (stateStr) {
const stateObj = JSON.parse(decodeURIComponent(stateStr));
action = stateObj.action || action;
callbackDeviceId = stateObj.device_id || callbackDeviceId;
}
} catch (_) {
// state 解析失败,忽略
}
// 存入 localStorage,确保 loading 页面复用时也能获取(redirectTo 可能不触发 onLoad
if (action) {
localStorage.setItem('__auth_action', action);
}
if (callbackDeviceId) {
localStorage.setItem('__auth_device_id', callbackDeviceId);
}
// state 为 "base" 或 "userinfo",直接透传
const state = urlParams.get('state') || '';
let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code);
if (action) {
loadingUrl += '&action=' + action;
if (state) {
loadingUrl += '&state=' + state;
}
if (callbackDeviceId) {
loadingUrl += '&device_id=' + callbackDeviceId;
}
uni.redirectTo({ url: loadingUrl });
// 必须用 reLaunch:支付宝回调时当前页可能就是 loading 页,
// redirectTo 到同一页面不会重新触发 onLoad,导致请求卡住
uni.reLaunch({ url: loadingUrl });
return;
}
+9 -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') || '';
}
@@ -67,6 +54,7 @@ export default {
if (this.action === 'userinfo') {
// 获取用户信息流程
this.statusText = '正在获取用户信息...';
if (code) {
this.handleUserInfoCallback(code);
} else {
@@ -132,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 || {};
@@ -297,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);
}
@@ -310,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 || {};
@@ -366,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 {
@@ -421,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 });
},
+1 -8
View File
@@ -334,7 +334,6 @@ export default {
try {
// 存入 localStorageloading 页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
localStorage.setItem('__auth_action', 'userinfo');
if (this.deviceId) {
localStorage.setItem('__auth_device_id', this.deviceId);
}
@@ -344,12 +343,6 @@ 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';
@@ -358,7 +351,7 @@ export default {
app_no: appNo,
scopes: scopes,
redirect_uri: redirectUri,
state: JSON.stringify(stateObj),
state: 'userinfo',
});
const data = res.data || {};