best-unit 0.0.45 → 0.0.47

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "best-unit",
3
3
  "private": false,
4
- "version": "0.0.45",
4
+ "version": "0.0.47",
5
5
  "type": "module",
6
6
  "main": "dist/best-unit.cjs",
7
7
  "module": "dist/best-unit.js",
@@ -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,24 @@ export function createAxiosInstance(options: CreateAxiosOptions = {}) {
19
20
  const instance: AxiosInstance = axios.create({ baseURL, timeout });
20
21
 
21
22
  // 请求拦截:加 token、国际化
22
- instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
23
- const fundUnitParams = JSON.parse(
24
- sessionStorage.getItem("fund_unit_params") || "{}"
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
- return config;
35
- });
28
+ const fundUnitParams = JSON.parse(
29
+ sessionStorage.getItem("fund_unit_params") || "{}"
30
+ );
31
+ const { token, locale } = fundUnitParams;
32
+ config.headers = {
33
+ ...config.headers,
34
+ Authorization: token,
35
+ "x-locale": locale === Locale.ZH ? "zh-CN" : "en-US",
36
+ } as any;
37
+
38
+ return config;
39
+ }
40
+ );
36
41
 
37
42
  // 响应拦截:code=0判定成功,其他走 onError
38
43
  instance.interceptors.response.use(
package/src/demo/App.tsx CHANGED
@@ -11,9 +11,21 @@ export default function DemoApp() {
11
11
  const [currentTheme, setCurrentTheme] = useState<Theme>(Theme.WHITE);
12
12
 
13
13
  const initApp = ({ locale, theme }: { locale?: Locale; theme?: Theme }) => {
14
+ // 测试代码:每次刷新页面时生成不同的 token
15
+ const generateTestToken = () => {
16
+ const timestamp = Date.now();
17
+ const randomId = Math.floor(Math.random() * 1000);
18
+ return `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTM0MTM1MjksIm1lcmNoYW50X2lkIjoxMTI4LCJ0aW1lc3RhbXAiOjE3NTMxNTQzMjl9.${timestamp}_${randomId}_TEST_TOKEN`;
19
+ };
20
+
21
+ const testToken = generateTestToken();
22
+ console.log("=== 测试 Token 生成 ===");
23
+ console.log("当前时间戳:", Date.now());
24
+ console.log("生成的测试 Token:", testToken);
25
+ console.log("========================");
26
+
14
27
  initFundUnit({
15
- token:
16
- "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTM0MTM1MjksIm1lcmNoYW50X2lkIjoxMTI4LCJ0aW1lc3RhbXAiOjE3NTMxNTQzMjl9.UAvzq0P4HCnbJR1Ga3CgF6q3vk2RHiZRvnAFohBTHpw",
28
+ token: testToken,
17
29
  merchant_id: "1128",
18
30
  biz_type: "ad",
19
31
  locale: locale as Locale,
@@ -54,9 +66,30 @@ export default function DemoApp() {
54
66
  borderRadius: isDark ? 8 : undefined,
55
67
  }}
56
68
  >
57
- 组件库可视化测试
69
+ 组件库可视化测试 (Token 测试版本)
58
70
  </h2>
59
71
 
72
+ {/* Token 测试信息显示 */}
73
+ <div
74
+ style={{
75
+ marginBottom: 20,
76
+ padding: 16,
77
+ border: "2px solid #1890ff",
78
+ borderRadius: 8,
79
+ backgroundColor: "#e6f7ff",
80
+ }}
81
+ >
82
+ <h3 style={{ marginTop: 0, marginBottom: 12, color: "#1890ff" }}>
83
+ 🧪 Token 测试信息:
84
+ </h3>
85
+ <p style={{ margin: 0, color: "#1890ff" }}>
86
+ 每次刷新页面都会生成新的测试 Token,请查看控制台输出。
87
+ </p>
88
+ <p style={{ margin: "8px 0 0 0", color: "#1890ff", fontSize: "12px" }}>
89
+ 当前时间戳: {Date.now()}
90
+ </p>
91
+ </div>
92
+
60
93
  {/* 国际化切换区域 */}
61
94
  <div
62
95
  style={{
@@ -1,7 +1,9 @@
1
- import { getAllDicts } from "../../api";
2
1
  import { Locale, Theme } from "../../types";
3
2
 
4
- export function initFundUnit(params: {
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,48 @@ export function initFundUnit(params: {
9
11
  theme?: Theme;
10
12
  locale?: Locale;
11
13
  }) {
12
- getAllDicts();
13
- const {
14
- merchant_id,
15
- biz_type,
16
- fund_balance_id,
17
- theme = Theme.WHITE,
18
- locale = Locale.ZH,
19
- } = params;
20
- const token = "Bearer " + params.token;
21
- sessionStorage.setItem(
22
- "fund_unit_params",
23
- JSON.stringify({
14
+ // 重置状态,允许重新初始化
15
+ isInitialized = false;
16
+ initPromise = null;
17
+
18
+ initPromise = new Promise(async (resolve) => {
19
+ const {
20
+ merchant_id,
21
+ biz_type,
22
+ fund_balance_id,
23
+ theme = Theme.WHITE,
24
+ locale = Locale.ZH,
25
+ } = params;
26
+ const token = "Bearer " + params.token;
27
+
28
+ const fundUnitParams = {
24
29
  merchantId: merchant_id,
25
30
  bizType: biz_type,
26
31
  fundBalanceId: fund_balance_id,
27
32
  token,
28
33
  theme,
29
34
  locale,
30
- })
31
- );
32
- return {
33
- token,
34
- merchantId: merchant_id,
35
- bizType: biz_type,
36
- fundBalanceId: fund_balance_id,
37
- theme,
38
- locale,
39
- };
35
+ };
36
+
37
+ sessionStorage.setItem("fund_unit_params", JSON.stringify(fundUnitParams));
38
+ isInitialized = true;
39
+
40
+ console.log("initFundUnit 更新完成:", fundUnitParams);
41
+ resolve(fundUnitParams);
42
+ });
43
+
44
+ return initPromise;
45
+ }
46
+
47
+ // 检查是否已初始化
48
+ export function isFundUnitInitialized(): boolean {
49
+ return isInitialized;
50
+ }
51
+
52
+ // 等待初始化完成
53
+ export function waitForInitialization(): Promise<any> {
54
+ if (isInitialized) {
55
+ return Promise.resolve();
56
+ }
57
+ return initPromise || Promise.resolve();
40
58
  }