1. 程式人生 > >微信小程式獲取使用者收貨地址 完整程式碼

微信小程式獲取使用者收貨地址 完整程式碼

獲取使用者收貨地址需要使用者點選授權,所以有兩種情況,確認授權、取消授權。

情況一,使用者第一次訪問使用者地址授權,並且點選確定授權。

情況二,使用者點選取消授權後,再次獲取授權

流程:(程式碼邏輯整理)

1.點選事件觸發函式,獲取使用者當前設定

2.根據使用者當前設定中的使用者授權結果,判斷是否包含收貨地址授權

3.如果包含收貨地址授權並且沒有取消過收貨地址授權,直接呼叫wx.chooseAddress(),獲取使用者收貨地址。

4.取消過收貨地址授權,呼叫wx.openSetting(),調起客戶端小程式設定介面讓使用者去開啟授權

4.1:使用者當前設定包含收貨地址授權但是使用者點選取消授權,呼叫wx.openSetting(),調起客戶端小程式設定介面讓使用者去開啟授權

4.2:使用者當前設定不包含收貨地址授權(說明是第一次開啟獲取使用者收貨地址資訊的授權),呼叫wx.chooseAddress(),獲取使用者收貨地址。

完整程式碼:

<button bindtap='aaaaaaa'>獲取地址</button>
  aaaaaaa() {
    wx.getSetting({
      success(res) {
        console.log("vres.authSetting['scope.address']:",res.authSetting['scope.address'])
        if (res.authSetting['scope.address']) {
          console.log("111")
          wx.chooseAddress({
            success(res) {
              console.log(res.userName)
              console.log(res.postalCode)
              console.log(res.provinceName)
              console.log(res.cityName)
              console.log(res.countyName)
              console.log(res.detailInfo)
              console.log(res.nationalCode)
              console.log(res.telNumber)
            }
          })
              // 使用者已經同意小程式使用錄音功能,後續呼叫 wx.startRecord 介面不會彈窗詢問
              
        } else {
          if (res.authSetting['scope.address'] == false) {
            console.log("222")
            wx.openSetting({
              success(res) {
                console.log(res.authSetting)
              
              }
            })
          } else {
            console.log("eee")
            wx.chooseAddress({
              success(res) {
                console.log(res.userName)
                console.log(res.postalCode)
                console.log(res.provinceName)
                console.log(res.cityName)
                console.log(res.countyName)
                console.log(res.detailInfo)
                console.log(res.nationalCode)
                console.log(res.telNumber)
              }
            })
          }
        }
      }
    })
  },