const Magix = require('magix') const View = require('dmp-new/view') Magix.applyStyle('@./lbs.less') // 门店半径可选项 const RADIUS = [ { text: '3公里以内精选人群', value: '2' } ] for (let i = 3; i < 11; i++) { const distance = i.toString() RADIUS.push({ text: `${distance}公里`, value: distance }) } // 门店边界类型 const RADIUS_TYPES = [{ text: '默认门店边界', value: '1' }, { text: '自定义', value: '2' }] // 时间周期可选项 const DATES = [{ text: '近3天', value: '3' }, { text: '近5天', value: '5,3' }, { text: '近7天', value: '7,5,3' }, { text: '近15天', value: '15,7,5,3' }, { text: '近30天', value: '30,15,7,5,3' }] // 人群行为可选项 let BEHAVIORS = [{ text: '到达', value: '1' }, { text: '常驻', value: '2' }] module.exports = View.extend({ tmpl: '@shop.html', init (options) { this.options = options }, async render (e) { const all = this.updater.get('all') const user = this.updater.get('user') const tag = this.options.viewData.tag // 根据标签里的option来确定选项值 if (tag.id) { const behaviors = [] const dates = [] for (const option of tag.options) { // 人群行为group if (option.optionGroupId == all.shopBehaviorOptionGroupId) { behaviors.push({ text: option.optionName, value: option.optionValue }) } // 时间周期group if (option.optionGroupId == all.shopDateRangeOptionGroupId) { dates.push({ text: option.optionName, value: option.optionValue }) } } if (behaviors.length) { BEHAVIORS = behaviors } // if (dates.length) { // DATES = dates // } } this.updater.set({ isDmp: all.dmpOperType == user.loginUser.operType, selectedOptions: [], // 选中的门店列表 radius: RADIUS, // 所有可选半径下拉列表 dates: DATES, // 所有可选时间周期 behaviors: BEHAVIORS, // 人群行为可选项 radiusTypes: RADIUS_TYPES, // 门店半径类型 currentRadius: '0.5', // 自定义半径距离 radiusType: '1', // 门店边界类型:1,默认边界,2,自定义 radiusDisabled: false, // 自定义边界是否禁用 behaviorType: BEHAVIORS[0].value, // 人群行为类型 dateRange: DATES[0].value, // 时间周期 shopList: [], // 搜索门店下拉列表 showShopList: false, // 是否显示门店下拉列表 shopName: '' // 搜索的门店名, }) // 编辑时,带了人群信息 if (this.options.viewData.selects) { this.setCrowd() } // 地图定位信息 const loc = this.getMapLocation() this.updater.digest(loc) // 覆盖数 this.getCoverage() // // this.owner.mountVframe('map', '@./map', { // currentRadius: this.updater.get('currentRadius'), // locations: this.updater.get('locations'), // radiusType: this.updater.get('radiusType') // }) }, // 编辑人群时,设置人群原来的设置 setCrowd () { // 编辑时,从selects里的values里取出半径数据 // 目前所有门店位置使用统一的半径 let radiusType = this.updater.get('radiusType') // 默认使用默认边界 let currentRadius = this.updater.get('currentRadius') const all = this.updater.get('all') // 编辑时默认设置【人群行为】&【时间周期】 let behaviorType = this.updater.get('behaviorType') let dateRange = this.updater.get('dateRange') let shopSelect // 门店select let ptSelect // 人群行为select let drSelect // 时间周期select const lbsStorageData = this.options.viewData.lbsStorageData // 编辑时已选门店列表selectedOptions 来自于 crowd.extra.lbsStorageData配置 // 默认第一个选中 const selectedOptions = lbsStorageData.currentType == '1' && lbsStorageData.showSelected.selectedOptions || [] for (const item of selectedOptions) { item.selected = false } selectedOptions[0] && (selectedOptions[0].selected = true) // 有批量上传的门店的禁用自定义门店半径 const radiusDisabled = this.checkRadiusDisabled(selectedOptions) for (const select of this.options.viewData.selects) { if (select.optionGroupId == all.shopOptionGroupId) { shopSelect = select } else if (select.optionGroupId == all.shopBehaviorOptionGroupId) { ptSelect = select } else if (select.optionGroupId == all.shopDateRangeOptionGroupId) { drSelect = select } } if (shopSelect) { const _radius = shopSelect.values.split(',')[0].split(':')[2] // 半径为0则表示使用默认边界 // 其他则是具体半径值 if (_radius !== '0') { radiusType = '2' currentRadius = _radius } } if (ptSelect) { behaviorType = ptSelect.values } if (drSelect) { dateRange = drSelect.values } this.updater.set({ selectedOptions, radiusType, currentRadius, behaviorType, dateRange, radiusDisabled }) }, // 地图定位信息 getMapLocation () { // 地图定位信息 let locations = [] const currentRadius = this.updater.get('currentRadius') const selectedOptions = this.updater.get('selectedOptions') if (selectedOptions.length) { for (const item of selectedOptions) { if (item.selected) { locations = item.locations } } } return { locations, currentRadius } }, checkSelected () { const selectedOptions = this.updater.get('selectedOptions') const behaviorType = this.updater.get('behaviorType') const dateRange = this.updater.get('dateRange') if (!selectedOptions.length) { return false } return true }, // 拼selectOptionTagSet里的标准selects格式数据 getSelects () { const selects = [] const selectedOptions = this.updater.get('selectedOptions') const all = this.updater.get('all') const shopValues = [] for (const item of selectedOptions) { for (const location of item.locations) { shopValues.push(location) } } if (shopValues.length) { // 门店 selects.push({ optionGroupId: all.shopOptionGroupId, tagId: all.shopTagId, values: shopValues.join(',') }) } // 人群行为 selects.push({ optionGroupId: all.shopBehaviorOptionGroupId, tagId: all.shopTagId, values: this.updater.get('behaviorType') }) // 时间周期 if (this.updater.get('behaviorType') == 1) { selects.push({ optionGroupId: all.shopDateRangeOptionGroupId, tagId: all.shopTagId, values: this.updater.get('dateRange') }) } else { // 常驻时间周期固定传values="0" selects.push({ optionGroupId: all.shopDateRangeOptionGroupId, tagId: all.shopTagId, values: '0' }) } return selects }, getShowSelected () { return { selectedOptions: this.updater.get('selectedOptions') } }, // 根据选项获取覆盖人数 async getCoverage () { const me = this const all = me.updater.get('all') const selects = this.getSelects() const selectTagOptionSet = $.extend(true, {}, all.selectTagOptionSet_421) // let behaviorType = this.updater.get('behaviorType') if (selects.length === 3) { selectTagOptionSet.selectTagOptionSet[0].selectTagOptionSet[0].selectTagOptionSet.push({ selects }) try { me.updater.digest({ coverageLoading: true }) const params = { selectTagOptionSet } const models = [{ name: 'api_analysis_coverage_post', params, isJson: true }] const [model] = await me.fetch(models) const data = model.get('data') me.updater.digest({ coverage: data.coverage, hintDataSecurity: data.hintDataSecurity }) } catch (error) { me.alert('错误提示', error.msg) me.updater.digest({ coverage: 0, hintDataSecurity: false }) } } else { me.updater.digest({ coverage: 0, hintDataSecurity: false }) } me.updater.digest({ coverageLoading: false }) }, // 搜索门店列表 async searchShop () { try { const params = { keyword: this.updater.get('shopName') } const models = [{ name: 'api_lbs_poi_get', params }] const [model] = await this.fetch(models) const data = model.get('data', []) return data } catch (error) { this.alert('错误提示', error.msg || error.message) } }, // 检测搜索下拉门店里的已添加状态 checkShopListSelected (selectedOptions, shop) { const _location = this.getLocations(shop) for (const item of selectedOptions) { for (const location of item.locations) { if (location === _location) { shop.selected = true break } } } }, // 取消搜索下拉门店里的已添加状态 checkShopListUnselected (locations, shopList) { for (const location of locations) { for (const shop of shopList) { const _location = this.getLocations(shop) if (_location === location) { shop.selected = false } } } }, getLocations (shop) { // let radius = '0' if (this.updater.get('radiusType') == '2') { radius = this.updater.get('currentRadius') } return `${shop.id}:${shop.location.split(',').join('*')}:${radius}:${shop.area}` }, // 切换门店半径 'changeRadius' (e) { const selectedOptions = this.updater.get('selectedOptions') const radiusType = this.updater.get('radiusType') const currentRadius = this.updater.get('currentRadius') // 切换门店半径类型,处理下数据selectedOptions // radiusType:1,默认边界,则半径传0 // radiusType:2,自定义,则半径传currentRadius for (const item of selectedOptions) { item.locations.forEach((location, i) => { const data = location.split(':') data[2] = radiusType === '1' ? '0' : currentRadius item.locations[i] = data.join(':') }) } this.updater.set({ selectedOptions }) // 刷新覆盖数 this.getCoverage() }, // 切换人群行为 'changeBehavior' (e) { this.getCoverage() }, // 切换时间周期 'changeDate' (e) { this.getCoverage() }, // 门店列表里的添加按钮 'addShop' (e) { const shop = e.params.shop const selectedOptions = this.updater.get('selectedOptions') // 添加门店,将当前门店选中 for (const item of selectedOptions) { item.selected = false } selectedOptions.push({ name: shop.name, locations: [this.getLocations(shop)], selected: true }) // 检测搜索下拉门店里的已添加状态 this.checkShopListSelected(selectedOptions, shop) this.updater.set({ selectedOptions }) // 地图定位信息 const loc = this.getMapLocation() this.updater.digest(loc) // 更新人群覆盖信息 this.getCoverage() }, // 搜索门店列表下拉里的查看按钮 'checkMap' (e) { const locations = [this.getLocations(e.params.shop)] this.updater.digest({ locations }) }, 'searchShop' (e) { const me = this const keyword = e.target.value.trim() const selectedOptions = this.updater.get('selectedOptions') this.updater.set({ shopName: keyword }) clearTimeout(this._itv) if (!keyword) { return this.updater.digest({ shopList: [] }) } this._itv = setTimeout(async () => { const shopList = await me.searchShop() for (const shop of shopList) { // 检测搜索下拉门店里的已添加状态 me.checkShopListSelected(selectedOptions, shop) } me.updater.digest({ shopList }) }, 300) }, // 判断是否全删除了批量的门店,没有批量门店的话,启用自定义半径设置 checkRadiusDisabled (selectedOptions) { selectedOptions = selectedOptions || this.updater.get('selectedOptions') let radiusDisabled = false for (const item of selectedOptions) { if (item.locations && item.locations.length > 1) { radiusDisabled = true break } } return radiusDisabled }, // 删除门店 'removeShop' (e) { const selectedOptions = this.updater.get('selectedOptions') const shopList = this.updater.get('shopList') const curr = selectedOptions[e.params.index] selectedOptions.splice(e.params.index, 1) // 取消门店搜索下拉里的选中 this.checkShopListUnselected(curr.locations, shopList) // 判断是否全删除了批量的门店,没有批量门店的话,启用自定义半径设置 const radiusDisabled = this.checkRadiusDisabled() this.updater.digest({ selectedOptions, shopList, radiusDisabled }) this.getCoverage() }, // 选中门店 'select' (e) { if (e.target.nodeName === 'A') { return } const selectedOptions = this.updater.get('selectedOptions') const shopList = this.updater.get('shopList') for (const item of selectedOptions) { item.selected = false } if (selectedOptions[e.params.index]) { selectedOptions[e.params.index].selected = true } this.updater.set({ selectedOptions, shopList }) // 地图定位信息 const loc = this.getMapLocation() this.updater.digest(loc) }, // 批量上传 'uploadBatch' () { const me = this const viewPath = '@./upload-shop-batch' const selectedOptions = this.updater.get('selectedOptions') const viewOptions = { addCallback (fileName, sucList) { // 批量添加的门店,禁止使用自定义门店半径 me.updater.set({ radiusType: 1, radiusDisabled: true }) // 添加门店,将当前门店选中 for (const item of selectedOptions) { item.selected = false // 恢复默认边界0 item.locations.forEach((location, index) => { const splits = location.split(':') splits[2] = '0' item.locations[index] = splits.join(':') }) } const locations = [] for (const shop of sucList) { locations.push(me.getLocations(shop)) } selectedOptions.push({ name: fileName, locations, selected: true }) me.updater.set({ selectedOptions }) // 地图定位信息 const loc = me.getMapLocation() me.updater.digest(loc) // 更新人群覆盖信息 me.getCoverage() } } const dialogOptions = { width: 600, top: 100 } this.mxDialog(viewPath, viewOptions, dialogOptions) }, 'shopSearchFocus' (e) { this.updater.digest({ showShopList: true }) }, '$doc' (e) { const target = $(e.target) const me = this if (!target.parents(`#${me.id}_shopSearchWrapper`).length && target.attr('data-id') !== 'shopAdd') { me.updater.digest({ showShopList: false }) } } })