flow.vue 7.71 KB
<!--
 * @Description: 客流转化
 * @Author: huyue
 * @Date: 2022-06-23 15:05:21
 * @LastEditors: huyue
 * @LastEditTime: 2022-07-07 17:27:53
-->
<template>
  <div>
    <div v-show="hasData" class="flow-box">
      <div class="flow-left">
        <div class="title inline" style="padding-left: 0">客流转化</div>
        <div class="chart-box">
          <div id="flow" :style="{ height: height - 4.1 + 'vh' }"></div>
        </div>
      </div>
      <div class="flow-right">
        <div class="ranking-title">
          <div @click="changeRank(1)" :class="isActive == 1 ? 'active' : ''">前5{{ isBrand ? '门店' : '品牌' }}</div>
          <div @click="changeRank(2)" :class="isActive == 2 ? 'active' : ''">后5{{ isBrand ? '门店' : '品牌' }}</div>
        </div>
        <div class="ranking-content">
          <div class="ranking-item" v-for="(item, index) in flowData" :key="index">
            <span class="ranking-sort">{{ index + 1 }}</span>
            <span class="ranking-name">
              <marquee :direction="directionTimer" :delay="delayTimer" :text="item.brand" />
            </span>
            <span class="ranking-text">{{ item.value }}</span>
          </div>
        </div>
      </div>
    </div>
    <div v-show="!hasData">
      <div class="title">客流转化</div>
      <div class="empty">
        <img src="../../assets/images/empty.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import { changeMixin } from '@/mixins'
import datas from '../../utils/data'
import utils from '../../utils/util'
import * as echarts from 'echarts'
export default {
  name: 'Flow',
  mixins: [changeMixin],
  props: ['height'],
  data() {
    return {
      directionTimer: datas.DefaultData.directionTimer,
      delayTimer: datas.DefaultData.delayTimer,
      flowData: [],
      isActive: 1,
      funnelData: [
        {
          name: '客流',
          type: 'enter',
          value: 100
        },
        {
          name: '留档',
          type: 'stayfile',
          value: 80
        },
        {
          name: '试驾',
          type: 'test',
          value: 60
        },
        {
          name: '订单',
          type: 'placeorder',
          value: 40
        },
        {
          name: '成交',
          type: 'success',
          value: 20
        }
      ],
      option: {},
      echartsFunnel: '',
      hasData: true,
      paramsData: {},
      topList: [],
      lastList: [],
      isBrand: false
    }
  },
  mounted() {
    this.echartsFunnel = echarts.init(document.getElementById('flow'))
    // resize
    window.addEventListener('resize', () => {
      this.echartsFunnel.resize()
    })
  },
  methods: {
    init(val) {
      this.paramsData = this.$store.state.app.reportParams
      this.isBrand = this.paramsData.brand && this.paramsData.brand.name
      this.getSaletTotal(this.paramsData)
      this.getSaleTrans(this.paramsData)
    },
    getSaletTotal(val) {
      const params = {
        req: {
          queryTime: this.getCurrentDate(),
          queryType: val.reportType
        }
      }
      var brandName = val.brand ? val.brand.name : ''
      this.api.sales[brandName ? 'storeTotal' : 'saleTotal'](params)
        .then(res => {
          if (res.code === '0') {
            if (res.data.dataList.length) {
              res.data.dataList[0].num.forEach((item, index) => {
                this.funnelData[index].num = item
              })
              this.hasData = !res.data.dataList[0].num.every(i => i == 0)
              if (this.hasData) this.draw()
            } else {
              this.hasData = false
            }
          }
        })
        .catch(() => {
          this.hasData = false
        })
    },
    draw() {
      this.option = {
        tooltip: {
          trigger: 'item',
          backgroundColor: 'rgba(68,68,68,0.8)',
          formatter: e => {
            return `${e.data.name}: ${e.data.num}`
          },
          textStyle: {
            color: '#fff',
            fontSize: 8
          },
          borderWidth: 0
        },
        color: ['rgba(235, 96, 2, 1)', 'rgba(235, 96, 2, .8)', 'rgba(235, 96, 2, .6)', 'rgba(235, 96, 2, .4)', 'rgba(245, 170, 2, .8)'],
        series: [
          {
            name: '漏斗图',
            type: 'funnel',
            left: 0,
            top: 8,
            bottom: 8,
            width: '100%',
            minSize: '30%',
            maxSize: '100%',
            sort: 'descending',
            gap: 2,
            label: {
              show: true,
              position: 'inside',
              color: '#fff'
            },
            labelLine: {
              length: 10,
              lineStyle: {
                width: 1,
                type: 'solid'
              }
            },
            itemStyle: {
              borderColor: '#fff',
              borderWidth: 0
            },
            emphasis: {
              label: {
                fontSize: 20
              }
            },
            data: this.funnelData
            // ]
          }
        ]
      }
      this.echartsFunnel.setOption(this.option)
      this.echartsFunnel.on(
        'mouseover',
        utils.debounce(data => {
          this.getSaleTrans(this.paramsData, data.data.type)
        }),
        1000 / 60
      )
    },
    getSaleTrans(val, queryItem = 'enter') {
      const params = {
        req: {
          queryItem: queryItem,
          queryTime: this.getCurrentDate(),
          queryType: val.reportType
        }
      }
      let api = 'saleTrans'
      if (val.brand && val.brand.name) {
        params.req.brandName = '品牌1'
        api = 'storeSaleTrans'
      }
      this.api.sales[api](params).then(res => {
        this.topList = []
        this.lastList = []
        if (res.data.dataList.length) {
          let { topList, lastList } = res.data.dataList[0]
          topList = topList ? JSON.parse(topList) : []
          lastList = lastList ? JSON.parse(lastList) : []
          for (const key in topList) {
            this.topList.push({
              brand: key,
              value: topList[key]
            })
          }
          for (const key in lastList) {
            this.lastList.push({
              brand: key,
              value: lastList[key]
            })
          }
        }
        this.flowData = this.isActive == 1 ? this.topList : this.lastList
      })
    },
    // 切换品牌排序
    changeRank(val) {
      this.isActive = val
      this.flowData = val == 1 ? this.topList : this.lastList
    }
  }
}
</script>

<style lang="scss" scoped>
.flow-box {
  display: flex;
  padding: 0 8px 1vh;
  box-sizing: border-box;
  .flow-left {
    padding-bottom: 0.9vh;
    width: 50%;
  }
  .flow-right {
    flex: 1;
    color: #fff;
    font-size: 10px;
    padding: 1vh 0 0.9vh 20px;
    .ranking-title {
      display: flex;
      width: 100%;
      margin-right: 20px;
      cursor: default;
      .active {
        background-color: #eb6001;
        color: #fff;
      }
      div {
        width: 50%;
        text-align: center;
        background-color: #353535;
        line-height: 2.4vh;
      }
    }
    .ranking-content {
      background-color: #262525;
    }
    .ranking-item {
      display: flex;
      width: 100%;
      align-items: center;
      line-height: 2.1vh;
      height: 2.1vh;
      .ranking-sort {
        display: inline-block;
        width: 0.3vw;
        text-align: center;
      }
      .ranking-name {
        width: calc(100% - 33% - 28px);
      }
      .ranking-text {
        width: 33%;
        padding-left: 4px;
        // width: calc(100% - 50px);
      }
      span {
        display: inline-block;
        &:first-child {
          padding: 0 8px;
        }
      }
    }
  }
}
.chart-box {
  width: 100%;
  box-sizing: border-box;
  // height: 11.7vh;
  padding: 0 25px;
}
</style>