2023-12-18 14:29:28 +08:00
|
|
|
<template>
|
2024-04-10 19:07:22 +08:00
|
|
|
<el-form ref="formRef" :model="formData" :rules="rules" label-width="80">
|
|
|
|
<el-row>
|
|
|
|
<el-col :span="12">
|
|
|
|
<el-form-item label="用户名" prop="username">
|
|
|
|
<el-input v-model="formData.username" placeholder="请输入用户名"
|
|
|
|
:disabled="type === 'edit'"></el-input>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
|
|
|
<el-col :span="12">
|
|
|
|
<el-form-item label="姓名" prop="name">
|
|
|
|
<el-input v-model="formData.name" placeholder="请输入姓名"></el-input>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
|
|
|
</el-row>
|
|
|
|
<el-row>
|
|
|
|
<el-col :span="12">
|
|
|
|
<el-form-item label="角色" prop="role">
|
|
|
|
<el-select v-model="formData.role" placeholder="请选择角色" multiple>
|
|
|
|
<el-option v-for="item in roleOption" :key="item.roleId" :label="item.roleName"
|
|
|
|
:value="item.roleId"/>
|
|
|
|
</el-select>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
|
|
|
<el-col :span="12">
|
|
|
|
<el-form-item label="邮箱" prop="email">
|
|
|
|
<el-input v-model="formData.email" placeholder="请输入邮箱"></el-input>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
|
|
|
</el-row>
|
|
|
|
<el-row>
|
|
|
|
<el-col :span="12">
|
|
|
|
<el-form-item label="电话" prop="phone">
|
|
|
|
<el-input v-model="formData.phone" placeholder="请输入电话"></el-input>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
2024-04-11 17:55:47 +08:00
|
|
|
<el-col :span="12" v-if="type != 'edit'">
|
|
|
|
<el-form-item label="锁定" prop="lockFlag">
|
2024-04-10 19:07:22 +08:00
|
|
|
<el-radio-group v-model="formData.lockFlag">
|
2024-04-11 17:55:47 +08:00
|
|
|
<el-radio :label="'0'" border>解锁</el-radio>
|
|
|
|
<el-radio :label="'1'" border>锁定</el-radio>
|
2024-04-10 19:07:22 +08:00
|
|
|
</el-radio-group>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
|
|
|
</el-row>
|
2024-04-11 17:55:47 +08:00
|
|
|
<el-row v-if="type != 'edit'">
|
2024-04-10 19:07:22 +08:00
|
|
|
<el-col :span="12">
|
|
|
|
<el-form-item label="密码" prop="password">
|
|
|
|
<el-input v-model="formData.password" placeholder="请输入密码"
|
|
|
|
:disabled="type === 'edit'"></el-input>
|
|
|
|
</el-form-item>
|
|
|
|
</el-col>
|
|
|
|
</el-row>
|
|
|
|
|
|
|
|
<div style="text-align: right;padding-top: 80px;">
|
|
|
|
<el-button class="f18" @click="close">取消</el-button>
|
|
|
|
<el-button class="f18" type="primary" @click="saveData">确认</el-button>
|
|
|
|
</div>
|
|
|
|
</el-form>
|
2023-12-18 14:29:28 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang='ts' setup>
|
2024-04-10 19:07:22 +08:00
|
|
|
import {onMounted, reactive, ref, toRefs, watch} from 'vue'
|
|
|
|
import {ElMessage, ElMessageBox} from 'element-plus'
|
|
|
|
import {getRoleList} from "@/api/role";
|
2024-04-11 17:55:47 +08:00
|
|
|
import {addUser, updateUI} from "@/api/user";
|
2023-12-18 14:29:28 +08:00
|
|
|
|
2023-12-28 14:27:47 +08:00
|
|
|
const emit = defineEmits(['close', 'save'])
|
2023-12-18 14:29:28 +08:00
|
|
|
|
|
|
|
const props = defineProps({
|
2024-04-10 19:07:22 +08:00
|
|
|
type: String
|
2023-12-18 14:29:28 +08:00
|
|
|
})
|
|
|
|
|
2024-04-10 19:07:22 +08:00
|
|
|
const roleOption = ref<any>([])
|
2024-02-22 20:48:08 +08:00
|
|
|
|
|
|
|
const validatorPhone = (rule: any, value: any, callback: any) => {
|
2024-04-10 19:07:22 +08:00
|
|
|
var isPhone = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
|
2024-02-22 20:48:08 +08:00
|
|
|
|
|
|
|
|
2024-04-10 19:07:22 +08:00
|
|
|
if (value.indexOf('****') >= 0) {
|
|
|
|
return callback().trim();
|
|
|
|
}
|
|
|
|
if (!isPhone.test(value)) {
|
|
|
|
callback(new Error('请输入合法手机号'));
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
2024-02-22 20:48:08 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-18 14:29:28 +08:00
|
|
|
const rules = reactive({
|
2024-04-10 19:07:22 +08:00
|
|
|
username: [
|
|
|
|
{required: true, message: '请输入用户名', trigger: ['blur', 'change']},
|
|
|
|
],
|
|
|
|
name: [
|
|
|
|
{required: true, message: '请输入姓名', trigger: ['blur', 'change']},
|
|
|
|
],
|
|
|
|
role: [
|
|
|
|
{required: true, message: '请选择角色', trigger: ['blur', 'change']},
|
|
|
|
],
|
|
|
|
phone: [
|
|
|
|
{required: true, validator: validatorPhone, trigger: ['blur', 'change']},
|
|
|
|
],
|
|
|
|
password: [
|
|
|
|
{required: true, message: '请输入密码', trigger: ['blur', 'change']},
|
|
|
|
]
|
2023-12-18 14:29:28 +08:00
|
|
|
})
|
|
|
|
|
2023-12-18 17:16:47 +08:00
|
|
|
const formRef = ref()
|
2023-12-18 14:29:28 +08:00
|
|
|
const formData = ref({
|
2024-04-10 19:07:22 +08:00
|
|
|
id: '',
|
|
|
|
username: '',
|
|
|
|
name: '',
|
2024-04-11 17:55:47 +08:00
|
|
|
role: [],
|
2024-04-10 19:07:22 +08:00
|
|
|
email: '',
|
|
|
|
phone: '',
|
2024-04-11 17:55:47 +08:00
|
|
|
password: '',
|
2024-04-10 19:07:22 +08:00
|
|
|
lockFlag: '0',
|
2023-12-18 14:29:28 +08:00
|
|
|
} as any)
|
|
|
|
|
|
|
|
onMounted(() => {
|
2024-04-10 19:07:22 +08:00
|
|
|
resetData()
|
|
|
|
})
|
|
|
|
|
|
|
|
getRoleList().then((res: any) => {
|
|
|
|
if (res && res.code == 0) {
|
|
|
|
roleOption.value = res.data
|
|
|
|
} else {
|
|
|
|
ElMessage.error('角色列表获取失败')
|
|
|
|
}
|
2023-12-18 14:29:28 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
defineExpose({
|
2024-04-10 19:07:22 +08:00
|
|
|
formData,
|
|
|
|
resetData,
|
2023-12-18 14:29:28 +08:00
|
|
|
})
|
2024-04-10 19:07:22 +08:00
|
|
|
|
2023-12-18 14:29:28 +08:00
|
|
|
function close() {
|
2024-04-10 19:07:22 +08:00
|
|
|
emit('close')
|
2023-12-18 14:29:28 +08:00
|
|
|
}
|
2024-04-10 19:07:22 +08:00
|
|
|
|
2023-12-18 14:29:28 +08:00
|
|
|
function resetData() {
|
2024-04-10 19:07:22 +08:00
|
|
|
getRoleList()
|
|
|
|
formRef.value.resetFields()
|
|
|
|
formData.value = {
|
|
|
|
id: '',
|
|
|
|
username: '',
|
|
|
|
name: '',
|
2024-04-11 17:55:47 +08:00
|
|
|
role: [],
|
2024-04-10 19:07:22 +08:00
|
|
|
email: '',
|
|
|
|
phone: '',
|
2024-04-11 17:55:47 +08:00
|
|
|
password: '',
|
2024-04-10 19:07:22 +08:00
|
|
|
lockFlag: '0',
|
|
|
|
}
|
2023-12-18 14:29:28 +08:00
|
|
|
}
|
2024-04-10 19:07:22 +08:00
|
|
|
|
2023-12-18 17:16:47 +08:00
|
|
|
const saveData = async () => {
|
2024-04-10 19:07:22 +08:00
|
|
|
await formRef.value.validate((valid: any, fields: any) => {
|
|
|
|
if (valid) {
|
2024-04-11 17:55:47 +08:00
|
|
|
if (props.type == "add") {
|
|
|
|
addUser(formData.value).then((data: any) => {
|
|
|
|
if (data.code == 0) {
|
|
|
|
ElMessage.success('保存成功!')
|
|
|
|
emit('save', formData.value, props.type)
|
|
|
|
close()
|
|
|
|
} else {
|
|
|
|
ElMessage.error("保存失败")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
updateUI({
|
|
|
|
userId: formData.value.userId,
|
|
|
|
username: formData.value.username,
|
|
|
|
name: formData.value.name,
|
|
|
|
role: formData.value.role,
|
|
|
|
email: formData.value.email,
|
|
|
|
phone: formData.value.phone
|
|
|
|
}).then((data: any) => {
|
|
|
|
if (data.code == 0) {
|
|
|
|
ElMessage.success('修改成功!')
|
|
|
|
emit('save', formData.value, props.type)
|
|
|
|
close()
|
|
|
|
} else {
|
|
|
|
ElMessage.error("修改失败")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-04-10 19:07:22 +08:00
|
|
|
}
|
|
|
|
})
|
2023-12-18 14:29:28 +08:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang='scss' scoped>
|
|
|
|
:deep(.el-form-item) {
|
2024-04-10 19:07:22 +08:00
|
|
|
.el-form-item__label {
|
|
|
|
display: block;
|
|
|
|
text-align: justify;
|
|
|
|
text-align-last: justify;
|
|
|
|
padding: 0 10px 0 20px;
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 14:29:28 +08:00
|
|
|
}
|
|
|
|
</style>
|