wangting
2024-12-24 8e93a889280064483b901959e3b42f966d0ad371
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<template>
  <el-container>
    <el-main>
      <basic-container>
        <div style="display: flex;justify-content: space-between;flex-wrap: wrap">
          <div class="tag-group">
            <span class="tag-group__title">分组</span>
            <el-tag
              v-for="item in types"
              :key="item.key"
              :type="checkedTypes.includes(item.key)?'success':'info'" size="small"
              effect="plain" @click="changeType(item.key)">
              {{ item.value }}
            </el-tag>
          </div>
          <avue-input v-model="searchText" @change="handleSearch" placeholder="查询" size="mini" prefixIcon="el-icon-search" style="width: 300px;max-width: 30%"></avue-input>
        </div>
        <div style="height: calc(100vh - 190px)">
          <div>
            <el-button icon="el-icon-plus" type="primary" size="mini" @click="addHandler">添加</el-button>
            <el-button icon="el-icon-upload" type="primary" size="mini" @click="uploadHandler">上传</el-button>
          </div>
 
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane v-for="item in iconList" :label="item.lable" :name="item.lable" style="height: calc(100vh - 270px);overflow: auto ">
              <div class="iconList">
                <div class="iconItem" v-for="svg in item.list"
                     :data-value="svg.name" v-right-click="{action:checkSvg,data:svg}">
                  <div class="svgContent" v-html="svg.content"></div>
                  <span>{{svg.name.split(':')[1]}}</span>
                </div>
              </div>
              <div v-if="menuVisible" class="el-cascader-panel is-bordered iconList__menu"  :style="menuPosition">
                <div class="iconList__item" @click="editHandler">编 辑</div>
                <div class="iconList__item" @click="delHandler">删 除</div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
        <!-- 上传 -->
        <el-dialog
          v-dialogDrag
          title="上传图标"
          :visible.sync="visible"
          append-to-body="true"
          class="avue-dialog"
          width="600px"
          @close="visibleCloseHandler"
        >
          <avue-form ref="form" :option="option" v-model="form" :upload-after="uploadAfter" style="margin-bottom: 0;height:90px;overflow: hidden"> </avue-form>
          <pre style="font-size: 12px;color: #b3b3b3;margin-top: 0;padding:5px;background-color: #f5f5f5">上传文件为json文件,示例如下
[
  {
    "name": "iconoir:accessibility",
    "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1em\" height=\"1em\" viewBox=\"0 0 24 24\"><g fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2S2 6.477 2 12s4.477 10 10 10M7 9l5 1m5-1l-5 1m0 0v3m0 0l-2 5m2-5l2 5\"/><path fill=\"currentColor\" d=\"M12 7a.5.5 0 1 1 0-1a.5.5 0 0 1 0 1\"/></g></svg>"
  },
  {
    "name": "iconoir:accessibility-sign",
    "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1em\" height=\"1em\" viewBox=\"0 0 24 24\"><g fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"m11.5 12.5l7-.5l-1.5 6.5m-5.5-6l4.5-5L12.5 5L10 7.5m8.5-1a2 2 0 1 1 0-4a2 2 0 0 1 0 4\"/><path d=\"M5.5 12.5a5 5 0 0 1 7.584 6M3.729 15A5 5 0 0 0 11 20.831\"/></g></svg>"
  }
 ]
 文件内容须严格按照示例格式,name为图标库名+图标名称,以“:”分隔,svg为图标html代码</pre>
        </el-dialog>
        <!-- 添加、修改 -->
        <el-dialog
          v-dialogDrag
          :title="editType=='add'?'添加图标':'修改图标'"
          :visible.sync="editVisible"
          append-to-body="true"
          class="avue-dialog"
          width="500px"
          @close="closeHandler"
        >
          <avue-form ref="editForm" :option="editOption" v-model="editForm"> </avue-form>
          <span slot="footer" class="dialog-footer">
           <el-button size="small" type="primary" @click="editSaveHandler">确 定</el-button>
           <el-button size="small" @click="closeHandler">取 消</el-button>
          </span>
        </el-dialog>
      </basic-container>
    </el-main>
  </el-container>
</template>
 
<script>
import {getIcons,addIcon,updateIcon,deleteIcon} from "@/api/UI/Icons";
import {getDicts} from "@/api/system/dict";
import {validatenull} from "@/util/validate";
import {getStore} from "@/util/store";
import store from "@/store";
import {getToken} from "@/util/auth";
 
