best-unit 0.0.45 → 0.0.46
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 +27 -27
- package/dist/best-unit.js +1423 -1425
- package/package.json +1 -1
- package/src/api/axiosInstance.ts +19 -13
- package/src/utils/business/index.ts +43 -24
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 { Locale } from "../types";
|
|
9
|
+
import { waitForInitialization } from "../utils/business";
|
|
9
10
|
|
|
10
11
|
export interface CreateAxiosOptions {
|
|
11
12
|
baseURL?: string;
|
|
@@ -19,20 +20,25 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
|
19
20
|
const instance: AxiosInstance = axios.create({ baseURL, timeout });
|
|
20
21
|
|
|
21
22
|
// 请求拦截:加 token、国际化
|
|
22
|
-
instance.interceptors.request.use(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const { token, locale } = fundUnitParams;
|
|
27
|
-
console.log(fundUnitParams, "fundUnitParams");
|
|
28
|
-
config.headers = {
|
|
29
|
-
...config.headers,
|
|
30
|
-
Authorization: token,
|
|
31
|
-
"x-locale": locale === Locale.ZH ? "zh-CN" : "en-US",
|
|
32
|
-
} as any;
|
|
23
|
+
instance.interceptors.request.use(
|
|
24
|
+
async (config: InternalAxiosRequestConfig) => {
|
|
25
|
+
// 等待初始化完成
|
|
26
|
+
await waitForInitialization();
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
const fundUnitParams = JSON.parse(
|
|
29
|
+
sessionStorage.getItem("fund_unit_params") || "{}"
|
|
30
|
+
);
|
|
31
|
+
const { token, locale } = fundUnitParams;
|
|
32
|
+
console.log(fundUnitParams, "fundUnitParams");
|
|
33
|
+
config.headers = {
|
|
34
|
+
...config.headers,
|
|
35
|
+
Authorization: token,
|
|
36
|
+
"x-locale": locale === Locale.ZH ? "zh-CN" : "en-US",
|
|
37
|
+
} as any;
|
|
38
|
+
|
|
39
|
+
return config;
|
|
40
|
+
}
|
|
41
|
+
);
|
|
36
42
|
|
|
37
43
|
// 响应拦截:code=0判定成功,其他走 onError
|
|
38
44
|
instance.interceptors.response.use(
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { getAllDicts } from "../../api";
|
|
2
1
|
import { Locale, Theme } from "../../types";
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
let isInitialized = false;
|
|
4
|
+
let initPromise: Promise<any> | null = null;
|
|
5
|
+
|
|
6
|
+
export async function initFundUnit(params: {
|
|
5
7
|
token: string;
|
|
6
8
|
merchant_id?: string;
|
|
7
9
|
biz_type?: string;
|
|
@@ -9,32 +11,49 @@ export function initFundUnit(params: {
|
|
|
9
11
|
theme?: Theme;
|
|
10
12
|
locale?: Locale;
|
|
11
13
|
}) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
// 如果已经在初始化中,等待完成
|
|
15
|
+
if (initPromise) {
|
|
16
|
+
return initPromise;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
initPromise = new Promise(async (resolve) => {
|
|
20
|
+
const {
|
|
21
|
+
merchant_id,
|
|
22
|
+
biz_type,
|
|
23
|
+
fund_balance_id,
|
|
24
|
+
theme = Theme.WHITE,
|
|
25
|
+
locale = Locale.ZH,
|
|
26
|
+
} = params;
|
|
27
|
+
const token = "Bearer " + params.token;
|
|
28
|
+
|
|
29
|
+
const fundUnitParams = {
|
|
24
30
|
merchantId: merchant_id,
|
|
25
31
|
bizType: biz_type,
|
|
26
32
|
fundBalanceId: fund_balance_id,
|
|
27
33
|
token,
|
|
28
34
|
theme,
|
|
29
35
|
locale,
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
sessionStorage.setItem("fund_unit_params", JSON.stringify(fundUnitParams));
|
|
39
|
+
isInitialized = true;
|
|
40
|
+
|
|
41
|
+
console.log("initFundUnit 更新完成:", fundUnitParams);
|
|
42
|
+
resolve(fundUnitParams);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return initPromise;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 检查是否已初始化
|
|
49
|
+
export function isFundUnitInitialized(): boolean {
|
|
50
|
+
return isInitialized;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 等待初始化完成
|
|
54
|
+
export function waitForInitialization(): Promise<any> {
|
|
55
|
+
if (isInitialized) {
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
}
|
|
58
|
+
return initPromise || Promise.resolve();
|
|
40
59
|
}
|