Compare commits

..

2 Commits

Author SHA1 Message Date
kk 258fa4a9f5 更新授权回调传参
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 44s
2026-07-11 11:37:53 +08:00
kk 45147426e0 将必要参数拼接在redirect_uri中
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 44s
2026-07-11 11:01:22 +08:00
3 changed files with 60 additions and 35 deletions
+17 -5
View File
@@ -30,7 +30,7 @@
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
deviceId = urlParams.get('device_id') || ''; deviceId = urlParams.get('device_id') || '';
// 兜底:从路径解析(如 /pages/index/index/N1154 // 兜底:从路径解析(如 /N1154)
if (!deviceId) { if (!deviceId) {
const path = window.location.pathname; const path = window.location.pathname;
const segments = path.split('/').filter(Boolean); const segments = path.split('/').filter(Boolean);
@@ -46,14 +46,26 @@
const code = urlParams.get('code'); const code = urlParams.get('code');
if (code) { if (code) {
// 有 code → 进入授权 Loading 页处理,保留 code/action/device_id 参数 // 有 code → 进入授权 Loading 页处理
const action = urlParams.get('action') || ''; // 从 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 解析失败,忽略
}
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;
} }
if (deviceId) { if (callbackDeviceId) {
loadingUrl += '&device_id=' + deviceId; loadingUrl += '&device_id=' + callbackDeviceId;
} }
uni.redirectTo({ url: loadingUrl }); uni.redirectTo({ url: loadingUrl });
return; return;
+31 -21
View File
@@ -29,22 +29,25 @@ export default {
return; return;
} }
// 优先从页面参数获取 code(App.vue 跳转时带过来) // 从 URL 参数获取 code(App.vue 跳转时带过来)
// 兜底从 URL search 参数获取(微信直接回调时)
let code = options.code || ''; let code = options.code || '';
if (!code) { if (!code) {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
code = urlParams.get('code') || ''; code = urlParams.get('code') || '';
// 同步获取 action(可能在 URL 参数中)
if (!this.action) {
this.action = urlParams.get('action') || '';
}
// 同步获取 device_id
if (!this.deviceId) {
this.deviceId = urlParams.get('device_id') || '';
}
} }
// 兜底:从 localStorage 恢复(redirect_uri 参数为主,localStorage 为备用)
if (!this.action) {
this.action = localStorage.getItem('__auth_action') || '';
}
if (!this.deviceId) {
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');
if (this.action === 'userinfo') { if (this.action === 'userinfo') {
// 获取用户信息流程 // 获取用户信息流程
if (code) { if (code) {
@@ -84,7 +87,7 @@ export default {
try { try {
this.statusText = '正在获取授权...'; this.statusText = '正在获取授权...';
// 回调地址必须是 HTTPS,使用配置文件中的地址 // 回调地址必须与微信公众号后台注册地址一致,不能带自定义参数
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
); );
@@ -98,8 +101,14 @@ export default {
const data = res.data || {}; const data = res.data || {};
if (data.code === 0 && data.data && data.data.auth_url) { if (data.code === 0 && data.data && data.data.auth_url) {
// 通过 state 传递 device_idOAuth 标准保证 state 原样回传)
let authUrl = data.data.auth_url;
if (this.deviceId) {
const stateVal = encodeURIComponent(JSON.stringify({ device_id: this.deviceId }));
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal;
}
// 跳转微信授权页(静默,不弹窗) // 跳转微信授权页(静默,不弹窗)
window.location.href = data.data.auth_url; window.location.href = authUrl;
} else { } else {
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' }); uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
setTimeout(() => this.goToScan(), 1500); setTimeout(() => this.goToScan(), 1500);
@@ -206,16 +215,10 @@ export default {
try { try {
this.statusText = '正在获取用户信息...'; this.statusText = '正在获取用户信息...';
// 回调地址:当前 loading 页,带 action=userinfo 和 device_id // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
const baseUri = config.authRedirectUri || ( const redirectUri = config.authRedirectUri || (
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
); );
const redirectUriObj = new URL(baseUri);
redirectUriObj.searchParams.set('action', 'userinfo');
if (this.deviceId) {
redirectUriObj.searchParams.set('device_id', this.deviceId);
}
const redirectUri = redirectUriObj.toString();
const res = await gatewayGet('/api/v1/user/auth/url', { const res = await gatewayGet('/api/v1/user/auth/url', {
app_no: appNo, app_no: appNo,
@@ -225,8 +228,15 @@ export default {
const data = res.data || {}; const data = res.data || {};
if (data.code === 0 && data.data && data.data.auth_url) { if (data.code === 0 && data.data && data.data.auth_url) {
// 通过 state 传递 action 和 device_idOAuth 标准保证 state 原样回传)
const stateObj = { action: 'userinfo' };
if (this.deviceId) {
stateObj.device_id = this.deviceId;
}
let authUrl = data.data.auth_url;
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + encodeURIComponent(JSON.stringify(stateObj));
// 跳转微信授权页(弹窗确认) // 跳转微信授权页(弹窗确认)
window.location.href = data.data.auth_url; window.location.href = authUrl;
} else { } else {
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' }); uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
setTimeout(() => this.goToHomeWithAction(), 1500); setTimeout(() => this.goToHomeWithAction(), 1500);
+12 -9
View File
@@ -325,14 +325,10 @@ export default {
} }
try { try {
// 回调地址:loading 页处理后跳回首页(带 action 和 device_id // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
const baseUri = config.authRedirectUri || window.location.origin + '/pages/auth/loading'; const redirectUri = config.authRedirectUri || (
const redirectUriObj = new URL(baseUri); window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
redirectUriObj.searchParams.set('action', 'userinfo'); );
if (this.deviceId) {
redirectUriObj.searchParams.set('device_id', this.deviceId);
}
const redirectUri = redirectUriObj.toString();
// 获取非静默授权链接(会弹窗确认) // 获取非静默授权链接(会弹窗确认)
const res = await gatewayGet('/api/v1/user/auth/url', { const res = await gatewayGet('/api/v1/user/auth/url', {
@@ -343,8 +339,15 @@ export default {
const data = res.data || {}; const data = res.data || {};
if (data.code === 0 && data.data && data.data.auth_url) { if (data.code === 0 && data.data && data.data.auth_url) {
// 通过 state 传递 action 和 device_idOAuth 标准保证 state 原样回传)
const stateObj = { action: 'userinfo' };
if (this.deviceId) {
stateObj.device_id = this.deviceId;
}
let authUrl = data.data.auth_url;
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + encodeURIComponent(JSON.stringify(stateObj));
// 跳转微信授权页(弹窗确认) // 跳转微信授权页(弹窗确认)
window.location.href = data.data.auth_url; window.location.href = authUrl;
} else { } else {
console.warn('[index] 获取授权链接失败:', data.message); console.warn('[index] 获取授权链接失败:', data.message);
} }