增加验证函数文件,对使用的公共函数进行封装

This commit is contained in:
wanglei 2025-10-24 11:11:57 +08:00
parent c5a12dbf9d
commit 8b69d78c3b
4 changed files with 39 additions and 28 deletions

View File

@ -38,9 +38,7 @@ end
--根据账号id获取账号信息
function _M.add_account()
--获取请求头并进行校验
if checkReqHeader() == false then
return
end
if checkReqHeader() == false then return end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据

View File

@ -8,17 +8,7 @@ local _M = {}
local dao = require("service.system.user")
local resp = require("util.response")
--验证请求头是否正确
local function checkReqHeader()
local headers = ngx.req.get_headers()
if headers["content-type"] ~= "application/json" then
local result = resp:json(0x000001)
resp:send(result)
return false
end
return true
end
local validator = require("util.validator")
--获取所有用户信息
function _M.get_alluser()
@ -38,7 +28,9 @@ end
--根据用户id获取用户信息
function _M.add_user()
--获取请求头并进行校验
if checkReqHeader() == false then
if validator.checkReqHeader() == false then
local result = resp:json(0x000001)
resp:send(result)
return
end
--读取请求体的数据

View File

@ -8,6 +8,7 @@ local pgmoon = require('pgmoon')
local dbconf = require("config.database")
local status = require("config.status")
local snowflake = require("util.snowflake")
local validator = require("util.validator")
local _M = {}
@ -57,18 +58,6 @@ local function execSQL(sql)
return code,res
end
--校验json数据的正确性并返回json解析后的数据
local function checkJson(jsonData)
local success, result = pcall(function()
return cjson.decode(jsonData)
end)
if success == true then
return true, result
end
local res = nil
return false,res
end
--通过查询条件判断数据库中的数据记录
local function checkAccountExist(where)
--组装sql语句
@ -95,7 +84,7 @@ end
--增加账号信息到数据表
function _M.addAccount(jsonData)
--验证数据的正确性,错误时返回
local success, result = checkJson(jsonData)
local success, result = validator.checkJson(jsonData)
if success == false then
return 0x000001,result
end

32
src/util/validator.lua Normal file
View File

@ -0,0 +1,32 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/24 11:01
--- 提供公共需要的验证接口等功能
local cjson = require('cjson')
local _M = {}
--验证请求头是否正确
function _M:checkReqHeader()
local headers = ngx.req.get_headers()
if headers["content-type"] ~= "application/json" then
return false
end
return true
end
--校验json数据的正确性并返回json解析后的数据
function _M:checkJson(jsonData)
local success, result = pcall(function()
return cjson.decode(jsonData)
end)
if success == true then
return true, result
end
local res = nil
return false,res
end
return _M