shareMixin.js 7.35 KB
import { wxConfig, bigdatautilApi } from '@/api'
export default {
  data () {
    return {
      entry: 'normal'
    }
  },
  mounted () {
    if (this.$route.meta.isTab || this.$route.meta.isIndex) this.wxInit()
    // wxConfig
    //   .createMiniprogramUrlLink({
    //     path: 'packageEquity/pages/active/active',
    //     appId: 'wxfe82b8d65d07808f',
    //     query: '?ss=ss',
    //     envVersion: 'release'
    //   })
    //   .then(url => {
    //     console.log(url)
    //   })
  },
  methods: {
    wxInit () {
      let _this = this
      wxConfig.init(['shareAppMessage', 'sendChatMessage', 'getCurExternalContact', 'getContext']).then(() => {
        wxConfig.getContext().then(({ entry }) => {
          _this.$parent.entry = entry
          _this.entry = entry
        })
      })
    },
    showShareModel (i = {}) {
      let {
        tabIndex,
        $route: { name }
      } = this
      if (name != 'merchant') {
        if (tabIndex == 0 && i.remainAmount < 1) return
        if (tabIndex == 1 && i.residueAmount < 1) return
      }
      wxConfig
        .getCurExternalContact()
        .then(res => {
          console.log('当前外部联系人', res)
          i.is_contact = res
          this.sendMessage(i)
        })
        .catch(() => {
          this.sendMessage(i)
        })
    },
    sendMessage (i) {
      const { shopmall_appid, request: { baseURL } } = window.$fetchConfig
      let { merchantId, merchantName, userName, sapCode, uuid } = this.getLocalStorage('dj_scrm_userInfo')
      let { tabIndex, entry } = this
      let { name } = this.$route
      let { userId, id, couponName, activityName, packageName, goodsName, goodsImage, discountType, shareType } = i
      let query = `merchantId=${merchantId}&shareTime=${Date.now()}`
      if (sapCode) query = `${query}&storeCode=${sapCode}`
      const entryType = entry != 'normal'
      this.showLoading()
      let params = {
        'coupon': {
          path: 'packageMine/pages/boutique/gift/gift',
          desc: '您的专属服务顾问为您发来了优惠券,请点击领取~',
          imgUrl: 'http://img01.glsx.com.cn/upload/biz/dracMini-yh-10004.png'
        },
        'activity': {
          path: 'packageMall/pages/detail/detail',
          desc: '您有一个秒杀活动可参加',
          imgUrl: 'http://img01.glsx.com.cn/upload/biz/dracMini-ms-10004.png'
        },
        'merchant': {
          path: 'pages/tab/index/index',
          desc: '',
          imgUrl: 'https://img01.glsx.com.cn/weapp/resource/scrm-mobile/activity/miniprogram-cover.jpg'
        }
      }[name],
        title = {
          'coupon': couponName,
          'activity': activityName,
          'merchant': merchantName
        }[name],
        h5Params = {}
      if (name === 'coupon') { /**卡券 */
        if (tabIndex == 0) {
          query = `id=${id}&userId=${userId}&employName=${this.toUnicode(i.employeeName)}&${query}`
        } else {
          params.path = 'packageMall/pages/coupon/coupon'
          query = `couponId=${id}&${query}&issueName=${this.toUnicode(userName)}&issueDate=${new Date().getTime()}&shareSource=1&shareStaffName=${this.toUnicode(userName)}&shareStaffUuid=${uuid}`
          title = `${couponName}(${window.$discountType[discountType]}券)`
          this.couponShare(i)
        }
      }
      if (name === 'activity') {   /**活动 */
        if (tabIndex == 0) {
          query = `isActivity=true&id=${id}&${query}&shareStaffName=${this.toUnicode(userName)}&shareStaffUuid=${uuid}`
        } else {
          /**直播 */
        }
      }
      if (name === 'merchant') { /**店铺 */
        switch (+tabIndex) {
          case 1:
            let isjt = i.packageType == 2
            const STORE_PATH = 'packageEquity/pages/package/package'
            const JT_PATH = 'packageEquity/pages/open/open'
            // params.path = 'packageEquity/pages/package/package'
            params.path = isjt ? JT_PATH : STORE_PATH
            params.imgUrl = 'http://img01.glsx.com.cn/upload/biz/dracMini-qy-10004.png'
            params.desc = '您的专属客服顾问为您发来了会员套餐,点击查看哦'
            query = `id=${id}&${query}&shareStaffName=${this.toUnicode(userName)}&shareStaffUuid=${uuid}&packageType=${i.packageType}`
            title = packageName
            break;
          case 2:
            params.path = 'packageMall/pages/detail/detail'
            params.imgUrl = goodsImage
            params.desc = ''
            query = `id=${id}&${query}&shareStaffName=${this.toUnicode(userName)}&shareStaffUuid=${uuid}`
            title = goodsName
            break
          case 3:
            let link = `${baseURL}/scrm-wx-merchant-activity/#/?${query}&id=${i.id}&shareStaffUuid=${uuid}&shareStaffName=${this.toUnicode(userName)}&shareStaffType=2`
            h5Params = {
              imgUrl: i.posterImg || i.activityImg,
              desc: `限时大型众筹,活动时间${new Date(i.startTime).format('yyyy/M/d')}-${new Date(i.endTime).format('yyyy/M/d')}`,
              title: i.forwardTitle || i.activityName,
              // link: `${baseURL}/dj-scrm-wx-work-main-lwk/#/?id=${i.id}`
              link
            }
            break
          default:
            break;
        }
      }
      if (shareType == 'h5') {
        console.log(h5Params)
        if (entryType) this.sendChatMessage(h5Params, 'news', () => {
          bigdatautilApi.reportTrack(i.id)
        })
        else wxConfig
          .shareAppMessage(h5Params)
          .then(() => {
            this.hideLoading()
            bigdatautilApi.reportTrack(i.id)
          })
          .catch(() => {
            this.hideLoading()
          })

      } else {
        if (entryType) this.sendChatMessage({
          appid: shopmall_appid,
          title,
          imgUrl: params.imgUrl,
          page: `/${params.path}.html?${query}`
        })
        else this.shareAppMessage({
          path: params.path,
          query,
          appId:i.packageType==2? 'wxfe82b8d65d07808f':shopmall_appid
        }, {
          desc: params.desc,
          title,
          imgUrl: params.imgUrl
        })
      }
    },
    sendChatMessage (params, msgtype = 'miniprogram', callBack) {
      wxConfig
        .sendChatMessage({
          msgtype,
          [msgtype]: params,
          enterChat: false
        })
        .then(() => {
          this.hideLoading()
          if (callBack) callBack()
        })
        .catch(err => {
          console.log('当前会话', err)
          this.hideLoading()
        })
    },
    shareAppMessage (linkParams, messageParams) {
      wxConfig
        .createMiniprogramUrlLink(linkParams)
        .then(link => {
          wxConfig
            .shareAppMessage({
              link,
              ...messageParams
            })
            .then(() => {
              this.hideLoading()
            })
            .catch(() => {
              this.hideLoading()
            })
        })
    },
    toUnicode (chineseStr = '') {
      let unicodeStr = ''
      for (let i = 0, iLength = chineseStr.length; i < iLength; i++) {
        unicodeStr += '@' + chineseStr.charCodeAt(i).toString(16)
      }
      return unicodeStr
    },
    couponShare (item) {
      let { is_contact, id } = item
      let obj = {
        couponId: id,
        shareType: 1
      }
      if (is_contact) obj.userList = [
        {
          userId: is_contact
        }
      ]
      else obj.shareType = 0
    }
  }
}