AuthPlatform/src/service/auth/auth.lua

84 lines
2.4 KiB
Lua
Raw Normal View History

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/28 11:09
---
local resp = require("util.response")
local authDao = require("dao.auth")
local jwt = require("resty.jwt")
local conf = require("config")
local validatorJson = require("validator.auth.auth")
local cjson = require("cjson.safe")
local _M = {}
--设置JWT的有效载荷
local obj = {
header = {typ="JWT", alg="HS256"},
payload = { -- 自定义数据
userid = "", -- 用户id
username = "", -- 用户名
role = "", -- 角色
--iss = "your_issuer", -- 签发者
--sub = "1234567890", -- 主题
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时
iat = os.time() -- 签发时间
}
}
--用户登录业务逻辑处理
function _M.login()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local retJson = validatorJson.validatorJson(body_data)
--验证失败则返回
if not retJson then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = authDao.login(cjson.decode(body_data))
--读取数据错误
if code ~= 0 or table.getn(ret) < 0 then
local result = resp:json(0x000001)
resp:send(result)
return
end
--获取的登陆的用户信息返回tocken
obj.payload.userid = ret["id"]
obj.payload.username = ret["name"]
obj.payload.role = ""
local jwt_token = jwt:sign(conf.secret_key, obj)
--ngx.say(jwt_token)
local data = {}
data["token"] = jwt_token
data["userInfo"] = ret
local result = resp:json(code, data)
resp:send(result)
end
--用户登出业务逻辑处理
function _M.logout()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = authDao.logout(cjson.decode(body_data))
local result = resp:json(code, ret)
resp:send(result)
end
return _M