RechargeCard.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. Card,
  20. Button,
  21. Input,
  22. InputNumber,
  23. Banner,
  24. Skeleton,
  25. Divider,
  26. Form,
  27. Space,
  28. Row,
  29. Col,
  30. Spin,
  31. } from '@douyinfe/semi-ui';
  32. import { SiAlipay, SiWechat, SiStripe } from 'react-icons/si';
  33. import { CreditCard, Gift, Link as LinkIcon, Coins, Wallet, BarChart2, TrendingUp } from 'lucide-react';
  34. import { IconGift } from '@douyinfe/semi-icons';
  35. import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
  36. const { Text } = Typography;
  37. const RechargeCard = ({
  38. t,
  39. enableOnlineTopUp,
  40. enableStripeTopUp,
  41. presetAmounts,
  42. selectedPreset,
  43. selectPresetAmount,
  44. formatLargeNumber,
  45. priceRatio,
  46. topUpCount,
  47. minTopUp,
  48. renderQuotaWithAmount,
  49. getAmount,
  50. setTopUpCount,
  51. setSelectedPreset,
  52. renderAmount,
  53. amountLoading,
  54. payMethods,
  55. preTopUp,
  56. paymentLoading,
  57. payWay,
  58. redemptionCode,
  59. setRedemptionCode,
  60. topUp,
  61. isSubmitting,
  62. topUpLink,
  63. openTopUpLink,
  64. // 新增:用于右侧统计卡片
  65. userState,
  66. renderQuota,
  67. statusLoading,
  68. }) => {
  69. const onlineFormApiRef = useRef(null);
  70. const redeemFormApiRef = useRef(null);
  71. const showAmountSkeleton = useMinimumLoadingTime(amountLoading);
  72. return (
  73. <Card className="!rounded-2xl shadow-sm border-0">
  74. {/* 卡片头部 */}
  75. <div className="flex items-center mb-4">
  76. <Avatar size="small" color="blue" className="mr-3 shadow-md">
  77. <CreditCard size={16} />
  78. </Avatar>
  79. <div>
  80. <Typography.Text className="text-lg font-medium">{t('账户充值')}</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' }}>{t('账户统计')}</Text>
  102. </div>
  103. {/* 统计数据 */}
  104. <div className='grid grid-cols-3 gap-6 mt-4'>
  105. {/* 当前余额 */}
  106. <div className='text-center'>
  107. <div className='text-2xl font-bold mb-2' style={{ color: 'white' }}>
  108. {renderQuota(userState?.user?.quota)}
  109. </div>
  110. <div className='flex items-center justify-center text-sm'>
  111. <Wallet size={14} className="mr-1" style={{ color: 'rgba(255,255,255,0.8)' }} />
  112. <Text style={{ color: 'rgba(255,255,255,0.8)', fontSize: '12px' }}>{t('当前余额')}</Text>
  113. </div>
  114. </div>
  115. {/* 历史消耗 */}
  116. <div className='text-center'>
  117. <div className='text-2xl font-bold mb-2' style={{ color: 'white' }}>
  118. {renderQuota(userState?.user?.used_quota)}
  119. </div>
  120. <div className='flex items-center justify-center text-sm'>
  121. <TrendingUp size={14} className="mr-1" style={{ color: 'rgba(255,255,255,0.8)' }} />
  122. <Text style={{ color: 'rgba(255,255,255,0.8)', fontSize: '12px' }}>{t('历史消耗')}</Text>
  123. </div>
  124. </div>
  125. {/* 请求次数 */}
  126. <div className='text-center'>
  127. <div className='text-2xl font-bold mb-2' style={{ color: 'white' }}>
  128. {userState?.user?.request_count || 0}
  129. </div>
  130. <div className='flex items-center justify-center text-sm'>
  131. <BarChart2 size={14} className="mr-1" style={{ color: 'rgba(255,255,255,0.8)' }} />
  132. <Text style={{ color: 'rgba(255,255,255,0.8)', fontSize: '12px' }}>{t('请求次数')}</Text>
  133. </div>
  134. </div>
  135. </div>
  136. </div>
  137. </div>
  138. }
  139. >
  140. {/* 在线充值表单 */}
  141. {statusLoading ? (
  142. <div className='py-8 flex justify-center'>
  143. <Spin size="large" />
  144. </div>
  145. ) : (enableOnlineTopUp || enableStripeTopUp) ? (
  146. <Form
  147. getFormApi={(api) => (onlineFormApiRef.current = api)}
  148. initValues={{ topUpCount: topUpCount }}
  149. >
  150. <div className='space-y-6'>
  151. {(enableOnlineTopUp || enableStripeTopUp) && (
  152. <Row gutter={12}>
  153. <Col xs={24} sm={24} md={24} lg={10} xl={10}>
  154. <Form.InputNumber
  155. field='topUpCount'
  156. label={t('充值数量')}
  157. disabled={!enableOnlineTopUp && !enableStripeTopUp}
  158. placeholder={t('充值数量,最低 ') + renderQuotaWithAmount(minTopUp)}
  159. value={topUpCount}
  160. min={minTopUp}
  161. max={999999999}
  162. step={1}
  163. precision={0}
  164. onChange={async (value) => {
  165. if (value && value >= 1) {
  166. setTopUpCount(value);
  167. setSelectedPreset(null);
  168. await getAmount(value);
  169. }
  170. }}
  171. onBlur={(e) => {
  172. const value = parseInt(e.target.value);
  173. if (!value || value < 1) {
  174. setTopUpCount(1);
  175. getAmount(1);
  176. }
  177. }}
  178. formatter={(value) => (value ? `${value}` : '')}
  179. parser={(value) => value ? parseInt(value.replace(/[^\d]/g, '')) : 0}
  180. extraText={
  181. <Skeleton
  182. loading={showAmountSkeleton}
  183. active
  184. placeholder={<Skeleton.Title style={{ width: 120, height: 20, borderRadius: 6 }} />}
  185. >
  186. <Text type="secondary" className='text-red-600'>
  187. {t('实付金额:')}<span style={{ color: 'red' }}>{renderAmount()}</span>
  188. </Text>
  189. </Skeleton>
  190. }
  191. style={{ width: '100%' }}
  192. />
  193. </Col>
  194. <Col xs={24} sm={24} md={24} lg={14} xl={14}>
  195. <Form.Slot label={t('选择支付方式')}>
  196. <Space wrap>
  197. {payMethods.map((payMethod) => (
  198. <Button
  199. key={payMethod.type}
  200. theme='outline'
  201. type='tertiary'
  202. onClick={() => preTopUp(payMethod.type)}
  203. disabled={(!enableOnlineTopUp && payMethod.type !== 'stripe') ||
  204. (!enableStripeTopUp && payMethod.type === 'stripe')}
  205. loading={paymentLoading && payWay === payMethod.type}
  206. icon={
  207. payMethod.type === 'alipay' ? (
  208. <SiAlipay size={18} color="#1677FF" />
  209. ) : payMethod.type === 'wxpay' ? (
  210. <SiWechat size={18} color="#07C160" />
  211. ) : payMethod.type === 'stripe' ? (
  212. <SiStripe size={18} color="#635BFF" />
  213. ) : (
  214. <CreditCard size={18} color={payMethod.color || 'var(--semi-color-text-2)'} />
  215. )
  216. }
  217. >
  218. {payMethod.name}
  219. </Button>
  220. ))}
  221. </Space>
  222. </Form.Slot>
  223. </Col>
  224. </Row>
  225. )}
  226. {(enableOnlineTopUp || enableStripeTopUp) && (
  227. <Form.Slot label={t('选择充值额度')}>
  228. <Space wrap>
  229. {presetAmounts.map((preset, index) => (
  230. <Button
  231. key={index}
  232. theme={selectedPreset === preset.value ? 'solid' : 'outline'}
  233. type={selectedPreset === preset.value ? 'primary' : 'tertiary'}
  234. onClick={() => {
  235. selectPresetAmount(preset);
  236. onlineFormApiRef.current?.setValue('topUpCount', preset.value);
  237. }}
  238. className='!rounded-lg !py-2 !px-3'
  239. >
  240. <div className='flex items-center gap-2'>
  241. <Coins size={14} className='opacity-80' />
  242. <span className='font-medium'>{formatLargeNumber(preset.value)}</span>
  243. <span className='text-xs text-gray-500'>¥{(preset.value * priceRatio).toFixed(2)}</span>
  244. </div>
  245. </Button>
  246. ))}
  247. </Space>
  248. </Form.Slot>
  249. )}
  250. </div>
  251. </Form>
  252. ) : (
  253. <Banner
  254. type='info'
  255. description={t('管理员未开启在线充值功能,请联系管理员开启或使用兑换码充值。')}
  256. className='!rounded-xl'
  257. closeIcon={null}
  258. />
  259. )}
  260. </Card>
  261. {/* 兑换码充值 */}
  262. <Card
  263. className='!rounded-xl w-full'
  264. title={
  265. <Text type='tertiary' strong>
  266. {t('兑换码充值')}
  267. </Text>
  268. }
  269. >
  270. <Form
  271. getFormApi={(api) => (redeemFormApiRef.current = api)}
  272. initValues={{ redemptionCode: redemptionCode }}
  273. >
  274. <Form.Input
  275. field='redemptionCode'
  276. noLabel={true}
  277. placeholder={t('请输入兑换码')}
  278. value={redemptionCode}
  279. onChange={(value) => setRedemptionCode(value)}
  280. prefix={<IconGift />}
  281. suffix={
  282. <div className='flex items-center gap-2'>
  283. <Button
  284. type='primary'
  285. theme='solid'
  286. onClick={topUp}
  287. loading={isSubmitting}
  288. >
  289. {t('兑换额度')}
  290. </Button>
  291. </div>
  292. }
  293. showClear
  294. style={{ width: '100%' }}
  295. extraText={topUpLink && (
  296. <Text type="tertiary">
  297. {t('在找兑换码?')}
  298. <Text
  299. type="secondary"
  300. underline
  301. className="cursor-pointer"
  302. onClick={openTopUpLink}
  303. >
  304. {t('购买兑换码')}
  305. </Text>
  306. </Text>
  307. )}
  308. />
  309. </Form>
  310. </Card>
  311. </Space>
  312. </Card>
  313. );
  314. };
  315. export default RechargeCard;