wujingjing
2024-11-18 7e22b0940b987546f1db06ae8ff12ef0657f7b09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<template>
    <ywDialog
        v-model="dialogIsShow"
        :headerIcon="dialogHeaderIcon"
        :title="dialogTitle"
        width="680"
        @dlgClosed="closeDialog"
        @submit="submitFormValue"
    >
        <el-form :model="dialogFormValue" ref="dialogFormRef" :rules="dialogFormRules" :label-width="isEditDialog ? 54 : 78">
            <el-row :gutter="35">
                <template v-if="!isEditDialog">
                    <el-col :span="12" class="mb20">
                        <el-form-item label="用户ID" prop="user_name">
                            <el-input v-model="dialogFormValue.user_name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="mb20"
                        ><el-form-item label="密码" prop="password" auto-complete="new-password" label-width="54">
                            <el-input type="password" v-model="dialogFormValue.password"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" class="mb20"
                        ><el-form-item label="确认密码" prop="confirmPassword" auto-complete="new-password">
                            <el-input type="password" v-model="dialogFormValue.confirmPassword"></el-input>
                        </el-form-item>
                    </el-col>
                </template>
                <el-col :span="12" class="mb20"
                    ><el-form-item label="电话" prop="phone" label-width="54">
                        <el-input v-model="dialogFormValue.phone"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb20">
                    <el-form-item label="中文名" prop="real_name">
                        <el-input v-model="dialogFormValue.real_name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb20">
                    <el-form-item label="部门" prop="part_id" label-width="54">
                        <el-select v-model="dialogFormValue.part_id">
                            <el-option v-for="item in departmentList" :key="item.id" :value="item.id" :label="item?.name"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb20"
                    ><el-form-item label="性别" prop="sex">
                        <el-select v-model="dialogFormValue.sex">
                            <el-option v-for="item in Object.keys(userSexMap)" :key="item" :value="item" :label="userSexMap[item]"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb20">
                    <el-form-item label="邮箱" prop="email" label-width="54">
                        <el-input v-model="dialogFormValue.email"></el-input>
                    </el-form-item>
                </el-col>
                <template v-if="!isEditDialog"
                    ><el-col :span="12" class="mb20"
                        ><el-form-item label="角色" prop="user_roles">
                            <el-select class="w100" placeholder="请选择角色" filterable multiple v-model="dialogFormValue.user_roles">
                                <el-option v-for="(item, index) in roleList" :key="index + ''" :value="item.id" :label="item?.title"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </template>
 
                <el-col :span="24" class="mb20">
                    <el-form-item label="说明" prop="note">
                        <el-input placeholder="请输入说明" v-model="dialogFormValue.note" type="textarea" :rows="3" /> </el-form-item
                ></el-col>
            </el-row>
        </el-form>
    </ywDialog>
</template>
 
<script setup lang="ts">
import ywDialog from '/@/components/dialog/yw-dialog.vue';
 
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
 
import { computed, ref, watch } from 'vue';
// import { useTableSort } from '/@/hooks/useTableSort';
// import { useValidateUniqueness } from '/@/hooks/useValidateUniqueness';
import { deepClone } from '/@/utils/other';
 
import { userSexMap } from '../types';
import * as userApi from '/@/api/auth/user';
import { formatDate } from '/@/utils/formatTime';
 
const props = defineProps(['item', 'roleList', 'departmentList']);
const emit = defineEmits(['update', 'insert']);
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditDialog = ref(false);
const dialogTitle = computed(() => {
    return isEditDialog.value ? `修改用户【${props.item?.user_name}】` : `添加用户`;
});
const dialogHeaderIcon = computed(() => {
    return isEditDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const dialogFormValue = ref(null);
const dialogIsShow = defineModel({
    type: Boolean,
});
const dialogFormRef = ref<FormInstance>(null);
const validatePassword = (_rule: any, value: any, callback: any) => {
    if (!dialogFormValue.value.password) {
        callback('请先输入新密码!');
    } else if (dialogFormValue.value.password !== dialogFormValue.value.confirmPassword) {
        callback(new Error('两次密码不一致!'));
    } else {
        callback();
    }
};
const dialogFormRules = ref<FormRules>({
    user_name: [{ required: true, message: '请输入用户ID', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    confirmPassword: [{ required: true, validator: validatePassword, trigger: 'blur' }],
});
const openOperateDialog = (row?) => {
    if (row) {
        isEditDialog.value = true;
        const { user_id, phone, real_name, part_id, sex, email, note } = row;
        dialogFormValue.value = deepClone({ user_id, phone, real_name, part_id, sex, email, note });
    } else {
        isEditDialog.value = false;
 
        dialogFormValue.value = {
            user_name: '',
            password: '',
            confirmPassword: '',
            phone: null,
            real_name: null,
            part_id: null,
            sex: null,
            email: null,
            note: null,
            user_roles: [],
        };
    }
};
const closeDialog = () => {
    dialogIsShow.value = false;
    dialogFormRef.value.clearValidate();
};
 
const submitFormValue = async () => {
    const valid = await dialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
    const updateTime = formatDate(new Date());
 
    if (isEditDialog.value) {
        const res = await userApi.updateUserInfoByPost(dialogFormValue.value);
        emit('update', { ...dialogFormValue.value });
 
        closeDialog();
        ElMessage.success('修改用户成功');
    } else {
        const sendForm = { ...dialogFormValue.value, user_roles: dialogFormValue.value.user_roles?.join(',') };
 
        const res = await userApi.addUserInfoByPost(sendForm);
        const newData = {
            user_id: res.user_id,
            ...dialogFormValue.value,
            create_time: updateTime.slice(0, 10),
        };
        emit('insert', newData);
        // tableData.value.push(newData);
        closeDialog();
        ElMessage.success('添加用户成功');
    }
};
 
//#endregion
 
watch(
    () => dialogIsShow.value,
    (val) => {
        if (!val) return;
        openOperateDialog(props.item);
    }
);
</script>
<style scoped lang="scss"></style>