美文网首页
小程序获取openid

小程序获取openid

作者: 秀萝卜 | 来源:发表于2021-11-26 10:30 被阅读0次

记得之前写过一次,在来一次吧
由于‘https://api.weixin.qq.com’不能加入request白名单
这个openid只能由后台或者云小程序获取,
这段代码可以在不限制域名的情况,测试开发使用

// 微信授权登录
    weixinLogin() {
        // 获取code
        var that = this
        Util.showLoading()
        wx.login({
            success: (res) => {
                that.get_openid(res.code)
            },
        })
    },
    // 获取openid
    get_openid(code) {
        var that = this
        var appid = config.appid
        var secret = config.secret
        wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',//请求微信服务器获取openid
            data: {},
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log("Openid为:" + res.data.openid) // oHGn55L0ayMhtYxzAiEYHZT4_EIU
                that.login(res.data.openid)
            },
        })
    },

相关文章

网友评论

      本文标题:小程序获取openid

      本文链接:https://www.haomeiwen.com/subject/ggvtxrtx.html