2024-11-12 15:44:05 +08:00
|
|
|
<template>
|
2025-02-17 22:42:51 +08:00
|
|
|
<a-button
|
|
|
|
v-permission="['system:user:create']"
|
|
|
|
v-if="props.isCreate"
|
|
|
|
type="primary"
|
|
|
|
@click="handleClick"
|
|
|
|
>
|
2024-11-12 15:44:05 +08:00
|
|
|
<template #icon><icon-plus /></template>
|
|
|
|
新建
|
|
|
|
</a-button>
|
|
|
|
<a-button
|
2025-01-10 10:40:37 +08:00
|
|
|
v-permission="['system:user:update']"
|
2024-11-12 15:44:05 +08:00
|
|
|
v-if="!props.isCreate"
|
|
|
|
type="outline"
|
|
|
|
size="small"
|
|
|
|
:style="{ marginRight: '10px', padding: '7px' }"
|
|
|
|
@click="handleClick"
|
|
|
|
>
|
|
|
|
<template #icon><icon-edit /></template>
|
|
|
|
修改
|
|
|
|
</a-button>
|
|
|
|
|
|
|
|
<a-modal
|
|
|
|
width="700px"
|
|
|
|
:visible="visible"
|
|
|
|
@ok="handleSubmit"
|
|
|
|
@cancel="handleCancel"
|
|
|
|
>
|
|
|
|
<template #title>{{ modalTitle }}</template>
|
|
|
|
<a-form ref="CreateRef" :model="formData" :style="{ width: '650px' }">
|
|
|
|
<a-form-item
|
|
|
|
field="username"
|
2025-01-15 20:54:47 +08:00
|
|
|
label="用户名"
|
2024-11-12 15:44:05 +08:00
|
|
|
:validate-trigger="['change', 'input']"
|
|
|
|
:rules="[
|
2025-01-15 20:54:47 +08:00
|
|
|
{ required: true, message: '用户名不能为空' },
|
2024-11-12 15:44:05 +08:00
|
|
|
{
|
|
|
|
match: /^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$/,
|
2025-01-15 20:54:47 +08:00
|
|
|
message: '请输入正确格式的用户名',
|
2024-11-12 15:44:05 +08:00
|
|
|
},
|
|
|
|
]"
|
|
|
|
>
|
|
|
|
<a-input
|
|
|
|
v-if="props.isCreate"
|
|
|
|
v-model="formData.username"
|
2025-01-15 20:54:47 +08:00
|
|
|
placeholder="请输入用户名"
|
2024-11-12 15:44:05 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div v-else>{{ formData.username }}</div>
|
|
|
|
</a-form-item>
|
|
|
|
<a-form-item
|
|
|
|
field="email"
|
2025-01-15 20:54:47 +08:00
|
|
|
label="Email"
|
2024-11-12 15:44:05 +08:00
|
|
|
:rules="[
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
type: 'email',
|
2025-01-15 20:54:47 +08:00
|
|
|
message: 'Email不能为空',
|
2024-11-12 15:44:05 +08:00
|
|
|
},
|
|
|
|
]"
|
|
|
|
:validate-trigger="['change', 'input']"
|
|
|
|
>
|
2025-02-17 22:42:51 +08:00
|
|
|
<a-input v-model="formData.email" placeholder="请输入Email" />
|
2024-11-12 15:44:05 +08:00
|
|
|
</a-form-item>
|
|
|
|
<a-form-item
|
|
|
|
field="phone"
|
2025-01-15 20:54:47 +08:00
|
|
|
label="电话"
|
2024-11-12 15:44:05 +08:00
|
|
|
:rules="[
|
2025-01-15 20:54:47 +08:00
|
|
|
{ required: true, message: '电话不能为空' },
|
|
|
|
{ match: /^1[3-9]\d{9}$/, message: '请输入正确格式的电话' },
|
2024-11-12 15:44:05 +08:00
|
|
|
]"
|
|
|
|
:validate-trigger="['change', 'input']"
|
|
|
|
>
|
2025-02-17 22:42:51 +08:00
|
|
|
<a-input v-model="formData.phone" placeholder="请输入电话" />
|
2024-11-12 15:44:05 +08:00
|
|
|
</a-form-item>
|
|
|
|
<a-form-item
|
|
|
|
field="password"
|
2025-01-15 20:54:47 +08:00
|
|
|
label="密码"
|
2024-11-12 15:44:05 +08:00
|
|
|
v-if="isCreate"
|
|
|
|
:validate-trigger="['change', 'input']"
|
2025-01-15 20:54:47 +08:00
|
|
|
:rules="[{ required: true, message: '密码不能为空' }]"
|
2024-11-12 15:44:05 +08:00
|
|
|
>
|
2025-02-17 22:42:51 +08:00
|
|
|
<a-input v-model="formData.password" placeholder="请输入密码" />
|
2024-11-12 15:44:05 +08:00
|
|
|
</a-form-item>
|
2025-01-15 20:54:47 +08:00
|
|
|
<a-form-item field="nickName" label="昵称">
|
2025-02-17 22:42:51 +08:00
|
|
|
<a-input v-model="formData.nickName" placeholder="请输入昵称" />
|
2024-11-12 15:44:05 +08:00
|
|
|
</a-form-item>
|
|
|
|
|
2025-01-15 20:54:47 +08:00
|
|
|
<a-form-item field="address" label="地址">
|
2025-02-17 22:42:51 +08:00
|
|
|
<a-input v-model="formData.address" placeholder="请输入地址" />
|
2024-11-12 15:44:05 +08:00
|
|
|
</a-form-item>
|
|
|
|
<a-form-item
|
|
|
|
field="deptId"
|
2025-01-15 20:54:47 +08:00
|
|
|
label="部门"
|
|
|
|
:rules="[{ required: true, message: '部门不能为空' }]"
|
2024-11-12 15:44:05 +08:00
|
|
|
:validate-trigger="['change']"
|
|
|
|
>
|
|
|
|
<a-tree-select
|
|
|
|
v-model="formData.deptId"
|
|
|
|
:field-names="{
|
|
|
|
key: 'id',
|
|
|
|
title: 'name',
|
|
|
|
children: 'children',
|
|
|
|
}"
|
|
|
|
:data="deptOptions"
|
|
|
|
allow-clear
|
2025-01-15 20:54:47 +08:00
|
|
|
placeholder="请选择所属部门"
|
2024-11-12 15:44:05 +08:00
|
|
|
/>
|
|
|
|
</a-form-item>
|
|
|
|
|
|
|
|
<a-form-item
|
2025-02-17 23:26:35 +08:00
|
|
|
field="roleIds"
|
2025-01-15 20:54:47 +08:00
|
|
|
label="角色"
|
|
|
|
:rules="[{ required: true, message: '角色不能为空' }]"
|
2024-11-12 15:44:05 +08:00
|
|
|
:validate-trigger="['change']"
|
|
|
|
>
|
|
|
|
<a-select
|
2025-02-17 23:26:35 +08:00
|
|
|
v-model="formData.roleIds"
|
2024-11-12 15:44:05 +08:00
|
|
|
:options="roleOptions"
|
|
|
|
:field-names="{
|
|
|
|
value: 'id',
|
|
|
|
label: 'name',
|
|
|
|
}"
|
2025-01-15 20:54:47 +08:00
|
|
|
placeholder="请选择角色"
|
2025-02-17 23:26:35 +08:00
|
|
|
multiple
|
2024-11-12 15:44:05 +08:00
|
|
|
/>
|
|
|
|
</a-form-item>
|
|
|
|
</a-form>
|
|
|
|
</a-modal>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2025-02-17 22:42:51 +08:00
|
|
|
import useVisible from '@/hooks/visible';
|
|
|
|
import { computed, PropType, ref } from 'vue';
|
|
|
|
import { CreateRecord } from '@/api/user';
|
|
|
|
import { FormInstance } from '@arco-design/web-vue/es/form';
|
|
|
|
import { queryRoleList } from '@/api/role';
|
|
|
|
import { getAllDeptTree } from '@/api/dept';
|
|
|
|
import { Message } from '@arco-design/web-vue';
|
|
|
|
import { useUserStore } from '@/store';
|
2024-11-12 15:44:05 +08:00
|
|
|
|
2025-02-17 22:42:51 +08:00
|
|
|
const props = defineProps({
|
|
|
|
prem: {
|
|
|
|
type: Object as PropType<CreateRecord>,
|
|
|
|
},
|
|
|
|
isCreate: Boolean,
|
|
|
|
});
|
|
|
|
const emit = defineEmits(['refresh']);
|
|
|
|
const modalTitle = computed(() => {
|
|
|
|
return props.isCreate ? '新建用户' : '编辑用户';
|
|
|
|
});
|
|
|
|
const { visible, setVisible } = useVisible(false);
|
|
|
|
const checkKeys = ref<number[]>([]);
|
2024-11-12 15:44:05 +08:00
|
|
|
|
2025-02-17 22:42:51 +08:00
|
|
|
const CreateRef = ref<FormInstance>();
|
2024-11-12 15:44:05 +08:00
|
|
|
|
2025-02-17 22:42:51 +08:00
|
|
|
const formData = ref<CreateRecord>({
|
|
|
|
value: undefined,
|
|
|
|
code: undefined,
|
|
|
|
username: '',
|
|
|
|
nickName: '',
|
|
|
|
password: '',
|
|
|
|
phone: '',
|
|
|
|
email: '',
|
|
|
|
enabled: '',
|
|
|
|
address: '',
|
|
|
|
deptId: undefined,
|
2025-02-17 23:26:35 +08:00
|
|
|
roleIds: [],
|
2025-02-17 22:42:51 +08:00
|
|
|
permissionIds: [],
|
|
|
|
authorities: [],
|
2024-11-12 15:44:05 +08:00
|
|
|
});
|
|
|
|
|
2025-02-17 22:42:51 +08:00
|
|
|
let formDifer = {};
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
|
|
|
// 部门数据
|
|
|
|
const deptOptions = ref();
|
|
|
|
const getDeptData = async () => {
|
|
|
|
const res = await getAllDeptTree(0);
|
|
|
|
deptOptions.value = res.data;
|
|
|
|
};
|
|
|
|
// 角色数据
|
|
|
|
const roleOptions = ref();
|
|
|
|
const getRoleData = async () => {
|
|
|
|
const res = await queryRoleList('');
|
|
|
|
roleOptions.value = res.data.records.filter((item: any) => {
|
|
|
|
return item.enabled !== false;
|
|
|
|
});
|
|
|
|
};
|
2024-11-12 15:44:05 +08:00
|
|
|
|
2025-02-17 22:42:51 +08:00
|
|
|
// 组件被点击
|
|
|
|
const handleClick = () => {
|
|
|
|
getDeptData();
|
|
|
|
getRoleData();
|
|
|
|
const userId = props.prem?.id;
|
|
|
|
// 编辑
|
|
|
|
if (!props.isCreate && userId) {
|
|
|
|
formData.value = props.prem;
|
|
|
|
formDifer = { ...props.prem };
|
2024-11-12 15:44:05 +08:00
|
|
|
}
|
2025-02-17 22:42:51 +08:00
|
|
|
setVisible(true);
|
|
|
|
};
|
2024-11-12 15:44:05 +08:00
|
|
|
|
2025-02-17 22:42:51 +08:00
|
|
|
// 做出只修改的部分
|
|
|
|
const diffDataForm = (newData: any, oldData: any) => {
|
|
|
|
const result = {}; // 报错修改的字段内容
|
|
|
|
Object.keys(oldData).forEach((key) => {
|
|
|
|
if (oldData[key] !== newData[key]) {
|
|
|
|
result[key] = newData[key];
|
2024-11-12 15:44:05 +08:00
|
|
|
}
|
2025-02-17 22:42:51 +08:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 提交
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
const valid = await CreateRef.value?.validate();
|
|
|
|
if (!valid) {
|
|
|
|
formData.value.permissionIds = checkKeys.value;
|
|
|
|
// 新增
|
|
|
|
if (props.isCreate) {
|
|
|
|
// formData.value.username = formData.value.email;
|
|
|
|
const res = await userStore.createUser(formData.value);
|
2024-11-12 15:44:05 +08:00
|
|
|
if (res.status === 200) {
|
|
|
|
Message.success({
|
2025-02-17 22:42:51 +08:00
|
|
|
content: '新建成功',
|
2024-11-12 15:44:05 +08:00
|
|
|
duration: 5 * 1000,
|
|
|
|
});
|
|
|
|
}
|
2025-02-17 22:42:51 +08:00
|
|
|
CreateRef.value?.resetFields();
|
|
|
|
} else {
|
|
|
|
// 编辑
|
2025-02-17 23:26:35 +08:00
|
|
|
// formDifer = diffDataForm(formData.value, formDifer);
|
|
|
|
// if (Object.keys(formDifer).length === 0) {
|
|
|
|
// Message.success({
|
|
|
|
// content: '未编辑',
|
|
|
|
// duration: 3 * 1000,
|
|
|
|
// });
|
|
|
|
// } else {
|
|
|
|
// formDifer.id = formData.value.id;
|
|
|
|
// const res = await userStore.updateUser(formData.value);
|
|
|
|
// if (res.status === 200) {
|
|
|
|
// Message.success({
|
|
|
|
// content: '修改成功',
|
|
|
|
// duration: 5 * 1000,
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
const res = await userStore.updateUser(formData.value);
|
|
|
|
if (res.status === 200) {
|
2025-02-17 22:42:51 +08:00
|
|
|
Message.success({
|
2025-02-17 23:26:35 +08:00
|
|
|
content: '修改成功',
|
|
|
|
duration: 5 * 1000,
|
2025-02-17 22:42:51 +08:00
|
|
|
});
|
|
|
|
}
|
2024-11-12 15:44:05 +08:00
|
|
|
}
|
2025-02-17 22:42:51 +08:00
|
|
|
checkKeys.value = [];
|
|
|
|
emit('refresh');
|
|
|
|
setVisible(false);
|
2024-11-12 15:44:05 +08:00
|
|
|
}
|
2025-02-17 22:42:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 关闭
|
|
|
|
const handleCancel = async () => {
|
2024-11-12 15:44:05 +08:00
|
|
|
checkKeys.value = [];
|
|
|
|
setVisible(false);
|
2025-02-17 22:42:51 +08:00
|
|
|
};
|
2024-11-12 15:44:05 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped></style>
|