首页 小程序 微信小程序常用api

微信小程序常用api


获取用户个人信息


<font color="#eee>代码如下:

 wx.getUserProfile({     desc: '自定义描述信息',     success:(res)=>{       console.log(res.);       let {userInfo}=res;       console.log(userInfo.avatarUrl)//用户头像       console.log(userInfo.nickName)//用户名     }   })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

res中的数据
在这里插入图片描述

获取用户手机号


1. 获取手机号

获取微信用户绑定的手机号,需先调用wx.login接口。
因为需要用户主动触发才能发起获取手机号接口,所以该功能不由 API 来调用,需用 button 组件的点击来触发。

注意:目前该接口针对非个人开发者,且完成了认证的小程序开放(不包含海外主体)。需谨慎使用,若用户举报较多或被发现在不必要场景下使用,微信有权永久回收该小程序的该接口权限。

  1. 使用方法

需要将 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)  }
  • 1
  • 2
  • 3
  • 4
  • 5
 <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">手机号</button>
  • 1

json中的数据结构如下

{    "phoneNumber": "13580006666",    "purePhoneNumber": "13580006666",    "countryCode": "86",    "watermark":    {        "appid":"APPID",        "timestamp": TIMESTAMP    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
参数说明类型
phoneNumberString用户绑定的手机号(国外手机号会有区号)
purePhoneNumberString没有区号的手机号
countryCodeString区号

客服


只能在小程序端显示,或者真机调试
代码如下:
<view> <button open-type="contact">客服</button></view>
  • 1

在这里插入图片描述

获取收获地址

<view >="container">  <form>    <view >="page-section">      <view >="weui-cells weui-cells_after-title">        <view >="weui-cell weui-cell_input">          <view >="weui-cell__hd">            <view >="weui-label">收货人姓名</view>          </view>          <view >="weui-cell__bd">            {{ addressInfo.userName }}          </view>        </view>        <view >="weui-cell weui-cell_input">          <view >="weui-cell__hd">            <view >="weui-label">邮编</view>          </view>          <view >="weui-cell__bd">            {{ addressInfo.postalCode }}          </view>        </view>        <view >="weui-cell weui-cell_input region">          <view >="weui-cell__hd">            <view >="weui-label">地区</view>          </view>          <view >="weui-cell__bd">            {{ addressInfo.provinceName }}            {{ addressInfo.cityName }}            {{ addressInfo.countyName }}          </view>        </view>        <view >="weui-cell weui-cell_input detail">          <view >="weui-cell__hd">            <view >="weui-label">收货地址</view>          </view>          <view >="weui-cell__bd">            {{ addressInfo.detailInfo }}          </view>        </view>          <view >="weui-cell weui-cell_input">          <view >="weui-cell__hd">            <view >="weui-label">国家码</view>          </view>          <view >="weui-cell__bd">            {{ addressInfo.nationalCode }}          </view>        </view>        <view >="weui-cell weui-cell_input">          <view >="weui-cell__hd">            <view >="weui-label">手机号码</view>          </view>          <view >="weui-cell__bd">            {{ addressInfo.telNumber }}          </view>        </view>      </view>    </view>  </form>    <view >="btn-area">    <button type="primary" bindtap="chooseAddress">获取收货地址</button>  </view></view>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
Page({  data: {    addressInfo: null  },  chooseAddress() {    wx.chooseAddress({      success: (res) => {        this.setData({          addressInfo: res        })      },      fail: function(err) {        console.log(err)      }    })  }})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。