获取用户个人信息
<font color="#eee>代码如下:
wx.getUserProfile({
desc: '自定义描述信息',
success:(res)=>{
console.log(res.);
let {userInfo}=res;
console.log(userInfo.avatarUrl)//用户头像
console.log(userInfo.nickName)//用户名
}
})
res中的数据
获取用户手机号
1. 获取手机号
获取微信用户绑定的手机号,需先调用wx.login接口。
因为需要用户主动触发才能发起获取手机号接口,所以该功能不由 API 来调用,需用 button 组件的点击来触发。
注意:目前该接口针对非个人开发者,且完成了认证的小程序开放(不包含海外主体)。需谨慎使用,若用户举报较多或被发现在不必要场景下使用,微信有权永久回收该小程序的该接口权限。
- 使用方法
需要将 button 组件 open-type 的值设置为 getPhoneNumber,当用户点击并同意之后,可以通过 bindgetphonenumber 事件回调获取到微信服务器返回的加密数据, 然后在第三方服务端结合 session_key 以及 app_id 进行解密获取手机号。
注意
在回调中调用 wx.login
登录,可能会刷新登录态。此时服务器使用 code 换取的 sessionKey
不是加密时使用的 sessionKey
,导致解密失败。建议开发者提前进行 login
;或者在回调中先使用 checkSession
进行登录态检查,避免 login 刷新登录态。
<font color="eee >代码如下(示例):
getPhoneNumber (e) {
console.log(e.detail.errMsg)
console.log(e.detail.iv)
console.log(e.detail.encryptedData)
}
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">手机号</button>
json中的数据结构如下
{
"phoneNumber": "13580006666",
"purePhoneNumber": "13580006666",
"countryCode": "86",
"watermark":
{
"appid":"APPID",
"timestamp": TIMESTAMP
}
}
参数 | 说明 | 类型 |
---|---|---|
phoneNumber | String | 用户绑定的手机号(国外手机号会有区号) |
purePhoneNumber | String | 没有区号的手机号 |
countryCode | String | 区号 |
客服
只能在小程序端显示,或者真机调试
代码如下:
<view> <button open-type="contact">客服</button></view>
获取收获地址
<view class="container">
<form>
<view class="page-section">
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd">
<view class="weui-label">收货人姓名</view>
</view>
<view class="weui-cell__bd">
{{ addressInfo.userName }}
</view>
</view>
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd">
<view class="weui-label">邮编</view>
</view>
<view class="weui-cell__bd">
{{ addressInfo.postalCode }}
</view>
</view>
<view class="weui-cell weui-cell_input region">
<view class="weui-cell__hd">
<view class="weui-label">地区</view>
</view>
<view class="weui-cell__bd">
{{ addressInfo.provinceName }}
{{ addressInfo.cityName }}
{{ addressInfo.countyName }}
</view>
</view>
<view class="weui-cell weui-cell_input detail">
<view class="weui-cell__hd">
<view class="weui-label">收货地址</view>
</view>
<view class="weui-cell__bd">
{{ addressInfo.detailInfo }}
</view>
</view>
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd">
<view class="weui-label">国家码</view>
</view>
<view class="weui-cell__bd">
{{ addressInfo.nationalCode }}
</view>
</view>
<view class="weui-cell weui-cell_input">
<view class="weui-cell__hd">
<view class="weui-label">手机号码</view>
</view>
<view class="weui-cell__bd">
{{ addressInfo.telNumber }}
</view>
</view>
</view>
</view>
</form>
<view class="btn-area">
<button type="primary" bindtap="chooseAddress">获取收货地址</button>
</view>
</view>
Page({
data: {
addressInfo: null
},
chooseAddress() {
wx.chooseAddress({
success: (res) => {
this.setData({
addressInfo: res
})
},
fail: function(err) {
console.log(err)
}
})
}
})