tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<template>
  <div>
    <div v-if="!showEdit">
      <el-container class="layout-container-demo">
        <el-aside width="400px">
          <el-scrollbar>
            <el-card class="box-card">
              <template #header>
                <div class="card-header">
                  <el-input v-model="keyword" placeholder="字典名称" />
                  <Search class="icon_btn_class" @click="handleQuery()" />
                  <el-icon @click="handleCreate()"><CirclePlus /></el-icon>
                  <!-- <el-button class="button" text>Operation button</el-button> -->
                </div>
              </template>
 
              <div v-for="dict in dicts" :key="dict.id">
                <!-- 由于SVG图标默认不携带任何属性 -->
                <!-- 你需要直接提供它们 -->
                <el-link class="dict_class" @click="handleShowItem(dict.id)"> {{ dict.remark }}</el-link>
                <el-link class="dict_class" @click="handleShowItem(dict.id)"> {{ dict.name }} </el-link>
 
                <Edit class="icon_btn_class" @click="handleEdit(dict.id)" />
                <Delete class="icon_btn_class" @click="handleDelete(dict.id)" />
                <el-icon @click="handleCreateItem(dict)"><CirclePlus /></el-icon>
                <!-- <Search class="icon_btn_class" /> -->
                <!-- <Share class="icon_btn_class"  /> -->
              </div>
            </el-card>
          </el-scrollbar>
        </el-aside>
 
        <el-container>
          <el-header>
            <div class="toolbar">
              <el-select
                v-model="editId"
                @clear="handleClear"
                @change="handleDictChange"
                clearable
                filterable
                placeholder="父字典值"
              >
                <el-option v-for="item in dicts" :key="item.id" :label="item.remark" :value="item.id" />
              </el-select>
 
              <el-input v-model="itemKeyword" placeholder="字典项名称" />
              <Search class="icon_btn_class2" @click="handleQueryItems" />
              <!-- <el-icon ><CirclePlus /></el-icon> -->
            </div>
          </el-header>
 
          <el-main>
            <el-scrollbar>
              <el-table :data="dictItems">
                <el-table-column prop="remark" label="字典名称" />
                <el-table-column prop="name" label="字典值" />
                <el-table-column prop="label" label="字典项名称" />
                <el-table-column prop="value" label="字典项值" />
                <el-table-column prop="sort" label="排序号" />
 
                <el-table-column fixed="right" label="操作">
                  <template #default="scope">
                    <!-- <el-button link type="primary" size="small" @click="handleClick">Detail</el-button>
                        <el-button link type="primary" size="small">Edit</el-button> -->
                    <Edit class="icon_btn_class" @click="handleItemEdit(scope.row)" />
                    <Delete class="icon_btn_class" @click="handleItemDelete(scope.row)" />
                  </template>
                </el-table-column>
              </el-table>
            </el-scrollbar>
          </el-main>
        </el-container>
      </el-container>
    </div>
    <!-- 编辑页面 -->
    <sys-dict-modify v-else @editClose="editClose" :editId="editId"></sys-dict-modify>
 
    <div>
      <el-drawer v-model="drawer2" :direction="direction" :before-close="handleItemClose">
        <template #header>
          <h4>字典项新增</h4>
        </template>
        <div class="wrap">
          <el-row>
            <el-col :lg="16" :md="20" :sm="24" :xs="24">
              <el-form :model="sysDictItem" status-icon ref="form" label-width="100px" @submit.prevent :rules="rules">
                <el-form-item label="字典名称" prop="">
                  <el-input v-model="sysDictItem.remark" readonly></el-input>
                </el-form-item>
                <el-form-item label="字典项名称" prop="label">
                  <el-input v-model="sysDictItem.label" placeholder="请填写字典项名称"></el-input>
                </el-form-item>
                <el-form-item label="字典项值" prop="value">
                  <el-input v-model="sysDictItem.value" placeholder="请填写字典项值"></el-input>
                </el-form-item>
                <el-form-item label="排序号" prop="sort">
                  <el-input v-model="sysDictItem.sort" placeholder="请填写排序号"></el-input>
                </el-form-item>
                <el-form-item class="submit">
                  <el-button type="primary" @click="submitForm">保 存</el-button>
                  <el-button @click="resetForm">重 置</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
        <!-- <template #default>
           <div>
             <el-radio v-model="radio1" label="Option 1" size="large"
               >Option 1</el-radio
             >
             <el-radio v-model="radio1" label="Option 2" size="large"
               >Option 2</el-radio
             >
           </div>
         </template> -->
        <template #footer>
          <div style="flex: auto">
            <!-- <el-button @click="cancelClick">cancel</el-button>
             <el-button type="primary" @click="confirmClick">confirm</el-button> -->
          </div>
        </template>
      </el-drawer>
    </div>
  </div>
