best-unit 1.2.11 → 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 +8 -8
- package/dist/best-unit.js +712 -733
- package/package.json +1 -1
- package/src/api/axiosInstance.ts +29 -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,54 +15,41 @@ 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
|
-
//
|
|
25
|
+
// 请求拦截:等待 fund_unit_params 就绪,动态设置 baseURL、token、国际化
|
|
46
26
|
instance.interceptors.request.use(
|
|
47
27
|
async (config: InternalAxiosRequestConfig) => {
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
const obj = JSON.parse(
|
|
51
|
-
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
52
|
-
);
|
|
53
|
-
if (!obj || Object.keys(obj).length === 0) {
|
|
54
|
-
await waitUntilParamsReady();
|
|
55
|
-
}
|
|
56
|
-
} catch (_) {
|
|
57
|
-
await waitUntilParamsReady();
|
|
58
|
-
}
|
|
28
|
+
// 等待 fund_unit_params 非空
|
|
29
|
+
await waitFundUnitReady();
|
|
59
30
|
|
|
60
|
-
|
|
61
|
-
const latestParams = JSON.parse(
|
|
31
|
+
const fundUnitParams = JSON.parse(
|
|
62
32
|
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
63
33
|
);
|
|
64
|
-
|
|
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
|
+
|
|
65
53
|
config.headers = {
|
|
66
54
|
...config.headers,
|
|
67
55
|
Authorization: token,
|
|
@@ -111,63 +99,13 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
111
99
|
|
|
112
100
|
// 缓存 axios 实例
|
|
113
101
|
let httpInstance: AxiosInstance | null = null;
|
|
114
|
-
let creatingPromise: Promise<void> | null = null;
|
|
115
|
-
|
|
116
|
-
// 等待 fund_unit_params 不为空对象(无超时)
|
|
117
|
-
function waitUntilParamsReady(): Promise<void> {
|
|
118
|
-
return new Promise((resolve) => {
|
|
119
|
-
const check = () => {
|
|
120
|
-
try {
|
|
121
|
-
const obj = JSON.parse(
|
|
122
|
-
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
123
|
-
);
|
|
124
|
-
if (obj && Object.keys(obj).length > 0) return resolve();
|
|
125
|
-
} catch (_) {
|
|
126
|
-
// ignore
|
|
127
|
-
}
|
|
128
|
-
setTimeout(check, 50);
|
|
129
|
-
};
|
|
130
|
-
check();
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
102
|
|
|
134
103
|
// 获取 axios 实例的函数
|
|
135
104
|
export function http(): AxiosInstance {
|
|
136
|
-
if (httpInstance)
|
|
137
|
-
|
|
138
|
-
async function ensureInstanceReady(): Promise<void> {
|
|
139
|
-
if (httpInstance) return;
|
|
140
|
-
if (!creatingPromise) {
|
|
141
|
-
creatingPromise = waitUntilParamsReady()
|
|
142
|
-
.then(() => {
|
|
143
|
-
httpInstance = createAxiosInstance();
|
|
144
|
-
})
|
|
145
|
-
.finally(() => {
|
|
146
|
-
creatingPromise = null;
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
return creatingPromise;
|
|
105
|
+
if (!httpInstance) {
|
|
106
|
+
httpInstance = createAxiosInstance();
|
|
150
107
|
}
|
|
151
|
-
|
|
152
|
-
const proxy: any = {};
|
|
153
|
-
[
|
|
154
|
-
"request",
|
|
155
|
-
"get",
|
|
156
|
-
"delete",
|
|
157
|
-
"head",
|
|
158
|
-
"options",
|
|
159
|
-
"post",
|
|
160
|
-
"put",
|
|
161
|
-
"patch",
|
|
162
|
-
].forEach((method) => {
|
|
163
|
-
proxy[method] = async (...args: any[]) => {
|
|
164
|
-
await ensureInstanceReady();
|
|
165
|
-
// @ts-ignore
|
|
166
|
-
return (httpInstance as any)[method](...args);
|
|
167
|
-
};
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
return proxy as AxiosInstance;
|
|
108
|
+
return httpInstance;
|
|
171
109
|
}
|
|
172
110
|
|
|
173
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
|
+
}
|