const Magix = require('magix') const View = require('dmp-new/view') Magix.applyStyle('@./channels.less') const YOUKU_ADM_IDS = [32, 108] // 优酷adm渠道id const DONGFENG_IDS = [35, 122] // 东风系统id const COVERAGE_LIMIT_TIP_IDS = [1, 34, 105] // 这几个渠道在覆盖数小于100W的时候,给出提示 const NO_CANCEL_SYNC_CHANNELS = [36, 29, 31, 117] // 指定某些渠道ID不可以取消同步 const NOTICE_CHANNELS = [1, 34, 105] // 这些渠道同步前先给提示,引导同步引力魔方 // const AUTO_SOLID_CHANNELS = [36, 32] // 这些渠道同步时提示用户该人群会自动固化 const AOGE_ID = 117 module.exports = View.extend({ tmpl: '@channels.html', init(options) { this.options = options }, async render() { const channels = await this.getChannels() const { user } = this.updater.get() // 报表首页传进来是否推荐top3ChannelsIds // 人群列表打开的渠道选择是否推荐,由前端写死 // 52:策略中心 const { apps = [], // top3ChannelsIds = [52, 105], topChannelName } = this.options if (user.isXiaoer) { this.updater.set({ AOGE_ID // 奥格渠道特殊处理,只有小二版有 }) } if (user.isDmp) { // 东风渠道获取品牌 const brands = await this.getAgentBrands() this.updater.set({ brands }) } // 同步状态 for (const channel of channels) { for (const app of apps) { if (app.appId == channel.id) { channel.appStatus = app.status } } } this.updater.digest({ isTargetSearch: user.permissions.includes('CROWD_DELIVER_TARGET_SEARCH'), // 是否同步的渠道是搜索帐号类型, YOUKU_ADM_IDS, DONGFENG_IDS, NOTICE_CHANNELS, channels, // top3ChannelsIds, topChannelName: topChannelName }) }, // 获取东风品牌代理商列表 async getAgentBrands() { try { const models = [ { name: 'api_crowd_dongfeng_brands_get' } ] const [model] = await this.fetch(models) const data = model.get('data.list', []) const brandIdMap = {} const result = [] // 根据 id 归类下,有相同品牌id下不同的agent for (const brand of data) { if (!brandIdMap[brand.id]) { brand.agents = [brand] result.push(brand) brandIdMap[brand.id] = brand } else { brandIdMap[brand.id].agents.push(brand) } } const dataMap = Magix.toMap(result, 'id') return { data: result, dataMap } } catch (error) { this.alert('错误提示', error.msg || error.message || error, null, { mask: true }) } }, // 获取渠道 async getChannels() { try { const params = { crowdId: this.options.id } let api = 'api_deliverapp_deliverapplist_get' if (this.options.memberId) { params.memberId = this.options.memberId api = 'api_isv_deliverapp_deliverapplist_get' } const models = [ { name: api, params } ] const [model] = await this.fetch(models) const data = model.get('data.list', []) for (const channel of data) { if (YOUKU_ADM_IDS.includes(channel.id)) { channel.accounts = [ { nickname: '优酷ADM', targetId: 117810562 } ] } } return data } catch (error) { this.alert('错误提示', error.msg) } }, async searchAccount(e) { const channel = e.params.channel const channels = this.updater.get('channels') if (!channel._nickname.trim()) { return } channel.selected = channel.selected || [] channel.accounts = [] this.updater.digest({ loadingAccounts: true, channels }) try { const models = [ { name: 'api_user_find_get', params: { nickname: channel._nickname, pageSize: 150 } } ] const [model] = await this.fetch(models) const users = model.get('data.users', []) channel.accounts = users // 全选状态判断 channel.isAll = channel.selected.length === channel.accounts.length this.updater.digest({ channels }) } catch (error) { this.alert('错误提示', error.msg || error.message) } this.updater.digest({ loadingAccounts: false }) }, // 搜索品牌 async 'showBrands'(e) { // 输入框输入关键词时触发show // 当前输入框输入的值 const keyword = e.keyword const instance = Magix.Vframe.get(this.id + '_brandSearch') instance.invoke('showLoading') const [model] = await this.fetch({ name: 'api_deliver_dongfeng_brand_get', params: { keyword } }) const list = model.get('data', []) this._brands = list instance.invoke('update', [list]) instance.invoke('hideLoading') }, // 选择品牌 'selectBrand'(e) { const channels = this.updater.get('channels') let dongfengChannel for (const channel of channels) { if (DONGFENG_IDS.includes(channel.id)) { dongfengChannel = channel } } for (const brand of this._brands) { if (brand.id == e.selected) { dongfengChannel.accounts = [] for (const agent of brand.agentList) { dongfengChannel.accounts.push({ targetId: agent.id, nickname: agent.name }) } } } if (!e.selected) { delete dongfengChannel.selected delete dongfengChannel.selectedItem delete dongfengChannel.accounts } this.updater.digest({ channels }) }, 'searchAccount'(e) { if (e.keyCode === 13) { this.searchAccount(e) } }, 'searchAccount'(e) { this.searchAccount(e) }, // 奥格渠道请求租户列表 async getAogeLessees(channel) { const { channels } = this.updater.get() try { const models = [ { name: 'api_crowd_augeNamespace_list_get' } ] const [model] = await this.fetch(models) const lessees = model.get('data', []) channel.lessees = lessees if (lessees.length) { channel.selected = lessees[0].id.toString() } this.updater.digest({ channels }) } catch (error) { this.alert('错误提示', error.msg || error.message || error, null, { mask: true }) } }, async 'getAccounts'(e) { const { user, channels, isTargetSearch, AOGE_ID } = this.updater.get() const channel = e.params.channel // operType=7 为小二账号,通过搜索的形式选择要同步的账号 if (channel.accounts) { return } this.updater.digest({ loadingAccounts: true }) // 奥格渠道请求租户列表 if (channel.id == AOGE_ID) { await this.getAogeLessees(channel) } else { if (isTargetSearch) { this.updater.digest({ loadingAccounts: false }) return } const params = { crowdId: this.options.id, appId: channel.id, // 服务商版的同步渠道用this.options.memberId memberId: this.options.memberId || user.loginUser.userId } try { const api = this.options.memberId ? 'api_isv_crowd_appUser__get' : 'api_crowd_appUser__get' const models = [ { name: api, params } ] const [model] = await this.fetch(models) const accounts = model.get('data.list', []) channel.accounts = accounts // 选中的帐号 channel.selected = [] for (const account of channel.accounts) { if (account.num != 0) { channel.selected.push(account.targetId) } } // 全选状态判断 if (channel.selected.length === channel.accounts.length) { channel.isAll = true } this.updater.digest({ channels }) } catch (error) { this.alert('错误提示', error.msg) } } this.updater.digest({ loadingAccounts: false }) }, 'selectAll'(e) { const channels = this.updater.get('channels') const channel = e.params.channel const selected = [] if (e.target.checked) { channel.accounts.forEach(item => { selected.push(item.targetId || item.memberId) }) } channel.isAll = e.target.checked channel.selected = selected this.updater.digest({ channels }) }, async 'syncChannel'(e) { const { all, user, channels, brands, AOGE_ID } = this.updater.get() const me = this const channel = e.params.channel const params = { crowdId: this.options.id, appId: channel.id } // 指定某些渠道不可取消同步 if ( (!channel.selected || !channel.selected.length) && NO_CANCEL_SYNC_CHANNELS.includes(channel.id) ) { return this._alert( { title: '提示', content: '该渠道无法取消同步' }, { type: 'error' } ) } if ( COVERAGE_LIMIT_TIP_IDS.includes(channel.id) && this.options.coverage < 1000000 && !user.isXiaoer ) { // 这几个渠道在覆盖数小于100W的时候,给出提示(在商家版里) this.confirm({ title: '提示', content: '您圈定的人群规模较小,建议您扩大人群规模以免影响投放(建议人群规模在100W及以上),确定继续同步该渠道吗?', enterCallback() { run.bind(me)() } }) } // else if (AUTO_SOLID_CHANNELS.includes(channel.id)) { // // 这几个渠道给出用户提示同步时会自动固化人群 // this.confirm({ // title: '提示', // content: '同步该渠道会自动固化人群,确定继续同步该渠道吗?', // enterCallback () { // run.bind(me)() // } // }) // } else { run.bind(this)() } async function run() { if (channel.selected && channel.selected.length) { if (typeof channel.selected === 'string') { params.targetIds = channel.selected } else { params.targetIds = channel.selected.join(',') } } // 东风系统渠道特殊处理 // 固定targetIds为120994541 // 增加ext字段,放品牌以及代理商id // ext: '{"brandId":12345,"agentId":12345}' if (DONGFENG_IDS.includes(channel.id)) { params.targetIds = user.isDmp ? user.loginUser.userId : all.dongfengTargetId if (user.isDmp) { // 商家版 if (channel.brandId) { channel.selected = [channel.brandId] } const ext = { brandId: channel.brandId, agentId: channel.agentId, dfCrowdSource: 14 } params.ext = JSON.stringify(ext) } else { if (!channel.selected) { return this.alert( '提示', '该品牌没有代理商或您还未选择代理商,请搜索其他品牌', null, { mask: true } ) } const ext = { brandId: channel.selectedItem && channel.selectedItem.value, agentId: channel.selected } params.ext = JSON.stringify(ext) } } // 奥格渠道特殊处理: else if (AOGE_ID == channel.id) { params.targetIds = user.loginUser.userId params.ext = JSON.stringify({ namespace: channel.selected || '' }) } // 没有选择帐户的话则认为是取消同步,给个confirm确认提示 if (!channel.selected || !channel.selected.length) { // '提示', '您未选择任何店铺,视为取消同步该渠道,确定执行本操作吗?', null, { mask: true } this.confirm({ title: '提示', content: '您未选择任何店铺,视为取消同步该渠道,确定执行本操作吗?', enterCallback: () => { syncChannel.bind(this)() }, mask: true }) } else if ( NOTICE_CHANNELS.find(channelId => params.targetIds.toString().includes(channelId) ) ) { this.confirm({ title: '提示', content: '超级推荐&钻展将在2021年12月将进行如下改造:12月15日停止计划/单元新建功能,12月27日停止计划/单元编辑功能,为了您后续达摩盘人群正常使用,建议您准备同步至原超级推荐&智钻的人群,尽快同步至引力魔方渠道上进行投放!', enterCallback: () => { syncChannel.bind(this)() }, mask: true }) } else { syncChannel.bind(this)() } async function syncChannel() { try { const models = { name: 'api_crowd_app__post', params } // 服务商版接口不同,还要额外传个memberId if (user.isIsv) { models.name = 'api_isv_crowd_app_post' models.params.memberId = this.options.memberId } const [model] = await this.fetch(models) const data = model.get('data') if (channel.selected && channel.selected.length) { channel.appStatus = 1 this.alert('提示', '恭喜,营销渠道同步成功!', null, { mask: true }) } else { channel.appStatus = 0 this.alert('提示', '恭喜,营销渠道取消同步成功!', null, { mask: true }) } this.updater.digest({ channels }) } catch (error) { this.alert('错误提示', error.msg || error.message) } } } } })