461 lines
13 KiB
Vue
461 lines
13 KiB
Vue
<script>
|
||
import {Delete, Edit, Search, User, Document} from "@element-plus/icons-vue";
|
||
import {mapState, mapActions, mapMutations} from "vuex";
|
||
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
||
import {copy, fuzzyMatching, splitKeyWords, showPassword, hidePassword, refreshData} from "@/utils/common.js";
|
||
import request, {frontendUser, frontendUsers} from "@/utils/request.js";
|
||
|
||
|
||
export default {
|
||
name: "ZooUser",
|
||
components: {ZooUserFormDialog},
|
||
data() {
|
||
return {
|
||
//表格分页显示的数据
|
||
pagesData: [],
|
||
pageSize: 20,//每页有多少条数据
|
||
currentPage: 1,//当前页
|
||
|
||
//搜索框的输入
|
||
searchInput: {
|
||
option: '',
|
||
keyword: ''
|
||
},
|
||
|
||
// 控制弹窗的属性
|
||
dialog: {
|
||
dialogData: {},//弹窗表单数据
|
||
addDialogVisible: false,//添加弹窗
|
||
editDialogVisible: false,//编辑弹窗
|
||
detailDialogVisible: false,//详情弹窗
|
||
},
|
||
}
|
||
},
|
||
computed: {
|
||
//返回图标
|
||
// region
|
||
Search() {
|
||
return Search
|
||
},
|
||
User() {
|
||
return User
|
||
},
|
||
Delete() {
|
||
return Delete
|
||
},
|
||
Edit() {
|
||
return Edit
|
||
},
|
||
Document() {
|
||
return Document
|
||
},
|
||
|
||
// endregion
|
||
...mapState(["users", 'usersTableData'])
|
||
},
|
||
watch: {
|
||
// 搜索框存在输入,自动调用搜索(刷新数据包含搜索)
|
||
searchInput: {
|
||
deep: true,
|
||
handler(val) {
|
||
this.refresh();
|
||
}
|
||
}
|
||
},
|
||
methods: {
|
||
hidePassword,
|
||
showPassword,
|
||
...mapActions(['getUsers']),
|
||
...mapMutations(['updateUsers','updateUsersTableData']),
|
||
|
||
//显示详情弹窗
|
||
showDetail(data) {
|
||
console.log('显示详情', data)
|
||
|
||
//从后端获取需要查询的数据,防止数据前后不一致
|
||
request.queryUserRequest(data.username).then(response => {
|
||
if (response.data.data === null) {
|
||
this.refresh()
|
||
return ElMessage({
|
||
message: '该用户不存在,请刷新',
|
||
type: 'warning',
|
||
})
|
||
}else {
|
||
this.dialog.dialogData = frontendUser(response.data.data)
|
||
// 显示编辑弹窗
|
||
this.dialog.detailDialogVisible = true
|
||
}
|
||
})
|
||
},
|
||
|
||
// 显示编辑弹窗
|
||
showEdit(data) {
|
||
console.log('编辑弹窗', data)
|
||
|
||
//从后端获取需要查询的数据,防止数据前后不一致
|
||
request.queryUserRequest(data.username).then(response => {
|
||
if (response.data.data === null) {
|
||
this.refresh()
|
||
return ElMessage({
|
||
message: '该用户不存在,请刷新',
|
||
type: 'warning',
|
||
})
|
||
}else {
|
||
this.dialog.dialogData = frontendUser(response.data.data)
|
||
// 显示编辑弹窗
|
||
this.dialog.editDialogVisible = true
|
||
}
|
||
})
|
||
},
|
||
|
||
edit(data) {
|
||
console.log('编辑', data)
|
||
|
||
//判断表单是否为空
|
||
if (this.isEmpty(data)) {
|
||
return;
|
||
}
|
||
|
||
//这里将数据传给后端进行数据更新
|
||
//从后端获取需要查询的数据,防止数据前后不一致
|
||
request.queryUserRequest(data.username).then(response => {
|
||
if (response.data.data === null) {
|
||
this.refresh()
|
||
this.dialog.editDialogVisible = false;
|
||
return ElMessage({
|
||
message: '该用户不存在,请刷新',
|
||
type: 'warning',
|
||
})
|
||
}else {
|
||
request.updateUserRequest(data).then(response => {
|
||
if (response.data.code===1){
|
||
// 编辑成功
|
||
// 刷新数据
|
||
this.refresh()
|
||
|
||
this.dialog.editDialogVisible = false//关闭窗口
|
||
|
||
return ElMessage({
|
||
message: '编辑成功',
|
||
type: 'success',
|
||
})
|
||
}else {
|
||
return ElMessage({
|
||
message: '编辑失败',
|
||
type: 'warning',
|
||
})
|
||
}
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
delete_(data) {
|
||
console.log('删除', data)
|
||
|
||
return ElMessageBox.confirm(
|
||
'该操作不可撤销,是否继续?',
|
||
'删除用户:' + data.username + '(' + data.id + ')',
|
||
{
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning',
|
||
}
|
||
).then(() => {
|
||
//从后端获取需要查询的数据,防止数据前后不一致
|
||
request.queryUserRequest(data.username).then(response => {
|
||
if (response.data.data===null){
|
||
this.refresh();
|
||
return ElMessage({
|
||
message: '该用户不存在,请刷新',
|
||
type: 'warning',
|
||
})
|
||
}else {
|
||
//进行删除操作
|
||
request.deleteUserRequest(data.id).then(response => {
|
||
if (response.data.code===1){
|
||
// 刷新数据
|
||
this.refresh()
|
||
|
||
//返回提示
|
||
return ElMessage({
|
||
message: '删除成功',
|
||
type: 'success',
|
||
})
|
||
}else {
|
||
return ElMessage({
|
||
message: '删除失败',
|
||
type: 'warning',
|
||
})
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}).catch(() => {
|
||
ElMessage({
|
||
type: 'info',
|
||
message: '取消删除',
|
||
})
|
||
})
|
||
},
|
||
|
||
add(data) {
|
||
console.log('添加', data)
|
||
|
||
//验证是否为空
|
||
if (this.isEmpty(data)) {
|
||
return;
|
||
}
|
||
|
||
//先查询用户是否存在
|
||
request.queryUserRequest(data.username).then(response => {
|
||
if (response.data.data!==null){
|
||
return ElMessage({
|
||
message: '用户名重复',
|
||
type: 'warning',
|
||
})
|
||
}else {
|
||
//添加成功
|
||
request.addUserRequest(data).then(response => {
|
||
if (response.data.code===1){
|
||
// 刷新数据
|
||
this.refresh()
|
||
|
||
this.dialog.addDialogVisible = false//添加成功关闭窗口
|
||
|
||
return ElMessage({
|
||
message: '添加成功',
|
||
type: 'success',
|
||
})
|
||
}else {
|
||
return ElMessage({
|
||
message: '添加失败',
|
||
type: 'warning',
|
||
})
|
||
}
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 判断表单是否为空
|
||
isEmpty(form) {
|
||
if (!form.username || !form.password) {
|
||
//返回提示
|
||
return ElMessage({
|
||
message: '用户名和密码不能为空',
|
||
type: 'warning',
|
||
})
|
||
}
|
||
},
|
||
|
||
search() {
|
||
console.log('搜索', this.searchInput)
|
||
|
||
let searchResult = []//搜索结果数组
|
||
|
||
//第一步匹配搜索选项
|
||
if (typeof this.searchInput.option === 'number') {
|
||
this.users.forEach(e => {
|
||
if (e.auth === this.searchInput.option) {
|
||
searchResult.push(copy(e))//如果符合特定权限,则添加到搜索结果
|
||
}
|
||
})
|
||
} else { //如果没有选择特定用户身份,代表所有数据都需要参加到下一轮的模糊搜索
|
||
searchResult = copy(this.users)
|
||
}
|
||
|
||
let searchResult2 = []
|
||
let keyset = splitKeyWords(this.searchInput.keyword)//切割关键字
|
||
|
||
//第二步进行模糊搜索
|
||
searchResult.forEach(e => {
|
||
//如果该对象能匹配上所有关键字,则添加到结果
|
||
// console.log('开始进行模糊匹配,对象、关键字集合分别为',e,keyset)
|
||
let is = fuzzyMatching(e, keyset);
|
||
if (is) {
|
||
searchResult2.push(e)
|
||
}
|
||
// console.log('结束模糊匹配,结果为',e,keyset,is)
|
||
})
|
||
searchResult = searchResult2
|
||
|
||
this.updateUsersTableData(searchResult)
|
||
// this.getPagesData()
|
||
},
|
||
|
||
//当前页切换时触发
|
||
handleCurrentChange(val) {
|
||
console.log('分页被切换',val)
|
||
this.currentPage = val
|
||
this.getPagesData()
|
||
},
|
||
|
||
getPagesData() {
|
||
console.log('获取分页数据')
|
||
const start = (this.currentPage - 1) * this.pageSize
|
||
const end = start + this.pageSize
|
||
this.pagesData = this.usersTableData.slice(start, end)
|
||
},
|
||
|
||
//刷新数据的方法
|
||
refresh(){
|
||
console.log('刷新数据')
|
||
|
||
//先拉取一遍数据
|
||
request.queryUserRequest().then(response => {
|
||
if (response.data.code === 1) {
|
||
this.updateUsers(frontendUsers(response.data.data))
|
||
}
|
||
//可能输入了搜索关键字,故执行一次搜索
|
||
this.search()
|
||
|
||
//获取结果的分页
|
||
this.getPagesData()
|
||
})
|
||
}
|
||
},
|
||
mounted() {
|
||
this.refresh()
|
||
refreshData();
|
||
},
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div id="user-root">
|
||
<!-- 头部-->
|
||
<div class="select">
|
||
<div class="left">
|
||
<el-button type="primary" :icon="User" @click="dialog.addDialogVisible = true">创建用户</el-button>
|
||
</div>
|
||
<div class="right">
|
||
<el-input
|
||
v-model="searchInput.keyword"
|
||
style="width: 500px"
|
||
placeholder="搜索用户 空格隔开关键字"
|
||
>
|
||
<template #prepend>
|
||
<el-select v-model="searchInput.option" placeholder="身份" style="width: 90px">
|
||
<el-option label="不选择" value=""/>
|
||
<el-option label="管理员" :value="0"/>
|
||
<el-option label="饲养员" :value="1"/>
|
||
<el-option label="兽医" :value="2"/>
|
||
</el-select>
|
||
</template>
|
||
<template #append>
|
||
<el-button :icon="Search" @click="refresh()"/>
|
||
</template>
|
||
</el-input>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 数据-->
|
||
<div class="table">
|
||
<el-table :data="pagesData" style="height: 100%" border stripe :row-style="{height: '40px'}" :cell-style="{padding:'0'}">
|
||
<el-table-column fixed prop="id" label="用户ID" width="70"/>
|
||
<el-table-column fixed prop="username" label="用户名" width="150"/>
|
||
<el-table-column prop="password" label="密码" width="150">
|
||
<template #default="scope">
|
||
<div @mouseover="showPassword($event,scope.row.password)" @mouseleave="hidePassword($event)">••••••••</div>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column prop="auth" label="身份" width="700">
|
||
<template #default="scope">
|
||
<span v-if="scope.row.auth===0" style="color: #409EFF">管理员</span>
|
||
<span v-else-if="scope.row.auth===1" style="color: #67C23A">饲养员</span>
|
||
<span v-else-if="scope.row.auth===2" style="color: #E6A23C">兽医</span>
|
||
<span v-else>未知</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column fixed="right" label="操作" width="190">
|
||
<template #default="scope">
|
||
<el-button link type="primary" size="small" :icon="Document" @click="showDetail(scope.row)">
|
||
详情
|
||
</el-button>
|
||
<el-button link type="primary" size="small" :icon="Edit" @click="showEdit(scope.row)">
|
||
编辑
|
||
</el-button>
|
||
<el-button link type="danger" size="small" :icon="Delete" @click="delete_(scope.row)">
|
||
删除
|
||
</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
</div>
|
||
|
||
<!-- 分页-->
|
||
<div class="pager relative">
|
||
<div class="center">
|
||
<el-pagination background layout="total, prev, pager, next, jumper" :total="usersTableData.length"
|
||
:page-size="pageSize" small @current-change="handleCurrentChange"/>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<!-- 添加用户对话框-->
|
||
<el-dialog v-model="dialog.addDialogVisible" title="添加用户" width="400" align-center draggable overflow
|
||
destroy-on-close>
|
||
<ZooUserFormDialog>
|
||
<template #default="scope">
|
||
<el-button @click="dialog.addDialogVisible = false">
|
||
取消
|
||
</el-button>
|
||
<el-button type="primary" @click="add(scope.form)">
|
||
添加
|
||
</el-button>
|
||
</template>
|
||
</ZooUserFormDialog>
|
||
</el-dialog>
|
||
|
||
<!-- 编辑用户对话框-->
|
||
<el-dialog v-model="dialog.editDialogVisible" title="编辑用户" width="400" align-center draggable overflow
|
||
destroy-on-close>
|
||
<ZooUserFormDialog :data="dialog.dialogData" edit>
|
||
<template #default="scope">
|
||
<el-button @click="dialog.editDialogVisible = false">
|
||
取消
|
||
</el-button>
|
||
<el-button type="primary" @click="edit(scope.form)">
|
||
修改
|
||
</el-button>
|
||
</template>
|
||
</ZooUserFormDialog>
|
||
</el-dialog>
|
||
|
||
<!-- 用户详情对话框-->
|
||
<el-dialog v-model="dialog.detailDialogVisible" title="查询用户" width="400" align-center draggable overflow
|
||
destroy-on-close>
|
||
<ZooUserFormDialog :data="dialog.dialogData" detail>
|
||
<template #default="scope">
|
||
<el-button @click="dialog.detailDialogVisible = false">
|
||
关闭
|
||
</el-button>
|
||
</template>
|
||
</ZooUserFormDialog>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<style scoped>
|
||
/*.table {*/
|
||
/* !*width: 1300px;*!*/
|
||
/* height: 546px;*/
|
||
/* margin: 0 30px;*/
|
||
/*}*/
|
||
|
||
/*.left {*/
|
||
/* margin-left: 30px;*/
|
||
/*}*/
|
||
|
||
/*.right {*/
|
||
/* margin-right: 30px;*/
|
||
/*}*/
|
||
|
||
/*.select {*/
|
||
/* width: 100%;*/
|
||
/* height: 60px;*/
|
||
/* line-height: 60px;*/
|
||
/*}*/
|
||
</style> |