index.vue
23.6 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
<!-- 菜单列表 -->
<template>
<div>
<el-row>
<el-col :span="4" style="padding-top: 16px;">
<el-button icon="el-icon-plus" :size="btn_size" type="primary" @click="handleAdd">创建菜单</el-button>
<!-- 新增的导入菜单按钮 -->
<el-button icon="el-icon-plus" :size="btn_size" type="primary" @click="openImportDialog">导入菜单</el-button>
</el-col>
<el-col style="text-align: right;" :span="20">
<glsx-combi-form
ref="showSearch"
:formObj="searchForm"
:formData="searchFormData"
:labelWidth="'120px'"
>
<el-button :size="btn_size" type="primary" @click="reset">重置</el-button>
<el-button :size="btn_size" type="primary" :id="tableParams.btnId">查询</el-button>
</glsx-combi-form>
</el-col>
</el-row>
<d2-table
ref="menuTable"
:searchForm="tableSearchForm"
:pageConf="pageConf"
:tableParams="tableParams"
:getListApi="getListApi"
:beforeSearchFn="beforeSearchFn"
>
<span slot="auth-button" slot-scope="scope">
<el-button type="text" @click="handleEdit(scope.scope)">修改</el-button>
<el-button type="text" @click="handleDelete(scope.scope)">删除</el-button>
</span>
</d2-table>
<glsx-dialog
:title="editForm.menuCode ? '编辑菜单' : '创建菜单'"
:isVisible="editVisible"
width="650px"
@closeDialog="handleClose"
@confirmDialog="handleConfirm"
>
<glsx-combi-form
ref="editFormRef"
labelWidth="120px"
:formRef="editChildRef"
:formObj="editForm"
:formData="editFormData"
:formRules="editRule"
>
<template slot="menuTree" slot-scope="scope">
<select-tree
v-if="editVisible"
:form="editForm"
:width="'400'"
:targetItem="scope.scope"
:treeData="baseData.menuTreeList"
:defaultProps="treeDefaultProps"
@selected="handleTreeSelect"
></select-tree>
</template>
<template slot="menuIcon" slot-scope="scope">
<el-select
v-model="editForm[scope.scope.name]"
placeholder="请选择icon"
clearable
style="width: 400px"
>
<el-option
v-for="item in scope.scope.optionList"
:key="item[scope.scope.optionId]"
:label="item[scope.scope.optionLabel]"
:value="item[scope.scope.optionId]"
style="height: 24px;margin: 5px 0;"
>
<el-image
style="width: 24px; height: 24px"
:src="item[scope.scope.optionLabel]"
fit="contain"
></el-image>
</el-option>
</el-select>
</template>
</glsx-combi-form>
</glsx-dialog>
<!-- 导入菜单的弹窗 -->
<glsx-dialog
title="导入菜单"
:isVisible="importDialogVisible"
width="650px"
@closeDialog="closeDialog"
@confirmDialog="importFile"
>
<div class="file-upload-container">
<span class="required-star">*</span>上传文件:
<el-input
v-model="fileName"
placeholder="请选择文件"
readonly
size="middle"
style="display:inline-block; width: calc(100% - 150px);"
></el-input>
<el-button @click="selectFile" type="primary" size="small" style="margin-left:80px;margin-top: 10px;">选择文件</el-button>
<input
type="file"
ref="fileInput"
style="display: none;"
@change="handleFileChange"
>
</div>
</glsx-dialog>
</div>
</template>
<script>
import _ from 'lodash'
import SelectTree from '../components/SelectTree'
import { menuApi, baseApi } from '@/api/productManage'
import {uploadApi } from '@/api/upload'
export default {
name: 'menuList',
data() {
return {
importDialogVisible: false,
fileName: '',
selectedFile: null,
searchForm: {
productIds: [],
menuName: ''
},
tableSearchForm: {
productIds: [],
menuName: ''
},
oldVal: {
productIds: []
},
searchFormData: [
{
label: '产品',
name: 'productIds',
type: 'select',
width: '300px',
multiple: true,
optionList: [],
optionId: 'dictValue',
optionLabel: 'dictLabel',
optionsName: 'productList',
changeAttr: (value, targetItem) => {
if (value.includes('')) {
this.searchForm[targetItem.name] = !this.oldVal[
targetItem.name
].includes('')
? targetItem.optionList.map(item => item[targetItem.optionId])
: this.searchForm[targetItem.name].filter(item => item !== '')
} else {
if (this.oldVal[targetItem.name].includes('')) {
this.searchForm[targetItem.name] = []
} else if (value.length === targetItem.optionList.length - 1) {
this.searchForm[targetItem.name].push('')
}
}
this.oldVal[targetItem.name] = this.searchForm[targetItem.name]
this.tableSearchForm[targetItem.name] = this.searchForm[
targetItem.name
].includes('')
? []
: this.searchForm[targetItem.name]
}
},
{
label: '父菜单名称',
name: 'parentName',
type: 'input',
isClear: true,
keyFn: this.keyDowmGetList
},
{
label: '菜单名称',
name: 'menuName',
type: 'input',
isClear: true,
keyFn: this.keyDowmGetList
}
],
getListApi: menuApi.getListApi,
pageConf: {
pageSize: 'pageSize',
pageNum: 'pageNum',
totalCount: 'data.count'
},
// 表格参数
tableParams: {
btnId: new Date().getTime() + parseInt(Math.random() * 1000),
listName: 'data.data',
tableOption: true,
tableColumns: [
{
label: '菜单名称',
prop: 'menuName'
},
{
label: '菜单编号',
prop: 'menuCode'
},
{
label: '排序值',
prop: 'sortPosition'
},
{
label: '菜单地址',
prop: 'menuUrl'
},
{
label: '父级菜单名',
prop: 'parentName'
},
{
label: '菜单类型',
prop: 'isSpecial',
formatter: cellValue => {
return {
0: '普通菜单',
1: '特殊菜单'
}[cellValue.isSpecial]
}
},
{
label: '产品名称',
prop: 'productName'
},
{
label: '更新时间',
prop: 'updatedTime'
}
]
},
// 弹窗
title: '创建菜单',
editVisible: false,
editChildRef: 'editChildRef',
editForm: {
isSpecial: 0,
parentName: '',
parentId: '',
ownedCusgroupType: '',
ownedCusgroupValue: [],
ownedMerchant: ''
},
editRule: {
productId: [
{ required: true, message: '请选择产品', trigger: 'change' }
],
isSpecial: [
{ required: true, message: '请选择菜单类型', trigger: 'change' }
],
parentId: [
{ required: true, message: '请选择父级菜单', trigger: 'change' }
],
menuName: [
{ required: true, message: '请输入菜单名称', trigger: 'change' }
],
menuUrl: [
{ required: true, message: '请输入菜单地址', trigger: 'change' }
],
// iconId: [{ required: true, message: '请上传菜单ICON', trigger: 'change' }],
sort: [
{ required: true, message: '请选择产品排序值', trigger: 'change' }
],
ownedCusgroupType: [
{ required: true, message: '请选择所属客户群体', trigger: 'change' }
],
ownedCusgroupValue: [
{ required: true, message: '请选择', trigger: 'change' }
],
menuPosition: [
{ required: true, message: '请输入菜单位置', trigger: 'change' },
{
required: true,
validator: (rule, value, callback) => {
return /^[0-9]*$/.test(value)
? callback()
: callback(new Error('菜单位置只能输入数字'))
},
trigger: 'change'
}
]
},
treeDefaultProps: {
parent: 'parentId',
children: 'childlist',
value: 'id',
label: 'menuName'
},
// 下拉框对应List
attrToList: {
'1': 'accounTypetList',
'2': 'accountLevelList',
'4': 'accountRoleList'
},
importMenuUrl:'',
editFormData: [
{
label: '产品',
name: 'productId',
width: '400px',
type: 'select',
isDisabled: false,
optionList: [],
optionId: 'dictValue',
optionLabel: 'dictLabel',
optionsName: 'productList',
changeAttr: (value, targetItem) => {
this.editForm.parentId = ''
this.editForm.parentName = ''
this.getMenuTree(value)
}
},
{
label: '菜单类型',
name: 'isSpecial',
type: 'radio',
optionList: [
{
dictValue: 0,
dictLabel: '普通菜单'
},
{
dictValue: 1,
dictLabel: '特殊菜单'
}
],
optionId: 'dictValue',
optionLabel: 'dictLabel',
handleRadioChange: e => {
this.isMenuSpecial(e === 1)
}
},
{
label: '父级菜单',
name: 'parentId',
isDisabled: false,
nameLabel: 'parentName',
addHtml: 'menuTree',
formName: 'editForm'
},
{
label: '菜单名称',
name: 'menuName',
type: 'input',
width: '400px',
maxlength: 15
},
{
label: '菜单地址',
name: 'menuUrl',
width: '400px',
type: 'input',
maxlength: 120
},
{
label: '菜单ICON',
name: 'iconId',
type: 'select',
addHtml: 'menuIcon',
optionList: [],
optionId: 'id',
optionLabel: 'fileUrl',
optionsName: 'iconList'
},
{
label: '菜单排序值',
name: 'sort',
width: '190px',
type: 'select',
optionList: [],
optionId: 'dictValue',
optionLabel: 'dictLabel',
optionsName: 'sortList'
},
{
label: '',
name: 'menuPosition',
width: '200px',
type: 'input',
maxlength: 5,
placeholder: '请输入菜单位置'
},
{
label: '所属客户群体',
name: 'ownedCusgroupType',
width: '190px',
type: 'select',
isHide: true,
optionList: [],
optionId: 'dictValue',
optionLabel: 'dictLabel',
optionsName: 'ownCusList',
changeAttr: (value, targetItem) => {
// baseData[attrToList[form[targetItem.firstVal]]]
this.editFormData[
this.editFormData.findIndex(
item => item.name === 'ownedCusgroupValue'
)
].isHide = value === 3
this.editFormData[
this.editFormData.findIndex(item => item.name === 'ownedMerchant')
].isHide = value !== 3
this.editForm.ownedCusgroupValue = []
this.editForm.ownedMerchant = ''
this.editRule.ownedCusgroupValue[0].required = value !== 3
this.$refs.editFormRef.$refs[this.editChildRef].validateField(
'ownedCusgroupValue'
)
if (value !== 3) {
this.baseData.cusGroupList = this.baseData[this.attrToList[value]]
this.getOptionsList('editFormData', false)
}
}
},
{
label: '',
name: 'ownedCusgroupValue',
width: '200px',
type: 'select',
isHide: true,
multiple: true,
// isDisabled: true,
optionList: [],
optionId: 'dictValue',
optionLabel: 'dictLabel',
optionsName: 'cusGroupList'
},
{
label: '商户/门店ID',
name: 'ownedMerchant',
width: '400px',
type: 'input',
isHide: true,
inputType: 'textarea',
maxlength: 255,
placeholder: '请输入商户/门店ID,多商户或者门店时,用“,”隔开。'
}
],
// 基本数据
baseData: {
productList: [],
menuTreeList: [],
sortList: [],
iconList: [],
ownCusList: [],
accounTypetList: [],
accountLevelList: [],
accountRoleList: [
{
dictValue: 1,
dictLabel: '商户'
},
{
dictValue: 2,
dictLabel: '门店'
}
]
}
}
},
components: {
SelectTree
},
mounted() {
Promise.all([
baseApi.getAllDictApi({ dictType: 'product' }),
baseApi.getAllDictApi({ dictType: 'sort_value' }),
baseApi.getIcon({ fileType: 2 }),
baseApi.getAllDictApi({ dictType: 'owned_customer_group' }),
baseApi.getAllDictApi({ dictType: 'account_type' }),
baseApi.getAllDictApi({ dictType: 'account_level' })
]).then(([product, sort, icon, ownCus, account, accountLevel]) => {
this.baseData.productList = product.data
this.baseData.sortList = sort.data
this.baseData.iconList = icon.data
this.baseData.ownCusList = ownCus.data
this.baseData.accounTypetList = account.data
this.baseData.accountLevelList = accountLevel.data
this.getOptionsList('searchFormData')
this.getOptionsList('editFormData', false)
})
},
methods: {
openImportDialog() {
this.importDialogVisible = true;
},
closeDialog() {
this.importDialogVisible = false;
this.fileName = '';
this.selectedFile = null;
},
selectFile() {
this.$refs.fileInput.click();
},
handleFileChange(event) {
const file = event.target.files[0];
if (file) {
const validFileTypes = ['xlsx', 'xls']; // 允许的文件类型
const fileExt = file.name.split('.').pop().toLowerCase();
if (validFileTypes.includes(fileExt)) {
this.fileName = file.name;
this.selectedFile = file;
const reader = new FileReader();
reader.readAsDataURL(this.selectedFile);
reader.onload = (event) => {
const base64 = event.target.result.split(',')[1];
const fileType = this.selectedFile.name.split('.').pop();
const fileUrl = fileType + '@' + base64
uploadApi.multipleUpload({ file:fileUrl}).then(
(res) => {
console.log(res.status)
if (res.status == 200) {
this.importMenuUrl = res.data.data
}else{
this.$message.error('菜单导入失败,请重试');
}
},
(err) => {
this.$message.error(err);
}
);
};
} else {
this.$message.error('不支持的文件类型');
this.$refs.fileInput.value = ''; // 清除不正确的文件
}
}
},
importFile() {
if (!this.importMenuUrl) {
this.$message.error('请先选择文件');
return;
}
menuApi.buildVersionMenuFunctionNew({ url:this.importMenuUrl}).then(
(res) => {
console.log(res)
if (res.code == '0' && res.data ) {
this.$message.success('菜单导入成功');
this.closeDialog();
} else {
this.$message.error('菜单导入失败,请重试');
}
},
(err) => {
this.$message.error(err);
}
);
},
keyDowmGetList() {
this.$refs.menuTable.refreshTable()
},
getOptionsList(formName, hasAll = true) {
this[formName].forEach((item, index) => {
if (item.type === 'select') {
this.$set(
this[formName][index],
'optionList',
_.cloneDeep(this.baseData[item.optionsName])
)
if (
hasAll &&
this.baseData[item.optionsName].length &&
this.baseData[item.optionsName][0][
this[formName][index].optionLabel
] !== '全部'
) {
const temp = {}
temp[this[formName][index].optionId] = ''
temp[this[formName][index].optionLabel] = '全部'
this[formName][index].optionList.unshift(temp)
}
}
})
},
beforeSearchFn() {
return new Promise(resolve => {
this.$nextTick(() => {
this.tableSearchForm = _.cloneDeep(this.searchForm)
resolve(this.tableSearchForm)
})
})
},
getMenuTree(productId, fn) {
baseApi.getMenuTree({ productId: productId }).then(res => {
this.baseData.menuTreeList = res.data
this.baseData.menuTreeList.unshift({
id: ' ',
menuName: '无(表示顶级菜单)',
childlist: []
})
this.getOptionsList('editFormData', false)
fn && fn()
})
},
isMenuSpecial(isSpecial = true) {
if (!isSpecial) {
this.editForm.ownedCusgroupType = ''
this.editForm.ownedCusgroupValue = []
this.editForm.ownedMerchant = ''
}
this.baseData.cusGroupList = isSpecial
? _.cloneDeep(
this.baseData[this.attrToList[this.editForm.ownedCusgroupType]]
)
: []
if (
this.editForm.menuCode &&
isSpecial &&
this.editForm.ownedCusgroupType !== 3
) {
this.baseData.cusGroupList.forEach(item => {
this.editForm.ownedCusgroupValue.includes(item.dictValue) &&
(item.isDisabled = true)
})
}
this.getOptionsList('editFormData', false)
this.editFormData[
this.editFormData.findIndex(item => item.name === 'ownedCusgroupType')
].isHide = !isSpecial
this.editFormData[
this.editFormData.findIndex(item => item.name === 'ownedCusgroupValue')
].isHide =
!isSpecial || (isSpecial && this.editForm.ownedCusgroupType === 3)
this.editFormData[
this.editFormData.findIndex(item => item.name === 'ownedMerchant')
].isHide =
!isSpecial || (isSpecial && this.editForm.ownedCusgroupType !== 3)
this.editRule.ownedCusgroupValue[0].required =
isSpecial && this.editForm.ownedCusgroupType !== 3
this.editRule.ownedCusgroupType[0].required = isSpecial
},
editDisable() {
this.editFormData[
this.editFormData.findIndex(item => item.label === '产品')
].isDisabled = !!this.editForm.menuCode
this.editFormData[
this.editFormData.findIndex(item => item.label === '父级菜单')
].isDisabled = !!this.editForm.menuCode
this.editFormData[
this.editFormData.findIndex(item => item.name === 'isSpecial')
].isDisabled = !!this.editForm.menuCode
// this.editFormData[this.editFormData.findIndex(item => item.name === 'ownedCusgroupValue')].isDisabled = !!this.editForm.menuCode && !(this.editForm.ownedCusgroupType !== 3)
if (!this.editForm.menuCode) {
this.editFormData[
this.editFormData.findIndex(
item => item.name === 'ownedCusgroupValue'
)
].isHide = true
this.editFormData[
this.editFormData.findIndex(item => item.name === 'ownedMerchant')
].isHide = true
}
},
handleAdd(isEdit = false) {
this.editVisible = !this.editVisible
baseApi.getAllDictApi({ dictType: 'product' }).then(res => {
this.$set(this.baseData, 'productList', res.data)
this.getOptionsList('editFormData', false)
})
this.editFormData[
this.editFormData.findIndex(item => item.name === 'ownedCusgroupType')
].isDisabled = this.editForm.menuCode
this.editDisable()
!isEdit && this.isMenuSpecial(false)
this.$refs.editFormRef.$refs[this.editChildRef] &&
this.$refs.editFormRef.$refs[this.editChildRef].clearValidate()
},
handleConfirm() {
// console.log(this.editForm)
this.$refs.editFormRef.$refs[this.editChildRef].validate(valid => {
if (valid) {
var apiName = this.editForm.menuCode ? 'updateMenuApi' : 'saveMenuApi'
var params = _.cloneDeep(this.editForm)
this.editForm.ownedCusgroupValue &&
(params.ownedCusgroupValue = this.editForm.ownedCusgroupValue.join(
','
))
menuApi[apiName](params)
.then(res => {
// console.log(res)
if (res.code === '0') {
this.notice.okTips()
this.keyDowmGetList()
this.handleClose()
} else {
this.notice.errorTips(res.message)
}
})
.catch(errMsg => {
this.notice.errorTips(errMsg)
})
}
})
},
handleClose() {
this.editForm = {
isSpecial: 0,
parentName: '',
parentId: '',
ownedCusgroupType: '',
ownedCusgroupValue: [],
ownedMerchant: ''
}
var resetBaseNameList = ['menuTreeList', 'cusGroupList']
resetBaseNameList.forEach(item => {
this.baseData[item] = []
})
this.getOptionsList('editFormData', false)
this.$refs.editFormRef.$refs[this.editChildRef].clearValidate()
this.handleAdd()
},
handleEdit(row) {
this.editForm = _.cloneDeep(row)
if (!this.editForm.parentId) {
this.editForm.parentId = ' '
this.editForm.parentName = '无(表示顶级菜单)'
}
if (this.editForm.isSpecial) {
this.editForm.ownedCusgroupValue &&
(this.editForm.ownedCusgroupValue = this.editForm.ownedCusgroupValue
.split(',')
.map(item => +item))
this.isMenuSpecial(true)
}
this.getMenuTree(this.editForm.productId, () => {
this.handleAdd(true)
})
},
handleDelete(row) {
this.$confirm(
'删除菜单,会删除该菜单下的所有菜单及功能,请谨慎操作!',
'删除',
{
confirmButtonText: '确定删除',
cancelButtonText: '取消',
type: 'error'
}
)
.then(() => {
menuApi.deleteMenuApi({ id: row.id }).then(res => {
if (res.code === '0') {
this.notice.okTips()
this.keyDowmGetList()
}
})
})
.catch(() => {})
},
handleTreeSelect(obj) {
this[obj.formName][obj.targetItem.nameLabel] = obj.val
? obj.val.menuName
: ''
this[obj.formName][obj.targetItem.name] = obj.val ? obj.val.id : ''
},
reset() {
this.searchForm = {
productIds: [],
menuName: ''
}
this.$nextTick(() => {
this.$refs.menuTable.refreshTable()
})
}
}
}
</script>
<style scoped>
.file-upload-container .required-star {
color: red;
}
</style>