叶松
2023-10-26 979c7c817b2e7d2430ac447b10f943c12dac3b64
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<template>
    <!-- 安全管理 ==> 风险分级管控 => 危险源告知-->
    <div class="main">
        <!-- header -->
        <div class="main_header">
            <div class="header_item">
                <span class="header_label">安全等级:</span>
                <el-select v-model="queryInfo.levelType" placeholder="请选择安全等级" clearable>
                    <el-option v-for="item in safetyLevels" :key="item.levelType" :label="item.name"
                        :value="item.levelType">
                    </el-option>
                </el-select>
            </div>
            <div class="header_item">
                <el-button icon="el-icon-search" v-permission="'search'" @click="queryReset">查询</el-button>
                <el-button class="search_btn" icon="el-icon-plus" v-permission="'insert'" @click="addRow">新增</el-button>
            </div>
        </div>
 
        <!-- table -->
        <div class="main_content">
            <cpnTable :table-index="true" :table-data="dataList" :table-columns="tableColumns" :page-total="total"
                :page-num.sync="queryInfo.pageNum" :page-size.sync="queryInfo.pageSize" :page-change="pageChange">
            </cpnTable>
        </div>
 
        <!-- dialog -->
        <el-dialog class="prop_dialog" v-if="isRender" :title="dialogTitle" :visible.sync="asyncVisible" width="460px"
            @close="closeForm">
            <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto" class="rule_form">
                <el-form-item label="区域名称:" prop="region">
                    <el-input v-model="ruleForm.region" clearable placeholder="请输入区域名称"></el-input>
                </el-form-item>
                <el-form-item label="安全等级:" prop="levelType">
                    <el-select v-model="ruleForm.levelType" placeholder="请选择">
                        <el-option v-for="item in safetyLevels" :key="item.levelType" :label="item.name"
                            :value="item.levelType">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="区域颜色:" prop="regionColor">
                    <el-input v-model="ruleForm.regionColor" placeholder="请选择区域颜色" disabled></el-input>
                    <el-color-picker v-model="ruleForm.regionColor"></el-color-picker>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="asyncVisible = false">取 消</el-button>
                <el-button class="submit_btn" @click="onSubmit('ruleForm')">提 交</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import {throttle} from '@/plugins/public'; // 导入节流、动态切换组件尺寸方法
