属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
code | string |
属性 | 类型 | 说明 |
errMsg | string | 错误信息 |
errno | Number |
wx.login({success (res) {if (res.code) {//发起网络请求wx.request({url: 'https://example.com/onLogin',data: {code: res.code}})} else {console.log('登录失败!' + res.errMsg)}}})
属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
wx.getAuthCode({scopes:["CUSTOM_SCOPE_X","CUSTOM_SCOPE_Y"],success (res) {if (res.code) {//发起网络请求wx.request({url: 'https://example.com/onLogin',data: {code: res.code}})} else {console.log('登录失败!' + res.errMsg)}}})
属性 | 类型 | 默认值 | 必填 | 说明 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
wx.checkSession({success () {//session_key 未过期,并且在本生命周期一直有效},fail () {// session_key 已经失效,需要重新执行登录流程wx.login() //重新登录}})
属性 | 类型 | 默认值 | 必填 | 说明 |
lang | string | en | 否 | 显示用户信息的语言,合法值为: en:英文 zh_CN:简体中文 zh_TW:繁体中文 |
desc | string | - | 是 | 声明获取用户个人信息后的用途,不超过30个字符 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 默认值 | 必填 | 说明 |
lang | string | en | 否 | 显示用户信息的语言,合法值为: en:英文 zh_CN:简体中文 zh_TW:繁体中文 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
userInfo | UserInfo | 用户信息对象 |
<!-- 如果只是展示用户头像昵称,可以使用 <open-data /> 组件 --><open-data type="userAvatarUrl"></open-data><open-data type="userNickName"></open-data><!-- 需要使用 button 来授权登录 --><button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button><view wx:else>请升级superapp版本</view>
Page({data: {canIUse: wx.canIUse('button.open-type.getUserInfo')},onLoad: function() {// 查看是否授权userAvatarUrlwx.getSetting({success (res){if (res.authSetting['scope.userInfo']) {// 已经授权,可以直接调用 getUserInfo 获取头像昵称wx.getUserInfo({success: function(res) {console.log(res.userInfo)}})}}})},bindGetUserInfo (e) {console.log(e.detail.userInfo)}})
// 必须是在用户已经授权的情况下调用wx.getUserInfo({success: function(res) {var userInfo = res.userInfovar nickName = userInfo.nickNamevar avatarUrl = userInfo.avatarUrl}})
属性 | 类型 | 说明 |
nickName | string | 用户昵称 |
avatarUrl | string | 用户头像图片的 URL。URL 最后一个数值代表正方形头像大小(有 0、46、64、96、132 数值可选,0 代表 640x640 的正方形头像,46 表示 46x46 的正方形头像,剩余数值以此类推。默认132),用户没有头像时该项为空。若用户更换头像,原有头像 URL 将失效 |
gender | number | 用户性别。不再返回,合法值为: 0:未知 1:男性 2:女性 |
country | string | 用户所在国家。不再返回 |
province | string | 用户所在省份。不再返回 |
city | string | 用户所在城市。不再返回 |
language | string | 显示 country,province,city 所用的语言。强制返回 “zh_CN”,合法值为: en:英文 zh_CN:简体中文 zh_TW:繁体中文 |
<button open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"><image src="{{userHead}}" class="userHead" mode="aspectFill" /></button>
Page({data: {userHead: defaultAvatar,},onChooseAvatar(e) {console.log('onChooseAvatar===', e)const { avatarUrl } = e.detail;if (avatarUrl) {this.setData({userHead: avatarUrl,})}},})
<view class="settingItem"><text class="caption">昵称</text><input type="nickname" class="value" placeholder="请输入昵称" /></view>
<buttonclass="button"type="default"size="mini"open-type="getPhoneNumber"bindgetphonenumber="handleGetPhoneNumber">获取手机号码</button>
Page({handleGetPhoneNumber(e) {const { code, errMsg } = e.detailif (code) {// 发起网络请求从后台获取手机号wx.request({url: 'https://example.com/getPhoneNumber',data: {code},success: res => console.log('phone:', res)})} else {console.error(errMsg)}},});
<buttonclass="button"type="default"size="mini"open-type="getEmailAddress"bindgetemailaddress="handleGetEmailAddress">获取邮箱</button>
Page({handleGetEmailAddress(e) {const { code, errMsg } = e.detailif (code) {// 发起网络请求从后台获取邮箱wx.request({url: 'https://example.com/getEmail',data: {code},success: res => console.log('mail:', res)})} else {console.error(errMsg)}},});
属性 | 类型 | 默认值 | 必填 | 说明 |
timeStamp | string | - | 是 | 时间戳,从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间 |
nonceStr | string | - | 是 | 随机字符串,长度为32个字符以下 |
package | string | - | 是 | 统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*** |
signType | string | - | 是 | 签名算法,应与后台下单时的值一致 通常是: RSA |
paySign | string | - | 是 | |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
wx.requestPayment({timeStamp: '',nonceStr: '',package: '',signType: 'RSA',paySign: '',success (res) {/* res通常由superapp返回给小程序前端,结构由superapp自定义* 按照标准实现,支付成功后 会在这里收到 res.errMsg = ‘requestPayment:ok’* 建议小程序在成功回调里,再调用小程序后台接口进行支付状态的确认。确保支付回调信息已经同步到小程序后台*/if(res.errMsg === 'resquestPayment:ok'){// send request to miniprogram backend to comfirm payment state}},fail (err) {// 该错误内容通常由superapp返回给小程序前端,需要由小程序前端进行报错后的相关用户引导提示}})
属性 | 类型 | 默认值 | 必填 | 说明 |
tmplIds | Array | - | 是 | 需要订阅的消息模板的id的集合,一次调用最多可订阅3条消息。消息模板id在控制台[小程序管理-订阅消息]中配置。每个 tmplId 对应的模板标题需要不相同,否则会被过滤。 |
success | function | - | 否 | 接口调用成功的回调函数 |
fail | function | - | 否 | 接口调用失败的回调函数 |
complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
errMsg | String | 接口调用成功时 errMsg 值为 'requestSubscribeMessage:ok' |
[TEMPLATE_ID: string] | String | [TEMPLATE_ID]是动态的键,即模板 id,值包括'accept'、'reject'、'ban'、'filter' 'accept':表示用户同意订阅该条 id 对应的模板消息 'reject':表示用户拒绝订阅该条 id 对应的模板消息 'ban':表示已被后台封禁 'filter':表示该模板因为模板标题同名被后台过滤 例如 { errMsg: "requestSubscribeMessage:ok", zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: "accept"} 表示用户同意订阅zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE这条消息 |
属性 | 类型 | 说明 |
errMsg | String | 接口调用失败错误信息 |
errno | Number | 接口调用失败错误码 |
errCode | errMsg | 说明 |
10001 | TmplIds can't be empty | 参数传空了 |
10002 | Request list fail | 网络问题,请求消息列表失败 |
10003 | Request subscribe fail | 网络问题,订阅请求发送失败 |
20001 | No template data return, verify the template id exist | 没有模板数据,一般是模板 ID 不存在或者和模板类型不对应导致的 |
20002 | Templates type must be same | 模板消息类型 既有一次性的又有永久的 |
20003 | Templates count out of max bounds | 模板消息数量超过上限 |
20004 | The main switch is switched off | 用户关闭了主开关,无法进行订阅 |
20005 | This mini program was banned from subscribing messages | 小程序被禁封 |
20013 | Reject DeviceMsg Template | 不允许通过该接口订阅设备消息 |
wx.requestSubscribeMessage({tmplIds: [''],success: (res) => {console.log('requestSubscribeMessage===success', res)}})
文档反馈