请求拦截Code
Yann authored at 2025-07-01 14:00:58 Yann committed at 2025-07-01 15:20:51
8.15 KiB
cube-front
<template>
  <div>
    <div class="page-header">
      <h1>用户统计</h1>
      <p>查看用户访问统计数据</p>
    </div>

    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户统计列表</span>
          <div>
            <el-button @click="refreshList">
              <el-icon><Refresh /></el-icon>
              刷新
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索表单 -->
      <div class="search-form">
        <el-form :model="searchForm" inline>
          <el-form-item label="用户ID">
            <el-input v-model="searchForm.userId" placeholder="请输入用户ID" />
          </el-form-item>
          <el-form-item label="统计日期">
            <el-date-picker
              v-model="searchForm.statDate"
              type="date"
              placeholder="选择统计日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据表格 -->
      <div class="table-container">
        <el-table
          :data="tableData"
          v-loading="loading.list"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="id" label="编号" width="80" />
          <el-table-column prop="userId" label="用户ID" width="100" />
          <el-table-column prop="userName" label="用户名" />
          <el-table-column prop="loginCount" label="登录次数" width="100" />
          <el-table-column prop="lastLogin" label="最后登录" width="160">
            <template #default="{ row }">
              {{ formatDate(row.lastLogin) }}
            </template>
          </el-table-column>
          <el-table-column prop="onlineTime" label="在线时长(分钟)" width="140" />
          <el-table-column prop="pageViews" label="页面访问数" width="120" />
          <el-table-column prop="statDate" label="统计日期" width="120" />
          <el-table-column prop="createTime" label="创建时间" width="160">
            <template #default="{ row }">
              {{ formatDate(row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button size="small" @click="handleDetail(row)">
                详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog v-model="showDetailDialog" title="用户统计详情" width="600px">
      <div v-loading="loading.detail">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="编号">{{ detailData.id }}</el-descriptions-item>
          <el-descriptions-item label="用户ID">{{ detailData.userId }}</el-descriptions-item>
          <el-descriptions-item label="用户名">{{ detailData.userName }}</el-descriptions-item>
          <el-descriptions-item label="登录次数">{{ detailData.loginCount }}</el-descriptions-item>
          <el-descriptions-item label="最后登录">{{ formatDate(detailData.lastLogin) }}</el-descriptions-item>
          <el-descriptions-item label="在线时长(分钟)">{{ detailData.onlineTime }}</el-descriptions-item>
          <el-descriptions-item label="页面访问数">{{ detailData.pageViews }}</el-descriptions-item>
          <el-descriptions-item label="统计日期">{{ detailData.statDate }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ formatDate(detailData.createTime) }}</el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ formatDate(detailData.updateTime) }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <template #footer>
        <el-button @click="showDetailDialog = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { request } from '@core/utils/request'

// 用户统计接口
interface UserStat {
  id: number
  userId: number
  userName: string
  loginCount: number
  lastLogin: string
  onlineTime: number
  pageViews: number
  statDate: string
  createTime: string
  updateTime: string
}

// 搜索表单
const searchForm = reactive({
  userId: '',
  statDate: ''
})

// 表格数据
const tableData = ref<UserStat[]>([])
const selectedRows = ref<UserStat[]>([])

// 详情数据
const detailData = ref<Partial<UserStat>>({})
const showDetailDialog = ref(false)

// 分页
const pagination = reactive({
  page: 1,
  size: 20,
  total: 0
})

// 加载状态
const loading = reactive({
  list: false,
  detail: false
})

// 获取用户统计列表
const getUserStatList = async () => {
  try {
    loading.list = true

    const params = new URLSearchParams({
      page: pagination.page.toString(),
      size: pagination.size.toString()
    })

    // 添加搜索条件
    if (searchForm.userId) params.append('userId', searchForm.userId)
    if (searchForm.statDate) params.append('statDate', searchForm.statDate)

    const data = await request.get(`/Admin/UserStat?${params}`)

    if (data?.data) {
      tableData.value = data.data?.list || []
      pagination.total = data.data?.total || 0
    } else {
      throw new Error('获取用户统计列表失败')
    }
  } catch {
    tableData.value = [];
    pagination.total = 0;
  } finally {
    loading.list = false
  }
}

// 获取用户统计详情
const getUserStatDetail = async (id: number) => {
  try {
    loading.detail = true

    const data = await request.get(`/Admin/UserStat/Detail?id=${id}`)

    if (data?.data) {
      detailData.value = data.data || {}
    } else {
      throw new Error('获取用户统计详情失败')
    }
  } catch {
    // 错误提示已经在 request 拦截器中自动处理
  } finally {
    loading.detail = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  getUserStatList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    userId: '',
    statDate: ''
  })
  pagination.page = 1
  getUserStatList()
}

// 刷新列表
const refreshList = () => {
  getUserStatList()
}

// 查看详情
const handleDetail = async (row: UserStat) => {
  await getUserStatDetail(row.id)
  showDetailDialog.value = true
}

// 表格选择变化
const handleSelectionChange = (selection: UserStat[]) => {
  selectedRows.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.size = size
  pagination.page = 1
  getUserStatList()
}

// 页码变化
const handleCurrentChange = (page: number) => {
  pagination.page = page
  getUserStatList()
}

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN')
}

// 组件挂载时获取数据
onMounted(() => {
  getUserStatList()
})
</script>

<style scoped>
.page-header {
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 500;
}

.page-header p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.table-container {
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
}
</style>