请求拦截Code
Yann authored at 2025-07-01 14:00:58 Yann committed at 2025-07-01 15:20:51
10.15 KiB
cube-front
<template>
  <div>
    <div class="page-header">
      <h1>OAuth日志管理</h1>
      <p>OAuth认证登录日志查看与管理</p>
    </div>

    <el-card>
      <template #header>
        <div class="card-header">
          <span>OAuth日志列表</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="提供商">
            <el-input v-model="searchForm.provider" placeholder="请输入提供商" />
          </el-form-item>
          <el-form-item label="用户ID">
            <el-input v-model="searchForm.userId" placeholder="请输入用户ID" />
          </el-form-item>
          <el-form-item label="操作">
            <el-input v-model="searchForm.action" placeholder="请输入操作" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchForm.success" placeholder="选择状态">
              <el-option label="全部" :value="null" />
              <el-option label="成功" :value="true" />
              <el-option label="失败" :value="false" />
            </el-select>
          </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="provider" label="提供商" width="120" />
          <el-table-column prop="userId" label="用户ID" width="100" />
          <el-table-column prop="userName" label="用户名" />
          <el-table-column prop="action" label="操作" />
          <el-table-column prop="success" label="状态" width="80">
            <template #default="{ row }">
              <el-tag :type="row.success ? 'success' : 'danger'">
                {{ row.success ? '成功' : '失败' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="redirectUri" label="回调地址" show-overflow-tooltip />
          <el-table-column prop="source" label="来源" />
          <el-table-column prop="createIP" label="创建地址" />
          <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="OAuth日志详情" width="800px">
      <div v-loading="loading.detail">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="编号">{{ detailData.id }}</el-descriptions-item>
          <el-descriptions-item label="提供商">{{ detailData.provider }}</el-descriptions-item>
          <el-descriptions-item label="连接ID">{{ detailData.connectId }}</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.action }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="detailData.success ? 'success' : 'danger'">
              {{ detailData.success ? '成功' : '失败' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="回调地址" :span="2">{{ detailData.redirectUri }}</el-descriptions-item>
          <el-descriptions-item label="响应类型">{{ detailData.responseType }}</el-descriptions-item>
          <el-descriptions-item label="授权域">{{ detailData.scope }}</el-descriptions-item>
          <el-descriptions-item label="状态数据" :span="2">{{ detailData.state }}</el-descriptions-item>
          <el-descriptions-item label="来源">{{ detailData.source }}</el-descriptions-item>
          <el-descriptions-item label="访问令牌" :span="2">{{ detailData.accessToken }}</el-descriptions-item>
          <el-descriptions-item label="刷新令牌" :span="2">{{ detailData.refreshToken }}</el-descriptions-item>
          <el-descriptions-item label="追踪ID" :span="2">{{ detailData.traceId }}</el-descriptions-item>
          <el-descriptions-item label="创建地址">{{ detailData.createIP }}</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-item label="详细信息" :span="2">
            <div style="max-height: 200px; overflow-y: auto;">
              {{ detailData.remark }}
            </div>
          </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'

// OAuth日志接口
interface OAuthLog {
  id: number
  provider: string
  connectId: number
  userId: number
  userName: string
  action: string
  success: boolean
  redirectUri: string
  responseType: string
  scope: string
  state: string
  source: string
  accessToken: string
  refreshToken: string
  traceId: string
  remark: string
  createIP: string
  createTime: string
  updateTime: string
}

// 搜索表单
const searchForm = reactive({
  provider: '',
  userId: '',
  action: '',
  success: null as boolean | null
})

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

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

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

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

// 获取OAuth日志列表
const getOAuthLogList = async () => {
  try {
    loading.list = true

    const data = await request.get('/Admin/OAuthLog', {
      params: {
        page: pagination.page,
        size: pagination.size,
        provider: searchForm.provider || undefined,
        userId: searchForm.userId || undefined,
        action: searchForm.action || undefined,
        success: searchForm.success !== null ? searchForm.success : undefined
      }
    })

    // 处理不同的响应格式
    if (data && typeof data === 'object' && 'list' in data && 'total' in data) {
      tableData.value = Array.isArray(data.list) ? data.list : [];
      pagination.total = data.total || 0;
    } else if (Array.isArray(data)) {
      tableData.value = data;
      pagination.total = data.length;
    } else {
      tableData.value = [];
      pagination.total = 0;
    }
  } catch {
    tableData.value = [];
    pagination.total = 0;
  } finally {
    loading.list = false
  }
}

// 获取OAuth日志详情
const getOAuthLogDetail = async (id: number) => {
  try {
    loading.detail = true

    const data = await request.get('/Admin/OAuthLog/Detail', {
      params: { id }
    })

    if (data) {
      detailData.value = data || {}
    }
  } catch {
    // 错误提示已经在 request 拦截器中自动处理
  } finally {
    loading.detail = false
  }
}

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

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    provider: '',
    userId: '',
    action: '',
    success: null
  })
  pagination.page = 1
  getOAuthLogList()
}

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

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

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

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

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

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

// 组件挂载时获取数据
onMounted(() => {
  getOAuthLogList()
})
</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>