AuthPlatform/src/service/system/user.lua

180 lines
5.3 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
--- 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
--数据库执行sql语句
local function execSQL(sql)
local res = nil
if sql == '' or sql == nil then
return 0x000003,res
end
--获取数据库连接
local code,conn = get_con(dbconf.postgres)
if code ~= 0 then
return 0x000003,res
end
--执行数据库操作
res = conn:query(sql)
if not res then
print("query sql failed: "..sql)
return 0x000003,res
end
--关闭数据库
conn:disconnect()
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语句
local sql = string.format("select count(*) as count from \"tbl_users\" %s", where)
print("check sql: "..sql)
--获取数据库连接
return execSQL(sql)
end
-- 查询数据表中的所有用户信息
function _M.getAllUser()
--组装sql语句
local sql = "select * from \"tbl_users\""
return execSQL(sql)
end
--根据用户id获取用户信息
function _M.getUser(id)
--组装sql语句
local sql = "select * from \"tbl_users\" where id='"..id.."'"
return execSQL(sql)
end
--增加用户信息到数据表
function _M.addUser(jsonData)
--验证数据的正确性,错误时返回
local success, result = checkJson(jsonData)
if success == false then
return 0x000001,result
end
--解析json中的键和数据值
local keys = ""
local values = ""
local username, phone, email
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
end
--根据用户、手机号、邮箱进行验证用户是否存在
local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
local ok, res = checkUserExist(where)
if ok ~= 0 then
return 0x000001,res
end
local num = 0
for _, row in ipairs(res) do
for key, value in pairs(row) do
num = value
end
end
print("exec result:", num)
if num > 0 then
return 0x010000,nil
end
--自己增加对应的uuid数据值
local newKeys = keys.."id"
local newValues = values.."'"..getUuid().."'"
--组装sql语句
local sql = string.format("insert into \"tbl_users\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql)
end
--增加用户信息到数据表
function _M.delete_user(id)
--组装sql语句
local sql = "delete from \"tbl_users\" where id='"..id.."'"
return execSQL(sql)
end
--更新用户信息到数据表
function _M.update_user(id, jsonData)
--根据用户id进行验证用户是否存在
local where = string.format("where id='%s'", id)
local ok, res = checkUserExist(where)
if ok ~= 0 then
return 0x000001,res
end
local num = 0
for _, row in ipairs(res) do
for key, value in pairs(row) do
num = value
end
end
print("exec result:", num)
if num <= 0 then
return 0x01000C,nil
end
--验证数据的正确性,错误时返回
local success, result = checkJson(jsonData)
if success == false then
return 0x000001,result
end
--解析json中的键和数据值
local tmp = ""
for key, value in pairs(result) do
local val = (type(value) == "string") and "'"..value.."'" or value
tmp = string.format("%s=%s,", key, val)
end
local vals = tmp:sub(1, #tmp - 1)
--组装sql语句
local sql = string.format("update \"tbl_users\" set %s where id='%s'", vals, id)
return execSQL(sql)
end
return _M