(四十六)获取code,openid,accessToken






code: {{code}}
openId: {{openid}}
unionId:{{unionid}} 
access_token:{{access_token}} 



Page({
    data: {
        code: "",
        openid: "",
        access_token: "",
        unionid: "",
        appid: "wx32eaff4484d917d9",
        secret: "148227e326f23a83991803d923363709",
    },
    onLoad: function (options) {
        // this.getCode();
        // this.getOpenid();
        // this.getAccess_token();
        this.getUserInfo();
    },
    getUserInfo() {
        wx.getUserInfo({
            success: function (res) {
                console.log(res)
            }
        })
    },
    getCode() {
        var that = this;
        wx.login({
            success: function (res) {
                console.log(res);
                var code = res.code;
                that.setData({
                    code: code
                })
            }
        });
    },
    getOpenid() {
        var that = this;
        wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
                appid: this.data.appid,
                secret: this.data.secret,
                js_code: this.data.code,
                grant_type: 'authorization_code'
            },
            success(res) {
                console.log(res);
                var openid = res.data.openid
                var unionid = res.data.unionid
                that.setData({
                    openid: openid,
                    unionid: unionid
                })
            }
        })
    },
    getAccess_token() {
        var that = this;
        wx.request({
            url: 'https://api.weixin.qq.com/cgi-bin/token',
            data: {
                appid: this.data.appid,
                secret: this.data.secret,
                grant_type: 'client_credential',
            },
            success(res) {
                console.log(res);
                var access_token = res.data.access_token
                that.setData({
                    access_token: access_token
                })
            }
        })
    },
    userInfoHandler(e) {
        console.log(e)
    }
})
/* pages/getUnionId/getUnionId.wxss */
.a{
    font-size:30rpx;
    padding:30rpx;
}
.btn {
    width: 80%;
    height:88rpx;
    line-height: 88rpx;
    border-radius: 10rpx;
    text-align: center;
    margin: 40rpx 10%;
    background: rgb(241, 165, 25);
    color: #fff;
    font-size:34rpx;
  }
  .btn_hover{
    color:rgb(241, 241, 241);
    background : rgb(224, 149, 8)
  }

你可能感兴趣的:((四十六)获取code,openid,accessToken)