@brushes/component-core 1.1.2 → 1.1.4

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": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "main": "src/index.ts",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -26,6 +26,7 @@
26
26
  "antd": "^5.24.5",
27
27
  "lodash": "^4.17.21",
28
28
  "react-error-boundary": "^4.0.11",
29
+ "sm-crypto": "^0.3.13",
29
30
  "zustand": "^5.0.3"
30
31
  }
31
32
  }
@@ -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 { encryptBySM2 } from '../tool';
5
6
 
6
7
  const useStyle = createStyles(({ token, css }, { height }: { height: number }) => {
7
8
  return {
@@ -12,7 +13,7 @@ const useStyle = createStyles(({ token, css }, { height }: { height: number }) =
12
13
  `
13
14
  };
14
15
  });
15
- export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', codeKey, height = 40 }: { api?: string; codeKey?: string; height?: number }) => {
16
+ export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', codeKey, height = 40, encryption = false }: { api?: string; codeKey?: string; height?: number; encryption?: boolean }) => {
16
17
  const { styles } = useStyle({ height });
17
18
  const form = Form.useFormInstance();
18
19
  const isDisabled = form.getFieldValue('isDisabled');
@@ -25,7 +26,7 @@ export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', code
25
26
  }
26
27
  }, [isDisabled]);
27
28
 
28
- const fetchCode = () => {
29
+ const fetchCode = async () => {
29
30
  setLoading(true);
30
31
  const { getFieldsValue } = form;
31
32
  const { loginName, userPhone, userinfoConPhone, ...rest } = getFieldsValue();
@@ -35,7 +36,12 @@ export const ImageCodeComponent = ({ api = '/web/ml/mlogin/getVerCode.img', code
35
36
  params = rest[codeKey];
36
37
  }
37
38
 
38
- fetch(`${api}?userPhone=${params}`)
39
+ let userPhoneParam = params;
40
+ if (encryption) {
41
+ userPhoneParam = encodeURIComponent(await encryptBySM2(params));
42
+ }
43
+
44
+ fetch(`${api}?userPhone=${userPhoneParam}`)
39
45
  .then((code) => {
40
46
  return code;
41
47
  })
@@ -1,2 +1,3 @@
1
1
  export * from './combine';
2
2
  export * from './fullpath';
3
+ export * from './sm2Crypto';
@@ -0,0 +1,41 @@
1
+ import { sm2 } from 'sm-crypto';
2
+ import { message } from 'antd';
3
+ import { post } from '@brushes/request';
4
+
5
+ const PUBLIC_KEY_URL = '/web/dd/falgSetting/getFalgSettingByCode.json';
6
+ const PUBLIC_KEY_CODE = 'SM2_PUBLIC_KEY';
7
+
8
+ let publicKeyPromise: Promise<string> | null = null;
9
+
10
+ const requestPublicKey = async (): Promise<string> => {
11
+ const response: any = await post(PUBLIC_KEY_URL, { flagSettingCode: PUBLIC_KEY_CODE });
12
+
13
+ const publicKey = response?.flagSettingInfo || (response?.dataObj && response.dataObj.flagSettingInfo);
14
+
15
+ if (!publicKey) {
16
+ throw new Error('未获取到 SM2 公钥');
17
+ }
18
+
19
+ return publicKey;
20
+ };
21
+
22
+ const getPublicKey = (): Promise<string> => {
23
+ if (!publicKeyPromise) {
24
+ publicKeyPromise = requestPublicKey().catch((error) => {
25
+ publicKeyPromise = null;
26
+ throw error;
27
+ });
28
+ }
29
+
30
+ return publicKeyPromise;
31
+ };
32
+
33
+ export const encryptBySM2 = async (plainText: string): Promise<string> => {
34
+ try {
35
+ const publicKey = await getPublicKey();
36
+ return `04${sm2.doEncrypt(String(plainText), publicKey, 1)}`;
37
+ } catch (error) {
38
+ message.error('SM2 密钥获取失败');
39
+ return plainText;
40
+ }
41
+ };