2025-10-22 08:46:41 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by .
|
|
|
|
|
|
--- DateTime: 2025/9/25 08:19
|
|
|
|
|
|
--- 业务逻辑 对用户数据表进行数据表业务处理
|
2025-11-18 10:04:45 +08:00
|
|
|
|
local status = require("util.status")
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local resp = require("util.response")
|
2025-11-10 19:34:43 +08:00
|
|
|
|
local userDao = require("dao.system.user")
|
2025-11-15 16:07:07 +08:00
|
|
|
|
local validator = require("validator.system.user")
|
2025-10-30 11:30:42 +08:00
|
|
|
|
local cjson = require("cjson.safe")
|
2025-11-04 09:33:40 +08:00
|
|
|
|
local token = require("util.token")
|
2025-11-08 16:10:04 +08:00
|
|
|
|
local perm = require("util.permissionfilter")
|
2025-10-22 08:46:41 +08:00
|
|
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
2025-11-04 09:33:40 +08:00
|
|
|
|
--验证用户id与token中的用户id是否一致
|
|
|
|
|
|
local function getUserId()
|
|
|
|
|
|
--获取请求头中的令牌数据
|
|
|
|
|
|
local auth_header = ngx.var.http_Authorization
|
|
|
|
|
|
--验证数据的正确性
|
|
|
|
|
|
local retToken = token.authorizationToken(auth_header)
|
|
|
|
|
|
--token前面已经进行验证,不需要进行判断
|
|
|
|
|
|
--验证成功获取用户id信息
|
|
|
|
|
|
local userid = retToken["body"]["payload"]["userid"]
|
|
|
|
|
|
return userid
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--获取所有用户信息
|
2025-11-06 15:36:15 +08:00
|
|
|
|
function _M.getSystemUsers(m)
|
|
|
|
|
|
--获取登录的用户信息
|
2025-11-08 16:10:04 +08:00
|
|
|
|
--local userid = ngx.ctx.userid
|
|
|
|
|
|
--local username = ngx.ctx.username
|
2025-11-07 07:58:29 +08:00
|
|
|
|
local role = ngx.ctx.role
|
2025-11-08 16:10:04 +08:00
|
|
|
|
--ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." role:"..role)
|
2025-11-07 10:33:32 +08:00
|
|
|
|
--权限数据
|
|
|
|
|
|
local perms = ngx.ctx.perms
|
2025-11-08 16:10:04 +08:00
|
|
|
|
--local method = m._method
|
|
|
|
|
|
--local path = m._path
|
|
|
|
|
|
--ngx.log(ngx.INFO, "path:"..path.." method:"..method)
|
2025-11-07 10:33:32 +08:00
|
|
|
|
--判断当前接口用户和角色是否有权限
|
2025-11-08 16:10:04 +08:00
|
|
|
|
if perm:hasPermission(role, perms) == false then
|
|
|
|
|
|
ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
|
|
end
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--获取页码和请求的数据量
|
2026-02-04 09:44:29 +08:00
|
|
|
|
local args = ngx.req.get_uri_args()
|
|
|
|
|
|
local pageNum = args["pagenum"] or 1
|
|
|
|
|
|
local pageSize = args["pagesize"] or 10
|
2025-11-18 10:04:45 +08:00
|
|
|
|
local code, ret = userDao.getSystemUsers(pageNum, pageSize)
|
|
|
|
|
|
local state = status.SUCCESS
|
|
|
|
|
|
if code ~= 0 then state = status.DATA_IS_WRONG end
|
|
|
|
|
|
resp: response(state, ret)
|
2025-10-22 14:35:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据用户id获取用户信息
|
2025-10-29 17:29:17 +08:00
|
|
|
|
function _M.getSystemUser(m)
|
2025-11-08 16:10:04 +08:00
|
|
|
|
local role = ngx.ctx.role
|
|
|
|
|
|
--权限数据
|
|
|
|
|
|
local perms = ngx.ctx.perms
|
2025-11-12 09:36:00 +08:00
|
|
|
|
print("get getSystemUser:", role, perms)
|
2025-11-08 16:10:04 +08:00
|
|
|
|
--判断当前接口用户和角色是否有权限
|
|
|
|
|
|
if perm:hasPermission(role, perms) == false then
|
|
|
|
|
|
ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
|
|
end
|
2025-11-06 15:36:15 +08:00
|
|
|
|
--获取登录的用户信息
|
|
|
|
|
|
local payload = ngx.var.uid
|
2025-11-04 09:33:40 +08:00
|
|
|
|
local userid = getUserId()
|
2025-11-18 10:04:45 +08:00
|
|
|
|
if userid ~= m.id then --非管理员情况下
|
2025-11-12 09:36:00 +08:00
|
|
|
|
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致", userid, m.id)
|
2025-11-04 09:33:40 +08:00
|
|
|
|
ngx.status = ngx.HTTP_NOT_ALLOWED
|
|
|
|
|
|
ngx.exit(ngx.HTTP_NOT_ALLOWED)
|
|
|
|
|
|
end
|
2025-11-18 10:04:45 +08:00
|
|
|
|
local code, ret = userDao.getSystemUser(m.id)
|
|
|
|
|
|
local state = status.SUCCESS
|
|
|
|
|
|
if code ~= 0 then state = status.DATA_IS_WRONG end
|
2026-02-04 09:44:29 +08:00
|
|
|
|
resp:response(state, ret[1])
|
2025-10-22 14:35:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--根据用户id获取用户信息
|
2025-11-06 15:36:15 +08:00
|
|
|
|
function _M.addSystemUser(m)
|
2025-11-08 16:10:04 +08:00
|
|
|
|
local role = ngx.ctx.role
|
|
|
|
|
|
--权限数据
|
|
|
|
|
|
local perms = ngx.ctx.perms
|
|
|
|
|
|
--判断当前接口用户和角色是否有权限
|
|
|
|
|
|
if perm:hasPermission(role, perms) == false then
|
|
|
|
|
|
ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
|
|
end
|
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-11-15 16:07:07 +08:00
|
|
|
|
local ok = validator.validateJson(body_data)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--验证失败则返回
|
|
|
|
|
|
if not ok then
|
2025-11-18 10:04:45 +08:00
|
|
|
|
resp:response(status.PARAM_NOT_COMPLETE)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
return
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--ngx.say(body_data)
|
2025-10-31 21:32:39 +08:00
|
|
|
|
local jsonData = cjson.decode(body_data)
|
|
|
|
|
|
--ngx.say(jsonData)
|
|
|
|
|
|
local code, ret = userDao.addSystemUser(jsonData)
|
2025-11-18 10:04:45 +08:00
|
|
|
|
local state = status.SUCCESS
|
|
|
|
|
|
if code ~= 0 then state = status.DATA_IS_WRONG end
|
|
|
|
|
|
resp: response(state, ret)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--根据用户id删除用户信息
|
|
|
|
|
|
function _M.deleteSystemUser(m)
|
2025-11-08 16:10:04 +08:00
|
|
|
|
local role = ngx.ctx.role
|
|
|
|
|
|
--权限数据
|
|
|
|
|
|
local perms = ngx.ctx.perms
|
|
|
|
|
|
--判断当前接口用户和角色是否有权限
|
|
|
|
|
|
if perm:hasPermission(role, perms) == false then
|
|
|
|
|
|
ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
|
|
end
|
2025-10-29 23:00:17 +08:00
|
|
|
|
local code, ret = userDao.deleteSystemUser(m.id)
|
2025-11-18 10:04:45 +08:00
|
|
|
|
local state = status.SUCCESS
|
|
|
|
|
|
if code ~= 0 then state = status.DATA_IS_WRONG end
|
|
|
|
|
|
resp: response(state, ret)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--根据用户id删除用户信息
|
|
|
|
|
|
function _M.updateSystemUser(m)
|
2025-11-08 16:10:04 +08:00
|
|
|
|
local role = ngx.ctx.role
|
|
|
|
|
|
--权限数据
|
|
|
|
|
|
local perms = ngx.ctx.perms
|
|
|
|
|
|
--判断当前接口用户和角色是否有权限
|
|
|
|
|
|
if perm:hasPermission(role, perms) == false then
|
|
|
|
|
|
ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
|
|
end
|
2025-11-04 09:33:40 +08:00
|
|
|
|
local userid = getUserId()
|
|
|
|
|
|
if userid ~= m.id then
|
|
|
|
|
|
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致")
|
|
|
|
|
|
ngx.exit(ngx.HTTP_NOT_ALLOWED)
|
|
|
|
|
|
end
|
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-11-15 16:07:07 +08:00
|
|
|
|
local ok = validator.validateJson(body_data)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--验证失败则返回
|
|
|
|
|
|
if not ok then
|
2025-11-18 10:04:45 +08:00
|
|
|
|
resp:response(status.PARAM_NOT_COMPLETE)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
return
|
2025-10-23 17:46:02 +08:00
|
|
|
|
end
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--将数据更新到数据表中
|
2025-10-30 11:30:42 +08:00
|
|
|
|
local code, ret = userDao.updateSystemUser(m.id, cjson.decode(body_data))
|
2025-11-18 10:04:45 +08:00
|
|
|
|
local state = status.SUCCESS
|
|
|
|
|
|
if code ~= 0 then state = status.DATA_IS_WRONG end
|
|
|
|
|
|
resp: response(state, ret)
|
2025-10-23 17:46:02 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-22 08:46:41 +08:00
|
|
|
|
return _M
|