import cpnTable from '@/components/element/Table'
export default {
    data() {
        return {
            isRender: false,
            loading: false,
            asyncVisible: false,
            submitMode: '', // add update
            total: 0,
            queryInfo: {
                pageNum: 1,
                pageSize: 10,
                levelType: '',
            },
            safetyLevelMap: {
                1: {name: 'IV-A级', levelType: 1},
                2: {name: 'IV-B级', levelType: 1},
                3: {name: 'IV-C级', levelType: 1},
                4: {name: 'IV-D级', levelType: 1},
            }, //  安全等级 map
            safetyLevels: [
                {name: 'IV-A级', levelType: 1},
                {name: 'IV-B级', levelType: 2},
                {name: 'IV-C级', levelType: 3},
                {name: 'IV-D级', levelType: 4},
            ], // 安全等级 arr
            dataList: [],
            tableColumns: [], // table配置
            ruleForm: {}, // 按钮表单
            rules: {
                region: [{required: true, message: '请输入区域名称', trigger: 'blur'}],
                levelType: [{required: true, message: '请选择安全等级', trigger: 'change'}],
                regionColor: [{required: true, message: '请选择区域颜色', trigger: 'change'}],
            },
            $http: '', // api路径赋值
        }
    },
    components: {
        cpnTable
    },
    computed: {
        isUpdate() {
            return this.submitMode === 'update'
        },
        dialogTitle() {
            return this.isUpdate ? '修改区域' : '新增区域'
        },
    },
    created() {
        this.$http = this.$api.Safety.RiskGrad.warning
        this.setFormProps()
        this.setTableColumn()
        this.getLists()
    },
    methods: {
        // 获取table列表数据
        getLists() {
            this.loading = true
            let params = this.queryInfo
            this.$http.getLists(params).then(res => {
                if (res.statusMsg === 'ok') {
                    this.total = res.data.total
                    this.dataList = res.data.list
                }
                this.loading = false
            })
        },
        // 初始化 ruleform
        setFormProps(options = {}) {
            let _form = {
                region: '', // 区域名称
                levelType: '', // 安全等级
                regionColor: '',  // 区域等级颜色
            }
            this.ruleForm = Object.keys(options).length ? options : _form
        },
        // 初始化 table 配置
        setTableColumn() {
            this.tableColumns = [
                {index: true},
                {name: "区域名称", key: "region"},
                {name: "安全等级", key: "levelType", formatter: (row) => {return this.safetyLevelMap[row.levelType].name}},
                {name: "区域颜色", key: "regionColor"},
                {
                    operation: true, name: "操作", width: 140, value: [
                        {name: "修改", class: "table_btn", permission: "update", handleRow: this.updateRow},
                        {name: "删除", class: "delete_btn", permission: "delete", handleRow: this.deleteRow},
                    ]
                },
            ]
        },
        // 重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields()
        },
        // 显示表单
        showForm() {
            !this.isRender && (this.isRender = true)
            this.asyncVisible = true
        },
        // 隐藏表单
        closeForm() {
            this.asyncVisible = false
            this.resetForm('ruleForm')
        },
        // 查询按钮列表信息
        queryReset() {
            this.queryInfo.pageNum = 1
            this.queryInfo.pageSize = 10
            this.getLists()
        },
        // 添加数据
        addRow() {
            this.submitMode = 'add'
            this.showForm()
        },
        // 更新数据
        updateRow(row) {
            this.submitMode = 'update'
            this.showForm()
            this.$nextTick(() => {
                this.ruleForm.id = ''
                Object.keys(this.ruleForm).forEach(item => {
                    if (row.hasOwnProperty.call(row, item)) {
                        this.ruleForm[item] = row[item]
                    }
                })
            })
        },
        // 删除数据
        deleteRow(row) {
            const params = {id: row.id}
            this.$confirm("该操作将删除该信息,是否继续删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.$http.delete(params).then(res => {
                    if (res.statusMsg === 'ok') {
                        this.queryReset();
                        this.$message.success("删除成功!")
                    } else {
                        this.$message.warning(res.statusMsg)
                    }
                })
            }).catch(() => {
                this.$message.warning("您已取消")
            })
        },
        // 提交表单
        onSubmit: throttle(function () {
            this.$refs.ruleForm.validate(valid => {
                if (!valid) return
                const params = this.ruleForm
                if (this.isUpdate) {
                    // 更新
                    this.$http.update(params).then((res) => {
                        if (res.statusMsg === 'ok') {
                            this.closeForm()
                            this.getLists()
                            this.$message.success('更新成功!')
                        } else {
                            this.$message.warning(res.statusMsg)
                        }
                    })
                } else {
                    // 添加
                    this.$http.insert(params).then((res) => {
                        if (res.statusMsg === 'ok') {
                            this.closeForm()
                            this.getLists()
                            this.$message.success('添加成功!')
                        } else {
                            this.$message.warning(res.statusMsg)
                        }
                    })
                }
            })
        }, 1000),
        // 分页改变
        pageChange() {
            this.getLists()
        },
    }
}
</script>
  
<style lang="scss" scoped>
@import '@/style/layout-main.scss';
</style>
<style lang="scss">
.el-color-dropdown__btns {
    .el-color-dropdown__btn {
        background: #fff;
        border: 1px solid #dcdfe6;
        color: #606266;
    }
 
    .el-color-dropdown__link-btn {
        border-color: transparent;
        background-color: transparent;
        color: #66b1ff;
    }
}
</style>