best-unit 2.1.5 → 2.1.11

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.
@@ -37,6 +37,11 @@ export interface BalanceData {
37
37
  pendingAmount: string;
38
38
  status: string;
39
39
  createdAt: string;
40
+ merchantBalances: Array<{
41
+ fundBalanceId: string;
42
+ merchantId: number;
43
+ bizType: string;
44
+ }>;
40
45
  }
41
46
 
42
47
  export interface InitParams {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "best-unit",
3
3
  "private": false,
4
- "version": "2.1.5",
4
+ "version": "2.1.11",
5
5
  "type": "module",
6
6
  "main": "dist/best-unit.cjs",
7
7
  "module": "dist/best-unit.js",
package/src/api/index.ts CHANGED
@@ -7,51 +7,19 @@ export interface MerchantBalanceItem {
7
7
  bizType: string;
8
8
  }
9
9
 
10
- export const getAllBalances = async () => {
11
- const fundUnitParams = JSON.parse(
12
- sessionStorage.getItem("fund_unit_params") || "{}",
13
- );
14
- return http()
15
- .post("/merchant/all-balances", {
16
- merchant_id: Number(fundUnitParams.merchantId),
17
- })
18
- .then((res) => {
19
- const allBalances: MerchantBalanceItem[] = (
20
- Array.isArray(res?.data) ? res.data : []
21
- ).map((item: any) => ({
22
- fundBalanceId: String(item.fund_balance_id || ""),
23
- merchantId: Number(item.merchant_id || 0),
24
- bizType: String(item.biz_type || ""),
25
- }));
26
-
27
- sessionStorage.setItem("allBalances", JSON.stringify(allBalances));
28
- return allBalances;
29
- });
30
- };
31
-
32
10
  // 获取余额
33
11
  export async function getBalance() {
34
12
  const fundUnitParams = JSON.parse(
35
13
  sessionStorage.getItem("fund_unit_params") || "{}",
36
14
  );
37
-
38
- const balanceRequest = http().get("/balance", {
15
+ const res = await http().get("/balance", {
39
16
  params: {
40
17
  merchant_id: fundUnitParams.merchantId,
41
18
  biz_type: fundUnitParams.bizType,
42
19
  fund_balance_id: fundUnitParams.fundBalanceId,
43
20
  },
44
21
  });
45
- const [balanceRes, allBalancesRes] = await Promise.allSettled([
46
- balanceRequest,
47
- getAllBalances(),
48
- ]);
49
-
50
- if (balanceRes.status !== "fulfilled") {
51
- throw balanceRes.reason;
52
- }
53
-
54
- const data = balanceRes.value.data;
22
+ const data = res.data;
55
23
  const balanceData = {
56
24
  fundBalanceId: data.fund_balance_id,
57
25
  merchantId: data.merchant_id,
@@ -67,12 +35,16 @@ export async function getBalance() {
67
35
  pendingAmount: data.pending_amount,
68
36
  status: data.status,
69
37
  createdAt: data.created_at,
38
+ merchantBalances: (Array.isArray(data.merchant_balances)
39
+ ? data.merchant_balances
40
+ : []
41
+ ).map((item: any) => ({
42
+ fundBalanceId: item.fund_balance_id,
43
+ merchantId: item.merchant_id,
44
+ bizType: item.biz_type,
45
+ })),
70
46
  };
71
47
 
72
- if (allBalancesRes.status !== "fulfilled") {
73
- console.error("获取子业务账户列表失败:", allBalancesRes.reason);
74
- }
75
-
76
48
  sessionStorage.setItem("balanceData", JSON.stringify(balanceData));
77
49
  return balanceData;
78
50
  }
@@ -156,6 +128,7 @@ export const createOfflineRecharge = async (data: any) => {
156
128
  allocation_ratios: Boolean(data.useFundAllocation)
157
129
  ? (data.allocationRatios || []).map((item: any) => ({
158
130
  fund_balance_id: item.fundBalanceId,
131
+ biz_type: item.bizType,
159
132
  ratio: String(item.ratio),
160
133
  }))
161
134
  : [],
@@ -1,6 +1,7 @@
1
1
  import { useEffect, useState } from "preact/hooks";
2
2
  import { t } from "@/local";
3
- import { getAllBalances, type MerchantBalanceItem } from "@/api";
3
+ import { type MerchantBalanceItem } from "@/api";
4
+ import { getBalanceData } from "@/utils/business";
4
5
 
5
6
  interface FundAllocationSectionProps {
6
7
  theme: any;
@@ -19,6 +20,25 @@ function getBizTypeLabel(bizType: string) {
19
20
  return bizType;
20
21
  }
21
22
 
23
+ function getBizTypePlaceholder(bizType: string) {
24
+ const prefix = t("请输入");
25
+ const label = getBizTypeLabel(bizType);
26
+ return /[A-Za-z]$/.test(prefix) ? `${prefix} ${label}` : `${prefix}${label}`;
27
+ }
28
+
29
+ function formatPercentageInput(value: string) {
30
+ let nextValue = value.replace(/[^\d.]/g, "");
31
+ nextValue = nextValue.replace(/\.(?=.*\.)/g, "");
32
+ nextValue = nextValue.replace(/^(\d+)(\.\d{0,2})?.*$/, "$1$2");
33
+ nextValue = nextValue.replace(/^0+(\d)/, "$1");
34
+
35
+ if (nextValue.startsWith(".")) {
36
+ nextValue = `0${nextValue}`;
37
+ }
38
+
39
+ return nextValue;
40
+ }
41
+
22
42
  export function useFundAllocation() {
23
43
  const [subBusinesses, setSubBusinesses] = useState<MerchantBalanceItem[]>([]);
24
44
  const [useFundAllocation, setUseFundAllocation] = useState(false);
@@ -45,17 +65,24 @@ export function useFundAllocation() {
45
65
  );
46
66
  };
47
67
 
48
- const cached = JSON.parse(sessionStorage.getItem("allBalances") || "[]");
49
- if (Array.isArray(cached) && cached.length > 0) {
50
- applySubBusinesses(cached);
68
+ const cached = JSON.parse(sessionStorage.getItem("balanceData") || "{}");
69
+ const cachedMerchantBalances = Array.isArray(cached?.merchantBalances)
70
+ ? cached.merchantBalances
71
+ : [];
72
+ if (cachedMerchantBalances.length > 0) {
73
+ applySubBusinesses(cachedMerchantBalances);
51
74
  }
52
75
 
53
- getAllBalances()
54
- .then((items) => {
55
- applySubBusinesses(items);
76
+ getBalanceData()
77
+ .then((balanceData: any) => {
78
+ applySubBusinesses(
79
+ Array.isArray(balanceData?.merchantBalances)
80
+ ? balanceData.merchantBalances
81
+ : [],
82
+ );
56
83
  })
57
84
  .catch(() => {
58
- if (!Array.isArray(cached)) {
85
+ if (cachedMerchantBalances.length === 0) {
59
86
  applySubBusinesses([]);
60
87
  }
61
88
  });
@@ -123,6 +150,7 @@ export function useFundAllocation() {
123
150
 
124
151
  return subBusinesses.map((item) => ({
125
152
  fundBalanceId: item.fundBalanceId,
153
+ bizType: item.bizType,
126
154
  ratio: String(subBizPercentages[item.bizType] || ""),
127
155
  }));
128
156
  };
@@ -191,16 +219,16 @@ export function FundAllocationSection({
191
219
  {" (%)"}
192
220
  </label>
193
221
  <input
194
- type="number"
195
- min="0"
196
- max="100"
197
- step="0.01"
198
- placeholder="0"
222
+ type="text"
223
+ inputMode="decimal"
224
+ placeholder={getBizTypePlaceholder(item.bizType)}
199
225
  value={subBizPercentages[item.bizType] || ""}
200
226
  onChange={(e) => {
201
227
  onPercentageChange(
202
228
  item.bizType,
203
- (e.target as HTMLInputElement).value,
229
+ formatPercentageInput(
230
+ (e.target as HTMLInputElement).value,
231
+ ),
204
232
  );
205
233
  }}
206
234
  style={{
package/src/demo/App.tsx CHANGED
@@ -39,13 +39,13 @@ export default function DemoApp() {
39
39
  initFundUnit({
40
40
  token:
41
41
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjaGFudF9pZCI6MTAwMiwidGltZXN0YW1wIjoxNzUzNDMwNjkyLCJleHAiOjE3NTM0MzQyOTJ9.OPYKUxLQ3C36rKiAmCnUwxTkvM9eKgtsnoxrB9Ntrag",
42
- fund_balance_id: "FB1FNZ5Q55M7QP2C",
42
+ // fund_balance_id: "FB1FNZ5Q55M7QP2C",
43
43
  user_id: "19b8a77c-d3fc-45da-9520-4a07463123df",
44
44
  locale: locale as Locale,
45
45
  theme: theme ?? Theme.WHITE,
46
46
  env: env ?? currentEnv,
47
47
  size: Size.SMALL,
48
- biz_type: "fulfill",
48
+ biz_type: "ad",
49
49
  merchant_id: "1128",
50
50
  // theme_config: {
51
51
  // white: {
package/src/local/en.ts CHANGED
@@ -131,6 +131,7 @@ export const en: Record<string, string> = {
131
131
  提交审核: "Submit for Review",
132
132
  请选择转账时间: "Please select transfer time",
133
133
  请输入转账金额: "Please enter transfer amount",
134
+ 请输入: "Please enter",
134
135
  是否使用资金分配: "Use fund allocation",
135
136
  是: "Yes",
136
137
  否: "No",
package/src/local/zh.ts CHANGED
@@ -94,6 +94,7 @@ export const zh: Record<string, string> = {
94
94
  提交审核: "提交审核",
95
95
  请选择转账时间: "请选择转账时间",
96
96
  请输入转账金额: "请输入转账金额",
97
+ 请输入: "请输入",
97
98
  是否使用资金分配: "是否使用资金分配",
98
99
  是: "是",
99
100
  否: "否",
@@ -37,6 +37,11 @@ export interface BalanceData {
37
37
  pendingAmount: string;
38
38
  status: string;
39
39
  createdAt: string;
40
+ merchantBalances: Array<{
41
+ fundBalanceId: string;
42
+ merchantId: number;
43
+ bizType: string;
44
+ }>;
40
45
  }
41
46
 
42
47
  export interface InitParams {
@@ -1,6 +1,7 @@
1
1
  import { getAllDicts, getBalance } from "@/api";
2
2
  import { resetHttpInstance } from "@/api/axiosInstance";
3
3
  import { Locale, Size, Theme, type Env } from "@/types";
4
+ import type { MerchantBalanceItem } from "@/api";
4
5
 
5
6
  // 余额数据结构接口
6
7
  export interface BalanceData {
@@ -17,6 +18,7 @@ export interface BalanceData {
17
18
  pendingAmount: string;
18
19
  status: string;
19
20
  createdAt: string;
21
+ merchantBalances: MerchantBalanceItem[];
20
22
  }
21
23
 
22
24
  interface ThemeConfig {