import Magix from 'magix' import * as View from '../../../view' // const Helper = require('dmp-new/views/mixins/helper') import * as Helper from '../../mixins/helper' import GetApi from './getApi' Magix.applyStyle('@./create-crowd.less') const EMPTY_COVERAGE = '--' export default View.extend({ tmpl: '@create-crowd.html', mixins: [GetApi], init(options) { this.options = options }, async render() { const { bizCode, crowd, permissions } = this.options this.updater.set({ bizCode, isShowRecommend: true // 默认展开标签组合推荐 }) const groups = await this.getTagGroups() const groupsTree = Helper.parseToTree(groups) this.sumTagsByGroup(groupsTree) // const tagGroups = groupsTree[0] // 根据权限dmpTagSortScene 来决定顶层的基础属性标签是否置后 let tagGroupsSort = [] if (permissions.includes('dmpTagSortScene')) { tagGroupsSort = [ { type: 'sub', // 子结构的标签分组 children: tagGroups.children }, { type: 'top', // 顶层结构的基础属性 tags: tagGroups.tags } ] // 子标签分组排在前面的时候,第一个分组默认打开 // 并且递归找到第一个标签打开 if (tagGroups.children[0]) { tagGroups.children[0].isExpand = true if (!tagGroups.children[0].curTagId) { this.recurGetFirstTag(tagGroups.children[0]) } } } else { tagGroupsSort = [ { type: 'top', tags: tagGroups.tags }, { type: 'sub', children: tagGroups.children } ] } this.updater.set({ tagGroupsSort, permissions, EMPTY_COVERAGE, countBaseTags: () => { const { tagGroups } = this.updater.get() const selectTagOptionSetDTO = this.changeGroupsToSet(tagGroups) return selectTagOptionSetDTO._selectedTagCounts || 0 }, acceptCallback: data => { this.acceptRecommend(data) }, crowd, selectTagOptionSetDTO: (crowd && crowd.selectTagOptionSetDTO) || {}, tagGroups, showAll: false, showMax: 100 // 基础属性最多显示6个,其他先隐藏 }) // 编辑时有crowd if (crowd) { await this.setTagOptions() } this.updater.digest() }, 'showHideRecommend'(e) { this.updater.digest({ isShowRecommend: !this.updater.get('isShowRecommend') }) }, // 编辑或有推荐标签时,选中那些选项值 async setTagOptions() { const { tagGroups } = this.updater.get() // 先获取所有有选项的标签的详情信息,需要里面的optionGroup.ext信息 const tagIdMap = await this.getSelectTagInfo() this.updater.set({ tagIdMap }) // 编辑人群时,选中选项 this.setTagSelected() // tagGroups下每个分组group都挂上递归算好的selected this.setGroupSelected(tagGroups) // 获取覆盖数 this.getCoverage() }, // 先获取所有有选项的标签的详情信息,获取里面的optionGroup.ext信息 async getSelectTagInfo() { const tagIds = [] this.recurSelectedTag(set => { tagIds.push(set.selects[0].tagId) }) const models = [] for (const tagId of tagIds) { models.push(this.getTag(tagId, this.options.bizCode)) } const results = await Promise.all(models) const tagIdMap = Magix.toMap(results, 'id') return tagIdMap }, // 遍历selectTagOptionSetDTO,找到所有有选项的标签 recurSelectedTag(callback) { const { selectTagOptionSetDTO } = this.updater.get() function recur(sets) { for (const set of sets) { if (set.selects && set.selects.length) { // 有选项的标签 // let tagId = set.selects[0].tagId callback(set) // tagIds.push(tagId) } if (set.selectTagOptionSetDTO) { recur(set.selectTagOptionSetDTO) } } } recur([selectTagOptionSetDTO]) }, // 递归查找所有selectTagOptionSetDTO下有选项的标签 setTagSelected() { this.recurSelectedTag(set => { const tagId = set.selects[0].tagId this.setTagGroupSelected(tagId, set.selects) }) }, // 有选项的标签对应查找tagGrups下的tags,附上optionGroups.selected值 setTagGroupSelected(tagId, selects) { const { tagGroups, tagIdMap } = this.updater.get() let isFound = false function recur(groups) { if (isFound) { return } for (const group of groups) { if (group.tags) { for (const tag of group.tags) { if (tag.id == tagId) { // 从tagIdMap里拿optionGroups,需要里面的ext信息 tag.optionGroups = (tagIdMap[tagId] && tagIdMap[tagId].optionGroups) || [] // tag.optionGroups = [] for (const select of selects) { const names = select.names ? select.names.split(',') : [] for (const optionGroup of tag.optionGroups) { if (optionGroup.id == select.optionGroupId) { optionGroup.selected = { names, values: select.values.split(',') } } } // tag.optionGroups.push({ // selected: { // names, // values: select.values.split(',') // }, // id: select.optionGroupId // }) } tag.tagSelected = { selects } isFound = true } } } // if (group.children) { recur(group.children) } } } recur([tagGroups]) }, // 计算下每个分组下的标签总数 sumTagsByGroup(groupsTree) { function recur(_groupsTree) { for (const group of _groupsTree) { let total = 0 if (group.tags) { total += group.tags.length } function _recur(groups) { for (const _group of groups) { if (_group.tags) { total += _group.tags.length } if (_group.children) { _recur(_group.children) } } } if (group.children) { _recur(group.children) } group.tagsCount = total if (group.children) { recur(group.children) } } } recur(groupsTree) }, // 标签选项选中后触发的操作 emitSelected(tag, selectCheck) { const { tagGroups } = this.updater.get() // 根据标签ID递归查tagGroups树下的某个标签,并将标签覆盖 this.setTag(tag, selectCheck, tagGroups) // tagGroups下每个分组group都挂上递归算好的selected this.setGroupSelected(tagGroups) this.updater.digest({ tagGroups }) // 获取人群覆盖数 this.getCoverage() }, // 获取人群覆盖数 async getCoverage() { const { tagGroups } = this.updater.get() const selectTagOptionSetDTO = this.changeGroupsToSet(tagGroups) this.updater.set({ selectTagOptionSetDTO }) // const parentView = this.owner.parent().$v const { scaleType } = parentView.updater.get() await parentView.getCoverage() if (scaleType == 2) { await parentView.getCoverageAdjust() } }, // 将tagGroups分组形式的标签转换成标准selectTagOptionSetDTO格式 changeGroupsToSet(tagGroups) { const tagSet = {} let _selectedTagCounts = 0 // 总共选了多少个标签 function recur(group, _tagSet) { const selectTagOptionSetDTO = [] if (group.tags) { for (const tag of group.tags) { // tag.selectType 标识当前标签是否选择了不限 // 1: 不限,2:自定义 if (tag.optionGroups && tag.selectType != '1') { _selectedTagCounts++ const selects = [] for (const optionGroup of tag.optionGroups) { if (optionGroup.selected) { selects.push({ tagId: tag.id, tagName: tag.tagName, optionGroupId: optionGroup.id, values: optionGroup.selected.values.join(','), names: optionGroup.selected.names.join(',') }) } } selectTagOptionSetDTO.push({ selects }) } // else { // selectTagOptionSetDTO.push({ // selects: [] // }) // } } } if (group.children) { for (const child of group.children) { const subTagSet = { operator: child.operator } selectTagOptionSetDTO.push(subTagSet) recur(child, subTagSet) } } _tagSet.operator = group.operator _tagSet.selectTagOptionSetDTO = selectTagOptionSetDTO } recur(tagGroups, tagSet) tagSet._selectedTagCounts = _selectedTagCounts return tagSet }, // 根据标签ID递归查tagGroups树下的某个标签 setTag(tag, selectCheck, tagGroups) { let isMatch = false function recur(groups) { for (const group of groups) { if (group.tags) { for (const _tag of group.tags) { if (_tag.id == tag.id) { // 覆盖上tag完整信息 const tagName = _tag.tagName $.extend(_tag, tag) _tag.tagName = tagName if (!selectCheck) { // 未全选的话删除掉optionGroups,代表该标签未选择 delete _tag.optionGroups } isMatch = true } } } if (group.children && !isMatch) { recur(group.children) } } } recur([tagGroups]) return tagGroups }, // tagGroups下每个分组group都挂上递归算好的selected setGroupSelected(tagGroups) { if (tagGroups.children) { for (const group of tagGroups.children) { const selected = [] // 分组下所有已选择选项的标签 function recur(groups) { for (const _group of groups) { if (_group.tags) { for (const tag of _group.tags) { // 有optionGroups代表该标签有选项了 if (tag.optionGroups) { const _optionNames = [] for (const g of tag.optionGroups) { // if (g.ext) { try { g.ext = JSON.parse(g.ext) } catch (error) {} } if (!g.ext || !g.ext.hideInFront) { // hideInFront的分组前端不显示,但是需要传给后端 _optionNames.push( (g.selected && g.selected.names.join(',')) || '' ) } } const optionNames = _optionNames.join('-') const names = `${tag.tagName}-${optionNames}` // 超过字符省略号 const maxLength = 15 let ellipsisNames if (names.length > maxLength) { ellipsisNames = names.slice(0, maxLength) + '...' } const obj = { id: tag.id, names, ellipsisNames: undefined } if (ellipsisNames) { obj.ellipsisNames = ellipsisNames } selected.push(obj) } } } if (_group.children) { recur(_group.children) } } } recur([group]) group.selected = selected } } return tagGroups }, // 获取标签分组 async getTagGroups() { const { bizCode } = this.updater.get() try { const models = [ { name: 'dmp-advgateway_api_dmp_tag_group_list_get', params: { bizCode, groupType: 1, needTags: true } } ] const [model] = await this.fetch(models) const data = model.get('data.result', []) for (const group of data) { try { group.ext = JSON.parse(group.ext) || {} } catch (error) { group.ext = {} } } return data } catch (error) { this.alert('错误提示', error.msg || error.message || error, null, { mask: true }) } }, // 移除某个标签 'removeTag'(e) { const { tagGroups } = this.updater.get() const { tag, group } = e.params const { id } = tag let foundTag = false function recur(groups) { if (foundTag) return for (const _group of groups) { if (_group.tags) { for (const tag of _group.tags) { if (tag.id == id) { delete tag.optionGroups foundTag = true break } } } if (_group.children) { recur(_group.children) } } } recur([group]) // 更新下标签选项的选中或取消状态 this.refreshTagSelect(id) // 重新计算下group.selected this.setGroupSelected(tagGroups) this.updater.digest({ tagGroups }) // 获取人群覆盖数 this.getCoverage() }, // 更新下标签选项的选中或取消状态 async refreshTagSelect(tagId) { const tagVframe = await this.waitVframeCreated( Magix.Vframe.get(`${this.id}_${tagId}`) ) if (tagVframe) { delete tagVframe.$v.options.tagSelected tagVframe.$v.render() } }, // 子view(tag-select)调用的用来获取已选择的标签的选项 getTagSelects(logicMode, index, tagId) { const { tagGroups } = this.updater.get() let selects if (!tagGroups) { return } const curTag = this.getCurTag(tagGroups, tagId) // 递归找到当前tagId对应的tag信息,拿里面的optionGroups来转换成options.tagSelected if (curTag?.optionGroups?.length) { selects = [] for (const group of curTag.optionGroups) { if (group.selected) { selects.push({ tagId, tagName: curTag.tagName, names: group.selected.names.join(','), values: group.selected.values.join(','), optionGroupId: group.id }) } } } return selects }, // 递归找到当前tagId对应的tag信息,拿里面的optionGroups来转换成options.tagSelected getCurTag(tagGroups, tagId) { let foundTag function recur(groups) { for (const group of groups) { if (group.tags) { for (const tag of group.tags) { if (tag.id == tagId) { foundTag = tag } } } if (group.children && !foundTag) { recur(group.children) } } } recur([tagGroups]) return foundTag }, // 清除某分组下所有已选择的标签 'clearTagSet'(e) { const { group } = e.params const { tagGroups } = this.updater.get() // group.isExpand = false function recur(groups) { for (const _group of groups) { if (_group.tags) { for (const tag of _group.tags) { delete tag.optionGroups } } if (_group.children) { recur(_group.children) } } } recur([group]) // 更新下所有标签选项的选中或取消状态 for (const selected of group.selected) { this.refreshTagSelect(selected.id) } // 重新计算下group.selected this.setGroupSelected(tagGroups) this.updater.digest({ tagGroups }) // 获取人群覆盖数 this.getCoverage() }, // 展开/收起标签分组下的标签设置 'expandGroup'(e) { const { group } = e.params const { tagGroups } = this.updater.get() // 同级全部收起 for (const _group of group._parent.children) { if (_group !== group) { _group.isExpand = false } } group.isExpand = !group.isExpand // 未选择标签时,默认选中第一个标签打开(递归寻找) if (group.isExpand && !group.curTagId) { this.recurGetFirstTag(group) } this.updater.digest({ tagGroups }) }, // 未选择标签时,默认选中第一个标签打开(递归寻找) recurGetFirstTag(group) { function recur(groups) { if (group.curTagId) { return } for (const _group of groups) { if (_group.tags && _group.tags.length) { group.curTagId = _group.tags[0].id if (_group !== group) { _group.isExpand = true } // 父级所有isExpand设为true打开状态 let curGroup = _group while (curGroup._parent && curGroup !== group) { curGroup._parent.isExpand = true curGroup = curGroup._parent } break } else { if (_group.children) { recur(_group.children) } } } } recur([group]) }, // 选择标签 'selectTag'(e) { const { group, tag } = e.params const { tagGroups } = this.updater.get() group.curTagId = tag.id this.updater.digest({ tagGroups }) }, // 显示/收起子分组 'showHideGroup'(e) { const { tagGroups } = this.updater.get() const { group } = e.params // 同级全部收起 for (const _group of group._parent.children) { if (_group !== group) { _group.isExpand = false } } // 当前的展开或收起 group.isExpand = !group.isExpand this.updater.digest({ tagGroups }) }, // 展开收起更多选项 'showMore'(e) { const { showAll } = this.updater.get() this.updater.digest({ showAll: !showAll }) }, // 使用优质人群快捷圈选 'useSpeedyCrowd'(e) { const viewPath = '@./speedy-crowds' const viewOptions = { bizCode: this.options.bizCode, callback: async data => { this.acceptRecommend(data) } } const dialogOptions = { width: 890, header: { title: '优质人群快速圈选' }, footer: { enter: false, cancelText: '关闭' } } this.mxModal(viewPath, viewOptions, dialogOptions) }, // 采纳推荐的标签组合 async acceptRecommend(data) { const parentView = this.owner.parent().$v parentView.updater.set({ combineRecommendIds: [data.combinationKeyId.toString()] }) // 之前选中过的标签选项清空下 const { tagGroups } = this.updater.get() this.recurTagGroups([tagGroups], tag => { const tagVframe = Magix.Vframe.get(`${this.id}_${tag.id}`) if (tagVframe) { delete tagVframe.$v.options.tagSelected } delete tag.optionGroups delete tag.tagSelected }) // 设置选中项的标签 this.updater.set({ selectTagOptionSetDTO: data.selectTagOptionSetDTO, source: 'tag_recommend_bp' // 推荐打个标识 }) await this.setTagOptions() this.updater.digest() // 所有标签重新mount一下 this.recurTagGroups([tagGroups], tag => { const tagVframe = Magix.Vframe.get(`${this.id}_${tag.id}`) if (tagVframe) { tagVframe.invoke('render') } }) }, // 递归查找tagGroups下所有的tags recurTagGroups(tagGroups, cb) { function recur(groups) { for (const group of groups) { if (group.tags && group.tags.length) { for (const tag of group.tags) { cb(tag) } } if (group.children && group.children.length) { recur(group.children) } } } recur(tagGroups) } })