MicroStore.vue 9.91 KB
<!--
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-09-14 10:26:58
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-10-10 13:46:15
 * @FilePath: \scrm-micro-home\src\views\home\components\MicroStore.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="analysis-main">
    <div class="analysis-head">
      微店经营分析
    </div>
    <div class="analysis-body">
      <div class="analysis-bar flex ">
        <div class="analysis-bar-left">
          <div class="analysis-bar-wrap">
            <div class="warp-desc">总销售额</div>
            <div class="warp-num">{{ orderDetail.totalSaleAmount | formatNumber }}</div>
            <div class="warp-price">
              本月销售额
              <span>¥{{ orderDetail.saleAmount | formatNumber }}</span>
            </div>
            <img src="@/assets/img/home/money-icon.png" />
          </div>
          <div class="analysis-bar-wrap">
            <div class="warp-desc">总订单数</div>
            <div class="warp-num">{{ orderDetail.totalOrderCount | formatNumber }}</div>
            <div class="warp-price">
              本月订单数
              <span>{{ orderDetail.orderCount }}</span>
            </div>
            <img src="@/assets/img/home/order-icon.png" />
          </div>
        </div>
        <div class="analysis-bar-right">
          <div class="flex-row-between">
            <div class="chart-title">销售额趋势</div>
            <div class="chart-time">
              <el-radio-group v-model="dateValue" size="small">
                <el-radio-button label="day">本月</el-radio-button>
                <el-radio-button label="month">全年</el-radio-button>
              </el-radio-group>
              <el-date-picker class="chart-time-picker" @change="dateChange" v-model="timeValue" size="small" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </div>
          </div>

          <line-chart :lineList="lineList" domId="store1" functionStr="getLineOddOptions"></line-chart>
        </div>
      </div>
      <div class="analysis-circle">
        <div class="analysis-circle-title">业务占比分析</div>
        <div class="flex" v-if="!emptyStatus">
          <div class="analysis-circle-left">
            <line-chart :lineList="orderList" domId="order1" functionStr="getPieOptions"></line-chart>
          </div>
          <div class="analysis-circle-right">
            <div class="chart-item" v-for="(item, index) in orderList" :key="index">
              <div class="chart-item-1">
                <i :style="{ background: item.color }"></i>
                {{ item.label }}
              </div>
              <div class="chart-item-2">
                <img src="@/assets/img/home/pie-arrow.png" />
              </div>
              <div class="chart-item-3">占比 {{ item.radio }}%</div>
              <div class="chart-item-4">销售额 ¥{{ item.saleAmount | formatNumber }}</div>
              <div class="chart-item-4">订单数:{{ item.orderCount }}</div>
            </div>
          </div>
        </div>
        <div v-else class="empty-panel">
          <div class="empty-img"><img src="@/assets/img/home/empty.png" /></div>
          <div class="empty-text">暂无数据</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import util from '@/utils/util'
