2025-10-27 21:18:16 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by .
|
|
|
|
|
|
--- DateTime: 2025/9/27 17:06
|
|
|
|
|
|
--- 业务逻辑 对权限数据表进行数据表业务处理
|
|
|
|
|
|
local validator = require("util.validator")
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local helpers = require("share.helpers")
|
2025-10-27 21:18:16 +08:00
|
|
|
|
local permission = require("model.permission")
|
|
|
|
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local dao = require("service.system.permission")
|
|
|
|
|
|
local resp = require("util.response")
|
|
|
|
|
|
local validator = require("util.validator")
|
|
|
|
|
|
|
|
|
|
|
|
--获取所有权限信息
|
|
|
|
|
|
function _M.get_allpermission()
|
|
|
|
|
|
local code,ret = dao.getAllPermission()
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据权限id获取权限信息
|
|
|
|
|
|
function _M.get_permission(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
local code,ret = dao.getPermission(id)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据角色id获取使用的权限
|
|
|
|
|
|
function _M.get_permission_by_role(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
local code,ret = dao.getPermissionByRole(id)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账号id获取账号信息
|
|
|
|
|
|
function _M.add_permission()
|
|
|
|
|
|
--获取请求头并进行校验
|
|
|
|
|
|
if validator.checkReqHeader() == false then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
--判断请求体数据是否为空
|
|
|
|
|
|
if body_data == nil then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
--ngx.say(body_data)
|
|
|
|
|
|
local code, ret = dao.addPermission(body_data)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账号id删除账号信息
|
|
|
|
|
|
function _M.delete_permission(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
local code, ret = dao.deletePermission(id)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账号id删除账号信息
|
|
|
|
|
|
function _M.update_permission(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
--判断请求体数据是否为空
|
|
|
|
|
|
if body_data == nil then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
local code, ret = dao.updatePermission(id, body_data)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-27 21:18:16 +08:00
|
|
|
|
-- 查询数据表中的所有权限信息
|
|
|
|
|
|
function _M.getAllPermission()
|
|
|
|
|
|
return permission:all()
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据权限id获取权限信息
|
|
|
|
|
|
function _M.getPermission(id)
|
|
|
|
|
|
return permission.find(id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 08:39:22 +08:00
|
|
|
|
--根据角色id获取角色的权限
|
|
|
|
|
|
function _M.getPermissionByRole(id)
|
|
|
|
|
|
--权限表与角色表进行表关系查询返回结果
|
|
|
|
|
|
--todo
|
|
|
|
|
|
return permission.first(id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-27 21:18:16 +08:00
|
|
|
|
--增加权限信息到数据表
|
|
|
|
|
|
function _M.addPermission(jsonData)
|
|
|
|
|
|
--验证数据的正确性,错误时返回
|
|
|
|
|
|
local success, result = validator.checkJson(jsonData)
|
|
|
|
|
|
if success == false then
|
|
|
|
|
|
return 0x000001, result
|
|
|
|
|
|
end
|
|
|
|
|
|
--解析json中的键和数据值
|
|
|
|
|
|
local name = ""
|
|
|
|
|
|
for key, value in pairs(result) do
|
|
|
|
|
|
if key == "name" then name = value end
|
|
|
|
|
|
end
|
|
|
|
|
|
--根据权限进行验证是否存在
|
|
|
|
|
|
local code, res = permission:where("name", "=", name):get()
|
|
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return 0x000001, res
|
|
|
|
|
|
end
|
|
|
|
|
|
local num = 0
|
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
|
for key, value in pairs(row) do
|
|
|
|
|
|
num = num + 1
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
--权限存在时返回权限已经存在
|
2025-10-28 16:57:06 +08:00
|
|
|
|
if num > 0 then
|
2025-10-27 21:18:16 +08:00
|
|
|
|
return 0x01000C, nil
|
|
|
|
|
|
end
|
|
|
|
|
|
--键值为id产生uuid数据值,增加到json中
|
|
|
|
|
|
result.id = helpers.getUuid()
|
|
|
|
|
|
local ret = helpers.convert_json(result)
|
|
|
|
|
|
-- 创建一个权限
|
|
|
|
|
|
return permission:create('{'..ret..'}')
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--删除权限信息到数据表
|
2025-10-27 21:18:16 +08:00
|
|
|
|
function _M.deletePermission(id)
|
|
|
|
|
|
return permission:delete(id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--更新权限信息到数据表
|
|
|
|
|
|
function _M.updatePermission(id, jsonData)
|
|
|
|
|
|
--根据权限id进行验证权限是否存在
|
|
|
|
|
|
local code, res = permission:find(id)
|
|
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return 0x000001, res
|
|
|
|
|
|
end
|
|
|
|
|
|
local num = 0
|
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
|
for key, value in pairs(row) do
|
|
|
|
|
|
num = num + 1
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
--权限不存在返回错误
|
|
|
|
|
|
if num <= 0 then
|
|
|
|
|
|
return 0x01000C, nil
|
|
|
|
|
|
end
|
|
|
|
|
|
--验证数据的正确性,错误时返回
|
|
|
|
|
|
local success, result = validator.checkJson(jsonData)
|
|
|
|
|
|
if success == false then
|
|
|
|
|
|
return 0x000001, result
|
|
|
|
|
|
end
|
|
|
|
|
|
--对数据内容进行更新
|
|
|
|
|
|
return permission:where('id', '=', id):update(jsonData)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return _M
|