summary.js 10.9 KB
// packageOutput/pages/summary/summary.js
import outputApi from '~/api/outputApi/index'
import { IMG_URL } from '~/utils/constantData'
import { getNowMonth } from '~/utils/format'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMGURL: IMG_URL,
    sceneCodeList: ['SC001', 'SC002', 'SC003', 'SC004'],
    merchantInfo: {},
    sceneObj: {
      scene1: null,
      scene2: null,
      scene3: null,
      scene4: null
    },
    sceneActive: 4,
    scenePriceList: [{
      total: 0
    }, {
      total: 0
    }, {
      total: 0
    }, {
      total: 0
    }],
    // 活动内容
    scrollLeft: 0,
    activityActive: 0,
    activityList: [],
    sceneActivityObj: {
      scene1: [],
      scene2: [],
      scene3: [],
      scene4: []
    },
    // 产值
    searchForm: {
      scene2Idx: 0,
      scene3Idx: 0,
      scene4Idx: 0
    },
    baseData: {
      timeList: ['全部', '本月', '本日'],
      actStatusList: ['全部', '申请中', '活动中', '已结束']
    }
  },

  // 场景切换
  handleSceneChange(e) {
    var idx = e.detail.sceneIdx
    this.setData({
      sceneActive: idx,
      activityActive: 0
    })
    if (!this.data.sceneActivityObj['scene' + this.data.sceneActive].length) {
      this.getSceneProStatus(this.data.sceneCodeList[this.data.sceneActive - 1])
      return true
    }
    if ([2, 3].includes(this.data.sceneActive)) {
      this.getCustomerDetail(false, false)
    } else {
      this.setCurActivity()
    }
  },

  // 活动切换
  handleActChange(e) {
    var idx = e.currentTarget.dataset.idx
    this.setData({
      activityActive: idx
    })
    if ([2, 3].includes(this.data.sceneActive)) {
      this.getCustomerDetail(false, false)
    }
  },

  setCurActivity() {
    this.setData({
      activityList: JSON.parse(JSON.stringify(this.data.sceneActivityObj['scene' + (this.data.sceneActive)]))
    })
    // activityActive为0时,触发置顶(左)
    if (this.data.activityActive == 0) {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  // 搜索条件切换
  handleSearch() {
    wx.showActionSheet({
      itemList: this.data.sceneActive != 4 ? this.data.baseData.timeList : this.data.baseData.actStatusList,
      success: res => {
        this.setData({
          ['searchForm.scene' + this.data.sceneActive + 'Idx']: res.tapIndex
        })
        this.data.sceneActive != 4 ? this.getCustomerDetail(false, false) : this.getActivityRevenue()
      }
    })
  },

  handleJump(e) {
    // console.log(this.data.activityList[this.data.activityActive])
    var params = {
      merchantInfo: this.data.merchantInfo,
      sceneActive: this.data.sceneActive
      // sceneCodeList: this.data.sceneCodeList
    }
    wx.$router.go('packageOutput.openBusiness', params)
  },

  getSceneProStatus(sceneCode, isFirst = false) {
    var params = {
      serviceMerchantId: this.data.merchantInfo.pMerchantId,
      merchant4sId: this.data.merchantInfo.merchantId,
      sceneCode: sceneCode
    }
    outputApi.getSceneProStatus(params).then(res => {
      this.setData({
        ['sceneActivityObj.scene1']: res[0].productList,
        ['sceneActivityObj.scene2']: res[1].productList,
        ['sceneActivityObj.scene3']: res[2].productList,
        ['sceneActivityObj.scene4']: res[3].productList
      })
      if (isFirst) {
        this.getCustomerDetail(isFirst)
      }
      if ([2, 3].includes(+this.data.sceneActive)) {
        this.getCustomerDetail()
      } else if (this.data.sceneActive == 4) {
        this.getActivityRevenue()
      } else {
        this.setCurActivity()
      }
    })
  },

  initTime() {
    let date = new Date()
    let today = date.getDate()
    let month = getNowMonth()
    const todayTime = {
      startTime: `${month}-${today} 00:00:00`,
      endTime: `${month}-${today} 23:59:59`
    }
    const monthTime = {
      startTime: `${month}-01 00:00:00`,
      endTime: `${month}-${today} 23:59:59`
    }
    const defaultTime = {
      startTime: '',
      endTime: `${month}-${today} 23:59:59`
    }
    this.setData({
      dataObj: {
        todayTime,
        monthTime,
        defaultTime
      }
    })
  },

  dealSearchTime() {
    var params = {}
    switch (this.data.searchForm['scene' + this.data.sceneActive + 'Idx']) {
      // 本月
      case 1:
        params = this.data.dataObj.monthTime
        break
      // 本日
      case 2:
        params = this.data.dataObj.todayTime
        break
    }
    return params
  },

  /** 1.1和1.2场景产品数据处理
   * 调同一个接口,返回两个服务下面的全部产品的数据
   */
  getCustomerDetail(isFirst = false, isChangeOtherTotal = true) {
    var params = this.dealSearchTime()
    outputApi.getCustomerDetail({ merchantId: this.data.merchantInfo.merchantId, ...params }).then(res => {
      var actList = this.data.sceneActivityObj['scene' + (isFirst ? 2 : this.data.sceneActive)]
      var actCodeList = actList.map(item => item.productCode)
      var curTotal = 0
      var otherTotal = 0
      var otherScene = isFirst ? 2 : (this.data.sceneActive == 2 ? 3 : 2)
      res.forEach(item => {
        var idx = actCodeList.findIndex(i => i == item.productCode)
        if (idx != -1) {
          var orderTotal = item.orderTotal
          if (item.orderTotal) {
            item.orderTotal.calCustomerUnitPrice = Math.round(orderTotal.totalSubOrderOrderMoney / orderTotal.upgradeCount).toFixed(0)
            item.orderTotal.calExperience = Math.round((orderTotal.experienceCount * 100) / orderTotal.toShopCount).toFixed(0)
            item.orderTotal.calTransform = Math.round((orderTotal.upgradeCount * 100) / orderTotal.experienceCount).toFixed(0)
          }
          actList[idx] = {
            ...actList[idx],
            orderTotal: item.orderTotal
          }
          curTotal = curTotal + (item.orderTotal?.totalSubOrderOrderMoney || 0)
        } else {
          otherTotal = otherTotal + (item.orderTotal?.totalSubOrderOrderMoney || 0)
        }
      })
      if (!isFirst) {
        this.setData({
          ['sceneActivityObj.scene' + this.data.sceneActive]: actList
        })
      }
      // 总金额
      this.setData({
        ['scenePriceList[' + (isFirst ? 1 : (this.data.sceneActive - 1)) + '].total']: curTotal.toFixed(2)
      })
      if (isChangeOtherTotal) {
        this.setData({
          ['scenePriceList[' + (isFirst ? 2 : (otherScene - 1)) + '].total']: otherTotal.toFixed(2)
        })
      }
      !isFirst && this.setCurActivity()
    })
  },

  dealSearchStatus() {
    var params = {}
    switch (this.data.searchForm['scene' + this.data.sceneActive + 'Idx']) {
      case 0:
        params.activityStatuList = [1, 2, 3]
        break
      // 申请中
      case 1:
        params.activityStatuList = [1]
        break
      // 活动中
      case 2:
        params.activityStatuList = [2]
        break
      // 已结束
      case 3:
        params.activityStatuList = [3]
        break
    }
    return params
  },

  /** 查询场景4的产值 */
  getActivityRevenue(isFirst = false) {
    wx.showLoading()
    var params = this.dealSearchStatus()
    outputApi.getActivityRevenue({ merchantId: this.data.merchantInfo.merchantId, ...params }).then(res => {
      // console.log(res)
      // console.log(this.data.sceneActivityObj, '++++++')
      var actList = this.data.sceneActivityObj['scene' + (isFirst ? 4 : this.data.sceneActive)]
      var total = 0
      res.forEach(item => {
        total = total + item.totalAmount
      })
      actList.forEach((item, idx) => {
        var curItem = res.find(k => k.goodsCategoryCode == item.productCode)
        if (curItem) {
          curItem.totalNum = curItem.activityList.length
          curItem.activityList.forEach(k => {
            // 时间
            k.time = k.startTime.slice(5).replace(/-/g, '/') + '~' + k.endTime.slice(5).replace(/-/g, '/')
            let startTimeFormat = k.startTime.slice(0,10).replace(/-/g, '/') + ' 00:00:00'
            let nowDateFormat = new Date().toLocaleDateString().replace(/-/g, '/') + ' 23:59:59'
            // activityStatus 1:未开始   2:进行中  3:已结束
            // 0-未开始;1-活动中;2-已结束
            switch (k.activityStatus) {
              case 1:
                k.interDays = parseInt((new Date(startTimeFormat).getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24)) + 1
                break
              case 2:
                let endTimeFormat = k.endTime.slice(0,10).replace(/-/g, '/') + ' 23:59:59'
                k.interDays = parseInt((new Date().getTime() - new Date(startTimeFormat).getTime()) / (1000 * 60 * 60 * 24)) + 1
                k.toEndDays = parseInt((new Date(endTimeFormat).getTime() - new Date(nowDateFormat).getTime()) / (1000 * 60 * 60 * 24)) + 1
                break
              case 3:
                k.interDays = parseInt((new Date(nowDateFormat).getTime() - new Date(k.endTime.replace(/-/g, '/')).getTime()) / (1000 * 60 * 60 * 24)) + 1
                break
            }
            k.suggestTxtList = []
            k.suggestList = []
            if (k.activityStatus == 2) {
              if (k.visitCount < 200) {
                k.suggestTxtList.push('距离活动结束剩余' + k.toEndDays + '天')
                k.suggestList.push('低于200个独立访客,触达车主的数量太少,建议与4S店关键人沟通注册员工的转发执行情况')
              }
              if (k.shareRate < 0.4) {
                k.suggestTxtList.push('分享员工占比低于40%')
                k.suggestList.push('分享员工占比低于40%,建议与4S店关键人沟通下注册员工的转发执行情况')
              }
              if (k.conversionRate < 0.05) {
                k.suggestTxtList.push('点击转化率低于5%')
                k.suggestList.push('点击转化率低于5%,建议调整该产品的品类话术和营销内容')
              }
              k.suggestTxt = k.suggestTxtList.join(',') + '。'
            }
          })
          actList[idx].outputData = curItem
        } else {
          actList[idx].outputData = []
        }
      })
      if (!isFirst) {
        this.setData({
          ['sceneActivityObj.scene' + this.data.sceneActive]: actList
        })
      }
      this.setData({
        ['scenePriceList[3].total']: total.toFixed(2)
      })
      !isFirst && this.setCurActivity()
    }).finally(() => {
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initTime()
    wx.$router.getData(obj => {
      var scene = {}
      Object.keys(this.data.sceneObj).forEach(item => {
        scene[item] = obj[item]
      })
      this.setData({
        merchantInfo: obj,
        sceneObj: scene,
        sceneCodeList: obj.sceneCodeList
      })

      this.getSceneProStatus(this.data.sceneCodeList[this.data.sceneActive - 1], true)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setNavigationBarTitle({
      title: this.data.merchantInfo.merchantName,
    })
  }
})