From c5a12dbf9d2ece824497709d472adb70db6bb186 Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Fri, 24 Oct 2025 10:44:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=95=B0=E6=8D=AE=E5=92=8C=E6=95=B0=E6=8D=AE=E8=A1=A8?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=9B=B8=E5=85=B3=E4=B8=9A=E5=8A=A1=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/api.lua | 6 +-- src/api/system/account.lua | 28 +++++++++----- src/service/system/account.lua | 70 +++++++++++++++++++++++++--------- src/service/system/user.lua | 16 ++++---- 4 files changed, 82 insertions(+), 38 deletions(-) diff --git a/src/api/api.lua b/src/api/api.lua index 0242622..b1b037a 100644 --- a/src/api/api.lua +++ b/src/api/api.lua @@ -42,10 +42,10 @@ local routes = { }, --账户相关路由接口 { - paths = { "/api/get-accounts" }, - metadata = { "metadata get-accounts" }, + paths = { "/api/get-account" }, + metadata = { "metadata get-account" }, methods = { "GET" }, - handler = accountApi.get_allaccounts, + handler = accountApi.get_allaccount, }, { paths = { "/api/get-account/:id" }, diff --git a/src/api/system/account.lua b/src/api/system/account.lua index ac374e3..3d6c03a 100644 --- a/src/api/system/account.lua +++ b/src/api/system/account.lua @@ -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,7 +35,7 @@ function _M.get_account(m) resp:send(result) end ---根据用户id获取用户信息 +--根据账号id获取账号信息 function _M.add_account() --获取请求头并进行校验 if checkReqHeader() == false then @@ -57,7 +57,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 +65,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 diff --git a/src/service/system/account.lua b/src/service/system/account.lua index 7933cb6..320a352 100644 --- a/src/service/system/account.lua +++ b/src/service/system/account.lua @@ -70,31 +70,29 @@ local function checkJson(jsonData) 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) @@ -112,8 +110,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 +127,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 diff --git a/src/service/system/user.lua b/src/service/system/user.lua index a40d2af..e36008b 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -72,7 +72,7 @@ end --通过查询条件判断数据库中的数据记录 local function checkUserExist(where) --组装sql语句 - 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) @@ -81,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 @@ -130,19 +130,19 @@ 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.update_user(id, jsonData) +function _M.updateUser(id, jsonData) --根据用户id进行验证用户是否存在 local where = string.format("where id='%s'", id) local ok, res = checkUserExist(where) @@ -172,7 +172,7 @@ function _M.update_user(id, jsonData) end local vals = tmp:sub(1, #tmp - 1) --组装sql语句 - local sql = string.format("update \"tbl_users\" set %s where id='%s'", vals, id) + local sql = string.format("update \"tbl_user\" set %s where id='%s'", vals, id) return execSQL(sql) end