ProviderModal.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. 'use client';
  2. import { useState, useEffect } from 'react';
  3. import {
  4. Loader2,
  5. X,
  6. Sparkles,
  7. CreditCard,
  8. QrCode,
  9. Globe,
  10. Layers
  11. } from 'lucide-react';
  12. import { Provider } from '@/types';
  13. import { toast } from 'react-hot-toast';
  14. import { DEFAULT_PAYMENT_ICONS, getPaymentProviderIcon } from './paymentIcons';
  15. import { ImageUploadField } from '@/components/common/ImageUploadField';
  16. interface ProviderModalProps {
  17. isOpen: boolean;
  18. onClose: () => void;
  19. provider?: Provider | null;
  20. onSubmit: (data: any) => Promise<void>;
  21. }
  22. const PAYMENT_PRESETS = [
  23. {
  24. name: 'wechat',
  25. title: '微信支付 (WeChat Pay)',
  26. channel: 'qr_static',
  27. currency: 'CNY',
  28. icon: DEFAULT_PAYMENT_ICONS.wechat,
  29. },
  30. {
  31. name: 'alipay',
  32. title: '支付宝 (Alipay)',
  33. channel: 'qr_static',
  34. currency: 'CNY',
  35. icon: DEFAULT_PAYMENT_ICONS.alipay,
  36. },
  37. {
  38. name: 'creem',
  39. title: 'Creem (AI Agent Payment)',
  40. channel: 'online_link',
  41. currency: 'USD',
  42. icon: DEFAULT_PAYMENT_ICONS.creem,
  43. },
  44. {
  45. name: 'stripe',
  46. title: 'Stripe 国际信用卡',
  47. channel: 'online_link',
  48. currency: 'USD',
  49. icon: DEFAULT_PAYMENT_ICONS.stripe,
  50. },
  51. {
  52. name: 'paypal',
  53. title: 'PayPal 快捷支付',
  54. channel: 'online_link',
  55. currency: 'USD',
  56. icon: DEFAULT_PAYMENT_ICONS.paypal,
  57. },
  58. {
  59. name: 'usdt_trc20',
  60. title: 'USDT (TRC20 静态地址)',
  61. channel: 'qr_static',
  62. currency: 'USD',
  63. icon: DEFAULT_PAYMENT_ICONS.usdt_trc20,
  64. },
  65. {
  66. name: 'bank_transfer',
  67. title: '银行转账 (对公/对私)',
  68. channel: 'qr_static',
  69. currency: 'CNY',
  70. icon: DEFAULT_PAYMENT_ICONS.bank_transfer,
  71. }
  72. ];
  73. export default function ProviderModal({ isOpen, onClose, provider, onSubmit }: ProviderModalProps) {
  74. const [loading, setLoading] = useState(false);
  75. const [form, setForm] = useState({
  76. name: '',
  77. title: '',
  78. icon: '',
  79. currency: 'CNY',
  80. channel: 'qr_static',
  81. enabled: 1
  82. });
  83. const isEditing = Boolean(provider?.id);
  84. useEffect(() => {
  85. if (isOpen) {
  86. if (provider) {
  87. setForm({
  88. name: provider.name || '',
  89. title: provider.title || '',
  90. icon: getPaymentProviderIcon(provider.name, provider.icon),
  91. currency: provider.currency || 'CNY',
  92. channel: provider.channel || 'qr_static',
  93. enabled: Number(provider.enabled ?? 1)
  94. });
  95. } else {
  96. setForm({
  97. name: '',
  98. title: '',
  99. icon: '',
  100. currency: 'CNY',
  101. channel: 'qr_static',
  102. enabled: 1
  103. });
  104. }
  105. }
  106. }, [isOpen, provider]);
  107. const applyPreset = (preset: typeof PAYMENT_PRESETS[0]) => {
  108. setForm(prev => ({
  109. ...prev,
  110. name: preset.name,
  111. title: preset.title,
  112. icon: preset.icon,
  113. channel: preset.channel,
  114. currency: preset.currency
  115. }));
  116. };
  117. const handleNameChange = (val: string) => {
  118. const nextName = val.toLowerCase();
  119. if (!form.icon || Object.values(DEFAULT_PAYMENT_ICONS).includes(form.icon)) {
  120. const matched = getPaymentProviderIcon(nextName);
  121. setForm(prev => ({ ...prev, name: nextName, icon: matched || prev.icon }));
  122. } else {
  123. setForm(prev => ({ ...prev, name: nextName }));
  124. }
  125. };
  126. const handleSubmit = async (e: React.FormEvent) => {
  127. e.preventDefault();
  128. if (!form.name.trim() || !form.title.trim()) {
  129. toast.error('请填写内部标识和显示名称');
  130. return;
  131. }
  132. const finalIcon = form.icon || getPaymentProviderIcon(form.name);
  133. setLoading(true);
  134. try {
  135. await onSubmit({ ...form, icon: finalIcon, id: provider?.id });
  136. onClose();
  137. } catch {
  138. // ignore
  139. } finally {
  140. setLoading(false);
  141. }
  142. };
  143. if (!isOpen) return null;
  144. return (
  145. <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-grayscale p-4 font-mono">
  146. <div className="bg-neutral-50 border-2 border-black shadow-[8px_8px_0px_#000] w-full max-w-xl max-h-[90vh] flex flex-col overflow-hidden animate-in zoom-in-95 duration-200">
  147. {/* Header */}
  148. <div className="px-5 py-3.5 border-b-2 border-black flex justify-between items-center bg-neutral-200 text-black select-none">
  149. <div className="flex items-center gap-2 text-xs font-bold uppercase tracking-wider">
  150. <span className="w-2.5 h-2.5 bg-amber-300 border border-black inline-block" />
  151. <span>[ {isEditing ? 'EDIT GATEWAY' : 'NEW GATEWAY'} // 支付服务商 ]</span>
  152. </div>
  153. <button
  154. onClick={onClose}
  155. className="w-6 h-6 bg-white hover:bg-amber-300 border border-black flex items-center justify-center font-bold text-xs transition"
  156. >
  157. ✕
  158. </button>
  159. </div>
  160. {/* Form Content */}
  161. <form onSubmit={handleSubmit} className="p-5 overflow-y-auto space-y-4 bg-white flex-1">
  162. {/* Preset Buttons */}
  163. {!isEditing && (
  164. <div className="bg-neutral-50 p-3 border-2 border-black shadow-[2px_2px_0px_#000] space-y-2">
  165. <span className="text-xs font-black text-black uppercase block">
  166. [ 快速预设模板 // 一键填充 ]:
  167. </span>
  168. <div className="grid grid-cols-2 sm:grid-cols-3 gap-1.5">
  169. {PAYMENT_PRESETS.map((preset) => {
  170. const isSelected = form.name === preset.name;
  171. return (
  172. <button
  173. key={preset.name}
  174. type="button"
  175. onClick={() => applyPreset(preset)}
  176. className={`p-1.5 border text-xs font-bold transition active:translate-x-0.5 active:translate-y-0.5 truncate text-left ${
  177. isSelected
  178. ? 'bg-amber-300 border-black text-black shadow-[1px_1px_0px_#000]'
  179. : 'bg-white border-neutral-300 hover:border-black text-black'
  180. }`}
  181. >
  182. {preset.name}
  183. </button>
  184. );
  185. })}
  186. </div>
  187. </div>
  188. )}
  189. {/* Form Fields */}
  190. <div className="grid grid-cols-1 sm:grid-cols-2 gap-3 text-xs">
  191. <div className="space-y-1">
  192. <label className="font-bold text-black uppercase">[ 01 // 内部标识名 (Code) ]:</label>
  193. <input
  194. type="text"
  195. required
  196. disabled={isEditing}
  197. placeholder="例如: wechat, stripe"
  198. value={form.name}
  199. onChange={(e) => handleNameChange(e.target.value)}
  200. className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-mono focus:bg-white outline-none disabled:bg-neutral-200 text-black shadow-[2px_2px_0px_#000]"
  201. />
  202. </div>
  203. <div className="space-y-1">
  204. <label className="font-bold text-black uppercase">[ 02 // 前台显示名称 ]:</label>
  205. <input
  206. type="text"
  207. required
  208. placeholder="例如: 微信支付, 国际信用卡"
  209. value={form.title}
  210. onChange={(e) => setForm({ ...form, title: e.target.value })}
  211. className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-sans focus:bg-white outline-none text-black shadow-[2px_2px_0px_#000]"
  212. />
  213. </div>
  214. <div className="space-y-1">
  215. <label className="font-bold text-black uppercase">[ 03 // 结算币种 ]:</label>
  216. <input
  217. type="text"
  218. required
  219. placeholder="CNY, USD, EUR..."
  220. value={form.currency}
  221. onChange={(e) => setForm({ ...form, currency: e.target.value.toUpperCase() })}
  222. className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-mono font-bold focus:bg-white outline-none text-black shadow-[2px_2px_0px_#000]"
  223. />
  224. </div>
  225. <div className="space-y-1">
  226. <label className="font-bold text-black uppercase">[ 04 // 接入通道类型 ]:</label>
  227. <select
  228. value={form.channel}
  229. onChange={(e) => setForm({ ...form, channel: e.target.value })}
  230. className="w-full border-2 border-black bg-neutral-50 p-2 text-xs font-bold outline-none text-black shadow-[2px_2px_0px_#000]"
  231. >
  232. <option value="qr_static">静态二维码 (QR Static)</option>
  233. <option value="online_link">在线直连网关 (Online Link)</option>
  234. </select>
  235. </div>
  236. </div>
  237. <div className="space-y-1">
  238. <ImageUploadField
  239. label="[ 05 // 渠道图标(本地上传至 R2) ]:"
  240. value={form.icon || ''}
  241. onChange={(url) => setForm({ ...form, icon: url })}
  242. prefix="icons"
  243. aspectRatio="1:1"
  244. compact={true}
  245. helperText="选择本地 PNG, SVG, JPG 图标 (最大 1MB),留空默认使用内置矢量图标"
  246. />
  247. </div>
  248. <div className="pt-2 flex justify-end gap-3 border-t-2 border-dashed border-neutral-300">
  249. <button
  250. type="button"
  251. onClick={onClose}
  252. className="px-4 py-2 border-2 border-black bg-white hover:bg-neutral-100 text-black text-xs font-bold shadow-[2px_2px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
  253. >
  254. [ 取消 ]
  255. </button>
  256. <button
  257. type="submit"
  258. disabled={loading}
  259. className="px-6 py-2 bg-amber-300 hover:bg-amber-400 text-black border-2 border-black text-xs font-black shadow-[3px_3px_0px_#000] transition active:translate-x-0.5 active:translate-y-0.5 uppercase disabled:opacity-50"
  260. >
  261. {loading ? <Loader2 size={13} className="animate-spin" /> : null}
  262. <span>[ 保存渠道设置 ]</span>
  263. </button>
  264. </div>
  265. </form>
  266. </div>
  267. </div>
  268. );
  269. }