增加后台管理模块
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<template>
|
||||
<router-view />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
html, body, #app {
|
||||
height: 100%;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,53 @@
|
||||
import request from '@/utils/request'
|
||||
import type {
|
||||
AdminPageRequest,
|
||||
AdminCreateRequest,
|
||||
AdminUpdateRequest,
|
||||
Admin
|
||||
} from '@/types/admin'
|
||||
import type { ApiResponse, PageResult } from '@/types/common'
|
||||
|
||||
// 分页查询管理员
|
||||
export function getAdminPage(params: AdminPageRequest) {
|
||||
return request<ApiResponse<PageResult<Admin>>>({
|
||||
url: '/admin/page',
|
||||
method: 'get',
|
||||
params
|
||||
})
|
||||
}
|
||||
|
||||
// 根据ID获取管理员
|
||||
export function getAdminById(id: string) {
|
||||
return request<ApiResponse<Admin>>({
|
||||
url: '/admin/detail',
|
||||
method: 'get',
|
||||
params: { id }
|
||||
})
|
||||
}
|
||||
|
||||
// 创建管理员
|
||||
export function createAdmin(data: AdminCreateRequest) {
|
||||
return request<ApiResponse<Admin>>({
|
||||
url: '/admin/create',
|
||||
method: 'post',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
// 更新管理员
|
||||
export function updateAdmin(data: AdminUpdateRequest) {
|
||||
return request<ApiResponse<Admin>>({
|
||||
url: '/admin/update',
|
||||
method: 'put',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
// 删除管理员
|
||||
export function deleteAdmin(id: string) {
|
||||
return request<ApiResponse<void>>({
|
||||
url: '/admin/delete',
|
||||
method: 'delete',
|
||||
params: { id }
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import request from '@/utils/request'
|
||||
import type { AdminLoginRequest, AdminAuthResponse } from '@/types/admin'
|
||||
import type { ApiResponse } from '@/types/common'
|
||||
|
||||
// 管理员登录
|
||||
export function login(data: AdminLoginRequest) {
|
||||
return request<ApiResponse<AdminAuthResponse>>({
|
||||
url: '/admin/auth/login',
|
||||
method: 'post',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
// 获取当前管理员信息
|
||||
export function getAdminInfo() {
|
||||
return request({
|
||||
url: '/admin/auth/info',
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 管理员登出
|
||||
export function logout() {
|
||||
return request({
|
||||
url: '/admin/auth/logout',
|
||||
method: 'post'
|
||||
})
|
||||
}
|
||||
|
||||
// 刷新Token
|
||||
export function refreshToken(refreshToken: string) {
|
||||
return request({
|
||||
url: '/admin/auth/refreshToken',
|
||||
method: 'post',
|
||||
data: { refreshToken }
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import request from '@/utils/request'
|
||||
import type { UserPageRequest, User } from '@/types/user'
|
||||
import type { ApiResponse, PageResult } from '@/types/common'
|
||||
|
||||
// 分页查询用户
|
||||
export function getUserPage(params: UserPageRequest) {
|
||||
return request<ApiResponse<PageResult<User>>>({
|
||||
url: '/user/page',
|
||||
method: 'get',
|
||||
params
|
||||
})
|
||||
}
|
||||
|
||||
// 根据ID获取用户
|
||||
export function getUserById(id: string) {
|
||||
return request<ApiResponse<User>>({
|
||||
url: '/user/detail',
|
||||
method: 'get',
|
||||
params: { id }
|
||||
})
|
||||
}
|
||||
|
||||
// 更新用户状态
|
||||
export function updateUserStatus(id: string, status: number) {
|
||||
return request<ApiResponse<void>>({
|
||||
url: '/user/updateStatus',
|
||||
method: 'put',
|
||||
data: { id, status }
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
<template>
|
||||
<el-container class="layout-container">
|
||||
<el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
|
||||
<div class="logo">
|
||||
<span v-if="!isCollapse">情绪博物馆</span>
|
||||
<span v-else>情</span>
|
||||
</div>
|
||||
<el-menu
|
||||
:default-active="activeMenu"
|
||||
:collapse="isCollapse"
|
||||
:unique-opened="true"
|
||||
router
|
||||
>
|
||||
<template v-for="route in menuRoutes" :key="route.path">
|
||||
<el-sub-menu v-if="route.children && route.children.length > 1" :index="route.path">
|
||||
<template #title>
|
||||
<el-icon v-if="route.meta?.icon">
|
||||
<component :is="route.meta.icon" />
|
||||
</el-icon>
|
||||
<span>{{ route.meta?.title }}</span>
|
||||
</template>
|
||||
<el-menu-item
|
||||
v-for="child in route.children"
|
||||
:key="child.path"
|
||||
:index="route.path + '/' + child.path"
|
||||
>
|
||||
<el-icon v-if="child.meta?.icon">
|
||||
<component :is="child.meta.icon" />
|
||||
</el-icon>
|
||||
<span>{{ child.meta?.title }}</span>
|
||||
</el-menu-item>
|
||||
</el-sub-menu>
|
||||
<el-menu-item v-else :index="route.redirect || route.path">
|
||||
<el-icon v-if="route.meta?.icon">
|
||||
<component :is="route.meta.icon" />
|
||||
</el-icon>
|
||||
<span>{{ route.meta?.title }}</span>
|
||||
</el-menu-item>
|
||||
</template>
|
||||
</el-menu>
|
||||
</el-aside>
|
||||
|
||||
<el-container>
|
||||
<el-header class="header">
|
||||
<div class="header-left">
|
||||
<el-icon class="collapse-icon" @click="toggleCollapse">
|
||||
<Fold v-if="!isCollapse" />
|
||||
<Expand v-else />
|
||||
</el-icon>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
<el-dropdown @command="handleCommand">
|
||||
<div class="user-info">
|
||||
<el-avatar :size="32" :src="adminInfo?.avatar">
|
||||
{{ adminInfo?.username?.charAt(0) }}
|
||||
</el-avatar>
|
||||
<span class="username">{{ adminInfo?.username }}</span>
|
||||
</div>
|
||||
<template #dropdown>
|
||||
<el-dropdown-menu>
|
||||
<el-dropdown-item command="profile">个人信息</el-dropdown-item>
|
||||
<el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
|
||||
</el-dropdown-menu>
|
||||
</template>
|
||||
</el-dropdown>
|
||||
</div>
|
||||
</el-header>
|
||||
|
||||
<el-main class="main-content">
|
||||
<router-view />
|
||||
</el-main>
|
||||
</el-container>
|
||||
</el-container>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useAdminStore } from '@/stores/admin'
|
||||
import { Fold, Expand } from '@element-plus/icons-vue'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const adminStore = useAdminStore()
|
||||
|
||||
const isCollapse = ref(false)
|
||||
const adminInfo = computed(() => adminStore.adminInfo)
|
||||
|
||||
const activeMenu = computed(() => route.path)
|
||||
|
||||
const menuRoutes = computed(() => {
|
||||
return router.getRoutes().filter(r =>
|
||||
r.path === '/' || (r.path.startsWith('/') && !r.meta?.hidden && r.component?.name !== 'Layout')
|
||||
)
|
||||
})
|
||||
|
||||
const toggleCollapse = () => {
|
||||
isCollapse.value = !isCollapse.value
|
||||
}
|
||||
|
||||
const handleCommand = (command: string) => {
|
||||
if (command === 'logout') {
|
||||
adminStore.logout()
|
||||
} else if (command === 'profile') {
|
||||
// TODO: 跳转到个人信息页面
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
adminStore.initAdminInfo()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.layout-container {
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
background-color: #304156;
|
||||
transition: width 0.3s;
|
||||
|
||||
.logo {
|
||||
height: 60px;
|
||||
line-height: 60px;
|
||||
text-align: center;
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
color: #fff;
|
||||
background-color: #2b3a4b;
|
||||
}
|
||||
|
||||
:deep(.el-menu) {
|
||||
border-right: none;
|
||||
background-color: #304156;
|
||||
|
||||
.el-menu-item,
|
||||
.el-sub-menu__title {
|
||||
color: #bfcbd9;
|
||||
|
||||
&:hover {
|
||||
background-color: #263445;
|
||||
}
|
||||
|
||||
&.is-active {
|
||||
background-color: #409eff;
|
||||
color: #fff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background-color: #fff;
|
||||
box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
|
||||
padding: 0 20px;
|
||||
|
||||
.header-left {
|
||||
.collapse-icon {
|
||||
font-size: 20px;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover {
|
||||
color: #409eff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.header-right {
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
cursor: pointer;
|
||||
|
||||
.username {
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.main-content {
|
||||
background-color: #f0f2f5;
|
||||
padding: 20px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,20 @@
|
||||
import { createApp } from 'vue'
|
||||
import ElementPlus from 'element-plus'
|
||||
import 'element-plus/dist/index.css'
|
||||
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
import pinia from './stores'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
// 注册所有图标
|
||||
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
||||
app.component(key, component)
|
||||
}
|
||||
|
||||
app.use(ElementPlus)
|
||||
app.use(router)
|
||||
app.use(pinia)
|
||||
|
||||
app.mount('#app')
|
||||
@@ -0,0 +1,84 @@
|
||||
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
|
||||
import Layout from '@/layouts/Layout.vue'
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: () => import('@/views/Login.vue'),
|
||||
meta: { title: '登录', hidden: true }
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: Layout,
|
||||
redirect: '/dashboard',
|
||||
children: [
|
||||
{
|
||||
path: 'dashboard',
|
||||
name: 'Dashboard',
|
||||
component: () => import('@/views/Dashboard.vue'),
|
||||
meta: { title: '仪表盘', icon: 'DataLine' }
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: '/admin',
|
||||
component: Layout,
|
||||
redirect: '/admin/list',
|
||||
meta: { title: '管理员管理', icon: 'User' },
|
||||
children: [
|
||||
{
|
||||
path: 'list',
|
||||
name: 'AdminList',
|
||||
component: () => import('@/views/admin/AdminList.vue'),
|
||||
meta: { title: '管理员列表', icon: 'User' }
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: '/user',
|
||||
component: Layout,
|
||||
redirect: '/user/list',
|
||||
meta: { title: '用户管理', icon: 'UserFilled' },
|
||||
children: [
|
||||
{
|
||||
path: 'list',
|
||||
name: 'UserList',
|
||||
component: () => import('@/views/user/UserList.vue'),
|
||||
meta: { title: '用户列表', icon: 'UserFilled' }
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
name: 'NotFound',
|
||||
component: () => import('@/views/NotFound.vue'),
|
||||
meta: { title: '404', hidden: true }
|
||||
}
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes
|
||||
})
|
||||
|
||||
// 路由守卫
|
||||
router.beforeEach((to, from, next) => {
|
||||
const token = localStorage.getItem('adminToken')
|
||||
|
||||
if (to.path === '/login') {
|
||||
if (token) {
|
||||
next('/')
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
} else {
|
||||
if (token) {
|
||||
next()
|
||||
} else {
|
||||
next('/login')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,79 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import type { Admin, AdminLoginRequest } from '@/types/admin'
|
||||
import { login as loginApi, logout as logoutApi, getAdminInfo } from '@/api/auth'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import router from '@/router'
|
||||
|
||||
export const useAdminStore = defineStore('admin', () => {
|
||||
const token = ref<string>(localStorage.getItem('adminToken') || '')
|
||||
const adminInfo = ref<Admin | null>(null)
|
||||
|
||||
// 登录
|
||||
const login = async (loginForm: AdminLoginRequest) => {
|
||||
try {
|
||||
const res = await loginApi(loginForm)
|
||||
token.value = res.data.accessToken
|
||||
adminInfo.value = res.data.adminInfo
|
||||
|
||||
localStorage.setItem('adminToken', res.data.accessToken)
|
||||
localStorage.setItem('adminRefreshToken', res.data.refreshToken)
|
||||
localStorage.setItem('adminInfo', JSON.stringify(res.data.adminInfo))
|
||||
|
||||
ElMessage.success('登录成功')
|
||||
router.push('/')
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 获取管理员信息
|
||||
const fetchAdminInfo = async () => {
|
||||
try {
|
||||
const res = await getAdminInfo()
|
||||
adminInfo.value = res.data
|
||||
localStorage.setItem('adminInfo', JSON.stringify(res.data))
|
||||
} catch (error) {
|
||||
console.error('获取管理员信息失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 登出
|
||||
const logout = async () => {
|
||||
try {
|
||||
await logoutApi()
|
||||
} catch (error) {
|
||||
console.error('登出失败:', error)
|
||||
} finally {
|
||||
token.value = ''
|
||||
adminInfo.value = null
|
||||
localStorage.removeItem('adminToken')
|
||||
localStorage.removeItem('adminRefreshToken')
|
||||
localStorage.removeItem('adminInfo')
|
||||
router.push('/login')
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化管理员信息
|
||||
const initAdminInfo = () => {
|
||||
const storedInfo = localStorage.getItem('adminInfo')
|
||||
if (storedInfo) {
|
||||
try {
|
||||
adminInfo.value = JSON.parse(storedInfo)
|
||||
} catch (error) {
|
||||
console.error('解析管理员信息失败:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
adminInfo,
|
||||
login,
|
||||
logout,
|
||||
fetchAdminInfo,
|
||||
initAdminInfo
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
import { createPinia } from 'pinia'
|
||||
|
||||
const pinia = createPinia()
|
||||
|
||||
export default pinia
|
||||
@@ -0,0 +1,72 @@
|
||||
// 管理员相关类型定义
|
||||
|
||||
export interface Admin {
|
||||
id: string
|
||||
account: string
|
||||
username: string
|
||||
email?: string
|
||||
phone?: string
|
||||
avatar?: string
|
||||
role: 'super_admin' | 'admin' | 'operator'
|
||||
permissions?: string
|
||||
status: number
|
||||
lastLoginTime?: string
|
||||
lastLoginIp?: string
|
||||
loginCount?: number
|
||||
department?: string
|
||||
position?: string
|
||||
createTime?: string
|
||||
updateTime?: string
|
||||
}
|
||||
|
||||
export interface AdminLoginRequest {
|
||||
account: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export interface AdminAuthResponse {
|
||||
accessToken: string
|
||||
refreshToken: string
|
||||
expiresIn: number
|
||||
adminInfo: Admin
|
||||
loginTime: string
|
||||
}
|
||||
|
||||
export interface AdminPageRequest {
|
||||
current: number
|
||||
size: number
|
||||
account?: string
|
||||
username?: string
|
||||
email?: string
|
||||
phone?: string
|
||||
role?: string
|
||||
status?: number
|
||||
department?: string
|
||||
keyword?: string
|
||||
}
|
||||
|
||||
export interface AdminCreateRequest {
|
||||
account: string
|
||||
password: string
|
||||
username: string
|
||||
email?: string
|
||||
phone?: string
|
||||
avatar?: string
|
||||
role: string
|
||||
permissions?: string
|
||||
department?: string
|
||||
position?: string
|
||||
}
|
||||
|
||||
export interface AdminUpdateRequest {
|
||||
id: string
|
||||
username?: string
|
||||
email?: string
|
||||
phone?: string
|
||||
avatar?: string
|
||||
role?: string
|
||||
permissions?: string
|
||||
status?: number
|
||||
department?: string
|
||||
position?: string
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// 通用类型定义
|
||||
|
||||
export interface ApiResponse<T = any> {
|
||||
code: number
|
||||
message: string
|
||||
data: T
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
export interface PageResult<T = any> {
|
||||
records: T[]
|
||||
total: number
|
||||
current: number
|
||||
size: number
|
||||
pages: number
|
||||
}
|
||||
|
||||
export interface MenuItem {
|
||||
path: string
|
||||
name: string
|
||||
icon?: string
|
||||
children?: MenuItem[]
|
||||
meta?: {
|
||||
title: string
|
||||
icon?: string
|
||||
roles?: string[]
|
||||
hidden?: boolean
|
||||
}
|
||||
}
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_APP_TITLE: string
|
||||
readonly VITE_APP_BASE_API: string
|
||||
readonly VITE_APP_PORT: string
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv
|
||||
}
|
||||
|
||||
declare module '*.vue' {
|
||||
import type { DefineComponent } from 'vue'
|
||||
const component: DefineComponent<{}, {}, any>
|
||||
export default component
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// 用户相关类型定义
|
||||
|
||||
export interface User {
|
||||
id: string
|
||||
account: string
|
||||
username: string
|
||||
email?: string
|
||||
phone?: string
|
||||
avatar?: string
|
||||
nickname?: string
|
||||
status: number
|
||||
memberLevel?: string
|
||||
totalDays?: number
|
||||
lastActiveTime?: string
|
||||
createTime?: string
|
||||
updateTime?: string
|
||||
}
|
||||
|
||||
export interface UserPageRequest {
|
||||
current: number
|
||||
size: number
|
||||
account?: string
|
||||
username?: string
|
||||
email?: string
|
||||
phone?: string
|
||||
status?: number
|
||||
memberLevel?: string
|
||||
keyword?: string
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import router from '@/router'
|
||||
|
||||
const service: AxiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_APP_BASE_API,
|
||||
timeout: 15000
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('adminToken')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => {
|
||||
console.error('请求错误:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
(response: AxiosResponse) => {
|
||||
const res = response.data
|
||||
|
||||
if (res.code !== 200) {
|
||||
ElMessage.error(res.message || '请求失败')
|
||||
|
||||
if (res.code === 401) {
|
||||
localStorage.removeItem('adminToken')
|
||||
localStorage.removeItem('adminInfo')
|
||||
router.push('/login')
|
||||
}
|
||||
|
||||
return Promise.reject(new Error(res.message || '请求失败'))
|
||||
}
|
||||
|
||||
return res
|
||||
},
|
||||
(error) => {
|
||||
console.error('响应错误:', error)
|
||||
|
||||
if (error.response) {
|
||||
const { status, data } = error.response
|
||||
|
||||
if (status === 401) {
|
||||
ElMessage.error('未登录或登录已过期')
|
||||
localStorage.removeItem('adminToken')
|
||||
localStorage.removeItem('adminInfo')
|
||||
router.push('/login')
|
||||
} else if (status === 403) {
|
||||
ElMessage.error('无权限访问')
|
||||
} else if (status === 404) {
|
||||
ElMessage.error('请求的资源不存在')
|
||||
} else if (status === 500) {
|
||||
ElMessage.error(data?.message || '服务器错误')
|
||||
} else {
|
||||
ElMessage.error(data?.message || '请求失败')
|
||||
}
|
||||
} else if (error.request) {
|
||||
ElMessage.error('网络错误,请检查网络连接')
|
||||
} else {
|
||||
ElMessage.error('请求配置错误')
|
||||
}
|
||||
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default service
|
||||
@@ -0,0 +1,25 @@
|
||||
// 本地存储工具
|
||||
|
||||
export const storage = {
|
||||
get(key: string) {
|
||||
const value = localStorage.getItem(key)
|
||||
if (!value) return null
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
},
|
||||
|
||||
set(key: string, value: any) {
|
||||
localStorage.setItem(key, JSON.stringify(value))
|
||||
},
|
||||
|
||||
remove(key: string) {
|
||||
localStorage.removeItem(key)
|
||||
},
|
||||
|
||||
clear() {
|
||||
localStorage.clear()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 表单验证规则
|
||||
|
||||
export const validateAccount = (rule: any, value: string, callback: any) => {
|
||||
if (!value) {
|
||||
callback(new Error('请输入账号'))
|
||||
} else if (value.length < 3 || value.length > 50) {
|
||||
callback(new Error('账号长度必须在3-50个字符之间'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
export const validatePassword = (rule: any, value: string, callback: any) => {
|
||||
if (!value) {
|
||||
callback(new Error('请输入密码'))
|
||||
} else if (value.length < 6 || value.length > 20) {
|
||||
callback(new Error('密码长度必须在6-20个字符之间'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
export const validateEmail = (rule: any, value: string, callback: any) => {
|
||||
if (!value) {
|
||||
callback()
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
|
||||
callback(new Error('邮箱格式不正确'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
export const validatePhone = (rule: any, value: string, callback: any) => {
|
||||
if (!value) {
|
||||
callback()
|
||||
} else if (!/^1[3-9]\d{9}$/.test(value)) {
|
||||
callback(new Error('手机号格式不正确'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<template>
|
||||
<div class="dashboard">
|
||||
<h2 class="page-title">仪表盘</h2>
|
||||
|
||||
<el-row :gutter="20" class="stats-row">
|
||||
<el-col :span="6">
|
||||
<el-card class="stat-card">
|
||||
<div class="stat-content">
|
||||
<div class="stat-icon" style="background-color: #409eff;">
|
||||
<el-icon><User /></el-icon>
|
||||
</div>
|
||||
<div class="stat-info">
|
||||
<div class="stat-value">1,234</div>
|
||||
<div class="stat-label">用户总数</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="6">
|
||||
<el-card class="stat-card">
|
||||
<div class="stat-content">
|
||||
<div class="stat-icon" style="background-color: #67c23a;">
|
||||
<el-icon><UserFilled /></el-icon>
|
||||
</div>
|
||||
<div class="stat-info">
|
||||
<div class="stat-value">5</div>
|
||||
<div class="stat-label">管理员总数</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="6">
|
||||
<el-card class="stat-card">
|
||||
<div class="stat-content">
|
||||
<div class="stat-icon" style="background-color: #e6a23c;">
|
||||
<el-icon><TrendCharts /></el-icon>
|
||||
</div>
|
||||
<div class="stat-info">
|
||||
<div class="stat-value">89</div>
|
||||
<div class="stat-label">今日活跃</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="6">
|
||||
<el-card class="stat-card">
|
||||
<div class="stat-content">
|
||||
<div class="stat-icon" style="background-color: #f56c6c;">
|
||||
<el-icon><ChatDotRound /></el-icon>
|
||||
</div>
|
||||
<div class="stat-info">
|
||||
<div class="stat-value">456</div>
|
||||
<div class="stat-label">对话总数</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
<el-row :gutter="20" class="chart-row">
|
||||
<el-col :span="16">
|
||||
<el-card>
|
||||
<template #header>
|
||||
<span>用户增长趋势</span>
|
||||
</template>
|
||||
<div class="chart-container" ref="userChartRef"></div>
|
||||
</el-card>
|
||||
</el-col>
|
||||
|
||||
<el-col :span="8">
|
||||
<el-card>
|
||||
<template #header>
|
||||
<span>最近登录</span>
|
||||
</template>
|
||||
<el-table :data="recentLogins" style="width: 100%">
|
||||
<el-table-column prop="username" label="用户" />
|
||||
<el-table-column prop="time" label="时间" />
|
||||
</el-table>
|
||||
</el-card>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { User, UserFilled, TrendCharts, ChatDotRound } from '@element-plus/icons-vue'
|
||||
import * as echarts from 'echarts'
|
||||
|
||||
const userChartRef = ref<HTMLElement>()
|
||||
|
||||
const recentLogins = ref([
|
||||
{ username: '张三', time: '2分钟前' },
|
||||
{ username: '李四', time: '5分钟前' },
|
||||
{ username: '王五', time: '10分钟前' },
|
||||
{ username: '赵六', time: '15分钟前' }
|
||||
])
|
||||
|
||||
onMounted(() => {
|
||||
initUserChart()
|
||||
})
|
||||
|
||||
const initUserChart = () => {
|
||||
if (!userChartRef.value) return
|
||||
|
||||
const chart = echarts.init(userChartRef.value)
|
||||
|
||||
const option = {
|
||||
tooltip: {
|
||||
trigger: 'axis'
|
||||
},
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value'
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: '新增用户',
|
||||
type: 'line',
|
||||
data: [12, 23, 18, 29, 35, 42, 38],
|
||||
smooth: true,
|
||||
itemStyle: {
|
||||
color: '#409eff'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
chart.setOption(option)
|
||||
|
||||
window.addEventListener('resize', () => {
|
||||
chart.resize()
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.dashboard {
|
||||
.page-title {
|
||||
margin-bottom: 20px;
|
||||
font-size: 24px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.stats-row {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
.stat-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
|
||||
.stat-icon {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
border-radius: 10px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #fff;
|
||||
font-size: 28px;
|
||||
}
|
||||
|
||||
.stat-info {
|
||||
flex: 1;
|
||||
|
||||
.stat-value {
|
||||
font-size: 28px;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 14px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.chart-container {
|
||||
height: 300px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,142 @@
|
||||
<template>
|
||||
<div class="login-container">
|
||||
<div class="login-box">
|
||||
<div class="login-header">
|
||||
<h2>情绪博物馆管理后台</h2>
|
||||
<p>Emotion Museum Admin</p>
|
||||
</div>
|
||||
|
||||
<el-form
|
||||
ref="loginFormRef"
|
||||
:model="loginForm"
|
||||
:rules="loginRules"
|
||||
class="login-form"
|
||||
@keyup.enter="handleLogin"
|
||||
>
|
||||
<el-form-item prop="account">
|
||||
<el-input
|
||||
v-model="loginForm.account"
|
||||
placeholder="请输入账号"
|
||||
size="large"
|
||||
prefix-icon="User"
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item prop="password">
|
||||
<el-input
|
||||
v-model="loginForm.password"
|
||||
type="password"
|
||||
placeholder="请输入密码"
|
||||
size="large"
|
||||
prefix-icon="Lock"
|
||||
show-password
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item>
|
||||
<el-button
|
||||
type="primary"
|
||||
size="large"
|
||||
:loading="loading"
|
||||
class="login-button"
|
||||
@click="handleLogin"
|
||||
>
|
||||
登录
|
||||
</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<div class="login-footer">
|
||||
<p>默认账号: admin / admin123</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive } from 'vue'
|
||||
import { useAdminStore } from '@/stores/admin'
|
||||
import type { FormInstance, FormRules } from 'element-plus'
|
||||
import { validateAccount, validatePassword } from '@/utils/validate'
|
||||
|
||||
const adminStore = useAdminStore()
|
||||
const loginFormRef = ref<FormInstance>()
|
||||
const loading = ref(false)
|
||||
|
||||
const loginForm = reactive({
|
||||
account: '',
|
||||
password: ''
|
||||
})
|
||||
|
||||
const loginRules: FormRules = {
|
||||
account: [{ required: true, validator: validateAccount, trigger: 'blur' }],
|
||||
password: [{ required: true, validator: validatePassword, trigger: 'blur' }]
|
||||
}
|
||||
|
||||
const handleLogin = async () => {
|
||||
if (!loginFormRef.value) return
|
||||
|
||||
await loginFormRef.value.validate(async (valid) => {
|
||||
if (valid) {
|
||||
loading.value = true
|
||||
try {
|
||||
await adminStore.login(loginForm)
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.login-container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
}
|
||||
|
||||
.login-box {
|
||||
width: 400px;
|
||||
padding: 40px;
|
||||
background: #fff;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.login-header {
|
||||
text-align: center;
|
||||
margin-bottom: 30px;
|
||||
|
||||
h2 {
|
||||
font-size: 24px;
|
||||
color: #333;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: 14px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
.login-form {
|
||||
.login-button {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.login-footer {
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
|
||||
p {
|
||||
font-size: 12px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,45 @@
|
||||
<template>
|
||||
<div class="not-found">
|
||||
<div class="content">
|
||||
<h1>404</h1>
|
||||
<p>页面不存在</p>
|
||||
<el-button type="primary" @click="goHome">返回首页</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useRouter } from 'vue-router'
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
const goHome = () => {
|
||||
router.push('/')
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.not-found {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
background-color: #f0f2f5;
|
||||
|
||||
.content {
|
||||
text-align: center;
|
||||
|
||||
h1 {
|
||||
font-size: 120px;
|
||||
color: #409eff;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: 24px;
|
||||
color: #666;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,318 @@
|
||||
<template>
|
||||
<div class="admin-list">
|
||||
<h2 class="page-title">管理员列表</h2>
|
||||
|
||||
<el-card class="search-card">
|
||||
<el-form :model="searchForm" :inline="true">
|
||||
<el-form-item label="账号">
|
||||
<el-input v-model="searchForm.account" placeholder="请输入账号" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item label="姓名">
|
||||
<el-input v-model="searchForm.username" placeholder="请输入姓名" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item label="角色">
|
||||
<el-select v-model="searchForm.role" placeholder="请选择角色" clearable>
|
||||
<el-option label="超级管理员" value="super_admin" />
|
||||
<el-option label="管理员" value="admin" />
|
||||
<el-option label="运营" value="operator" />
|
||||
</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>
|
||||
</el-card>
|
||||
|
||||
<el-card class="table-card">
|
||||
<template #header>
|
||||
<div class="card-header">
|
||||
<span>管理员列表</span>
|
||||
<el-button type="primary" @click="handleAdd">新增管理员</el-button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<el-table :data="tableData" v-loading="loading" stripe>
|
||||
<el-table-column prop="account" label="账号" />
|
||||
<el-table-column prop="username" label="姓名" />
|
||||
<el-table-column prop="email" label="邮箱" />
|
||||
<el-table-column prop="phone" label="手机号" />
|
||||
<el-table-column prop="role" label="角色">
|
||||
<template #default="{ row }">
|
||||
<el-tag v-if="row.role === 'super_admin'" type="danger">超级管理员</el-tag>
|
||||
<el-tag v-else-if="row.role === 'admin'" type="primary">管理员</el-tag>
|
||||
<el-tag v-else type="info">运营</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="status" label="状态">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
|
||||
{{ row.status === 1 ? '正常' : '禁用' }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="createTime" label="创建时间" />
|
||||
<el-table-column label="操作" width="200" fixed="right">
|
||||
<template #default="{ row }">
|
||||
<el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
|
||||
<el-button type="danger" link @click="handleDelete(row)">删除</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<el-pagination
|
||||
v-model:current-page="pagination.current"
|
||||
v-model:page-size="pagination.size"
|
||||
:total="pagination.total"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
layout="total, sizes, prev, pager, next, jumper"
|
||||
@size-change="fetchData"
|
||||
@current-change="fetchData"
|
||||
class="pagination"
|
||||
/>
|
||||
</el-card>
|
||||
|
||||
<!-- 新增/编辑对话框 -->
|
||||
<el-dialog
|
||||
v-model="dialogVisible"
|
||||
:title="dialogTitle"
|
||||
width="600px"
|
||||
@close="handleDialogClose"
|
||||
>
|
||||
<el-form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:rules="formRules"
|
||||
label-width="100px"
|
||||
>
|
||||
<el-form-item label="账号" prop="account">
|
||||
<el-input v-model="formData.account" :disabled="isEdit" />
|
||||
</el-form-item>
|
||||
<el-form-item label="密码" prop="password" v-if="!isEdit">
|
||||
<el-input v-model="formData.password" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="姓名" prop="username">
|
||||
<el-input v-model="formData.username" />
|
||||
</el-form-item>
|
||||
<el-form-item label="邮箱" prop="email">
|
||||
<el-input v-model="formData.email" />
|
||||
</el-form-item>
|
||||
<el-form-item label="手机号" prop="phone">
|
||||
<el-input v-model="formData.phone" />
|
||||
</el-form-item>
|
||||
<el-form-item label="角色" prop="role">
|
||||
<el-select v-model="formData.role" style="width: 100%">
|
||||
<el-option label="超级管理员" value="super_admin" />
|
||||
<el-option label="管理员" value="admin" />
|
||||
<el-option label="运营" value="operator" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="部门" prop="department">
|
||||
<el-input v-model="formData.department" />
|
||||
</el-form-item>
|
||||
<el-form-item label="职位" prop="position">
|
||||
<el-input v-model="formData.position" />
|
||||
</el-form-item>
|
||||
<el-form-item label="状态" prop="status" v-if="isEdit">
|
||||
<el-radio-group v-model="formData.status">
|
||||
<el-radio :label="1">正常</el-radio>
|
||||
<el-radio :label="0">禁用</el-radio>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<template #footer>
|
||||
<el-button @click="dialogVisible = false">取消</el-button>
|
||||
<el-button type="primary" @click="handleSubmit" :loading="submitLoading">
|
||||
确定
|
||||
</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
|
||||
import { getAdminPage, createAdmin, updateAdmin, deleteAdmin } from '@/api/admin'
|
||||
import type { Admin, AdminPageRequest } from '@/types/admin'
|
||||
import { validateAccount, validatePassword, validateEmail, validatePhone } from '@/utils/validate'
|
||||
|
||||
const loading = ref(false)
|
||||
const submitLoading = ref(false)
|
||||
const dialogVisible = ref(false)
|
||||
const isEdit = ref(false)
|
||||
const formRef = ref<FormInstance>()
|
||||
|
||||
const searchForm = reactive<AdminPageRequest>({
|
||||
current: 1,
|
||||
size: 10
|
||||
})
|
||||
|
||||
const pagination = reactive({
|
||||
current: 1,
|
||||
size: 10,
|
||||
total: 0
|
||||
})
|
||||
|
||||
const tableData = ref<Admin[]>([])
|
||||
|
||||
const formData = reactive({
|
||||
id: '',
|
||||
account: '',
|
||||
password: '',
|
||||
username: '',
|
||||
email: '',
|
||||
phone: '',
|
||||
role: 'admin',
|
||||
department: '',
|
||||
position: '',
|
||||
status: 1
|
||||
})
|
||||
|
||||
const formRules: FormRules = {
|
||||
account: [{ required: true, validator: validateAccount, trigger: 'blur' }],
|
||||
password: [{ required: true, validator: validatePassword, trigger: 'blur' }],
|
||||
username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
|
||||
email: [{ validator: validateEmail, trigger: 'blur' }],
|
||||
phone: [{ validator: validatePhone, trigger: 'blur' }],
|
||||
role: [{ required: true, message: '请选择角色', trigger: 'change' }]
|
||||
}
|
||||
|
||||
const dialogTitle = ref('新增管理员')
|
||||
|
||||
const fetchData = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const params = {
|
||||
...searchForm,
|
||||
current: pagination.current,
|
||||
size: pagination.size
|
||||
}
|
||||
const res = await getAdminPage(params)
|
||||
tableData.value = res.data.records
|
||||
pagination.total = res.data.total
|
||||
} catch (error) {
|
||||
console.error('获取管理员列表失败:', error)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearch = () => {
|
||||
pagination.current = 1
|
||||
fetchData()
|
||||
}
|
||||
|
||||
const handleReset = () => {
|
||||
Object.assign(searchForm, {
|
||||
current: 1,
|
||||
size: 10,
|
||||
account: '',
|
||||
username: '',
|
||||
role: ''
|
||||
})
|
||||
fetchData()
|
||||
}
|
||||
|
||||
const handleAdd = () => {
|
||||
isEdit.value = false
|
||||
dialogTitle.value = '新增管理员'
|
||||
dialogVisible.value = true
|
||||
}
|
||||
|
||||
const handleEdit = (row: Admin) => {
|
||||
isEdit.value = true
|
||||
dialogTitle.value = '编辑管理员'
|
||||
Object.assign(formData, row)
|
||||
dialogVisible.value = true
|
||||
}
|
||||
|
||||
const handleDelete = (row: Admin) => {
|
||||
ElMessageBox.confirm('确定要删除该管理员吗?', '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(async () => {
|
||||
try {
|
||||
await deleteAdmin(row.id)
|
||||
ElMessage.success('删除成功')
|
||||
fetchData()
|
||||
} catch (error) {
|
||||
console.error('删除失败:', error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!formRef.value) return
|
||||
|
||||
await formRef.value.validate(async (valid) => {
|
||||
if (valid) {
|
||||
submitLoading.value = true
|
||||
try {
|
||||
if (isEdit.value) {
|
||||
await updateAdmin(formData)
|
||||
ElMessage.success('更新成功')
|
||||
} else {
|
||||
await createAdmin(formData)
|
||||
ElMessage.success('创建成功')
|
||||
}
|
||||
dialogVisible.value = false
|
||||
fetchData()
|
||||
} catch (error) {
|
||||
console.error('提交失败:', error)
|
||||
} finally {
|
||||
submitLoading.value = false
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const handleDialogClose = () => {
|
||||
formRef.value?.resetFields()
|
||||
Object.assign(formData, {
|
||||
id: '',
|
||||
account: '',
|
||||
password: '',
|
||||
username: '',
|
||||
email: '',
|
||||
phone: '',
|
||||
role: 'admin',
|
||||
department: '',
|
||||
position: '',
|
||||
status: 1
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
fetchData()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.admin-list {
|
||||
.page-title {
|
||||
margin-bottom: 20px;
|
||||
font-size: 24px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.search-card {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.table-card {
|
||||
.card-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.pagination {
|
||||
margin-top: 20px;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,206 @@
|
||||
<template>
|
||||
<div class="user-list">
|
||||
<h2 class="page-title">用户列表</h2>
|
||||
|
||||
<el-card class="search-card">
|
||||
<el-form :model="searchForm" :inline="true">
|
||||
<el-form-item label="账号">
|
||||
<el-input v-model="searchForm.account" placeholder="请输入账号" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item label="用户名">
|
||||
<el-input v-model="searchForm.username" placeholder="请输入用户名" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item label="手机号">
|
||||
<el-input v-model="searchForm.phone" placeholder="请输入手机号" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item label="状态">
|
||||
<el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
|
||||
<el-option label="正常" :value="1" />
|
||||
<el-option label="禁用" :value="0" />
|
||||
</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>
|
||||
</el-card>
|
||||
|
||||
<el-card class="table-card">
|
||||
<template #header>
|
||||
<span>用户列表</span>
|
||||
</template>
|
||||
|
||||
<el-table :data="tableData" v-loading="loading" stripe>
|
||||
<el-table-column prop="account" label="账号" />
|
||||
<el-table-column prop="username" label="用户名" />
|
||||
<el-table-column prop="nickname" label="昵称" />
|
||||
<el-table-column prop="phone" label="手机号" />
|
||||
<el-table-column prop="email" label="邮箱" />
|
||||
<el-table-column prop="memberLevel" label="会员等级">
|
||||
<template #default="{ row }">
|
||||
<el-tag v-if="row.memberLevel === 'vip'" type="warning">VIP</el-tag>
|
||||
<el-tag v-else type="info">普通</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="status" label="状态">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
|
||||
{{ row.status === 1 ? '正常' : '禁用' }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="createTime" label="注册时间" />
|
||||
<el-table-column label="操作" width="200" fixed="right">
|
||||
<template #default="{ row }">
|
||||
<el-button type="primary" link @click="handleView(row)">查看</el-button>
|
||||
<el-button
|
||||
:type="row.status === 1 ? 'warning' : 'success'"
|
||||
link
|
||||
@click="handleToggleStatus(row)"
|
||||
>
|
||||
{{ row.status === 1 ? '禁用' : '启用' }}
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<el-pagination
|
||||
v-model:current-page="pagination.current"
|
||||
v-model:page-size="pagination.size"
|
||||
:total="pagination.total"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
layout="total, sizes, prev, pager, next, jumper"
|
||||
@size-change="fetchData"
|
||||
@current-change="fetchData"
|
||||
class="pagination"
|
||||
/>
|
||||
</el-card>
|
||||
|
||||
<!-- 用户详情对话框 -->
|
||||
<el-dialog v-model="detailVisible" title="用户详情" width="600px">
|
||||
<el-descriptions :column="2" border v-if="currentUser">
|
||||
<el-descriptions-item label="账号">{{ currentUser.account }}</el-descriptions-item>
|
||||
<el-descriptions-item label="用户名">{{ currentUser.username }}</el-descriptions-item>
|
||||
<el-descriptions-item label="昵称">{{ currentUser.nickname }}</el-descriptions-item>
|
||||
<el-descriptions-item label="手机号">{{ currentUser.phone }}</el-descriptions-item>
|
||||
<el-descriptions-item label="邮箱">{{ currentUser.email }}</el-descriptions-item>
|
||||
<el-descriptions-item label="会员等级">{{ currentUser.memberLevel }}</el-descriptions-item>
|
||||
<el-descriptions-item label="使用天数">{{ currentUser.totalDays }}</el-descriptions-item>
|
||||
<el-descriptions-item label="状态">
|
||||
<el-tag :type="currentUser.status === 1 ? 'success' : 'danger'">
|
||||
{{ currentUser.status === 1 ? '正常' : '禁用' }}
|
||||
</el-tag>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="注册时间" :span="2">{{ currentUser.createTime }}</el-descriptions-item>
|
||||
<el-descriptions-item label="最后活跃" :span="2">{{ currentUser.lastActiveTime }}</el-descriptions-item>
|
||||
</el-descriptions>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { getUserPage, updateUserStatus } from '@/api/user'
|
||||
import type { User, UserPageRequest } from '@/types/user'
|
||||
|
||||
const loading = ref(false)
|
||||
const detailVisible = ref(false)
|
||||
const currentUser = ref<User | null>(null)
|
||||
|
||||
const searchForm = reactive<UserPageRequest>({
|
||||
current: 1,
|
||||
size: 10
|
||||
})
|
||||
|
||||
const pagination = reactive({
|
||||
current: 1,
|
||||
size: 10,
|
||||
total: 0
|
||||
})
|
||||
|
||||
const tableData = ref<User[]>([])
|
||||
|
||||
const fetchData = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const params = {
|
||||
...searchForm,
|
||||
current: pagination.current,
|
||||
size: pagination.size
|
||||
}
|
||||
const res = await getUserPage(params)
|
||||
tableData.value = res.data.records
|
||||
pagination.total = res.data.total
|
||||
} catch (error) {
|
||||
console.error('获取用户列表失败:', error)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearch = () => {
|
||||
pagination.current = 1
|
||||
fetchData()
|
||||
}
|
||||
|
||||
const handleReset = () => {
|
||||
Object.assign(searchForm, {
|
||||
current: 1,
|
||||
size: 10,
|
||||
account: '',
|
||||
username: '',
|
||||
phone: '',
|
||||
status: undefined
|
||||
})
|
||||
fetchData()
|
||||
}
|
||||
|
||||
const handleView = (row: User) => {
|
||||
currentUser.value = row
|
||||
detailVisible.value = true
|
||||
}
|
||||
|
||||
const handleToggleStatus = (row: User) => {
|
||||
const action = row.status === 1 ? '禁用' : '启用'
|
||||
ElMessageBox.confirm(`确定要${action}该用户吗?`, '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(async () => {
|
||||
try {
|
||||
await updateUserStatus(row.id, row.status === 1 ? 0 : 1)
|
||||
ElMessage.success(`${action}成功`)
|
||||
fetchData()
|
||||
} catch (error) {
|
||||
console.error(`${action}失败:`, error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
fetchData()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.user-list {
|
||||
.page-title {
|
||||
margin-bottom: 20px;
|
||||
font-size: 24px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.search-card {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.table-card {
|
||||
.pagination {
|
||||
margin-top: 20px;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user