wujingjing
2024-10-25 a34acfc9181792e2c4a6fa113bd9e21780c5224d
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
<template>
    <ywDialog
        v-model="dialogIsShow"
        :headerIcon="dialogHeaderIcon"
        :title="dialogTitle"
        width="470"
        @dlgClosed="closeDialog"
        @submit="submitFormValue"
    >
        <el-form :model="dialogFormValue" ref="dialogFormRef" :rules="dialogFormRules" label-width="76">
            <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-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';
 
const props = defineProps(['item', 'roleList']);
const emit = defineEmits(['update', 'insert']);
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditDialog = ref(false);
const dialogTitle = computed(() => {
    return `修改用户【${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 dialogFormRules = ref<FormRules>({});
const openOperateDialog = (row) => {
    dialogFormValue.value = {
        user_roles: row.user_roles ?? [],
    };
};
const closeDialog = () => {
    dialogIsShow.value = false;
    dialogFormRef.value.clearValidate();
};
 
const submitFormValue = async () => {
    const valid = await dialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
 
    const sendForm = { user_id: props.item.user_id, user_roles: dialogFormValue.value.user_roles.join(',') };
 
    const res = await userApi.updateUserRolesByPost(sendForm);
    emit('update', { user_id: props.item.user_id, user_roles: dialogFormValue.value.user_roles });
 
    closeDialog();
    ElMessage.success('修改角色成功');
};
 
//#endregion
 
watch(
    () => dialogIsShow.value,
    (val) => {
        if (!val) return;
        openOperateDialog(props.item);
    }
);
</script>
<style scoped lang="scss"></style>