--- --- Generated by EmmyLua(https://github.com/EmmyLua) --- Created by . --- DateTime: 2025/9/27 15:19 --- 业务逻辑 对用户角色数据表进行数据表业务处理 local validator = require("util.validator") local helpers = require("share.helpers") local role = require("model.role") local _M = {} local dao = require("service.system.role") local resp = require("util.response") local validator = require("util.validator") --获取所有角色信息 function _M.get_allrole() local code,ret = dao.getAllRole() local result = resp:json(code, ret) resp:send(result) end --根据角色id获取角色信息 function _M.get_role(m) local id = m.id local code,ret = dao.getRole(id) local result = resp:json(code, ret) resp:send(result) end --根据角色id获取角色信息 function _M.add_role() --获取请求头并进行校验 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.addRole(body_data) local result = resp:json(code, ret) resp:send(result) end --根据角色id删除角色信息 function _M.delete_role(m) local id = m.id local code, ret = dao.deleteRole(id) local result = resp:json(code, ret) resp:send(result) end --根据角色id删除角色信息 function _M.update_role(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.updateRole(id, body_data) local result = resp:json(code, ret) resp:send(result) end -- 查询数据表中的所有角色信息 function _M.getAllRole() return role:all() end --根据角色id获取角色信息 function _M.getRole(id) return role.find(id) end --增加角色信息到数据表 function _M.addRole(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 = role: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 --角色存在时返回账号已经存在 if num > 0 then return 0x01000C, nil end --键值为id产生uuid数据值,增加到json中 result.id = helpers.getUuid() local ret = helpers.convert_json(result) -- 创建一个角色 return role:create('{'..ret..'}') end --删除角色信息到数据表 function _M.deleteRole(id) return role:delete(id) end --更新角色信息到数据表 function _M.updateRole(id, jsonData) --根据角色id进行验证角色是否存在 local code, res = role: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 role:where('id', '=', id):update(jsonData) end return _M