小程序 之登錄 wx.login()


小程序的登錄關鍵在於使用wx.login()方法后,要到取到code值傳到后台,

再用小程序平台本帳號生成的appid+addsecret+code去微信接口服務取得用戶唯一標識后即可登錄【注意:此步驟必須要在本項目后台服務中才能成功,在小程序端是無效的】

appid+addsecret

下圖是官方的登錄流程圖:

 

 

 

放上代碼

showTopTips:function(e){ //登錄/注冊提交事件
    if (userName==''){
      app.toastShow(this, "請輸入手機號", "error");
    } else if (userPassword==''){
      app.toastShow(this, "請輸入驗證碼", "error");
    }else{
      var that = this
      wx.login({//調用獲取用戶openId
        success: function (res) {
          var loginDevice = 'W'; //唯一標識 = W + 臨時code值
          loginDevice = loginDevice + res.code //臨時code值
          var appid = '1100310183560349'; //appid wxf79825c96701f981
          var timestamp = Date.parse(new Date());//獲取當前時間戳 
          timestamp = timestamp / 1000;
          var version = '1.0'; //版本號
          var sign = 'erwlkrjlkwjelrjwlke'; //簽名
          var timestamp = Date.parse(new Date());//獲取當前時間戳 
          timestamp = timestamp / 1000;
          var loginChannel = '1003'; //登錄渠道:1001 ios手機 1002 android手機 1003 微信小程序 1004 手機H5
          wx.request({
            method: "post",
            url: 'http://uat.*****.com/xiao***/user/baseInfo/userLogin', //僅為示例,並非真實的接口地址
            data: '{"appId": "' + appid + '", "timestamp": ' + timestamp + ', "version": "' + version + '", "sign": "' + sign + '", "mobile": "' + userName + '","validateCode":"' + userPassword + '","loginChannel":"' + loginChannel + '","loginDevice":"' + loginDevice + '",}@#@1100310183560349' //"validateWay": 1, "validateType": 2
            ,
            dataType: "json",
            header: {
              'content-type': 'application/json' // 默認值
            },
            success: function (res) {
              var userIdEnc = res.data.data.userIdEnc;  //用戶唯一標識
              var loginDevice = res.data.data.loginDevice;
              wx.setStorageSync('userIdEnc', userIdEnc); //將userIdEnc存入本地緩存
              wx.setStorageSync('loginDevice', loginDevice);//將loginDevice存入本地緩存
            
              that.redirectToIndex();

              // that.setData({
              //   id_token: res.data.id_token,
              //   response: res
              // })
              // try {
              //   wx.setStorageSync('id_token', res.data.id_token)
              // } catch (e) {
              // }


              if (res.code == '0000') {
                console.log("注冊成功");
                // wx.redirectTo({
                //   url: '../../pages/index/index',
                // })
              } else if (res.code == '1002') { //超時
                that.errorShow('超時');
              } else if (res.code == '1002') { //帳號凍結
                that.errorShow('帳號凍結');
              } else {  //失敗
                that.errorShow('注冊/登錄失敗');
              }

            },
            fail: function (res) {
              //console.log(res.data);
              console.log('is failed')
            }
          })



        }, fail: function (res) {
          console.log('獲取臨時code失敗!' + res.errMsg)
        }
      })
      
    }
  },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM