马宇豪
2024-01-26 c694cffc8541d921e5256d33e14e3237454de950
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
<template>
    <div class="system-add-role-container">
        <el-dialog :title="title" v-model="isShowDialog" width="769px">
            <el-form :model="roleForm" size="default" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="证书名称">
                            <el-input v-model="roleForm.name" placeholder="请输入证书名称" clearable style="width: 100%"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="isShowDialog = !isShowDialog" size="default">取 消</el-button>
                    <el-button type="primary" @click="onSubmit" v-throttle size="default">{{ buttonName }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
 
<script lang="ts">
import { ElMessage } from 'element-plus';
import { reactive, toRefs, defineComponent } from 'vue';
import {certApi} from "/@/api/systemManage/certificate";
 
// 定义接口来定义对象的类型
interface MenuDataTree {
    id: number;
    label: string;
    children?: MenuDataTree[];
}
interface RoleState {
    title: string;
    buttonName: string;
    isShowDialog: boolean;
    roleForm: {
      id: null | number
      name: string;
    };
    menuData: Array<MenuDataTree>;
    menuProps: {
        children: string;
        label: string;
    };
}
 
export default defineComponent({
    name: 'certificateDialog',
    setup(prop, context) {
        const state = reactive<RoleState>({
            isShowDialog: false,
            title: '',
            buttonName: '',
            roleForm: {
                id: null,
                name: '', // 证书名称
            },
            menuData: [],
            menuProps: {
                children: 'children',
                label: 'label'
            }
        });
        // 打开弹窗
        const openDialog = (type: string, value: any) => {
            state.isShowDialog = true;
            if (type === '新增') {
                state.title = '新增证书';
                state.buttonName = '新增';
                state.roleForm = {
                  id: null,
                  name: ''
                };
            } else {
                state.title = '修改证书';
                state.buttonName = '修改';
                state.roleForm.id = JSON.parse(JSON.stringify(value)).id
                state.roleForm.name = JSON.parse(JSON.stringify(value)).name
            }
        };
        // 新增
        const onSubmit = async () => {
            if (state.title === '新增证书') {
                let res = await certApi().addCerttype({name: state.roleForm.name});
                if (res.data.code === '200') {
                    ElMessage({
                        type: 'success',
                        message: '证书新增成功',
                        duration: 2000
                    });
                    state.isShowDialog = false;
                    context.emit('refreshCertList');
                } else {
                    ElMessage({
                        type: 'warning',
                        message: res.data.msg
                    });
                }
            } else {
                let res = await certApi().modCerttype(state.roleForm);
                if (res.data.code === '200') {
                    ElMessage({
                        type: 'success',
                        message: '证书修改成功',
                        duration: 2000
                    });
                    state.isShowDialog = false;
                    context.emit('refreshCertList');
                } else {
                    ElMessage({
                        type: 'warning',
                        message: res.data.msg
                    });
                }
            }
        };
        // 获取菜单结构数据
        return {
            onSubmit,
            openDialog,
            ...toRefs(state)
        };
    }
});
</script>
 
<style scoped lang="scss">
.system-add-role-container {
    .menu-data-tree {
        width: 100%;
        border: 1px solid var(--el-border-color);
        border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
        padding: 5px;
    }
}
</style>