import Magix from 'magix' import * as View from '../../../view' import Mixins from './mixins' import GetApi from './getApi' Magix.applyStyle('@./option-recommend.less') export default View.extend({ mixins: [Mixins, GetApi], tmpl: '@option-recommend.html', init (options) { this.options = options }, async render () { const { tagName, optionName, selectTagOptionSetDTO } = this.options // 权限 const permissions = Magix.config('bpCustomCrowdPermissions') const { recommends, selected } = await this.getRecommends() const adPredict = await this.getAdPredict() this.updater.digest({ selects: selectTagOptionSetDTO.selectTagOptionSetDTO[0], permissions, tagName, optionName, recommends, selected, adPredict }) }, // 递归selectTagOptionSetDTO recurSelectTagOptionSet (selectTagOptionSetDTO, cb) { function recur (sets) { for (const tagSet of sets) { if (tagSet.selects && tagSet.selects.length) { cb(tagSet.selects) } if (tagSet.selectTagOptionSetDTO && tagSet.selectTagOptionSetDTO.length) { recur(tagSet.selectTagOptionSetDTO) } } } recur(selectTagOptionSetDTO) }, // 获取推荐 async getRecommends () { try { const { bizCode, selectTagOptionSetDTO, queryBizId } = this.options const params = { bizCode, selectTagOptionSetDTO, queryBizId } const models = [{ name: 'appone_api_dmp_tag_recommend_list_post', params }] const [model] = await this.fetch(models) const data = model.get('data.list', []) // 已勾选的推荐选项 const selected = [] // 从selectTagOptionSetDTO里取出拼合成显示的标签名+选项值 for (const item of data) { let tagName = '' const optionNames = [] let isMatch = true let tagId this.recurSelectTagOptionSet([item.selectTagOptionSetDTO], selects => { // 拼名称 tagName = selects[0].tagName tagId = selects[0].tagId for (const select of selects) { optionNames.push(select.names) } // 从create-crowd的view里的tagGroups找出匹配到已选中的推荐标签,勾上 const createCrowdView = this.owner.parent().parent().parent().$v const { tagGroups } = createCrowdView.updater.get() this.recurTagGroups([tagGroups], tag => { if (tag.id == tagId) { if (tag.optionGroups && tag.optionGroups.length) { for (const group of tag.optionGroups) { for (const select of selects) { if (select.optionGroupId == group.id) { if (select.values !== group.selected.values.join(',')) { isMatch = false break } } } } } else { isMatch = false } return false } }) if (isMatch) { selected.push(tagId) } item._showName = `${tagName}:${optionNames.join(',')}` item.tagId = tagId }) } return { recommends: data, selected } } catch (error) { this.alert('错误提示', error.msg || error.message || error, null, { mask: true }) } }, // 获取广告预估 async getAdPredict () { try { const { bizCode, selectTagOptionSetDTO } = this.options const currentApp = this.getCodeByWebBizCode(bizCode) const params = { bizCode, selectTagOptionSetDTO, adPredictQuery: { queryType: 2, deliverType: currentApp.deliverId, cateId: '' } } const models = [{ name: 'appone_api_dmp_analysis_adpredict_post', params }] const [model] = await this.fetch(models) const data = model.get('data.adPredict', {}) return data } catch (error) { this.alert('错误提示', error.msg || error.message || error, null, { mask: true }) } }, // 选择推荐标签 'selectRecommend' (e) { const { recommend } = e.params const { tagViewId, bizCode } = this.options const combinationKeyId = recommend.combinationKeyId.toString() // 关联推荐id可以有多个,去重 // let combinationKeyId = recommend.combinationKeyId.toString() const customCrowdView = this.owner.parent().parent().parent().parent().$v const { relateRecommendIds } = customCrowdView.updater.get() if (!relateRecommendIds.includes(combinationKeyId) && combinationKeyId) { relateRecommendIds.push(combinationKeyId) } customCrowdView.updater.set({ relateRecommendIds }) this.recurSelectTagOptionSet([recommend.selectTagOptionSetDTO], async selects => { // 找到推荐标签对应的vframe,重新render下, const tagId = selects[0].tagId const tagVframe = await this.waitVframeCreated(Magix.Vframe.get(`${tagViewId}_${tagId}`)) if (tagVframe) { delete tagVframe.$v.options.tagSelected // 删除掉原先选择过的选项 if (e.target.checked) { tagVframe.$v.options.tagSelected = { selects } } tagVframe.invoke('render') } // 在create-crowd页面里处理选中项目,归总显示的问题 // tagGroups下每个分组group都挂上递归算好的selected const createCrowdView = this.owner.parent().parent().parent().$v const { tagGroups } = createCrowdView.updater.get() // 推荐的标识 createCrowdView.updater.set({ source: 'tag_relation_bp' }) this.recurTagGroups([tagGroups], async tag => { if (tag.id == tagId) { if (e.target.checked) { const tagInfo = await this.getTag(tagId, bizCode) tag.optionGroups = tagInfo.optionGroups for (const select of selects) { const names = select.names ? select.names.split(',') : [] for (const group of tag.optionGroups) { if (group.id == select.optionGroupId) { group.selected = { names, values: select.values.split(',') } } } } tag.tagSelected = { selects } } else { delete tag.optionGroups delete tag.tagSelected } createCrowdView.setGroupSelected(tagGroups) createCrowdView.updater.digest({ tagGroups }) // 更新下覆盖数 createCrowdView.getCoverage() return false } }) }) }, // 递归tagGroups recurTagGroups (tagGroups, cb) { let isContinue = true function recur (groups) { if (isContinue === false) { return } for (const group of groups) { if (group.tags && group.tags.length) { for (const tag of group.tags) { isContinue = cb(tag) } } if (group.children && group.children.length) { recur(group.children) } } } recur(tagGroups) } })