增加账号相关接口,和相关的业务逻辑代码,并修改接口相关函数

This commit is contained in:
wanglei 2025-10-23 11:14:19 +08:00
parent 89c57ccd97
commit da2d95a9b4
5 changed files with 263 additions and 5 deletions

4
conf/system/account.conf Normal file
View File

@ -0,0 +1,4 @@
#API接口文件
location /api {
content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/api.lua';
}

View File

@ -5,9 +5,11 @@
---
local radix = require("resty.radixtree")
local userApi = require("api.system.user")
local accountApi = require("api.system.account")
--定义相关路由前端接口url地址
local routes = {
--用户相关路由接口
{
paths = { "/api/get-users" },
metadata = { "metadata get-users" },
@ -38,6 +40,37 @@ local routes = {
methods = { "PUT" },
handler = userApi.update_user,
},
--账户相关路由接口
{
paths = { "/api/get-account" },
metadata = { "metadata get-account" },
methods = { "GET" },
handler = accountApi.get_allaccounts,
},
{
paths = { "/api/get-account/:id" },
metadata = { "metadata /api/get-account/id" },
methods = { "GET" },
handler = accountApi.get_account,
},
{
paths = { "/api/delete-account/:id" },
metadata = { "metadata /api/delete-account/id" },
methods = { "DELETE" },
handler = accountApi.delete_account,
},
{
paths = { "/api/add-account" },
metadata = { "metadata /api/add-account" },
methods = { "POST" },
handler = accountApi.add_account,
},
{
paths = { "/api/update-account/:id" },
metadata = { "metadata /api/update-account/id" },
methods = { "PUT" },
handler = accountApi.update_account,
},
}
-- 初始化路由

View File

@ -0,0 +1,76 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/9/25 08:19
---
local _M = {}
local dao = require("service.system.user")
local resp = require("util.response")
--验证请求头是否正确
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(uuid)
local code,ret = dao.getAllAccount(uuid)
local result = resp:json(code, ret)
resp:send(result)
end
--根据用户id获取用户信息
function _M.get_account(m)
local id = m.id
local code,ret = dao.getAccount(id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据用户id获取用户信息
function _M.add_account()
--获取请求头并进行校验
if checkReqHeader() == false then
return
end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--判断请求体数据是否为空
if body_data == nil then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = dao.addAccount(body_data)
local result = resp:json(code, ret)
resp:send(result)
end
--根据用户id删除用户信息
function _M.delete_account(m)
local id = m.id
local code, ret = dao.deleteAccount(id)
local result = resp:json(code, ret)
resp:send(result)
end
--根据用户id删除用户信息
function _M.update_account(m)
local id = m.id
local code, ret = dao.updateAccount(id)
local result = resp:json(code, ret)
resp:send(result)
end
return _M

View File

@ -0,0 +1,145 @@
---
--- 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 checkAccountExist(username, phone, email)
--组装sql语句
local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
local sql = string.format("select count(*) as count from \"T_Users\" %s", where)
print("check sql: "..sql)
--获取数据库连接
return execSQL(sql)
end
-- 查询数据表中的所有账户根据用户的uuid
function _M.getAllAccount(uuid)
--组装sql语句
local sql = "select * from \"T_Users\""
return execSQL(sql)
end
--根据用户id获取用户信息
function _M.getAccount(uuid)
--组装sql语句
local sql = "select * from \"T_Users\" where uuid='"..uuid.."'"
return execSQL(sql)
end
--增加用户信息到数据表
function _M.addAccount(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 ok, res = checkAccountExist(username, phone, email)
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.."uuid"
local newValues = values.."'"..getUuid().."'"
--组装sql语句
local sql = string.format("insert into \"T_Users\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql)
end
--增加用户信息到数据表
function _M.deleteAccount(uuid)
--组装sql语句
local sql = "delete from \"T_Users\" where uuid='"..uuid.."'"
return execSQL(sql)
end
return _M

View File

@ -88,9 +88,9 @@ function _M.getAllUser()
end
--根据用户id获取用户信息
function _M.getUser(uuid)
function _M.getUser(id)
--组装sql语句
local sql = "select * from \"T_Users\" where uuid='"..uuid.."'"
local sql = "select * from \"T_Users\" where id='"..id.."'"
return execSQL(sql)
end
@ -128,7 +128,7 @@ function _M.addUser(jsonData)
return 0x010000,nil
end
--自己增加对应的uuid数据值
local newKeys = keys.."uuid"
local newKeys = keys.."id"
local newValues = values.."'"..getUuid().."'"
--组装sql语句
local sql = string.format("insert into \"T_Users\"(%s)values(%s)", newKeys, newValues)
@ -136,9 +136,9 @@ function _M.addUser(jsonData)
end
--增加用户信息到数据表
function _M.delete_user(uuid)
function _M.delete_user(id)
--组装sql语句
local sql = "delete from \"T_Users\" where uuid='"..uuid.."'"
local sql = "delete from \"T_Users\" where id='"..id.."'"
return execSQL(sql)
end