Compare commits

...

1 Commits

Author SHA1 Message Date
kk 925ebe9bb9 post请求用户授权信息
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 42s
2026-07-11 11:59:09 +08:00
2 changed files with 31 additions and 6 deletions
+20 -5
View File
@@ -105,7 +105,13 @@ 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 }));
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal; 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;
@@ -233,8 +239,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 +282,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;
+11 -1
View File
@@ -344,8 +344,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 {