share.js 1.35 KB
/** 分享
 * @param {*} shareTitle 分享标题
 * @param {*} sharePath 分享页面路径 不传默认当前页
 * @param {*} shareImage 分享图片 不传默认当前页面截图
 * @param {*} shareQuery 分享页面参数
 * @param {*} noPageAuth 分享页面是否需要登录拦截
 * @returns
 */
class Share {
  constructor() {
    console.log('---------------- 分享初始化 ----------------')
    this.initPageProxy()
  }
  /**全局注册事件 */
  initPageProxy() {
    let thisPage = Page
    let _this = this
    Page = function (el) {
      try {
        el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments)
      } catch (el) {
        thisPage.apply(this, arguments)
      }
    }
  }
  /**设置全局分享 */
  pageLeaveProxy(page) {
    if (page.onShareAppMessage) {
      page.onShareAppMessage = function () {
        let {
          shareTitle,
          sharePath,
          shareImage,
          shareQuery,
          noPageAuth=true
        } = this,
        path = `/${this.route}`
        if (sharePath) path = `/${sharePath}`
        if (shareQuery) path += `?${shareQuery}`
        if (!noPageAuth) path = `/login/loading/loading?url=${encodeURIComponent(path)}`
        console.log(path)
        return {
          path,
          title: shareTitle,
          imageUrl: shareImage
        }
      }
    }

  }
}
new Share()