微信小程序获取code

Page({
  data: {
    code: "",
    openid: "",
    session_key: ""
  },
  login: function () {
    var that = this;
    wx.login({  
      success: function (log) {
        console.log(log.code);
        that.setData({
          code: log.code
        });
        wx.request({
          url: '',
          data: { code: log.code },
          success: function (data) {
            console.log(data);
            that.setData({
              openid: data.data.openid,
              session_key: data.data.session_key
            });
          }
        })
      }
    })
  }
})
<!--index.wxml-->
<view class="container">
    <text class="txt-css txt-left">{{mytest}}</text>
    <button bindtap='login'>获取code与openID</button>
<text>code:{{code}}</text>
<text>openID:{{openid}}</text>
<text>session_key:{{session_key}}</text>
</view>

你可能感兴趣的:(前端,Demo)