shareMixin.js 9.21 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
        })
      })
    },
    iosTimeFormat (time) {
      return time.replace(/\-/g, '/')
    },
    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, channelType } = this.getLocalStorage('dj_scrm_userInfo')
      let { tabIndex, entry } = this
      let { name } = this.$route
      let { userId, id, couponName, activityName, packageName, goodsName, goodsImage, discountType, shareType, marketingType } = i
      let query = `merchantId=${merchantId}&shareTime=${Date.now()}`
      if (sapCode) query = `${query}&storeCode=${sapCode}`
      const entryType = entry != 'normal'
      this.showLoading()
      let params = {
        coupon: {
          path: channelType == 2 ? 'packageMine/pages/boutique/gift/gift' : 'packageShopmall/packageMine/pages/boutique/gift/gift',
          desc: '您的专属服务顾问为您发来了优惠券,请点击领取~',
          imgUrl: 'http://img01.glsx.com.cn/upload/biz/dracMini-yh-10004.png'
        },
        activity: {
          path: channelType == 2 ? 'packageMall/pages/detail/detail' : 'packageShopmall/packageMall/pages/detail/detail',
          desc: '您有一个秒杀活动可参加',
          imgUrl: 'http://img01.glsx.com.cn/upload/biz/dracMini-ms-10004.png'
        },
        merchant: {
          path: channelType == 2 ? 'pages/tab/index/index' : 'pages/tab/shopMall/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 = channelType == 2 ? 'packageMall/pages/coupon/coupon' : 'packageShopmall/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) {
        // 1-普通秒杀;2-直播间秒杀;3-众筹
        switch (marketingType) {
          case 1:
            query = `isActivity=true&id=${id}&${query}&shareStaffName=${this.toUnicode(userName)}&shareStaffUuid=${uuid}`
            break
          case 2:
            /** 直播 */
            break
          case 3:
            shareType = 'h5'
            let { corpId, agentId } = this.getLocalStorage('dj_scrm_corpId')
            let link = `${baseURL}/scrm-wx-merchant-activity/#/`
            link = `${link}?${query}&id=${i.id}&shareStaffUuid=${uuid}&shareStaffName=${this.toUnicode(userName)}&shareStaffType=2&corpId=${corpId}&sharedTime=${Date.now()}`
            if (agentId) link = `${link}&agentId=${agentId}`
            h5Params = {
              imgUrl: i.posterImg || i.activityImg,
              desc: i.activitySharingWords || `限时大型众筹,活动时间${new Date(this.iosTimeFormat(i.startTime)).format('yyyy/M/d')}-${new Date(this.iosTimeFormat(i.endTime)).format('yyyy/M/d')}`,
              title: i.forwardTitle || i.activityName,
              link
            }
            break
          default:
            break
        }
        // } else {
        //   /**直播 */
        // }
      }
      if (name === 'merchant') {
        /**店铺 */
        switch (+tabIndex) {
          case 1:
            let isjt = i.packageType == 2
            const STORE_PATH = channelType == 2 ? 'packageEquity/pages/package/package' : 'packageShopmall/packageEquity/pages/package/package'
            const JT_PATH = channelType == 2 ? 'packageEquity/pages/open/open' : 'packageShopmall/packageEquity/pages/open/open'
            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 = channelType == 2 ? 'packageMall/pages/detail/detail' : 'packageShopmall/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 { corpId, agentId } = this.getLocalStorage('dj_scrm_corpId')
            let link = `${baseURL}/scrm-wx-merchant-activity/#/`
            link = `${link}?${query}&id=${i.id}&shareStaffUuid=${uuid}&shareStaffName=${this.toUnicode(userName)}&shareStaffType=2&corpId=${corpId}&sharedTime=${Date.now()}`
            if (agentId) link = `${link}&agentId=${agentId}`
            h5Params = {
              imgUrl: i.posterImg || i.activityImg,
              desc: i.activitySharingWords || `限时大型众筹,活动时间${new Date(this.iosTimeFormat(i.startTime)).format('yyyy/M/d')}-${new Date(this.iosTimeFormat(i.endTime)).format('yyyy/M/d')}`,
              title: i.forwardTitle || i.activityName,
              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: 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
    }
  }
}