import Magix from 'magix' import View from '../../../../view' Magix.applyStyle('@./create-crowd.less') export default View.extend({ tmpl: '@create-crowd.html', init (options) { this.options = options }, async render () { const { date, period, brandId, brandsMap } = this.options const { Dayjs } = this.updater.get() // const loc = Magix.Router.parse() const scenes = [{ text: '会员招募', value: '1' }, { text: '会员首购引导', value: '2' }] const { sceneType = scenes[0].value } = this.options const statusIdList = await this.getStatusIds() const { statusIds = [statusIdList[3].id] } = this.options // 默认选中行动人群 const memberTypes = [{ text: '全量会员', value: '1' }, { text: '全量非会员', value: '0' }] const memberStatusMap = { 1: memberTypes, 2: [{ text: '近30天首购会员', value: '1' }, { text: '近30天未购会员', value: '0' }] } const { mbrType = memberTypes[1].value } = this.options const validDate = Dayjs().add(30, 'd').format('YYYY-MM-DD') const newDate = Dayjs().subtract(1, 'd').format('YYYY-MM-DD') this.updater.digest({ memberStatusMap, newDate, validDate, statusIdList, statusIds, date, period, brandId, brandsMap, scenes, sceneType, memberTypes, mbrType }) this.refreshCoverage() }, async getStatusIds () { try { const models = [{ name: 'api_asset_deeplink_statusList_get' }] const [model] = await this.fetch(models) const data = model.get('data', []) return data.filter(item => item.id !== 0) } catch (error) { this.alert('错误提示', error.msg || error.message || error) } }, async createCrowd (isCreateCrowd) { try { const api = isCreateCrowd ? 'api_asset_deeplink_member_crowd_create_post' : 'api_asset_deeplink_member_crowd_select_get' const { brandId } = this.options const { crowdName, validDate, statusIds, mbrType, sceneType } = this.updater.get() const params = { sceneType, brandId, crowdName, validDate, statusIds: statusIds.join(','), mbrType } const models = { name: api, params, isJson: false } // 直接创建人群 if (isCreateCrowd) { params.statusIds = statusIds models.isJson = true } const [model] = await this.fetch(models) const data = model.get('data', {}) return data } catch (error) { this._alert('错误提示', error.msg || error.message || error) return {} } }, async refreshCoverage () { const { brandId } = this.options const { statusIds, mbrType, sceneType } = this.updater.get() if (!statusIds.length) { return this.updater.digest({ coverage: undefined }) } const params = { sceneType, brandId, statusIds: statusIds.join(','), mbrType } this.updater.digest({ coverageLoading: true }) const coverage = await this.getCoverage(params) this.updater.digest({ coverage, coverageLoading: false }) }, async getCoverage (params) { try { const models = [{ name: 'api_asset_deeplink_member_crowd_coverage_get', params }] const [model] = await this.fetch(models) const data = model.get('data.coverage') return data } catch (error) { this.alert('错误提示', error.msg || error.message || error) } }, 'changeScene' (e) { const { memberTypes } = this.updater.get() const { mbrType = memberTypes[1].value } = this.options this.updater.digest({ mbrType }) this['change']() }, 'change' (e) { this.refreshCoverage() }, 'cancel' (e) { const { dialog } = this.options dialog.close() }, // 直接创建人群 async 'createCrowd' (e) { if (!this.isValid()) { return } this.updater.digest({ createLoading: true }) await this.createCrowd(true) this.updater.digest({ createLoading: false }) this._alert('提示', '恭喜,人群创建成功!', () => { this.options.dialog.close() }) }, // 叠加更多圈人 async 'customCrowd' (e) { // 只校验部分 if (!this.isValid({ sizzle: `#${this.id}_validForm` })) { return } const { crowdName = '', validDate = '' } = this.updater.get() this.updater.digest({ customLoading: true }) const { selectTagOptionSet, extraDataValue } = await this.createCrowd() this.updater.digest({ customLoading: false }) if (selectTagOptionSet) { Magix.Router.to('/crowds-new/create', { selectTagOptionSet: JSON.stringify(selectTagOptionSet), crowdName, validDate, $deeplink_assets_mbr_custom_1: extraDataValue }) } } })