import * as dayjs from 'dayjs'
export default {
  data() {
    return {
      dateValue: null,
      lineList: {
        title: '精改业务-精品升级',
        backgroundColor: 'linear-gradient(179deg, #F5F7FF 1%, #F9FBFF 100%);',
        color: ['#98B8FF', '#5850FF', '#83D8FF', '#0087FA'],
        lineList: []
      },
      timeValue: [],
      emptyStatus: true,
      orderDetail: {},
      orderList: [
        { label: '商城', color: '#5850FF', orderSource: 1, saleAmount: 0, orderCount: 0, radio: 0 },
        { label: '直播', color: '#50CEFB', orderSource: 2, saleAmount: 0, orderCount: 0, radio: 0 },
        { label: '秒杀', color: '#BACEFD', orderSource: 3, saleAmount: 0, orderCount: 0, radio: 0 },
        { label: '众筹', color: '#87F2E3', orderSource: 4, saleAmount: 0, orderCount: 0, radio: 0 },
        { label: '优惠套餐', color: '#B1E7FC', orderSource: 8, saleAmount: 0, orderCount: 0, radio: 0 },
        { label: '膨胀金', color: '#C7CBD2', orderSource: 5, saleAmount: 0, orderCount: 0, radio: 0 }
      ]
    }
  },
  filters: {
    formatNumber(val) {
      return util.formatNumber(val) || 0
    }
  },
  watch: {
    dateValue(val) {
      if (val) {
        this.storeData(val)
      }
    }
  },
  mounted() {
    const start = new Date()
    this.timeValue = [start.setTime(start.getTime() - 3600 * 1000 * 24 * 14), new Date()]
    this.init(this.timeValue)
  },
  methods: {
    init(date) {
      this.getLineList({
        startTime: dayjs(date[0]).format('YYYY-MM-DD') + ' 00:00:00',
        endTime: dayjs(date[1]).format('YYYY-MM-DD') + ' 23:59:59'
      })
    },
    dateChange(val, isReset = true) {
      const params = {
        startTime: dayjs(val[0]).format('YYYY-MM-DD') + ' 00:00:00',
        endTime: dayjs(val[1]).format('YYYY-MM-DD') + ' 23:59:59',
        aggregateType: this.dateValue
      }
      this.getLineList(params)
      if (isReset) this.dateValue = null
    },
    //选择本月/本年
    storeData(name) {
      let startTime, endTime
      let now = new Date()
      let nowMonth = now.getMonth()
      let nowYear = now.getFullYear()
      this.currentTime = name
      switch (name) {
        case 'day':
          startTime = new Date(nowYear, nowMonth, 1)
          endTime = new Date()
          // endTime = new Date(nowYear, nowMonth + 1, 0)
          break
        case 'month':
          startTime = new Date(nowYear, 0, 1)
          endTime = new Date()
          // endTime = new Date(nowYear, 11, 31)
          break
      }
      this.timeValue = [startTime, endTime]
      this.dateChange(this.timeValue, false)
    },
    getLineList(params) {
      this.emptyStatus = false
      this.$apis.home.analysisOrder(params).then(res => {
        this.orderDetail = res || {}
        this.lineList.lineList = res.trendData || []
        if (this.dateValue == 'month') {
          this.lineList.lineList.forEach(i => {
            i.aggregateTime = i.aggregateTime.substring(0, 7)
          })
        }
        const pieData = res.pieData || []
        const saleAmount = pieData
          .map(item => item.saleAmount)
          .reduce((prev, cur) => {
            return prev + cur
          }, 0)
        if (pieData.length)
          pieData.forEach(item => {
            this.orderList.forEach(i => {
              if (i.orderSource == item.orderSource) {
                i.saleAmount = item.saleAmount
                i.orderCount = item.orderCount
                i.radio = ((100 * item.saleAmount) / saleAmount).toFixed(2)
              }
            })
          })
        else this.emptyStatus = true
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.analysis-main {
  background: #fff;
  margin-top: 16px;
  .analysis-head {
    font-size: 24px;
    color: #333333;
    padding-left: 20px;
    height: 65px;
    line-height: 65px;
    border-bottom: 1px solid #f0f0fb;
  }
  .analysis-body {
    padding: 16px 20px;
    .analysis-bar-left {
      width: 280px;
      .analysis-bar-wrap {
        width: 280px;
        height: 147px;
        background-image: linear-gradient(179deg, #ecefff 0%, #f7faff 79%);
        border-radius: 4px;
        padding: 20px 24px;
        box-sizing: border-box;
        position: relative;
        .warp-desc {
          font-size: 14px;
          color: #666666;
        }
        img {
          width: 110px;
          height: 147px;
          position: absolute;
          right: 0px;
          top: 0px;
          z-index: 10;
        }
        .warp-num {
          color: #333333;
          font-size: 26px;
          line-height: 38px;
          font-weight: 600;
          margin-top: 15px;
        }
        .warp-price {
          font-size: 14px;
          color: #666666;
          margin-top: 26px;
          span {
            color: #2cca74;
            margin-left: 10px;
          }
        }
        &:first-child {
          margin-bottom: 16px;
        }
      }
    }
    .analysis-bar-right {
      flex: 1;
      border: 1px solid #f0f0fb;
      border-radius: 4px;
      padding: 20px;
      margin-left: 20px;
      height: 310px;
      box-sizing: border-box;
      overflow: hidden;
      .chart-title {
        font-size: 16px;
        color: #333333;
        text-align: center;
      }
      .chart-time {
        display: flex;
        justify-content: center;
        &-picker {
          margin-left: 10px;
        }
      }
    }
    .analysis-circle {
      margin-top: 16px;
      padding: 16px 20px;
      border: 1px solid #f0f0fb;
      border-radius: 4px;
      height: 262px;
      .analysis-circle-title {
        font-size: 16px;
        color: #333333;
        text-align: center;
        margin-bottom: 20px;
      }
      .empty-panel {
        text-align: center;
        color: #999999;
        font-size: 16px;
        .empty-img {
          width: 338px;
          height: 142px;
          margin: 20px auto;
        }
      }
      &-left {
        width: 360px;
      }
      &-right {
        flex: 1;
        .chart-item {
          display: grid;
          grid-template-columns: 20% 20% 20% 20% 20%;
          margin-top: 22px;
          font-size: 14px;
          color: #666666;
          &-1 {
            color: #333333;
            i {
              display: inline-block;
              width: 8px;
              border-radius: 50%;
              height: 8px;
              margin-right: 8px;
            }
          }
          &-2 {
            img {
              width: 31.5px;
            }
          }
        }
      }
    }
  }
}
</style>