這是第一次接觸微信小程序這塊,我是新手,希望各位大牛指點共同學習。
首先需要注意的幾點:
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>
我通過獲取這些事因為為了后面為微信小程序支付做准備。如果那里不對,請指教。謝謝大家
如果感覺不錯可以打賞一下
