From 0ec73cca949451fc4d67e3bf89bb27d71b3d0cfb Mon Sep 17 00:00:00 2001 From: Stev_Wang <304865932@qq.com> Date: Mon, 5 Jan 2026 11:02:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/.env.development | 4 +- frontend/src/layouts/AdminLayout.vue | 2 - frontend/src/stores/admin.ts | 4 - frontend/src/utils/request.ts | 16 ++-- frontend/src/views/admin/UserManagement.vue | 84 +++++++++++++++------ 5 files changed, 72 insertions(+), 38 deletions(-) diff --git a/frontend/.env.development b/frontend/.env.development index d8c7915..db05b6a 100644 --- a/frontend/.env.development +++ b/frontend/.env.development @@ -1,4 +1,4 @@ # 开发环境变量 -# API 基础地址 -VITE_API_BASE_URL=http://localhost:3000 +# API 基础地址(使用代理,设置为空字符串) +VITE_API_BASE_URL= diff --git a/frontend/src/layouts/AdminLayout.vue b/frontend/src/layouts/AdminLayout.vue index 1a7f4a6..20ed9f5 100644 --- a/frontend/src/layouts/AdminLayout.vue +++ b/frontend/src/layouts/AdminLayout.vue @@ -54,8 +54,6 @@ const route = useRoute() const router = useRouter() const adminStore = useAdminStore() -console.log('AdminLayout - adminStore.userInfo:', adminStore.userInfo) - const activeKey = ref(String(route.name)) const menuOptions = [ diff --git a/frontend/src/stores/admin.ts b/frontend/src/stores/admin.ts index b3ec8fc..0fb9e3b 100644 --- a/frontend/src/stores/admin.ts +++ b/frontend/src/stores/admin.ts @@ -30,13 +30,9 @@ export const useAdminStore = defineStore('admin', () => { const login = async (username: string, password: string) => { try { const data = await loginApi(username, password) - console.log('登录API返回的数据:', data) if (data.success && data.data) { - console.log('设置token:', data.data.token) - console.log('设置用户信息:', data.data.user) setToken(data.data.token) setUserInfo(data.data.user) - console.log('登录后userInfo:', userInfo.value) return { success: true, message: data.message || '登录成功' } } return { success: false, message: data.message || '登录失败' } diff --git a/frontend/src/utils/request.ts b/frontend/src/utils/request.ts index 995ec97..84476d4 100644 --- a/frontend/src/utils/request.ts +++ b/frontend/src/utils/request.ts @@ -1,9 +1,16 @@ -import axios from 'axios' +import axios, { type AxiosInstance, type AxiosResponse } from 'axios' +import type { ApiResponse } from '@/types/api' const request = axios.create({ - baseURL: import.meta.env.VITE_API_BASE_URL, + baseURL: import.meta.env.VITE_API_BASE_URL || '', timeout: 10000 -}) +}) as AxiosInstance & { + (config: any): Promise> + get(url: string, config?: any): Promise> + post(url: string, data?: any, config?: any): Promise> + put(url: string, data?: any, config?: any): Promise> + delete(url: string, config?: any): Promise> +} request.interceptors.request.use( (config) => { @@ -26,11 +33,10 @@ request.interceptors.request.use( ) request.interceptors.response.use( - (response) => { + (response: AxiosResponse): any => { return response.data }, (error) => { - console.error('请求错误:', error) return Promise.reject(error) } ) diff --git a/frontend/src/views/admin/UserManagement.vue b/frontend/src/views/admin/UserManagement.vue index 95c5fe9..950adf8 100644 --- a/frontend/src/views/admin/UserManagement.vue +++ b/frontend/src/views/admin/UserManagement.vue @@ -1,7 +1,7 @@ @@ -82,7 +85,8 @@ import { ref, reactive, onMounted, h } from 'vue' import { NCard, NForm, NFormItem, NInput, NSelect, NButton, NSpace, NGrid, NGi, NDataTable, useMessage, useDialog } from 'naive-ui' import { RiAddLine, RiUploadLine, RiEditLine, RiDeleteBinLine, RiLockLine, RiLockUnlockLine } from '@remixicon/vue' -import axios from 'axios' +import request from '@/utils/request' +import type { ApiResponse } from '@/types/api' import UserFormModal from './components/UserFormModal.vue' // 消息提示 @@ -147,8 +151,18 @@ const columns = [ }, row.status === 1 ? '启用' : '禁用') } }, - { title: '创建时间', key: 'createdAt', width: 180 }, - { title: '更新时间', key: 'updatedAt', width: 180 }, + { + title: '创建时间', + key: 'createdAt', + width: 180, + render: (row: any) => formatDate(row.createdAt) + }, + { + title: '更新时间', + key: 'updatedAt', + width: 180, + render: (row: any) => formatDate(row.updatedAt) + }, { title: '操作', key: 'actions', @@ -181,11 +195,23 @@ const showUserFormModal = ref(false) const userFormMode = ref<'create' | 'edit'>('create') const currentUser = ref(null) +// 格式化时间函数 +const formatDate = (dateString: string) => { + if (!dateString) return '' + const date = new Date(dateString) + const year = date.getFullYear() + const month = String(date.getMonth() + 1).padStart(2, '0') + const day = String(date.getDate()).padStart(2, '0') + const hours = String(date.getHours()).padStart(2, '0') + const minutes = String(date.getMinutes()).padStart(2, '0') + return `${year}-${month}-${day} ${hours}:${minutes}` +} + // 获取用户列表 const fetchUserList = async () => { loading.value = true try { - const response = await axios.get('/api/admin/users', { + const response = await request.get('/api/admin/users', { params: { username: searchForm.username || undefined, roleId: searchForm.roleId || undefined, @@ -193,11 +219,11 @@ const fetchUserList = async () => { page: pagination.page, pageSize: pagination.pageSize } - }) + }) as ApiResponse<{ list: any[]; total: number }> - if (response.data.success) { - userList.value = response.data.data.list - pagination.itemCount = response.data.data.total + if (response.success && response.data) { + userList.value = response.data.list + pagination.itemCount = response.data.total } } catch (error) { message.error('获取用户列表失败') @@ -209,10 +235,10 @@ const fetchUserList = async () => { // 获取角色列表 const fetchRoleList = async () => { try { - const response = await axios.get('/api/admin/roles/list') - if (response.data.success) { - roleList.value = response.data.data - roleOptions.value = response.data.data.map((role: any) => ({ + const response = await request.get('/api/admin/roles/list') as ApiResponse + if (response.success && response.data) { + roleList.value = response.data + roleOptions.value = response.data.map((role: any) => ({ label: role.roleName, value: role.id })) @@ -261,7 +287,7 @@ const handleToggleStatus = (row: any) => { negativeText: '取消', onPositiveClick: async () => { try { - await axios.put(`/api/admin/users/${row.id}`, { + await request.put(`/api/admin/users/${row.id}`, { roleId: row.roleId, status: newStatus }) @@ -283,7 +309,7 @@ const handleDelete = (row: any) => { negativeText: '取消', onPositiveClick: async () => { try { - await axios.delete(`/api/admin/users/${row.id}`) + await request.delete(`/api/admin/users/${row.id}`) message.success('删除成功') fetchUserList() } catch (error: any) { @@ -316,6 +342,14 @@ onMounted(() => { padding: 20px; } +.search-card { + margin-bottom: 16px; +} + +.table-card { + margin-bottom: 16px; +} + .action-buttons { margin: 16px 0; }