sharePopup.js 4.4 KB
// components/sharePopup/sharePopup.js
import {imgUrlMixin} from '~/mixins/index'
import index_const from "~/pages/tab/index/index_const"
import Draw from '~/utils/Draw'
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [imgUrlMixin],
  properties: {
    data: {
      type: Object,
      observer(val) {
        let _this = this
        let thisPage = wx.$router.getPageInstance()
        _this.setData({
          navHeight: thisPage.data.navHeight
				})
        if (val.showSharePopup) {
            wx.$api.showLoading('处理中')
						val.boxImageHeight = 66
							// 计算弹窗图片区域最大高度
						wx.$util.getViewInfo('.layout', res => {
							_this.setData({
								postersBoxHeight: res.height * 0.7,
              })
              wx.getImageInfo({
                src: `${val.posterImg}?v=${Date.now()}`,
                success(e) {
                  _this.setData({
                    postersImageHeight: (e.height / e.width ) * 247
                  })
                  val.postersImageHeight = (e.height / e.width ) * 247
                  _this.init(val)
                },
                fail() {
                  wx.$api.showToast('生成失败')
                }
              })
						})
					
				}
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    USERINFO: wx.$storage.get('userInfo'),
    activityTypList: index_const.data.activityTypList
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async init(n) {
			n.activityName = n.activityName.length > 12 ? `${n.activityName.substring(0, 12)}...` : n.activityName
      const drawfun = new Draw({
        width: 279,
        height: n.postersImageHeight + 76,
        borderRadius: 8
      }, this),
      {
        userInfo
      } = wx.$storage.get(),
      res = await drawfun.begin({
        imageList: [{
            x: 0,
            y: 0,
            width: 279,
            height: n.boxImageHeight,
            url: `${this.data.IMGURL}taskDetail_bg.png`
          },
          {
            x: 12,
            y: 12,
            width: 20,
            height: 20,
            borderRadius: 40,
            url: userInfo.sta_avatarUrl ? userInfo.sta_avatarUrl : userInfo.weChatAvatarUrl ? userInfo.weChatAvatarUrl : `${this.data.IMGURL}default_avatar.png`
            // url: `${this.data.IMGURL}default_avatar.png`
          },
          {
            x: 16,
            y: n.boxImageHeight + 10,
            width: 247,
            height: n.postersImageHeight,
            url: n.posterImg
          },
        ],
        textList: [{
            x: 44,
            y: 14,
            fontSize: 14,
            color: '#2D2D33',
            text: userInfo.name,
            textBaseline: 'top'
          },
          {
            x: 50 + drawfun.getTextWidth(userInfo.name, 14),
            y: 15,
            fontSize: 12,
            color: '#6E6E7A',
            text: '邀请你参加',
            textBaseline: 'top'
          },
          {
            x: 44,
            y: 36,
            fontSize: 14,
            color: '#1B233D',
            text: n.activityName,
            textBaseline: 'top',
            fontWeight: 500
          },
          {
            x: 48 + drawfun.getTextWidth(n.activityName, 14) + 9,
            y: 39,
            fontSize: 10,
            color: '#4466F0',
            text: '众筹',
            textBaseline: 'top',
            border: {
              width: 1,
              color: '#8FA5FF',
              radius: 2,
              padding: [4, 5]
            }
          },
        ],
      })
      wx.hideLoading()
      this.setData({
        poster: res.path
      })
    },
    close() {
      let thisPage = wx.$router.getPageInstance()
      this.setData({
        poster: ""
      })
      thisPage.setData({
        sharePopupData: {
          showSharePopup: false
        }
      })
    },
    saveImage(e) {
      const filePath = this.data.poster,
			_this = this
      wx.saveImageToPhotosAlbum({
          filePath,
          success(){
            wx.$api.showToast('保存成功')
          },
          fail(err) {
            if(err.errMsg=='saveImageToPhotosAlbum:fail auth deny'){
              _this.triggerEvent('showAuth',true)
            }
            // const auth = _this.selectComponent('#auth')
            // auth.check()
          }
        }
      )
    },
    emptyFunction() {

    },
    authFail(e) {
      this.setData({
        showAuth: true
      })
    }
  }
})