ProviderList.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. return (
  127. <div className="space-y-4 font-mono">
  128. {/* 顶部工具栏 */}
  129. <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">
  130. {/* 搜索框 */}
  131. <div className="relative flex-1 max-w-md">
  132. <Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-black pointer-events-none" />
  133. <input
  134. type="text"
  135. placeholder="搜索渠道名称 / 标识 / 币种..."
  136. value={searchQuery}
  137. onChange={e => setSearchQuery(e.target.value)}
  138. 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"
  139. />
  140. {searchQuery && (
  141. <button
  142. onClick={() => setSearchQuery('')}
  143. className="absolute right-2.5 top-1/2 -translate-y-1/2 text-xs text-black hover:text-rose-600 font-bold"
  144. >
  145. ✕
  146. </button>
  147. )}
  148. </div>
  149. {/* 筛选与视图切换 */}
  150. <div className="flex items-center gap-2 flex-wrap">
  151. <select
  152. value={channelFilter}
  153. onChange={e => setChannelFilter(e.target.value as any)}
  154. 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]"
  155. >
  156. <option value="all">[ 全部通道类型 ]</option>
  157. <option value="qr_static">静态二维码 (QR)</option>
  158. <option value="online_link">在线直连 (Link)</option>
  159. </select>
  160. <select
  161. value={statusFilter}
  162. onChange={e => setStatusFilter(e.target.value as any)}
  163. 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]"
  164. >
  165. <option value="all">[ 全部状态 ]</option>
  166. <option value="active">已启用</option>
  167. <option value="disabled">已禁用</option>
  168. </select>
  169. <div className="flex items-center border-2 border-black bg-neutral-100 shadow-[2px_2px_0px_#000]">
  170. <button
  171. onClick={() => setViewMode('grid')}
  172. className={`p-1.5 transition ${viewMode === 'grid' ? 'bg-amber-300 text-black font-bold' : 'text-neutral-500 hover:text-black'}`}
  173. title="卡片视图"
  174. >
  175. <LayoutGrid size={14} />
  176. </button>
  177. <button
  178. onClick={() => setViewMode('table')}
  179. className={`p-1.5 transition ${viewMode === 'table' ? 'bg-amber-300 text-black font-bold' : 'text-neutral-500 hover:text-black'}`}
  180. title="表格视图"
  181. >
  182. <List size={14} />
  183. </button>
  184. </div>
  185. </div>
  186. </div>
  187. {/* 内容区域 */}
  188. {loading ? (
  189. <div className="bg-white p-12 text-center border-2 border-black shadow-[4px_4px_0px_#000]">
  190. <Loader2 className="animate-spin text-black mx-auto mb-2" size={24} />
  191. <p className="text-xs font-black uppercase">[ FETCHING PAYMENT GATEWAYS... ]</p>
  192. </div>
  193. ) : filteredProviders.length === 0 ? (
  194. <div className="bg-white p-12 text-center border-2 border-black shadow-[4px_4px_0px_#000] space-y-3">
  195. <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">
  196. <Layers size={18} />
  197. </div>
  198. <h3 className="text-sm font-black text-black uppercase">[ NO GATEWAYS FOUND ]</h3>
  199. <p className="text-xs text-neutral-500 font-sans">未检索到匹配的支付渠道配置</p>
  200. </div>
  201. ) : viewMode === 'grid' ? (
  202. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
  203. {filteredProviders.map((p) => {
  204. const isEnabled = Boolean(p.enabled);
  205. const isToggling = togglingId === p.id;
  206. return (
  207. <div
  208. key={p.id}
  209. className={`bg-white border-2 border-black shadow-[4px_4px_0px_#000] flex flex-col justify-between transition ${
  210. isEnabled ? '' : 'bg-neutral-100 opacity-80'
  211. }`}
  212. >
  213. {/* 卡片头部 */}
  214. <div className="p-4 space-y-3">
  215. <div className="flex items-start justify-between gap-2">
  216. <div className="flex items-center gap-2.5 min-w-0">
  217. <ProviderLogo name={p.name} icon={p.icon} size="lg" />
  218. <div className="min-w-0">
  219. <h4 className="text-sm font-black text-black truncate font-sans" title={p.title || p.name}>
  220. {p.title || p.name}
  221. </h4>
  222. <div className="flex items-center gap-1 text-[10px] text-neutral-500 font-mono mt-0.5">
  223. <span>{p.name}</span>
  224. <button
  225. onClick={() => handleCopyCode(p.name)}
  226. className="hover:text-black"
  227. title="复制标识代码"
  228. >
  229. {copiedCode === p.name ? <Check size={10} className="text-black" /> : <Copy size={10} />}
  230. </button>
  231. </div>
  232. </div>
  233. </div>
  234. <button
  235. onClick={() => handleToggle(p)}
  236. disabled={isToggling || !onToggleStatus}
  237. className={`p-1 border border-black text-xs font-bold ${
  238. isEnabled ? 'bg-amber-300 text-black' : 'bg-neutral-200 text-neutral-500'
  239. }`}
  240. title={isEnabled ? '点击禁用' : '点击启用'}
  241. >
  242. {isToggling ? <Loader2 size={14} className="animate-spin" /> : isEnabled ? 'ON' : 'OFF'}
  243. </button>
  244. </div>
  245. <div className="pt-2 border-t border-dashed border-neutral-300 flex items-center gap-1.5 flex-wrap">
  246. {renderChannelBadge(p.channel)}
  247. {renderCurrencyBadge(p.currency)}
  248. </div>
  249. </div>
  250. {/* 卡片底部操作栏 */}
  251. <div className="px-4 py-2.5 bg-neutral-100 border-t-2 border-black flex items-center justify-between gap-2 text-xs">
  252. <div>
  253. {p.channel === 'qr_static' ? (
  254. <button
  255. onClick={() => onManageQr(p)}
  256. 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"
  257. >
  258. <QrCode size={12} />
  259. <span>[ 码池 ]</span>
  260. </button>
  261. ) : (
  262. <span className="text-[10px] text-neutral-500 font-bold uppercase">
  263. ONLINE GATEWAY
  264. </span>
  265. )}
  266. </div>
  267. <div className="flex items-center gap-1.5">
  268. <button
  269. onClick={() => onEdit(p)}
  270. 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"
  271. title="编辑配置"
  272. >
  273. [ 编辑 ]
  274. </button>
  275. <button
  276. onClick={() => onDelete(p)}
  277. 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"
  278. title="删除渠道"
  279. >
  280. [ 删除 ]
  281. </button>
  282. </div>
  283. </div>
  284. </div>
  285. );
  286. })}
  287. </div>
  288. ) : (
  289. /* 表格视图 */
  290. <div className="bg-white border-2 border-black shadow-[4px_4px_0px_#000] overflow-hidden">
  291. <div className="overflow-x-auto">
  292. <table className="min-w-full divide-y-2 divide-black text-left">
  293. <thead className="bg-neutral-100 text-xs font-black text-black uppercase">
  294. <tr>
  295. <th className="px-4 py-3">[ 服务商 / 标识 ]</th>
  296. <th className="px-4 py-3">[ 通道类型 ]</th>
  297. <th className="px-4 py-3">[ 结算货币 ]</th>
  298. <th className="px-4 py-3">[ 状态 ]</th>
  299. <th className="px-4 py-3 text-right">[ 操作 ]</th>
  300. </tr>
  301. </thead>
  302. <tbody className="divide-y divide-black text-xs">
  303. {filteredProviders.map((p) => {
  304. const isEnabled = Boolean(p.enabled);
  305. const isToggling = togglingId === p.id;
  306. return (
  307. <tr key={p.id} className="hover:bg-amber-50/70">
  308. <td className="px-4 py-3">
  309. <div className="flex items-center gap-2.5">
  310. <ProviderLogo name={p.name} icon={p.icon} size="sm" />
  311. <div>
  312. <div className="font-bold text-black font-sans">{p.title || p.name}</div>
  313. <div className="text-[10px] font-mono text-neutral-500">{p.name}</div>
  314. </div>
  315. </div>
  316. </td>
  317. <td className="px-4 py-3 whitespace-nowrap">
  318. {renderChannelBadge(p.channel)}
  319. </td>
  320. <td className="px-4 py-3 whitespace-nowrap">
  321. {renderCurrencyBadge(p.currency)}
  322. </td>
  323. <td className="px-4 py-3 whitespace-nowrap">
  324. <button
  325. onClick={() => handleToggle(p)}
  326. disabled={isToggling || !onToggleStatus}
  327. className={`px-2 py-0.5 border border-black text-[10px] font-black ${
  328. isEnabled ? 'bg-amber-300 text-black' : 'bg-neutral-200 text-neutral-500'
  329. }`}
  330. >
  331. {isToggling ? <Loader2 size={12} className="animate-spin" /> : isEnabled ? '已启用' : '已禁用'}
  332. </button>
  333. </td>
  334. <td className="px-4 py-3 text-right whitespace-nowrap">
  335. <div className="inline-flex items-center gap-1.5">
  336. {p.channel === 'qr_static' && (
  337. <button
  338. onClick={() => onManageQr(p)}
  339. 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"
  340. >
  341. 码池
  342. </button>
  343. )}
  344. <button
  345. onClick={() => onEdit(p)}
  346. 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"
  347. >
  348. 编辑
  349. </button>
  350. <button
  351. onClick={() => onDelete(p)}
  352. 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"
  353. >
  354. 删除
  355. </button>
  356. </div>
  357. </td>
  358. </tr>
  359. );
  360. })}
  361. </tbody>
  362. </table>
  363. </div>
  364. </div>
  365. )}
  366. </div>
  367. );
  368. }