message-verify 1.0.0-beta.1 → 1.0.0-beta.10

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/README.md CHANGED
@@ -9,4 +9,29 @@
9
9
  ### 1. 引入组件
10
10
 
11
11
  ```typescript
12
- import {createMessageVerifyModal} from 'message-verify';
12
+ import {createMessageVerifyModal} from 'message-verify';
13
+ ```
14
+
15
+ ### 2. 参数说明
16
+
17
+ ```typescript
18
+ - data :字符串,包含手机号、验证码 key 等信息(JSON 字符串)。
19
+ - config :对象,必须包含 headers 字段(可为空对象),用于 http 请求配置。
20
+ - lang :组件使用语言,支持 'zh' (中文)、 'en' (英文)、 'id' (印尼语)。
21
+ - http :http 客户端实例(如 axios)。
22
+ - onClose :弹窗关闭回调(可选)。
23
+ ```
24
+
25
+ ### 3. 示例
26
+
27
+ ```typescript
28
+ createMessageVerifyModal({
29
+ data,
30
+ config: { headers: {} }, // 必须包含 headers 字段
31
+ lang: 'zh', // 支持 'zh' | 'en' | 'id'
32
+ http: axios,
33
+ onClose: () => {
34
+ // 可选,弹窗关闭回调
35
+ }
36
+ });
37
+ ```
package/dist/index.d.ts CHANGED
@@ -1,18 +1,3 @@
1
+ import { ModalConfig } from './utils/type.js';
1
2
  export declare const initFingerprint: () => Promise<string>;
2
- export type ModalConfig = {
3
- data: string;
4
- onClose?: () => void;
5
- config: object;
6
- lang: 'zh' | 'en' | 'id';
7
- http: {
8
- (params: object): Promise<unknown>;
9
- defaults: {
10
- headers: {
11
- common: {
12
- [key: string]: unknown;
13
- };
14
- };
15
- };
16
- };
17
- };
18
3
  export declare const createMessageVerifyModal: (modalProps: ModalConfig) => void;
package/dist/index.js CHANGED
@@ -2,7 +2,13 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import ReactDOM from 'react-dom/client';
3
3
  import VerifyModal from './verify-modal.js';
4
4
  import Fingerprint2 from 'fingerprintjs2';
