Compare commits

...

2 Commits

6 changed files with 53 additions and 67 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

@ -3,12 +3,11 @@
--- Created by .
--- DateTime: 2025/9/25 08:19
--- 业务逻辑 对用户数据表进行数据表业务处理
local cjson = require('cjson')
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 helpers = require("util.helpers")
local _M = {}
@ -27,15 +26,6 @@ local function get_con(cfg)
return code,conn
end
local function getUuid()
local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
local snow = snowflake.new(workerId, datacenterId)
local id = snow:generateUniqueId()-- 生成ID
--print("Generated ID:", snow.int64_to_string(id))
return snow.int64_to_string(id)
end
--数据库执行sql语句
local function execSQL(sql)
local res = nil
@ -62,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
@ -91,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
@ -117,7 +105,7 @@ function _M.addAccount(jsonData)
end
--自己增加对应的uuid数据值
local newKeys = keys.."id"
local newValues = values.."'"..getUuid().."'"
local newValues = values.."'"..helpers.getUuid().."'"
--组装sql语句
local sql = string.format("insert into \"tbl_account\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql)
@ -149,7 +137,7 @@ function _M.updateAccount(id, jsonData)
return 0x01000C,nil
end
--验证数据的正确性,错误时返回
local success, result = checkJson(jsonData)
local success, result = validator.checkJson(jsonData)
if success == false then
return 0x000001,result
end

View File

@ -3,11 +3,11 @@
--- Created by .
--- DateTime: 2025/9/25 08:19
--- 业务逻辑 对用户数据表进行数据表业务处理
local cjson = require('cjson')
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 helpers = require("util.helpers")
local _M = {}
@ -26,15 +26,6 @@ local function get_con(cfg)
return code,conn
end
local function getUuid()
local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
local snow = snowflake.new(workerId, datacenterId)
local id = snow:generateUniqueId()-- 生成ID
--print("Generated ID:", snow.int64_to_string(id))
return snow.int64_to_string(id)
end
--数据库执行sql语句
local function execSQL(sql)
local res = nil
@ -57,18 +48,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 checkUserExist(where)
--组装sql语句
@ -95,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
@ -128,7 +107,7 @@ function _M.addUser(jsonData)
end
--自己增加对应的uuid数据值
local newKeys = keys.."id"
local newValues = values.."'"..getUuid().."'"
local newValues = values.."'"..helpers.getUuid().."'"
--组装sql语句
local sql = string.format("insert into \"tbl_user\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql)
@ -160,7 +139,7 @@ function _M.updateUser(id, jsonData)
return 0x01000C,nil
end
--验证数据的正确性,错误时返回
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)

19
src/util/helpers.lua Normal file
View File

@ -0,0 +1,19 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/24 11:36
---
local snowflake = require("util.snowflake")
local _M = {}
function _M:getUuid()
local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
local snow = snowflake.new(workerId, datacenterId)
local id = snow:generateUniqueId()-- 生成ID
--print("Generated ID:", snow.int64_to_string(id))
return snow.int64_to_string(id)
end
return _M

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)