lzhe
2024-06-14 963a2313f4f8959715293d38f69894078150d508
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
        <div class="modelTitle">
            <span>通知类型 {{addBusinessForm.name}}</span>
            <span>
                服务商
                <el-select style="width: 80%"  v-model="serviceproviders" :disabled="mode != 'add'">
                    <el-option v-for="item in addBusinessForm.providerInfos" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
            </span>
        </div>
        <el-form :model="addBusinessForm" :rules="addBusinessRules" :disabled="mode=='show'" ref="dialogForm" label-width="140px" label-position="center">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="请输入配置名称" prop="pname">
                        <el-input v-model="addBusinessForm.pname" placeholder="模版名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24" v-for="(item,index) in addBusinessForm.configuration">
                    <el-form-item :label="item.pname" v-if="item.name != 'properties'">
                        <el-input v-model="item.value" :placeholder="item.name" clearable></el-input>
                    </el-form-item>
                    <el-form-item :label="item.pname" v-if="item.name == 'properties'">
                        <div class="array-content">
                            <el-row v-for="(item1,index1) in item.value">
                                <el-col v-for="(item2,index2) in item1" :span="7">
                                    <el-input v-model="item2.value" placeholder ="请输入" style="width: 76%;margin-right: 10px;"></el-input>
                                    <el-icon style="vertical-align: -3px;"><ArrowRightBold /></el-icon>
                                </el-col>
                                <el-col :span="3" class="otherBtn">
                                    <el-button type="primary" size="small" plain @click="addpro(index,index1)" v-show="index1 == (item.value.length-1)">添加</el-button>
                                    <el-button type="danger" size="small" style="margin-left:0;" plain @click="delData(index,index1)" v-show="index1 != (item.value.length-1)">删除</el-button>
                                </el-col>
                            </el-row>
                        </div>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="businessSubmit">确定</el-button>
        </template>
    </el-dialog>
</template>
 
<script>
    import * as ElementPlusIconsVue from '@element-plus/icons-vue'
    let icons = []
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        icons.push(key)
    }
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                serviceproviders: "",
                mode: "add",
                titleMap: {
                    add: '新增配置',
                    edit: '编辑配置',
                    show: '查看'
                },
                visible: false,
                isSaveing: false,
                //表单数据
                addBusinessForm: {
                    providerInfos: [],
                    notifyTemplateName: "",
                    agentId: "",
                    title: "",
                    message: ""
                },
                //验证规则
                addBusinessRules: {
                    pname:[{required: true, message: '请输入模版名称'}]
                },
            }
        },
        mounted() {
        },
        methods: {
            addpro(index,index1) {
                this.addBusinessForm.configuration[index].value.push([
                    {name: 'name', value: ''},
                    {name: 'value', value: ''},
                    {name: 'description', value: ''},
                ])
            },
            delData(index,index1) {
                this.addBusinessForm.configuration[index].value.splice(index1,1);
            },
            //显示
            open(mode='add',obj){
                if(mode == 'add') {
                    this.addBusinessForm = obj;
                    this.$nextTick(()=> {
                        this.serviceproviders = this.addBusinessForm.providerInfos[0].id;
                    })
                    var setArr = [];
                    obj.properties.forEach(item=> {
                        setArr.push({pname:item.name,value: "",name: item.name})
                    })
                    this.addBusinessForm.configuration = setArr;
                }
                this.mode = mode;
                this.visible = true;
                return this
            },
            //表单提交方法
            businessSubmit(){
                var configuration = {};
                this.addBusinessForm.configuration.forEach(item=> {
                    configuration[item.name] = item.value;
                })
                var obj = {
                    configuration: configuration,
                }
                if(this.mode == 'edit') {
                    obj.id = this.addBusinessForm.configEntity.id;
                    obj.name = this.addBusinessForm.configEntity.name;
                    obj.provider = this.addBusinessForm.configEntity.provider;
                    obj.type = this.addBusinessForm.configEntity.type;
                    if(this.addBusinessForm.configEntity.type == "email") {  //邮件
                        var newArr = [];
                        configuration.properties.forEach(item=> {
                            var formattedObject = {};
                            item.forEach(item => {  
                              formattedObject[item.name] = item.value;  
                            });
                            newArr.push(formattedObject);
                        })
                        configuration.properties = newArr;
                    }
                }
                if(this.mode == 'add') {  //新增
                    obj.name = this.addBusinessForm.pname;
                    obj.provider = this.serviceproviders;
                    obj.type = this.addBusinessForm.id;
                }
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        this.$HTTP.put("/api/blade-notify/notifier/config",obj).then(res=> {
                            this.isSaveing = false;
                            if(res.code == 200) {
                                this.$emit('success', this.addBusinessForm, this.mode);
                                this.visible = false;
                                this.$message.success("编辑成功");
                            }else {
                                this.$alert(res.message, "提示", {type: 'error'});
                            }
                        })
                    }else{
                        return false;
                    }
                })
            },
            //表单注入数据
            setData(data){
                if(data.configEntity == null) return;
                var configuration = data.configEntity.configuration;
                data.configuration = [];
                for(var key in configuration) {
                    if(Array.isArray(configuration[key])) {
                        var value = configuration[key];
                        var temArr = [];
                        value.forEach(item=> {
                            var setArr = [];
                            for(var key1 in item) {
                                setArr.push({
                                    name: key1,
                                    value: item[key1]
                                })
                            }
                            temArr.push(setArr);
                        })
                        data.configuration.push({
                            name: key,
                            value: temArr
                        })
                    }else {
                        data.configuration.push({
                            name: key,
                            value: configuration[key]
                        })
                    }
                }
                data.properties.forEach(item=> {
                    data.configuration.forEach(item1=> {
                        if(item1.name == item.property) {
                            item1.pname = item.name;
                        }
                    })
                })
                //可以和上面一样单个注入,也可以像下面一样直接合并进去
                Object.assign(this.addBusinessForm,data);
                this.$nextTick(()=> {
                    this.serviceproviders = this.addBusinessForm.providerInfos[0].id;
                })
            }
        },
        components: {
            ...ElementPlusIconsVue
        },
    }
</script>
 
<style scoped>
.modelTitle {
    padding-left: 45px;
    padding-bottom: 20px;
}
.modelTitle span {
    display: inline-block;
    width:50%;
}
.array-content {
    border: 1px solid #d8d8d8;
    border-radius: 2px;
    min-height: 200px;
    width: 100%;
    padding: 12px;
}
.otherBtn {
    margin-bottom: 12px;
}
</style>