outputCrowdfund.js 7.58 KB
// packageOutput/pages/summary/components/outputCrowdfund/outputCrowdfund.js
import wxqrcode from '~/utils/lib/wxqrcode'
import { IMG_URL } from '~/utils/constantData'
import { deleteSecond } from '~/utils/format'
import outputApi from '~/api/outputApi/index'
const IMAGES = Object.freeze({
  download: `${IMG_URL}/output/download.webp`
})
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderObj: {
      type: Object,
      value: () => { },
      observer(n) {
        n && this.dealStaffList()
      }
    }
  },

  options: {
    styleIsolation: 'shared'
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMGURL: IMG_URL,
    IMAGES,
    isShowTip: true,
    isShowPoster: false,
    orderFormList: [
      {
        label: '总场次',
        name: 'totalNum'
      },
      {
        label: '总订单',
        name: 'totalOrderCount'
      },
      {
        label: '成交金额',
        name: 'totalAmount',
        prefix: '¥'
      }
    ],
    merchantFormList: [
      {
        label: '预计活动时间',
        name: 'time'
      },
      {
        label: '实际活动时间',
        name: 'realTime'
      },
      {
        label: '订单数',
        name: 'orderCount',
        className: 'font-number'
      },
      {
        label: '产值',
        name: 'realAmount',
        className: 'red font-number',
        prefix: '¥',
        isNumber: true
      },
      {
        label: '预估产值',
        name: 'predictAmount',
        className: 'font-number',
        prefix: '¥',
        isNumber: true
      }
    ],
    tableHeadList: [
      {
        label: 'rankIdx',
        width: '40rpx'
      },
      {
        label: '业务员',
        name: 'shareName'
      },
      {
        label: '转发',
        name: 'shareCount'
      },
      {
        label: '浏览',
        name: 'browseCount'
      },
      {
        label: '成交',
        name: 'dealCount',
        width: '92rpx'
      },
      {
        label: '产值',
        name: 'totalAmount',
        prefix: '¥'
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    dealStaffList() {
      var list = this.data.orderObj
      list &&
        list.activityList?.forEach(item => {
          // 1-未开始;2-活动中;3-已结束
          let sliceNum = item.activityStatus == 3 ? 3 : 10
          item.showStaffList = item.staffList.length > sliceNum ? item.staffList.slice(0, sliceNum) : item.staffList
          item.staffList = item.staffList.length > sliceNum ? item.staffList.slice(sliceNum) : []
        })
      this.setData({
        orderObj: list
      })
    },
    handleMore(e) {
      var { idx } = e.currentTarget.dataset
      var list = this.data.orderObj
      var item = list.activityList[idx]
      list.activityList[idx].showStaffList = list.activityList[idx].showStaffList.concat(item.staffList.length > 10 ? item.staffList.slice(0, 10) : item.staffList)
      list.activityList[idx].staffList = item.staffList.length > 10 ? item.staffList.slice(10) : []
      this.setData({
        orderObj: list
      })
    },
    handleCopy(e) {
      var { idx } = e.currentTarget.dataset
      var currentItem = this.data.orderObj.activityList[idx]
      var qr = wxqrcode.createQrCodeImg(currentItem.activityUrl)
      const base64src = function (base64data) {
        return new Promise((resolve, reject) => {
          const [a, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
          if (!format) reject(new Error('ERROR_BASE64SRC_PARSE'))
          const filePath = `${wx.env.USER_DATA_PATH}/tmp_base64src.${format}`
          const buffer = wx.base64ToArrayBuffer(bodyData)
          wx.getFileSystemManager().writeFile({
            filePath,
            data: buffer,
            encoding: 'binary',
            success() {
              resolve(filePath)
            },
            fail() {
              reject(new Error('ERROR_BASE64SRC_WRITE'))
            }
          })
        })
      }
      base64src(qr).then(res => {
        wx.getImageInfo({
          src: res,
          success(obj) {
            wx.showShareImageMenu({
              path: obj.path
            })
          },
          fail() { }
        })
      })
    },
    handleJumpH5(e) {
      // const { item } = e.currentTarget.dataset
      // wx.$router.jumpH5(item.activityUrl)
    },
    async download(e) {
      const { item } = e.currentTarget.dataset
      // posterObj = this.selectComponent('#crowdPoster'),
      //   { options } = wx.$router.getPageInstance()
      // const { options } = wx.$router.getPageInstance()
      // console.log(options)
      // try {
      //   let flag = await posterObj.check(item)
      //   posterObj.setData({
      //     poster: flag,
      //     show: true
      //   })
      // } catch (error) {
      wx.$api.showLoading('加载中...')
      outputApi
        .getPosterUrl({
          id: item.activityId
        })
        .then(res => {
          item.posterUrl = res

          this.setData({
            crowdPosterItem: item,
            isShowTip: false
          })
        })
        .catch(() => {
          wx.$api.showToast('下载海报失败')
        })
      // }
    },
    /**长按海报 */
    async longtouch(e) {
      console.log('长按海报', e)
      let { activityId } = e.detail
      const res = await outputApi.reportPosterTrack({
        activityId
      })
      this.triggerEvent('posterTrack')
    },
    /**长按战报/喜报 */
    async handlePosterTouch(e) { },
    /**战报 */
    handleReport(e) {
      const { item, idx } = e.currentTarget.dataset
      wx.$api.showLoading('战报生成中')
      outputApi
        .getNewReportUrl({ activityId: item.activityId })
        .then(res => {
          // console.log(res)
          if (!res.url) {
            wx.$api.showToast('战报生成失败')
            return true
          }
          var posterItem = {
            ...item,
            posterUrl: res.url,
            updatedTime: deleteSecond(res.updatedDate),
            type: 'warReport'
          }
          this.setData({
            crowdPosterItem: posterItem,
            isShowTip: false
          })
          wx.$api.hideLoading()
          this.triggerEvent('handleZhanReport', { item: item, idx: idx, posterItem: posterItem })
        })
        .catch(() => {
          wx.$api.showToast('下载战报失败')
        })
        .finally(() => {
          wx.$api.hideLoading()
        })
    },
    /**喜报 */
    handleXiReport(e) {
      const { item } = e.currentTarget.dataset
      wx.$api.showLoading('喜报生成中')
      outputApi
        .getNewGoodUrl({ activityId: item.activityId })
        .then(res => {
          if (!res.goodUrl) {
            wx.$api.showToast('暂无喜报')
            return true
          }
          var posterItem = {
            ...item,
            posterUrl: res.goodUrl,
            updatedTime: res.goodReportDate ? deleteSecond(res.goodReportDate) : '',
            type: 'warReport'
          }
          this.setData({
            crowdPosterItem: posterItem
          })
          wx.$api.hideLoading()
        })
        .catch(() => {
          wx.$api.showToast('下载喜报失败')
        })
        .finally(() => {
          // wx.$api.hideLoading()
        })
      // var posterItem = {
      //   ...item,
      //   posterUrl: item.goodUrl,
      //   // date: item.goodReportDate,
      //   updatedTime: deleteSecond(item.goodReportDate),
      //   type: 'warReport'
      // }
      // this.setData({
      //   // posterItem: posterItem,
      //   // isShowPoster: true,
      //   crowdPosterItem: posterItem,
      //   isShowTip: false
      // })
    }
  }
})