Compare commits

..

No commits in common. "839fbf4d78c5f3c1a13276e13cb6278ff082b688" and "8b69d78c3b88269540434e75a09e7968a667cb29" have entirely different histories.

6 changed files with 67 additions and 53 deletions

View File

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

View File

@ -3,11 +3,12 @@
--- Created by . --- Created by .
--- DateTime: 2025/9/25 08:19 --- DateTime: 2025/9/25 08:19
--- 业务逻辑 对用户数据表进行数据表业务处理 --- 业务逻辑 对用户数据表进行数据表业务处理
local cjson = require('cjson')
local pgmoon = require('pgmoon') 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 validator = require("util.validator") local validator = require("util.validator")
local helpers = require("util.helpers")
local _M = {} local _M = {}
@ -26,6 +27,15 @@ local function get_con(cfg)
return code,conn return code,conn
end 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语句 --数据库执行sql语句
local function execSQL(sql) local function execSQL(sql)
local res = nil local res = nil
@ -52,7 +62,7 @@ end
local function checkAccountExist(where) local function checkAccountExist(where)
--组装sql语句 --组装sql语句
local sql = string.format("select count(*) as count from \"tbl_account\" %s", where) local sql = string.format("select count(*) as count from \"tbl_account\" %s", where)
--ngx.say("check sql: "..sql) print("check sql: "..sql)
--获取数据库连接 --获取数据库连接
return execSQL(sql) return execSQL(sql)
end end
@ -81,14 +91,16 @@ function _M.addAccount(jsonData)
--解析json中的键和数据值 --解析json中的键和数据值
local keys = "" local keys = ""
local values = "" local values = ""
local name = "" local username, phone, email
for key, value in pairs(result) do for key, value in pairs(result) do
keys = keys..key.."," keys = keys..key..","
values = values..((type(value) == "string") and "'"..value.."'" or value).."," values = values..((type(value) == "string") and "'"..value.."'" or value)..","
if key == "name" then name = value end if key == "username" then username = value end
if key == "phone" then phone = value end
if key == "email" then email = value end
end end
--根据用户进行验证用户是否存在 --根据用户、手机号、邮箱进行验证用户是否存在
local where = string.format("where name='%s'", name) local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
local ok, res = checkAccountExist(where) local ok, res = checkAccountExist(where)
if ok ~= 0 then if ok ~= 0 then
return 0x000001,res return 0x000001,res
@ -105,7 +117,7 @@ function _M.addAccount(jsonData)
end end
--自己增加对应的uuid数据值 --自己增加对应的uuid数据值
local newKeys = keys.."id" local newKeys = keys.."id"
local newValues = values.."'"..helpers.getUuid().."'" local newValues = values.."'"..getUuid().."'"
--组装sql语句 --组装sql语句
local sql = string.format("insert into \"tbl_account\"(%s)values(%s)", newKeys, newValues) local sql = string.format("insert into \"tbl_account\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql) return execSQL(sql)
@ -137,7 +149,7 @@ function _M.updateAccount(id, jsonData)
return 0x01000C,nil return 0x01000C,nil
end end
--验证数据的正确性,错误时返回 --验证数据的正确性,错误时返回
local success, result = validator.checkJson(jsonData) local success, result = checkJson(jsonData)
if success == false then if success == false then
return 0x000001,result return 0x000001,result
end end

View File

@ -3,11 +3,11 @@
--- Created by . --- Created by .
--- DateTime: 2025/9/25 08:19 --- DateTime: 2025/9/25 08:19
--- 业务逻辑 对用户数据表进行数据表业务处理 --- 业务逻辑 对用户数据表进行数据表业务处理
local cjson = require('cjson')
local pgmoon = require('pgmoon') 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 validator = require("util.validator") local snowflake = require("util.snowflake")
local helpers = require("util.helpers")
local _M = {} local _M = {}
@ -26,6 +26,15 @@ local function get_con(cfg)
return code,conn return code,conn
end 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语句 --数据库执行sql语句
local function execSQL(sql) local function execSQL(sql)
local res = nil local res = nil
@ -48,6 +57,18 @@ 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 checkUserExist(where) local function checkUserExist(where)
--组装sql语句 --组装sql语句
@ -74,7 +95,7 @@ end
--增加用户信息到数据表 --增加用户信息到数据表
function _M.addUser(jsonData) function _M.addUser(jsonData)
--验证数据的正确性,错误时返回 --验证数据的正确性,错误时返回
local success, result = validator.checkJson(jsonData) local success, result = checkJson(jsonData)
if success == false then if success == false then
return 0x000001,result return 0x000001,result
end end
@ -107,7 +128,7 @@ function _M.addUser(jsonData)
end end
--自己增加对应的uuid数据值 --自己增加对应的uuid数据值
local newKeys = keys.."id" local newKeys = keys.."id"
local newValues = values.."'"..helpers.getUuid().."'" local newValues = values.."'"..getUuid().."'"
--组装sql语句 --组装sql语句
local sql = string.format("insert into \"tbl_user\"(%s)values(%s)", newKeys, newValues) local sql = string.format("insert into \"tbl_user\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql) return execSQL(sql)
@ -139,7 +160,7 @@ function _M.updateUser(id, jsonData)
return 0x01000C,nil return 0x01000C,nil
end end
--验证数据的正确性,错误时返回 --验证数据的正确性,错误时返回
local success, result = validator.checkJson(jsonData) local success, result = checkJson(jsonData)
if success == false then if success == false then
return 0x000001,result return 0x000001,result
end end

View File

@ -3,21 +3,16 @@
--- Created by admin. --- Created by admin.
--- DateTime: 2025/10/15 09:12 --- DateTime: 2025/10/15 09:12
--- ---
--local snowflake = require("util.snowflake") local snowflake = require("util.snowflake")
--
--local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间 local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
--local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间 local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
--local snow = snowflake.new(workerId, datacenterId) local snow = snowflake.new(workerId, datacenterId)
--local id = snow:generateUniqueId()-- 生成ID local id = snow:generateUniqueId()-- 生成ID
--ngx.say("Generated ID:"..snow.int64_to_string(id)) ngx.say("Generated ID:"..snow.int64_to_string(id))
--max =a and b or c--a?b:c --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 newKeys = keys:sub(1, #keys -1)
--local newValues = values:sub(1, #values -1) --local newValues = values:sub(1, #values -1)

View File

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