best-unit 0.0.11 → 0.0.13
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 +6 -1
- package/dist/best-unit.js +2340 -560
- package/package.json +2 -1
- package/src/api/axiosInstance.ts +72 -0
- package/src/api/index.ts +16 -0
- package/src/api/proxy.ts +9 -0
- package/src/components/ModalForm/components/Button.tsx +19 -2
- package/src/components/ModalForm/components/OfflineTransferForm.tsx +12 -2
- package/src/components/ModalForm/components/OnlineRechargeForm.tsx +12 -2
- package/src/components/ModalForm/components/recharge.tsx +3 -0
- package/src/components/ModalForm/index.tsx +13 -1
- package/src/demo/App.tsx +12 -5
- package/vite.config.ts +14 -10
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "best-unit",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.13",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/best-unit.cjs",
|
|
7
7
|
"module": "dist/best-unit.js",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"preview": "vite preview"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
+
"axios": "^1.10.0",
|
|
16
17
|
"preact": "^10.26.9",
|
|
17
18
|
"preact-custom-element": "^4.3.0"
|
|
18
19
|
},
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import type {
|
|
3
|
+
AxiosInstance,
|
|
4
|
+
AxiosResponse,
|
|
5
|
+
InternalAxiosRequestConfig,
|
|
6
|
+
} from "axios";
|
|
7
|
+
|
|
8
|
+
export interface CreateAxiosOptions {
|
|
9
|
+
baseURL?: string;
|
|
10
|
+
timeout?: number;
|
|
11
|
+
getToken?: () => string | null;
|
|
12
|
+
getLocale?: () => string | null;
|
|
13
|
+
onError?: (msg: string, error: any) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createAxiosInstance(options: CreateAxiosOptions = {}) {
|
|
17
|
+
const {
|
|
18
|
+
baseURL = "/api",
|
|
19
|
+
timeout = 10000,
|
|
20
|
+
getToken,
|
|
21
|
+
getLocale,
|
|
22
|
+
onError,
|
|
23
|
+
} = options;
|
|
24
|
+
|
|
25
|
+
const instance: AxiosInstance = axios.create({ baseURL, timeout });
|
|
26
|
+
|
|
27
|
+
// 请求拦截:加 token、国际化
|
|
28
|
+
instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
29
|
+
if (getToken) {
|
|
30
|
+
const token = getToken();
|
|
31
|
+
if (token)
|
|
32
|
+
config.headers = { ...config.headers, Authorization: token } as any;
|
|
33
|
+
}
|
|
34
|
+
if (getLocale) {
|
|
35
|
+
const locale = getLocale();
|
|
36
|
+
if (locale)
|
|
37
|
+
config.headers = {
|
|
38
|
+
...config.headers,
|
|
39
|
+
"Accept-Language": locale,
|
|
40
|
+
} as any;
|
|
41
|
+
}
|
|
42
|
+
return config;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// 响应拦截:code=0判定成功,其他走 onError
|
|
46
|
+
instance.interceptors.response.use(
|
|
47
|
+
(response: AxiosResponse) => {
|
|
48
|
+
if (response.data && response.data.code === 0) {
|
|
49
|
+
return response.data;
|
|
50
|
+
}
|
|
51
|
+
if (onError) {
|
|
52
|
+
onError(
|
|
53
|
+
response.data?.msg || response.data?.message || "未知错误",
|
|
54
|
+
response
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return Promise.reject(response.data || { message: "未知错误" });
|
|
58
|
+
},
|
|
59
|
+
(error) => {
|
|
60
|
+
if (onError) {
|
|
61
|
+
onError(error.message, error);
|
|
62
|
+
}
|
|
63
|
+
return Promise.reject(error);
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return instance;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 默认实例
|
|
71
|
+
const http = createAxiosInstance();
|
|
72
|
+
export default http;
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import axiosInstance from "./axiosInstance";
|
|
2
|
+
|
|
3
|
+
export function getBalance(params: {
|
|
4
|
+
merchant_id: string;
|
|
5
|
+
biz_type: string;
|
|
6
|
+
token?: string;
|
|
7
|
+
}) {
|
|
8
|
+
const { token, ...rest } = params;
|
|
9
|
+
return axiosInstance.get("/balance", {
|
|
10
|
+
params: rest,
|
|
11
|
+
headers: token ? { Authorization: token } : undefined,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 示例用法:
|
|
16
|
+
// getBalance({ merchant_id: '1128', biz_type: 'ad', token: 'xxx' }).then(res => console.log(res));
|
package/src/api/proxy.ts
ADDED
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
import type { ComponentChildren } from "preact";
|
|
2
|
+
import { getBalance } from "../../../api";
|
|
2
3
|
|
|
3
4
|
interface ButtonProps {
|
|
4
|
-
onClick
|
|
5
|
+
onClick?: () => void;
|
|
5
6
|
color?: string;
|
|
6
7
|
children: ComponentChildren;
|
|
8
|
+
merchantId?: string;
|
|
9
|
+
bizType?: string;
|
|
10
|
+
token?: string;
|
|
7
11
|
}
|
|
8
12
|
|
|
9
|
-
export function ThemedButton({
|
|
13
|
+
export function ThemedButton({
|
|
14
|
+
onClick,
|
|
15
|
+
color,
|
|
16
|
+
children,
|
|
17
|
+
merchantId,
|
|
18
|
+
bizType,
|
|
19
|
+
token,
|
|
20
|
+
}: ButtonProps) {
|
|
21
|
+
// 组件加载时直接调用
|
|
22
|
+
if (merchantId && bizType) {
|
|
23
|
+
getBalance({ merchant_id: merchantId, biz_type: bizType, token }).then(
|
|
24
|
+
(res) => console.log(res)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
10
27
|
return (
|
|
11
28
|
<button
|
|
12
29
|
style={{
|
|
@@ -118,7 +118,12 @@ export const OfflineTransferForm: FunctionalComponent<
|
|
|
118
118
|
marginLeft: 16,
|
|
119
119
|
cursor: "pointer",
|
|
120
120
|
},
|
|
121
|
-
error: {
|
|
121
|
+
error: {
|
|
122
|
+
color: "#ff4d4f",
|
|
123
|
+
fontSize: 13,
|
|
124
|
+
marginTop: 4,
|
|
125
|
+
textAlign: "left",
|
|
126
|
+
},
|
|
122
127
|
}
|
|
123
128
|
: {
|
|
124
129
|
label: {
|
|
@@ -214,7 +219,12 @@ export const OfflineTransferForm: FunctionalComponent<
|
|
|
214
219
|
marginLeft: 16,
|
|
215
220
|
cursor: "pointer",
|
|
216
221
|
},
|
|
217
|
-
error: {
|
|
222
|
+
error: {
|
|
223
|
+
color: "#ff4d4f",
|
|
224
|
+
fontSize: 13,
|
|
225
|
+
marginTop: 4,
|
|
226
|
+
textAlign: "left",
|
|
227
|
+
},
|
|
218
228
|
};
|
|
219
229
|
|
|
220
230
|
const handleFileChange = (e: any) => {
|
|
@@ -57,7 +57,12 @@ export const OnlineRechargeForm: FunctionalComponent<
|
|
|
57
57
|
selectError: {
|
|
58
58
|
border: "1px solid #ff4d4f",
|
|
59
59
|
},
|
|
60
|
-
error: {
|
|
60
|
+
error: {
|
|
61
|
+
color: "#ff4d4f",
|
|
62
|
+
fontSize: 13,
|
|
63
|
+
marginTop: 4,
|
|
64
|
+
textAlign: "left",
|
|
65
|
+
},
|
|
61
66
|
buttonCancel: {
|
|
62
67
|
background: "#fff",
|
|
63
68
|
color: "#222",
|
|
@@ -114,7 +119,12 @@ export const OnlineRechargeForm: FunctionalComponent<
|
|
|
114
119
|
selectError: {
|
|
115
120
|
border: "1px solid #ff4d4f",
|
|
116
121
|
},
|
|
117
|
-
error: {
|
|
122
|
+
error: {
|
|
123
|
+
color: "#ff4d4f",
|
|
124
|
+
fontSize: 13,
|
|
125
|
+
marginTop: 4,
|
|
126
|
+
textAlign: "left",
|
|
127
|
+
},
|
|
118
128
|
buttonCancel: {
|
|
119
129
|
background: "#23262F",
|
|
120
130
|
color: "#fff",
|
|
@@ -7,6 +7,9 @@ export function BestUnit(props: any) {
|
|
|
7
7
|
const [visible, setVisible] = useState(false);
|
|
8
8
|
const [whiteTheme, setWhiteTheme] = useState(true);
|
|
9
9
|
const color = props.theme?.primaryColor;
|
|
10
|
+
const merchantId = props.merchant_id;
|
|
11
|
+
const bizType = props.biz_type;
|
|
12
|
+
const token = props.token;
|
|
10
13
|
|
|
11
14
|
const handleSubmit = async (form: {
|
|
12
15
|
amount: string;
|
|
@@ -18,7 +21,13 @@ export function BestUnit(props: any) {
|
|
|
18
21
|
|
|
19
22
|
return (
|
|
20
23
|
<div>
|
|
21
|
-
<ThemedButton
|
|
24
|
+
<ThemedButton
|
|
25
|
+
color={color}
|
|
26
|
+
onClick={() => setVisible(true)}
|
|
27
|
+
merchantId={merchantId}
|
|
28
|
+
bizType={bizType}
|
|
29
|
+
token={token}
|
|
30
|
+
>
|
|
22
31
|
打开表单
|
|
23
32
|
</ThemedButton>
|
|
24
33
|
<button
|
|
@@ -39,6 +48,9 @@ export function BestUnit(props: any) {
|
|
|
39
48
|
onSubmit={handleSubmit}
|
|
40
49
|
color={color}
|
|
41
50
|
whiteTheme={whiteTheme}
|
|
51
|
+
merchantId={merchantId}
|
|
52
|
+
bizType={bizType}
|
|
53
|
+
token={token}
|
|
42
54
|
/>
|
|
43
55
|
</div>
|
|
44
56
|
);
|
package/src/demo/App.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { npmTest, printCurrentTime } from
|
|
2
|
-
import { BestUnit } from
|
|
1
|
+
import { npmTest, printCurrentTime } from "../main";
|
|
2
|
+
import { BestUnit } from "../components/ModalForm";
|
|
3
3
|
|
|
4
4
|
export default function DemoApp() {
|
|
5
5
|
return (
|
|
@@ -7,13 +7,20 @@ export default function DemoApp() {
|
|
|
7
7
|
<h2>组件库可视化测试</h2>
|
|
8
8
|
<div>
|
|
9
9
|
<h3>BestUnit 组件演示:</h3>
|
|
10
|
-
<BestUnit
|
|
10
|
+
<BestUnit
|
|
11
|
+
theme={{ primaryColor: "#6366f1" }}
|
|
12
|
+
merchant_id="1128"
|
|
13
|
+
biz_type="ad"
|
|
14
|
+
token="123"
|
|
15
|
+
/>
|
|
11
16
|
</div>
|
|
12
17
|
<div>
|
|
13
18
|
<h3>方法演示:</h3>
|
|
14
|
-
<button onClick={npmTest} style={{ marginRight: 10 }}
|
|
19
|
+
<button onClick={npmTest} style={{ marginRight: 10 }}>
|
|
20
|
+
调用 npmTest()
|
|
21
|
+
</button>
|
|
15
22
|
<button onClick={printCurrentTime}>调用 printCurrentTime()</button>
|
|
16
23
|
</div>
|
|
17
24
|
</div>
|
|
18
25
|
);
|
|
19
|
-
}
|
|
26
|
+
}
|
package/vite.config.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { defineConfig } from
|
|
2
|
-
import preact from
|
|
1
|
+
import { defineConfig } from "vite";
|
|
2
|
+
import preact from "@preact/preset-vite";
|
|
3
|
+
import proxy from "./src/api/proxy";
|
|
3
4
|
|
|
4
5
|
// https://vite.dev/config/
|
|
5
6
|
export default defineConfig({
|
|
6
7
|
plugins: [preact()],
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
server: {
|
|
9
|
+
proxy,
|
|
10
|
+
},
|
|
11
|
+
build: {
|
|
12
|
+
outDir: "dist", //自定义构建输出目录target:'es2020'
|
|
13
|
+
lib: {
|
|
14
|
+
entry: "src/main.ts", //入口文件路径
|
|
15
|
+
formats: ["es", "cjs"],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|