国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用)

這篇具有很好參考價值的文章主要介紹了mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

微信小程序登錄鑒權(quán)流程如下:
wx.getUserProfilewx.getUserInfo接口被收回了,都不能彈出授權(quán)窗口,只能使用頭像昵稱填寫能力去獲取微信用戶信息。
在鑒權(quán)頁面如下操作
1、在onShow中調(diào)用微信登錄wx.login獲取到唯一的code(用來獲取openid);
2、根據(jù)wx.login獲取的code調(diào)用后臺接口來獲取openid;
3、根據(jù)獲取openid調(diào)用后臺接口獲取token是否存在token:有token進入首頁,沒有則返回登錄頁面以下將講解如何使用微信綁定的手機號碼一鍵登錄小程序)。

最終效果

mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用),mpvue 微信小程序組件封裝,mpvue微信小程序,微信小程序,mpVue,getPhoneNumber,微信授權(quán),手機一鍵登錄,小程序登錄流程

鑒權(quán)流程如下(鑒權(quán)頁面:auth.vue):

1、在onShow中調(diào)用微信登錄wx.login獲取到唯一的code

onShow() {
    // 調(diào)用微信登錄wx.login
    this.logining()
  },
methods: {
    logining() {
      wx.showLoading({
        title: '登錄微信用戶',
        mask: true
      })
      wx.login({
        success: (res) => {
          if (res.code) {
            wx.hideLoading()
            this.code = res.code
            // console.log('wx.login獲得code成功', res)
            // vuex中的openId/unionId若存在就直接調(diào)用獲取token接口
            if (this.openId) {
              this.getAccessToken(this.openId)
            } else {
              this.getOpenId({ code: this.code })
            }
          } else {
            console.log('獲取用戶登錄態(tài)失??!' + res.errMsg)
          }
        },
        fail(err) {
          wx.hideLoading()
          wx.showToast({
            title: 'wx.login失敗' + err,
            icon: 'none',
            duration: 1000
          })
        }
      })
    },
 }

2、根據(jù)wx.login獲取的code調(diào)用后臺接口來獲取openid

methods: {
 // 獲取openid
    async getOpenId(parameter) {
      wx.showLoading({
        title: '獲取openId',
        mask: true
      })
      const res = await this.$http('getOpenId', parameter)
      console.log('獲取openId', res)
      wx.hideLoading()
      if (res.success) {
        // 生成唯一值
        this.setOpenId(res.data.openid)
        // console.log('獲取openId---值', res.data.openid)
        this.getAccessToken(res.data.openid)
      }
    }
  }

3、根據(jù)獲取openid調(diào)用后臺接口獲取token

