对公共函数进行封装测试进行修改并可增加账号相关数据内容

This commit is contained in:
wanglei 2025-10-24 14:58:03 +08:00
parent 5f8257e91d
commit 839fbf4d78
5 changed files with 27 additions and 29 deletions

View File

@ -8,17 +8,7 @@ local _M = {}
local dao = require("service.system.account")
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_allaccount()
@ -38,7 +28,11 @@ end
--根据账号id获取账号信息
function _M.add_account()
--获取请求头并进行校验
if checkReqHeader() == false then return end
if validator.checkReqHeader() == false then
local result = resp:json(0x000001)
resp:send(result)
return
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据

View File

@ -52,7 +52,7 @@ end
local function checkAccountExist(where)
--组装sql语句
local sql = string.format("select count(*) as count from \"tbl_account\" %s", where)
print("check sql: "..sql)
--ngx.say("check sql: "..sql)
--获取数据库连接
return execSQL(sql)
end
@ -81,16 +81,14 @@ function _M.addAccount(jsonData)
--解析json中的键和数据值
local keys = ""
local values = ""
local username, phone, email
local name = ""
for key, value in pairs(result) do
keys = keys..key..","
values = values..((type(value) == "string") and "'"..value.."'" or value)..","
if key == "username" then username = value end
if key == "phone" then phone = value end
if key == "email" then email = value end
if key == "name" then name = value end
end
--根据用户、手机号、邮箱进行验证用户是否存在
local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
--根据用户进行验证用户是否存在
local where = string.format("where name='%s'", name)
local ok, res = checkAccountExist(where)
if ok ~= 0 then
return 0x000001,res

View File

@ -74,7 +74,7 @@ end
--增加用户信息到数据表
function _M.addUser(jsonData)
--验证数据的正确性,错误时返回
local success, result = checkJson(jsonData)
local success, result = validator.checkJson(jsonData)
if success == false then
return 0x000001,result
end

View File

@ -3,16 +3,21 @@
--- Created by admin.
--- DateTime: 2025/10/15 09:12
---
local snowflake = require("util.snowflake")
local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
local snow = snowflake.new(workerId, datacenterId)
local id = snow:generateUniqueId()-- 生成ID
ngx.say("Generated ID:"..snow.int64_to_string(id))
--local snowflake = require("util.snowflake")
--
--local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
--local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
--local snow = snowflake.new(workerId, datacenterId)
--local id = snow:generateUniqueId()-- 生成ID
--ngx.say("Generated ID:"..snow.int64_to_string(id))
--max =a and b or c--a?b:c
local cjson = require("cjson")
local header = ngx.req.get_header()
local payload = ngx.req.get_payload()
ngx.say("payload:", payload)
--去掉组装最后一位逗号(,)
--local newKeys = keys:sub(1, #keys -1)
--local newValues = values:sub(1, #values -1)

View File

@ -9,7 +9,7 @@ local cjson = require('cjson')
local _M = {}
--验证请求头是否正确
function _M:checkReqHeader()
function _M.checkReqHeader()
local headers = ngx.req.get_headers()
if headers["content-type"] ~= "application/json" then
return false
@ -18,7 +18,8 @@ function _M:checkReqHeader()
end
--校验json数据的正确性并返回json解析后的数据
function _M:checkJson(jsonData)
function _M.checkJson(jsonData)
--ngx.say(jsonData)
local success, result = pcall(function()
return cjson.decode(jsonData)
end)