5
- export const initFingerprint = () => {
5
+ let cachedFingerprint = null;
6
+ export const initFingerprint = async () => {
7
+ if (cachedFingerprint) {
8
+ // 已有缓存,直接返回 Promise
9
+ return Promise.resolve(cachedFingerprint);
10
+ }
11
+ // 首次异步获取并缓存
6
12
  return new Promise((resolve) => {
7
13
  Fingerprint2.get(function (components) {
8
14
  const includeKeys = ['userAgent', 'deviceMemory', 'cpuClass', 'hardwareConcurrency', 'platform'];
@@ -11,6 +17,7 @@ export const initFingerprint = () => {
11
17
  .map(component => component.value)
12
18
  .join('###');
13
19
  const fingerprint = Fingerprint2.x64hash128(values, 31);
20
+ cachedFingerprint = fingerprint;
14
21
  resolve(fingerprint);
15
22
  });
16
23
  });
@@ -4,6 +4,7 @@ declare const _default: {
4
4
  verifyCode: string;
5
5
  pleaseEnterAndSend: string;
6
6
  sendSuccess: string;
7
+ sendFailed: string;
7
8
  reSend: string;
8
9
  countDownSecound: string;
9
10
  };
@@ -4,6 +4,7 @@ export default {
4
4
  verifyCode: 'Verification Code',
5
5
  pleaseEnterAndSend: 'Please enter the image verification code first, then resend the code',
6
6
  sendSuccess: 'Successfully sent',
7
+ sendFailed: 'Send failed',
7
8
  reSend: 'Resend',
8
9
  countDownSecound: '{countdown} seconds'
9
10
  };
@@ -4,6 +4,7 @@ declare const _default: {
4
4
  verifyCode: string;
5
5
  pleaseEnterAndSend: string;
6
6
  sendSuccess: string;
7
+ sendFailed: string;
7
8
  reSend: string;
8
9
  countDownSecound: string;
9
10
  };
@@ -4,6 +4,7 @@ export default {
4
4
  verifyCode: 'Kode Verifikasi',
5
5
  pleaseEnterAndSend: 'Harap masukkan kode verifikasi gambar terlebih dahulu, lalu kirim ulang kode',
6
6
  sendSuccess: 'Berhasil dikirim',
7
+ sendFailed: 'Gagal mengirim',
7
8
  reSend: 'Kirim Ulang',
8
9
  countDownSecound: '{countdown} detik'
9
10
  };
@@ -4,6 +4,7 @@ declare const _default: {
4
4
  verifyCode: string;
5
5
  pleaseEnterAndSend: string;
6
6
  sendSuccess: string;
7
+ sendFailed: string;
7
8
  reSend: string;
8
9
  countDownSecound: string;
9
10
  };
@@ -4,6 +4,7 @@ export default {
4
4
  verifyCode: '验证码',
5
5
  pleaseEnterAndSend: '请输入图形验证码后,重新发送验证码',
6
6
  sendSuccess: '发送成功',
7
+ sendFailed: '发送失败',
7
8
  reSend: '重新发送',
8
9
  countDownSecound: '{countdown}秒'
9
10
  };
package/dist/main.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { createRoot } from 'react-dom/client';
3
- import { createMessageVerifyModal } from './index.js'; // 或 './index'
3
+ import { createMessageVerifyModal, initFingerprint } from './index.js'; // 或 './index'
4
4
  import { axios } from '@yqg/resource';
5
5
  const data = "{\"mobile\":\"188****4035\",\"verifyCodeKey\":\"3f025b33-988e-47c0-950d-6beb776d043f\",\"needValid\":true,\"step\":2,\"message\":\"需要填写验证码\"}";
6
+ const fp = await initFingerprint();
7
+ console.log(fp);
6
8
  createRoot(document.getElementById('root')).render(_jsx("button", { onClick: () => createMessageVerifyModal({
7
9
  data,
8
- config: {},
10
+ config: { headers: {} },
9
11
  lang: 'en',
10
12
  http: axios,
11
13
  }), children: "Show Modal" }));
@@ -1 +1 @@
1
- {"root":["../src/index.tsx","../src/main.tsx","../src/resource.ts","../src/verify-modal.tsx","../src/vite-env.d.ts","../src/compoments/button.tsx","../src/compoments/index.ts","../src/compoments/input.tsx","../src/compoments/message.tsx","../src/compoments/modal.tsx","../src/locales/en.ts","../src/locales/id.ts","../src/locales/zh.ts","../src/utils/i18n.ts","../src/utils/status.ts"],"version":"5.7.3"}
1
+ {"root":["../src/index.tsx","../src/main.tsx","../src/resource.ts","../src/verify-modal.tsx","../src/vite-env.d.ts","../src/compoments/button.tsx","../src/compoments/index.ts","../src/compoments/input.tsx","../src/compoments/message.tsx","../src/compoments/modal.tsx","../src/locales/en.ts","../src/locales/id.ts","../src/locales/zh.ts","../src/utils/i18n.ts","../src/utils/status.ts","../src/utils/type.ts"],"version":"5.7.3"}
@@ -0,0 +1,22 @@
1
+ export type ModalConfig = {
2
+ data: string;
3
+ onClose?: () => void;
4
+ config: {
5
+ headers: {
6
+ [key: string]: string | undefined;
7
+ };
8
+ [key: string]: unknown;
9
+ };
10
+ lang: 'zh' | 'en' | 'id';
11
+ http: {
12
+ (params: object): Promise<unknown>;
13
+ defaults: {
14
+ headers: {
15
+ common: {
16
+ [key: string]: unknown;
17
+ };
18
+ };
19
+ };
20
+ };
21
+ api?: string;
22
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,19 +1,4 @@
1
- type ModalConfig = {
2
- data: string;
3
- onClose?: () => void;
4
- config: object;
5
- lang: 'zh' | 'en' | 'id';
6
- http: {
7
- (params: object): Promise<unknown>;
8
- defaults: {
9
- headers: {
10
- common: {
11
- [key: string]: unknown;
12
- };
13
- };
14
- };
15
- };
16
- };
1
+ import { ModalConfig } from './utils/type.js';
17
2
  declare const CreateMessageVerifyModal: ({ props }: {
18
3
  props: ModalConfig;
19
4
  }) => import("react/jsx-runtime.js").JSX.Element;
@@ -3,14 +3,15 @@ import { useEffect, useState } from 'react';
3
3
  import { Modal, Input, Button, message } from './compoments/index.js';
4
4
  import Api from './resource.js';
5
5
  import t from './utils/i18n.js';
6
+ import { axios } from '@yqg/resource';
6
7
  const CreateMessageVerifyModal = ({ props }) => {
7
8
  const [visible, setVisible] = useState(true);
8
- const [countdown, setCountdown] = useState(3);
9
+ const [countdown, setCountdown] = useState(60);
9
10
  const [otp, setOtp] = useState('');
10
11
  const [captchaImage, setCaptchaImage] = useState('');
11
12
  const [captchCode, setCaptchCode] = useState('');
12
13
  const [captchaKey, setCaptchaKey] = useState('');
13
- const { data, config, http, lang } = props || {};
14
+ const { data, config, http, lang, api = '/admin/sms/send' } = props || {};
14
15
  const dataObj = JSON.parse(data || '{}');
15
16
  const { mobile, verifyCodeKey } = dataObj || {};
16
17
  const getKey = async () => {
@@ -46,27 +47,33 @@ const CreateMessageVerifyModal = ({ props }) => {
46
47
  };
47
48
  }, [visible, countdown]);
48
49
  const handleResend = async () => {
49
- setCountdown(60);
50
50
  const formData = new FormData();
51
51
  formData.append('captchaKey', captchaKey);
52
52
  formData.append('captcha', captchCode);
53
- const res = await Api.reSendCode(formData);
54
- console.log('handleResend=>res', res);
53
+ // 重新发送验证码
54
+ const res = await axios.post(api, formData);
55
+ const { code, detail } = res.data?.status || {};
56
+ if (code !== 0) {
57
+ message.error(detail || t('sendFailed', lang));
58
+ return;
59
+ }
55
60
  message.success(t('sendSuccess', lang));
61
+ setCountdown(60);
56
62
  };
57
63
  return (_jsxs(Modal, { width: 420, visible: visible, onClose: () => {
58
64
  setVisible(false);
59
65
  props.onClose?.();
60
66
  }, children: [_jsx("div", { style: { fontSize: 14, color: '#666' }, children: t('alreadySend', lang, { phone: mobile }) }), _jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 8, marginTop: 12 }, children: [_jsx(Input.OTP, { length: 6, value: otp, onChange: (val) => {
61
67
  setOtp(val);
68
+ console.log('打印Content-Type', config.headers['Content-Type']);
62
69
  if (val.length === 6) {
63
- console.log('otp', val);
64
70
  http({
65
71
  ...config,
66
72
  headers: {
67
73
  ...http.defaults.headers.common,
68
74
  'sms-code': val,
69
75
  'sms-code-key': verifyCodeKey,
76
+ 'Content-Type': config.headers['Content-Type'],
70
77
  }
71
78
  });
72
79
  setVisible(false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "message-verify",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.10",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "dependencies": {
package/src/index.tsx CHANGED
@@ -1,8 +1,15 @@
1
1
  import ReactDOM from 'react-dom/client'
2
2
  import VerifyModal from './verify-modal.js'
3
3
  import Fingerprint2 from 'fingerprintjs2';
4
+ import { ModalConfig } from './utils/type.js';
4
5
 
5
- export const initFingerprint = (): Promise<string> => {
6
+ let cachedFingerprint: string | null = null;
7
+ export const initFingerprint = async (): Promise<string> => {
8
+ if (cachedFingerprint) {
9
+ // 已有缓存,直接返回 Promise
10
+ return Promise.resolve(cachedFingerprint);
11
+ }
12
+ // 首次异步获取并缓存
6
13
  return new Promise((resolve) => {
7
14
  Fingerprint2.get(function (components) {
8
15
  const includeKeys = ['userAgent', 'deviceMemory', 'cpuClass', 'hardwareConcurrency', 'platform'];
@@ -11,28 +18,12 @@ export const initFingerprint = (): Promise<string> => {
11
18
  .map(component => component.value)
12
19
  .join('###');
13
20
  const fingerprint = Fingerprint2.x64hash128(values, 31);
21
+ cachedFingerprint = fingerprint;
14
22
  resolve(fingerprint);
15
23
  });
16
24
  });
17
25
  };
18
26
 
19
- export type ModalConfig = {
20
- data: string;
21
- onClose?: () => void
22
- config: object;
23
- lang: 'zh' | 'en' | 'id';
24
- http: {
25
- (params: object): Promise<unknown>;
26
- defaults: {
27
- headers: {
28
- common: {
29
- [key: string]: unknown;
30
- }
31
- }
32
- }
33
- };
34
- }
35
-
36
27
  let modalRoot: ReactDOM.Root | null = null
37
28
 
38
29
  export const createMessageVerifyModal = (modalProps: ModalConfig) => {
package/src/locales/en.ts CHANGED
@@ -4,6 +4,7 @@ export default {
4
4
  verifyCode: 'Verification Code',
5
5
  pleaseEnterAndSend: 'Please enter the image verification code first, then resend the code',
6
6
  sendSuccess: 'Successfully sent',
7
+ sendFailed: 'Send failed',
7
8
  reSend: 'Resend',
8
9
  countDownSecound: '{countdown} seconds'
9
10
  };
package/src/locales/id.ts CHANGED
@@ -4,6 +4,7 @@ export default {
4
4
  verifyCode: 'Kode Verifikasi',
5
5
  pleaseEnterAndSend: 'Harap masukkan kode verifikasi gambar terlebih dahulu, lalu kirim ulang kode',
6
6
  sendSuccess: 'Berhasil dikirim',
7
+ sendFailed: 'Gagal mengirim',
7
8
  reSend: 'Kirim Ulang',
8
9
  countDownSecound: '{countdown} detik'
9
10
  };
package/src/locales/zh.ts CHANGED
@@ -4,6 +4,7 @@ export default {
4
4
  verifyCode: '验证码',
5
5
  pleaseEnterAndSend: '请输入图形验证码后,重新发送验证码',
6
6
  sendSuccess: '发送成功',
7
+ sendFailed: '发送失败',
7
8
  reSend: '重新发送',
8
9
  countDownSecound: '{countdown}秒'
9
10
  };
package/src/main.tsx CHANGED
@@ -1,13 +1,16 @@
1
1
  import { createRoot } from 'react-dom/client';
2
- import { createMessageVerifyModal } from './index.js'; // 或 './index'
2
+ import { createMessageVerifyModal, initFingerprint } from './index.js'; // 或 './index'
3
3
  import { axios } from '@yqg/resource';
4
4
 
5
5
  const data = "{\"mobile\":\"188****4035\",\"verifyCodeKey\":\"3f025b33-988e-47c0-950d-6beb776d043f\",\"needValid\":true,\"step\":2,\"message\":\"需要填写验证码\"}";
6
6
 
7
+ const fp = await initFingerprint();
8
+ console.log(fp);
9
+
7
10
  createRoot(document.getElementById('root')!).render(
8
11
  <button onClick={() => createMessageVerifyModal({
9
12
  data,
10
- config: {},
13
+ config: {headers: {}},
11
14
  lang: 'en',
12
15
  http: axios,
13
16
  })}>
@@ -0,0 +1,23 @@
1
+
2
+ export type ModalConfig = {
3
+ data: string;
4
+ onClose?: () => void
5
+ config: {
6
+ headers: {
7
+ [key: string]: string | undefined;
8
+ };
9
+ [key: string]: unknown;
10
+ };
11
+ lang: 'zh' | 'en' | 'id';
12
+ http: {
13
+ (params: object): Promise<unknown>;
14
+ defaults: {
15
+ headers: {
16
+ common: {
17
+ [key: string]: unknown;
18
+ }
19
+ }
20
+ }
21
+ };
22
+ api?: string;
23
+ }
@@ -2,31 +2,17 @@ import { useEffect, useState } from 'react'
2
2
  import { Modal, Input, Button, message } from './compoments/index.js'
3
3
  import Api from './resource.js'
4
4
  import t from './utils/i18n.js'
5
+ import { ModalConfig } from './utils/type.js';
6
+ import { axios } from '@yqg/resource';
5
7
 
6
- type ModalConfig = {
7
- data: string;
8
- onClose?: () => void;
9
- config: object;
10
- lang: 'zh' | 'en' | 'id';
11
- http: {
12
- (params: object): Promise<unknown>;
13
- defaults: {
14
- headers: {
15
- common: {
16
- [key: string]: unknown;
17
- }
18
- }
19
- }
20
- };
21
- }
22
8
  const CreateMessageVerifyModal = ({ props }: { props: ModalConfig }) => {
23
9
  const [visible, setVisible] = useState(true);
24
- const [countdown, setCountdown] = useState(3);
10
+ const [countdown, setCountdown] = useState(60);
25
11
  const [otp, setOtp] = useState('');
26
12
  const [captchaImage, setCaptchaImage] = useState<string>('');
27
13
  const [captchCode, setCaptchCode] = useState<string>('');
28
14
  const [captchaKey, setCaptchaKey] = useState<string>('');
29
- const { data, config, http, lang } = props || {};
15
+ const { data, config, http, lang, api='/admin/sms/send' } = props || {};
30
16
  const dataObj = JSON.parse(data || '{}');
31
17
  const { mobile, verifyCodeKey } = dataObj || {};
32
18
 
@@ -63,13 +49,18 @@ const CreateMessageVerifyModal = ({ props }: { props: ModalConfig }) => {
63
49
  }, [visible, countdown]);
64
50
 
65
51
  const handleResend = async () => {
66
- setCountdown(60);
67
52
  const formData = new FormData();
68
53
  formData.append('captchaKey', captchaKey);
69
54
  formData.append('captcha', captchCode);
70
- const res = await Api.reSendCode(formData);
71
- console.log('handleResend=>res', res);
55
+ // 重新发送验证码
56
+ const res = await axios.post(api, formData);
57
+ const { code, detail } = res.data?.status || {};
58
+ if (code !== 0) {
59
+ message.error(detail || t('sendFailed', lang));
60
+ return;
61
+ }
72
62
  message.success(t('sendSuccess', lang));
63
+ setCountdown(60);
73
64
  }
74
65
 
75
66
  return (
@@ -89,14 +80,15 @@ const CreateMessageVerifyModal = ({ props }: { props: ModalConfig }) => {
89
80
  value={otp}
90
81
  onChange={(val) => {
91
82
  setOtp(val);
83
+ console.log('打印Content-Type', config.headers['Content-Type'])
92
84
  if (val.length === 6) {
93
- console.log('otp', val);
94
85
  http({
95
86
  ...config,
96
87
  headers: {
97
88
  ...http.defaults.headers.common,
98
89
  'sms-code': val,
99
90
  'sms-code-key': verifyCodeKey,
91
+ 'Content-Type': config.headers['Content-Type'],
100
92
  }
101
93
  });
102
94
  setVisible(false);