小程序自帶分享功能和通過button按鈕實(shí)現(xiàn)分享功能

1.首先應(yīng)該自定義一個(gè)button按鈕 自帶屬性 open-type="share"

1.png

wxml代碼:
<button open-type="share">分享給朋友,邀請(qǐng)?jiān)S愿</button>
//記?。涸O(shè)置樣式必須在button里面設(shè)置內(nèi)樣式

2.點(diǎn)擊微信小程序自帶的分享按鈕實(shí)現(xiàn)轉(zhuǎn)發(fā)效果。同時(shí)滿足以下條件:若是分享點(diǎn)進(jìn)去的頁(yè)面,實(shí)現(xiàn)返回鍵的按鈕圖片改變--房子圖標(biāo)。若是直接查看的頁(yè)面將不改變圖標(biāo),同時(shí)也可回到主頁(yè)面。

image.png

  wxml代碼:
  //1.首先先做一個(gè)if else的判斷判斷
  <image wx:if="{{isShare}}" class="back-btn" src="/images/home/school_back@2x.png" bindtap="backHome"></image>
  <image wx:else class="back-btn" src="/images/common/nav_back_def@2x.png" bindtap="backHome"></image>
  //切記:一定要記得把id的參數(shù)傳進(jìn)去,防止分享點(diǎn)進(jìn)去沒有值。
  js代碼:
 //1.在onload里面先變量及賦值
    var that = this
    var id = options.id
    var reseller = options.reseller
    if (!reseller) {
      reseller = 0
    }
    var normal = true
    if (options.normal == 'false') {
      normal = false
    }
    that.setData({
      id: id,
      user: app.globalData.userInfo,
      reseller: reseller,
      normal: normal,
    })
    var shared = false
    var isShare = options.isShare
    if (isShare == "1") {
      shared = true}
    this.setData({
      id: id,
      user: app.globalData.userInfo,
      isShare: shared
    })
    var recommender = options.recommender
    if (recommender != null && recommender != undefined && recommender != "") {
      app.globalData.recommender = recommender
    }
//2.在onShareAppMessage里面寫路徑以及傳遞參數(shù)。
  onShareAppMessage: function () {
    var user = this.data.user
    if (user) {
      return {
        path: '/pages/wish/detail?recommender=' + user.uid + '&isShare=1' + "&id=" + this.data.id
      }
    } else {
      return {
        path: '/pages/wish/detail?id=' + this.data.id + '&isShare=1'
      }
    }
  }
//3.點(diǎn)擊左上角的按鈕 都將回到主頁(yè)面。
 backHome: function (e) {
    if (this.data.isShare) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容