</template>
 
<script>
import { onMounted, ref, reactive } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { Edit, View as IconView } from '@element-plus/icons-vue'
 
import sysDictModel from '@/model/sysDict'
import sysDictItemModel from '@/model/sysDictItem'
 
import SysDictModify from './sys-dict.vue'
 
export default {
  components: {
    SysDictModify,
  },
  setup() {
    const dicts = ref([])
    const loading = ref(false)
    const showEdit = ref(false)
    const editId = ref('')
    const keyword = ref('')
 
    // 子项字典
    const form = ref(null)
    const dictItems = ref([])
    const drawer2 = ref(false)
    const direction = ref('rtl')
    const sysDictItem = reactive({ id: '', dict_id: '', label: '', value: '', sort: 0, remark: '', name: '' })
    const sysDict = reactive({ name: '', remark: '' })
    const itemKeyword = ref('')
    const editItemId = ref('')
 
    const listAssign = (a, b) => Object.keys(a).forEach(key => {
      a[key] = b[key] || a[key]
    })
 
    const listAssignNull = a => Object.keys(a).forEach(key => {
      a[key] = ''
    })
 
    onMounted(() => {
      getSysDictList()
      // 列出所有字典项目
      getSysDictItemList()
    })
 
    const getSysDictList = async () => {
      try {
        loading.value = true
        dicts.value = await sysDictModel.getSysDictList(keyword.value)
        loading.value = false
      } catch (error) {
        loading.value = false
        if (error.code === 10020) {
          dicts.value = []
        }
      }
    }
 
    const handleQuery = () => {
      // 列出所有字典
      getSysDictList()
    }
 
    const handleDetail = id => {
      editId.value = id
    }
 
    const handleCreate = () => {
      showEdit.value = true
      editId.value = ''
    }
 
    const handleEdit = id => {
      showEdit.value = true
      editId.value = id
    }
 
    const handleDelete = id => {
      ElMessageBox.confirm('此操作将永久删除该字典, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const res = await sysDictModel.deleteSysDict(id)
          if (res.code < window.MAX_SUCCESS_CODE) {
            getSysDictList()
            ElMessage.success(`${res.message}`)
          }
        })
        .catch(() => {
          // 用户点击取消按钮时执行的操作
          // 可以选择不做任何事情,也可以显示取消消息
          // ElMessage.info('操作已取消'); // 如果需要显示取消消息,可以取消注释
        })
    }
 
    const editClose = () => {
      showEdit.value = false
      getSysDictList()
      getSysDictItemList()
    }
 
    const onSubmit = () => {
      console.log('submit!')
    }
 
    // 字典子项目
 
    const handleQueryItems = () => {
      getSysDictItemList()
    }
 
    const handleShowItem = dictId => {
      // 根据当前的字典查找字典项目,并展示
      editId.value = dictId
      sysDictItem.dict_id = dictId
      getSysDictItemList()
    }
 
    const getSysDictItemList = async () => {
      try {
        loading.value = true
        dictItems.value = await sysDictItemModel.getSysDictItemList(itemKeyword.value, editId.value)
        loading.value = false
      } catch (error) {
        loading.value = false
        if (error.code === 10020) {
          dictItems.value = []
        }
      }
    }
 
    // 创建子字典项目
 
    // 表单验证规则
    const { rules } = getRules()
 
    const handleCreateItem = dict => {
      drawer2.value = true
      // listAssign(sysDict,dict);
      // 创建的时候将id设置为空
      sysDictItem.id = ''
      sysDictItem.dict_id = dict.id
      sysDictItem.remark = dict.remark
      sysDictItem.name = dict.name
 
      // console.log(sysDict)
    }
 
    // 重置表单
    const resetForm = () => {
      form.value.resetFields()
      // listAssignNull(sysDictItem)
      sysDictItem.label = ''
      sysDictItem.value = ''
    }
 
    const submitForm = async formName => {
      form.value.validate(async valid => {
        if (valid) {
          let res = {}
          if (sysDictItem.id) {
            res = await sysDictItemModel.editSysDictItem(sysDictItem.id, sysDictItem)
          } else {
            res = await sysDictItemModel.createSysDictItem(sysDictItem)
          }
          if (res.code < window.MAX_SUCCESS_CODE) {
            ElMessage.success(`${res.message}`)
            resetForm()
            getSysDictItemList()
          }
        } else {
          console.error('error submit!!')
          ElMessage.error('请将信息填写完整')
        }
      })
    }
 
    const indexMethod = index => index + 1
 
    const handleItemClose = () => {
      resetForm()
      drawer2.value = false
    }
 
    const handleClear = () => {
      editId.value = ''
      sysDictItem.dict_id = ''
      getSysDictItemList()
    }
 
    const handleDictChange = val => {
      editId.value = val
      sysDictItem.dict_id = val
      getSysDictItemList()
    }
 
    const handleItemEdit = row => {
      listAssign(sysDictItem, row)
      drawer2.value = true
    }
 
    const handleItemDelete = row => {
      ElMessageBox.confirm('此操作将永久删除该字典, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const res = await sysDictItemModel.deleteSysDictItem(row.id)
          if (res.code < window.MAX_SUCCESS_CODE) {
            getSysDictItemList()
            ElMessage.success(`${res.message}`)
          }
        })
        .catch(() => {
          // 用户点击取消按钮时执行的操作
          // 可以选择不做任何事情,也可以显示取消消息
          // ElMessage.info('操作已取消'); // 如果需要显示取消消息,可以取消注释
        })
    }
 
    return {
      keyword,
      dicts,
      editId,
      showEdit,
      editClose,
      handleQuery,
      handleCreate,
      handleEdit,
      handleDelete,
      handleShowItem,
      onSubmit,
 
      handleCreateItem,
      drawer2,
      direction,
      sysDictItem,
      sysDict,
      resetForm,
      submitForm,
      rules,
      form,
      dictItems,
      itemKeyword,
      handleItemClose,
      handleQueryItems,
      handleClear,
      handleDictChange,
      handleItemEdit,
      handleItemDelete,
    }
  },
}
 
