1.展示用户对话框并根据id填充用户信息
Dialog 对话框
// 展示编辑用户的对话框
async showEditDialog (id) {
// console.log(id)
const { data: res } = await this.$http.get('users/' + id)
if (res.meta.status !== 200) {
return this.message.error('查询用户信息失败')
}
this.editForm = res.data
this.editDialogVisible = true
}
2.渲染修改用户的表单数据
<!--修改用户的对话框-->
<!--修改用户的对话框-->
<el-dialog
title="修改用户"
:visible.sync="editDialogVisible"
width="50%"
>
<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
<el-form-item label="用户名">
<el-input disabled v-model="editForm.username"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="editForm.email"></el-input>
</el-form-item>
<el-form-item label="手机号" prop="mobile">
<el-input v-model="editForm.mobile"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="editDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
</span>
</el-dialog>
// 修改用户的规则
editFormRules: {
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ validator: checkEmail, trigger: 'blur' }
],
mobile: [
{ required: true, message: '请输入手机号', trigger: 'blur' },
{ validator: checkMobile, trigger: 'blur' }
]
}
3.提交表单完成数据修改
给确定按钮绑定事件
// 点击按钮,修改用户
editUser () {
this.KaTeX parse error: Expected '}', got 'EOF' at end of input: …} = await this.http.put(‘users/’ + this.editForm.id, { email: this.editForm.email, mobile: this.editForm.mobile })
if (res.meta.status !== 200) {
return this.KaTeX parse error: Expected 'EOF', got '}' at position 33: …用户失败') }̲ this.message.success(‘更新用户成功’)
// 隐藏更新用户的对话框
this.editDialogVisible = false
// 重新获取用户数据
this.getUserList()
})
}
删除用户操作
MessageBox 弹框----确认消息
MessageBox:需要全局挂载
Vue.prototype.$confirm = MessageBox.confirm
删除按钮绑定事件
// 根据id删除用户
async removeUserById (id) {
// 询问是否删除
const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).catch(err => {
return err
})
// 如果用户点击确定,则返回值为confirm
// console.log(confirmResult)
// 如果confirmResult != confirm
if (confirmResult !== 'confirm') {
return this.$message.info('已取消删除')
}
const { data: res } = await this.$http.delete('users/' + id)
if (res.meta.status !== 200) {
return this.$message.error('删除失败')
}
this.$message.success('删除用户成功')
this.getUserList()
}
分配角色按钮功能实现(User.vue)
Select 选择器
<!--分配角色-->
<el-tooltip :enterable="false" effect="dark" content="分配角色" placement="top">
<el-button @click="setRole(scope.row)" size="mini" type="warning" icon="el-icon-setting"></el-button>
</el-tooltip>
<!--分配角色的对话框-->
<el-dialog
title="分配角色"
:visible.sync="setRoleDialogVisible"
width="50%"
@close="setRoleDialogClosed"
>
<div>
<p>当前用户:{{userInfo.username}}</p>
<p>当前角色:{{userInfo.role_name}}</p>
<p>
分配新角色:
<el-select v-model="seletedRoleId" placeholder="请选择">
<el-option
v-for="item in rolesList"
:key="item.id"
:label="item.roleName"
:value="item.id">
</el-option>
</el-select>
</p>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="setRoleDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="saveRoleInfo">确 定</el-button>
</span>
</el-dialog>
// 展示分配角色的对话框
async setRole (userinfo) {
this.userInfo = userinfo
// 在展示对话框之前获取所有角色列表
const { data: res } = await this.$http.get('roles')
if (res.meta.status !== 200) {
return this.$message.error('获取角色列表失败')
}
this.rolesList = res.data
this.setRoleDialogVisible = true
},
// 点击按钮,分配角色
async saveRoleInfo () {
if (!this.seletedRoleId) {
return this.$message.error('请选择要分配的角色')
}
const { data: res } = await this.$http.put(`users/${this.userInfo.id}/role`, {
rid: this.seletedRoleId
})
if (res.meta.status !== 200) {
return this.$message.error('更新用户角色失败')
}
this.$message.success('更新角色成功')
this.getUserList()
this.setRoleDialogVisible = false
},
// 监听分配角色对话框的关闭
setRoleDialogClosed () {
this.seletedRoleId = ''
this.userInfo = {}
}
注:admin权限是不能被修改的
提交代码到码云即可