Applet implements user address and user information authorization

Applet implements user address authorization

First, bindtap="getAddress"bind the click event through the button , and store the obtained user information in data; after storage, in order to avoid the pop-up window next time, you need to onLoaduse it to wx.getSettingcheck whether the user has been authorized. If authorized, you can directly obtain the data and No pop-up window appears.

Special note : If the user clicks to cancel the authorization , there will be a situation where the user clicks the button again and cannot react, so here needs to be processed, that is, to determine the authorization first and then decide what method to call.

The prompt message after clicking the button

  1. Get the user's shipping address
    1. Bind click event
    2. Call the built-in API of the applet to get the user's address wx.chooseAddress
  2. Get the user's permission status for the applet granted to get the addressscope
    1. If a user clicks get prompt box addresses determined , then authSetting scope.addressthe scopevalue of truedirectly calling api to obtain an address
    2. Assuming that the user has never called the address api, then scopethe value is to undefineddirectly call the api to get the address
    3. Assuming that the user clicks on the prompt box to get the delivery address to cancel , scopethe value is false, at this time, the user needs to be induced to open the authorization setting page wx.openSetting, and when the user regains the address authority, the API is called to obtain the address

Authorization settings page

After the user manually opens the permission, click again and the get address api will be called normally

onLoad: function () {
    //获得地址信息
   wx.getSetting({
     success: res => {
       if (res.authSetting['scope.address']) {
         wx.chooseAddress({
           success: res => {
             this.setData({
               hasAddress: true,
               address: res
             })
           }
         })
       }
     }
   })
   
  },
  // 获取地址权限
  getAddress(){
    wx.getSetting({
      success: (result)=>{
        const scopeAddress = result.authSetting["scope.address"]
        if(scopeAddress===true||scopeAddress===undefined){
          wx.chooseAddress({
            success: (result)=>{
              console.log(result)
              this.setData({
                address:result
              })
            },
          });
        }
        else{
          wx.openSetting({
            success:(result)=>{
              console.log(result)
            }
          })
        }
      }
    });
  }

Applet implements user information authorization

First, open-type="getUserInfo"obtain the permission from the user through the button , and then bindgetuserinfo="getUserInfo"store the obtained user information in the data by binding the callback function (the function name is customized); after the storage, in order to avoid the pop-up window next time, you need to onLoaduse the wx.getSettingview Whether the user has authorized, if authorized, can directly obtain data without pop-up window

  onLoad: function () {
    wx.getSetting({
     success: res => {
       console.log(res)
       if (res.authSetting['scope.userInfo']) {
         // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
         wx.getUserInfo({
           success: res => {
             console.log(res)
             this.setData({
               login: true,
               avatarUrl: res.userInfo.avatarUrl,
               userInfo: res.userInfo,
               cloudID: getApp().globalData.cloudID
             })
           }
         })
       }
     }
   })
  },
  getUserInfo() {
    wx.getSetting({
      success: (res) => {
        const scopeUserInfo = res.authSetting["scope.userInfo"]
        if (scopeUserInfo === true) {
          wx.getUserInfo({
            success: (res) => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo,
                cloudID: getApp().globalData.cloudID
              })
            },
          });
        } 
      }
    });
 }

to sum up

  1. Obtain the user's address, bind the click event and consider three cases of user click.
  2. To obtain user permissions, you can use the open-typeproperties of the button

Like it if it's useful !

Published 128 original articles · 52 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/weixin_44523860/article/details/105332127