import Magix from 'magix' import View from '../../../view' Magix.applyStyle('@./flow-crowd.less') const BLOCK_HEIGHT = 80 // 每一块的高度 const BLOCK_GAP = 10 const STATUS_DEEPEN_ID = 'deeplinkSubdivideStatusDeepenAnalysis' export default View.extend({ tmpl: '@flow-crowd.html', init(options) { this.options = options }, async render() { // const loc = Magix.Router.parse() this.updater.set(this.options) const { benchmark } = this.updater.get() // 概览数据 const { data, dataFlow } = await this.getData() const initialAssetLength = data.initialAsset.list.length this.updater.digest({ BLOCK_HEIGHT, BLOCK_GAP, svgHeight: initialAssetLength * BLOCK_HEIGHT + (initialAssetLength - 1) * BLOCK_GAP, data, dataFlow }) // 渲染流转svg this.renderFlowSvg() // 有同行数据,延迟请求加载 if (benchmark) { await this.renderBenchmark() } }, // 渲染流转svg renderFlowSvg() { const { dataFlow, statusId, data } = this.updater.get() const svgWidth = $(`#${this.id}_svg`).outerWidth() const curveLeft = svgWidth / 7 // 曲线转折点的横坐标 const lineLeft = svgWidth / 2.2 // 曲线后跟直线的起始横坐标 const blockWholeHeight = BLOCK_HEIGHT + BLOCK_GAP const hafBlockHeight = BLOCK_HEIGHT / 2 let startIndex // 流转起始人群索引 data.initialAsset.list.find((item, i) => { if (item.statusId == statusId) { startIndex = i return true } }) const startPos = hafBlockHeight + startIndex * blockWholeHeight // Y轴开始位置 // 排序流转人数,以计算流转的lineWidth const list = dataFlow.list.map(item => { return { uv: item.uv, statusId: item.statusId } }) list.sort((a, b) => { return b.uv - a.uv }) let step = Math.round(list.length / 3) step = step === 0 ? 1 : step // 最粗的线 list.slice(0, 1 * step).forEach(item => { item.lineWidth = 6 }) // 中等的线 list.slice(1 * step, 2 * step).forEach(item => { item.lineWidth = 5 }) // 最细的线 list.slice(2 * step).forEach(item => { item.lineWidth = 4 }) dataFlow.list.forEach((item, i) => { let endIndex // 流转结束人群索引 data.endAsset.list.find((_item, _i) => { if (_item.statusId == item.statusId) { endIndex = _i return true } }) const endPos = hafBlockHeight + endIndex * blockWholeHeight // Y轴结束位置 const svgConfig = { // svg画图path配置 d: `M 0 ${startPos} Q ${curveLeft} ${endPos} ${lineLeft} ${endPos} L ${svgWidth} ${endPos}`, lineWidth: 5 // 默认粗细 } // 获取线粗细 list.find((_item, _i) => { if (_item.statusId == item.statusId) { svgConfig.lineWidth = _item.lineWidth return true } }) item.svgConfig = svgConfig item.endIndex = endIndex }) this.updater.digest({ dataFlow }) }, // 延迟加载同行数据 async renderBenchmark() { const { benchmarkId } = this.options const [_data, _dataFlow] = await Promise.all([ this.getOriginData('1', benchmarkId), this.getDataFlowOrigin('1', benchmarkId) ]) const { data, dataFlow } = this.updater.get() // 同行数据合并进主数据 data.initialAsset.list.forEach((item, i) => { item.benchmarkLoading = false item.uvBenchmark = _data.initialAsset.list[i].uv item.reachedUvRateBenchmark = _data.initialAsset.list[i].reachedUvRate }) data.endAsset.list.forEach((item, i) => { item.benchmarkLoading = false item.uvBenchmark = _data.endAsset.list[i].uv item.uvGrowthRateBenchmark = _data.endAsset.list[i].uvGrowthRate }) // 流转的同行数据 dataFlow.list.forEach((item, i) => { item.benchmarkLoading = false item.uvBenchmark = _dataFlow.list[i].uv item.uvRateBenchmark = _dataFlow.list[i].uvRate }) this.updater.digest({ data, dataFlow }) }, grouped(list) { const _map = {} const result = [] for (const item of list) { if (_map[item.aiplKey]) { _map[item.aiplKey].push(item) } else { _map[item.aiplKey] = [item] result.push(_map[item.aiplKey]) } } return result }, // 汇总数据 async getData() { const [data, dataFlow] = await Promise.all([ this.getDataOverview(), this.getDataFlow() ]) data.initialAsset.groupedList = this.grouped(data.initialAsset.list) return { data, dataFlow } }, // 获取资产起始和结束数据 async getDataOverview() { const data = await this.getOriginData('0') const { benchmark } = this.updater.get() if (benchmark) { for (const item of data.initialAsset.list) { item.benchmarkLoading = true } for (const item of data.endAsset.list) { item.benchmarkLoading = true } } return data }, // 获取流转数据 async getDataFlow() { const data = await this.getDataFlowOrigin('0') const { benchmark } = this.updater.get() if (benchmark) { for (const item of data.list) { item.benchmarkLoading = true } } return data }, // 获取流转数据 async getOriginData(benchmark, benchmarkId) { const { brandId, dateType, startDate, bizDate, analyseCrowd = {} } = this.updater.get() try { const params = { brandId, dateType, startDate, bizDate, benchmark, benchmarkId, crowdId: analyseCrowd.crowdId } const models = [ { name: 'api_asset_deeplink_transfer_overview_get', params // cache: 60 * 60 * 1000 // 接口较慢,一小时内缓存 } ] const [model] = await this.fetch(models) const data = model.get('data', {}) return data } catch (error) { this.alert('错误提示', error.msg || error.message || error) } }, // 获取流转数据 async getDataFlowOrigin(benchmark, benchmarkId) { const { brandId, dateType, startDate, bizDate, statusId, analyseCrowd = {} } = this.updater.get() try { const params = { brandId, dateType, startDate, bizDate, statusId, benchmark, benchmarkId, crowdId: analyseCrowd.crowdId } const models = [ { name: 'api_asset_deeplink_transfer_get', params } ] const [model] = await this.fetch(models) const data = model.get('data', {}) return data } catch (error) { this.alert('错误提示', error.msg || error.message || error) } }, // 浮层打开创建人群 createCrowd(params = {}) { const viewPath = 'dmp-new/views/pages/deeplink/create-crowd' const { brandsMap, brandId, dateType, startDate, bizDate, statusId } = this.updater.get() const viewOptions = { brand: brandsMap[brandId], dateType, startDate, bizDate, statusId, createType: 3, // 创建入口,1:圈人浮层,2:渠道投放,3:流转圈人浮层,4:流转贡献圈人 isFlow: true // 流转圈人标识 } Object.assign(viewOptions, params) const dialogOptions = { header: { title: '圈选品牌DEEPLINK资产流转人群' }, footer: { enter: false, cancel: false }, width: 736 } this.mxModal(viewPath, viewOptions, dialogOptions) }, // 切换人群 'selectStatusId'(e) { const { statusId } = e.params // 细分市场页面特殊处理,top-bar 选中分层人群 // deeplinkSubdividePermeateFlow from './subdivide/index.html' const deeplinkSubdividePermeateFlowVf = Magix.Vframe.get( 'deeplinkSubdividePermeateFlow' ) if (deeplinkSubdividePermeateFlowVf) { deeplinkSubdividePermeateFlowVf.invoke('setStatusId', [statusId]) } Magix.Router.to({ statusId }) }, // 切换起始或结束日期 'changeDate'(e) { const { startDate, bizDate } = this.updater.get() Magix.Router.to({ startDate, bizDate }) }, // 查看各渠道贡献 'gotoContribute'(e) { // document.querySelector(`#${this.owner.parent().id}_contribute`).scrollIntoView({ // behavior: 'smooth' // }) Magix.Router.to({ statusDeepenType: '2' }) document.getElementById(STATUS_DEEPEN_ID).scrollIntoView({ behavior: 'smooth' }) }, // 圈选流转人群资产 'crowdFlow'(e) { const { data } = this.updater.get() this.createCrowd({ // transferData: '0,2001;2001,2002_2003' transferData: data.transferData }) }, // 查看新增人群资产流转 'gotoNewAsset'(e) { Magix.Router.to({ statusId: '0' }) } })