AuthPlatform/src/service/system/user.lua

77 lines
2.0 KiB
Lua
Raw Normal View History

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by .
--- DateTime: 2025/9/25 08:19
2025-10-14 14:45:47 +08:00
--- 业务逻辑
local cjson = require('cjson')
local pgmoon = require('pgmoon')
local dbconf = require("config.database")
local _M = {}
2025-10-14 14:45:47 +08:00
local function get_con(cfg)
-- 创建一个新的连接
local conn = pgmoon.new(cfg);
---- 连接到数据库
local ok, err = conn:connect()
if not ok then
error("Connection failed: " .. err)
ngx.exit(ngx.HTTP_NOT_FOUND)
2025-10-14 14:45:47 +08:00
end
--ngx.say("Connection success")
return conn
end
-- 查询数据表中的所有用户信息
function _M.getAllUser()
--组装sql语句
local sql = "select * from \"T_Users\""
--获取数据库连接
local conn = get_con(dbconf.postgres)
--设置数据库的编码格式
--conn:exec("SET NAMES UTF8")
--执行数据库操作
local res = conn:query(sql)
if not res then
error("Query failed: " .. err)
--ngx.say(err)
ngx.exit(ngx.HTTP_NOT_FOUND)
end
--整理数据库结果返回值
for _, row in ipairs(res) do
for key, value in pairs(row) do
ngx.say(key .. ":" .. tostring(value))
end
end
--关闭数据库
conn:disconnect()
end
--根据用户id获取用户信息
function _M.getUser(id)
--组装sql语句
local sql = "select * from \"T_Users\" where id="..id
--获取数据库连接
local conn = get_con(dbconf.postgres)
--设置数据库的编码格式
--conn:exec("SET NAMES UTF8")
--执行数据库操作
local res = conn:query(sql)
if not res then
error("Query failed: " .. err)
--ngx.say(err)
ngx.exit(ngx.HTTP_NOT_FOUND)
end
--整理数据库结果返回值
2025-10-16 20:08:20 +08:00
--for _, row in ipairs(res) do
-- for key, value in pairs(row) do
-- ngx.say(key .. ":" .. tostring(value))
-- end
--end
ngx.say(cjson.encode(res))
--关闭数据库
conn:disconnect()
end
return _M