增加验证函数文件,对使用的公共函数进行封装
This commit is contained in:
parent
c5a12dbf9d
commit
8b69d78c3b
|
|
@ -38,9 +38,7 @@ end
|
||||||
--根据账号id获取账号信息
|
--根据账号id获取账号信息
|
||||||
function _M.add_account()
|
function _M.add_account()
|
||||||
--获取请求头并进行校验
|
--获取请求头并进行校验
|
||||||
if checkReqHeader() == false then
|
if checkReqHeader() == false then return end
|
||||||
return
|
|
||||||
end
|
|
||||||
--读取请求体的数据
|
--读取请求体的数据
|
||||||
ngx.req.read_body()
|
ngx.req.read_body()
|
||||||
--获取请求数据
|
--获取请求数据
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,7 @@ local _M = {}
|
||||||
|
|
||||||
local dao = require("service.system.user")
|
local dao = require("service.system.user")
|
||||||
local resp = require("util.response")
|
local resp = require("util.response")
|
||||||
|
local validator = require("util.validator")
|
||||||
--验证请求头是否正确
|
|
||||||
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
|
|
||||||
|
|
||||||
--获取所有用户信息
|
--获取所有用户信息
|
||||||
function _M.get_alluser()
|
function _M.get_alluser()
|
||||||
|
|
@ -38,7 +28,9 @@ end
|
||||||
--根据用户id获取用户信息
|
--根据用户id获取用户信息
|
||||||
function _M.add_user()
|
function _M.add_user()
|
||||||
--获取请求头并进行校验
|
--获取请求头并进行校验
|
||||||
if checkReqHeader() == false then
|
if validator.checkReqHeader() == false then
|
||||||
|
local result = resp:json(0x000001)
|
||||||
|
resp:send(result)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
--读取请求体的数据
|
--读取请求体的数据
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ local pgmoon = require('pgmoon')
|
||||||
local dbconf = require("config.database")
|
local dbconf = require("config.database")
|
||||||
local status = require("config.status")
|
local status = require("config.status")
|
||||||
local snowflake = require("util.snowflake")
|
local snowflake = require("util.snowflake")
|
||||||
|
local validator = require("util.validator")
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
||||||
|
|
@ -57,18 +58,6 @@ local function execSQL(sql)
|
||||||
return code,res
|
return code,res
|
||||||
end
|
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)
|
local function checkAccountExist(where)
|
||||||
--组装sql语句
|
--组装sql语句
|
||||||
|
|
@ -95,7 +84,7 @@ end
|
||||||
--增加账号信息到数据表
|
--增加账号信息到数据表
|
||||||
function _M.addAccount(jsonData)
|
function _M.addAccount(jsonData)
|
||||||
--验证数据的正确性,错误时返回
|
--验证数据的正确性,错误时返回
|
||||||
local success, result = checkJson(jsonData)
|
local success, result = validator.checkJson(jsonData)
|
||||||
if success == false then
|
if success == false then
|
||||||
return 0x000001,result
|
return 0x000001,result
|
||||||
end
|
end
|
||||||
|
|
|
||||||
32
src/util/validator.lua
Normal file
32
src/util/validator.lua
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user