Files
tyapi-frontend/src/pages/admin/transactions/index.vue
2026-03-21 14:33:00 +08:00

885 lines
24 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<ListPageLayout
title="消费记录管理"
subtitle="管理系统内所有用户的消费记录"
>
<!-- 单用户模式显示 -->
<template #stats v-if="singleUserMode">
<div :class="['flex items-center gap-2 text-sm text-gray-600', isMobile ? 'flex-wrap' : '']">
<User class="w-4 h-4 flex-shrink-0" />
<span class="truncate">当前用户{{ currentUser?.company_name || currentUser?.phone }}</span>
<span :class="['text-gray-400', isMobile ? 'w-full text-xs' : '']">(仅显示当前用户)</span>
</div>
</template>
<!-- 单用户模式操作按钮 -->
<template #actions v-if="singleUserMode">
<div :class="['flex items-center gap-3', isMobile ? 'flex-wrap' : '']">
<div :class="['flex items-center gap-2 text-sm text-gray-600', isMobile ? 'w-full mb-2' : '']">
<User class="w-4 h-4" />
<span class="truncate">{{ currentUser?.company_name || currentUser?.phone }}</span>
</div>
<div :class="['flex gap-2', isMobile ? 'w-full' : '']">
<el-button :size="isMobile ? 'small' : 'default'" @click="exitSingleUserMode" :class="isMobile ? 'flex-1' : ''">
<Close class="w-4 h-4 mr-1" />
取消
</el-button>
<el-button :size="isMobile ? 'small' : 'default'" type="primary" @click="goBackToUsers" :class="isMobile ? 'flex-1' : ''">
<Back class="w-4 h-4 mr-1" />
返回用户管理
</el-button>
</div>
</div>
</template>
<!-- 筛选区域 -->
<template #filters>
<FilterSection>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
<FilterItem label="企业名称" v-if="!singleUserMode">
<el-input
v-model="filters.company_name"
placeholder="输入企业名称"
clearable
@input="handleFilterChange"
class="w-full"
/>
</FilterItem>
<FilterItem label="产品名称">
<el-input
v-model="filters.product_name"
placeholder="输入产品名称"
clearable
@input="handleFilterChange"
class="w-full"
/>
</FilterItem>
<FilterItem label="交易ID">
<el-input
v-model="filters.transaction_id"
placeholder="输入交易ID"
clearable
@input="handleFilterChange"
class="w-full"
/>
</FilterItem>
<FilterItem label="消费时间" class="col-span-1 md:col-span-2">
<el-date-picker
v-model="dateRange"
type="datetimerange"
range-separator=""
start-placeholder="开始时间"
end-placeholder="结束时间"
format="YYYY-MM-DD HH:mm:ss"
value-format="YYYY-MM-DD HH:mm:ss"
@change="handleTimeRangeChange"
class="w-full"
:size="isMobile ? 'small' : 'default'"
/>
</FilterItem>
<FilterItem label="金额范围">
<div :class="['flex gap-2', isMobile ? 'flex-col' : '']">
<el-input
v-model="filters.min_amount"
placeholder="最小金额"
clearable
@input="handleFilterChange"
:class="isMobile ? 'w-full' : 'flex-1'"
/>
<span :class="['text-gray-400 self-center', isMobile ? 'hidden' : '']">-</span>
<el-input
v-model="filters.max_amount"
placeholder="最大金额"
clearable
@input="handleFilterChange"
:class="isMobile ? 'w-full' : 'flex-1'"
/>
</div>
</FilterItem>
</div>
<template #stats>
共找到 {{ total }} 条消费记录
</template>
<template #buttons>
<div :class="['flex gap-2', isMobile ? 'flex-wrap w-full' : '']">
<el-button :size="isMobile ? 'small' : 'default'" @click="resetFilters" :class="isMobile ? 'flex-1' : ''">
重置筛选
</el-button>
<el-button :size="isMobile ? 'small' : 'default'" type="primary" @click="loadTransactions" :class="isMobile ? 'flex-1' : ''">
应用筛选
</el-button>
<el-button :size="isMobile ? 'small' : 'default'" type="success" @click="showExportDialog" :class="isMobile ? 'flex-1' : ''">
<Download class="w-4 h-4 mr-1" />
导出数据
</el-button>
</div>
</template>
</FilterSection>
</template>
<!-- 表格区域 -->
<template #table>
<div v-if="loading" class="flex justify-center items-center py-12">
<el-loading size="large" />
</div>
<!-- 移动端卡片布局 -->
<div v-else-if="isMobile && transactions.length > 0" class="transaction-cards">
<div
v-for="transaction in transactions"
:key="transaction.id"
class="transaction-card"
>
<div class="card-header">
<div class="flex-1">
<div class="flex items-center gap-2 mb-1">
<span class="font-mono text-sm text-gray-600 truncate">{{ transaction.transaction_id }}</span>
</div>
<div v-if="!singleUserMode && transaction.company_name" class="text-xs text-gray-500 truncate">
{{ transaction.company_name }}
</div>
</div>
<span class="font-semibold text-red-600 text-lg">¥{{ formatPrice(transaction.amount) }}</span>
</div>
<div class="card-body">
<div v-if="!singleUserMode && transaction.user?.phone" class="card-row">
<span class="card-label">手机号</span>
<span class="card-value text-sm">{{ transaction.user.phone }}</span>
</div>
<div class="card-row">
<span class="card-label">产品名称</span>
<span class="card-value text-blue-600 font-medium">{{ transaction.product_name || '未知产品' }}</span>
</div>
<div class="card-row">
<span class="card-label">消费时间</span>
<span class="card-value text-sm">
<div>{{ formatDate(transaction.created_at) }}</div>
<div class="text-gray-500">{{ formatTime(transaction.created_at) }}</div>
</span>
</div>
</div>
<div class="card-footer">
<el-button
type="primary"
size="small"
@click="handleViewDetail(transaction)"
class="action-btn w-full"
>
查看详情
</el-button>
</div>
</div>
</div>
<!-- 桌面端表格布局 -->
<div v-else-if="!isMobile" class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
<div class="table-container">
<el-table
:data="transactions"
style="width: 100%"
:header-cell-style="{
background: '#f8fafc',
color: '#475569',
fontWeight: '600',
fontSize: '14px'
}"
:cell-style="{
fontSize: '14px',
color: '#1e293b'
}"
>
<el-table-column prop="transaction_id" label="交易ID" min-width="180">
<template #default="{ row }">
<span class="font-mono text-sm text-gray-600">{{ row.transaction_id }}</span>
</template>
</el-table-column>
<el-table-column prop="company_name" label="企业名称" min-width="150" v-if="!singleUserMode">
<template #default="{ row }">
<div>
<div class="font-medium text-blue-600">{{ row.company_name || '未知企业' }}</div>
<div class="text-xs text-gray-500">{{ row.user?.phone }}</div>
</div>
</template>
</el-table-column>
<el-table-column prop="product_name" label="产品名称" min-width="150">
<template #default="{ row }">
<div>
<div class="font-medium text-blue-600">{{ row.product_name || '未知产品' }}</div>
</div>
</template>
</el-table-column>
<el-table-column prop="amount" label="消费金额" width="120">
<template #default="{ row }">
<span class="font-semibold text-red-600">¥{{ formatPrice(row.amount) }}</span>
</template>
</el-table-column>
<el-table-column prop="created_at" label="消费时间" width="160">
<template #default="{ row }">
<div class="text-sm">
<div class="text-gray-900">{{ formatDate(row.created_at) }}</div>
<div class="text-gray-500">{{ formatTime(row.created_at) }}</div>
</div>
</template>
</el-table-column>
<el-table-column label="操作" width="120" fixed="right">
<template #default="{ row }">
<div class="flex items-center space-x-2">
<el-button
size="small"
type="primary"
@click="handleViewDetail(row)"
>
查看详情
</el-button>
</div>
</template>
</el-table-column>
</el-table>
</div>
</div>
<!-- 空状态 -->
<div v-else-if="!loading && transactions.length === 0" class="bg-white rounded-lg shadow-sm border border-gray-200 p-12 text-center">
<p class="text-gray-500">暂无消费记录</p>
</div>
</template>
<!-- 分页 -->
<template #pagination>
<el-pagination
v-if="total > 0"
v-model:current-page="currentPage"
v-model:page-size="pageSize"
:page-sizes="[10, 20, 50, 100]"
:total="total"
:layout="isMobile ? 'prev, pager, next' : 'total, sizes, prev, pager, next, jumper'"
:small="isMobile"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</template>
<template #extra>
<!-- 消费记录详情弹窗 -->
<el-dialog
v-model="detailDialogVisible"
title="消费记录详情"
:width="isMobile ? '90%' : '800px'"
class="transaction-detail-dialog"
>
<div v-if="selectedTransaction" class="space-y-6">
<!-- 基本信息 -->
<div class="info-section">
<h4 class="text-lg font-semibold text-gray-900 mb-4">基本信息</h4>
<div :class="['grid gap-4', isMobile ? 'grid-cols-1' : 'grid-cols-2']">
<div class="info-item">
<span class="info-label">交易ID</span>
<span class="info-value font-mono">{{ selectedTransaction?.transaction_id || '-' }}</span>
</div>
<div class="info-item">
<span class="info-label">产品名称</span>
<span class="info-value">{{ selectedTransaction?.product_name || '未知产品' }}</span>
</div>
<div class="info-item">
<span class="info-label">消费金额</span>
<span class="info-value text-red-600 font-semibold">¥{{ formatPrice(selectedTransaction?.amount) }}</span>
</div>
<div class="info-item" v-if="!singleUserMode">
<span class="info-label">企业名称</span>
<span class="info-value">{{ selectedTransaction?.company_name || '未知企业' }}</span>
</div>
</div>
</div>
<!-- 时间信息 -->
<div class="info-section">
<h4 class="text-lg font-semibold text-gray-900 mb-4">时间信息</h4>
<div :class="['grid gap-4', isMobile ? 'grid-cols-1' : 'grid-cols-2']">
<div class="info-item">
<span class="info-label">消费时间</span>
<span class="info-value">{{ formatDateTime(selectedTransaction?.created_at) }}</span>
</div>
<div class="info-item">
<span class="info-label">更新时间</span>
<span class="info-value">{{ formatDateTime(selectedTransaction?.updated_at) }}</span>
</div>
</div>
</div>
</div>
<div v-else class="flex justify-center items-center py-8">
<el-loading size="large" />
</div>
</el-dialog>
</template>
</ListPageLayout>
<!-- 导出弹窗 -->
<ExportDialog
v-model="exportDialogVisible"
title="导出钱包交易记录"
:loading="exportLoading"
:show-company-select="true"
:show-product-select="true"
:show-recharge-type-select="false"
:show-status-select="false"
:show-date-range="true"
@confirm="handleExport"
/>
</template>
<script setup>
import { userApi, walletTransactionApi } from '@/api'
import ExportDialog from '@/components/common/ExportDialog.vue'
import FilterItem from '@/components/common/FilterItem.vue'
import FilterSection from '@/components/common/FilterSection.vue'
import ListPageLayout from '@/components/common/ListPageLayout.vue'
import { useMobileTable } from '@/composables/useMobileTable'
import { Back, Close, Download, User } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
// 移动端检测
const { isMobile, isTablet } = useMobileTable()
// 响应式数据
const loading = ref(false)
const transactions = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const detailDialogVisible = ref(false)
const selectedTransaction = ref(null)
const dateRange = ref([])
// 单用户模式
const singleUserMode = ref(false)
const currentUser = ref(null)
// 导出相关
const exportDialogVisible = ref(false)
const exportLoading = ref(false)
// 筛选条件
const filters = reactive({
company_name: '',
product_name: '',
transaction_id: '',
min_amount: '',
max_amount: '',
start_time: '',
end_time: ''
})
// 初始化
onMounted(async () => {
await checkSingleUserMode()
await loadTransactions()
})
// 检查单用户模式
const checkSingleUserMode = async () => {
const userId = route.query.user_id
if (userId) {
singleUserMode.value = true
await loadUserInfo(userId)
}
}
// 加载用户信息
const loadUserInfo = async (userId) => {
try {
const response = await userApi.getUserDetail(userId)
currentUser.value = response.data
} catch (error) {
console.error('加载用户信息失败:', error)
ElMessage.error('加载用户信息失败')
}
}
// 加载消费记录
const loadTransactions = async () => {
loading.value = true
try {
const params = {
page: currentPage.value,
page_size: pageSize.value,
...filters
}
// 单用户模式添加用户ID筛选
if (singleUserMode.value && currentUser.value?.id) {
params.user_id = currentUser.value.id
}
const response = await walletTransactionApi.getAdminWalletTransactions(params)
transactions.value = response.data?.items || []
total.value = response.data?.total || 0
} catch (error) {
console.error('加载消费记录失败:', error)
ElMessage.error('加载消费记录失败')
} finally {
loading.value = false
}
}
// 格式化价格
const formatPrice = (price) => {
if (!price) return '0.00'
return Number(price).toFixed(2)
}
// 格式化日期
const formatDate = (date) => {
if (!date) return '-'
return new Date(date).toLocaleDateString('zh-CN')
}
// 格式化时间
const formatTime = (date) => {
if (!date) return '-'
return new Date(date).toLocaleTimeString('zh-CN', {
hour: '2-digit',
minute: '2-digit'
})
}
// 格式化日期时间
const formatDateTime = (date) => {
if (!date) return '-'
return new Date(date).toLocaleString('zh-CN')
}
// 处理筛选变化
const handleFilterChange = () => {
currentPage.value = 1
loadTransactions()
}
// 处理时间范围变化
const handleTimeRangeChange = (range) => {
if (range && range.length === 2) {
filters.start_time = range[0]
filters.end_time = range[1]
} else {
filters.start_time = ''
filters.end_time = ''
}
currentPage.value = 1
loadTransactions()
}
// 重置筛选
const resetFilters = () => {
Object.keys(filters).forEach(key => {
filters[key] = ''
})
dateRange.value = []
currentPage.value = 1
loadTransactions()
}
// 处理分页大小变化
const handleSizeChange = (size) => {
pageSize.value = size
currentPage.value = 1
loadTransactions()
}
// 处理当前页变化
const handleCurrentChange = (page) => {
currentPage.value = page
loadTransactions()
}
// 退出单用户模式
const exitSingleUserMode = () => {
singleUserMode.value = false
currentUser.value = null
router.replace({ name: 'AdminTransactions' })
loadTransactions()
}
// 返回用户管理
const goBackToUsers = () => {
const query = { user_id: currentUser.value?.id }
// 如果当前用户有手机号,添加到查询参数
if (currentUser.value?.phone) {
query.phone = currentUser.value.phone
}
// 如果当前用户有企业名称,添加到查询参数
if (currentUser.value?.enterprise_info?.company_name) {
query.company_name = currentUser.value.enterprise_info.company_name
}
router.push({
name: 'AdminUsers',
query
})
}
// 查看详情
const handleViewDetail = (transaction) => {
selectedTransaction.value = transaction
detailDialogVisible.value = true
}
// 导出相关方法
const showExportDialog = () => {
exportDialogVisible.value = true
}
const handleExport = async (options) => {
try {
exportLoading.value = true
// 构建导出参数
const params = {
format: options.format
}
// 添加企业筛选
if (options.companyIds.length > 0) {
params.user_ids = options.companyIds.join(',')
}
// 添加产品筛选
if (options.productIds.length > 0) {
params.product_ids = options.productIds.join(',')
}
// 添加时间范围筛选
if (options.dateRange && options.dateRange.length === 2) {
params.start_time = options.dateRange[0]
params.end_time = options.dateRange[1]
}
// 调用导出API
const response = await walletTransactionApi.exportAdminWalletTransactions(params)
// 创建下载链接
const blob = new Blob([response.data], {
type: options.format === 'excel'
? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
: 'text/csv;charset=utf-8'
})
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = `钱包交易记录.${options.format === 'excel' ? 'xlsx' : 'csv'}`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(url)
ElMessage.success('导出成功')
exportDialogVisible.value = false
} catch (error) {
console.error('导出失败:', error)
ElMessage.error('导出失败,请稍后重试')
} finally {
exportLoading.value = false
}
}
// 监听路由变化
watch(() => route.query.user_id, async (newUserId) => {
if (newUserId) {
singleUserMode.value = true
await loadUserInfo(newUserId)
} else {
singleUserMode.value = false
currentUser.value = null
}
await loadTransactions()
})
</script>
<style scoped>
.info-section {
border: 1px solid #e5e7eb;
border-radius: 0.5rem;
padding: 1rem;
}
.info-item {
display: flex;
align-items: flex-start;
gap: 0.5rem;
}
.info-label {
font-size: 0.875rem;
font-weight: 500;
color: #374151;
min-width: 100px;
}
.info-value {
font-size: 0.875rem;
color: #111827;
flex: 1;
}
.transaction-detail-dialog :deep(.el-dialog) {
border-radius: 16px;
overflow: hidden;
}
.transaction-detail-dialog :deep(.el-dialog__header) {
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
border-bottom: 1px solid rgba(226, 232, 240, 0.6);
padding: 20px 24px;
}
.transaction-detail-dialog :deep(.el-dialog__title) {
font-size: 18px;
font-weight: 600;
color: #1e293b;
}
.transaction-detail-dialog :deep(.el-dialog__body) {
padding: 24px;
max-height: 70vh;
overflow-y: auto;
}
/* 导出弹窗样式 */
.export-dialog :deep(.el-dialog) {
border-radius: 16px;
overflow: hidden;
}
.export-dialog :deep(.el-dialog__header) {
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
border-bottom: 1px solid rgba(226, 232, 240, 0.6);
padding: 20px 24px;
}
.export-dialog :deep(.el-dialog__title) {
font-size: 18px;
font-weight: 600;
color: #1e293b;
}
.export-dialog :deep(.el-dialog__body) {
padding: 24px;
max-height: 70vh;
overflow-y: auto;
}
.export-section {
border: 1px solid #e5e7eb;
border-radius: 0.5rem;
padding: 1rem;
background: #f9fafb;
}
/* 表格样式优化 */
:deep(.el-table) {
border-radius: 8px;
overflow: hidden;
}
:deep(.el-table th) {
background: #f8fafc !important;
border-bottom: 1px solid #e2e8f0;
}
:deep(.el-table td) {
border-bottom: 1px solid #f1f5f9;
}
:deep(.el-table tr:hover > td) {
background: #f8fafc !important;
}
/* 表格容器 */
.table-container {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
/* 移动端卡片布局 */
.transaction-cards {
display: flex;
flex-direction: column;
gap: 12px;
}
.transaction-card {
background: white;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 16px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.card-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 12px;
padding-bottom: 12px;
border-bottom: 1px solid #f3f4f6;
}
.card-body {
display: flex;
flex-direction: column;
gap: 8px;
margin-bottom: 12px;
}
.card-row {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 8px;
}
.card-label {
font-size: 12px;
color: #6b7280;
font-weight: 500;
min-width: 80px;
flex-shrink: 0;
}
.card-value {
font-size: 14px;
color: #1f2937;
text-align: right;
word-break: break-word;
flex: 1;
}
.card-footer {
padding-top: 12px;
border-top: 1px solid #f3f4f6;
}
.action-btn {
width: 100%;
}
/* 响应式设计 */
@media (max-width: 768px) {
.info-item {
flex-direction: column;
align-items: flex-start;
gap: 0.25rem;
}
.info-label {
min-width: auto;
}
/* 表格在移动端优化 */
.table-container {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
:deep(.el-table) {
font-size: 12px;
min-width: 1000px;
}
:deep(.el-table th),
:deep(.el-table td) {
padding: 8px 4px;
}
:deep(.el-table .cell) {
padding: 0 4px;
word-break: break-word;
line-height: 1.4;
}
/* 分页组件在移动端优化 */
:deep(.el-pagination) {
justify-content: center;
}
:deep(.el-pagination .el-pagination__sizes) {
display: none;
}
:deep(.el-pagination .el-pagination__total) {
display: none;
}
:deep(.el-pagination .el-pagination__jump) {
display: none;
}
/* 对话框在移动端优化 */
.transaction-detail-dialog :deep(.el-dialog__body),
.export-dialog :deep(.el-dialog__body) {
padding: 16px;
max-height: 80vh;
overflow-y: auto;
}
}
/* 超小屏幕进一步优化 */
@media (max-width: 480px) {
.transaction-card {
padding: 12px;
}
.card-header {
flex-direction: column;
gap: 8px;
}
.card-body {
gap: 6px;
}
.card-label {
font-size: 11px;
min-width: 70px;
}
.card-value {
font-size: 13px;
}
.card-row {
flex-direction: column;
align-items: flex-start;
gap: 4px;
}
.card-value {
text-align: left;
}
.action-btn {
font-size: 12px;
padding: 6px 8px;
}
}
</style>