This commit is contained in:
@@ -305,8 +305,9 @@
|
||||
};
|
||||
|
||||
const loadAssetInfo = async () => {
|
||||
const identifier = userStore.state.identifier;
|
||||
const identifier = String(userStore.state.identifier || uni.getStorageSync('identifier') || '').trim();
|
||||
if (!identifier) return;
|
||||
if (userStore.state.identifier !== identifier) userStore.setIdentifier(identifier);
|
||||
const context = getSessionContext();
|
||||
|
||||
loading.value = true;
|
||||
@@ -401,11 +402,18 @@
|
||||
};
|
||||
|
||||
const handleIndexEntry = async () => {
|
||||
if (userStore.state.loginPending) return;
|
||||
const token = uni.getStorageSync('token');
|
||||
if (!token) {
|
||||
const identifier = String(userStore.state.identifier || uni.getStorageSync('identifier') || '').trim();
|
||||
if (!token || !identifier) {
|
||||
if (token && !identifier) {
|
||||
userStore.clearUser();
|
||||
uni.showToast({ title: '缺少资产标识,请重新输入或扫码登录', icon: 'none' });
|
||||
}
|
||||
uni.reLaunch({ url: '/pages/login/login' });
|
||||
return;
|
||||
}
|
||||
if (userStore.state.identifier !== identifier) userStore.setIdentifier(identifier);
|
||||
|
||||
if (indexEntryChecking.value) return;
|
||||
indexEntryChecking.value = true;
|
||||
@@ -1001,7 +1009,11 @@
|
||||
awaitingHomeReminders.value = true;
|
||||
notificationFallbackPending = false;
|
||||
notificationQueued = false;
|
||||
if (!getSessionContext().token) {
|
||||
if (userStore.state.loginPending) {
|
||||
awaitingHomeReminders.value = false;
|
||||
return;
|
||||
}
|
||||
if (!getSessionContext().token || !String(userStore.state.identifier || uni.getStorageSync('identifier') || '').trim()) {
|
||||
awaitingHomeReminders.value = false;
|
||||
handleIndexEntry();
|
||||
return;
|
||||
|
||||
@@ -71,7 +71,14 @@
|
||||
import { useUserStore } from '@/store/index.js';
|
||||
import { initWxConfig, wxScan, isInWechat } from '@/utils/wxsdk.js';
|
||||
|
||||
const WECHAT_LOGIN_CONTEXT_KEY = 'wechatLoginContext';
|
||||
|
||||
const userStore = useUserStore();
|
||||
// Close the asset-request gate synchronously, before cached homepage
|
||||
// components can react to the callback's old token or saved identifier.
|
||||
if (new URLSearchParams(window.location.search).get('code') && sessionStorage.getItem('assetToken')) {
|
||||
userStore.setLoginPending(true);
|
||||
}
|
||||
const identifier = ref(uni.getStorageSync('identifier') || '');
|
||||
const loading = ref(false);
|
||||
const agreed = ref(true);
|
||||
@@ -82,6 +89,24 @@
|
||||
const resetLoadingState = () => {
|
||||
loading.value = false;
|
||||
};
|
||||
const initializeLoginSdk = async () => {
|
||||
if (isInWechat()) await initWxConfig();
|
||||
};
|
||||
const resumeStoredSession = async (assetIdentifier) => {
|
||||
userStore.setLoginPending(true);
|
||||
loading.value = true;
|
||||
try {
|
||||
userStore.setIdentifier(assetIdentifier);
|
||||
await initializeLoginSdk();
|
||||
sessionStorage.removeItem('assetToken');
|
||||
sessionStorage.removeItem(WECHAT_LOGIN_CONTEXT_KEY);
|
||||
userStore.setLoginPending(false);
|
||||
uni.reLaunch({ url: '/pages/index/index' });
|
||||
} catch (e) {
|
||||
uni.showToast({ title: e?.msg || e?.message || '微信初始化失败,请重新登录', icon: 'none' });
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(async () => {
|
||||
// Remove the obsolete post-bind re-login notice from older client builds.
|
||||
@@ -89,13 +114,19 @@
|
||||
const urlIdentifier = getPathDeviceId();
|
||||
const token = uni.getStorageSync('token');
|
||||
const storedIdentifier = uni.getStorageSync('identifier') || '';
|
||||
// Complete OAuth first. An existing token or the callback's identifier
|
||||
// must not bypass the callback or start another authorization round.
|
||||
if (getCode() && sessionStorage.getItem('assetToken')) {
|
||||
await handleWechatCallback();
|
||||
return;
|
||||
}
|
||||
|
||||
// 从外部链接进入时,链接中的资产标识优先于本地登录态。
|
||||
// 只有相同资产才能复用已登录的会话,避免将上一台设备的数据带到新链接中。
|
||||
if (urlIdentifier) {
|
||||
identifier.value = urlIdentifier;
|
||||
if (token && urlIdentifier === storedIdentifier) {
|
||||
uni.reLaunch({ url: '/pages/index/index' });
|
||||
await resumeStoredSession(urlIdentifier);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -103,16 +134,15 @@
|
||||
userStore.clearUser();
|
||||
}
|
||||
|
||||
doLogin();
|
||||
await doLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
if (token) {
|
||||
uni.reLaunch({ url: '/pages/index/index' });
|
||||
if (token && storedIdentifier.trim()) {
|
||||
await resumeStoredSession(storedIdentifier.trim());
|
||||
return;
|
||||
}
|
||||
|
||||
handleWechatCallback();
|
||||
if (token) userStore.clearUser();
|
||||
|
||||
// 初始化微信 SDK (仅在微信浏览器内执行)
|
||||
// #ifdef H5
|
||||
@@ -163,10 +193,13 @@
|
||||
return wechatAppId.value;
|
||||
};
|
||||
|
||||
const redirectToWxAuth = async (assetToken) => {
|
||||
const redirectToWxAuth = async (assetToken, assetIdentifier) => {
|
||||
sessionStorage.setItem('assetToken', assetToken);
|
||||
sessionStorage.setItem(WECHAT_LOGIN_CONTEXT_KEY, JSON.stringify({ assetToken, identifier: assetIdentifier }));
|
||||
const appId = await getWechatAppId();
|
||||
const redirectUri = encodeURIComponent(window.location.origin + window.location.pathname);
|
||||
const callbackUrl = new URL(window.location.pathname, window.location.origin);
|
||||
callbackUrl.searchParams.set('identifier', assetIdentifier);
|
||||
const redirectUri = encodeURIComponent(callbackUrl.toString());
|
||||
const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=wechat_auth#wechat_redirect`;
|
||||
window.location.href = url;
|
||||
};
|
||||
@@ -290,19 +323,23 @@
|
||||
|
||||
const doLogin = async () => {
|
||||
loading.value = true;
|
||||
userStore.setLoginPending(true);
|
||||
// 丢弃上一轮未完成授权留下的临时凭证,避免校验失败时继续复用。
|
||||
if (typeof sessionStorage !== 'undefined') {
|
||||
sessionStorage.removeItem('assetToken');
|
||||
sessionStorage.removeItem(WECHAT_LOGIN_CONTEXT_KEY);
|
||||
}
|
||||
try {
|
||||
const enteredIdentifier = identifier.value;
|
||||
const enteredIdentifier = String(identifier.value || '').trim();
|
||||
if (!enteredIdentifier) throw { msg: '请输入有效的资产标识' };
|
||||
const { verifyData, verifiedIdentifier } = await verifyAssetWithIccidRetry(enteredIdentifier);
|
||||
|
||||
userStore.setAssetToken(verifyData.asset_token);
|
||||
userStore.setIdentifier(verifiedIdentifier);
|
||||
|
||||
await redirectToWxAuth(verifyData.asset_token);
|
||||
await redirectToWxAuth(verifyData.asset_token, verifiedIdentifier);
|
||||
} catch (e) {
|
||||
userStore.setLoginPending(false);
|
||||
console.error('登录失败', e);
|
||||
uni.showToast({
|
||||
title: e?.msg || e?.message || '资产校验失败,请稍后重试',
|
||||
@@ -318,15 +355,38 @@
|
||||
const assetToken = sessionStorage.getItem('assetToken');
|
||||
|
||||
if (!code || !assetToken) return;
|
||||
userStore.setLoginPending(true);
|
||||
let loginContext = null;
|
||||
try {
|
||||
loginContext = JSON.parse(sessionStorage.getItem(WECHAT_LOGIN_CONTEXT_KEY) || 'null');
|
||||
} catch (e) {
|
||||
// Old clients may only have the legacy assetToken and saved identifier.
|
||||
}
|
||||
const contextIdentifier = loginContext?.assetToken === assetToken && typeof loginContext.identifier === 'string'
|
||||
? loginContext.identifier.trim() : '';
|
||||
const loginIdentifier = contextIdentifier || getPathDeviceId() ||
|
||||
String(uni.getStorageSync('identifier') || userStore.state.identifier || identifier.value || '').trim();
|
||||
if (!loginIdentifier) {
|
||||
clearCode();
|
||||
sessionStorage.removeItem('assetToken');
|
||||
sessionStorage.removeItem(WECHAT_LOGIN_CONTEXT_KEY);
|
||||
userStore.clearUser();
|
||||
uni.showToast({ title: '缺少资产标识,请重新输入或扫码登录', icon: 'none' });
|
||||
loading.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
loading.value = true;
|
||||
clearCode();
|
||||
|
||||
try {
|
||||
const loginData = await authApi.wechatLogin(assetToken, code);
|
||||
// Restore the verified identifier before publishing the login state or
|
||||
// mounting homepage components that immediately request asset/info.
|
||||
userStore.setIdentifier(loginIdentifier);
|
||||
userStore.setToken(loginData.token);
|
||||
userStore.setLoginPhoneState(loginData);
|
||||
uni.setStorageSync('identifier', userStore.state.identifier);
|
||||
identifier.value = loginIdentifier;
|
||||
|
||||
// #ifdef H5
|
||||
if (loginData.user_info) {
|
||||
@@ -337,6 +397,13 @@
|
||||
}
|
||||
// #endif
|
||||
|
||||
// Keep the gate closed while JSSDK config/ready is still pending.
|
||||
// Homepage and bind-page asset/info requests only start afterwards.
|
||||
await initializeLoginSdk();
|
||||
sessionStorage.removeItem('assetToken');
|
||||
sessionStorage.removeItem(WECHAT_LOGIN_CONTEXT_KEY);
|
||||
userStore.setLoginPending(false);
|
||||
|
||||
if (userStore.state.needBindPhone === true) {
|
||||
uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' });
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user