AuthPlatform/src/service/auth/auth.lua

180 lines
5.2 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 validator = require("validator.auth.auth")
local cjson = require("cjson.safe")
local token = require("util.token")
local _M = {}
--用户登录业务逻辑处理
function _M.login()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local retJson = validator.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
local id = ""
local username = ""
for _, row in ipairs(ret) do
id = row.id
username = row.username
end
--获取的登陆的用户信息返回tocken
ngx.log(ngx.INFO, "userid:"..id.." username:"..username)
local jwt_token = token.generateToken(id, username)
local data = {}
data["token"] = jwt_token
data["userInfo"] = ret
local result = resp:json(code, data)
resp:send(result)
end
--用户注册业务逻辑处理
function _M.signup()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local retJson = validator.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()
--获取请求头中的令牌数据
local auth_header = ngx.var.http_Authorization
--验证数据的正确性
local ret = token.authorizationToken(auth_header)
--验证失败则返回
local code = ret["code"]
if code ~= 200 then
local result = resp:json(code, ret["message"])
resp:send(result)
return
end
--验证成功记录登出的日志信息
ngx.log(ngx.INFO, cjson.encode(ret["body"]))
local userid = ret["body"]["payload"]["userid"]
local username = ret["body"]["payload"]["username"]
ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." logout system")
local result = resp:json(0, "用户退出系统成功")
resp:send(result)
end
--根据token获取用户信息
function _M.user()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local retJson = validator.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
--根据token获取用户登录权限
function _M.permission()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local retJson = validator.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
return _M