微信小程序-手機號授權登錄


<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">微信授權手機號登錄</button>
getPhoneNumber:function(res){
    const encryptedData = res.detail.encryptedData
    const iv = res.detail.iv
    if (res.detail.encryptedData) {
      //用戶按了允許授權按鈕
      var that = this;
      wx.login({
        success(res) {
          const code = res.code
          // 根據小程序返回的密鑰傳給后端獲取真正的手機號
          axios({
            url: '/wx/miniProgram/login', method: "POST", data: {
              code: code
            }
          }).then(({ res }) => {
            if(res.status === 0){
              axios({
                url: '/wx/miniProgram/getPhoneNumber', method: "POST", data: {
                  code: code,
                  encryptedData: encryptedData,
                  iv:iv
                }
              }).then(({ res }) => {
                  wx.switchTab({
                    url: '../index/index',
                    success: (res) => {}
                })
              })
            }
          })
          
        }
      })
    } else {
      //用戶按了拒絕按鈕
      wx.showModal({
        title: '警告',
        content: '您點擊了拒絕授權,將無法進入小程序,請授權之后再進入!!!',
        showCancel: false,
        confirmText: '返回授權',
        success: function (res) {
          if (res.confirm) {
            console.log('用戶點擊了“返回授權”');
          }
        }
      });
    }
  }


免責聲明!

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



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