ChannelKeyViewExample.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, { useState } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import { Button, Modal } from '@douyinfe/semi-ui';
  18. import { useSecureVerification } from '../../../hooks/common/useSecureVerification';
  19. import { createApiCalls } from '../../../services/secureVerification';
  20. import SecureVerificationModal from '../modals/SecureVerificationModal';
  21. import ChannelKeyDisplay from '../ui/ChannelKeyDisplay';
  22. /**
  23. * 渠道密钥查看组件使用示例
  24. * 展示如何使用通用安全验证系统
  25. */
  26. const ChannelKeyViewExample = ({ channelId }) => {
  27. const { t } = useTranslation();
  28. const [keyData, setKeyData] = useState('');
  29. const [showKeyModal, setShowKeyModal] = useState(false);
  30. // 使用通用安全验证 Hook
  31. const {
  32. isModalVisible,
  33. verificationMethods,
  34. verificationState,
  35. startVerification,
  36. executeVerification,
  37. cancelVerification,
  38. setVerificationCode,
  39. switchVerificationMethod,
  40. } = useSecureVerification({
  41. onSuccess: (result) => {
  42. // 验证成功后处理结果
  43. if (result.success && result.data?.key) {
  44. setKeyData(result.data.key);
  45. setShowKeyModal(true);
  46. }
  47. },
  48. successMessage: t('密钥获取成功'),
  49. });
  50. // 开始查看密钥流程
  51. const handleViewKey = async () => {
  52. const apiCall = createApiCalls.viewChannelKey(channelId);
  53. await startVerification(apiCall, {
  54. title: t('查看渠道密钥'),
  55. description: t('为了保护账户安全,请验证您的身份。'),
  56. preferredMethod: 'passkey', // 可以指定首选验证方式
  57. });
  58. };
  59. return (
  60. <>
  61. {/* 查看密钥按钮 */}
  62. <Button type='primary' theme='outline' onClick={handleViewKey}>
  63. {t('查看密钥')}
  64. </Button>
  65. {/* 安全验证模态框 */}
  66. <SecureVerificationModal
  67. visible={isModalVisible}
  68. verificationMethods={verificationMethods}
  69. verificationState={verificationState}
  70. onVerify={executeVerification}
  71. onCancel={cancelVerification}
  72. onCodeChange={setVerificationCode}
  73. onMethodSwitch={switchVerificationMethod}
  74. title={verificationState.title}
  75. description={verificationState.description}
  76. />
  77. {/* 密钥显示模态框 */}
  78. <Modal
  79. title={t('渠道密钥信息')}
  80. visible={showKeyModal}
  81. onCancel={() => setShowKeyModal(false)}
  82. footer={
  83. <Button type='primary' onClick={() => setShowKeyModal(false)}>
  84. {t('完成')}
  85. </Button>
  86. }
  87. width={700}
  88. style={{ maxWidth: '90vw' }}
  89. >
  90. <ChannelKeyDisplay
  91. keyData={keyData}
  92. showSuccessIcon={true}
  93. successText={t('密钥获取成功')}
  94. showWarning={true}
  95. />
  96. </Modal>
  97. </>
  98. );
  99. };
  100. export default ChannelKeyViewExample;