messageBox.js 6.72 KB
// pages/mineDetail/messageBox/messageBox.js
import mineApi from '~/api/mineApi/index'
// import { handleCopy } from '~/utils/utils_wx'
import outputApi from '~/api/outputApi/index'
import { deleteSecond } from '~/utils/format'
// import drawQrcode from '~/utils/lib/weapp-qrcode.js'
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    navStyle: 'background-color: rgba(0,0,0,0);',
    loginType: 0,
    agree: true,
    tabIndx: 0,
    text: '',
    tabList: ['战报', '通知'],
    type: {
      warReport: {
        pageNum: 1,
        pageSize: 10,
        type: 0,
        list: 'warReportList'
      },
      message: {
        pageNum: 1,
        pageSize: 10,
        type: 1,
        list: 'messageList'
      }
    },
    // findKey: '',
    warReportList: [],
    messageList: [],
    serviceMerchantId: ''
  },
  attached() {
    // this.ewmChange()
    // this.initTime()
    let { userInfo } = wx.$storage.get()
    let merchantId = userInfo.subLoginUser.merchantId
    this.setData({
      serviceMerchantId: userInfo.subLoginUser.merchantId
      // serviceMerchantId: 12580
    })
    this.getMessageList('warReport')
    this.getMessageList('message')
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 实例化二维码canvas
    // ewmChange(url) {
    //   let size = {}
    //   size.w = (wx.getSystemInfoSync().windowWidth / 750) * 600
    //   size.h = size.w
    //   var that = this

    //   drawQrcode({
    //     width: size.w,
    //     height: size.h,
    //     canvasId: 'myQrcode',
    //     text: url || that.data.text
    //     // v1.0.0+版本支持在二维码上绘制图片
    //   })
    // },
    preView(e) {
      let { valueurl } = e.currentTarget.dataset
      // console.log(e)
      // console.log(valueUrl)
      // let urls = []
      // urls.push(valueUrl)
      // // wx.$router.jumpH5(valueUrl)
      // console.log(urls)
      // console.log([valueUrl])
      wx.$api.previewImage({
        urls: [valueurl],
        current: valueurl
      })
    },
    showPoster(e) {
      const { item } = e.currentTarget.dataset
      let updatedTime = deleteSecond(item.createdTime)

      this.setData({
        // findKey: 'crowdFundActivityId',
        posterItem: {
          activityName: item.activityName,
          posterUrl: item.valueUrl,
          updatedTime,
          // crowdFundActivityId: item.valueUrl,
          type: 'warReport'
        }
      })
    },
    async copyLink(e) {
      const { item } = e.currentTarget.dataset
      // posterObj = this.selectComponent('#poster')
      // this.setData({
      //   findKey: 'activityId'
      // })
      // try {
      //   let flag = await posterObj.check(item)
      //   posterObj.setData({
      //     poster: flag,
      //     show: true
      //   })
      // } catch (error) {
      wx.$api.showLoading('加载中...')
      outputApi
        .getPosterUrl({
          id: item.crowdFundActivityId
        })
        .then(res => {
          item.posterUrl = res
          item.type = 'motherChainPoster'
          console.log(item)
          this.setData({
            posterItem: item,
            isShowTip: false
          })
        })
        .catch(() => {
          wx.$api.showToast('下载海报失败')
        })
      // }
    },
    share(e) {
      // console.log(e)
      let that = this
      const dpr = wx.getSystemInfoSync().pixelRatio
      wx.canvasToTempFilePath({
        canvasId: 'myQrcode',
        x: 0,
        y: 0,
        width: 600,
        height: 600,
        destWidth: 600 * dpr, // width*屏幕像素密度
        destHeight: 600 * dpr, // height*屏幕像素密度
        fileType: 'jpg',
        quality: 1, //图片的质量,目前仅对 jpg 有效。取值范围为 (0, 1],不在范围内时当作 1.0 处理。
        success: function (res) {
          var tempFilePath = res.tempFilePath
          wx.getImageInfo({
            src: tempFilePath,
            success(obj) {
              wx.showShareImageMenu({
                path: obj.path
              })
            },
            fail() {}
          })
        },
        fail: function (res) {
          console.log(res)
        }
      })
    },
    navbarReady(e) {
      let { height, statusBarHeight } = e.detail
      console.log(e)
      this.setData({
        tabBarStyle: height / 2 + statusBarHeight
      })
    },
    async refreshList(e) {
      console.log(e)
      let { type } = this.data
      let { typename } = e.currentTarget.dataset
      type[typename].pageNum = 1
      let res = await this.getMessageList(typename)
      let toastObj = {
        success: {
          title: '刷新成功',
          icon: 'success'
        },
        fail: {
          title: '刷新失败',
          icon: 'error'
        }
      }
      wx.$api.showToast({
        ...toastObj[res],
        duration: 1000
      })
    },
    scrollonReachBottom(e) {
      console.log(e)
      let { typename } = e.currentTarget.dataset
      let { type } = this.data
      // 判断当前页是否是末页
      console.log(type[typename].pages == type[typename].currentPage)
      if (type[typename].pages == type[typename].pageNum) return wx.$api.showToast('列表已到底')
      type[typename].pageNum = type[typename].pageNum + 1
      this.setData({
        type
      })
      this.getMessageList(typename)
    },
    changeTabIndx(e) {
      let { detail } = e
      this.setData({
        tabIndx: detail
      })
    },
    MatchCeil(num) {
      return Match.ceil(num)
    },
    getMessageList(typeName) {
      let that = this
      return new Promise((resolve, reject) => {
        let { serviceMerchantId, type, warReportList, messageList } = this.data
        let list = type[typeName]['list']
        // console.log('list', this.data[list])
        // return
        let params = {
          serviceMerchantId: serviceMerchantId,
          ...type[typeName]
        }
        // console.log(Math.ceil(2.8))
        wx.$api.showLoading('加载中...')
        mineApi
          .messageBoxPage(params)
          .then(result => {
            console.log(result)
            let { count, data, pageSize, pageNum, pages } = result
            const listName = `${type[typeName]['list']}`
            type[typeName]['pages'] = pages
            type[typeName]['pageNum'] = pageNum
            console.log(listName)
            this.setData({
              [listName]: type[typeName].pageNum == 1 ? data : [...this.data[list], ...data],
              type,
              triggered: false
            })
            resolve('success')
          })
          .catch(err => {
            reject('fail')
          })
          .finally(() => {
            wx.$api.hideLoading()
          })
      })
    }
  }
})