best-unit 1.2.10 → 1.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/best-unit.cjs +9 -9
- package/dist/best-unit.js +695 -706
- package/package.json +1 -1
- package/src/api/axiosInstance.ts +43 -91
- package/src/api/waitUnit.ts +62 -0
package/package.json
CHANGED
package/src/api/axiosInstance.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
} from "axios";
|
|
7
7
|
import { message } from "@/components/common/message";
|
|
8
8
|
import { Env, Locale } from "@/types";
|
|
9
|
+
import { waitFundUnitReady } from "./waitUnit";
|
|
9
10
|
|
|
10
11
|
export interface CreateAxiosOptions {
|
|
11
12
|
baseURL?: string;
|
|
@@ -14,49 +15,50 @@ export interface CreateAxiosOptions {
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
17
|
-
//
|
|
18
|
-
let
|
|
19
|
-
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
console.log("fundUnitParams", fundUnitParams);
|
|
23
|
-
const { env } = fundUnitParams;
|
|
24
|
-
|
|
25
|
-
let apiUrl: string;
|
|
26
|
-
switch (env) {
|
|
27
|
-
case Env.DEV:
|
|
28
|
-
case Env.DEVELOPMENT:
|
|
29
|
-
apiUrl = "/api";
|
|
30
|
-
break;
|
|
31
|
-
case Env.TEST:
|
|
32
|
-
apiUrl = "https://fund.bestfulfill.tech/api/sdk";
|
|
33
|
-
break;
|
|
34
|
-
case Env.PROD:
|
|
35
|
-
case Env.PRODUCTION:
|
|
36
|
-
default:
|
|
37
|
-
apiUrl = "https://fund.bestfulfill.com/api/sdk";
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
18
|
+
// 实例初始的 baseURL 仅作兜底,真正的 baseURL 在请求阶段基于最新 fund_unit_params 计算
|
|
19
|
+
let apiUrl: string = "https://fund.bestfulfill.com/api/sdk";
|
|
40
20
|
|
|
41
21
|
const { baseURL = apiUrl, timeout = 10000, onError } = options;
|
|
42
22
|
|
|
43
23
|
const instance: AxiosInstance = axios.create({ baseURL, timeout });
|
|
44
24
|
|
|
45
|
-
//
|
|
46
|
-
instance.interceptors.request.use(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
25
|
+
// 请求拦截:等待 fund_unit_params 就绪,动态设置 baseURL、token、国际化
|
|
26
|
+
instance.interceptors.request.use(
|
|
27
|
+
async (config: InternalAxiosRequestConfig) => {
|
|
28
|
+
// 等待 fund_unit_params 非空
|
|
29
|
+
await waitFundUnitReady();
|
|
30
|
+
|
|
31
|
+
const fundUnitParams = JSON.parse(
|
|
32
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const { env, token, locale } = fundUnitParams || {};
|
|
36
|
+
|
|
37
|
+
// 按 env 设置 baseURL(覆盖实例级 baseURL,以便热切换环境)
|
|
38
|
+
switch (env) {
|
|
39
|
+
case Env.DEV:
|
|
40
|
+
case Env.DEVELOPMENT:
|
|
41
|
+
config.baseURL = "/api";
|
|
42
|
+
break;
|
|
43
|
+
case Env.TEST:
|
|
44
|
+
config.baseURL = "https://fund.bestfulfill.tech/api/sdk";
|
|
45
|
+
break;
|
|
46
|
+
case Env.PROD:
|
|
47
|
+
case Env.PRODUCTION:
|
|
48
|
+
default:
|
|
49
|
+
config.baseURL = "https://fund.bestfulfill.com/api/sdk";
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
config.headers = {
|
|
54
|
+
...config.headers,
|
|
55
|
+
Authorization: token,
|
|
56
|
+
"x-locale": locale === Locale.ZH ? "zh-CN" : "en-US",
|
|
57
|
+
} as any;
|
|
58
|
+
|
|
59
|
+
return config;
|
|
60
|
+
}
|
|
61
|
+
);
|
|
60
62
|
|
|
61
63
|
// 响应拦截:code=0判定成功,其他走 onError
|
|
62
64
|
instance.interceptors.response.use(
|
|
@@ -97,63 +99,13 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
97
99
|
|
|
98
100
|
// 缓存 axios 实例
|
|
99
101
|
let httpInstance: AxiosInstance | null = null;
|
|
100
|
-
let creatingPromise: Promise<void> | null = null;
|
|
101
|
-
|
|
102
|
-
// 等待 fund_unit_params 不为空对象(无超时)
|
|
103
|
-
function waitUntilParamsReady(): Promise<void> {
|
|
104
|
-
return new Promise((resolve) => {
|
|
105
|
-
const check = () => {
|
|
106
|
-
try {
|
|
107
|
-
const obj = JSON.parse(
|
|
108
|
-
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
109
|
-
);
|
|
110
|
-
if (obj && Object.keys(obj).length > 0) return resolve();
|
|
111
|
-
} catch (_) {
|
|
112
|
-
// ignore
|
|
113
|
-
}
|
|
114
|
-
setTimeout(check, 50);
|
|
115
|
-
};
|
|
116
|
-
check();
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
102
|
|
|
120
103
|
// 获取 axios 实例的函数
|
|
121
104
|
export function http(): AxiosInstance {
|
|
122
|
-
if (httpInstance)
|
|
123
|
-
|
|
124
|
-
async function ensureInstanceReady(): Promise<void> {
|
|
125
|
-
if (httpInstance) return;
|
|
126
|
-
if (!creatingPromise) {
|
|
127
|
-
creatingPromise = waitUntilParamsReady()
|
|
128
|
-
.then(() => {
|
|
129
|
-
httpInstance = createAxiosInstance();
|
|
130
|
-
})
|
|
131
|
-
.finally(() => {
|
|
132
|
-
creatingPromise = null;
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
return creatingPromise;
|
|
105
|
+
if (!httpInstance) {
|
|
106
|
+
httpInstance = createAxiosInstance();
|
|
136
107
|
}
|
|
137
|
-
|
|
138
|
-
const proxy: any = {};
|
|
139
|
-
[
|
|
140
|
-
"request",
|
|
141
|
-
"get",
|
|
142
|
-
"delete",
|
|
143
|
-
"head",
|
|
144
|
-
"options",
|
|
145
|
-
"post",
|
|
146
|
-
"put",
|
|
147
|
-
"patch",
|
|
148
|
-
].forEach((method) => {
|
|
149
|
-
proxy[method] = async (...args: any[]) => {
|
|
150
|
-
await ensureInstanceReady();
|
|
151
|
-
// @ts-ignore
|
|
152
|
-
return (httpInstance as any)[method](...args);
|
|
153
|
-
};
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
return proxy as AxiosInstance;
|
|
108
|
+
return httpInstance;
|
|
157
109
|
}
|
|
158
110
|
|
|
159
111
|
// 重置 axios 实例(当 fund_unit_params 更新时调用)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// 等待 fund_unit_params 就绪的工具
|
|
2
|
+
|
|
3
|
+
type ResolveFn = () => void;
|
|
4
|
+
|
|
5
|
+
let pendingResolvers: ResolveFn[] = [];
|
|
6
|
+
let pollingTimer: number | null = null;
|
|
7
|
+
|
|
8
|
+
function readFundUnitParams(): Record<string, any> {
|
|
9
|
+
try {
|
|
10
|
+
const raw = sessionStorage.getItem("fund_unit_params") || "{}";
|
|
11
|
+
return JSON.parse(raw);
|
|
12
|
+
} catch (e) {
|
|
13
|
+
return {};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isFundUnitParamsReady(): boolean {
|
|
18
|
+
const params = readFundUnitParams();
|
|
19
|
+
// 判定“非空对象”
|
|
20
|
+
return params && typeof params === "object" && Object.keys(params).length > 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getFundUnitParams(): Record<string, any> {
|
|
24
|
+
return readFundUnitParams();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function startPollingIfNeeded(): void {
|
|
28
|
+
if (pollingTimer !== null) return;
|
|
29
|
+
// 轻量轮询,检测到就绪后立即 resolve 并停止
|
|
30
|
+
pollingTimer = window.setInterval(() => {
|
|
31
|
+
if (isFundUnitParamsReady()) {
|
|
32
|
+
if (pollingTimer !== null) {
|
|
33
|
+
window.clearInterval(pollingTimer);
|
|
34
|
+
pollingTimer = null;
|
|
35
|
+
}
|
|
36
|
+
const resolvers = pendingResolvers.slice();
|
|
37
|
+
pendingResolvers = [];
|
|
38
|
+
resolvers.forEach((resolve) => resolve());
|
|
39
|
+
}
|
|
40
|
+
}, 100);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 对外暴露:等待 fund_unit_params 就绪
|
|
44
|
+
export function waitFundUnitReady(): Promise<void> {
|
|
45
|
+
if (isFundUnitParamsReady()) return Promise.resolve();
|
|
46
|
+
startPollingIfNeeded();
|
|
47
|
+
return new Promise<void>((resolve) => {
|
|
48
|
+
pendingResolvers.push(resolve);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 可选:手动通知(如果你在别处设置了 fund_unit_params 后希望立刻触发)
|
|
53
|
+
export function notifyFundUnitParamsUpdated(): void {
|
|
54
|
+
if (!isFundUnitParamsReady()) return;
|
|
55
|
+
const resolvers = pendingResolvers.slice();
|
|
56
|
+
pendingResolvers = [];
|
|
57
|
+
resolvers.forEach((resolve) => resolve());
|
|
58
|
+
if (pollingTimer !== null) {
|
|
59
|
+
window.clearInterval(pollingTimer);
|
|
60
|
+
pollingTimer = null;
|
|
61
|
+
}
|
|
62
|
+
}
|