methods: {
 // 獲取token
   async getAccessToken(openId) {
      const res = await this.$http('getAccessToken', { openid: openId })
      // console.log('獲取token', res)
      if (res.success) {
        if (res.data) {
          this.isShowPhone = false
          this.setToken(res.data)
          this.$reLaunch('/pages/tabbarPage/main')
        } else {
         // 是否顯示 一鍵登錄按鈕
          this.isShowPhone = true
        }
      }
    },

如何使用微信綁定的手機號碼實現(xiàn)一鍵登錄小程序

1、微信小程序開發(fā)文檔地址

2、步驟及注意事項:

1、因為需要用戶主動觸發(fā)才能發(fā)起獲取手機號接口,所以該功能不由 API 來調(diào)用,需用 button 組件的點擊來觸發(fā)。
2、需要將button組件 open-type 的值設(shè)置為 getPhoneNumber,當用戶點擊并允許之后,可以通過 bindgetphonenumber 事件回調(diào)獲取到動態(tài)令牌code,根據(jù)code調(diào)用phonenumber.getPhoneNumber接口,消費code來換取用戶手機號。每個code有效期為5分鐘,且只能消費一次。
注:getPhoneNumber 返回的 codewx.login 返回的 code 作用是不一樣的,不能混用。

3、頁面使用

<template>
  <div class="auth flex-box flex-ver flex-col">
    <button
      class="btn flex-box flex-ver"
      v-if="isShowPhone"
      type="primary"
      open-type="getPhoneNumber"
      @getphonenumber="getPhoneNumber"
    >門店手機號一鍵登錄</button>
  </div>
</template>
<script>
export default {
  name: 'Auth',
  data() {
    return {
      isShowPhone: false // 是否顯示獲取手機號按鈕
    }
  },
  methods: {
    // 用戶按了允許授權(quán)按鈕
    async getPhoneNumber(e) {
      console.log('獲取手機號碼Code:', e.mp.detail)
      if (e.mp.detail.code) {
        // 用戶按了允許授權(quán)按鈕
        // console.log('獲取手機號碼:', e.mp.detail)
        this.isShowPhone = false
        const res = await this.$http('registerPhone', { code: e.mp.detail.code, openid: this.openId })
        if (res.success) {
          this.setToken(res.data)
          this.$reLaunch('/pages/tabbarPage/main')
        }
      } else {
        // 用戶按了拒絕按鈕
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '拒絕授權(quán)將無法注冊登陸小程序!',
          showCancel: false,
          success: (res) => {
            if (res.confirm) {
              this.isShowPhone = true
            }
          }
        })
      }
    }
  }
}
</script>

4、頁面效果截圖如下

1、初始頁面如下:

mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用),mpvue 微信小程序組件封裝,mpvue微信小程序,微信小程序,mpVue,getPhoneNumber,微信授權(quán),手機一鍵登錄,小程序登錄流程

2、點擊按鈕后如下:

mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用),mpvue 微信小程序組件封裝,mpvue微信小程序,微信小程序,mpVue,getPhoneNumber,微信授權(quán),手機一鍵登錄,小程序登錄流程

3、點擊不允許后如下:

mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用),mpvue 微信小程序組件封裝,mpvue微信小程序,微信小程序,mpVue,getPhoneNumber,微信授權(quán),手機一鍵登錄,小程序登錄流程

4、點擊允許后(直接進入首頁)如下:

mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用),mpvue 微信小程序組件封裝,mpvue微信小程序,微信小程序,mpVue,getPhoneNumber,微信授權(quán),手機一鍵登錄,小程序登錄流程

鑒權(quán)頁面:auth.vue代碼如下:

<template>
  <div class="auth flex-box flex-ver flex-col">
    <div class="loading-wrap flex-box flex-col flex-ver">
      <img class="launch_loading_logo" src="../../static/images/logo.png" />
      <p class="app-name">{{ appName }}</p>
    </div>
    <button
      class="btn flex-box flex-ver"
      v-if="isShowPhone"
      type="primary"
      open-type="getPhoneNumber"
      @getphonenumber="getPhoneNumber"
    >門店手機號一鍵登錄</button>
  </div>
