请求拦截Code
Yann authored at 2025-07-01 14:00:58 Yann committed at 2025-07-01 15:20:51
5.14 KiB
cube-front
<template>
  <div class="user-connect-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <h3>用户连接管理</h3>
        </div>
      </template>

      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.q" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="编号" width="80" />
        <el-table-column prop="provider" label="提供者" />
        <el-table-column prop="openID" label="用户标识" />
        <el-table-column prop="userName" label="用户名" />
        <el-table-column prop="nickName" label="昵称" />
        <el-table-column prop="avatar" label="头像" width="80">
          <template #default="scope">
            <el-avatar v-if="scope.row.avatar" :src="scope.row.avatar" :size="40" />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="refreshToken" label="刷新令牌" show-overflow-tooltip />
        <el-table-column prop="expire" label="过期时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column label="启用" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.enable ? 'success' : 'danger'">
              {{ scope.row.enable ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

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

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

// 定义用户连接类型接口
interface UserConnect {
  id: number;
  provider: string;
  openID: string;
  userName: string;
  nickName: string;
  avatar: string;
  refreshToken: string;
  expire: string;
  enable: boolean;
  updateTime: string;
}

// 表格数据
const tableData = ref<UserConnect[]>([]);
const loading = ref(false);
const total = ref(0);
const currentPage = ref(1);
const pageSize = ref(10);

// 查询表单
const searchForm = reactive({
  q: '',
});

// 加载数据
const loadData = async () => {
  loading.value = true;
  try {
    const data = await request.get('/Admin/UserConnect', {
      params: {
        pageIndex: currentPage.value,
        pageSize: pageSize.value,
        q: searchForm.q
      }
    });

    // 处理不同的响应格式
    if (data && typeof data === 'object' && 'data' in data && 'page' in data) {
      // 包含分页信息的响应
      tableData.value = Array.isArray(data.data) ? data.data : [];
      total.value = data.page?.totalCount || tableData.value.length;
    } else if (Array.isArray(data)) {
      // 直接返回数组的响应(无分页信息)
      tableData.value = data;
      total.value = data.length;
    } else {
      tableData.value = [];
      total.value = 0;
    }
  } catch {
    tableData.value = [];
    total.value = 0;
  } finally {
    loading.value = false;
  }
};

// 页码变更处理
const handleCurrentChange = (page: number) => {
  currentPage.value = page;
  loadData();
};

// 每页显示条数变更处理
const handleSizeChange = (size: number) => {
  pageSize.value = size;
  currentPage.value = 1;
  loadData();
};

// 搜索
const handleSearch = () => {
  currentPage.value = 1;
  loadData();
};

// 重置搜索
const resetSearch = () => {
  searchForm.q = '';
  currentPage.value = 1;
  loadData();
};

// 删除
const handleDelete = (row: UserConnect) => {
  ElMessageBox.confirm('确认删除该用户连接吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      try {
        await request.delete('/Admin/UserConnect', {
          params: { id: row.id }
        });
        loadData();
      } catch {
        // 错误提示已经在 request 拦截器中自动处理
      }
    })
    .catch(() => { });
};

// 初始化加载数据
onMounted(() => {
  loadData();
});
</script>

<style scoped>
.user-connect-container {
  padding: 20px;
}

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

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>