|
|
@@ -290,6 +290,15 @@ function shouldRefreshSessionForUrlUser() {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ // 小程序每次打开 H5 都会透传 URL 登录态。同步完成后,页面级
|
|
|
+ // ensureMiniProgramSessionFresh({force:true}) 不应再无条件调用 ssLogin:
|
|
|
+ // 同账号且已有服务端会话时直接复用,只有账号变化或会话缺失才刷新。
|
|
|
+ // 否则多个 WebView 同时自动登录会反复轮换设备令牌,后发请求可能拿到旧令牌。
|
|
|
+ const urlAuthState = window.__MINI_AUTH_STATE__ || {};
|
|
|
+ if (urlAuthState.isDifferentUrlUser) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
const lastAuthUserId = normalizeAuthUserId(
|
|
|
localStorage.getItem(H5_LAST_AUTH_USER_ID_KEY)
|
|
|
);
|
|
|
@@ -298,6 +307,8 @@ function shouldRefreshSessionForUrlUser() {
|
|
|
storedUserInfo.yhid || storedUserInfo.userId
|
|
|
);
|
|
|
|
|
|
+ // 本机还没有完成过一次账号确认时,仍先恢复一次会话;
|
|
|
+ // 后端令牌自动登录已改为幂等,不会因此轮换令牌或让其他页面下线。
|
|
|
if (!lastAuthUserId) {
|
|
|
return true;
|
|
|
}
|
|
|
@@ -308,20 +319,10 @@ function shouldRefreshSessionForUrlUser() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- const lastAuthToken = localStorage.getItem(H5_LAST_AUTH_TOKEN_KEY) || "";
|
|
|
- if (lastAuthToken && lastAuthToken !== token) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
const currentSessionId = getJSessionId();
|
|
|
- const lastSessionId =
|
|
|
- localStorage.getItem(H5_LAST_AUTH_SESSION_ID_KEY) || "";
|
|
|
if (!currentSessionId) {
|
|
|
return true;
|
|
|
}
|
|
|
- if (lastSessionId && currentSessionId !== lastSessionId) {
|
|
|
- return true;
|
|
|
- }
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
@@ -620,8 +621,23 @@ async function ensureMiniProgramSessionFresh(options = {}) {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ // force 只用于绕过短时间冷却;页面初始化不能仅凭 force 就重复登录。
|
|
|
+ // 真正的会话失效由请求层传 forceRecovery=true,换账号则由 URL 账号状态识别。
|
|
|
+ const shouldRecover =
|
|
|
+ forceRecovery || shouldRefreshSessionForUrlUser();
|
|
|
+ if (!shouldRecover) {
|
|
|
+ console.log("H5_SESSION_REFRESH_SKIP_SAME_USER", {
|
|
|
+ page: options.page || "",
|
|
|
+ service: options.service || "",
|
|
|
+ urlUserId,
|
|
|
+ force: forceRefresh,
|
|
|
+ });
|
|
|
+ return { refreshed: false, skipped: true, reason: "SAME_USER" };
|
|
|
+ }
|
|
|
+
|
|
|
if (
|
|
|
forceRefresh &&
|
|
|
+ !forceRecovery &&
|
|
|
!options.bypassCooldown &&
|
|
|
urlUserId &&
|
|
|
hasFreshMiniProgramSession(token, urlUserId)
|
|
|
@@ -634,14 +650,6 @@ async function ensureMiniProgramSessionFresh(options = {}) {
|
|
|
return { refreshed: false, skipped: true, reason: "RECENT_SESSION" };
|
|
|
}
|
|
|
|
|
|
- if (!forceRefresh && !forceRecovery && !shouldRefreshSessionForUrlUser()) {
|
|
|
- console.log("H5_SESSION_REFRESH_SKIP_SAME_USER", {
|
|
|
- page: options.page || "",
|
|
|
- service: options.service || "",
|
|
|
- urlUserId,
|
|
|
- });
|
|
|
- return { refreshed: false, skipped: true, reason: "SAME_USER" };
|
|
|
- }
|
|
|
if (forceRefresh) {
|
|
|
console.log("H5_SESSION_REFRESH_FORCE", {
|
|
|
page: options.page || "",
|