TransactionList.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <template>
  2. <cwg-load-more-wrapper ref="loadMoreWrapperRef" :loading="loading" :finished="finished" :height='108'
  3. :refresher-enabled="pageSize !== 4" @reach-bottom="loadMore" @refresh="handleRefresh">
  4. <view v-if="filteredRecords.length > 0" :class="{
  5. 'records-list': true,
  6. 'records-list1': pageSize === 4
  7. }">
  8. <view v-for="record in filteredRecords" :key="record.id" class="record-card"
  9. @click="goToTransactionDetail(record)">
  10. <view class="record-main">
  11. <view class="record-left">
  12. <view class="type-icon transaction-icon">
  13. <cwg-icon class="icons" :name="getTransactionIcon(record.type)" :size="20" color="#2563eb" />
  14. </view>
  15. <view class="record-info">
  16. <view class="info-header">
  17. <text class="record-type">{{ getTransactionTypeText(record.type) }}</text>
  18. <view :class="['status-badge', getStatusBadgeClass(record.status)]">
  19. <cwg-icon class="icons" :name="getStatusIcon(record.status)" :size="12"
  20. :color="getStatusColor(record.status)" />
  21. <text :class="['status-text', getStatusTextClass(record.status)]">
  22. {{ getStatusText(record.status) }}
  23. </text>
  24. </view>
  25. </view>
  26. <text class="record-detail">{{ record.remark || record.merchant || '--' }}</text>
  27. </view>
  28. </view>
  29. <view class="record-right">
  30. <text class="amount-transaction">{{ Number(record.amount || 0) >= 0 ? '+' : '-' }}{{
  31. Math.abs(Number(record.amount || 0)).toFixed(2) }} {{ record.currency || 'USD' }}</text>
  32. <text class="fee-text">{{ t('global.p17') }} {{ Number(record.fee || 0).toFixed(2) }}</text>
  33. </view>
  34. </view>
  35. <view class="record-footer">
  36. <text class="footer-time">{{ formatDateTime(record.transactionTime) }}</text>
  37. <view class="footer-actions">
  38. <text class="footer-order">
  39. {{ t('global.p15') }}: {{ formatOrderNo(record.tradeNo) }}
  40. </text>
  41. <cwg-icon class="footer-order-icon" name="copy" :size="14" color="#9ca3af"
  42. @click.stop="copyOrderNo(record.tradeNo)" />
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <cwg-empty-state v-else :text="t('empty-state.c2')" />
  48. </cwg-load-more-wrapper>
  49. </template>
  50. <script setup lang="ts">
  51. import { ref, computed, watch, onMounted } from 'vue';
  52. import dayjs from 'dayjs';
  53. import { useI18n } from 'vue-i18n';
  54. import { showToast } from '@/utils/toast';
  55. import { ucardApi, TransactionInfo } from '@/api/ucard';
  56. import { transactionTypeMap, transactionStatusMap } from '@/utils/dataMap';
  57. import useCardStore from '@/stores/use-card-store';
  58. interface RecordItem extends TransactionInfo {
  59. type?: string;
  60. typeStr?: string;
  61. remark?: string;
  62. status?: string | number;
  63. transactionTime?: string | number;
  64. fee?: number;
  65. currency?: string;
  66. tradeNo?: string;
  67. merchant?: string;
  68. }
  69. type NormalizedStatus = 'success' | 'processing' | 'failed';
  70. const props = defineProps<{
  71. cardNo: string;
  72. pageSize: number;
  73. typeIndex: number;
  74. statusIndex: number;
  75. dateFilter: string;
  76. typeOptions: string[];
  77. }>();
  78. const { t } = useI18n();
  79. const records = ref<RecordItem[]>([]);
  80. const page = ref(1);
  81. const pageSize = computed(() => props.pageSize || 10);
  82. const loading = ref(false);
  83. const finished = ref(false);
  84. const cardStore = useCardStore();
  85. const normalizeStatus = (status?: string | number): NormalizedStatus => {
  86. if (!status) return 'success';
  87. const statusStr = String(status).toLowerCase();
  88. if (statusStr === 'processing' || statusStr === 'wait_process') return 'processing';
  89. if (statusStr === 'fail' || statusStr === 'failed') return 'failed';
  90. if (statusStr === 'succeed' || statusStr === 'success') return 'success';
  91. return 'success';
  92. };
  93. const getStatusText = (status?: string | number): string => {
  94. if (!status) return '';
  95. const statusKey = String(status).toLowerCase();
  96. if (transactionStatusMap[statusKey as keyof typeof transactionStatusMap]) {
  97. return t(transactionStatusMap[statusKey as keyof typeof transactionStatusMap]);
  98. }
  99. const normalized = normalizeStatus(status);
  100. if (normalized === 'success') return t('card.Status.t1');
  101. if (normalized === 'processing') return t('card.Status.t3');
  102. return t('card.Status.t2');
  103. };
  104. const getStatusIcon = (status?: string | number): string => {
  105. const normalized = normalizeStatus(status);
  106. if (normalized === 'success') return 'checkmarkempty1';
  107. if (normalized === 'processing') return 'info1';
  108. return 'closeempty1';
  109. };
  110. const getStatusColor = (status?: string | number): string => {
  111. const normalized = normalizeStatus(status);
  112. if (normalized === 'success') return '#22c55e';
  113. if (normalized === 'processing') return '#eab308';
  114. return '#ef4444';
  115. };
  116. const getStatusBadgeClass = (status?: string | number) => `status-${normalizeStatus(status)}`;
  117. const getStatusTextClass = (status?: string | number) => `status-text-${normalizeStatus(status)}`;
  118. const getStatusValue = (index: number): NormalizedStatus | null => {
  119. if (index === 0) return null;
  120. const statusMap: NormalizedStatus[] = ['success', 'processing', 'failed'];
  121. return statusMap[index - 1];
  122. };
  123. const getTransactionTypeText = (type?: string): string => {
  124. if (!type) return '--';
  125. const key = type.toLowerCase();
  126. if (transactionTypeMap[key as keyof typeof transactionTypeMap]) {
  127. return t(transactionTypeMap[key as keyof typeof transactionTypeMap]);
  128. }
  129. return type;
  130. };
  131. const getTransactionIcon = (type = ''): string => {
  132. if (type.includes('购买') || type === 'refund') return 'cart';
  133. if (type.includes('提现') || type === 'auth') return 'minus-filled';
  134. if (type.includes('转账')) return 'redo';
  135. if (type.includes('话费')) return 'phone';
  136. if (type.includes('缴费')) return 'flame';
  137. if (type === 'maintain_fee') return 'servicefee';
  138. return 'servicefee';
  139. };
  140. const formatDateTime = (time?: string | number): string => {
  141. if (!time) return '--';
  142. try {
  143. let date: dayjs.Dayjs;
  144. if (typeof time === 'number') {
  145. if (time.toString().length === 10) {
  146. date = dayjs.unix(time);
  147. } else {
  148. date = dayjs(time);
  149. }
  150. } else {
  151. date = dayjs(time);
  152. }
  153. if (!date.isValid()) return '--';
  154. return date.format('YYYY-MM-DD HH:mm:ss');
  155. } catch (error) {
  156. return '--';
  157. }
  158. };
  159. const getDatePart = (time?: string | number): string => {
  160. if (!time) return '';
  161. try {
  162. let date: dayjs.Dayjs;
  163. if (typeof time === 'number') {
  164. if (time.toString().length === 10) {
  165. date = dayjs.unix(time);
  166. } else {
  167. date = dayjs(time);
  168. }
  169. } else {
  170. date = dayjs(time);
  171. }
  172. if (!date.isValid()) return '';
  173. return date.format('YYYY-MM-DD');
  174. } catch (error) {
  175. return '';
  176. }
  177. };
  178. const formatOrderNo = (orderNo?: string) => {
  179. if (!orderNo) return '--';
  180. if (orderNo.length <= 20) return orderNo;
  181. return orderNo.slice(0, 6) + '...' + orderNo.slice(-4);
  182. };
  183. const copyOrderNo = (orderNo?: string) => {
  184. if (!orderNo) return;
  185. uni.setClipboardData({
  186. data: orderNo,
  187. success: () => {
  188. uni.showToast({
  189. title: t('card.Msg.m8') || '复制成功',
  190. icon: 'success'
  191. });
  192. }
  193. });
  194. };
  195. const fetchRecords = async (isLoadMore = false) => {
  196. if (!props.cardNo || loading.value) return;
  197. if (isLoadMore && finished.value) return;
  198. loading.value = true;
  199. try {
  200. const res = await ucardApi.transactionsList({
  201. cardNo: props.cardNo,
  202. beginDate: props.dateFilter ? dayjs(props.dateFilter).format('YYYY-MM-DD') : undefined,
  203. endDate: props.dateFilter ? dayjs(props.dateFilter).format('YYYY-MM-DD') : undefined,
  204. page: { current: page.value, row: pageSize.value },
  205. });
  206. const data = res.code === 200 && Array.isArray(res.data) ? res.data : [];
  207. if (isLoadMore) {
  208. records.value.push(...data);
  209. } else {
  210. records.value = data;
  211. }
  212. if (data.length < pageSize.value) {
  213. finished.value = true;
  214. } else {
  215. finished.value = false;
  216. }
  217. } catch (error: any) {
  218. if (!isLoadMore) {
  219. records.value = [];
  220. }
  221. showToast(error?.message || String(error));
  222. } finally {
  223. loading.value = false;
  224. }
  225. };
  226. const goToTransactionDetail = (record: RecordItem) => {
  227. const amount = Number(record.amount || 0);
  228. const fee = Number(record.fee || 0);
  229. const normalizedStatus = normalizeStatus(record.status);
  230. const detailPayload = {
  231. category: 'transaction' as const,
  232. orderNo: record.tradeNo || '',
  233. type: getTransactionTypeText(record.type),
  234. amount,
  235. fee,
  236. actualAmount: amount - fee,
  237. currency: record.currency || 'USD',
  238. orderStatus: normalizedStatus,
  239. statusMessage: getStatusText(record.status),
  240. createTime: formatDateTime(record.transactionTime),
  241. completeTime: '',
  242. merchant: record.merchant || '',
  243. bankCard: '',
  244. remark: record.remark || '',
  245. approvalSteps: [] as any[]
  246. };
  247. cardStore.saveOrderDetail(detailPayload);
  248. uni.navigateTo({
  249. url: '/pages/recharge-record/detail'
  250. });
  251. };
  252. const loadMore = () => {
  253. if (finished.value || loading.value) return;
  254. page.value++;
  255. fetchRecords(true);
  256. };
  257. const filteredRecords = computed(() => {
  258. return records.value.filter(record => {
  259. const type = record.typeStr || record.type;
  260. if (props.typeIndex > 0 && type !== props.typeOptions[props.typeIndex]) {
  261. return false;
  262. }
  263. const statusValue = getStatusValue(props.statusIndex);
  264. if (statusValue && normalizeStatus(record.status) !== statusValue) {
  265. return false;
  266. }
  267. if (props.dateFilter) {
  268. const time = record.transactionTime;
  269. const datePart = getDatePart(time);
  270. if (!datePart || datePart !== props.dateFilter) {
  271. return false;
  272. }
  273. }
  274. return true;
  275. });
  276. });
  277. watch([() => props.dateFilter], () => {
  278. page.value = 1;
  279. finished.value = false;
  280. fetchRecords();
  281. }, { immediate: false });
  282. const loadMoreWrapperRef = ref<any>(null);
  283. const refresh = async () => {
  284. page.value = 1;
  285. finished.value = false;
  286. await fetchRecords();
  287. };
  288. const handleRefresh = async () => {
  289. await refresh();
  290. // 停止下拉刷新动画
  291. if (loadMoreWrapperRef.value) {
  292. loadMoreWrapperRef.value.stopRefresh();
  293. }
  294. };
  295. onMounted(() => {
  296. fetchRecords();
  297. });
  298. defineExpose({
  299. refresh
  300. });
  301. </script>
  302. <style scoped lang="scss">
  303. @import "@/uni.scss";
  304. .records-list {
  305. display: flex;
  306. flex-direction: column;
  307. gap: px2rpx(12);
  308. padding: px2rpx(16);
  309. }
  310. .records-list1 {
  311. flex-direction: row;
  312. flex-wrap: wrap;
  313. gap: px2rpx(12);
  314. padding: px2rpx(16) 0;
  315. }
  316. .record-card {
  317. background-color: #ffffff;
  318. border-radius: px2rpx(12);
  319. border: 1px solid #e5e7eb;
  320. overflow: hidden;
  321. transition: box-shadow 0.3s;
  322. padding: px2rpx(16);
  323. }
  324. .record-card:active {
  325. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  326. }
  327. .record-main {
  328. display: flex;
  329. align-items: flex-start;
  330. justify-content: space-between;
  331. padding-bottom: px2rpx(16);
  332. }
  333. .record-left {
  334. display: flex;
  335. align-items: flex-start;
  336. gap: px2rpx(12);
  337. flex: 1;
  338. min-width: 0;
  339. }
  340. .type-icon {
  341. width: px2rpx(40);
  342. height: px2rpx(40);
  343. border-radius: px2rpx(10);
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. flex-shrink: 0;
  348. }
  349. .transaction-icon {
  350. background-color: #eff6ff;
  351. }
  352. .icons {
  353. width: px2rpx(20);
  354. height: px2rpx(20);
  355. }
  356. .record-info {
  357. flex: 1;
  358. min-width: 0;
  359. display: flex;
  360. flex-direction: column;
  361. gap: px2rpx(6);
  362. }
  363. .info-header {
  364. display: flex;
  365. align-items: center;
  366. gap: px2rpx(8);
  367. flex-wrap: wrap;
  368. }
  369. .record-type {
  370. font-size: px2rpx(15);
  371. color: #111827;
  372. }
  373. .status-badge {
  374. display: flex;
  375. align-items: center;
  376. gap: px2rpx(4);
  377. padding: px2rpx(3) px2rpx(2);
  378. border-radius: px2rpx(12);
  379. }
  380. .status-success {
  381. background-color: #f0fdf4;
  382. }
  383. .status-processing {
  384. background-color: #fefce8;
  385. }
  386. .status-failed {
  387. background-color: #fef2f2;
  388. }
  389. .status-text {
  390. font-size: px2rpx(11);
  391. }
  392. .status-text-success {
  393. color: #22c55e;
  394. }
  395. .status-text-processing {
  396. color: #eab308;
  397. }
  398. .status-text-failed {
  399. color: #ef4444;
  400. }
  401. .record-detail {
  402. font-size: px2rpx(13);
  403. color: #6b7280;
  404. overflow: hidden;
  405. text-overflow: ellipsis;
  406. white-space: nowrap;
  407. }
  408. .record-right {
  409. display: flex;
  410. flex-direction: column;
  411. align-items: flex-end;
  412. gap: px2rpx(4);
  413. margin-left: px2rpx(12);
  414. flex-shrink: 0;
  415. }
  416. .amount-transaction {
  417. font-size: px2rpx(18);
  418. color: #111827;
  419. }
  420. .fee-text {
  421. font-size: px2rpx(11);
  422. color: #9ca3af;
  423. }
  424. .record-footer {
  425. display: flex;
  426. align-items: center;
  427. justify-content: space-between;
  428. padding-top: px2rpx(16);
  429. border-top: 1px solid #f3f4f6;
  430. }
  431. .footer-actions {
  432. display: flex;
  433. align-items: center;
  434. gap: px2rpx(8);
  435. }
  436. .footer-time {
  437. font-size: px2rpx(11);
  438. color: #9ca3af;
  439. }
  440. .footer-actions {
  441. display: flex;
  442. align-items: center;
  443. gap: px2rpx(2);
  444. }
  445. .footer-order {
  446. font-size: px2rpx(11);
  447. color: #9ca3af;
  448. }
  449. .footer-detail {
  450. font-size: px2rpx(11);
  451. color: #2563eb;
  452. }
  453. </style>