openresty 获取微信 openID 示例

openresty 获取微信 openID 示例

其中涉及到了 resty.http 和 cjson 需要自行下载,百度一下就可以搜到

-- 需要配置 resty.http 库
local http = require "resty.http"
local cjson = require "cjson"

local appid = "xxxxxxxxxxxxxxxxxxxxxxxx"
local secret = "xxxxxxxxxxxxxxxxxxxxxxxx"

local gameConfig = {
    dev = {
        appid = "xxxxxxxxxxxxxxxxxxxxxx",
        secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
}


-- 获取请求参数 使用 get_body_data  之前一点先要 read_body
ngx.req.read_body()

local function sayGoodBy(infom)
local cjson = require "cjson"
    local txt = cjson.encode(infom)
    ngx.say(txt)
    ngx.exit(ngx.HTTP_OK)
end

-- 成功返回
local function RequestOK(body)
    local res = {}
    res["status"] = 0
    res["data"] = body
    sayGoodBy(res);
end

-- 失败返回
local function RequestError(codeNo, msg)
    local res = {}
    res["status"] = 1
    res["code"] = codeNo
    res["errMsg"] = msg
    ngx.log(ngx.ERR,"statu:"..res["status"].." code:"..res["code"]);
    sayGoodBy(res)
end

local args = ngx.req.get_uri_args()
local data = ngx.req.get_body_data()

if args == nil then
    args = ngx.req.get_body_data()
end


-- info 定义返回数据
local info = {}

if args == nil then
    ngx.log(ngx.ERR, "参数为空")
    RequestError(201,"参数为空");
end

if not args.code then
    ngx.log(ngx.ERR, "缺少参数 code")
    RequestError(202,"缺少参数 code")
end

local function _json_decode(str)
    return cjson.decode(str)
end
-- 解析数据异常捕获
local function json_decode( str )
    local ok, t = pcall(_json_decode, str)
    if not ok then
      return nil
    end

    return t
end

-- 执行微信获取openID 请求
local function getOpenIDForWx(wxAppID, wxSecret, code, ssl_verify)
    local httpc = http.new()

    local dt = "appid="..wxAppID.."&secret="..wxSecret.."&js_code="..code.."&grant_type=authorization_code"

    ngx.log(ngx.ERR, "请求微信参数:",dt)
    local res, err = httpc:request_uri(
        "https://api.weixin.qq.com/sns/jscode2session?"..dt,
        {
            ssl_verify = false,//跳过服务端的证书验证
            method = "GET",
            headers = {
                ["Content-Type"] = "application/x-www-form-urlencoded",
            }
        }
    )

    if nil == res then
        ngx.log(ngx.ERR, "请求微信接口失败:" ,err)
        return nil, err
    end

    if 200 ~= res.status then
        return nil , "{status:"..res.statu"}"
    end

    local js = {}
    if type(res.body) == "string" then
        ngx.log(ngx.ERR,"请求微信获取的值:" ,res.body)
        local t = json_decode(res.body)
        if nil == t then
            return nil
        end
        js = t
    else
        js = res.body
    end
    return js, nil
end

-- 判断是获取哪个小程序的用户openID
if (args.account) then
    local account = args.account
    if (account == "dev") then
        appid = gameConfig.dev.appid
        secret = gameConfig.dev.secret
    end
end

local res, err = getOpenIDForWx(appid, secret, args.code)

if nil == res then
    RequestError(203, "获取OpenID失败:"..err)
else
    if res.openid then
        info["session_key"] = res.session_key
        info["openid"] = res.openid
    else
        info["errcode"] = res.errcode
        info["errmsg"] = res.errmsg
    end
    RequestOK(info);
end

你可能感兴趣的:(openresty 获取微信 openID 示例)