微信小プログラムは共有機能を実装しています.

3643 ワード

one ShareAppMessageをパッケージ化し、個人とグループに分けます.
1.下のコードはutilツールjsに封入します.
function onShareAppMessage(title, path, callback, imageUrl) {
  //            
  let defaultImageUrl = '../../images/share.jpg';
  return {
    title: title,
    path: path,
    imageUrl: imageUrl || defaultImageUrl,
    success(res) { 
      console.log("    !");
      if (!res.shareTickets) {
      	//     
        api.shareFriend().then(() => {
          console.warn("shareFriendSuccess!");
          //             
          callback && callback();
        });
      } else {
      	//    
        let st = res.shareTickets[0];
        wx.getShareInfo({
          shareTicket: st,
          success(res) {
            let iv = res.iv
            let encryptedData = res.encryptedData;
            api.groupShare(encryptedData, iv).then(() => {
              console.warn("groupShareSuccess!");
              //             
              callback && callback();
            });
          }
        });
      }
    },
    fail: function (res) {
      console.log("    !");
    }
  };
}

//      
module.exports = {
  onShareAppMessage: onShareAppMessage
}
2.呼び出しの例
  /**
   *          
   */
  onShareAppMessage: function (e) {
    let nickName = '';
    try {
      nickName = getApp().globalData.userInfo.nickName;
    } catch (e) {
      console.warn("     !");
      nickName = '  ';
    }
    let title = `[${nickName}@ ]         ,         `;;
    let path = '/pages/index/index';
    let shareCallBack = () => {
    	//         
        //
        //
    };
    //            
    return util.onShareAppMessage(title, path, shareCallBack);
  },