@brushes/component-core 2.0.1 → 2.0.3

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@brushes/component-core",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "main": "src/index.ts",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -22,6 +22,7 @@
22
22
  "@brushes/context": "*",
23
23
  "@babel/standalone": "^7.23.7",
24
24
  "@brushes/form": "^3.0.4",
25
+ "@brushes/request": "^2.0.2",
25
26
  "@craftjs/core": "^0.2.12",
26
27
  "antd": "^5.24.5",
27
28
  "lodash": "^4.17.21",
@@ -46,7 +46,7 @@ export const CodeComponent = ({ api = '/web/ml/muser/sendPhone.json', codeKey, h
46
46
  if (encryption) {
47
47
  params = {
48
48
  ...params,
49
- userPhone: await encryptBySM2(params.userPhone)
49
+ userPhone: await encryptBySM2(codeKey ? rest[codeKey] : params.userPhone)
50
50
  };
51
51
  }
52
52
 
@@ -2,6 +2,7 @@ import { useEffect, useState } from 'react';
2
2
  import { Form, Spin } from 'antd';
3
3
  import { dynamicFormFields } from '@brushes/form';
4
4
  import { createStyles } from 'antd-style';
5
+ import { get, toImageDataUrl } from '@brushes/request';
5
6
  import { encryptBySM2 } from '../tool';
6
7
 
7
8
  const useStyle = createStyles(({ token, css }, { height }: { height: number }) => {
@@ -13,6 +14,7 @@ const useStyle = createStyles(({ token, css }, { height }: { height: number }) =
13
14
  `
14
15
  };
15
16
  });
17
+
16
18
  export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', codeKey, height = 40, encryption = true }: { api?: string; codeKey?: string; height?: number; encryption?: boolean }) => {
17
19
  const { styles } = useStyle({ height });
18
20
  const form = Form.useFormInstance();
@@ -27,7 +29,6 @@ export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', code
27
29
  }, [isDisabled]);
28
30
 
29
31
  const fetchCode = async () => {
30
- setLoading(true);
31
32
  const { getFieldsValue } = form;
32
33
  const { loginName, userPhone, userinfoConPhone, ...rest } = getFieldsValue();
33
34
 
@@ -35,25 +36,21 @@ export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', code
35
36
  if (codeKey) {
36
37
  params = rest[codeKey];
37
38
  }
39
+ if (!params) return;
38
40
 
39
- let userPhoneParam = params;
40
- if (encryption) {
41
- userPhoneParam = encodeURIComponent(await encryptBySM2(params));
41
+ setLoading(true);
42
+ try {
43
+ // 加密后的手机号原样交给 request 编码;这里不能再 encodeURIComponent,否则会二次编码
44
+ const userPhoneParam = encryption ? await encryptBySM2(params) : params;
45
+ // 按 @brushes/request 文档:get(url, params),二进制用第三个参数 responseType
46
+ const res = await get(api, { userPhone: userPhoneParam, t: Date.now() }, { responseType: 'arraybuffer' });
47
+ setVerImg(toImageDataUrl(res));
48
+ } catch (error) {
49
+ // 图形验证码拿不到时保留占位,不弹全局错误,不打断用户填表
50
+ setVerImg('');
51
+ } finally {
52
+ setLoading(false);
42
53
  }
43
-
44
- fetch(`${api}?userPhone=${userPhoneParam}`)
45
- .then((code) => {
46
- return code;
47
- })
48
- .then((res) => res.arrayBuffer())
49
- .then((res) => {
50
- const imgUrl = 'data:image/png;base64,' + window.btoa(String.fromCharCode(...new Uint8Array(res)));
51
- setVerImg(imgUrl);
52
- })
53
- .catch((error: any) => {})
54
- .finally(() => {
55
- setLoading(false);
56
- });
57
54
  };
58
55
  return (
59
56
  <div className={styles.wrap}>
@@ -69,7 +66,7 @@ export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', code
69
66
  autoComplete: 'off',
70
67
  placeholder: '请输入',
71
68
  addonAfter: (
72
- <Spin spinning={loading}>{verImg ? <img src={verImg} onClick={fetchCode} style={{ width: 90, height: height - 2 }} /> : <div style={{ width: 90, height: height - 2 }}></div>}</Spin>
69
+ <Spin spinning={loading}>{verImg ? <img src={verImg} onClick={fetchCode} style={{ width: 90, height: height - 2, cursor: 'pointer' }} /> : <div style={{ width: 90, height: height - 2 }}></div>}</Spin>
73
70
  ),
74
71
  style: {
75
72
  height