微信小程序 分享功能

 /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    return  this.getPageShareInfo(currentPage);
   
  },
  onShareTimeline : function () {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    return  this.getPageShareInfo(currentPage);
  },
  getPageShareInfo(page) {
    return function (res) {
      // --- 步骤 1: 从页面 data 中读取分享信息 ---
      const { shareTitle, shareImage, sharePath, shareQuery } = page.data;

      // --- 步骤 2: 智能生成分享内容 ---
      const title = shareTitle || '默认分享标题';
      
      // 处理路径和参数
      const basePath = sharePath || `/${page.route}`; // 优先使用 sharePath,否则使用当前页面路径
      const queryParams = shareQuery || page.options; // 优先使用 shareQuery,否则使用页面跳转时的参数
      const fullPath = this._buildPathWithParams(basePath, queryParams);

      // 处理图片
      const imageUrl = shareImage || '/images/default-share.jpg';

      return {
        title: title,
        path: fullPath,
        imageUrl: imageUrl,
      };
    };
  },

 

posted @ 2025-11-23 13:18  鲨鱼大王  阅读(0)  评论(0)    收藏  举报