微信小程序-手機(jī)號(hào)授權(quán)登錄

效果圖如下:


image.png

html頁面

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">微信授權(quán)手機(jī)號(hào)登錄</button>

js頁面

  getPhoneNumber:function(res){
    const encryptedData = res.detail.encryptedData
    const iv = res.detail.iv
    if (res.detail.encryptedData) {
      //用戶按了允許授權(quán)按鈕
      var that = this;
      wx.login({
        success(res) {
          const code = res.code
          // 根據(jù)小程序返回的密鑰傳給后端獲取真正的手機(jī)號(hào)
          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: '您點(diǎn)擊了拒絕授權(quán),將無法進(jìn)入小程序,請(qǐng)授權(quán)之后再進(jìn)入!!!',
        showCancel: false,
        confirmText: '返回授權(quán)',
        success: function (res) {
          if (res.confirm) {
            console.log('用戶點(diǎn)擊了“返回授權(quán)”');
          }
        }
      });
    }
  },
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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