Compare commits

..

4 Commits

7 changed files with 172 additions and 72 deletions

View File

@ -11,10 +11,10 @@ local accountApi = require("api.system.account")
local routes = {
--用户相关路由接口
{
paths = { "/api/get-users" },
metadata = { "metadata get-users" },
paths = { "/api/get-user" },
metadata = { "metadata get-user" },
methods = { "GET" },
handler = userApi.get_allusers,
handler = userApi.get_alluser,
},
{
paths = { "/api/get-user/:id" },
@ -45,7 +45,7 @@ local routes = {
paths = { "/api/get-account" },
metadata = { "metadata get-account" },
methods = { "GET" },
handler = accountApi.get_allaccounts,
handler = accountApi.get_allaccount,
},
{
paths = { "/api/get-account/:id" },

View File

@ -6,7 +6,7 @@
local _M = {}
local dao = require("service.system.user")
local dao = require("service.system.account")
local resp = require("util.response")
--验证请求头是否正确
@ -20,14 +20,14 @@ local function checkReqHeader()
return true
end
--获取所有用户信息
function _M.get_allaccount(uuid)
local code,ret = dao.getAllAccount(uuid)
--获取所有账号信息
function _M.get_allaccount()
local code,ret = dao.getAllAccount()
local result = resp:json(code, ret)
resp:send(result)
end
--根据用户id获取用户信息
--根据账号id获取账号信息
function _M.get_account(m)
local id = m.id
local code,ret = dao.getAccount(id)
@ -35,12 +35,10 @@ function _M.get_account(m)
resp:send(result)
end
--根据用户id获取用户信息
--根据账号id获取账号信息
function _M.add_account()
--获取请求头并进行校验
if checkReqHeader() == false then
return
end
if checkReqHeader() == false then return end
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
@ -57,7 +55,7 @@ function _M.add_account()
resp:send(result)
end
--根据用户id删除用户信息
--根据账号id删除账号信息
function _M.delete_account(m)
local id = m.id
local code, ret = dao.deleteAccount(id)
@ -65,10 +63,20 @@ function _M.delete_account(m)
resp:send(result)
end
--根据用户id删除用户信息
--根据账号id删除账号信息
function _M.update_account(m)
local id = m.id
local code, ret = dao.updateAccount(id)
--读取请求体的数据
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
local code, ret = dao.updateAccount(id, body_data)
local result = resp:json(code, ret)
resp:send(result)
end

View File

@ -8,20 +8,10 @@ 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
local validator = require("util.validator")
--获取所有用户信息
function _M.get_allusers()
function _M.get_alluser()
local code,ret = dao.getAllUser()
local result = resp:json(code, ret)
resp:send(result)
@ -38,7 +28,9 @@ end
--根据用户id获取用户信息
function _M.add_user()
--获取请求头并进行校验
if checkReqHeader() == false then
if validator.checkReqHeader() == false then
local result = resp:json(0x000001)
resp:send(result)
return
end
--读取请求体的数据
@ -68,7 +60,17 @@ end
--根据用户id删除用户信息
function _M.update_user(m)
local id = m.id
local code, ret = dao.update_user(id)
--读取请求体的数据
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
local code, ret = dao.update_user(id, body_data)
local result = resp:json(code, ret)
resp:send(result)
end

View File

@ -38,4 +38,5 @@ return {
[0x010009] = '重置密码失败,用户不存在',
[0x01000A] = '获取用户信息失败,用户未登录',
[0x01000B] = '获取用户信息失败,用户不存在',
[0x01000C] = '修改用户信息失败,用户不存在',
}

View File

@ -8,6 +8,7 @@ local pgmoon = require('pgmoon')
local dbconf = require("config.database")
local status = require("config.status")
local snowflake = require("util.snowflake")
local validator = require("util.validator")
local _M = {}
@ -57,47 +58,33 @@ local function execSQL(sql)
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)
local function checkAccountExist(where)
--组装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)
local sql = string.format("select count(*) as count from \"tbl_account\" %s", where)
print("check sql: "..sql)
--获取数据库连接
return execSQL(sql)
end
-- 查询数据表中的所有账户根据用户的uuid
function _M.getAllAccount(uuid)
-- 查询数据表中的所有账号信息
function _M.getAllAccount()
--组装sql语句
local sql = "select * from \"T_Users\""
local sql = "select * from \"tbl_account\""
return execSQL(sql)
end
--根据用户id获取用户信息
function _M.getAccount(uuid)
--根据用户id获取账号信息
function _M.getAccount(id)
--组装sql语句
local sql = "select * from \"T_Users\" where uuid='"..uuid.."'"
local sql = "select * from \"tbl_account\" where id='"..id.."'"
return execSQL(sql)
end
--增加用户信息到数据表
--增加账号信息到数据表
function _M.addAccount(jsonData)
--验证数据的正确性,错误时返回
local success, result = checkJson(jsonData)
local success, result = validator.checkJson(jsonData)
if success == false then
return 0x000001,result
end
@ -112,8 +99,9 @@ function _M.addAccount(jsonData)
if key == "phone" then phone = value end
if key == "email" then email = value end
end
--校验用户是否存在
local ok, res = checkAccountExist(username, phone, email)
--根据用户、手机号、邮箱进行验证用户是否存在
local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
local ok, res = checkAccountExist(where)
if ok ~= 0 then
return 0x000001,res
end
@ -128,17 +116,52 @@ function _M.addAccount(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)
local sql = string.format("insert into \"tbl_account\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql)
end
--增加用户信息到数据表
function _M.deleteAccount(uuid)
--增加账号信息到数据表
function _M.deleteAccount(id)
--组装sql语句
local sql = "delete from \"T_Users\" where uuid='"..uuid.."'"
local sql = "delete from \"tbl_account\" where id='"..id.."'"
return execSQL(sql)
end
--更新账号信息到数据表
function _M.updateAccount(id, jsonData)
--根据用户id进行验证用户是否存在
local where = string.format("where id='%s'", id)
local ok, res = checkAccountExist(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_account\" set %s where id='%s'", vals, id)
return execSQL(sql)
end

View File

@ -70,11 +70,9 @@ local function checkJson(jsonData)
end
--通过查询条件判断数据库中的数据记录
--根据用户、手机号、邮箱进行验证用户是否存在
local function checkUserExist(username, phone, email)
local function checkUserExist(where)
--组装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 \"tbl_users\" %s", where)
local sql = string.format("select count(*) as count from \"tbl_user\" %s", where)
print("check sql: "..sql)
--获取数据库连接
return execSQL(sql)
@ -83,14 +81,14 @@ end
-- 查询数据表中的所有用户信息
function _M.getAllUser()
--组装sql语句
local sql = "select * from \"tbl_users\""
local sql = "select * from \"tbl_user\""
return execSQL(sql)
end
--根据用户id获取用户信息
function _M.getUser(id)
--组装sql语句
local sql = "select * from \"tbl_users\" where id='"..id.."'"
local sql = "select * from \"tbl_user\" where id='"..id.."'"
return execSQL(sql)
end
@ -112,8 +110,9 @@ function _M.addUser(jsonData)
if key == "phone" then phone = value end
if key == "email" then email = value end
end
--校验用户是否存在
local ok, res = checkUserExist(username, phone, email)
--根据用户、手机号、邮箱进行验证用户是否存在
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
@ -131,14 +130,49 @@ function _M.addUser(jsonData)
local newKeys = keys.."id"
local newValues = values.."'"..getUuid().."'"
--组装sql语句
local sql = string.format("insert into \"tbl_users\"(%s)values(%s)", newKeys, newValues)
local sql = string.format("insert into \"tbl_user\"(%s)values(%s)", newKeys, newValues)
return execSQL(sql)
end
--增加用户信息到数据表
function _M.delete_user(id)
function _M.deleteUser(id)
--组装sql语句
local sql = "delete from \"tbl_users\" where id='"..id.."'"
local sql = "delete from \"tbl_user\" where id='"..id.."'"
return execSQL(sql)
end
--更新用户信息到数据表
function _M.updateUser(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_user\" set %s where id='%s'", vals, id)
return execSQL(sql)
end

32
src/util/validator.lua Normal file
View File

@ -0,0 +1,32 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/24 11:01
--- 提供公共需要的验证接口等功能
local cjson = require('cjson')
local _M = {}
--验证请求头是否正确
function _M:checkReqHeader()
local headers = ngx.req.get_headers()
if headers["content-type"] ~= "application/json" then
return false
end
return true
end
--校验json数据的正确性并返回json解析后的数据
function _M: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
return _M