const Magix = require('magix') const View = require('dmp-new/view') Magix.applyStyle('@./create-logic.less') const RemoveEmptySet = require('@../../mixins/removeEmptySet') const symbols = [{ name: '并集', symbol: '+' }, { name: '交集', symbol: 'x' }, { name: '差集', symbol: '-' }, { name: '左括号', symbol: '(' }, { name: '右括号', symbol: ')' }] const symbolsMap = { '+': 'UNION', '-': 'MINUS', x: 'INTERSECT', '(': 'OPEN_PAREN', ')': 'CLOSE_PAREN' } const MaxTagsCount = 10 // 可选标签上限 const MaxCrowdsCount = 3 // 可选人群上限 module.exports = View.extend({ tmpl: '@create-logic.html', mixins: [RemoveEmptySet], init (options) { this.options = options }, async render () { const { all, user, Dayjs } = this.updater.get() const loc = Magix.Router.parse() const minDate = Dayjs().format('YYYY-MM-DD') const defaultDate = Dayjs().add(30, 'days').format('YYYY-MM-DD') const maxDate = Dayjs().add(1, 'years').format('YYYY-MM-DD') // 是否固化人群 - 小二版 const solidates = [{ name: '否', value: '0' }, { name: '是', value: '1', tips: '选择固化人群后,该人群不会进行每日更新' }] const isCombine = loc.get('isCombine') === 'true' this.updater.set({ solidates, isCombine }) const params = { coverage: 0, selectTagOptionSet: {}, extra: { tagCombination: '' // 标签组合表达式,数组格式,json化 }, groupId: 0, crowdName: '', validDate: defaultDate } // 小二版并且不是人群组合才有固化人群选项 if (user.isXiaoer || user.permissions.includes('CROWD_SOLIDATE')) { params.extra.solidate = solidates[0].value } // 标签 let tagCombination = [] let tagList = [] // 人群 let crowdList = [] if (loc.get('id')) { const crowd = this.options.crowd $.extend(true, params, crowd) if (loc.get('isCopy')) { params.crowdName = params.crowdName + '_副本' } try { tagCombination = JSON.parse(params.extra.tagCombination) } catch (error) { tagCombination = params.extra.tagCombination || [] } try { tagList = JSON.parse(params.extra.tagList) } catch (error) { tagList = params.extra.tagList || [] } try { crowdList = JSON.parse(params.extra.crowdList) } catch (error) { crowdList = params.extra.crowdList || [] } } // 应用场景 const scenes = all.scenes // 应用场景extra.APPLICATION_SCENE默认值:0 if (!params.extra.APPLICATION_SCENE) { params.extra.APPLICATION_SCENE = '0' } this.updater.digest({ id: loc.get('id'), scenes, crowdList, MaxTagsCount, MaxCrowdsCount, min: minDate, max: maxDate, isCopy: loc.get('isCopy'), coverageLoading: false, params, formulaLegal: true, symbols, // 符号 tagList, // 已添加的标签列表 tagCombination // 标签组合表达式,数组格式 }) // this.checkTagCombination() }, // 加速人群,轮询查询加速结果 async accelerateCrowd (crowdId) { const me = this function polling () { return new Promise(async (resolve) => { try { const models = [{ name: 'api_crowd_accelerate_task_post', params: { bizId: 2, crowdId }, isJson: true }] const [model] = await me.fetch(models) const data = model.get('data') if (data == 1) { // 加速中 setTimeout(async () => { const { status } = await polling() if (status == 1) { await polling() } else { resolve({ status }) } }, 2000) } else { // 成功&失败 resolve({ status: data }) } // return data } catch (error) { resolve({ status: 3, error: error.msg || error.message }) } }) } const _result = await polling() return _result }, // 符号拖拽 'symbolDragstart' (e) { const symbols = this.updater.get('symbols') const symbol = symbols[e.params.index] const dropData = { type: 'symbol', data: symbol } e.originalEvent.dataTransfer.setData('dropData', JSON.stringify(dropData)) }, // 标签组合里的标签或符号的拖拽 'combDragstart' (e) { this._dropDataIndex = e.params.index // 标识拖拽目标的index const tagCombination = this.updater.get('tagCombination') const dropData = tagCombination[e.params.index] dropData.index = e.params.index e.originalEvent.dataTransfer.setData('dropData', JSON.stringify(dropData)) }, 'tagWrapDragover' (e) { e.preventDefault() }, // 标签组合框的经过效果 'tagWrapDragenter' (e) { e.preventDefault() this.updater.digest({ isTagWrapDragenter: true }) }, // 标签组合框的离开效果 'tagWrapDragleave' (e) { e.preventDefault() const flag = Magix.inside(e.relatedTarget, e.eventTarget) if (!flag) { this.updater.digest({ isTagWrapDragenter: false }) } }, getPureTag (tag) { const _tag = {} const whiteKeys = ['id', 'tagName', 'optionGroups', '_isSampled', '_memberId', '_coverage'] for (const key of whiteKeys) { _tag[key] = tag[key] } return _tag }, // 模块放置操作 async 'tagWrapDrop' (e) { e.preventDefault() e.stopPropagation() this._dropDataIndex = undefined // 拖放到了框里,取消_dropDataIndex const dropData = JSON.parse(e.originalEvent.dataTransfer.getData('dropData')) const tagCombination = this.updater.get('tagCombination') const COMBINE_CROWD_MAX = 3 const COMBINE_TAG_MAX = 15 // 人群拖拽提示 if (dropData.type === 'crowd') { const crowdsCombination = tagCombination.filter(item => item.type === 'crowd') if (crowdsCombination.length >= COMBINE_CROWD_MAX) { this.updater.digest({ isTagWrapDragenter: false }) return this.alert('错误提示', `一个新组合人群最多可添加${COMBINE_CROWD_MAX}个可组合人群。`, null, { mask: true }) } dropData.data._accelerateStatus = 1 // 加速中 this.accelerateCrowd(dropData.data.crowdId).then(({ error, status }) => { dropData.data._accelerateStatus = status if (error) { dropData.data._accelerateError = error } this.updater.digest({ tagCombination }) this.checkTagCombination() }) } else if (dropData.type === 'tag') { const tagsCombination = tagCombination.filter(item => item.type === 'tag') if (tagsCombination.length >= COMBINE_TAG_MAX) { this.updater.digest({ isTagWrapDragenter: false }) return this.alert('错误提示', `一个新组合人群最多可添加${COMBINE_TAG_MAX}个标签。`, null, { mask: true }) } } // let tagCombination = this.updater.get('tagCombination') const dragIndex = dropData.index // 拖动的元素的index const index = e.params.index // 放置元素的index dropData.active = false // tag标签数据精简下 if (dropData.type === 'tag') { if (dragIndex === undefined) { // 已经选择过选项的标签在逻辑框里拖动时不弹出标签浮层 // 判断依据是有没有index const selectedTag = await this.selectTag(dropData.data) if (!selectedTag) { return this.updater.digest({ isTagWrapDragenter: false }) } dropData.data = selectedTag } dropData.data = this.getPureTag(dropData.data) } if (dragIndex !== undefined) { tagCombination.splice(dragIndex, 1) const _index = dragIndex >= index ? index : index - 1 if (index !== undefined) { tagCombination[_index] && (tagCombination[_index].active = false) tagCombination.splice(_index, 0, dropData) } } else { if (index !== undefined) { tagCombination[index] && (tagCombination[index].active = false) tagCombination.splice(index, 0, dropData) } } if (index === undefined) { tagCombination.push(dropData) } this.updater.digest({ tagCombination, isTagWrapDragenter: false, isOverItem: false }) // check this.checkTagCombination() }, // 拖拽一个标签弹出浮层选择选项 selectTag (tag) { const tagId = tag.id return new Promise((resolve, reject) => { this.mxDialog('@../crowds-bp/tag-select', { isMarket: true, isCustom: true, // 标识是从自定义圈里的逻辑圈人里的标签浮层 tagId, callback: (selects) => { // 调整数据格式 const optionGroups = selects.map(select => { return { id: select.optionGroupId, tagId: tag.id, selectedOptions: select.selectedOptions, selected: { names: select.names.split(','), values: select.values.split(',') } } }) const selectedTag = { id: tag.id, tagName: tag.tagName, optionGroups } resolve(selectedTag) }, cancelCallback: () => { resolve() } }, { width: 800, top: 100 }) }) }, // 标签组合框里的标签或符号的经过效果 'combDragenter' (e) { e.preventDefault() const tagCombination = this.updater.get('tagCombination') const curr = tagCombination[e.params.index] curr.active = true this.updater.digest({ isOverItem: true, tagCombination }) }, // 标签组合框里的标签或符号的离开效果 'combDragleave' (e) { e.preventDefault() const flag = Magix.inside(e.relatedTarget, e.eventTarget) if (!flag) { const tagCombination = this.updater.get('tagCombination') const curr = tagCombination[e.params.index] curr.active = false this.updater.digest({ tagCombination, isOverItem: false }) } }, // 标签组合框里的标签或符号拖拽结束 'combDragend' (e) { const tagCombination = this.updater.get('tagCombination') if (this._dropDataIndex !== undefined) { // 拖到了框外面等于删除 tagCombination.splice(this._dropDataIndex, 1) this.updater.digest({ tagCombination }) this.checkTagCombination() } }, // 移除标签或人群 'remove' (e) { const { index } = e.params const { tagCombination } = this.updater.get() tagCombination.splice(index, 1) this.updater.digest({ tagCombination }) this.checkTagCombination() }, // 编辑标签 'edit' (e) { const { tagCombination } = this.updater.get() const { comb } = e.params const selects = [] const tagId = comb.data.id const tagName = comb.data.tagName for (const group of comb.data.optionGroups) { selects.push({ tagId, optionGroupId: group.id, names: group.selected.names.join(','), values: group.selected.values.join(','), tagName: comb.data.tagName, selectedOptions: group.selectedOptions }) } const tagSelected = { selects } this.mxDialog('@../crowds-bp/tag-select', { tagSelected, isMarket: true, isCustom: true, // 标识是从自定义圈里的逻辑圈人里的标签浮层 tagId, callback: (selects) => { // 调整数据格式 const optionGroups = selects.map(select => { return { id: select.optionGroupId, tagId, selected: { names: select.names.split(','), values: select.values.split(',') }, selectedOptions: select.selectedOptions } }) const selectedTag = { id: tagId, tagName, optionGroups } comb.data = selectedTag this.updater.digest({ tagCombination }) this.checkTagCombination() } }, { width: 800, top: 100 }) }, // 检测标签组合是否合法 async checkTagCombination (e) { const { tagCombination, params } = this.updater.get() const _tagCombination = [] for (const comb of tagCombination) { // 标签 if (comb.type === 'tag') { const selects = [] for (const group of comb.data.optionGroups) { if (!group._hide) { selects.push({ optionGroupId: group.id, names: group.selected.names.join(','), values: group.selected.values.join(','), tagId: group.tagId }) } } _tagCombination.push({ selects, type: comb.type, symbol: null }) } // 人群 else if (comb.type === 'crowd') { // _accelerateStatus = 2,加速完毕的人群才能用 if (comb.data._accelerateStatus == 2) { // const selects = [{ tagId: 147278, // 人群id固定存为一个标签的values optionGroupId: 30287, values: comb.data.crowdId.toString(), names: comb.data.crowdName }] _tagCombination.push({ selects, type: 'tag', symbol: null }) } } // 其它操作符 else { _tagCombination.push({ selects: null, type: comb.type, symbol: symbolsMap[comb.data.symbol] }) } } if (!_tagCombination.length) { return } try { const models = [{ name: 'api_tag_expression_translate_post', params: { tagExpression: _tagCombination }, isJson: true }] const [model] = await this.fetch(models) const checkResult = model.get('data') params.selectTagOptionSet = checkResult this.updater.digest({ params, formulaLegal: !!checkResult, formulaLegalMsg: '标签表达式不合法,请检查标签/运算符/括号是否匹配且完整' }) } catch (error) { params.selectTagOptionSet = {} this.updater.digest({ formulaLegal: false, formulaLegalMsg: error.msg }) } const crowdExtraVframe = await this.waitVframeCreated(Magix.Vframe.get(`${this.owner.parent().id}_crowdCreate`)) crowdExtraVframe.invoke('updateCoverage') }, 'cancel' (e) { window.history.back() } })