修改用户返回数据和数据表相关的内容,对用户进行是否存在判断

This commit is contained in:
wanglei 2025-10-23 17:46:02 +08:00
parent a3a7ee26ea
commit 6f60a6a49b
4 changed files with 53 additions and 8 deletions

View File

@ -42,8 +42,8 @@ local routes = {
},
--账户相关路由接口
{
paths = { "/api/get-account" },
metadata = { "metadata get-account" },
paths = { "/api/get-accounts" },
metadata = { "metadata get-accounts" },
methods = { "GET" },
handler = accountApi.get_allaccounts,
},

View File

@ -68,7 +68,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

@ -70,10 +70,8 @@ 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)
print("check sql: "..sql)
--获取数据库连接
@ -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
@ -142,4 +141,39 @@ function _M.delete_user(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