|
|
|
@@ -16,6 +16,7 @@ export default {
|
|
|
|
|
statusText: '正在登录...',
|
|
|
|
|
deviceId: '',
|
|
|
|
|
action: '',
|
|
|
|
|
navigating: false, // 防止重复跳转
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
onLoad(options) {
|
|
|
|
@@ -66,6 +67,7 @@ export default {
|
|
|
|
|
|
|
|
|
|
if (this.action === 'userinfo') {
|
|
|
|
|
// 获取用户信息流程
|
|
|
|
|
this.statusText = '正在获取用户信息...';
|
|
|
|
|
if (code) {
|
|
|
|
|
this.handleUserInfoCallback(code);
|
|
|
|
|
} else {
|
|
|
|
@@ -91,7 +93,19 @@ export default {
|
|
|
|
|
* 发起 OAuth 授权(静默授权,只获取 openid)
|
|
|
|
|
*/
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
const platform = detectPlatform();
|
|
|
|
@@ -151,6 +165,15 @@ export default {
|
|
|
|
|
* 处理授权回调(用 code 换 openid,再用 openid 登录)
|
|
|
|
|
*/
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
if (!appNo) {
|
|
|
|
@@ -163,6 +186,7 @@ export default {
|
|
|
|
|
const usedCode = uni.getStorageSync('__used_auth_code');
|
|
|
|
|
if (usedCode === code) {
|
|
|
|
|
console.warn('[loading] code 已使用过,跳过');
|
|
|
|
|
this.navigating = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
uni.setStorageSync('__used_auth_code', code);
|
|
|
|
@@ -247,6 +271,17 @@ export default {
|
|
|
|
|
* 发起非静默授权获取用户信息(snsapi_userinfo,弹窗确认)
|
|
|
|
|
*/
|
|
|
|
|
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 标记
|
|
|
|
|
uni.removeStorageSync('__used_auth_code');
|
|
|
|
|
|
|
|
|
@@ -308,6 +343,14 @@ export default {
|
|
|
|
|
* 处理用户信息授权回调(用 code 调 /api/v1/user/info 获取头像昵称)
|
|
|
|
|
*/
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
if (!appNo) {
|
|
|
|
@@ -319,6 +362,7 @@ export default {
|
|
|
|
|
const usedCode = uni.getStorageSync('__used_auth_code');
|
|
|
|
|
if (usedCode === code) {
|
|
|
|
|
console.warn('[loading] code 已使用过,跳过');
|
|
|
|
|
this.navigating = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
uni.setStorageSync('__used_auth_code', code);
|
|
|
|
@@ -379,6 +423,7 @@ export default {
|
|
|
|
|
: '/pages/index/index?action=mine';
|
|
|
|
|
localStorage.removeItem('__auth_device_id');
|
|
|
|
|
localStorage.removeItem('__auth_action');
|
|
|
|
|
uni.removeStorageSync('__auth_callback_done');
|
|
|
|
|
uni.redirectTo({ url });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@@ -388,6 +433,7 @@ export default {
|
|
|
|
|
? `/pages/index/index?device_id=${deviceId}`
|
|
|
|
|
: '/pages/index/index';
|
|
|
|
|
localStorage.removeItem('__auth_device_id');
|
|
|
|
|
uni.removeStorageSync('__auth_callback_done');
|
|
|
|
|
uni.redirectTo({ url });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@@ -403,6 +449,7 @@ export default {
|
|
|
|
|
const url = this.deviceId
|
|
|
|
|
? `/pages/register/register?device_id=${this.deviceId}`
|
|
|
|
|
: '/pages/register/register';
|
|
|
|
|
uni.removeStorageSync('__auth_callback_done');
|
|
|
|
|
uni.redirectTo({ url });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|