|
| 1 | +<template> |
| 2 | + <div class="page-container"> |
| 3 | + <div class="page-header"> |
| 4 | + <h2>用户管理</h2> |
| 5 | + <el-button type="primary" icon="Plus" @click="openAdd">新增用户</el-button> |
| 6 | + </div> |
| 7 | + |
| 8 | + <el-card> |
| 9 | + <el-table :data="tableData" stripe v-loading="loading"> |
| 10 | + <el-table-column prop="id" label="ID" width="80" /> |
| 11 | + <el-table-column prop="userName" label="用户名" /> |
| 12 | + <el-table-column prop="createdAt" label="创建时间" width="200" show-overflow-tooltip /> |
| 13 | + <el-table-column prop="updatedAt" label="更新时间" width="200" show-overflow-tooltip /> |
| 14 | + <el-table-column label="操作" width="200" fixed="right"> |
| 15 | + <template #default="{ row }"> |
| 16 | + <el-button size="small" link @click="openReset(row)">重置密码</el-button> |
| 17 | + <el-button size="small" type="danger" link @click="doDelete(row)" |
| 18 | + :disabled="row.id === 1">删除</el-button> |
| 19 | + </template> |
| 20 | + </el-table-column> |
| 21 | + </el-table> |
| 22 | + <el-pagination v-model:current-page="page.num" v-model:page-size="page.size" |
| 23 | + :total="page.total" layout="total,prev,pager,next" style="margin-top:16px;justify-content:flex-end" |
| 24 | + @current-change="loadData" /> |
| 25 | + </el-card> |
| 26 | + |
| 27 | + <!-- 修改我的密码 --> |
| 28 | + <el-card style="margin-top:16px"> |
| 29 | + <template #header><span>修改我的密码</span></template> |
| 30 | + <el-form :model="changePwdForm" label-width="100px" style="max-width:400px"> |
| 31 | + <el-form-item label="原密码"> |
| 32 | + <el-input v-model="changePwdForm.oldPassword" type="password" show-password /> |
| 33 | + </el-form-item> |
| 34 | + <el-form-item label="新密码"> |
| 35 | + <el-input v-model="changePwdForm.newPassword" type="password" show-password /> |
| 36 | + </el-form-item> |
| 37 | + <el-form-item label="确认新密码"> |
| 38 | + <el-input v-model="changePwdForm.confirmPassword" type="password" show-password /> |
| 39 | + </el-form-item> |
| 40 | + <el-form-item> |
| 41 | + <el-button type="primary" @click="doChangePwd">确认修改</el-button> |
| 42 | + </el-form-item> |
| 43 | + </el-form> |
| 44 | + </el-card> |
| 45 | + |
| 46 | + <!-- 新增用户弹窗 --> |
| 47 | + <el-dialog v-model="addVisible" title="新增用户" width="400px"> |
| 48 | + <el-form :model="addForm" label-width="80px"> |
| 49 | + <el-form-item label="用户名"> |
| 50 | + <el-input v-model="addForm.userName" /> |
| 51 | + </el-form-item> |
| 52 | + <el-form-item label="密码"> |
| 53 | + <el-input v-model="addForm.password" type="password" show-password /> |
| 54 | + </el-form-item> |
| 55 | + </el-form> |
| 56 | + <template #footer> |
| 57 | + <el-button @click="addVisible = false">取消</el-button> |
| 58 | + <el-button type="primary" @click="doAdd">确认</el-button> |
| 59 | + </template> |
| 60 | + </el-dialog> |
| 61 | + |
| 62 | + <!-- 重置密码弹窗 --> |
| 63 | + <el-dialog v-model="resetVisible" :title="`重置密码:${resetForm.userName}`" width="400px"> |
| 64 | + <el-form :model="resetForm" label-width="80px"> |
| 65 | + <el-form-item label="新密码"> |
| 66 | + <el-input v-model="resetForm.newPassword" type="password" show-password /> |
| 67 | + </el-form-item> |
| 68 | + </el-form> |
| 69 | + <template #footer> |
| 70 | + <el-button @click="resetVisible = false">取消</el-button> |
| 71 | + <el-button type="primary" @click="doReset">确认重置</el-button> |
| 72 | + </template> |
| 73 | + </el-dialog> |
| 74 | + </div> |
| 75 | +</template> |
| 76 | + |
| 77 | +<script setup lang="ts"> |
| 78 | +import { ref, reactive, onMounted } from 'vue' |
| 79 | +import { ElMessage, ElMessageBox } from 'element-plus' |
| 80 | +import request from '../../utils/request' |
| 81 | +
|
| 82 | +const loading = ref(false) |
| 83 | +const tableData = ref<any[]>([]) |
| 84 | +const page = reactive({ num: 1, size: 10, total: 0 }) |
| 85 | +const addVisible = ref(false) |
| 86 | +const resetVisible = ref(false) |
| 87 | +const addForm = reactive({ userName: '', password: '' }) |
| 88 | +const resetForm = reactive({ id: 0, userName: '', newPassword: '' }) |
| 89 | +const changePwdForm = reactive({ oldPassword: '', newPassword: '', confirmPassword: '' }) |
| 90 | +
|
| 91 | +onMounted(() => loadData()) |
| 92 | +
|
| 93 | +async function loadData() { |
| 94 | + loading.value = true |
| 95 | + try { |
| 96 | + const res: any = await request.post('/user/page', { pageNum: page.num, pageSize: page.size }) |
| 97 | + tableData.value = res.data.records |
| 98 | + page.total = res.data.total |
| 99 | + } finally { loading.value = false } |
| 100 | +} |
| 101 | +
|
| 102 | +function openAdd() { |
| 103 | + addForm.userName = '' |
| 104 | + addForm.password = '' |
| 105 | + addVisible.value = true |
| 106 | +} |
| 107 | +
|
| 108 | +async function doAdd() { |
| 109 | + if (!addForm.userName || !addForm.password) { ElMessage.warning('请填写完整'); return } |
| 110 | + await request.post('/user/add', addForm) |
| 111 | + ElMessage.success('新增成功') |
| 112 | + addVisible.value = false |
| 113 | + loadData() |
| 114 | +} |
| 115 | +
|
| 116 | +function openReset(row: any) { |
| 117 | + resetForm.id = row.id |
| 118 | + resetForm.userName = row.userName |
| 119 | + resetForm.newPassword = '' |
| 120 | + resetVisible.value = true |
| 121 | +} |
| 122 | +
|
| 123 | +async function doReset() { |
| 124 | + if (!resetForm.newPassword) { ElMessage.warning('请输入新密码'); return } |
| 125 | + await request.put('/user/resetPwd', { id: resetForm.id, newPassword: resetForm.newPassword }) |
| 126 | + ElMessage.success('密码重置成功') |
| 127 | + resetVisible.value = false |
| 128 | +} |
| 129 | +
|
| 130 | +async function doDelete(row: any) { |
| 131 | + await ElMessageBox.confirm(`确认删除用户「${row.userName}」?`, '提示', { type: 'warning' }) |
| 132 | + await request.delete(`/user/delete/${row.id}`) |
| 133 | + ElMessage.success('删除成功') |
| 134 | + loadData() |
| 135 | +} |
| 136 | +
|
| 137 | +async function doChangePwd() { |
| 138 | + if (!changePwdForm.oldPassword || !changePwdForm.newPassword) { |
| 139 | + ElMessage.warning('请填写完整') |
| 140 | + return |
| 141 | + } |
| 142 | + if (changePwdForm.newPassword !== changePwdForm.confirmPassword) { |
| 143 | + ElMessage.error('两次密码不一致') |
| 144 | + return |
| 145 | + } |
| 146 | + await request.put('/user/changePwd', { |
| 147 | + oldPassword: changePwdForm.oldPassword, |
| 148 | + newPassword: changePwdForm.newPassword |
| 149 | + }) |
| 150 | + ElMessage.success('密码修改成功,请重新登录') |
| 151 | + changePwdForm.oldPassword = '' |
| 152 | + changePwdForm.newPassword = '' |
| 153 | + changePwdForm.confirmPassword = '' |
| 154 | +} |
| 155 | +</script> |
| 156 | + |
| 157 | +<style scoped> |
| 158 | +.page-container { padding: 20px; } |
| 159 | +.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; } |
| 160 | +.page-header h2 { font-size: 20px; color: #333; } |
| 161 | +</style> |
0 commit comments