微信小程序 分享頁面做登錄攔截採坑記錄

(一)

上面是分享頁面代碼,用變量存儲需要跳轉的頁面路徑。

分享路徑直接跳轉到登錄頁,將需要跳轉的路徑以參數形式帶到登錄頁,注意需要用encodeURLComponent()轉義

 onShareAppMessage: function (ops) {
    let url = encodeURIComponent('../StatisticalEle/StatisticalEle? 
              id='+this.data.activeIndex);
    return {
        title: '數據分析報表', //轉發的標題。當前小程序名稱
        path: 'pages/login_dn/login_dn?url='+url,
        imageUrl: '',//自定義圖片路徑 支持PNG及JPG。顯示圖片長寬比是 5:4。
        success:function(res){
          wx.showToast({
            title: res+'成功',
            icon: 'none',
            duration:1000
          })
        }
    }
  },

 

(二)

在登錄頁面接收傳過來的參數。並且通過decodeURLComponent()方法編譯。並且將參數分割出來。

因爲我跳轉的tabbar頁面 不能直接帶參跳轉,只能將頁面參數存到app的全局變量裏。

if(options.url){
      let url = decodeURIComponent(options.url)
      let theRequest ={};
      let str = url.split("?"); //以“?”分割url
        for(let i=0;i<str.length;i++){
          theRequest[str[i].split("=")[0]] = decodeURI(str[i].split("=")[1]);
        }
      app.globalData.showActionIndex = theRequest.id
      console.log(theRequest)
      this.setData({
        redirectURL:str[0]
      })
 }

(三)登錄跳轉

 if (res.data.success && _this.data.redirectURL) {
            wx.switchTab({
              url: _this.data.redirectURL
            })
    }
           

(四)展示頁面處理,判斷並設置相關數據。

onShow(){
    if(app.globalData.showActionIndex){
      console.log()
      if(app.globalData.showActionIndex == 2 ){
        // 分析報告頁面
        this.setData({
          activeIndex:app.globalData.showActionIndex,
          scrollpage:false,
        })
      }else{
        this.setData({
          activeIndex:app.globalData.showActionIndex,
          scrollpage:true,
        })
      }
  }
  },

頁面分享測試,要記得及時清除手機本地的開發版本,因爲分享的頁面,代碼不會自動更新,需要新掃碼更新手機開發版本。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章