Compare commits
2 Commits
8b69d78c3b
...
839fbf4d78
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
839fbf4d78 | ||
| 5f8257e91d |
|
|
@ -8,17 +8,7 @@ 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()
|
||||||
|
|
@ -38,7 +28,11 @@ end
|
||||||
--根据账号id获取账号信息
|
--根据账号id获取账号信息
|
||||||
function _M.add_account()
|
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()
|
ngx.req.read_body()
|
||||||
--获取请求数据
|
--获取请求数据
|
||||||
|
|
|
||||||
|
|
@ -3,12 +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 snowflake = require("util.snowflake")
|
|
||||||
local validator = require("util.validator")
|
local validator = require("util.validator")
|
||||||
|
local helpers = require("util.helpers")
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
||||||
|
|
@ -27,15 +26,6 @@ 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
|
||||||
|
|
@ -62,7 +52,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)
|
||||||
print("check sql: "..sql)
|
--ngx.say("check sql: "..sql)
|
||||||
--获取数据库连接
|
--获取数据库连接
|
||||||
return execSQL(sql)
|
return execSQL(sql)
|
||||||
end
|
end
|
||||||
|
|
@ -91,16 +81,14 @@ function _M.addAccount(jsonData)
|
||||||
--解析json中的键和数据值
|
--解析json中的键和数据值
|
||||||
local keys = ""
|
local keys = ""
|
||||||
local values = ""
|
local values = ""
|
||||||
local username, phone, email
|
local name = ""
|
||||||
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 == "username" then username = value end
|
if key == "name" then name = value end
|
||||||
if key == "phone" then phone = value end
|
|
||||||
if key == "email" then email = value end
|
|
||||||
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)
|
local ok, res = checkAccountExist(where)
|
||||||
if ok ~= 0 then
|
if ok ~= 0 then
|
||||||
return 0x000001,res
|
return 0x000001,res
|
||||||
|
|
@ -117,7 +105,7 @@ function _M.addAccount(jsonData)
|
||||||
end
|
end
|
||||||
--自己增加对应的uuid数据值
|
--自己增加对应的uuid数据值
|
||||||
local newKeys = keys.."id"
|
local newKeys = keys.."id"
|
||||||
local newValues = values.."'"..getUuid().."'"
|
local newValues = values.."'"..helpers.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)
|
||||||
|
|
@ -149,7 +137,7 @@ function _M.updateAccount(id, jsonData)
|
||||||
return 0x01000C,nil
|
return 0x01000C,nil
|
||||||
end
|
end
|
||||||
--验证数据的正确性,错误时返回
|
--验证数据的正确性,错误时返回
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -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 snowflake = require("util.snowflake")
|
local validator = require("util.validator")
|
||||||
|
local helpers = require("util.helpers")
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
||||||
|
|
@ -26,15 +26,6 @@ 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
|
||||||
|
|
@ -57,18 +48,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 checkUserExist(where)
|
local function checkUserExist(where)
|
||||||
--组装sql语句
|
--组装sql语句
|
||||||
|
|
@ -95,7 +74,7 @@ end
|
||||||
--增加用户信息到数据表
|
--增加用户信息到数据表
|
||||||
function _M.addUser(jsonData)
|
function _M.addUser(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
|
||||||
|
|
@ -128,7 +107,7 @@ function _M.addUser(jsonData)
|
||||||
end
|
end
|
||||||
--自己增加对应的uuid数据值
|
--自己增加对应的uuid数据值
|
||||||
local newKeys = keys.."id"
|
local newKeys = keys.."id"
|
||||||
local newValues = values.."'"..getUuid().."'"
|
local newValues = values.."'"..helpers.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)
|
||||||
|
|
@ -160,7 +139,7 @@ function _M.updateUser(id, jsonData)
|
||||||
return 0x01000C,nil
|
return 0x01000C,nil
|
||||||
end
|
end
|
||||||
--验证数据的正确性,错误时返回
|
--验证数据的正确性,错误时返回
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,21 @@
|
||||||
--- 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)
|
||||||
|
|
|
||||||
19
src/util/helpers.lua
Normal file
19
src/util/helpers.lua
Normal 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
|
||||||
|
|
@ -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,7 +18,8 @@ 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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user