/**
 * 表单验证规则
 */
function getRules() {
  /**
   * 验证回调函数
   */
  const checkInfo = (rule, value, callback) => {
    if (!value) {
      callback(new Error('信息不能为空'))
    }
    callback()
  }
  const rules = {
    label: [{ validator: checkInfo, trigger: 'blur', required: true }],
    value: [{ validator: checkInfo, trigger: 'blur', required: true }],
  }
  return { rules }
}
</script>
 
<style scoped>
.layout-container-demo {
  min-height: 100% !important;
  /* margin:10px ; */
}
.layout-container-demo .el-header {
  position: relative;
  /* background-color: var(--el-color-primary-light-7); */
  color: var(--el-text-color-primary);
}
.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  /* background: var(--el-color-primary-light-8); */
}
.layout-container-demo .el-menu {
  border-right: none;
}
.layout-container-demo .el-main {
  padding: 0;
}
.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  /* right: 20px; */
}
 
.el-card {
  width: 99% !important;
  height: 100%;
  overflow: auto;
}
 
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
 
.text {
  font-size: 14px;
}
 
.item {
  margin-bottom: 18px;
}
 
.box-card {
  width: 480px;
  height: 100%;
}
 
.el-link {
  margin-right: 8px;
}
 
.icon_btn_class {
  width: 1em;
  height: 1em;
  margin-left: 4px;
  margin-right: 4px;
}
.icon_btn_class2 {
  width: 2em;
  height: 1em;
  margin-left: 4px;
}
 
.dict_class {
  width: 100px;
  overflow: hidden;
}
</style>