SecureVerificationModal.jsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect, useState } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import { Modal, Button, Input, Typography, Tabs, TabPane, Space, Spin } from '@douyinfe/semi-ui';
  18. /**
  19. * 通用安全验证模态框组件
  20. * 配合 useSecureVerification Hook 使用
  21. * @param {Object} props
  22. * @param {boolean} props.visible - 是否显示模态框
  23. * @param {Object} props.verificationMethods - 可用的验证方式
  24. * @param {Object} props.verificationState - 当前验证状态
  25. * @param {Function} props.onVerify - 验证回调
  26. * @param {Function} props.onCancel - 取消回调
  27. * @param {Function} props.onCodeChange - 验证码变化回调
  28. * @param {Function} props.onMethodSwitch - 验证方式切换回调
  29. * @param {string} props.title - 模态框标题
  30. * @param {string} props.description - 验证描述文本
  31. */
  32. const SecureVerificationModal = ({
  33. visible,
  34. verificationMethods,
  35. verificationState,
  36. onVerify,
  37. onCancel,
  38. onCodeChange,
  39. onMethodSwitch,
  40. title,
  41. description,
  42. }) => {
  43. const { t } = useTranslation();
  44. const [isAnimating, setIsAnimating] = useState(false);
  45. const [verifySuccess, setVerifySuccess] = useState(false);
  46. const { has2FA, hasPasskey, passkeySupported } = verificationMethods;
  47. const { method, loading, code } = verificationState;
  48. useEffect(() => {
  49. if (visible) {
  50. setIsAnimating(true);
  51. setVerifySuccess(false);
  52. } else {
  53. setIsAnimating(false);
  54. }
  55. }, [visible]);
  56. const handleKeyDown = (e) => {
  57. if (e.key === 'Enter' && code.trim() && !loading && method === '2fa') {
  58. onVerify(method, code);
  59. }
  60. if (e.key === 'Escape' && !loading) {
  61. onCancel();
  62. }
  63. };
  64. // 如果用户没有启用任何验证方式
  65. if (visible && !has2FA && !hasPasskey) {
  66. return (
  67. <Modal
  68. title={title || t('安全验证')}
  69. visible={visible}
  70. onCancel={onCancel}
  71. footer={
  72. <Button onClick={onCancel}>{t('确定')}</Button>
  73. }
  74. width={500}
  75. style={{ maxWidth: '90vw' }}
  76. >
  77. <div className='text-center py-6'>
  78. <div className='mb-4'>
  79. <svg
  80. className='w-16 h-16 text-yellow-500 mx-auto mb-4'
  81. fill='currentColor'
  82. viewBox='0 0 20 20'
  83. >
  84. <path
  85. fillRule='evenodd'
  86. d='M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z'
  87. clipRule='evenodd'
  88. />
  89. </svg>
  90. </div>
  91. <Typography.Title heading={4} className='mb-2'>
  92. {t('需要安全验证')}
  93. </Typography.Title>
  94. <Typography.Text type='tertiary'>
  95. {t('您需要先启用两步验证或 Passkey 才能查看敏感信息。')}
  96. </Typography.Text>
  97. <br />
  98. <Typography.Text type='tertiary'>
  99. {t('请前往个人设置 → 安全设置进行配置。')}
  100. </Typography.Text>
  101. </div>
  102. </Modal>
  103. );
  104. }
  105. return (
  106. <Modal
  107. title={title || t('安全验证')}
  108. visible={visible}
  109. onCancel={loading ? undefined : onCancel}
  110. closeOnEsc={!loading}
  111. footer={null}
  112. width={460}
  113. centered
  114. style={{
  115. maxWidth: 'calc(100vw - 32px)'
  116. }}
  117. bodyStyle={{
  118. padding: '20px 24px'
  119. }}
  120. >
  121. <div style={{ width: '100%' }}>
  122. {/* 描述信息 */}
  123. {description && (
  124. <Typography.Paragraph
  125. type="tertiary"
  126. style={{
  127. margin: '0 0 20px 0',
  128. fontSize: '14px',
  129. lineHeight: '1.6'
  130. }}
  131. >
  132. {description}
  133. </Typography.Paragraph>
  134. )}
  135. {/* 验证方式选择 */}
  136. <Tabs
  137. activeKey={method}
  138. onChange={onMethodSwitch}
  139. type='line'
  140. size='default'
  141. style={{ margin: 0 }}
  142. >
  143. {has2FA && (
  144. <TabPane
  145. tab={t('两步验证')}
  146. itemKey='2fa'
  147. >
  148. <div style={{ paddingTop: '20px' }}>
  149. <div style={{ marginBottom: '12px' }}>
  150. <Input
  151. placeholder={t('请输入6位验证码或8位备用码')}
  152. value={code}
  153. onChange={onCodeChange}
  154. size='large'
  155. maxLength={8}
  156. onKeyDown={handleKeyDown}
  157. autoFocus={method === '2fa'}
  158. disabled={loading}
  159. prefix={
  160. <svg style={{ width: 16, height: 16, marginRight: 8, flexShrink: 0 }} fill='currentColor' viewBox='0 0 20 20'>
  161. <path fillRule='evenodd' d='M18 8a6 6 0 01-7.743 5.743L10 14l-1 1-1 1H6v2H2v-4l4.257-4.257A6 6 0 1118 8zm-6-4a1 1 0 100 2 2 2 0 012 2 1 1 0 102 0 4 4 0 00-4-4z' clipRule='evenodd' />
  162. </svg>
  163. }
  164. style={{ width: '100%' }}
  165. />
  166. </div>
  167. <Typography.Text
  168. type="tertiary"
  169. size="small"
  170. style={{
  171. display: 'block',
  172. marginBottom: '20px',
  173. fontSize: '13px',
  174. lineHeight: '1.5'
  175. }}
  176. >
  177. {t('从认证器应用中获取验证码,或使用备用码')}
  178. </Typography.Text>
  179. <div style={{
  180. display: 'flex',
  181. justifyContent: 'flex-end',
  182. gap: '8px',
  183. flexWrap: 'wrap'
  184. }}>
  185. <Button onClick={onCancel} disabled={loading}>
  186. {t('取消')}
  187. </Button>
  188. <Button
  189. theme='solid'
  190. type='primary'
  191. loading={loading}
  192. disabled={!code.trim() || loading}
  193. onClick={() => onVerify(method, code)}
  194. >
  195. {t('验证')}
  196. </Button>
  197. </div>
  198. </div>
  199. </TabPane>
  200. )}
  201. {hasPasskey && passkeySupported && (
  202. <TabPane
  203. tab={t('Passkey')}
  204. itemKey='passkey'
  205. >
  206. <div style={{ paddingTop: '20px' }}>
  207. <div style={{
  208. textAlign: 'center',
  209. padding: '24px 16px',
  210. marginBottom: '20px'
  211. }}>
  212. <div style={{
  213. width: 56,
  214. height: 56,
  215. margin: '0 auto 16px',
  216. display: 'flex',
  217. alignItems: 'center',
  218. justifyContent: 'center',
  219. borderRadius: '50%',
  220. background: 'var(--semi-color-primary-light-default)',
  221. }}>
  222. <svg style={{ width: 28, height: 28, color: 'var(--semi-color-primary)' }} fill='currentColor' viewBox='0 0 20 20'>
  223. <path fillRule='evenodd' d='M18 8a6 6 0 01-7.743 5.743L10 14l-1 1-1 1H6v2H2v-4l4.257-4.257A6 6 0 1118 8zm-6-4a1 1 0 100 2 2 2 0 012 2 1 1 0 102 0 4 4 0 00-4-4z' clipRule='evenodd' />
  224. </svg>
  225. </div>
  226. <Typography.Title heading={5} style={{ margin: '0 0 8px', fontSize: '16px' }}>
  227. {t('使用 Passkey 验证')}
  228. </Typography.Title>
  229. <Typography.Text
  230. type='tertiary'
  231. style={{
  232. display: 'block',
  233. margin: 0,
  234. fontSize: '13px',
  235. lineHeight: '1.5'
  236. }}
  237. >
  238. {t('点击验证按钮,使用您的生物特征或安全密钥')}
  239. </Typography.Text>
  240. </div>
  241. <div style={{
  242. display: 'flex',
  243. justifyContent: 'flex-end',
  244. gap: '8px',
  245. flexWrap: 'wrap'
  246. }}>
  247. <Button onClick={onCancel} disabled={loading}>
  248. {t('取消')}
  249. </Button>
  250. <Button
  251. theme='solid'
  252. type='primary'
  253. loading={loading}
  254. disabled={loading}
  255. onClick={() => onVerify(method)}
  256. >
  257. {t('验证 Passkey')}
  258. </Button>
  259. </div>
  260. </div>
  261. </TabPane>
  262. )}
  263. </Tabs>
  264. </div>
  265. </Modal>
  266. );
  267. };
  268. export default SecureVerificationModal;