对相关函数进行封装,增加用户是否已经注册过判断
This commit is contained in:
parent
1ad62eb252
commit
896b001a2c
|
|
@ -26,6 +26,7 @@ return {
|
|||
[0x000004] = '未授权访问',
|
||||
|
||||
-- user module
|
||||
[0x010000] = '注册失败,用户已存在',
|
||||
[0x010001] = '注册失败,手机号已存在',
|
||||
[0x010002] = '登录失败,手机号或密码错误',
|
||||
[0x010003] = '登录失败,用户不存在',
|
||||
|
|
|
|||
|
|
@ -35,90 +35,17 @@ local function getUuid()
|
|||
return snow.int64_to_string(id)
|
||||
end
|
||||
|
||||
-- 查询数据表中的所有用户信息
|
||||
function _M.getAllUser()
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\""
|
||||
--获取数据库连接
|
||||
local code,conn = get_con(dbconf.postgres)
|
||||
--设置数据库的编码格式
|
||||
--conn:exec("SET NAMES UTF8")
|
||||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
print("get all users Query failed: "..sql)
|
||||
--数据库执行sql语句
|
||||
function _M.execSQL(sql)
|
||||
if sql == '' or sql == nil then
|
||||
return 0x000003,res
|
||||
end
|
||||
--整理数据库结果返回值
|
||||
--for _, row in ipairs(res) do
|
||||
-- for key, value in pairs(row) do
|
||||
-- ngx.say(key .. ":" .. tostring(value))
|
||||
-- end
|
||||
--end
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
return code,res
|
||||
end
|
||||
|
||||
--根据用户id获取用户信息
|
||||
function _M.getUser(id)
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\" where id="..id
|
||||
--获取数据库连接
|
||||
local code,conn = get_con(dbconf.postgres)
|
||||
--设置数据库的编码格式
|
||||
--conn:exec("SET NAMES UTF8")
|
||||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
print("Query failed: "..sql)
|
||||
return 0x000003,res
|
||||
end
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
return code,res
|
||||
end
|
||||
|
||||
--增加用户信息到数据表
|
||||
function _M.addUser(jsonData)
|
||||
--ngx.say(jsonData)
|
||||
local success, result = pcall(function()
|
||||
return cjson.decode(jsonData)
|
||||
end)
|
||||
local res = nil
|
||||
if success == false then
|
||||
return 0x000001,res
|
||||
end
|
||||
--解析json中的键和数据值
|
||||
local keys = ""
|
||||
local values = ""
|
||||
for key, value in pairs(result) do
|
||||
keys = keys..key
|
||||
local val = type(value)
|
||||
if val == "string" then
|
||||
values = values.."\'"..value.."\'"
|
||||
else
|
||||
values = values..value
|
||||
end
|
||||
keys = keys..","
|
||||
values = values..","
|
||||
end
|
||||
--去掉组装最后一位逗号(,)
|
||||
--local newKeys = keys:sub(1, #keys -1)
|
||||
--local newValues = values:sub(1, #values -1)
|
||||
--自己增加对应的uuid数据值
|
||||
local newKeys = keys.."uuid"
|
||||
local uuid = getUuid()
|
||||
local newValues = values.."\'"..uuid.."\'"
|
||||
--组装sql语句
|
||||
local sql = string.format("insert into \"T_Users\"(%s)values(%s)", newKeys, newValues)
|
||||
--ngx.say(sql)
|
||||
--获取数据库连接
|
||||
local code,conn = get_con(dbconf.postgres)
|
||||
--执行数据库操作
|
||||
res = conn:query(sql)
|
||||
if not res then
|
||||
print("adduser sql failed: "..sql)
|
||||
print("query sql failed: "..sql)
|
||||
return 0x000003,res
|
||||
end
|
||||
--关闭数据库
|
||||
|
|
@ -126,21 +53,81 @@ function _M.addUser(jsonData)
|
|||
return code,res
|
||||
end
|
||||
|
||||
--校验json数据的正确性,并返回json解析后的数据
|
||||
local function checkJson(jsonData)
|
||||
local success, result = pcall(function()
|
||||
return cjson.decode(jsonData)
|
||||
end)
|
||||
local res = nil
|
||||
return success,res
|
||||
end
|
||||
|
||||
--通过查询条件判断数据库中的数据记录
|
||||
--根据用户、手机号、邮箱进行验证用户是否存在
|
||||
local function checkUserExist(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(*) from \"T_Users\" %s", where)
|
||||
--获取数据库连接
|
||||
return execSQL(sql)
|
||||
end
|
||||
|
||||
-- 查询数据表中的所有用户信息
|
||||
function _M.getAllUser()
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\""
|
||||
return execSQL(sql)
|
||||
end
|
||||
|
||||
--根据用户id获取用户信息
|
||||
function _M.getUser(uuid)
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\" where uuid="..uuid
|
||||
return execSQL(sql)
|
||||
end
|
||||
|
||||
--增加用户信息到数据表
|
||||
function _M.delete_user(id)
|
||||
--组装sql语句
|
||||
local sql = "delete from \"T_Users\" where id="..id
|
||||
--获取数据库连接
|
||||
local code,conn = get_con(dbconf.postgres)
|
||||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
print("delete exec sql failed: "..sql)
|
||||
return 0x000003,res
|
||||
function _M.addUser(jsonData)
|
||||
--验证数据的正确性,错误时返回
|
||||
local success, result = checkJson(jsonData)
|
||||
if success == false then
|
||||
return 0x000001,result
|
||||
end
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
return code,res
|
||||
--解析json中的键和数据值
|
||||
local keys, 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 = checkUserExist(username, phone, email)
|
||||
if ok == false then
|
||||
return 0x000001,res
|
||||
end
|
||||
local count = 0
|
||||
for _k, _v in pairs(res) do
|
||||
count = _v
|
||||
end
|
||||
if count > 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.delete_user(uuid)
|
||||
--组装sql语句
|
||||
local sql = "delete from \"T_Users\" where uuid="..uuid
|
||||
return execSQL(sql)
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -11,6 +11,12 @@ local snow = snowflake.new(workerId, datacenterId)
|
|||
local id = snow:generateUniqueId()-- 生成ID
|
||||
ngx.say("Generated ID:"..snow.int64_to_string(id))
|
||||
|
||||
--max =a and b or c--a?b:c
|
||||
|
||||
--去掉组装最后一位逗号(,)
|
||||
--local newKeys = keys:sub(1, #keys -1)
|
||||
--local newValues = values:sub(1, #values -1)
|
||||
|
||||
--读取请求体的数据
|
||||
--ngx.req.read_body()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user