diff --git a/h5/activity/recharge-agency-list/index.html b/h5/activity/recharge-agency-list/index.html index 7b45028..b5cba4d 100644 --- a/h5/activity/recharge-agency-list/index.html +++ b/h5/activity/recharge-agency-list/index.html @@ -343,6 +343,7 @@ const DEFAULT_API_BASE = "https://jvapi.haiyihy.com/"; const DEFAULT_API_PREFIX = "/go"; const SELF_PROFILE_ENDPOINT = "/team/member/profile"; + const COUNTRY_CONFIG_ENDPOINT = "/sys/config/country"; const SELLER_ENDPOINT = "/app/h5/recharge-agency-list/sellers"; const DEFAULT_LIST_LIMIT = "500"; const DEFAULT_AVATAR = "../../assets/defaultAvatar-CdxWBK1k.png"; @@ -351,6 +352,7 @@ const listNode = document.querySelector("[data-agency-list]"); const browserLang = typeof navigator !== "undefined" ? navigator.language : "en"; const currentLang = normalizeLanguage(readParam(["lang"]) || browserLang); + let countryFlagMap = Object.create(null); function trimValue(value) { return String(value || "").trim(); @@ -698,9 +700,89 @@ return raw ? raw.split(",").map(trimValue).filter(Boolean) : []; } + function normalizeLookupKey(value) { + return trimValue(value).toLowerCase().replace(/\s+/g, " "); + } + + function addCountryFlag(map, key, flag) { + const normalizedKey = normalizeLookupKey(key); + const flagURL = trimValue(flag); + if (normalizedKey && flagURL && !map[normalizedKey]) { + map[normalizedKey] = flagURL; + } + } + + function splitCountryRegion(countryName, regionCode) { + const country = trimValue(countryName); + const region = trimValue(regionCode); + if (!country || region) { + return { countryName: country, regionCode: region }; + } + const parts = country.split(/\s+[-\u2013\u2014]\s+/).map(trimValue).filter(Boolean); + if (parts.length <= 1) { + return { countryName: country, regionCode: region }; + } + return { + countryName: parts[0], + regionCode: parts.slice(1).join(" - ") + }; + } + + function buildCountryFlagMap(json) { + const body = json && (json.body || json.data || json); + const countries = []; + const pushCountries = function (value) { + if (Array.isArray(value)) { + value.forEach(function (country) { countries.push(country); }); + } + }; + pushCountries(body); + if (body && typeof body === "object") { + pushCountries(body.openCountry); + pushCountries(body.tops); + pushCountries(body.countries); + pushCountries(body.records); + pushCountries(body.list); + } + + const map = Object.create(null); + countries.forEach(function (country) { + if (!country || typeof country !== "object") return; + const flag = firstNonBlank(country.nationalFlag, country.countryFlag, country.flag); + addCountryFlag(map, country.countryName, flag); + addCountryFlag(map, country.aliasName, flag); + addCountryFlag(map, country.enName, flag); + addCountryFlag(map, country.alphaTwo, flag); + addCountryFlag(map, country.alphaThree, flag); + addCountryFlag(map, country.countryCode, flag); + if (Array.isArray(country.countryCodeAliases)) { + country.countryCodeAliases.forEach(function (alias) { + addCountryFlag(map, alias, flag); + }); + } + }); + return map; + } + + function findCountryFlag() { + for (let index = 0; index < arguments.length; index += 1) { + const key = normalizeLookupKey(arguments[index]); + if (key && countryFlagMap[key]) return countryFlagMap[key]; + } + return ""; + } + function normalizeSeller(item) { const source = item || {}; const userBaseInfo = source.userBaseInfo || {}; + const rawCountryName = firstNonBlank(source.countryName, userBaseInfo.countryName); + const countryRegion = splitCountryRegion( + rawCountryName, + firstNonBlank(source.regionCode, userBaseInfo.regionCode) + ); + const countryCode = firstNonBlank(source.countryCode, userBaseInfo.countryCode); + const nationalFlag = normalizeFlagList(source.nationalFlag || source.nationalFlags); + const explicitCountryFlag = firstNonBlank(source.ownNationalFlag, source.countryFlag); return { id: firstNonBlank(source.id, userBaseInfo.id), sysOrigin: firstNonBlank(source.sysOrigin, userBaseInfo.originSys), @@ -712,14 +794,18 @@ ), userAvatar: firstNonBlank(source.userAvatar, userBaseInfo.userAvatar), userNickname: firstNonBlank(source.userNickname, userBaseInfo.userNickname, userBaseInfo.account), - countryCode: firstNonBlank(source.countryCode, userBaseInfo.countryCode), - countryName: firstNonBlank(source.countryName, userBaseInfo.countryName), - regionCode: firstNonBlank(source.regionCode, userBaseInfo.regionCode), + countryCode: countryCode, + countryName: countryRegion.countryName, + regionCode: countryRegion.regionCode, contactInfo: firstNonBlank(source.contactInfo, source.contact, source.phone), whatsapp: firstNonBlank(source.whatsapp, source.whatsApp, source.contactInfo, source.contact, source.phone), - nationalFlag: normalizeFlagList(source.nationalFlag || source.nationalFlags), + nationalFlag: nationalFlag, ownNationalFlag: firstNonBlank(source.ownNationalFlag), - countryFlag: firstNonBlank(source.ownNationalFlag, source.countryFlag, normalizeFlagList(source.nationalFlag || source.nationalFlags)[0]), + countryFlag: firstNonBlank( + explicitCountryFlag, + findCountryFlag(countryRegion.countryName, rawCountryName, countryCode), + nationalFlag[0] + ), transactionCount: source.transactionCount }; } @@ -750,6 +836,15 @@ return json && (json.body || json.data || json); } + async function fetchCountryConfig(authorization) { + try { + const json = await requestJSON(buildURL(resolveAPIBase(), COUNTRY_CONFIG_ENDPOINT), authorization); + countryFlagMap = buildCountryFlagMap(json); + } catch (_) { + countryFlagMap = Object.create(null); + } + } + async function fetchSellerList(authorization) { const json = await requestJSON(buildSellerURL(), authorization); return normalizePayload(json); @@ -764,6 +859,7 @@ } try { await fetchSelfProfile(authorization); + await fetchCountryConfig(authorization); const records = await fetchSellerList(authorization); if (!records.length) { renderState("No agencies available.");