106 lines
3.3 KiB
Lua
106 lines
3.3 KiB
Lua
---
|
||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||
--- Created by .
|
||
--- DateTime: 2025/9/25 08:19
|
||
--- 业务逻辑 对用户数据表进行数据表业务处理
|
||
local resp = require("util.response")
|
||
local userDao = require("dao.user")
|
||
local validatorJson = require("validator.system.user")
|
||
local cjson = require("cjson.safe")
|
||
local token = require("util.token")
|
||
|
||
local _M = {}
|
||
|
||
--验证用户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
|
||
|
||
--获取所有用户信息
|
||
function _M.getSystemUsers()
|
||
--获取页码和请求的数据量
|
||
--local args = ngx.req.get_uri_args()
|
||
local pageNum = ngx.var.pagenum or 1
|
||
local pageSize = ngx.var.pagesize or 10
|
||
local code,ret = userDao.getSystemUsers(pageNum, pageSize)
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
--根据用户id获取用户信息
|
||
function _M.getSystemUser(m)
|
||
local userid = getUserId()
|
||
if userid ~= m.id then
|
||
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致")
|
||
ngx.status = ngx.HTTP_NOT_ALLOWED
|
||
ngx.exit(ngx.HTTP_NOT_ALLOWED)
|
||
end
|
||
local code,ret = userDao.getSystemUser(m.id)
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
--根据用户id获取用户信息
|
||
function _M.addSystemUser()
|
||
--读取请求体的数据
|
||
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 jsonData = cjson.decode(body_data)
|
||
--ngx.say(jsonData)
|
||
local code, ret = userDao.addSystemUser(jsonData)
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
--根据用户id删除用户信息
|
||
function _M.deleteSystemUser(m)
|
||
local code, ret = userDao.deleteSystemUser(m.id)
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
--根据用户id删除用户信息
|
||
function _M.updateSystemUser(m)
|
||
local userid = getUserId()
|
||
if userid ~= m.id then
|
||
ngx.log(ngx.WARN, "用户与使用token中的用户id不一致")
|
||
ngx.status = ngx.HTTP_NOT_ALLOWED
|
||
ngx.exit(ngx.HTTP_NOT_ALLOWED)
|
||
end
|
||
--读取请求体的数据
|
||
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
|
||
--将数据更新到数据表中
|
||
local code, ret = userDao.updateSystemUser(m.id, cjson.decode(body_data))
|
||
local result = resp:json(code, ret)
|
||
resp:send(result)
|
||
end
|
||
|
||
return _M
|