小程序获取openid unionid session_key

  • .wxml

<button bindtap="paytap">授权</button>

  • .js
Page({
    paytap: function () {
        var that = this
        //登陆获取code
        wx.login({
            success: function (res) {
                that.getOpenId(res.code)

            }
        });
    },
    getOpenId: function (code) {
        var that = this;
        wx.request({
            url: "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code",
            data: {
                appid: '',
                secret: '',
                js_code: code,
                grant_type: 'authorization_code'
            },
            method: 'GET',
            success: function (res) {
                console.log(res)
                wx.setStorageSync("sessionid", res.header["Set-Cookie"])
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    }
})

posted @ 2018-06-11 16:34  清风白水  阅读(806)  评论(0编辑  收藏  举报