Compare commits

..

2 Commits

Author SHA1 Message Date
kk 9ecd6d963b loading.vue 的回调处理统一从 localStorage 读取
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 41s
2026-07-11 12:12:53 +08:00
kk 925ebe9bb9 post请求用户授权信息
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 42s
2026-07-11 11:59:09 +08:00
3 changed files with 66 additions and 14 deletions
+7
View File
@@ -60,6 +60,13 @@
} catch (_) { } catch (_) {
// state 解析失败,忽略 // state 解析失败,忽略
} }
// 存入 localStorage,确保 loading 页面复用时也能获取(redirectTo 可能不触发 onLoad
if (action) {
localStorage.setItem('__auth_action', action);
}
if (callbackDeviceId) {
localStorage.setItem('__auth_device_id', callbackDeviceId);
}
let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code); let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code);
if (action) { if (action) {
loadingUrl += '&action=' + action; loadingUrl += '&action=' + action;
+41 -12
View File
@@ -36,16 +36,13 @@ export default {
code = urlParams.get('code') || ''; code = urlParams.get('code') || '';
} }
// 兜底:从 localStorage 恢复(redirect_uri 参数为主localStorage 为备用 // 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发localStorage 由跳转函数清理
if (!this.action) { if (!this.action) {
this.action = localStorage.getItem('__auth_action') || ''; this.action = localStorage.getItem('__auth_action') || '';
} }
if (!this.deviceId) { if (!this.deviceId) {
this.deviceId = localStorage.getItem('__auth_device_id') || ''; this.deviceId = localStorage.getItem('__auth_device_id') || '';
} }
// 清理 localStorage(一次性使用,避免残留影响下次流程)
localStorage.removeItem('__auth_action');
localStorage.removeItem('__auth_device_id');
console.log('[loading] onLoad - action:', this.action, 'deviceId:', this.deviceId, 'code:', code ? 'yes' : 'no'); console.log('[loading] onLoad - action:', this.action, 'deviceId:', this.deviceId, 'code:', code ? 'yes' : 'no');
if (this.action === 'userinfo') { if (this.action === 'userinfo') {
@@ -87,6 +84,11 @@ export default {
try { try {
this.statusText = '正在获取授权...'; this.statusText = '正在获取授权...';
// 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
if (this.deviceId) {
localStorage.setItem('__auth_device_id', this.deviceId);
}
// 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数 // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
const redirectUri = config.authRedirectUri || ( const redirectUri = config.authRedirectUri || (
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
@@ -105,8 +107,14 @@ export default {
let authUrl = data.data.auth_url; let authUrl = data.data.auth_url;
if (this.deviceId) { if (this.deviceId) {
const stateVal = encodeURIComponent(JSON.stringify({ device_id: 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; authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal;
} }
}
// 跳转微信授权页(静默,不弹窗) // 跳转微信授权页(静默,不弹窗)
window.location.href = authUrl; window.location.href = authUrl;
} else { } else {
@@ -215,6 +223,12 @@ export default {
try { try {
this.statusText = '正在获取用户信息...'; this.statusText = '正在获取用户信息...';
// 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
localStorage.setItem('__auth_action', 'userinfo');
if (this.deviceId) {
localStorage.setItem('__auth_device_id', this.deviceId);
}
// 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数 // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
const redirectUri = config.authRedirectUri || ( const redirectUri = config.authRedirectUri || (
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
@@ -233,8 +247,15 @@ export default {
if (this.deviceId) { if (this.deviceId) {
stateObj.device_id = this.deviceId; stateObj.device_id = this.deviceId;
} }
const stateVal = encodeURIComponent(JSON.stringify(stateObj));
let authUrl = data.data.auth_url; let authUrl = data.data.auth_url;
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + encodeURIComponent(JSON.stringify(stateObj)); try {
const urlObj = new URL(authUrl);
urlObj.searchParams.set('state', stateVal);
authUrl = urlObj.toString();
} catch (_) {
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal;
}
// 跳转微信授权页(弹窗确认) // 跳转微信授权页(弹窗确认)
window.location.href = authUrl; window.location.href = authUrl;
} else { } else {
@@ -269,14 +290,16 @@ export default {
url.searchParams.delete('action'); url.searchParams.delete('action');
window.history.replaceState({}, '', url.toString()); window.history.replaceState({}, '', url.toString());
// 用 code 调用 /api/v1/user/info 获取用户信息 // 用 code 调用 POST /api/v1/user/info 获取用户信息(必须用 POST,GET 是另一个端点)
const res = await gatewayGet('/api/v1/user/info', { console.log('[loading] 请求 POST user/info, app_no:', appNo, 'code:', code);
const res = await gatewayPost('/api/v1/user/info', {
app_no: appNo, app_no: appNo,
code: code, code: code,
}); });
const data = res.data || {}; const data = res.data || {};
console.log('[loading] user info response:', JSON.stringify(data, null, 2)); console.log('[loading] user/info response:', JSON.stringify(data, null, 2));
console.log('[loading] user/info statusCode:', res.statusCode);
if (data.code === 0 && data.data) { if (data.code === 0 && data.data) {
const userInfo = data.data; const userInfo = data.data;
@@ -302,16 +325,22 @@ export default {
* 跳回首页并标记为"我的"tab * 跳回首页并标记为"我的"tab
*/ */
goToHomeWithAction() { goToHomeWithAction() {
const url = this.deviceId // 兼容页面复用:优先 this.deviceId,其次 localStorage
? `/pages/index/index?device_id=${this.deviceId}&action=mine` const deviceId = this.deviceId || localStorage.getItem('__auth_device_id') || '';
const url = deviceId
? `/pages/index/index?device_id=${deviceId}&action=mine`
: '/pages/index/index?action=mine'; : '/pages/index/index?action=mine';
localStorage.removeItem('__auth_device_id');
localStorage.removeItem('__auth_action');
uni.redirectTo({ url }); uni.redirectTo({ url });
}, },
goToHome() { goToHome() {
const url = this.deviceId const deviceId = this.deviceId || localStorage.getItem('__auth_device_id') || '';
? `/pages/index/index?device_id=${this.deviceId}` const url = deviceId
? `/pages/index/index?device_id=${deviceId}`
: '/pages/index/index'; : '/pages/index/index';
localStorage.removeItem('__auth_device_id');
uni.redirectTo({ url }); uni.redirectTo({ url });
}, },
+17 -1
View File
@@ -325,6 +325,12 @@ export default {
} }
try { try {
// 存入 localStorageloading 页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
localStorage.setItem('__auth_action', 'userinfo');
if (this.deviceId) {
localStorage.setItem('__auth_device_id', this.deviceId);
}
// 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数 // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
const redirectUri = config.authRedirectUri || ( const redirectUri = config.authRedirectUri || (
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
@@ -344,8 +350,18 @@ export default {
if (this.deviceId) { if (this.deviceId) {
stateObj.device_id = this.deviceId; stateObj.device_id = this.deviceId;
} }
const stateVal = encodeURIComponent(JSON.stringify(stateObj));
// 解析 auth_url,安全地设置 state 参数(避免重复)
let authUrl = data.data.auth_url; let authUrl = data.data.auth_url;
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + encodeURIComponent(JSON.stringify(stateObj)); 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; window.location.href = authUrl;
} else { } else {