RechargeCard.jsx 17 KB

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