From 899496bd0d09352b010974d53537334b0232b9d3 Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Thu, 30 Oct 2025 09:03:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0json=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E6=96=87=E4=BB=B6=EF=BC=8C=E5=B0=86json?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=8D=95=E7=8B=AC=E6=8F=90=E5=8F=96=E5=87=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/auth/auth.lua | 22 +++++++------------- src/service/system/account.lua | 23 ++++++--------------- src/service/system/application.lua | 21 +++---------------- src/service/system/department.lua | 20 +++--------------- src/service/system/permission.lua | 20 +++--------------- src/service/system/role.lua | 16 +++----------- src/service/system/user.lua | 24 +++++---------------- src/validator/auth/auth.lua | 26 +++++++++++++++++++++++ src/validator/system/account.lua | 29 ++++++++++++++++++++++++++ src/validator/system/application.lua | 31 ++++++++++++++++++++++++++++ src/validator/system/department.lua | 30 +++++++++++++++++++++++++++ src/validator/system/permission.lua | 30 +++++++++++++++++++++++++++ src/validator/system/role.lua | 26 +++++++++++++++++++++++ src/validator/system/user.lua | 30 +++++++++++++++++++++++++++ 14 files changed, 232 insertions(+), 116 deletions(-) create mode 100644 src/validator/auth/auth.lua create mode 100644 src/validator/system/account.lua create mode 100644 src/validator/system/application.lua create mode 100644 src/validator/system/department.lua create mode 100644 src/validator/system/permission.lua create mode 100644 src/validator/system/role.lua create mode 100644 src/validator/system/user.lua diff --git a/src/service/auth/auth.lua b/src/service/auth/auth.lua index 806f2d9..98bf737 100644 --- a/src/service/auth/auth.lua +++ b/src/service/auth/auth.lua @@ -3,24 +3,14 @@ --- Created by admin. --- DateTime: 2025/10/28 11:09 --- -local jsonschema = require("jsonschema") local resp = require("util.response") local authDao = require("dao.auth") local jwt = require("resty.jwt") local conf = require("config") +local validatorJson = require("validator.system.auth") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "username", type = "string"}, - {name = "password", type = "string"}, - {name = "captcha", type = "string"}, - {name = "checkKey", type = "string"}, - }, required = {"username", "password"}} -} - --设置JWT的有效载荷 local obj = { header = {typ="JWT", alg="HS256"}, @@ -40,8 +30,8 @@ function _M.login() ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() - -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + -- 验证数据是否符合json + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -71,8 +61,10 @@ function _M.logout() ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() - --判断请求体数据是否为空 - if body_data == nil then + -- 验证数据是否符合json + local ok = validatorJson.validatorJson(body_data) + --验证失败则返回 + if not ok then local result = resp:json(0x000001) resp:send(result) return diff --git a/src/service/system/account.lua b/src/service/system/account.lua index 8c65ba9..b5986f4 100644 --- a/src/service/system/account.lua +++ b/src/service/system/account.lua @@ -3,25 +3,12 @@ --- Created by . --- DateTime: 2025/9/25 08:25 --- 业务逻辑 对账户数据表进行数据表业务处理 -local jsonschema = require("jsonschema") local resp = require("util.response") local accountDao = require("dao.account") +local validatorJson = require("validator.system.account") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "name", type = "string"}, - {name = "password", type = "string"}, - {name = "real_name", type = "string"}, - {name = "department", type = "string"}, - {name = "office_phone", type = "string"}, - {name = "telephone", type = "string"}, - {name = "display_name", type = "string"}, - }, required = {"username"}} -} - --获取所有账户信息 function _M.getSystemAccounts() local pageNum = ngx.var.pagenum or 1 @@ -46,7 +33,7 @@ function _M.addSystemAccount() --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -72,8 +59,10 @@ function _M.updateSystemAccount(m) ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() - --判断请求体数据是否为空 - if body_data == nil then + -- 验证数据是否符合schema + local ok = validatorJson.validatorJson(body_data) + --验证失败则返回 + if not ok then local result = resp:json(0x000001) resp:send(result) return diff --git a/src/service/system/application.lua b/src/service/system/application.lua index ea9d755..3eb9a6d 100644 --- a/src/service/system/application.lua +++ b/src/service/system/application.lua @@ -3,27 +3,12 @@ --- Created by . --- DateTime: 2025/9/27 16:02 --- 业务逻辑 对应用数据表进行数据表业务处理 -local jsonschema = require("jsonschema") local resp = require("util.response") local applicationDao = require("dao.application") +local validatorJson = require("validator.system.application") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "name", type = "string"}, - {name = "display_name", type = "string"}, - {name = "logo", type = "string"}, - {name = "title", type = "string"}, - {name = "name", type = "string"}, - {name = "favicon", type = "string"}, - {name = "description", type = "string"}, - {name = "homepage_url", type = "string"}, - {name = "redirect_uris", type = "string"}, - }, required = {"name", "redirect_uris"}} -} - --获取所有应用程序信息 function _M.getSystemApplications() --获取页码和请求的数据量 @@ -62,7 +47,7 @@ function _M.addSystemApplication() --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -89,7 +74,7 @@ function _M.updateSystemApplication(m) --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) diff --git a/src/service/system/department.lua b/src/service/system/department.lua index f1fb930..23e7c77 100644 --- a/src/service/system/department.lua +++ b/src/service/system/department.lua @@ -3,26 +3,12 @@ --- Created by . --- DateTime: 2025/9/28 10:22 --- 业务逻辑 对组织架构数据表进行数据表业务处理 -local jsonschema = require("jsonschema") local resp = require("util.response") local departmentDao = require("dao.department") +local validatorJson = require("validator.system.department") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "name", type = "string"}, - {name = "seq", type = "string"}, - {name = "description", type = "string"}, - {name = "create_by", type = "string"}, - {name = "update_by", type = "string"}, - {name = "parent_id", type = "string"}, - {name = "leader", type = "string"}, - {name = "status", type = "string"}, - }, required = {"name"}} -} - --获取所有组织架构信息 function _M.getSystemDepartments() --获取页码和请求的数据量 @@ -47,7 +33,7 @@ function _M.addSystemDepartment() --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -74,7 +60,7 @@ function _M.updateSystemDepartment(m) --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) diff --git a/src/service/system/permission.lua b/src/service/system/permission.lua index eeb0e44..c3cfc95 100644 --- a/src/service/system/permission.lua +++ b/src/service/system/permission.lua @@ -3,26 +3,12 @@ --- Created by . --- DateTime: 2025/9/27 17:06 --- 业务逻辑 对权限数据表进行数据表业务处理 -local jsonschema = require("jsonschema") local resp = require("util.response") local permissionDao = require("dao.permission") +local validatorJson = require("validator.system.permission") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "username", type = "string"}, - {name = "phone", type = "string"}, - {name = "email", type = "string"}, - {name = "idcard", type = "string"}, - {name = "name", type = "string"}, - {name = "office_phone", type = "string"}, - {name = "telephone", type = "string"}, - {name = "display_name", type = "string"}, - }, required = {"username", "phone", "email", "idcard"}} -} - --获取所有权限信息 function _M.getSystemPermissions() --获取页码和请求的数据量 @@ -54,7 +40,7 @@ function _M.addSystemPermission() --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -81,7 +67,7 @@ function _M.updateSystemPermission(m) --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) diff --git a/src/service/system/role.lua b/src/service/system/role.lua index 1d056a0..2740d33 100644 --- a/src/service/system/role.lua +++ b/src/service/system/role.lua @@ -3,22 +3,12 @@ --- Created by . --- DateTime: 2025/9/27 15:19 --- 业务逻辑 对用户角色数据表进行数据表业务处理 -local jsonschema = require("jsonschema") local resp = require("util.response") local roleDao = require("dao.role") +local validatorJson = require("validator.system.role") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "role_name", type = "string"}, - {name = "description", type = "string"}, - {name = "create_by", type = "string"}, - {name = "update_by", type = "string"}, - }, required = {"role_name"}} -} - --获取所有角色信息 function _M.getSystemRoles() --获取页码和请求的数据量 @@ -44,7 +34,7 @@ function _M.addSystemRole() --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -71,7 +61,7 @@ function _M.updateSystemRole(m) --获取请求数据 local body_data = ngx.req.get_body_data() -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) diff --git a/src/service/system/user.lua b/src/service/system/user.lua index a2758c6..12f25d6 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -3,26 +3,12 @@ --- Created by . --- DateTime: 2025/9/25 08:19 --- 业务逻辑 对用户数据表进行数据表业务处理 -local jsonschema = require("jsonschema") local resp = require("util.response") local userDao = require("dao.user") +local validatorJson = require("validator.system.user") local _M = {} --- 定义一个JSON Schema -local schema = { - {type = "object", properties = { - {name = "username", type = "string"}, - {name = "phone", type = "string"}, - {name = "email", type = "string"}, - {name = "idcard", type = "string"}, - {name = "name", type = "string"}, - {name = "office_phone", type = "string"}, - {name = "telephone", type = "string"}, - {name = "display_name", type = "string"}, - }, required = {"username", "phone", "email", "idcard"}} -} - --获取所有用户信息 function _M.getSystemUsers() --获取页码和请求的数据量 @@ -47,8 +33,8 @@ function _M.addSystemUser() ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() - -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + -- 验证数据是否符合json + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) @@ -74,8 +60,8 @@ function _M.updateSystemUser(m) ngx.req.read_body() --获取请求数据 local body_data = ngx.req.get_body_data() - -- 验证数据是否符合schema - local ok, err = jsonschema:generate_validator(body_data, schema) + -- 验证数据是否符合json + local ok = validatorJson.validatorJson(body_data) --验证失败则返回 if not ok then local result = resp:json(0x000001) diff --git a/src/validator/auth/auth.lua b/src/validator/auth/auth.lua new file mode 100644 index 0000000..a099437 --- /dev/null +++ b/src/validator/auth/auth.lua @@ -0,0 +1,26 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by admin. +--- DateTime: 2025/10/30 08:09 +---业务逻辑 对账户登录的参数进行数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "username", type = "string"}, + {name = "password", type = "string"}, + {name = "captcha", type = "string"}, + {name = "checkKey", type = "string"}, + }, required = {"username", "password"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M \ No newline at end of file diff --git a/src/validator/system/account.lua b/src/validator/system/account.lua new file mode 100644 index 0000000..2cdc117 --- /dev/null +++ b/src/validator/system/account.lua @@ -0,0 +1,29 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by . +--- DateTime: 2025/10/30 08:18 +--- 业务逻辑 对账户数据参数进行数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "name", type = "string"}, + {name = "password", type = "string"}, + {name = "real_name", type = "string"}, + {name = "department", type = "string"}, + {name = "office_phone", type = "string"}, + {name = "telephone", type = "string"}, + {name = "display_name", type = "string"}, + }, required = {"name"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M diff --git a/src/validator/system/application.lua b/src/validator/system/application.lua new file mode 100644 index 0000000..c784b99 --- /dev/null +++ b/src/validator/system/application.lua @@ -0,0 +1,31 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by . +--- DateTime: 2025/10/30 08:14 +--- 业务逻辑 对应用数据参数进行数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "name", type = "string"}, + {name = "display_name", type = "string"}, + {name = "logo", type = "string"}, + {name = "title", type = "string"}, + {name = "name", type = "string"}, + {name = "favicon", type = "string"}, + {name = "description", type = "string"}, + {name = "homepage_url", type = "string"}, + {name = "redirect_uris", type = "string"}, + }, required = {"name", "redirect_uris"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M diff --git a/src/validator/system/department.lua b/src/validator/system/department.lua new file mode 100644 index 0000000..40a9ae0 --- /dev/null +++ b/src/validator/system/department.lua @@ -0,0 +1,30 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by . +--- DateTime: 2025/10/30 08:12 +--- 业务逻辑 对组织架构数据进行参数数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "name", type = "string"}, + {name = "seq", type = "string"}, + {name = "description", type = "string"}, + {name = "create_by", type = "string"}, + {name = "update_by", type = "string"}, + {name = "parent_id", type = "string"}, + {name = "leader", type = "string"}, + {name = "status", type = "string"}, + }, required = {"name"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M diff --git a/src/validator/system/permission.lua b/src/validator/system/permission.lua new file mode 100644 index 0000000..37caa29 --- /dev/null +++ b/src/validator/system/permission.lua @@ -0,0 +1,30 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by . +--- DateTime: 2025/10/30 08:11 +--- 业务逻辑 对权限参数进行数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "username", type = "string"}, + {name = "phone", type = "string"}, + {name = "email", type = "string"}, + {name = "idcard", type = "string"}, + {name = "name", type = "string"}, + {name = "office_phone", type = "string"}, + {name = "telephone", type = "string"}, + {name = "display_name", type = "string"}, + }, required = {"username", "phone", "email", "idcard"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M diff --git a/src/validator/system/role.lua b/src/validator/system/role.lua new file mode 100644 index 0000000..5c67081 --- /dev/null +++ b/src/validator/system/role.lua @@ -0,0 +1,26 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by . +--- DateTime: 2025/10/30 08:13 +--- 业务逻辑 对用户角色参数进行数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "role_name", type = "string"}, + {name = "description", type = "string"}, + {name = "create_by", type = "string"}, + {name = "update_by", type = "string"}, + }, required = {"role_name"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M diff --git a/src/validator/system/user.lua b/src/validator/system/user.lua new file mode 100644 index 0000000..15768f8 --- /dev/null +++ b/src/validator/system/user.lua @@ -0,0 +1,30 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by . +--- DateTime: 2025/10/30 08:19 +--- 业务逻辑 对用户参数进行数据的验证 +local jsonschema = require("jsonschema") + +local _M = {} + +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "username", type = "string"}, + {name = "phone", type = "string"}, + {name = "email", type = "string"}, + {name = "idcard", type = "string"}, + {name = "name", type = "string"}, + {name = "office_phone", type = "string"}, + {name = "telephone", type = "string"}, + {name = "display_name", type = "string"}, + }, required = {"username", "phone", "email", "idcard"}} +} + +function _M.validatorJson(jsonData) + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(jsonData, schema) + return ok +end + +return _M