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 onLoad
use it to wx.getSetting
check 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.
- Get the user's shipping address
- Bind click event
- Call the built-in API of the applet to get the user's address
wx.chooseAddress
- Get the user's permission status for the applet granted to get the address
scope
- If a user clicks get prompt box addresses determined , then
authSetting scope.address
thescope
value oftrue
directly calling api to obtain an address - Assuming that the user has never called the address api, then
scope
the value is toundefined
directly call the api to get the address - Assuming that the user clicks on the prompt box to get the delivery address to cancel ,
scope
the value isfalse
, at this time, the user needs to be induced to open the authorization setting pagewx.openSetting
, and when the user regains the address authority, the API is called to obtain the address
- If a user clicks get prompt box addresses determined , then
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 onLoad
use the wx.getSetting
view 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
- Obtain the user's address, bind the click event and consider three cases of user click.
- To obtain user permissions, you can use the
open-type
properties of the button
Like it if it's useful !