微信小程序-获取用户信息并传到服务器中

一、获取用户信息

        在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();

  },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值