export default {
name: "index",
  directives: {
    rightClick: {
      bind(el, binding) {
        el.oncontextmenu = (e) => {
          e.preventDefault();
          const menu = binding.value;
          menu.action(e,menu.data);
        };
      }
    }
  },
  data() {
    return {
      types: [],
      checkedTypes:[],
      searchText:'',
      activeName:'',
      allIconList:[],
      iconList:[],
      visible: false,
      checkItem:null,
      option: {
        labelWidth: 80,
        submitBtn:false,
        emptyBtn:false,
        column: [{
          label: '分组',
          prop: 'groups',
          type: 'select',
          span: 24,
          multiple:true,
          dicData:[],
          props:{
            label:'value',
            value:'key'
          }
        }, {
            label: "附件上传",
            prop: "file",
            type: "upload",
            dataType: "object",
            accept:'.json',
            action:"api/webIconController/importIcon",
            data:{},
            headers:this.uploadHeaders,
            span: 24,
          }
        ]
      },
      form:{
        type:'svg',
        groups:[],
        file:null
      },
      editType:'edit',
      editVisible:false,
      editOption:{
        labelWidth: 90,
        submitBtn:false,
        emptyBtn:false,
        column: [{
          label: '图标库名',
          prop: 'libname',
          type: 'input',
          disabled:true,
          span: 24,
          rules: [{
            required: true,
            message: "请输入图标库名",
            trigger: "blur"
          }]
        },{
          label: '名称',
          prop: 'iconname',
          type: 'input',
          disabled:true,
          span: 24,
          rules: [{
            required: true,
            message: "请输入图标名称",
            trigger: "blur"
          }]
        },{
          label: '分组',
          prop: 'groups',
          type: 'select',
          span: 24,
          multiple:true,
          dicData:[],
          props:{
            label:'value',
            value:'key'
          }
        }, {
          label: "svg代码",
          prop: "content",
          type: "textarea",
          span: 24,
          placeholder:'请输入图标svg代码,格式示例<svg xmlns=\"http://www.w3.org/2000/svg\" ></svg>',
          rules: [{
            required: true,
            message: "请输入图标svg代码",
            trigger: "blur"
          }]
        }]
      },
      editForm:{
        oid:'',
        libname:'',
        iconname:'',
        content:''
      },
      menuVisible:false,
      menuPosition: {
        top: 0,
        left: 0
      }
    }
  },
  computed: {
    uploadHeaders() {
      return {
        "Authorizationtoken":getToken(),
      };
    },
  },
  watch:{
    'form.groups'(val){
      this.option.column[1].data={
        type:'svg',
        groups:val
      }
    }
  },
  created() {
    this.getGroups();
    this.initList();  },
  methods:{
    getGroups() {
      getDicts('EnumIconGroups').then(res => {
        if (res.data.success) {
          this.types = res.data.obj;
          this.option.column[0].dicData = res.data.obj;
          this.editOption.column[2].dicData =res.data.obj;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    initList(reload){
      if(reload) {
        getIcons().then(res => {
          if (res.data.success) {
            this.iconList = res.data.data;
            this.allIconList = this.iconList;
            store.dispatch("setIcons", this.iconList);
            if (this.iconList.length > 0) {
              this.activeName = this.iconList[0].lable;
            }
          } else {
            this.$message.error(res.data.msg);
          }
        })
 
        return;
      }
      if (!validatenull(this.$store.state.icons)) {
        this.iconList = this.$store.state.icons;
      } else {
        this.iconList = getStore({ name:'icons'});
      }
      this.allIconList=this.iconList;
      if(this.iconList && this.iconList.length>0){
        this.activeName=this.iconList[0].lable;
      }else {
        getIcons().then(res => {
          if (res.data.success) {
            this.iconList = [{
              lable: 'iconoir',
              list: [{
                oid: '',
                groups: '1',
                "name": "iconoir:accessibility",
                "content": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1em\" height=\"1em\" viewBox=\"0 0 24 24\"><g fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"1.5\"><path d=\"M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2S2 6.477 2 12s4.477 10 10 10M7 9l5 1m5-1l-5 1m0 0v3m0 0l-2 5m2-5l2 5\"/><path fill=\"currentColor\" d=\"M12 7a.5.5 0 1 1 0-1a.5.5 0 0 1 0 1\"/></g></svg>"
              }]
            }]
            this.iconList = res.data.data;
            this.allIconList = this.iconList;
            store.dispatch("setIcons", this.iconList);
            if (this.iconList.length > 0) {
              this.activeName = this.iconList[0].lable;
            }
          } else {
            this.$message.error(res.data.msg);
          }
        })
      }
    },
    changeType(type){
      if(this.checkedTypes.includes(type)){
        this.checkedTypes=this.checkedTypes.filter(item=> item!=type)
      }else {
        this.checkedTypes.push(type)
      }
      //按照分类过滤图标
      if(this.checkedTypes.length==0){
        this.iconList=this.allIconList
      }else{
        const checkedTypes=JSON.stringify(this.checkedTypes);
        const iconList=[];
        this.allIconList.forEach(item=>{
          iconList.push({
            lable:item.lable,
            list:item.list.filter(iconitem=> {
              return checkedTypes.indexOf(iconitem.groups)!=-1;
            })
          })
        })
        this.iconList=iconList;
      }
      this.activeName=this.iconList[0].lable;
    },
    handleSearch(data){
      if(data.value==''){
        this.iconList=this.allIconList;
      }
      let iconList=[]
      this.allIconList.forEach(item=> {
        iconList.push({
          lable:item.lable,
          list: item.list.filter(iconItem=>{
            return iconItem.name.replace(item.lable,'').indexOf(data.value)!=-1;
          })
        })
      })
      this.iconList=iconList;
    },
    handleClick(tab, event){
 
    },
    uploadHandler(){
      this.visible = true;
    },
    //上传后
    uploadAfter(res,done){
      if (res.success) {
        this.$message.success(res.msg);
        this.initList(true);
        this.visibleCloseHandler();
      }else {
        this.$message.error(res.msg);
      }
      done();
    },
    // 关闭对话框
    visibleCloseHandler() {
      const form = {
        type:'svg',
        groups:[],
        file:null
      }
      this.form = form;
      this.visible = false;
      this.$refs.form.clearValidate();
    },
    checkSvg(event,svgObj){
      this.checkItem=svgObj;
      // 设置菜单可见
      this.menuVisible = true;
      this.menuPosition.top = `${event.clientY}px`;
      this.menuPosition.left = `${event.clientX}px`;
      document.addEventListener('click',()=>{
        this.menuVisible=false;
      })
    },
    // 上传保存
    uploadSaveHandler() {
      this.$refs.form.validate((valid,done) => {
        if (valid) {
          done()
          addIcon(this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg);
              this.initList(true);
              done();
              this.visibleCloseHandler();
            } else {
              this.$message.error(res.data.msg);
            }
          })
        } else {
          return false;
        }
      });
    },
    addHandler(){
      this.editOption.column[0].disabled=false;
      this.editOption.column[1].disabled=false;
      this.editType='add';
      this.editVisible=true;
    },
    editHandler(){
      this.editType='edit';
      this.editOption.column[0].disabled=true;
      this.editOption.column[1].disabled=true;
      this.editForm=this.checkItem;
      this.editForm.libname=this.checkItem.name.split(':')[0]
      this.editForm.iconname=this.checkItem.name.split(':')[1]
      this.editVisible=true;
    },
    editSaveHandler(){
      this.$refs.editForm.validate((valid,done) => {
        if (valid) {
          done()
          this.editForm.name= this.editForm.libname+':'+ this.editForm.iconname;
          if(this.editType=='edit'){
            updateIcon(this.editForm).then(res => {
              if (res.data.code === 200) {
                this.$message.success(res.data.msg);
                this.initList(true);
                done();
                this.closeHandler();
              } else {
                this.$message.error(res.data.msg);
              }
            })
          }else {
            addIcon(this.editForm).then(res => {
              if (res.data.code === 200) {
                this.$message.success(res.data.msg);
                this.initList(true);
                done();
                this.closeHandler();
              } else {
                this.$message.error(res.data.msg);
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    closeHandler(){
      this.editForm={
        type:'svg',
        oid:'',
        libname:'',
        iconname:'',
        groups:[],
        content:''
      };
      this.editOption.column[0].disabled=false;
      this.editOption.column[1].disabled=false;
      this.editVisible=false;
      this.$refs.editForm.clearValidate();
    },
    delHandler(){
      this.$confirm('您确定要删除该图标吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const {name,oid} = this.checkItem;
        const obj = {name,oid};
        deleteIcon(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg);
            this.initList(true);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>
 
<style scoped>
.tag-group{font-size: 14px;}
.el-tag{
  margin: 0 0 10px 10px;
  cursor: pointer;
}
.iconList{
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.iconList .iconItem{
  width: 85px;
  margin: 6px 5px 10px 5px;
  text-align: center;
}
.iconList .iconItem .svgContent{
  font-size: 26px;
}
.iconList .iconItem .svgContent:hover{
  transform: scale(1.5);
}
.iconList .iconItem span{
  font-size: 12px;
}
.iconList__menu{
  width: 150px;
  position: fixed;
  z-index: 1024;
  flex-wrap: wrap;
  background-color: #fff;
}
.iconList__item {
  height: 34px;
  line-height: 34px;
  outline: 0;
  padding: 0 10px;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 100%;
  color: #666;
}
.iconList__item:hover {
  cursor: pointer;
  color: #409eff
}
</style>