2025-10-28 16:57:06 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by admin.
|
|
|
|
|
|
--- DateTime: 2025/10/28 11:09
|
2025-11-07 17:06:39 +08:00
|
|
|
|
--- 用于
|
2025-10-28 16:57:06 +08:00
|
|
|
|
local resp = require("util.response")
|
2025-11-10 11:09:03 +08:00
|
|
|
|
local authDao = require("dao.login")
|
|
|
|
|
|
local validator = require("validator.system.login")
|
2025-10-30 11:30:42 +08:00
|
|
|
|
local cjson = require("cjson.safe")
|
2025-10-31 15:09:03 +08:00
|
|
|
|
local token = require("util.token")
|
2025-10-28 16:57:06 +08:00
|
|
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--用户登录业务逻辑处理
|
|
|
|
|
|
function _M.login()
|
2025-11-01 16:19:35 +08:00
|
|
|
|
--获取远端客户端的IP地址
|
|
|
|
|
|
local client_ip = ngx.var.remote_addr
|
|
|
|
|
|
ngx.log(ngx.INFO, "client_ip:"..client_ip.." login system")
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
2025-10-30 09:03:44 +08:00
|
|
|
|
-- 验证数据是否符合json
|
2025-10-31 15:09:03 +08:00
|
|
|
|
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
|
2025-11-07 17:06:39 +08:00
|
|
|
|
local id = ret[1].id
|
|
|
|
|
|
local username = ret[1].username
|
|
|
|
|
|
local role_id = ret[1].role_id
|
|
|
|
|
|
local role_name = ret[1].role_name
|
2025-10-31 15:09:03 +08:00
|
|
|
|
--获取的登陆的用户信息,返回tocken
|
2025-11-07 17:06:39 +08:00
|
|
|
|
local jwt_token = token.generateToken(id, username, role_id, role_name)
|
2025-10-31 15:09:03 +08:00
|
|
|
|
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)
|
2025-10-29 23:54:17 +08:00
|
|
|
|
--验证失败则返回
|
2025-10-30 11:30:42 +08:00
|
|
|
|
if not retJson then
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
--ngx.say(body_data)
|
2025-10-31 16:28:00 +08:00
|
|
|
|
local code, ret = authDao.signup(cjson.decode(body_data))
|
2025-10-29 23:54:17 +08:00
|
|
|
|
--读取数据错误
|
|
|
|
|
|
if code ~= 0 or table.getn(ret) < 0 then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
2025-10-31 16:28:00 +08:00
|
|
|
|
--返回注册成功信息
|
|
|
|
|
|
local result = resp:json(code, ret)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--用户登出业务逻辑处理
|
|
|
|
|
|
function _M.logout()
|
2025-10-31 15:09:03 +08:00
|
|
|
|
--获取请求头中的令牌数据
|
|
|
|
|
|
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"]
|
2025-11-07 17:06:39 +08:00
|
|
|
|
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")
|
2025-10-31 15:09:03 +08:00
|
|
|
|
local result = resp:json(0, "用户退出系统成功")
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据token获取用户信息
|
|
|
|
|
|
function _M.user()
|
2025-10-31 15:45:00 +08:00
|
|
|
|
--获取请求头中的令牌数据
|
|
|
|
|
|
local auth_header = ngx.var.http_Authorization
|
|
|
|
|
|
--验证数据的正确性
|
|
|
|
|
|
local retToken = token.authorizationToken(auth_header)
|
2025-10-30 09:03:44 +08:00
|
|
|
|
--验证失败则返回
|
2025-10-31 15:45:00 +08:00
|
|
|
|
local code = retToken["code"]
|
|
|
|
|
|
if code ~= 200 then
|
|
|
|
|
|
local result = resp:json(code, retToken["message"])
|
2025-10-29 17:29:17 +08:00
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
2025-10-31 15:45:00 +08:00
|
|
|
|
--验证成功获取用户id信息
|
|
|
|
|
|
local userid = retToken["body"]["payload"]["userid"]
|
|
|
|
|
|
local code, ret = authDao.getUser(userid)
|
2025-10-31 15:09:03 +08:00
|
|
|
|
--读取数据错误
|
|
|
|
|
|
if code ~= 0 or table.getn(ret) < 0 then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
2025-10-31 15:45:00 +08:00
|
|
|
|
--获取的登陆的用户信息
|
|
|
|
|
|
local result = resp:json(code, ret)
|
2025-10-31 15:09:03 +08:00
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据token获取用户登录权限
|
|
|
|
|
|
function _M.permission()
|
2025-10-31 16:28:00 +08:00
|
|
|
|
--获取请求头中的令牌数据
|
|
|
|
|
|
local auth_header = ngx.var.http_Authorization
|
|
|
|
|
|
--验证数据的正确性
|
|
|
|
|
|
local retToken = token.authorizationToken(auth_header)
|
2025-10-31 15:09:03 +08:00
|
|
|
|
--验证失败则返回
|
2025-10-31 16:28:00 +08:00
|
|
|
|
local code = retToken["code"]
|
|
|
|
|
|
if code ~= 200 then
|
|
|
|
|
|
local result = resp:json(code, retToken["message"])
|
2025-10-31 15:09:03 +08:00
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
2025-10-31 16:28:00 +08:00
|
|
|
|
--验证成功获取用户id信息
|
|
|
|
|
|
local userid = retToken["body"]["payload"]["userid"]
|
2025-11-07 17:06:39 +08:00
|
|
|
|
local username = retToken["body"]["payload"]["username"]
|
|
|
|
|
|
local role_id = retToken["body"]["payload"]["role_id"]
|
|
|
|
|
|
local role_name = retToken["body"]["payload"]["role_name"]
|
2025-10-31 16:28:00 +08:00
|
|
|
|
--通过用户id查询到用户的权限信息
|
|
|
|
|
|
local code, ret = authDao.getUser(userid)
|
2025-10-31 15:09:03 +08:00
|
|
|
|
--读取数据错误
|
|
|
|
|
|
if code ~= 0 or table.getn(ret) < 0 then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
2025-10-31 16:28:00 +08:00
|
|
|
|
--返回用户权限信息
|
|
|
|
|
|
local result = resp:json(code, ret)
|
2025-10-28 16:57:06 +08:00
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return _M
|