这是第一次接触微信小程序这块,我是新手,希望各位大牛指点共同学习。
首先需要注意的几点:
1.域名必须是https,参考官方文档

这是获得openid重要的一个必须是https域名
2.域名24小时注册以后才可以使用,在微信小程序个人后台设置->开发设置进行request设置域名->在小程序项目查看
好了直接上源码
这是.js代码
Page({ data: { nickName:'', userInfoAvatar:'', sex:'', province:'', city:'' }, onLoad: function () { var that = this; wx.getUserInfo({ success: function(res){ // success that.setData({ nickName:res.userInfo.nickName, userInfoAvatar:res.userInfo.avatarUrl, province:res.userInfo.province, city:res.userInfo.city }) switch(res.userInfo.gender){ case 0: that.setData({ sex:'未知' }) break; case 1: that.setData({ sex:'男' }) break; case 2: that.setData({ sex:'女' }) break; } }, fail: function() { // fail console.log("获取失败!") }, complete: function(res) { // complete wx.login({ success: function (res) { console.log(res.code) that.getOpenId(res.code) } }); //console.log("获取用户信息完成!") } }) }, getOpenId: function (code) { // console.log(code); var that = this; wx.request({ url: "https://api.weixin.qq.com/sns/jscode2session?appid=wxdae72126adeb10b9&secret=b8189377612f70e21d81263164979fac&js_code=" + code + "&grant_type=authorization_code", data: {}, // 013FWyZK0xtFg42yGe1L0ycFZK0FWyZs method: 'GET', success: function (res) { console.log(res) // console.log(res.data.openid) }, fail: function () { // fail }, complete: function () { // complete } }) }, }) |
这是xml代码
<view class="classname">
<view class="test">
<view>用户昵称:{{nickName}}</view>
<view style="display:flex">
<view style="width:100px;line-height:100px;">用户头像:</view>
<image style="width:100px;height:100px;" src="{{userInfoAvatar}}"/>
</view>
<view>性别:{{sex}}</view>
<view>所在地址(省):{{province}}</view>
<view>所在地址(市):{{city}}</view>
</view>
</view>
我通过获取这些事因为为了后面为微信小程序支付做准备。如果那里不对,请指教。谢谢大家
如果感觉不错可以打赏一下
