shareMixin.js 2.58 KB
import { wxConfig, marketingApi } from '@/api'
export default {
  data () {
    return {
      entry: 'normal'
    }
  },
  mounted () {
    this.wxInit()
  },
  methods: {
    wxInit () {
      let _this = this
      wxConfig.init(['getContext', 'shareAppMessage', 'shareToExternalMoments'])
    },
    shareToExternalMoments (i) {
      this.sendMessage(i, true)
    },
    sendMessage (i, isPyq) {
      const { shopmall_appid, envVersion } = window.$fetchConfig
      const path = 'pages/psdView/psdView'
      const { merchantId, uuid, sapCode } = this.getLocalStorage('dj_scrm_userInfo')
      const { entry } = this
      const { materialName, id, titleUrl, materialTitle, description } = i
      const imgUrl = 'https://img01.glsx.com.cn/weapp/resource/scrm-mobile/test/test.jpg'
      let query = `merchantId=${merchantId}&id=${id}&staffUuid=${uuid}&shareTime=${Date.now()}`
      if (sapCode) query = `${query}&storeCode=${sapCode}`
      wxConfig.createMiniprogramUrlLink({
        path,
        query,
        appId: shopmall_appid,
        envVersion
      })
        .then(link => {
          this.showLoading()
          if (isPyq) {
            console.log('朋友圈')
            wxConfig.shareToExternalMoments({
              attachments: [
                {
                  msgtype: 'link',    // 消息类型,必填
                  link: {
                    title: materialTitle || materialName,        // H5消息标题
                    imgUrl: titleUrl || imgUrl,    // H5消息封面图片URL
                    url: link  // H5消息页面url 必填
                  },
                }
              ]
            }).then(() => {
              this.sendReadBuriedPoint({ id, merchantId, uuid })
              this.hideLoading()
            })
              .catch(err => {
                this.hideLoading()
              })
          } else {
            let params = {
              link,
              title: materialTitle || materialName,
              desc: description || '',
              imgUrl: titleUrl || imgUrl
            }
            this.shareAppMessage(params, { id, merchantId, uuid })
          }
        })
    },
    sendReadBuriedPoint ({ id, merchantId, uuid }) {
      marketingApi.sendReadBuriedPoint({
        materialId: id,
        type: 1,
        merchantId,
        staffUuid: uuid
      })
    },
    shareAppMessage (params, sendParams) {
      wxConfig
        .shareAppMessage(params)
        .then(() => {
          this.sendReadBuriedPoint(sendParams)
          this.hideLoading()
        })
        .catch(() => {
          this.hideLoading()
        })
    }
  }
}