一、获取用户信息
在wxml文件中获取用户信息必须用户同意才可以获取到信息。
js对wx的参数进行设置,if else设置按钮可见性
data: {
userInfo: {},
hasUserInfo: false,
canIUseGetUserProfile: false,
info: "?",
list:0,
},
userInfo: {}包含用户的各项信息。
<view class="container">
<view class="userinfo">
<block wx:if="{{!hasUserInfo}}">
<button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 点击添加功德数 </button>
<button wx:else open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 点击添加功德数 </button>
</block>
<block wx:else>
<button type="primary" size="default" bindtap='houduanButton2'>点击添加功德数</button>
</block>
</view>
</view>
hasUserInfo为true才能点击正常 bindtap的按钮功能
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
onLoad(options) {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
wx.getUserProfile({
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
二、传值到后台
houduanButton2: function () {
this.setData({
list: this.data.list + 1
}),
console.log(this.data.userInfo.nickName),
wx.request( {
url: "http://localhost:9082/weixin/setPunna",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
data:{
poon:this.data.list,
nickname:this.data.userInfo.nickName,
},
dataType:"json",
complete: function( res ) {
console.log("数据发送成功")
}
})
},
wx.request是微信开发的固定函数,做各种请求设置。
that.setData 对属性赋值。getData获取值。
生命周期函数-类似create()
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
console.log("生命周期函数运行");
this.houduanButton1();
},