fix: load recharge agency list from api
This commit is contained in:
parent
548f0a5e80
commit
7f28fdbe46
@ -310,11 +310,15 @@
|
||||
(function () {
|
||||
const DEFAULT_API_BASE = "https://jvapi.haiyihy.com/";
|
||||
const DEFAULT_API_PREFIX = "/go";
|
||||
const ENDPOINT = "/app/h5/recharge-agency-list/sellers";
|
||||
const SELF_PROFILE_ENDPOINT = "/team/member/profile";
|
||||
const SELLER_ENDPOINT = "/app/h5/recharge-agency-list/sellers";
|
||||
const DEFAULT_LIST_LIMIT = "500";
|
||||
const DEFAULT_AVATAR = "../../assets/defaultAvatar-CdxWBK1k.png";
|
||||
const query = new URLSearchParams(window.location.search || "");
|
||||
const hashQuery = new URLSearchParams((window.location.hash.split("?")[1] || "").split("#")[0]);
|
||||
const listNode = document.querySelector("[data-agency-list]");
|
||||
const browserLang = typeof navigator !== "undefined" ? navigator.language : "en";
|
||||
const currentLang = normalizeLanguage(readParam(["lang"]) || browserLang);
|
||||
|
||||
function trimValue(value) {
|
||||
return String(value || "").trim();
|
||||
@ -330,6 +334,43 @@
|
||||
return "";
|
||||
}
|
||||
|
||||
function readRawParam(name) {
|
||||
const queries = [window.location.search.replace(/^\?/, "")];
|
||||
if (window.location.hash.includes("?")) {
|
||||
queries.push(window.location.hash.split("?")[1].split("#")[0]);
|
||||
}
|
||||
|
||||
for (const rawQuery of queries) {
|
||||
for (const part of rawQuery.split("&")) {
|
||||
if (!part) continue;
|
||||
const equalIndex = part.indexOf("=");
|
||||
const rawKey = equalIndex >= 0 ? part.slice(0, equalIndex) : part;
|
||||
const rawValue = equalIndex >= 0 ? part.slice(equalIndex + 1) : "";
|
||||
let decodedKey = "";
|
||||
try {
|
||||
decodedKey = decodeURIComponent(rawKey);
|
||||
} catch (_) {
|
||||
continue;
|
||||
}
|
||||
if (decodedKey !== name) continue;
|
||||
try {
|
||||
return decodeURIComponent(rawValue.replace(/\+/g, "%2B"));
|
||||
} catch (_) {
|
||||
return rawValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function normalizeLanguage(lang) {
|
||||
const value = trimValue(lang).toLowerCase();
|
||||
if (value.startsWith("ar")) return "ar";
|
||||
if (value.startsWith("tr")) return "tr";
|
||||
if (value.startsWith("id")) return "id";
|
||||
return "en";
|
||||
}
|
||||
|
||||
function normalizeBaseURL(base) {
|
||||
const value = trimValue(base) || DEFAULT_API_BASE;
|
||||
return value.endsWith("/") ? value : value + "/";
|
||||
@ -338,6 +379,9 @@
|
||||
function resolveAPIBase() {
|
||||
const override = readParam(["apiBase", "api_base"]);
|
||||
if (override) return normalizeBaseURL(override);
|
||||
if (typeof window.HYAPP_API_BASE_URL === "string") {
|
||||
return normalizeBaseURL(window.HYAPP_API_BASE_URL);
|
||||
}
|
||||
if (typeof window.__RECHARGE_AGENCY_API_BASE__ === "string") {
|
||||
return normalizeBaseURL(window.__RECHARGE_AGENCY_API_BASE__);
|
||||
}
|
||||
@ -357,6 +401,21 @@
|
||||
}
|
||||
}
|
||||
|
||||
function resolveGoAPIBase() {
|
||||
const override = readParam(["goApiBase", "goBase", "go_api_base"]);
|
||||
if (override) return normalizeBaseURL(override);
|
||||
if (typeof window.HYAPP_GO_API_BASE_URL === "string") {
|
||||
return normalizeBaseURL(window.HYAPP_GO_API_BASE_URL);
|
||||
}
|
||||
if (typeof window.__RECHARGE_AGENCY_GO_API_BASE__ === "string") {
|
||||
return normalizeBaseURL(window.__RECHARGE_AGENCY_GO_API_BASE__);
|
||||
}
|
||||
const apiBase = resolveAPIBase();
|
||||
const apiPrefix = resolveAPIPrefix(apiBase);
|
||||
if (!apiPrefix) return apiBase;
|
||||
return normalizeBaseURL(new URL(joinPath(apiPrefix).replace(/^\/+/, ""), apiBase).toString());
|
||||
}
|
||||
|
||||
function joinPath() {
|
||||
return "/" + Array.prototype.slice.call(arguments)
|
||||
.map((part) => trimValue(part).replace(/^\/+|\/+$/g, ""))
|
||||
@ -364,12 +423,14 @@
|
||||
.join("/");
|
||||
}
|
||||
|
||||
function buildURL(path) {
|
||||
const apiBase = resolveAPIBase();
|
||||
const apiPrefix = resolveAPIPrefix(apiBase);
|
||||
const url = new URL(joinPath(apiPrefix, path).replace(/^\/+/, ""), apiBase);
|
||||
function buildURL(baseURL, path) {
|
||||
return new URL(trimValue(path).replace(/^\/+/, ""), normalizeBaseURL(baseURL));
|
||||
}
|
||||
|
||||
function buildSellerURL() {
|
||||
const url = buildURL(resolveGoAPIBase(), SELLER_ENDPOINT);
|
||||
url.searchParams.set("cursor", readParam(["cursor"]) || "1");
|
||||
url.searchParams.set("limit", readParam(["limit"]) || "20");
|
||||
url.searchParams.set("limit", readParam(["limit"]) || DEFAULT_LIST_LIMIT);
|
||||
return url;
|
||||
}
|
||||
|
||||
@ -380,9 +441,9 @@
|
||||
}
|
||||
|
||||
function readAuthorization() {
|
||||
const direct = readParam(["Authorization", "authorization"]);
|
||||
const direct = readRawParam("Authorization") || readRawParam("authorization");
|
||||
if (direct) return normalizeAuthorization(direct);
|
||||
const token = readParam(["token", "accessToken", "access_token"]);
|
||||
const token = readRawParam("token") || readRawParam("accessToken") || readRawParam("access_token");
|
||||
if (token) return normalizeAuthorization(token);
|
||||
if (typeof window.__RECHARGE_AGENCY_AUTHORIZATION__ === "string") {
|
||||
return normalizeAuthorization(window.__RECHARGE_AGENCY_AUTHORIZATION__);
|
||||
@ -399,6 +460,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
function authHeadersFromAuthorization(authorization) {
|
||||
return {
|
||||
Accept: "application/json",
|
||||
Authorization: authorization,
|
||||
"req-lang": currentLang,
|
||||
"req-client": "H5",
|
||||
"req-version": "V2",
|
||||
"req-zone": Intl.DateTimeFormat().resolvedOptions().timeZone || "Asia/Shanghai",
|
||||
"req-app-intel": "version=1.0.0;build=1;model=H5;sysVersion=Web;channel=Web",
|
||||
"req-sys-origin": "origin=ATYOU;originChild=ATYOU"
|
||||
};
|
||||
}
|
||||
|
||||
function clearList() {
|
||||
while (listNode.firstChild) listNode.removeChild(listNode.firstChild);
|
||||
}
|
||||
@ -500,6 +574,7 @@
|
||||
}
|
||||
|
||||
function createRow(item) {
|
||||
item = normalizeSeller(item);
|
||||
const row = document.createElement("article");
|
||||
row.className = "agency-row";
|
||||
row.dataset.userId = trimValue(item.userId);
|
||||
@ -565,9 +640,74 @@
|
||||
return row;
|
||||
}
|
||||
|
||||
function firstNonBlank() {
|
||||
for (let index = 0; index < arguments.length; index += 1) {
|
||||
const value = trimValue(arguments[index]);
|
||||
if (value) return value;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function normalizeFlagList(value) {
|
||||
if (Array.isArray(value)) return value.map(trimValue).filter(Boolean);
|
||||
const raw = trimValue(value);
|
||||
return raw ? raw.split(",").map(trimValue).filter(Boolean) : [];
|
||||
}
|
||||
|
||||
function normalizeSeller(item) {
|
||||
const source = item || {};
|
||||
const userBaseInfo = source.userBaseInfo || {};
|
||||
return {
|
||||
id: firstNonBlank(source.id, userBaseInfo.id),
|
||||
sysOrigin: firstNonBlank(source.sysOrigin, userBaseInfo.originSys),
|
||||
userId: firstNonBlank(source.userId, source.uid, userBaseInfo.id),
|
||||
account: firstNonBlank(
|
||||
source.account,
|
||||
userBaseInfo.ownSpecialId && userBaseInfo.ownSpecialId.account,
|
||||
userBaseInfo.account
|
||||
),
|
||||
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),
|
||||
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),
|
||||
ownNationalFlag: firstNonBlank(source.ownNationalFlag),
|
||||
transactionCount: source.transactionCount
|
||||
};
|
||||
}
|
||||
|
||||
function normalizePayload(json) {
|
||||
const body = json && (json.body || json.data || json);
|
||||
return body && Array.isArray(body.records) ? body.records : [];
|
||||
if (Array.isArray(body)) return body;
|
||||
if (body && Array.isArray(body.records)) return body.records;
|
||||
if (body && Array.isArray(body.list)) return body.list;
|
||||
if (body && Array.isArray(body.items)) return body.items;
|
||||
return [];
|
||||
}
|
||||
|
||||
async function requestJSON(url, authorization) {
|
||||
const response = await fetch(url.toString(), {
|
||||
cache: "no-store",
|
||||
headers: authHeadersFromAuthorization(authorization),
|
||||
});
|
||||
const json = await response.json().catch(function () { return {}; });
|
||||
if (!response.ok || json.status === false) {
|
||||
throw new Error(json.errorMsg || json.message || "Unable to load agencies.");
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
async function fetchSelfProfile(authorization) {
|
||||
const json = await requestJSON(buildURL(resolveAPIBase(), SELF_PROFILE_ENDPOINT), authorization);
|
||||
return json && (json.body || json.data || json);
|
||||
}
|
||||
|
||||
async function fetchSellerList(authorization) {
|
||||
const json = await requestJSON(buildSellerURL(), authorization);
|
||||
return normalizePayload(json);
|
||||
}
|
||||
|
||||
async function fetchAgencies() {
|
||||
@ -578,18 +718,8 @@
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const response = await fetch(buildURL(ENDPOINT).toString(), {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: authorization,
|
||||
},
|
||||
});
|
||||
const json = await response.json().catch(function () { return {}; });
|
||||
if (!response.ok || json.status === false) {
|
||||
throw new Error(json.errorMsg || json.message || "Unable to load agencies.");
|
||||
}
|
||||
const records = normalizePayload(json);
|
||||
await fetchSelfProfile(authorization);
|
||||
const records = await fetchSellerList(authorization);
|
||||
if (!records.length) {
|
||||
renderState("No agencies available.");
|
||||
return;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user