2025-10-22 08:46:41 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- 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 _M = {}
|
|
|
|
|
|
|
|
|
|
|
|
--获取一个数据库操作连接
|
|
|
|
|
|
local function get_con(cfg)
|
|
|
|
|
|
local code = 0
|
|
|
|
|
|
-- 创建一个新的连接
|
|
|
|
|
|
local conn = pgmoon.new(cfg);
|
|
|
|
|
|
---- 连接到数据库
|
|
|
|
|
|
local ok, err = conn:connect()
|
|
|
|
|
|
if not ok then
|
|
|
|
|
|
print("Connection failed: " .. err)
|
|
|
|
|
|
code = 0x000002
|
|
|
|
|
|
end
|
|
|
|
|
|
--ngx.say("Connection success")
|
|
|
|
|
|
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
|
|
|
|
|
|
|
2025-10-22 14:35:41 +08:00
|
|
|
|
--数据库执行sql语句
|
2025-10-22 15:37:13 +08:00
|
|
|
|
local function execSQL(sql)
|
|
|
|
|
|
local res = nil
|
2025-10-22 14:35:41 +08:00
|
|
|
|
if sql == '' or sql == nil then
|
2025-10-22 08:46:41 +08:00
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
|
local code,conn = get_con(dbconf.postgres)
|
2025-10-22 15:37:13 +08:00
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
2025-10-22 08:46:41 +08:00
|
|
|
|
--执行数据库操作
|
2025-10-22 14:35:41 +08:00
|
|
|
|
res = conn:query(sql)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
if not res then
|
2025-10-22 14:35:41 +08:00
|
|
|
|
print("query sql failed: "..sql)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--关闭数据库
|
|
|
|
|
|
conn:disconnect()
|
|
|
|
|
|
return code,res
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-22 14:35:41 +08:00
|
|
|
|
--校验json数据的正确性,并返回json解析后的数据
|
|
|
|
|
|
local function checkJson(jsonData)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
local success, result = pcall(function()
|
|
|
|
|
|
return cjson.decode(jsonData)
|
|
|
|
|
|
end)
|
2025-10-22 15:37:13 +08:00
|
|
|
|
if success == true then
|
|
|
|
|
|
return true, result
|
|
|
|
|
|
end
|
2025-10-22 08:46:41 +08:00
|
|
|
|
local res = nil
|
2025-10-22 15:37:13 +08:00
|
|
|
|
return false,res
|
2025-10-22 14:35:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--通过查询条件判断数据库中的数据记录
|
|
|
|
|
|
--根据用户、手机号、邮箱进行验证用户是否存在
|
|
|
|
|
|
local function checkUserExist(username, phone, email)
|
|
|
|
|
|
--组装sql语句
|
2025-10-22 15:37:13 +08:00
|
|
|
|
local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
|
2025-10-23 16:04:34 +08:00
|
|
|
|
local sql = string.format("select count(*) as count from \"tbl_users\" %s", where)
|
2025-10-22 15:37:13 +08:00
|
|
|
|
print("check sql: "..sql)
|
2025-10-22 14:35:41 +08:00
|
|
|
|
--获取数据库连接
|
|
|
|
|
|
return execSQL(sql)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 查询数据表中的所有用户信息
|
|
|
|
|
|
function _M.getAllUser()
|
|
|
|
|
|
--组装sql语句
|
2025-10-23 16:04:34 +08:00
|
|
|
|
local sql = "select * from \"tbl_users\""
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return execSQL(sql)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据用户id获取用户信息
|
2025-10-23 11:14:19 +08:00
|
|
|
|
function _M.getUser(id)
|
2025-10-22 14:35:41 +08:00
|
|
|
|
--组装sql语句
|
2025-10-23 16:04:34 +08:00
|
|
|
|
local sql = "select * from \"tbl_users\" where id='"..id.."'"
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return execSQL(sql)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--增加用户信息到数据表
|
|
|
|
|
|
function _M.addUser(jsonData)
|
|
|
|
|
|
--验证数据的正确性,错误时返回
|
|
|
|
|
|
local success, result = checkJson(jsonData)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
if success == false then
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return 0x000001,result
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
--解析json中的键和数据值
|
2025-10-22 15:37:13 +08:00
|
|
|
|
local keys = ""
|
|
|
|
|
|
local values = ""
|
2025-10-22 14:35:41 +08:00
|
|
|
|
local username, phone, email
|
2025-10-22 08:46:41 +08:00
|
|
|
|
for key, value in pairs(result) do
|
2025-10-22 14:35:41 +08:00
|
|
|
|
keys = keys..key..","
|
2025-10-22 15:37:13 +08:00
|
|
|
|
values = values..((type(value) == "string") and "'"..value.."'" or value)..","
|
2025-10-22 14:35:41 +08:00
|
|
|
|
if key == "username" then username = value end
|
|
|
|
|
|
if key == "phone" then phone = value end
|
|
|
|
|
|
if key == "email" then email = value end
|
|
|
|
|
|
end
|
|
|
|
|
|
--校验用户是否存在
|
|
|
|
|
|
local ok, res = checkUserExist(username, phone, email)
|
2025-10-22 15:37:13 +08:00
|
|
|
|
if ok ~= 0 then
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return 0x000001,res
|
|
|
|
|
|
end
|
2025-10-22 15:37:13 +08:00
|
|
|
|
local num = 0
|
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
|
for key, value in pairs(row) do
|
|
|
|
|
|
num = value
|
|
|
|
|
|
end
|
2025-10-22 14:35:41 +08:00
|
|
|
|
end
|
2025-10-22 15:37:13 +08:00
|
|
|
|
print("exec result:", num)
|
|
|
|
|
|
if num > 0 then
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return 0x010000,nil
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
--自己增加对应的uuid数据值
|
2025-10-23 11:14:19 +08:00
|
|
|
|
local newKeys = keys.."id"
|
2025-10-22 15:37:13 +08:00
|
|
|
|
local newValues = values.."'"..getUuid().."'"
|
2025-10-22 08:46:41 +08:00
|
|
|
|
--组装sql语句
|
2025-10-23 16:04:34 +08:00
|
|
|
|
local sql = string.format("insert into \"tbl_users\"(%s)values(%s)", newKeys, newValues)
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return execSQL(sql)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--增加用户信息到数据表
|
2025-10-23 11:14:19 +08:00
|
|
|
|
function _M.delete_user(id)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
--组装sql语句
|
2025-10-23 16:04:34 +08:00
|
|
|
|
local sql = "delete from \"tbl_users\" where id='"..id.."'"
|
2025-10-22 14:35:41 +08:00
|
|
|
|
return execSQL(sql)
|
2025-10-22 08:46:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return _M
|