RechargeCard.jsx 18 KB

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