修改用户和账户接口中增加用户时自动增加使用的id信息,增加将json数据转换成表形式,便于数据操作接口新增数据

This commit is contained in:
wanglei 2025-10-27 17:17:58 +08:00
parent df3a0f6adf
commit 6512b2264c
4 changed files with 82 additions and 42 deletions

View File

@ -46,12 +46,11 @@ function _M.addAccount(jsonData)
if num <= 0 then
return 0x01000C, nil
end
--自己增加对应的uuid数据值
local newKeys = "id"
local newValues = "'"..helpers.getUuid().."'"
--键值为id产生uuid数据值增加到json中
result.id = helpers.getUuid()
local ret = helpers.convert_json(result)
-- 创建一个账号
return account:create(jsonData)
return account:create('{'..ret..'}')
end
--增加账号信息到数据表

View File

@ -50,12 +50,11 @@ function _M.addUser(jsonData)
return 0x01000C,nil
end
--产生uuid数据值增加到json中
local newKeys = "id"
local newValues = "'"..helpers.getUuid().."'"
--键值为id产生uuid数据值增加到json中
result.id = helpers.getUuid()
local ret = helpers.convert_json(result)
-- 创建一个用户
return user:create(jsonData)
return user:create('{'..ret..'}')
end
--增加用户信息到数据表

View File

@ -4,6 +4,7 @@
--- DateTime: 2025/10/15 09:12
---
--local snowflake = require("util.snowflake")
local helpers = require("util.helpers")
--
--local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
--local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
@ -13,6 +14,7 @@
--max =a and b or c--a?b:c
--[[
local User = require("model.user")
--获取数据表中的记录数
@ -88,14 +90,28 @@ for _, row in ipairs(data.data) do
ngx.say(key .. ":" .. tostring(value))
end
end
--]]
--获取请求参数的键值和数据值
--local cjson = require("cjson")
local cjson = require("cjson")
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--ngx.say(body_data)
local data = cjson.decode(body_data)
--键值为id产生uuid数据值增加到json中
data.id = helpers.getUuid()
local ret = helpers.convert_json(data)
ngx.say(ret)
--local header = ngx.req.get_headers()
--for k,v in pairs(header) do
-- ngx.say("[header] name:", k, "value:", v)
--end
--
--local payloads = ngx.req.get_uri_args()
--for k,v in pairs(payloads) do
-- ngx.say("[params] name:", k, " value:", v)
@ -105,42 +121,45 @@ end
--local newKeys = keys:sub(1, #keys -1)
--local newValues = values:sub(1, #values -1)
--[[
--读取请求体的数据
--ngx.req.read_body()
--获取请求数据
--local body_data = ngx.req.get_body_data()
local body_data = ngx.req.get_body_data()
--if not body_data then
-- ngx.say("read file error:", err)
-- return ngx.exit(400)
--end
--local len = #body_data
if not body_data then
ngx.say("read file error:", err)
return ngx.exit(400)
end
local len = #body_data
--local file_name = ngx.req.get_body_file()
--ngx.say("file length:", len)
local file_name = ngx.req.get_body_file()
ngx.say("file length:", len)
--ngx.req.read_body_in_buffer(ngx.var.request_body_file)
ngx.req.read_body_in_buffer(ngx.var.request_body_file)
--ngx.say(body_data)
ngx.say(body_data)
--]]
--local cjson = require("cjson")
--local file_path = "/home/frankly/work/test.dat"
--local file_length = 1024 * 1024 * 400
--local f, err = io.input(file_path, "r")
--if not f then
-- ngx.say("read file error:"..err)
-- return
--end
--local content = f:read(file_length) --读取文件内容
--f:close() --关闭文件
----ngx.say(content)
--local res = {
-- key = "data",
-- value = content
--}
----ngx.header["Length"] = #content
--ngx.header["Content-Type"] = 'application/json; charset=UTF-8'
--ngx.say(cjson.encode(res))
--ngx.log(ngx.INFO, "send data success")
--[[
local cjson = require("cjson")
local file_path = "/home/frankly/work/test.dat"
local file_length = 1024 * 1024 * 400
local f, err = io.input(file_path, "r")
if not f then
ngx.say("read file error:"..err)
return
end
local content = f:read(file_length) --读取文件内容
f:close() --关闭文件
--ngx.say(content)
local res = {
key = "data",
value = content
}
--ngx.header["Length"] = #content
ngx.header["Content-Type"] = 'application/json; charset=UTF-8'
ngx.say(cjson.encode(res))
ngx.log(ngx.INFO, "send data success")
--]]

View File

@ -155,8 +155,31 @@ local function log(...)
ngx.log(ngx.WARN, cjson.encode(args))
end
--转换json数据到 key=value形式
local function convert_json(t)
local arr = {}
local function convert_subtable(json_data, prefix)
for k, v in pairs(json_data) do
k = prefix..tostring(k)
if type(v) == 'number' or type(v) == 'boolean' then
table.insert(arr, k..'='..v)
elseif type(v) == 'table' then
convert_subtable(v, prefix..k..'_')
else
table.insert(arr, k..'='..string.format('%q', tostring(v)))
end
end
end
convert_subtable(t, '')
table.sort(arr)
return table.concat(arr, ',')
end
_M.getUuid = getUuid
_M.log = log
_M.convert_json = convert_json
_M.trim = trim
_M.get_cookie = get_cookie
_M.set_cookie = set_cookie