177 lines
5.5 KiB
Lua
177 lines
5.5 KiB
Lua
---
|
||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||
--- Created by admin.
|
||
--- DateTime: 2025/10/28 11:09
|
||
--- 用于
|
||
local resp = require("util.response")
|
||
local loginDao = require("dao.system.login")
|
||
local validator = require("validator.system.login")
|
||
local cjson = require("cjson.safe")
|
||
local token = require("util.token")
|
||
|
||
local _M = {}
|
||
|
||
--用户登录业务逻辑处理
|
||
function _M.login()
|
||
--获取远端客户端的IP地址
|
||
local client_ip = ngx.var.remote_addr
|
||
ngx.log(ngx.INFO, "client_ip:"..client_ip.." login system")
|
||
--读取请求体的数据
|
||
ngx.req.read_body()
|
||
--获取请求数据
|
||
local body_data = ngx.req.get_body_data()
|
||
--验证json数据是否正确
|
||
local ok, data = pcall(cjson.decode, body_data)
|
||
if not ok then
|
||
print("JSON解析失败:", data)
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
-- 验证数据是否符合json
|
||
local valid, errors = validator.validateJson(data)
|
||
--验证失败则返回
|
||
if not valid then
|
||
print("验证失败: ", errors)
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--ngx.say(body_data)
|
||
local code, ret = loginDao.login(data)
|
||
--读取数据错误
|
||
if code ~= 0 or table.getn(ret) < 0 then
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
local id = ret[1].id
|
||
local username = ret[1].username
|
||
local role_id = ret[1].role_id
|
||
local role_name = ret[1].role_name
|
||
--获取的登陆的用户信息,返回tocken
|
||
local jwt_token = token.generateToken(id, username, role_id, role_name)
|
||
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 ok, data = pcall(cjson.decode, body_data)
|
||
if not ok then
|
||
print("JSON解析失败:", data)
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
-- 验证数据是否符合json
|
||
local retJson = validator.validateJson(data)
|
||
--验证失败则返回
|
||
if not retJson then
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--ngx.say(body_data)
|
||
local code, ret = loginDao.signup(data)
|
||
--读取数据错误
|
||
if code ~= 0 or table.getn(ret) < 0 then
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--返回注册成功信息
|
||
local result = resp:json(code, ret)
|
||
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
|
||
--验证成功记录登出的日志信息
|
||
local userid = ret["body"]["payload"]["userid"]
|
||
local username = ret["body"]["payload"]["username"]
|
||
local role_id = ret["body"]["payload"]["role_id"]
|
||
local role_name = ret["body"]["payload"]["role_name"]
|
||
ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." role_id:"..role_id.." role_name:"..role_name.." logout system")
|
||
local result = resp:json(0, "用户退出系统成功")
|
||
resp:send(result)
|
||
end
|
||
|
||
--根据token获取用户信息
|
||
function _M.user()
|
||
--获取请求头中的令牌数据
|
||
local auth_header = ngx.var.http_Authorization
|
||
--验证数据的正确性
|
||
local retToken = token.authorizationToken(auth_header)
|
||
--验证失败则返回
|
||
local code = retToken["code"]
|
||
if code ~= 200 then
|
||
local result = resp:json(code, retToken["message"])
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--验证成功获取用户id信息
|
||
local userid = retToken["body"]["payload"]["userid"]
|
||
local code, ret = loginDao.getUser(userid)
|
||
--读取数据错误
|
||
if code ~= 0 or table.getn(ret) < 0 then
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--获取的登陆的用户信息
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
--根据token获取用户登录权限
|
||
function _M.permission()
|
||
--获取请求头中的令牌数据
|
||
local auth_header = ngx.var.http_Authorization
|
||
--验证数据的正确性
|
||
local retToken = token.authorizationToken(auth_header)
|
||
--验证失败则返回
|
||
local code = retToken["code"]
|
||
if code ~= 200 then
|
||
local result = resp:json(code, retToken["message"])
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--验证成功获取用户id信息
|
||
local userid = retToken["body"]["payload"]["userid"]
|
||
local username = retToken["body"]["payload"]["username"]
|
||
local role_id = retToken["body"]["payload"]["role_id"]
|
||
local role_name = retToken["body"]["payload"]["role_name"]
|
||
--通过用户id查询到用户的权限信息
|
||
local code, ret = loginDao.getUser(userid)
|
||
--读取数据错误
|
||
if code ~= 0 or table.getn(ret) < 0 then
|
||
local result = resp:json(0x000001)
|
||
resp:send(result)
|
||
return
|
||
end
|
||
--返回用户权限信息
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
return _M |