geeeger / blog

https://loofp.com
1 stars 0 forks source link

兼容一些场景下的分享 #23

Open geeeger opened 3 years ago

geeeger commented 3 years ago

点击进入查看

geeeger commented 3 years ago
import { isEmptyObject, isWx, isWxApp, isQQ, isQzone, isQQNews } from "./is";
import loader from './script-loader'
import service from './service'

function getWxConfig (url) {
  return someService
}

/**
 * @description shareSomething
 * @export
 * @param {Object} shareInfo
 * @param {string} shareInfo.pic
 * @param {string} shareInfo.title
 * @param {string} shareInfo.path for wxmp
 * @param {string} shareInfo.summary
 * @param {string} shareInfo.link h5 link
 */
export default function setShare(shareInfo) {
  if (shareInfo && !isEmptyObject(shareInfo)) {
    if (isWx()) {
      loader('//res2.wx.qq.com/open/js/jweixin-1.6.0.js', () => {
        if (isWxApp()) {
          wx.miniProgram.postMessage({
            data: {
              imageUrl: shareInfo.pic,
              title: shareInfo.title,
              path: shareInfo.path,
            },
          })
        }
        else {
          const shareConfig = {
            imgUrl: shareInfo.pic,
            title: shareInfo.title,
            desc: shareInfo.summary,
            dataUrl: '',
            type: '',
            link: shareInfo.link,
            success: () => {},
            fail: () => {},
            cancel: () => {},
          }

          getWxConfig(location.href.split('#')[0])
            .then(res => {
              const { data } = res
              if (data.ret === 0 && data.data) {
                const {appId, timestamp, noncestr, signature } = data.data

                wx.error(function (res) {
                  console.log('13213')
                  console.log(res)
                })
                wx.config({
                  // debug: true,
                  appId,
                  timestamp,
                  nonceStr: noncestr,
                  signature,
                  jsApiList: [
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                    'onMenuShareWeibo'
                  ]
                })

                wx.ready(() => {
                  wx.onMenuShareWeibo(shareConfig)
                  wx.updateTimelineShareData(shareConfig)
                  wx.updateAppMessageShareData(shareConfig)
                })
              }
            })
        }
      })
    }
    if (isQQ()) {
      loader('//open.mobile.qq.com/sdk/qqapi.js?_bid=152', () => {
        mqq.data.setShareInfo({
          title: shareInfo.title,
          desc: shareInfo.summary,
          share_url: shareInfo.link,
          image_url: shareInfo.pic,
        })
      })
    }
    if (isQzone()) {
      loader('//qzonestyle.gtimg.cn/qzone/phone/m/v4/widget/mobile/jsbridge.js?_bid=339', () => {
        QZAppExternal.setShare(() => {}, {
          type: 'share',
          image: shareInfo.pic,
          title: shareInfo.title,
          summary: shareInfo.summary,
          shareURL: shareInfo.link,
        })
      })
    }
    if (isQQNews()) {
      loader('//mat1.gtimg.com/auto/js/lib/newsJsBridge_160122.js', () => {
        window.getShareInfo = () => `{"can_share":"1"}`
        window.getNewsShareInfo = () => JSON.stringify({
          imgUrl: shareInfo.pic,
          title: shareInfo.title,
          desc: shareInfo.summary,
          linkUrl: shareInfo.link,
        })
      })
    }
  }
}