addOrEdit.vue
14.9 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
<template>
<div>
<GLDialog :title="dialogTitle" :visible.sync="groupVisible">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="grouping-ruleForm">
<el-form-item label="分群名称" prop="crowdGroupName" size="small">
<el-input v-model="ruleForm.crowdGroupName" placeholder="请输入分群名称" maxlength="30" show-word-limit></el-input>
</el-form-item>
<el-form-item label="分群描述" size="small">
<el-input v-model="ruleForm.crowdGroupDesc" placeholder="请输入分群描述" maxlength="100" show-word-limit></el-input>
</el-form-item>
<el-form-item label="概览是否展示" size="small">
<el-radio-group v-model="ruleForm.isDisplay">
<el-radio :label="1">是</el-radio>
<el-radio :label="2">否</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="选择展示位" size="small" v-show="ruleForm.isDisplay == 1">
<el-select v-model="ruleForm.displayLoc" placeholder="请选择">
<el-option v-for="item in 12" :key="item" :label="`${item}号展示位`" :value="item"></el-option>
</el-select>
</el-form-item>
<el-form-item label="选择人群" size="small">
<div class="group-tree-container">
<div class="group-tree-panel" :style="{ height: heightBox + 'px' }">
<template v-for="(item, index) in crowdUnitList">
<div :key="item" v-if="item.selectGroup && item.children && item.selectGroup.length != item.children.length">
<span class="el-tree-node__expand-icon el-icon-caret-right" :class="{ expanded: item.checked }" @click.self="handleExpand(index)"></span>
<el-checkbox :indeterminate="item.isIndeterminateGroup" v-model="item.gruopCheckAll" @change="handleGroup(item.gruopCheckAll, index)">{{ item.crowdName }}</el-checkbox>
<el-checkbox-group :style="{ 'max-height': item.checked ? heightBox - 70 + 'px' : '0px' }" v-model="item.groupArr" @change="handleCheckedGroup(item.groupArr, index)" v-show="item.checked">
<template v-for="crowd in item.children">
<el-checkbox v-if="isChecked(crowd, item)" :label="crowd" :key="crowd.crowdId">{{ crowd.crowdName }}</el-checkbox>
</template>
</el-checkbox-group>
</div>
</template>
</div>
<div class="group-tree-operation">
<div :class="[{ 'group-tree-btn': true, active: rightSelectNum }]" @click="handleRight"><i class="el-icon-arrow-right"></i></div>
<div :class="[{ 'group-tree-btn': true, active: leftSelectNum }]" @click="handleLeft"><i class="el-icon-arrow-left"></i></div>
</div>
<div class="group-tree-panel" :style="{ height: heightBox + 'px' }">
<template v-for="(item, index) in crowdUnitList">
<div :key="item" v-if="item.selectGroup && item.selectGroup.length > 0">
<span class="el-tree-node__expand-icon el-icon-caret-right" :class="{ expanded: item.checked_right }" @click.self="handleExpandChecked(index)"></span>
<el-checkbox :indeterminate="item.isIndeterminateGroup_right" v-model="item.gruopCheckAll_right" @change="handleGroupChecked(item.gruopCheckAll_right, index)">{{ item.crowdName }}</el-checkbox>
<el-checkbox-group :style="{ 'max-height': item.checked_right ? heightBox - 70 + 'px' : '0px' }" v-model="item.groupArr_right" @change="handleCheckedGroupChecked(item.groupArr_right, index)" v-show="item.checked_right">
<template v-for="crowd in item.children">
<el-checkbox v-if="!isChecked(crowd, item)" :label="crowd" :key="crowd.crowdId">{{ crowd.crowdName }}</el-checkbox>
</template>
</el-checkbox-group>
</div>
</template>
</div>
<div class="el-form-item__error" v-show="crowdStatus">请选择人群</div>
</div>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button plain size="small" class="btn-three" @click="groupVisible = false">取 消</el-button>
<el-button type="primary" size="small" @click="handleSelect">提 交</el-button>
</span>
</GLDialog>
</div>
</template>
<script>
export default {
name: 'addGrouping',
data() {
return {
heightBox: 0,
rightSelectNum: 0,
leftSelectNum: 0,
groupVisible: false,
crowdStatus: false,
dialogTitle: '新建分群',
ruleForm: {},
rules: {
crowdGroupName: [{ required: true, message: '请输入分群名称', trigger: 'blur' }]
},
crowdUnitList: [],
crowdUnitListReset: []
}
},
watch: {
crowdUnitList: {
handler(val) {
this.crowdStatus = false
},
deep: true
}
},
props: {
// dialogTitle: {
// type: String,
// default: '新建分群'
// }
},
mounted() {
let clientHeight = document.documentElement.clientHeight
this.heightBox = clientHeight - 520
this.getCrowdList()
},
methods: {
async getCrowdList() {
const commonParam = { pageSize: 100, pageNum: 1, merchantId: this.$globalState.session.merchantId }
const params1 = {
req: {
crowdType: 1,
...commonParam
}
}
const params2 = {
req: {
...commonParam,
crowdType: 2
}
}
const crowdUnitList1 = this.$apis.crowdManage.getCrowdList(params1)
const crowdUnitList2 = this.$apis.crowdManage.getCrowdList(params2)
const crowdUnitList = await Promise.all([crowdUnitList1, crowdUnitList2])
/*
groupArr: 勾选的人群
checked: 是否展开
isIndeterminateGroup:树节点选中
gruopCheckAll: 全选
selectGroup:右边树的选中数据
*/
this.crowdUnitList = [
{ crowdName: '推荐人群', children: crowdUnitList[0].data.crowdUnitList, groupArr: [], groupArr_right: [], checked: true, checked_right: true, isIndeterminateGroup: false, isIndeterminateGroup_right: false, gruopCheckAll: false, gruopCheckAll_right: false, selectGroup: [] },
{ crowdName: '自定义人群', children: crowdUnitList[1].data.crowdUnitList, groupArr: [], groupArr_right: [], checked: false, checked_right: false, isIndeterminateGroup: false, isIndeterminateGroup_right: false, gruopCheckAll: false, gruopCheckAll_right: false, selectGroup: [] }
]
this.crowdUnitListReset = this.$common.utils.deepClone(this.crowdUnitList)
},
//判断是否选中
isChecked(crowd, item) {
return !item.selectGroup.includes(crowd)
},
//推送右侧
handleRight() {
if (this.rightSelectNum == 0) {
return
}
this.crowdUnitList.forEach(group => {
group.selectGroup = group.selectGroup.concat(group.groupArr)
group.groupArr = []
group.gruopCheckAll = false
group.gruopCheckAll_right = false
if (group.groupArr_right.length) {
group.isIndeterminateGroup_right = true
}
group.isIndeterminateGroup = false
this.rightSelectNum = 0
})
this.autoExpand()
},
//推送左侧
handleLeft() {
if (this.leftSelectNum == 0) {
return
}
this.crowdUnitList.forEach(group => {
group.selectGroup = this.getReduceGroup(group.selectGroup, group.groupArr_right, 'crowdId')
group.groupArr_right = []
group.gruopCheckAll = false
group.gruopCheckAll_right = false
if (group.groupArr.length) {
group.isIndeterminateGroup = true
}
group.isIndeterminateGroup_right = false
this.leftSelectNum = 0
})
this.autoExpand()
},
handleSelect() {
this.$refs['ruleForm'].validate(valid => {
if (valid) {
let crowdArr = []
this.crowdUnitList.forEach(crowd => {
crowdArr = [...crowdArr, ...crowd.selectGroup]
})
if (crowdArr.length == 0) {
this.crowdStatus = true
return
}
const crowdIdList = []
crowdArr.forEach(arr => {
crowdIdList.push(arr.crowdId)
})
const params = {
req: {
merchantId: this.$globalState.session.merchantId,
crowdGroupType: 2,
crowdIdList
}
}
if (this.ruleForm.isDisplay == 2) {
delete this.ruleForm.displayLoc
}
Object.assign(params.req, this.ruleForm)
let crowdGroup
if (this.ruleForm.crowdGroupId) {
crowdGroup = 'editCrowdGroup'
} else {
crowdGroup = 'addCrowdGroup'
}
this.$apis.crowdManage[crowdGroup](params).then(res => {
if (res.data.code == 0) {
this.groupVisible = false
this.$message.success('操作成功')
this.$parent.searchTable()
} else {
// res.data.message
this.$message.error(res.data.message)
}
})
}
})
},
init(row) {
this.rightSelectNum = 0
this.leftSelectNum = 0
this.crowdUnitList = this.$common.utils.deepClone(this.crowdUnitListReset)
this.ruleForm = {
isDisplay: 1,
displayLoc: 1
}
if (row) {
this.ruleForm.crowdGroupId = row.crowdGroupId
this.dialogTitle = '编辑分群'
this.queryCrowdGroupDetailsByCrowdGroupId(row)
} else {
this.dialogTitle = '新建分群'
}
this.groupVisible = true
},
queryCrowdGroupDetailsByCrowdGroupId(row) {
const { crowdGroupName, crowdGroupDesc, crowdGroupId, crowdUnitList, isDisplay, displayLoc } = row
this.ruleForm = {
crowdGroupName,
crowdGroupDesc,
crowdGroupId,
displayLoc,
isDisplay
}
const arr1 = [],
arr2 = []
const crowdId1 = [],
crowdId2 = []
this.crowdUnitList[0].children.forEach(item => {
crowdId1.push(item.crowdId)
})
this.crowdUnitList[1].children.forEach(item => {
crowdId2.push(item.crowdId)
})
crowdUnitList.forEach(item => {
if (crowdId1.includes(item.crowdId)) {
arr1.push(item.crowdId)
}
if (crowdId2.includes(item.crowdId)) {
arr2.push(item.crowdId)
}
})
this.setTreeChecked(arr1, arr2)
},
setTreeChecked(arr1, arr2) {
this.crowdUnitList[0].children.forEach(item => {
if (arr1.includes(item.crowdId)) {
this.crowdUnitList[0].selectGroup.push(item)
}
})
this.crowdUnitList[1].children.forEach(item => {
if (arr2.includes(item.crowdId)) {
this.crowdUnitList[1].selectGroup.push(item)
}
})
this.autoExpand()
},
autoExpand() {
if (this.crowdUnitList[0].selectGroup.length == 0 && this.crowdUnitList[1].selectGroup.length > 0) {
this.crowdUnitList[0].checked_right = false
this.crowdUnitList[1].checked_right = true
}
if (this.crowdUnitList[0].selectGroup.length > 0 && this.crowdUnitList[1].selectGroup.length == 0) {
this.crowdUnitList[0].checked = false
this.crowdUnitList[1].checked = true
}
},
//左侧展开
handleExpand(index) {
this.crowdUnitList.forEach((item, idx) => {
if (idx == index) {
item.checked = !item.checked
} else {
item.checked = false
}
})
},
//右侧展开
handleExpandChecked(index) {
this.crowdUnitList.forEach((item, idx) => {
if (idx == index) {
item.checked_right = !item.checked_right
} else {
item.checked_right = false
}
})
},
getReduceGroup(arr1, arr2, identification) {
const selectGroupId = []
arr2.forEach(group => {
selectGroupId.push(group[identification])
})
return arr1.filter(child => !selectGroupId.includes(child[identification]))
},
//左侧全选
handleGroup(val, index) {
const surplusArr = this.getReduceGroup(this.crowdUnitList[index].children, this.crowdUnitList[index].selectGroup, 'crowdId')
this.crowdUnitList[index].groupArr = val ? surplusArr : []
this.crowdUnitList[index].isIndeterminateGroup = false
this.rightShow()
},
//左侧单选
handleCheckedGroup(value, index) {
const surplusArr = this.getReduceGroup(this.crowdUnitList[index].children, this.crowdUnitList[index].selectGroup, 'crowdId')
let checkedCount = value.length
this.crowdUnitList[index].gruopCheckAll = checkedCount === surplusArr.length
this.crowdUnitList[index].isIndeterminateGroup = checkedCount > 0 && checkedCount < surplusArr.length
this.rightShow()
},
//按钮判断
rightShow() {
let selectNum = 0
this.crowdUnitList.forEach(item => {
selectNum += item.groupArr.length
})
this.rightSelectNum = selectNum
},
//按钮判断
leftShow() {
let selectNum = 0
this.crowdUnitList.forEach(item => {
selectNum += item.groupArr_right.length
})
this.leftSelectNum = selectNum
},
//右侧全选
handleGroupChecked(val, index) {
this.crowdUnitList[index].groupArr_right = val ? this.crowdUnitList[index].selectGroup : []
this.crowdUnitList[index].isIndeterminateGroup_right = false
this.leftShow()
},
//右侧全选
handleCheckedGroupChecked(value, index) {
let checkedCount = value.length
this.crowdUnitList[index].gruopCheckAll_right = checkedCount === this.crowdUnitList[index].selectGroup.length
this.crowdUnitList[index].isIndeterminateGroup_right = checkedCount > 0 && checkedCount < this.crowdUnitList[index].selectGroup.length
this.leftShow()
}
}
}
</script>
<style lang="scss" scoped>
.group-tree-container {
display: flex;
.group-tree-operation {
width: 32px;
margin: 0px 16px;
display: flex;
flex-direction: column;
justify-content: center;
.group-tree-btn {
width: 32px;
height: 32px;
background: #f7f7f7;
border-radius: 4px;
border: 1px solid #dcdfe6;
text-align: center;
line-height: 32px;
cursor: pointer;
&.active {
background: #5850ff;
color: #ffffff;
}
&:first-child {
margin-bottom: 16px;
}
}
}
.group-tree-panel {
width: 50%;
border-radius: 2px;
padding: 8px;
border: 1px solid #eeeeee;
.el-tree-node__expand-icon {
font-size: 16px;
color: #000000;
}
/deep/ {
.el-checkbox-group {
margin-left: 30px;
overflow-y: auto;
animation: all ease-in 0.5s;
.el-checkbox {
display: block;
}
}
}
}
}
</style>