</template>
<script>
import { mapMutations, mapGetters } from 'vuex'
import { APP_NAME } from '@/utils/settingConfig'
export default {
  name: 'Auth',
  data() {
    return {
      appName: APP_NAME,
      code: '', // wx.login拿回來的code
      isShowPhone: false // 是否顯示獲取手機號按鈕
    }
  },
  computed: {
    ...mapGetters([
      'openId'
    ])
  },
  onLoad(option) {
    // console.log('接受參數(shù)', option)
    wx.showLoading({
      title: '檢查應(yīng)用版本',
      mask: true
    })
    // 檢測IOS版本
    wx.getSystemInfo({
      success: (res) => {
        wx.hideLoading()
        // console.log(11, res)
        let versionIos = res.system.split(' ')[1].split('.')[0]
        let version = res.system.split(' ')[0].toLowerCase()
        // console.log(222, version, versionIos)
        if (version === 'ios' && versionIos < 10) {
          wx.showModal({
            title: '提示',
            content: `當前ios版本為${res.system.split(' ')[1]},請升級10以上再登錄!`,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                console.log('用戶點擊確定')
              }
            }
          })
        }
      }
    })
  },
  onShow() {
    // 調(diào)用微信登錄wx.login
    this.logining()
  },
  methods: {
    ...mapMutations({
      setToken: 'user/SET_TOKEN',
      setOpenId: 'user/SET_OPEN_ID'
    }),
    // 調(diào)用微信登錄wx.login獲取code
    logining() {
      wx.showLoading({
        title: '登錄微信用戶',
        mask: true
      })
      wx.login({
        success: (res) => {
          if (res.code) {
            wx.hideLoading()
            this.code = res.code
            // console.log('wx.login獲得code成功', res)
            // vuex中的openId/unionId若存在就直接調(diào)用獲取token接口
            if (this.openId) {
              this.getAccessToken(this.openId)
            } else {
              this.getOpenId({ code: this.code })
            }
          } else {
            console.log('獲取用戶登錄態(tài)失??!' + res.errMsg)
          }
        },
        fail(err) {
          wx.hideLoading()
          wx.showToast({
            title: 'wx.login失敗' + err,
            icon: 'none',
            duration: 1000
          })
        }
      })
    },
    // 獲取openid
    async getOpenId(parameter) {
      wx.showLoading({
        title: '獲取openId',
        mask: true
      })
      const res = await this.$http('getOpenId', parameter)
      console.log('獲取openId', res)
      wx.hideLoading()
      if (res.success) {
        // 生成唯一值
        this.setOpenId(res.data.openid)
        // console.log('獲取openId---值', res.data.openid)
        this.getAccessToken(res.data.openid)
      }
    },
    // 獲取token
    async getAccessToken(openId) {
      const res = await this.$http('getAccessToken', { openid: openId })
      // console.log('獲取token', res)
      if (res.success) {
        if (res.data) {
          this.isShowPhone = false
          this.setToken(res.data)
          this.$reLaunch('/pages/tabbarPage/main')
        } else {
          this.isShowPhone = true
        }
      }
    },
    // 用戶按了允許授權(quán)按鈕
    async getPhoneNumber(e) {
      console.log('獲取手機號碼Code:', e.mp.detail)
      if (e.mp.detail.code) {
        // 用戶按了允許授權(quán)按鈕
        // console.log('獲取手機號碼:', e.mp.detail)
        this.isShowPhone = false
        const res = await this.$http('registerPhone', { code: e.mp.detail.code, openid: this.openId })
        if (res.success) {
          this.setToken(res.data)
          this.$reLaunch('/pages/tabbarPage/main')
        }
      } else {
        // 用戶按了拒絕按鈕
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '拒絕授權(quán)將無法注冊登陸小程序!',
          showCancel: false,
          success: (res) => {
            if (res.confirm) {
              this.isShowPhone = true
            }
          }
        })
      }
    }
  }
}
</script>
<style lang="scss">
.auth {
  height: 100%;
  .loading-wrap {
    .launch_loading_logo {
      width: 80px;
      height: 80px;
      border-radius: 80px;
    }
    .app-name {
      margin-top: 20px;
    }
  }
  .btn {
    position: fixed;
    bottom: 35px;
    width: 90%;
    border-radius: 20px;
  }
}
</style>

源碼地址

gitHub地址

碼云地址

其他文章

Vue3 + Vite + Ts開源后臺管理系統(tǒng)模板


基于ElementUi或AntdUI再次封裝基礎(chǔ)組件文檔


基于Element-plus再次封裝基礎(chǔ)組件文檔(vue3+ts)文章來源地址http://www.zghlxwxcb.cn/news/detail-517001.html

到了這里,關(guān)于mpVue 微信小程序授權(quán)登錄流程(即登錄鑒權(quán)流程)及獲取手機號一鍵登錄教程(getPhoneNumber使用)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包