activeBox.js 9.26 KB
// pages/tab/index/components/activeBox/activeBox.js
import {imgUrlMixin} from '~/mixins/index'
import ActivityApi  from "~/api/activityApi/index"
import { showEmoji } from '~/utils/emoji'
import {
  domain
} from '~/utils/request/http'
Component({
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true
  },
  behaviors: [imgUrlMixin],
  properties: {
    activeList: {
			type: Array,
			observer(newVal) {
      }
    },
    nothing: {
      type: Boolean
    },
    activeDateIndex: {
      type: Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    activityOverviewList: ['shareData', 'browseData', 'visitorData', 'orderData'],
    backStyle: [
      'background-image: linear-gradient(180deg, #FFFFFF 72%, #FFEDEF 100%);margin: 0 16rpx 16rpx 0;',
      'background-image: linear-gradient(179deg, #FFFFFF 76%, #FFF8E6 100%);',
      'background-image: linear-gradient(180deg, #FFFFFF 72%, #EBF1FF 100%);margin: 0 16rpx 0 0;',
      'background-image: linear-gradient(180deg, #FFFFFF 72%, #EBFEFF 100%);'
    ],
    activeList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 图片加载完成
    imageLoaded(event) {
      if (event.detail.width) {
        this.setData({imageLoaded: true})
      }
    },
    // 展开收起
    collapseClick(e) {
      let { key, flag } = e.currentTarget.dataset
      let { activeList } = this.data
      this.setData({
        activeDateIndex: 0
      })
      activeList.forEach((val, idx) => {
        if (key == idx) {
          if (flag && !val.missionList) {
            val.isloading = true
            ActivityApi.getActiveTaskList({activityId: val.id}).then(res => {
              val.isloading = false
              res.forEach(a => {
                a.platformMerchantMissionResponseList && a.platformMerchantMissionResponseList.forEach(k => {
                  k.detailInfo = {activityId: val.id, imgUrlList: []}
                  k.platformMerchantMissionMaterialResponseList && k.platformMerchantMissionMaterialResponseList.forEach(i => {
                    // materialType 素材类型1文案 2图片 3视频 4专属链接
                    if (i.materialType == 1) { // 文案
                      k.detailInfo.textContent = i.materialContent
                      k.detailInfo.filterTextContent = i.materialContent ? `<view style="word-break: break-all;">${showEmoji(i.materialContent)}</view>` : undefined
                    }
                    if (i.materialType == 2 || i.materialType == 3) { // 图片视频
                      k.detailInfo.imgUrlList.push({
                        url: i.materialContent,
                        type: i.materialType == 2 ? 'image' : 'video'
                      })
                    }
                    if (i.materialType == 4) { // 链接
                      k.detailInfo.linkUrl = val.activityUrl
                    }
                  })
                })
              })
              val.missionList = res
              val.newtaskCalenderList = this.calenderFilter(JSON.parse(JSON.stringify(val.missionList)) || [], 7)
              let d = new Date()
              let nowDate = (d.getMonth() + 1).toString().padStart(2, "0") +  "月" + d.getDate().toString().padStart(2, "0") + "日"
              let ind = 0 // 下标值
              val.calenderActiveIndex = 0
              val.newtaskCalenderList.forEach((v, index) => {
                v.forEach(k => {
                  k.time = (new Date(k.activityTime).getMonth() + 1).toString().padStart(2, "0") +  "月" + new Date(k.activityTime).getDate().toString().padStart(2, "0") + "日"
                  k.ind = ind
                  let completeStatusNum = 0
                  k.platformMerchantMissionResponseList.forEach(val => {
                    if (val.completeStatus == 2) completeStatusNum++
                  })
                  if (d.getTime() > new Date(`${k.activityTime.replace(/-/g,'/')} 23:59:59`).getTime()) k.status = 2
                  if (nowDate == k.time) {
                    val.calenderActiveIndex = index
                    this.setData({activeDateIndex : ind})
                  }
                  // status 1过去日期任务全部完成 2过去日期任务有未完成的 3当前日期或未来日期
                  if (completeStatusNum == v.length && d.getTime() > new Date(`${k.activityTime.replace(/-/g,'/')} 23:59:59`).getTime()) {
                    k.status = 1
                  } else if (d.getTime() > new Date(`${k.activityTime.replace(/-/g,'/')} 23:59:59`).getTime()) {
                    k.status = 2
                  } else {
                    k.status = 3
                  }
                  ind++
                })
              })
              val.isCollapse = false
              let itemVal = 'activeList['+idx+']'
              this.setData({
                [itemVal]: val
              })
            }).catch(err => {
              val.isloading = false
              let itemVal = 'activeList['+idx+']'
              this.setData({
                [itemVal]: val
              })
            })
          } else if (flag && val.missionList) { // 已请求数据 展开默认选中当天
            val.newtaskCalenderList = this.calenderFilter(JSON.parse(JSON.stringify(val.missionList)) || [], 7)
            let d = new Date()
            let nowDate = (d.getMonth() + 1).toString().padStart(2, "0") +  "月" + d.getDate().toString().padStart(2, "0") + "日"
            let ind = 0 // 下标值
            val.calenderActiveIndex = 0
            val.newtaskCalenderList.forEach((v, index) => {
              v.forEach(k => {
                k.time = (new Date(k.activityTime).getMonth() + 1).toString().padStart(2, "0") +  "月" + new Date(k.activityTime).getDate().toString().padStart(2, "0") + "日"
								k.ind = ind
								let completeStatusNum = 0
								k.platformMerchantMissionResponseList.forEach(i => {
									if (i.completeStatus == 2) completeStatusNum++
								})
                if (d.getTime() > new Date(`${k.activityTime.replace(/-/g,'/')} 23:59:59`).getTime()) k.status = 2
                if (nowDate == k.time) {
                  val.calenderActiveIndex = index
                  this.setData({activeDateIndex : ind})
								}
								// status 1过去日期任务全部完成 2过去日期任务有未完成的 3当前日期或未来日期
								if (completeStatusNum == v.length && d.getTime() > new Date(`${k.activityTime.replace(/-/g,'/')} 23:59:59`).getTime()) {
									k.status = 1
								} else if (d.getTime() > new Date(`${k.activityTime.replace(/-/g,'/')} 23:59:59`).getTime()) {
									k.status = 2
								} else {
									k.status = 3
								}
                ind++
              })
            })
            val.isCollapse = false
          } else {
            val.isCollapse = !flag
          }
        } else {
          val.isCollapse = true
        }
      })
      this.setData({
        activeList
      })
    },

    // 任务日历数据处理
    calenderFilter (arr, length) {
      var _arr = [] 
      while (arr.length) {
        _arr.push(arr.splice(0, length))
      }
      return _arr
    },

    toUnicode (chineseStr = '') {
      let unicodeStr = ''
      for (let i = 0, iLength = chineseStr.length; i < iLength; i++) {
        unicodeStr += '@' + chineseStr.charCodeAt(i).toString(16)
      }
      return unicodeStr
    },

    filterJumpUrl(item) {
      const userStorage = wx.$storage.get('userInfo')
      return `${domain}/scrm-wx-merchant-activity/#/?id=${item.id}&merchantId=${userStorage.merchantId}&shareStaffUuid=${userStorage.uuid}&shareStaffName=${this.toUnicode(userStorage.name)}&isExclusive=1&exclusiveUuid=${userStorage.uuid}&shareStaffType=2&sharedTime=${Date.now()}`
    },

    // 分享活动(复制链接)
    copyActivityUrl(e) {
      let { item } = e.currentTarget.dataset
      let thisPage = wx.$router.getPageInstance()
      wx.$api.showLoading()
      ActivityApi.getActivityPosters({id: item.id}).then(res => {
        // var code= QR.createQrCodeImg(res.activityUrl);
        // /*code是指图片base64格式数据*/
        // //声明文件系统
        // const fs = wx.getFileSystemManager();
        // //随机定义路径名称
        // var times = new Date().getTime();
        // var codeimg = wx.env.USER_DATA_PATH + '/' + times + '.png';
        // //将base64图片写入
        // fs.writeFile({
        //   filePath: codeimg,
        //   data: code.slice(22),
        //   encoding: 'base64',
        //   success: () => {
        //     thisPage.setData({
        //       sharePopupData: {
        //         imagePath: codeimg,
        //         activityName: res.activityName,
        //         address: res.activityAddressDetailName,
        //         posterImg: res.posterImg,
        //         showSharePopup: true
        //       },
        //     })
        //   }
        // });
        thisPage.setData({
          sharePopupData: {
            posterImg: res,
            activityName: item.activityName,
            showSharePopup: true
          },
        })
      })
		},
    // 活动详情
    goToDetail(e) {
      let { item } = e.currentTarget.dataset
      let h5Url = this.filterJumpUrl(item)
      console.log(h5Url)
      wx.$router.jumpH5(h5Url)
    },
    // 活动战报
    jump(e) {
      let { item } = e.currentTarget.dataset
      wx.$router.go('packageActivity.warReport', item)
    }
  }
})