ProviderList.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. 'use client';
  2. import { useState, useMemo } from 'react';
  3. import {
  4. Edit3,
  5. QrCode,
  6. Trash2,
  7. Coins,
  8. Search,
  9. LayoutGrid,
  10. List,
  11. Link as LinkIcon,
  12. Copy,
  13. Check,
  14. ToggleLeft,
  15. ToggleRight,
  16. Loader2,
  17. Plus,
  18. Layers,
  19. CreditCard
  20. } from 'lucide-react';
  21. import { Provider } from '@/types';
  22. import { getPaymentProviderIcon } from './paymentIcons';
  23. interface ProviderListProps {
  24. providers: Provider[];
  25. loading: boolean;
  26. onEdit: (provider: Provider) => void;
  27. onManageQr: (provider: Provider) => void;
  28. onDelete: (provider: Provider) => void;
  29. onToggleStatus?: (provider: Provider) => Promise<void>;
  30. onCreateNew?: () => void;
  31. }
  32. function ProviderLogo({ name, icon, size = 'md' }: { name: string; icon?: string; size?: 'sm' | 'md' | 'lg' }) {
  33. const [failed, setFailed] = useState(false);
  34. const resolvedIcon = getPaymentProviderIcon(name, icon);
  35. const containerClasses = {
  36. sm: 'w-7 h-7 border border-black p-0.5',
  37. md: 'w-9 h-9 border-2 border-black p-1 shadow-[1px_1px_0px_#000]',
  38. lg: 'w-10 h-10 border-2 border-black p-1 shadow-[2px_2px_0px_#000]'
  39. }[size];
  40. return (
  41. <div className={`${containerClasses} bg-white flex items-center justify-center shrink-0 overflow-hidden`}>
  42. {resolvedIcon && !failed ? (
  43. <img
  44. src={resolvedIcon}
  45. alt={name}
  46. className="w-full h-full object-contain"
  47. onError={() => setFailed(true)}
  48. />
  49. ) : (
  50. <div className="w-full h-full bg-neutral-100 flex items-center justify-center font-black text-black text-xs uppercase">
  51. {name ? name.slice(0, 2) : <CreditCard size={14} />}
  52. </div>
  53. )}
  54. </div>
  55. );
  56. }
  57. export default function ProviderList({
  58. providers,
  59. loading,
  60. onEdit,
  61. onManageQr,
  62. onDelete,
  63. onToggleStatus,
  64. onCreateNew
  65. }: ProviderListProps) {
  66. const [viewMode, setViewMode] = useState<'grid' | 'table'>('grid');
  67. const [searchQuery, setSearchQuery] = useState('');
  68. const [statusFilter, setStatusFilter] = useState<'all' | 'active' | 'disabled'>('all');
  69. const [channelFilter, setChannelFilter] = useState<'all' | 'qr_static' | 'online_link'>('all');
  70. const [copiedCode, setCopiedCode] = useState<string | null>(null);
  71. const [togglingId, setTogglingId] = useState<number | null>(null);
  72. const handleCopyCode = (code: string) => {
  73. navigator.clipboard.writeText(code);
  74. setCopiedCode(code);
  75. setTimeout(() => setCopiedCode(null), 2000);
  76. };
  77. const handleToggle = async (provider: Provider) => {
  78. if (!onToggleStatus) return;
  79. setTogglingId(provider.id);
  80. try {
  81. await onToggleStatus(provider);
  82. } finally {
  83. setTogglingId(null);
  84. }
  85. };
  86. const filteredProviders = useMemo(() => {
  87. return providers.filter(p => {
  88. const q = searchQuery.toLowerCase().trim();
  89. const matchSearch = !q ||
  90. p.name.toLowerCase().includes(q) ||
  91. (p.title && p.title.toLowerCase().includes(q)) ||
  92. (p.currency && p.currency.toLowerCase().includes(q));
  93. const isEnabled = Boolean(p.enabled);
  94. const matchStatus =
  95. statusFilter === 'all' ||
  96. (statusFilter === 'active' && isEnabled) ||
  97. (statusFilter === 'disabled' && !isEnabled);
  98. const matchChannel =
  99. channelFilter === 'all' ||
  100. p.channel === channelFilter;
  101. return matchSearch && matchStatus && matchChannel;
  102. });
  103. }, [providers, searchQuery, statusFilter, channelFilter]);
  104. const renderChannelBadge = (channel: string) => {
  105. if (channel === 'qr_static') {
  106. return (
  107. <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-neutral-100 text-[10px] font-bold uppercase text-black">
  108. <QrCode size={11} />
  109. 静态收款码
  110. </span>
  111. );
  112. }
  113. return (
  114. <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-amber-100 text-[10px] font-bold uppercase text-black">
  115. <LinkIcon size={11} />
  116. 在线直连网关
  117. </span>
  118. );
  119. };
  120. const renderCurrencyBadge = (currency: string) => (
  121. <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-white text-[10px] font-black text-black">
  122. <Coins size={11} />
  123. {currency || 'CNY'}
  124. </span>
  125. );
  126. const renderFeeBadge = (p: Provider) => {
  127. const cfg = p.config;
  128. if (!cfg || cfg.fee_rate === undefined) return null;
  129. const rate = Number(cfg.fee_rate);
  130. const fixed = Number(cfg.fee_fixed || 0);
  131. const isFree = rate === 0 && fixed === 0;
  132. return isFree ? (
  133. <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-emerald-100 text-[10px] font-bold text-emerald-900">
  134. 免手续费
  135. </span>
  136. ) : (
  137. <span className="inline-flex items-center gap-1 px-2 py-0.5 border border-black bg-amber-100 text-[10px] font-bold text-amber-950">
  138. 手续费: {cfg.fee_label || `${(rate * 100).toFixed(1)}%`}
  139. </span>
  140. );
  141. };
  142. return (
  143. <div className="space-y-4 font-mono">
  144. {/* 顶部工具栏 */}
  145. <div className="bg-white p-4 border-2 border-black shadow-[4px_4px_0px_#000] flex flex-col md:flex-row md:items-center justify-between gap-3">
  146. {/* 搜索框 */}
  147. <div className="relative flex-1 max-w-md">
  148. <Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-black pointer-events-none" />
  149. <input
  150. type="text"
  151. placeholder="搜索渠道名称 / 标识 / 币种..."
  152. value={searchQuery}
  153. onChange={e => setSearchQuery(e.target.value)}
  154. className="w-full pl-9 pr-8 py-2 text-xs bg-neutral-50 border-2 border-black focus:bg-white outline-none shadow-[2px_2px_0px_#000] text-black font-mono"
  155. />
  156. {searchQuery && (
  157. <button
  158. onClick={() => setSearchQuery('')}
  159. className="absolute right-2.5 top-1/2 -translate-y-1/2 text-xs text-black hover:text-rose-600 font-bold"
  160. >
  161. ✕
  162. </button>
  163. )}
  164. </div>
  165. {/* 筛选与视图切换 */}
  166. <div className="flex items-center gap-2 flex-wrap">
  167. <select
  168. value={channelFilter}
  169. onChange={e => setChannelFilter(e.target.value as any)}
  170. className="text-xs font-bold bg-neutral-50 border-2 border-black px-2.5 py-1.5 text-black outline-none shadow-[2px_2px_0px_#000]"
  171. >
  172. <option value="all">[ 全部通道类型 ]</option>
  173. <option value="qr_static">静态二维码 (QR)</option>
  174. <option value="online_link">在线直连 (Link)</option>
  175. </select>
  176. <select
  177. value={statusFilter}
  178. onChange={e => setStatusFilter(e.target.value as any)}
  179. className="text-xs font-bold bg-neutral-50 border-2 border-black px-2.5 py-1.5 text-black outline-none shadow-[2px_2px_0px_#000]"
  180. >
  181. <option value="all">[ 全部状态 ]</option>
  182. <option value="active">已启用</option>
  183. <option value="disabled">已禁用</option>
  184. </select>
  185. <div className="flex items-center border-2 border-black bg-neutral-100 shadow-[2px_2px_0px_#000]">
  186. <button
  187. onClick={() => setViewMode('grid')}
  188. className={`p-1.5 transition ${viewMode === 'grid' ? 'bg-amber-300 text-black font-bold' : 'text-neutral-500 hover:text-black'}`}
  189. title="卡片视图"
  190. >
  191. <LayoutGrid size={14} />
  192. </button>
  193. <button
  194. onClick={() => setViewMode('table')}
  195. className={`p-1.5 transition ${viewMode === 'table' ? 'bg-amber-300 text-black font-bold' : 'text-neutral-500 hover:text-black'}`}
  196. title="表格视图"
  197. >
  198. <List size={14} />
  199. </button>
  200. </div>
  201. </div>
  202. </div>
  203. {/* 内容区域 */}
  204. {loading ? (
  205. <div className="bg-white p-12 text-center border-2 border-black shadow-[4px_4px_0px_#000]">
  206. <Loader2 className="animate-spin text-black mx-auto mb-2" size={24} />
  207. <p className="text-xs font-black uppercase">[ FETCHING PAYMENT GATEWAYS... ]</p>
  208. </div>
  209. ) : filteredProviders.length === 0 ? (
  210. <div className="bg-white p-12 text-center border-2 border-black shadow-[4px_4px_0px_#000] space-y-3">
  211. <div className="w-10 h-10 bg-neutral-100 border-2 border-black flex items-center justify-center mx-auto shadow-[2px_2px_0px_#000] text-black">
  212. <Layers size={18} />
  213. </div>
  214. <h3 className="text-sm font-black text-black uppercase">[ NO GATEWAYS FOUND ]</h3>
  215. <p className="text-xs text-neutral-500 font-sans">未检索到匹配的支付渠道配置</p>
  216. </div>
  217. ) : viewMode === 'grid' ? (
  218. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
  219. {filteredProviders.map((p) => {
  220. const isEnabled = Boolean(p.enabled);
  221. const isToggling = togglingId === p.id;
  222. return (
  223. <div
  224. key={p.id}
  225. className={`bg-white border-2 border-black shadow-[4px_4px_0px_#000] flex flex-col justify-between transition ${
  226. isEnabled ? '' : 'bg-neutral-100 opacity-80'
  227. }`}
  228. >
  229. {/* 卡片头部 */}
  230. <div className="p-4 space-y-3">
  231. <div className="flex items-start justify-between gap-2">
  232. <div className="flex items-center gap-2.5 min-w-0">
  233. <ProviderLogo name={p.name} icon={p.icon} size="lg" />
  234. <div className="min-w-0">
  235. <h4 className="text-sm font-black text-black truncate font-sans" title={p.title || p.name}>
  236. {p.title || p.name}
  237. </h4>
  238. <div className="flex items-center gap-1 text-[10px] text-neutral-500 font-mono mt-0.5">
  239. <span>{p.name}</span>
  240. <button
  241. onClick={() => handleCopyCode(p.name)}
  242. className="hover:text-black"
  243. title="复制标识代码"
  244. >
  245. {copiedCode === p.name ? <Check size={10} className="text-black" /> : <Copy size={10} />}
  246. </button>
  247. </div>
  248. </div>
  249. </div>
  250. <button
  251. onClick={() => handleToggle(p)}
  252. disabled={isToggling || !onToggleStatus}
  253. className={`p-1 border border-black text-xs font-bold ${
  254. isEnabled ? 'bg-amber-300 text-black' : 'bg-neutral-200 text-neutral-500'
  255. }`}
  256. title={isEnabled ? '点击禁用' : '点击启用'}
  257. >
  258. {isToggling ? <Loader2 size={14} className="animate-spin" /> : isEnabled ? 'ON' : 'OFF'}
  259. </button>
  260. </div>
  261. <div className="pt-2 border-t border-dashed border-neutral-300 flex items-center gap-1.5 flex-wrap">
  262. {renderChannelBadge(p.channel)}
  263. {renderCurrencyBadge(p.currency)}
  264. {renderFeeBadge(p)}
  265. </div>
  266. </div>
  267. {/* 卡片底部操作栏 */}
  268. <div className="px-4 py-2.5 bg-neutral-100 border-t-2 border-black flex items-center justify-between gap-2 text-xs">
  269. <div>
  270. {p.channel === 'qr_static' ? (
  271. <button
  272. onClick={() => onManageQr(p)}
  273. className="px-2.5 py-1 bg-white hover:bg-amber-300 text-black border border-black font-bold text-[11px] shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 flex items-center gap-1 uppercase"
  274. >
  275. <QrCode size={12} />
  276. <span>[ 码池 ]</span>
  277. </button>
  278. ) : (
  279. <span className="text-[10px] text-neutral-500 font-bold uppercase">
  280. ONLINE GATEWAY
  281. </span>
  282. )}
  283. </div>
  284. <div className="flex items-center gap-1.5">
  285. <button
  286. onClick={() => onEdit(p)}
  287. className="px-2 py-1 bg-white hover:bg-neutral-200 text-black border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
  288. title="编辑配置"
  289. >
  290. [ 编辑 ]
  291. </button>
  292. <button
  293. onClick={() => onDelete(p)}
  294. className="px-2 py-1 bg-rose-100 hover:bg-rose-200 text-rose-800 border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
  295. title="删除渠道"
  296. >
  297. [ 删除 ]
  298. </button>
  299. </div>
  300. </div>
  301. </div>
  302. );
  303. })}
  304. </div>
  305. ) : (
  306. /* 表格视图 */
  307. <div className="bg-white border-2 border-black shadow-[4px_4px_0px_#000] overflow-hidden">
  308. <div className="overflow-x-auto">
  309. <table className="min-w-full divide-y-2 divide-black text-left">
  310. <thead className="bg-neutral-100 text-xs font-black text-black uppercase">
  311. <tr>
  312. <th className="px-4 py-3">[ 服务商 / 标识 ]</th>
  313. <th className="px-4 py-3">[ 通道类型 ]</th>
  314. <th className="px-4 py-3">[ 结算货币 ]</th>
  315. <th className="px-4 py-3">[ 交易手续费 ]</th>
  316. <th className="px-4 py-3">[ 状态 ]</th>
  317. <th className="px-4 py-3 text-right">[ 操作 ]</th>
  318. </tr>
  319. </thead>
  320. <tbody className="divide-y divide-black text-xs">
  321. {filteredProviders.map((p) => {
  322. const isEnabled = Boolean(p.enabled);
  323. const isToggling = togglingId === p.id;
  324. return (
  325. <tr key={p.id} className="hover:bg-amber-50/70">
  326. <td className="px-4 py-3">
  327. <div className="flex items-center gap-2.5">
  328. <ProviderLogo name={p.name} icon={p.icon} size="sm" />
  329. <div>
  330. <div className="font-bold text-black font-sans">{p.title || p.name}</div>
  331. <div className="text-[10px] font-mono text-neutral-500">{p.name}</div>
  332. </div>
  333. </div>
  334. </td>
  335. <td className="px-4 py-3 whitespace-nowrap">
  336. {renderChannelBadge(p.channel)}
  337. </td>
  338. <td className="px-4 py-3 whitespace-nowrap">
  339. {renderCurrencyBadge(p.currency)}
  340. </td>
  341. <td className="px-4 py-3 whitespace-nowrap">
  342. {renderFeeBadge(p)}
  343. </td>
  344. <td className="px-4 py-3 whitespace-nowrap">
  345. <button
  346. onClick={() => handleToggle(p)}
  347. disabled={isToggling || !onToggleStatus}
  348. className={`px-2 py-0.5 border border-black text-[10px] font-black ${
  349. isEnabled ? 'bg-amber-300 text-black' : 'bg-neutral-200 text-neutral-500'
  350. }`}
  351. >
  352. {isToggling ? <Loader2 size={12} className="animate-spin" /> : isEnabled ? '已启用' : '已禁用'}
  353. </button>
  354. </td>
  355. <td className="px-4 py-3 text-right whitespace-nowrap">
  356. <div className="inline-flex items-center gap-1.5">
  357. {p.channel === 'qr_static' && (
  358. <button
  359. onClick={() => onManageQr(p)}
  360. className="px-2 py-1 bg-white hover:bg-amber-300 text-black border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
  361. >
  362. 码池
  363. </button>
  364. )}
  365. <button
  366. onClick={() => onEdit(p)}
  367. className="px-2 py-1 bg-white hover:bg-neutral-200 text-black border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
  368. >
  369. 编辑
  370. </button>
  371. <button
  372. onClick={() => onDelete(p)}
  373. className="px-2 py-1 bg-rose-100 hover:bg-rose-200 text-rose-800 border border-black text-[11px] font-bold shadow-[1px_1px_0px_#000] active:translate-x-0.5 active:translate-y-0.5 uppercase"
  374. >
  375. 删除
  376. </button>
  377. </div>
  378. </td>
  379. </tr>
  380. );
  381. })}
  382. </tbody>
  383. </table>
  384. </div>
  385. </div>
  386. )}
  387. </div>
  388. );
  389. }