ChannelKeyViewExample.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  63. type='primary'
  64. theme='outline'
  65. onClick={handleViewKey}
  66. >
  67. {t('查看密钥')}
  68. </Button>
  69. {/* 安全验证模态框 */}
  70. <SecureVerificationModal
  71. visible={isModalVisible}
  72. verificationMethods={verificationMethods}
  73. verificationState={verificationState}
  74. onVerify={executeVerification}
  75. onCancel={cancelVerification}
  76. onCodeChange={setVerificationCode}
  77. onMethodSwitch={switchVerificationMethod}
  78. title={verificationState.title}
  79. description={verificationState.description}
  80. />
  81. {/* 密钥显示模态框 */}
  82. <Modal
  83. title={t('渠道密钥信息')}
  84. visible={showKeyModal}
  85. onCancel={() => setShowKeyModal(false)}
  86. footer={
  87. <Button type='primary' onClick={() => setShowKeyModal(false)}>
  88. {t('完成')}
  89. </Button>
  90. }
  91. width={700}
  92. style={{ maxWidth: '90vw' }}
  93. >
  94. <ChannelKeyDisplay
  95. keyData={keyData}
  96. showSuccessIcon={true}
  97. successText={t('密钥获取成功')}
  98. showWarning={true}
  99. />
  100. </Modal>
  101. </>
  102. );
  103. };
  104. export default ChannelKeyViewExample;