Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9ecd6d963b | |||
| 925ebe9bb9 | |||
| 258fa4a9f5 |
+20
-3
@@ -47,9 +47,26 @@
|
||||
|
||||
if (code) {
|
||||
// 有 code → 进入授权 Loading 页处理
|
||||
// redirect_uri 已携带 device_id 和 action,回调时直接从 URL 取
|
||||
const action = urlParams.get('action') || '';
|
||||
const callbackDeviceId = urlParams.get('device_id') || deviceId || '';
|
||||
// 从 state 中解析 device_id 和 action(OAuth 标准保证 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);
|
||||
}
|
||||
let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code);
|
||||
if (action) {
|
||||
loadingUrl += '&action=' + action;
|
||||
|
||||
+59
-24
@@ -36,16 +36,13 @@ export default {
|
||||
code = urlParams.get('code') || '';
|
||||
}
|
||||
|
||||
// 兜底:从 localStorage 恢复(redirect_uri 参数为主,localStorage 为备用)
|
||||
// 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发,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') {
|
||||
@@ -87,13 +84,15 @@ export default {
|
||||
try {
|
||||
this.statusText = '正在获取授权...';
|
||||
|
||||
// 回调地址:基础地址 + device_id 参数(回调时保留)
|
||||
let redirectUri = config.authRedirectUri || (
|
||||
// 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
|
||||
if (this.deviceId) {
|
||||
localStorage.setItem('__auth_device_id', this.deviceId);
|
||||
}
|
||||
|
||||
// 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
|
||||
const redirectUri = config.authRedirectUri || (
|
||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||
);
|
||||
if (this.deviceId) {
|
||||
redirectUri += (redirectUri.includes('?') ? '&' : '?') + 'device_id=' + encodeURIComponent(this.deviceId);
|
||||
}
|
||||
|
||||
// 使用 snsapi_base 静默授权,不弹窗
|
||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||
@@ -104,8 +103,20 @@ export default {
|
||||
|
||||
const data = res.data || {};
|
||||
if (data.code === 0 && data.data && data.data.auth_url) {
|
||||
// 通过 state 传递 device_id(OAuth 标准保证 state 原样回传)
|
||||
let authUrl = data.data.auth_url;
|
||||
if (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;
|
||||
}
|
||||
}
|
||||
// 跳转微信授权页(静默,不弹窗)
|
||||
window.location.href = data.data.auth_url;
|
||||
window.location.href = authUrl;
|
||||
} else {
|
||||
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
|
||||
setTimeout(() => this.goToScan(), 1500);
|
||||
@@ -212,14 +223,16 @@ export default {
|
||||
try {
|
||||
this.statusText = '正在获取用户信息...';
|
||||
|
||||
// 回调地址:基础地址 + action + device_id 参数(回调时保留)
|
||||
let redirectUri = config.authRedirectUri || (
|
||||
// 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
|
||||
localStorage.setItem('__auth_action', 'userinfo');
|
||||
if (this.deviceId) {
|
||||
localStorage.setItem('__auth_device_id', this.deviceId);
|
||||
}
|
||||
|
||||
// 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
|
||||
const redirectUri = config.authRedirectUri || (
|
||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||
);
|
||||
redirectUri += (redirectUri.includes('?') ? '&' : '?') + 'action=userinfo';
|
||||
if (this.deviceId) {
|
||||
redirectUri += '&device_id=' + encodeURIComponent(this.deviceId);
|
||||
}
|
||||
|
||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||
app_no: appNo,
|
||||
@@ -229,8 +242,22 @@ export default {
|
||||
|
||||
const data = res.data || {};
|
||||
if (data.code === 0 && data.data && data.data.auth_url) {
|
||||
// 通过 state 传递 action 和 device_id(OAuth 标准保证 state 原样回传)
|
||||
const stateObj = { action: 'userinfo' };
|
||||
if (this.deviceId) {
|
||||
stateObj.device_id = this.deviceId;
|
||||
}
|
||||
const stateVal = encodeURIComponent(JSON.stringify(stateObj));
|
||||
let authUrl = data.data.auth_url;
|
||||
try {
|
||||
const urlObj = new URL(authUrl);
|
||||
urlObj.searchParams.set('state', stateVal);
|
||||
authUrl = urlObj.toString();
|
||||
} catch (_) {
|
||||
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal;
|
||||
}
|
||||
// 跳转微信授权页(弹窗确认)
|
||||
window.location.href = data.data.auth_url;
|
||||
window.location.href = authUrl;
|
||||
} else {
|
||||
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
|
||||
setTimeout(() => this.goToHomeWithAction(), 1500);
|
||||
@@ -263,14 +290,16 @@ export default {
|
||||
url.searchParams.delete('action');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
|
||||
// 用 code 调用 /api/v1/user/info 获取用户信息
|
||||
const res = await gatewayGet('/api/v1/user/info', {
|
||||
// 用 code 调用 POST /api/v1/user/info 获取用户信息(必须用 POST,GET 是另一个端点)
|
||||
console.log('[loading] 请求 POST user/info, app_no:', appNo, 'code:', code);
|
||||
const res = await gatewayPost('/api/v1/user/info', {
|
||||
app_no: appNo,
|
||||
code: code,
|
||||
});
|
||||
|
||||
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) {
|
||||
const userInfo = data.data;
|
||||
@@ -296,16 +325,22 @@ export default {
|
||||
* 跳回首页并标记为"我的"tab
|
||||
*/
|
||||
goToHomeWithAction() {
|
||||
const url = this.deviceId
|
||||
? `/pages/index/index?device_id=${this.deviceId}&action=mine`
|
||||
// 兼容页面复用:优先 this.deviceId,其次 localStorage
|
||||
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';
|
||||
localStorage.removeItem('__auth_device_id');
|
||||
localStorage.removeItem('__auth_action');
|
||||
uni.redirectTo({ url });
|
||||
},
|
||||
|
||||
goToHome() {
|
||||
const url = this.deviceId
|
||||
? `/pages/index/index?device_id=${this.deviceId}`
|
||||
const deviceId = this.deviceId || localStorage.getItem('__auth_device_id') || '';
|
||||
const url = deviceId
|
||||
? `/pages/index/index?device_id=${deviceId}`
|
||||
: '/pages/index/index';
|
||||
localStorage.removeItem('__auth_device_id');
|
||||
uni.redirectTo({ url });
|
||||
},
|
||||
|
||||
|
||||
@@ -325,14 +325,16 @@ export default {
|
||||
}
|
||||
|
||||
try {
|
||||
// 回调地址:基础地址 + action + device_id 参数(回调时保留)
|
||||
let redirectUri = config.authRedirectUri || (
|
||||
// 存入 localStorage(loading 页面复用时 onLoad 不会重新触发,需要从 localStorage 读取)
|
||||
localStorage.setItem('__auth_action', 'userinfo');
|
||||
if (this.deviceId) {
|
||||
localStorage.setItem('__auth_device_id', this.deviceId);
|
||||
}
|
||||
|
||||
// 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数
|
||||
const redirectUri = config.authRedirectUri || (
|
||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||
);
|
||||
redirectUri += (redirectUri.includes('?') ? '&' : '?') + 'action=userinfo';
|
||||
if (this.deviceId) {
|
||||
redirectUri += '&device_id=' + encodeURIComponent(this.deviceId);
|
||||
}
|
||||
|
||||
// 获取非静默授权链接(会弹窗确认)
|
||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||
@@ -343,8 +345,25 @@ export default {
|
||||
|
||||
const data = res.data || {};
|
||||
if (data.code === 0 && data.data && data.data.auth_url) {
|
||||
// 通过 state 传递 action 和 device_id(OAuth 标准保证 state 原样回传)
|
||||
const stateObj = { action: 'userinfo' };
|
||||
if (this.deviceId) {
|
||||
stateObj.device_id = this.deviceId;
|
||||
}
|
||||
const stateVal = encodeURIComponent(JSON.stringify(stateObj));
|
||||
// 解析 auth_url,安全地设置 state 参数(避免重复)
|
||||
let authUrl = data.data.auth_url;
|
||||
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 = data.data.auth_url;
|
||||
window.location.href = authUrl;
|
||||
} else {
|
||||
console.warn('[index] 获取授权链接失败:', data.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user