Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a071f760d3 | |||
| 445d428d8b | |||
| bbbd386e1e | |||
| fd3cd58123 | |||
| 97d4be5772 | |||
| b2b92ec6b4 | |||
| b336b901d3 | |||
| 9439bcf9d7 | |||
| d862cad1a0 | |||
| 85d5193071 | |||
| 9551d9d8b2 |
+2
-2
@@ -42,8 +42,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 检查 URL 中是否有授权回调的 code
|
// 3. 检查 URL 中是否有授权回调的 code(支付宝用 auth_code,微信用 code)
|
||||||
const code = urlParams.get('code');
|
const code = urlParams.get('code') || urlParams.get('auth_code');
|
||||||
|
|
||||||
if (code) {
|
if (code) {
|
||||||
// 有 code → 进入授权 Loading 页处理
|
// 有 code → 进入授权 Loading 页处理
|
||||||
|
|||||||
+98
-38
@@ -16,6 +16,7 @@ export default {
|
|||||||
statusText: '正在登录...',
|
statusText: '正在登录...',
|
||||||
deviceId: '',
|
deviceId: '',
|
||||||
action: '',
|
action: '',
|
||||||
|
navigating: false, // 防止重复跳转
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
onLoad(options) {
|
onLoad(options) {
|
||||||
@@ -30,10 +31,29 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 从 URL 参数获取 code(App.vue 跳转时带过来)
|
// 从 URL 参数获取 code(App.vue 跳转时带过来)
|
||||||
let code = options.code || '';
|
// 支付宝回调参数名是 auth_code,微信是 code
|
||||||
|
let code = options.code || options.auth_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') || 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 兜底
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发,localStorage 由跳转函数清理)
|
// 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发,localStorage 由跳转函数清理)
|
||||||
@@ -72,7 +92,19 @@ export default {
|
|||||||
* 发起 OAuth 授权(静默授权,只获取 openid)
|
* 发起 OAuth 授权(静默授权,只获取 openid)
|
||||||
*/
|
*/
|
||||||
async startAuth() {
|
async startAuth() {
|
||||||
// 清除上次使用的 code 标记
|
// 防止重复调用(实例级 + storage 级双重保护)
|
||||||
|
if (this.navigating) {
|
||||||
|
console.log('[Auth] 已在跳转中,忽略重复 startAuth');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// handleCallback 正在处理中或已完成,禁止发起新授权
|
||||||
|
if (uni.getStorageSync('__auth_callback_done')) {
|
||||||
|
console.log('[Auth] 回调处理中/已完成,跳过 startAuth');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.navigating = true;
|
||||||
|
|
||||||
|
// 清除上次使用的 code 标记(仅在发起新授权时清除,不在 handleCallback 流程中)
|
||||||
uni.removeStorageSync('__used_auth_code');
|
uni.removeStorageSync('__used_auth_code');
|
||||||
|
|
||||||
const platform = detectPlatform();
|
const platform = detectPlatform();
|
||||||
@@ -97,29 +129,26 @@ export default {
|
|||||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||||
);
|
);
|
||||||
|
|
||||||
// 使用 snsapi_base 静默授权,不弹窗
|
// 静默授权:微信用 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', {
|
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||||
app_no: appNo,
|
app_no: appNo,
|
||||||
scopes: 'snsapi_base',
|
scopes: scopes,
|
||||||
redirect_uri: redirectUri,
|
redirect_uri: redirectUri,
|
||||||
|
state: Object.keys(stateObj).length > 0 ? JSON.stringify(stateObj) : '',
|
||||||
});
|
});
|
||||||
|
|
||||||
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_id(OAuth 标准保证 state 原样回传)
|
// 后端已将 state 写入授权 URL,直接跳转
|
||||||
let authUrl = data.data.auth_url;
|
window.location.href = 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 = 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);
|
||||||
@@ -135,6 +164,15 @@ export default {
|
|||||||
* 处理授权回调(用 code 换 openid,再用 openid 登录)
|
* 处理授权回调(用 code 换 openid,再用 openid 登录)
|
||||||
*/
|
*/
|
||||||
async handleCallback(code) {
|
async handleCallback(code) {
|
||||||
|
// 防止重复调用(实例级 + storage 级双重保护)
|
||||||
|
if (this.navigating) {
|
||||||
|
console.log('[Auth] 已在跳转中,忽略重复 handleCallback');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.navigating = true;
|
||||||
|
// storage 级保护:跨页面实例生效,防止支付宝整页重载后重复执行
|
||||||
|
uni.setStorageSync('__auth_callback_done', '1');
|
||||||
|
|
||||||
const appNo = uni.getStorageSync('app_no') || getAppNo(detectPlatform());
|
const appNo = uni.getStorageSync('app_no') || getAppNo(detectPlatform());
|
||||||
|
|
||||||
if (!appNo) {
|
if (!appNo) {
|
||||||
@@ -147,6 +185,7 @@ export default {
|
|||||||
const usedCode = uni.getStorageSync('__used_auth_code');
|
const usedCode = uni.getStorageSync('__used_auth_code');
|
||||||
if (usedCode === code) {
|
if (usedCode === code) {
|
||||||
console.warn('[loading] code 已使用过,跳过');
|
console.warn('[loading] code 已使用过,跳过');
|
||||||
|
this.navigating = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uni.setStorageSync('__used_auth_code', code);
|
uni.setStorageSync('__used_auth_code', code);
|
||||||
@@ -154,6 +193,7 @@ export default {
|
|||||||
// 立即清除 URL 中的 code 参数,避免刷新重复处理
|
// 立即清除 URL 中的 code 参数,避免刷新重复处理
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
url.searchParams.delete('code');
|
url.searchParams.delete('code');
|
||||||
|
url.searchParams.delete('auth_code');
|
||||||
url.searchParams.delete('state');
|
url.searchParams.delete('state');
|
||||||
window.history.replaceState({}, '', url.toString());
|
window.history.replaceState({}, '', url.toString());
|
||||||
|
|
||||||
@@ -230,6 +270,17 @@ export default {
|
|||||||
* 发起非静默授权获取用户信息(snsapi_userinfo,弹窗确认)
|
* 发起非静默授权获取用户信息(snsapi_userinfo,弹窗确认)
|
||||||
*/
|
*/
|
||||||
async startUserInfoAuth() {
|
async startUserInfoAuth() {
|
||||||
|
// 防止重复调用(实例级 + storage 级双重保护)
|
||||||
|
if (this.navigating) {
|
||||||
|
console.log('[Auth] 已在跳转中,忽略重复 startUserInfoAuth');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (uni.getStorageSync('__auth_callback_done')) {
|
||||||
|
console.log('[Auth] 回调处理中/已完成,跳过 startUserInfoAuth');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.navigating = true;
|
||||||
|
|
||||||
// 清除上次使用的 code 标记
|
// 清除上次使用的 code 标记
|
||||||
uni.removeStorageSync('__used_auth_code');
|
uni.removeStorageSync('__used_auth_code');
|
||||||
|
|
||||||
@@ -256,30 +307,26 @@ export default {
|
|||||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||||
);
|
);
|
||||||
|
|
||||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
// 用户信息授权:微信用 snsapi_userinfo,支付宝用 auth_user
|
||||||
app_no: appNo,
|
const scopes = platform === 'alipay' ? 'auth_user' : 'snsapi_userinfo';
|
||||||
scopes: 'snsapi_userinfo',
|
|
||||||
redirect_uri: redirectUri,
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = res.data || {};
|
// 构建 state(包含 action 和 device_id,回调时原样回传)
|
||||||
if (data.code === 0 && data.data && data.data.auth_url) {
|
|
||||||
// 通过 state 传递 action 和 device_id(OAuth 标准保证 state 原样回传)
|
|
||||||
const stateObj = { action: 'userinfo' };
|
const stateObj = { action: 'userinfo' };
|
||||||
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;
|
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||||
try {
|
app_no: appNo,
|
||||||
const urlObj = new URL(authUrl);
|
scopes: scopes,
|
||||||
urlObj.searchParams.set('state', stateVal);
|
redirect_uri: redirectUri,
|
||||||
authUrl = urlObj.toString();
|
state: JSON.stringify(stateObj),
|
||||||
} catch (_) {
|
});
|
||||||
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal;
|
|
||||||
}
|
const data = res.data || {};
|
||||||
// 跳转微信授权页(弹窗确认)
|
if (data.code === 0 && data.data && data.data.auth_url) {
|
||||||
window.location.href = authUrl;
|
// 后端已将 state 写入授权 URL,直接跳转
|
||||||
|
window.location.href = data.data.auth_url;
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
|
uni.showToast({ title: data.message || '获取授权失败', icon: 'none' });
|
||||||
setTimeout(() => this.goToHomeWithAction(), 1500);
|
setTimeout(() => this.goToHomeWithAction(), 1500);
|
||||||
@@ -295,6 +342,14 @@ export default {
|
|||||||
* 处理用户信息授权回调(用 code 调 /api/v1/user/info 获取头像昵称)
|
* 处理用户信息授权回调(用 code 调 /api/v1/user/info 获取头像昵称)
|
||||||
*/
|
*/
|
||||||
async handleUserInfoCallback(code) {
|
async handleUserInfoCallback(code) {
|
||||||
|
// 防止重复调用(实例级 + storage 级双重保护)
|
||||||
|
if (this.navigating) {
|
||||||
|
console.log('[Auth] 已在跳转中,忽略重复 handleUserInfoCallback');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.navigating = true;
|
||||||
|
uni.setStorageSync('__auth_callback_done', '1');
|
||||||
|
|
||||||
const appNo = uni.getStorageSync('app_no') || getAppNo(detectPlatform());
|
const appNo = uni.getStorageSync('app_no') || getAppNo(detectPlatform());
|
||||||
|
|
||||||
if (!appNo) {
|
if (!appNo) {
|
||||||
@@ -306,6 +361,7 @@ export default {
|
|||||||
const usedCode = uni.getStorageSync('__used_auth_code');
|
const usedCode = uni.getStorageSync('__used_auth_code');
|
||||||
if (usedCode === code) {
|
if (usedCode === code) {
|
||||||
console.warn('[loading] code 已使用过,跳过');
|
console.warn('[loading] code 已使用过,跳过');
|
||||||
|
this.navigating = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uni.setStorageSync('__used_auth_code', code);
|
uni.setStorageSync('__used_auth_code', code);
|
||||||
@@ -313,6 +369,7 @@ export default {
|
|||||||
// 立即清除 URL 中的 code/action 参数,避免刷新重复处理
|
// 立即清除 URL 中的 code/action 参数,避免刷新重复处理
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
url.searchParams.delete('code');
|
url.searchParams.delete('code');
|
||||||
|
url.searchParams.delete('auth_code');
|
||||||
url.searchParams.delete('state');
|
url.searchParams.delete('state');
|
||||||
url.searchParams.delete('action');
|
url.searchParams.delete('action');
|
||||||
window.history.replaceState({}, '', url.toString());
|
window.history.replaceState({}, '', url.toString());
|
||||||
@@ -365,6 +422,7 @@ export default {
|
|||||||
: '/pages/index/index?action=mine';
|
: '/pages/index/index?action=mine';
|
||||||
localStorage.removeItem('__auth_device_id');
|
localStorage.removeItem('__auth_device_id');
|
||||||
localStorage.removeItem('__auth_action');
|
localStorage.removeItem('__auth_action');
|
||||||
|
uni.removeStorageSync('__auth_callback_done');
|
||||||
uni.redirectTo({ url });
|
uni.redirectTo({ url });
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -374,6 +432,7 @@ export default {
|
|||||||
? `/pages/index/index?device_id=${deviceId}`
|
? `/pages/index/index?device_id=${deviceId}`
|
||||||
: '/pages/index/index';
|
: '/pages/index/index';
|
||||||
localStorage.removeItem('__auth_device_id');
|
localStorage.removeItem('__auth_device_id');
|
||||||
|
uni.removeStorageSync('__auth_callback_done');
|
||||||
uni.redirectTo({ url });
|
uni.redirectTo({ url });
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -389,6 +448,7 @@ export default {
|
|||||||
const url = this.deviceId
|
const url = this.deviceId
|
||||||
? `/pages/register/register?device_id=${this.deviceId}`
|
? `/pages/register/register?device_id=${this.deviceId}`
|
||||||
: '/pages/register/register';
|
: '/pages/register/register';
|
||||||
|
uni.removeStorageSync('__auth_callback_done');
|
||||||
uni.redirectTo({ url });
|
uni.redirectTo({ url });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
+18
-24
@@ -344,34 +344,28 @@ export default {
|
|||||||
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
window.location.origin.replace(/^http:/, 'https:') + window.location.pathname
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取非静默授权链接(会弹窗确认)
|
// 构建 state(包含 action 和 device_id,回调时原样回传)
|
||||||
const res = await gatewayGet('/api/v1/user/auth/url', {
|
|
||||||
app_no: appNo,
|
|
||||||
scopes: 'snsapi_userinfo',
|
|
||||||
redirect_uri: redirectUri,
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = res.data || {};
|
|
||||||
if (data.code === 0 && data.data && data.data.auth_url) {
|
|
||||||
// 通过 state 传递 action 和 device_id(OAuth 标准保证 state 原样回传)
|
|
||||||
const stateObj = { action: 'userinfo' };
|
const stateObj = { action: 'userinfo' };
|
||||||
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 参数(避免重复)
|
// 用户信息授权:微信用 snsapi_userinfo,支付宝用 auth_user
|
||||||
let authUrl = data.data.auth_url;
|
const platform = uni.getStorageSync('platform') || 'wechat';
|
||||||
try {
|
const scopes = platform === 'alipay' ? 'auth_user' : 'snsapi_userinfo';
|
||||||
const urlObj = new URL(authUrl);
|
|
||||||
urlObj.searchParams.set('state', stateVal);
|
const res = await gatewayGet('/api/v1/user/auth/url', {
|
||||||
authUrl = urlObj.toString();
|
app_no: appNo,
|
||||||
} catch (_) {
|
scopes: scopes,
|
||||||
// URL 解析失败时直接拼接
|
redirect_uri: redirectUri,
|
||||||
authUrl += (authUrl.includes('?') ? '&' : '?') + 'state=' + stateVal;
|
state: JSON.stringify(stateObj),
|
||||||
}
|
});
|
||||||
console.log('[index] 跳转授权页:', authUrl);
|
|
||||||
// 跳转微信授权页(弹窗确认)
|
const data = res.data || {};
|
||||||
window.location.href = authUrl;
|
if (data.code === 0 && data.data && data.data.auth_url) {
|
||||||
|
console.log('[index] 跳转授权页:', data.data.auth_url);
|
||||||
|
// 后端已将 state 写入授权 URL,直接跳转
|
||||||
|
window.location.href = data.data.auth_url;
|
||||||
} else {
|
} else {
|
||||||
console.warn('[index] 获取授权链接失败:', data.message);
|
console.warn('[index] 获取授权链接失败:', data.message);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -6,9 +6,9 @@
|
|||||||
// app_no 编码表:platform + terminalType → app_no
|
// app_no 编码表:platform + terminalType → app_no
|
||||||
const APP_NO_MAP = {
|
const APP_NO_MAP = {
|
||||||
'wechat_h5': '60000001102001',
|
'wechat_h5': '60000001102001',
|
||||||
'wechat_mp': '600102101',
|
'wechat_mp': '60000001102101',
|
||||||
'alipay_h5': '600112001',
|
'alipay_h5': '60000001112001',
|
||||||
'alipay_mp': '600112101',
|
'alipay_mp': '60000001112101',
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user