微信小程序-自建小程序服务器 c# 小程序获取用户信息
只看这里:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
废弃的接口
微信小程序获取用户信息的接口多次变更,如下:
方式一:open-data展示用户信息`不推荐`
方式二: wx.getUserInfo `不推荐`
方式三:open-type="getUserInfo" `不推荐`
方式四:wx.getUserProfile `不推荐`
小程序用户头像昵称获取规则调整公告: https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01
具体使用见:
https://juejin.cn/post/7080880853617737764
Note 20240120
1.wx.getUserProfile和wx.getUserInfo都不能获取用户昵称和头像
2.在开发环境可以正常调用 wx.getUserProfile,并且可以正常返回;可是当我发布到正式环境时,却无法调用,按钮点击死活没有反应,我不明白什么意思,现在新用户没法使用我们小程序了。
3.我今天才发现这点,还以为所有用户都能回传“微信用户”和默认头像就不改代码了,没想到有些低版本的用户没法兼容这个调整,直接就没反应,导致新用户不能注册。
现在的解决方案20240120
添加一个页面让用户点击头像和名称自己填写或者用新的接口分别获取。
解决方案
用官方最新的接口,那两接口已经废弃
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html
本地设置基础库版本改成2.21.4
用户自己填写。自己上传
最新的接口
https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01
<!--index.wxml-->
<scroll-view class="scrollarea" scroll-y type="list">
<view class="container">
<view class="userinfo">
<block wx:if="{{canIUseNicknameComp && !hasUserInfo}}">
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
<image class="avatar" src="{{userInfo.avatarUrl}}"></image>
</button>
<view class="nickname-wrapper">
<text class="nickname-label">昵称</text>
<input type="nickname" class="nickname-input" placeholder="请输入昵称" bind:change="onInputChange" />
</view>
</block>
<block wx:elif="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
<view wx:else> 请使用2.10.4及以上版本基础库 </view>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
</view>
</view>
</scroll-view>
// miniprogram/pages/my/address/index.js
Page({
/**
* 页面的初始数据
*/
data: {
address: {
name: '',
phone: '',
detail: ''
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var self = this;
wx.getStorage({
key: 'address',
success: function (res) {
self.setData({
address: res.data
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
formSubmit(e) {
const value = e.detail.value;
if (value.name && value.phone && value.detail) {
wx.setStorage({
key: 'address',
data: value,
success() {
wx.navigateBack();
}
})
} else {
wx.showModal({
title: '提示',
content: '请填写完整资料',
showCancel: false
})
}
}
})
source:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html
https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01