import Magix from 'magix' import View from 'dmp-new/view' Magix.applyStyle('@./flow.less') const BLOCK_HEIGHT = 60 // 每一块的高度 const BLOCK_GAP = 10 export default View.extend({ tmpl: '@flow.html', init(options) { this.options = options }, async render() { // const loc = Magix.Router.parse() const { report, taskId, transferType, top20, brandsMap, isInflow, statusIds } = this.options const transferTypeText = isInflow ? '流入' : '流出' const transferTypeTextReverse = isInflow ? '流出' : '流入' let { statusId = statusIds[0].id, competeBrandId = top20[0]?.brandId } = this.updater.get() this.updater.set({ statusId, isInflow, competeBrandId, transferType, transferTypeText, transferTypeTextReverse, top20, brandsMap }) // 概览、流转数据 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, statusIds, // 人群分层数据传到子view用来当筛选下拉 competeBrandId, report, taskId }) // 渲染流转svg this.renderFlowSvg() }, // 渲染流转svg renderFlowSvg() { const { dataFlow, statusId, data } = this.updater.get() const svgWidth = $(`#${this.id}_svg`).outerWidth() const curveLeft = svgWidth / 12 // 曲线转折点的横坐标 const lineLeft = svgWidth / 5 // 曲线后跟直线的起始横坐标 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 }) }, 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() { try { const { taskId, transferType } = this.options const { competeBrandId, noOverview, data } = this.updater.get() // 切换人群分层时不再请求概览数据 if (noOverview) { return data } const params = { taskId, competeBrandId, transferType } const models = [ { name: 'api_deeplink_compete_transfer_overview_get', params } ] const [model] = await this.fetch(models) return model.get('data', {}) } catch (error) { console.error(error) } }, // 流转细节 async getDataFlow() { try { const { taskId, transferType } = this.options const { competeBrandId, statusId } = this.updater.get() const params = { taskId, competeBrandId, transferType, statusId } const models = [ { name: 'api_deeplink_compete_transfer_get', params } ] const [model] = await this.fetch(models) const data = model.get('data', {}) return data } catch (error) { console.error(error) } }, // 切换人群 'selectStatusId'(e) { const { statusId } = e.params this.updater.set({ statusId, noOverview: true // 不重新请求概览数据 }) this.render() }, 'changeBrand'(e) { this.updater.set({ noOverview: false // 重新请求概览数据 }) this.render() }, 'createCrowd'(e) { const { report, taskId, transferType, top20, brandsMap, isInflow } = this.options const { competeBrandId, statusIds, statusId } = this.updater.get() const viewPath = '@./dialog/create-crowd-level' const viewOptions = { report, taskId, statusId, transferType, top20, brandsMap, isInflow, competeBrandId, statusIds } const dialogOptions = { header: { title: isInflow ? '圈选流入本品牌资产TOP20竞争品牌人群' : '圈选本品牌流出资产TOP20竞争品牌人群' }, footer: { enter: false, cancel: false }, width: 780 } this.mxModal(viewPath, viewOptions, dialogOptions) } })