RechargeCard.jsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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, { useRef } from 'react';
  16. import {
  17. Avatar,
  18. Typography,
  19. Tag,
  20. Card,
  21. Button,
  22. Banner,
  23. Skeleton,
  24. Form,
  25. Space,
  26. Row,
  27. Col,
  28. Spin,
  29. Tooltip,
  30. } from '@douyinfe/semi-ui';
  31. import { SiAlipay, SiWechat, SiStripe } from 'react-icons/si';
  32. import {
  33. CreditCard,
  34. Coins,
  35. Wallet,
  36. BarChart2,
  37. TrendingUp,
  38. Receipt,
  39. } from 'lucide-react';
  40. import { IconGift } from '@douyinfe/semi-icons';
  41. import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
  42. import { getCurrencyConfig } from '../../helpers/render';
  43. const { Text } = Typography;
  44. const RechargeCard = ({
  45. t,
  46. enableOnlineTopUp,
  47. enableStripeTopUp,
  48. presetAmounts,
  49. selectedPreset,
  50. selectPresetAmount,
  51. formatLargeNumber,
  52. priceRatio,
  53. topUpCount,
  54. minTopUp,
  55. renderQuotaWithAmount,
  56. getAmount,
  57. setTopUpCount,
  58. setSelectedPreset,
  59. renderAmount,
  60. amountLoading,
  61. payMethods,
  62. preTopUp,
  63. paymentLoading,
  64. payWay,
  65. redemptionCode,
  66. setRedemptionCode,
  67. topUp,
  68. isSubmitting,
  69. topUpLink,
  70. openTopUpLink,
  71. userState,
  72. renderQuota,
  73. statusLoading,
  74. topupInfo,
  75. onOpenHistory,
  76. }) => {
  77. const onlineFormApiRef = useRef(null);
  78. const redeemFormApiRef = useRef(null);
  79. const showAmountSkeleton = useMinimumLoadingTime(amountLoading);
  80. return (
  81. <Card className='!rounded-2xl shadow-sm border-0'>
  82. {/* 卡片头部 */}
  83. <div className='flex items-center justify-between mb-4'>
  84. <div className='flex items-center'>
  85. <Avatar size='small' color='blue' className='mr-3 shadow-md'>
  86. <CreditCard size={16} />
  87. </Avatar>
  88. <div>
  89. <Typography.Text className='text-lg font-medium'>
  90. {t('账户充值')}
  91. </Typography.Text>
  92. <div className='text-xs'>{t('多种充值方式,安全便捷')}</div>
  93. </div>
  94. </div>
  95. <Button
  96. icon={<Receipt size={16} />}
  97. theme='solid'
  98. onClick={onOpenHistory}
  99. >
  100. {t('账单')}
  101. </Button>
  102. </div>
  103. <Space vertical style={{ width: '100%' }}>
  104. {/* 统计数据 */}
  105. <Card
  106. className='!rounded-xl w-full'
  107. cover={
  108. <div
  109. className='relative h-30'
  110. style={{
  111. '--palette-primary-darkerChannel': '37 99 235',
  112. backgroundImage: `linear-gradient(0deg, rgba(var(--palette-primary-darkerChannel) / 80%), rgba(var(--palette-primary-darkerChannel) / 80%)), url('/cover-4.webp')`,
  113. backgroundSize: 'cover',
  114. backgroundPosition: 'center',
  115. backgroundRepeat: 'no-repeat',
  116. }}
  117. >
  118. <div className='relative z-10 h-full flex flex-col justify-between p-4'>
  119. <div className='flex justify-between items-center'>
  120. <Text strong style={{ color: 'white', fontSize: '16px' }}>
  121. {t('账户统计')}
  122. </Text>
  123. </div>
  124. {/* 统计数据 */}
  125. <div className='grid grid-cols-3 gap-6 mt-4'>
  126. {/* 当前余额 */}
  127. <div className='text-center'>
  128. <div
  129. className='text-base sm:text-2xl font-bold mb-2'
  130. style={{ color: 'white' }}
  131. >
  132. {renderQuota(userState?.user?.quota)}
  133. </div>
  134. <div className='flex items-center justify-center text-sm'>
  135. <Wallet
  136. size={14}
  137. className='mr-1'
  138. style={{ color: 'rgba(255,255,255,0.8)' }}
  139. />
  140. <Text
  141. style={{
  142. color: 'rgba(255,255,255,0.8)',
  143. fontSize: '12px',
  144. }}
  145. >
  146. {t('当前余额')}
  147. </Text>
  148. </div>
  149. </div>
  150. {/* 历史消耗 */}
  151. <div className='text-center'>
  152. <div
  153. className='text-base sm:text-2xl font-bold mb-2'
  154. style={{ color: 'white' }}
  155. >
  156. {renderQuota(userState?.user?.used_quota)}
  157. </div>
  158. <div className='flex items-center justify-center text-sm'>
  159. <TrendingUp
  160. size={14}
  161. className='mr-1'
  162. style={{ color: 'rgba(255,255,255,0.8)' }}
  163. />
  164. <Text
  165. style={{
  166. color: 'rgba(255,255,255,0.8)',
  167. fontSize: '12px',
  168. }}
  169. >
  170. {t('历史消耗')}
  171. </Text>
  172. </div>
  173. </div>
  174. {/* 请求次数 */}
  175. <div className='text-center'>
  176. <div
  177. className='text-base sm:text-2xl font-bold mb-2'
  178. style={{ color: 'white' }}
  179. >
  180. {userState?.user?.request_count || 0}
  181. </div>
  182. <div className='flex items-center justify-center text-sm'>
  183. <BarChart2
  184. size={14}
  185. className='mr-1'
  186. style={{ color: 'rgba(255,255,255,0.8)' }}
  187. />
  188. <Text
  189. style={{
  190. color: 'rgba(255,255,255,0.8)',
  191. fontSize: '12px',
  192. }}
  193. >
  194. {t('请求次数')}
  195. </Text>
  196. </div>
  197. </div>
  198. </div>
  199. </div>
  200. </div>
  201. }
  202. >
  203. {/* 在线充值表单 */}
  204. {statusLoading ? (
  205. <div className='py-8 flex justify-center'>
  206. <Spin size='large' />
  207. </div>
  208. ) : enableOnlineTopUp || enableStripeTopUp ? (
  209. <Form
  210. getFormApi={(api) => (onlineFormApiRef.current = api)}
  211. initValues={{ topUpCount: topUpCount }}
  212. >
  213. <div className='space-y-6'>
  214. {(enableOnlineTopUp || enableStripeTopUp) && (
  215. <Row gutter={12}>
  216. <Col xs={24} sm={24} md={24} lg={10} xl={10}>
  217. <Form.InputNumber
  218. field='topUpCount'
  219. label={t('充值数量')}
  220. disabled={!enableOnlineTopUp && !enableStripeTopUp}
  221. placeholder={
  222. t('充值数量,最低 ') + renderQuotaWithAmount(minTopUp)
  223. }
  224. value={topUpCount}
  225. min={minTopUp}
  226. max={999999999}
  227. step={1}
  228. precision={0}
  229. onChange={async (value) => {
  230. if (value && value >= 1) {
  231. setTopUpCount(value);
  232. setSelectedPreset(null);
  233. await getAmount(value);
  234. }
  235. }}
  236. onBlur={(e) => {
  237. const value = parseInt(e.target.value);
  238. if (!value || value < 1) {
  239. setTopUpCount(1);
  240. getAmount(1);
  241. }
  242. }}
  243. formatter={(value) => (value ? `${value}` : '')}
  244. parser={(value) =>
  245. value ? parseInt(value.replace(/[^\d]/g, '')) : 0
  246. }
  247. extraText={
  248. <Skeleton
  249. loading={showAmountSkeleton}
  250. active
  251. placeholder={
  252. <Skeleton.Title
  253. style={{
  254. width: 120,
  255. height: 20,
  256. borderRadius: 6,
  257. }}
  258. />
  259. }
  260. >
  261. <Text type='secondary' className='text-red-600'>
  262. {t('实付金额:')}
  263. <span style={{ color: 'red' }}>
  264. {renderAmount()}
  265. </span>
  266. </Text>
  267. </Skeleton>
  268. }
  269. style={{ width: '100%' }}
  270. />
  271. </Col>
  272. <Col xs={24} sm={24} md={24} lg={14} xl={14}>
  273. <Form.Slot label={t('选择支付方式')}>
  274. {payMethods && payMethods.length > 0 ? (
  275. <Space wrap>
  276. {payMethods.map((payMethod) => {
  277. const minTopupVal =
  278. Number(payMethod.min_topup) || 0;
  279. const isStripe = payMethod.type === 'stripe';
  280. const disabled =
  281. (!enableOnlineTopUp && !isStripe) ||
  282. (!enableStripeTopUp && isStripe) ||
  283. minTopupVal > Number(topUpCount || 0);
  284. const buttonEl = (
  285. <Button
  286. key={payMethod.type}
  287. theme='outline'
  288. type='tertiary'
  289. onClick={() => preTopUp(payMethod.type)}
  290. disabled={disabled}
  291. loading={
  292. paymentLoading && payWay === payMethod.type
  293. }
  294. icon={
  295. payMethod.type === 'alipay' ? (
  296. <SiAlipay size={18} color='#1677FF' />
  297. ) : payMethod.type === 'wxpay' ? (
  298. <SiWechat size={18} color='#07C160' />
  299. ) : payMethod.type === 'stripe' ? (
  300. <SiStripe size={18} color='#635BFF' />
  301. ) : (
  302. <CreditCard
  303. size={18}
  304. color={
  305. payMethod.color ||
  306. 'var(--semi-color-text-2)'
  307. }
  308. />
  309. )
  310. }
  311. className='!rounded-lg !px-4 !py-2'
  312. >
  313. {payMethod.name}
  314. </Button>
  315. );
  316. return disabled &&
  317. minTopupVal > Number(topUpCount || 0) ? (
  318. <Tooltip
  319. content={
  320. t('此支付方式最低充值金额为') +
  321. ' ' +
  322. minTopupVal
  323. }
  324. key={payMethod.type}
  325. >
  326. {buttonEl}
  327. </Tooltip>
  328. ) : (
  329. <React.Fragment key={payMethod.type}>
  330. {buttonEl}
  331. </React.Fragment>
  332. );
  333. })}
  334. </Space>
  335. ) : (
  336. <div className='text-gray-500 text-sm p-3 bg-gray-50 rounded-lg border border-dashed border-gray-300'>
  337. {t('暂无可用的支付方式,请联系管理员配置')}
  338. </div>
  339. )}
  340. </Form.Slot>
  341. </Col>
  342. </Row>
  343. )}
  344. {(enableOnlineTopUp || enableStripeTopUp) && (
  345. <Form.Slot
  346. label={
  347. <div className='flex items-center gap-2'>
  348. <span>{t('选择充值额度')}</span>
  349. {(() => {
  350. const { symbol, rate, type } = getCurrencyConfig();
  351. if (type === 'USD') return null;
  352. return (
  353. <span
  354. style={{
  355. color: 'var(--semi-color-text-2)',
  356. fontSize: '12px',
  357. fontWeight: 'normal',
  358. }}
  359. >
  360. (1 $ = {rate.toFixed(2)} {symbol})
  361. </span>
  362. );
  363. })()}
  364. </div>
  365. }
  366. >
  367. <div className='grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-2'>
  368. {presetAmounts.map((preset, index) => {
  369. const discount =
  370. preset.discount ||
  371. topupInfo?.discount?.[preset.value] ||
  372. 1.0;
  373. const originalPrice = preset.value * priceRatio;
  374. const discountedPrice = originalPrice * discount;
  375. const hasDiscount = discount < 1.0;
  376. const actualPay = discountedPrice;
  377. const save = originalPrice - discountedPrice;
  378. // 根据当前货币类型换算显示金额和数量
  379. const { symbol, rate, type } = getCurrencyConfig();
  380. const statusStr = localStorage.getItem('status');
  381. let usdRate = 7; // 默认CNY汇率
  382. try {
  383. if (statusStr) {
  384. const s = JSON.parse(statusStr);
  385. usdRate = s?.usd_exchange_rate || 7;
  386. }
  387. } catch (e) {}
  388. let displayValue = preset.value; // 显示的数量
  389. let displayActualPay = actualPay;
  390. let displaySave = save;
  391. if (type === 'USD') {
  392. // 数量保持USD,价格从CNY转USD
  393. displayActualPay = actualPay / usdRate;
  394. displaySave = save / usdRate;
  395. } else if (type === 'CNY') {
  396. // 数量转CNY,价格已是CNY
  397. displayValue = preset.value * usdRate;
  398. } else if (type === 'CUSTOM') {
  399. // 数量和价格都转自定义货币
  400. displayValue = preset.value * rate;
  401. displayActualPay = (actualPay / usdRate) * rate;
  402. displaySave = (save / usdRate) * rate;
  403. }
  404. return (
  405. <Card
  406. key={index}
  407. style={{
  408. cursor: 'pointer',
  409. border:
  410. selectedPreset === preset.value
  411. ? '2px solid var(--semi-color-primary)'
  412. : '1px solid var(--semi-color-border)',
  413. height: '100%',
  414. width: '100%',
  415. }}
  416. bodyStyle={{ padding: '12px' }}
  417. onClick={() => {
  418. selectPresetAmount(preset);
  419. onlineFormApiRef.current?.setValue(
  420. 'topUpCount',
  421. preset.value,
  422. );
  423. }}
  424. >
  425. <div style={{ textAlign: 'center' }}>
  426. <Typography.Title
  427. heading={6}
  428. style={{ margin: '0 0 8px 0' }}
  429. >
  430. <Coins size={18} />
  431. {formatLargeNumber(preset.value)} $
  432. {hasDiscount && (
  433. <Tag style={{ marginLeft: 4 }} color='green'>
  434. {t('折').includes('off')
  435. ? (
  436. (1 - parseFloat(discount)) *
  437. 100
  438. ).toFixed(1)
  439. : (discount * 10).toFixed(1)}
  440. {t('折')}
  441. </Tag>
  442. )}
  443. </Typography.Title>
  444. <div
  445. style={{
  446. color: 'var(--semi-color-text-2)',
  447. fontSize: '12px',
  448. margin: '4px 0',
  449. }}
  450. >
  451. {t('实付')} {symbol}
  452. {displayActualPay.toFixed(2)},
  453. {hasDiscount
  454. ? `${t('节省')} ${symbol}${displaySave.toFixed(2)}`
  455. : `${t('节省')} ${symbol}0.00`}
  456. </div>
  457. </div>
  458. </Card>
  459. );
  460. })}
  461. </div>
  462. </Form.Slot>
  463. )}
  464. </div>
  465. </Form>
  466. ) : (
  467. <Banner
  468. type='info'
  469. description={t(
  470. '管理员未开启在线充值功能,请联系管理员开启或使用兑换码充值。',
  471. )}
  472. className='!rounded-xl'
  473. closeIcon={null}
  474. />
  475. )}
  476. </Card>
  477. {/* 兑换码充值 */}
  478. <Card
  479. className='!rounded-xl w-full'
  480. title={
  481. <Text type='tertiary' strong>
  482. {t('兑换码充值')}
  483. </Text>
  484. }
  485. >
  486. <Form
  487. getFormApi={(api) => (redeemFormApiRef.current = api)}
  488. initValues={{ redemptionCode: redemptionCode }}
  489. >
  490. <Form.Input
  491. field='redemptionCode'
  492. noLabel={true}
  493. placeholder={t('请输入兑换码')}
  494. value={redemptionCode}
  495. onChange={(value) => setRedemptionCode(value)}
  496. prefix={<IconGift />}
  497. suffix={
  498. <div className='flex items-center gap-2'>
  499. <Button
  500. type='primary'
  501. theme='solid'
  502. onClick={topUp}
  503. loading={isSubmitting}
  504. >
  505. {t('兑换额度')}
  506. </Button>
  507. </div>
  508. }
  509. showClear
  510. style={{ width: '100%' }}
  511. extraText={
  512. topUpLink && (
  513. <Text type='tertiary'>
  514. {t('在找兑换码?')}
  515. <Text
  516. type='secondary'
  517. underline
  518. className='cursor-pointer'
  519. onClick={openTopUpLink}
  520. >
  521. {t('购买兑换码')}
  522. </Text>
  523. </Text>
  524. )
  525. }
  526. />
  527. </Form>
  528. </Card>
  529. </Space>
  530. </Card>
  531. );
  532. };
  533. export default RechargeCard;