diff --git a/h5/agency-center/agency-center.js b/h5/agency-center/agency-center.js index b44ac8b..b7f1337 100644 --- a/h5/agency-center/agency-center.js +++ b/h5/agency-center/agency-center.js @@ -431,11 +431,17 @@ function getAllowedPages(roles) { return pages; } +function getCurrentRoutePath() { + const pathWithoutSlash = window.location.pathname.replace(/\/+$/, "") || "/"; + const pathWithoutIndex = pathWithoutSlash.replace(/\/index\.html$/, "") || "/"; + return pathWithoutIndex.replace(/^\/h5(?=\/|$)/, "") || "/"; +} + function applyPermissionRedirect() { const roles = getIdentityRoles(state.identityInfo); const primaryRole = getPrimaryRole(roles); const allowedPages = getAllowedPages(roles); - const currentPath = window.location.pathname.replace(/\/$/, "") || "/"; + const currentPath = getCurrentRoutePath(); if (!allowedPages.has(currentPath)) { navigateTo(roleDefaultPages[primaryRole] || "/apply"); diff --git a/h5/bd-static/shared.js b/h5/bd-static/shared.js index 5ca31d4..092ab2e 100644 --- a/h5/bd-static/shared.js +++ b/h5/bd-static/shared.js @@ -153,7 +153,7 @@ const text = { }); let headers = { - authorization: "Bearer undefined", + authorization: "", "req-app-intel": "version=1.0.0;build=1;model=SM-G9550;sysVersion=9;channel=Google", "req-client": "Android", "req-imei": "8fa54d728ab449e04f9292329ed44bda", @@ -165,6 +165,7 @@ let headers = { }; export const lang = resolveLanguage(); +let appConnectionPromise = null; export function t(key) { return text[lang]?.[key] || text.en[key] || key; @@ -206,21 +207,21 @@ function parseAppHeaders(raw) { const appIntel = splitHeaderPairs(data["Req-App-Intel"]); const sysOrigin = splitHeaderPairs(data["Req-Sys-Origin"]); return { - authorization: data.Authorization, - reqLang: data["Req-Lang"], - appVersion: appIntel.version, - buildVersion: appIntel.build, - channel: appIntel.channel, - reqImei: appIntel["Req-Imei"], - origin: sysOrigin.origin, - child: sysOrigin.child + authorization: data.Authorization || data.authorization, + reqLang: data["Req-Lang"] || data.reqLang || data.lang, + appVersion: appIntel.version || data["App-Version"] || data.appVersion, + buildVersion: appIntel.build || data["Build-Version"] || data.buildVersion, + channel: appIntel.channel || data["App-Channel"] || data.appChannel || data.channel, + reqImei: data["Req-Imei"] || data.reqImei || appIntel["Req-Imei"], + origin: sysOrigin.origin || data["Sys-Origin"] || data.sysOrigin, + child: sysOrigin.originChild || sysOrigin.child || data["Sys-Origin-Child"] || data.sysOriginChild }; } export function setHeadersFromApp(rawOrObject) { const data = typeof rawOrObject === "string" ? parseAppHeaders(rawOrObject) : rawOrObject || {}; const next = {}; - if (data.authorization) next.authorization = data.authorization; + if (Object.prototype.hasOwnProperty.call(data, "authorization")) next.authorization = data.authorization || ""; if (data.reqLang) next["req-lang"] = data.reqLang; if (data.appVersion || data.buildVersion || data.channel) { next["req-app-intel"] = [ @@ -234,18 +235,87 @@ export function setHeadersFromApp(rawOrObject) { headers = { ...headers, ...next }; } +function isAppEnvironment() { + return Boolean(window.app || window.webkit || window.FlutterPageControl || window.FlutterApp); +} + +function requestAccessOrigin() { + return new Promise((resolve) => { + let settled = false; + let timeoutId = null; + const finish = (raw) => { + if (settled) { + if (raw) setHeadersFromApp(raw); + return; + } + settled = true; + if (timeoutId) window.clearTimeout(timeoutId); + resolve(raw || null); + }; + + window.renderData = finish; + window.getIosAccessOriginParam = finish; + + if (!isAppEnvironment()) { + setHeadersFromApp({ + authorization: "", + reqLang: lang, + appVersion: "1.0.0", + buildVersion: "1", + channel: "Web", + reqImei: "H5-STATIC", + origin: "LIKEI", + child: "LIKEI" + }); + finish(null); + return; + } + + const requestFromFlutter = () => { + if (window.FlutterApp && typeof window.FlutterApp.postMessage === "function") { + window.FlutterApp.postMessage("requestAccessOrigin"); + } + }; + + if (window.app && typeof window.app.getAccessOrigin === "function") { + finish(window.app.getAccessOrigin()); + return; + } + + requestFromFlutter(); + timeoutId = window.setTimeout(() => finish(null), 12000); + + let attempt = 0; + const poll = () => { + attempt += 1; + if (window.app && typeof window.app.getAccessOrigin === "function") { + finish(window.app.getAccessOrigin()); + return; + } + if (attempt < 50) { + window.setTimeout(poll, 100); + return; + } + requestFromFlutter(); + }; + + poll(); + }); +} + export async function connectToApp() { - window.renderData = (raw) => raw && setHeadersFromApp(raw); - window.getIosAccessOriginParam = window.renderData; - if (window.app && typeof window.app.getAccessOrigin === "function") { - const raw = window.app.getAccessOrigin(); - if (raw) setHeadersFromApp(raw); - return; + if (!appConnectionPromise) { + appConnectionPromise = requestAccessOrigin() + .then((raw) => { + if (raw) setHeadersFromApp(raw); + return { success: true, environment: isAppEnvironment() ? "app" : "browser" }; + }) + .finally(() => { + appConnectionPromise = null; + }); } - if (window.FlutterApp && typeof window.FlutterApp.postMessage === "function") { - window.FlutterApp.postMessage("requestAccessOrigin"); - } - setHeadersFromApp({ reqLang: lang, authorization: "" }); + + return appConnectionPromise; } export async function request(method, path, body) { diff --git a/h5/invite-agency/index.html b/h5/invite-agency/index.html index 8d12d9c..a61d085 100644 --- a/h5/invite-agency/index.html +++ b/h5/invite-agency/index.html @@ -23,6 +23,6 @@