2025-10-29 17:29:17 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by admin.
|
|
|
|
|
|
--- DateTime: 2025/10/25 16:36
|
|
|
|
|
|
--- 数据表模型文件
|
|
|
|
|
|
|
|
|
|
|
|
local helpers = require("share.helpers")
|
|
|
|
|
|
--引用使用的库文件
|
|
|
|
|
|
local model = require("share.model")
|
|
|
|
|
|
--创建一个数据表相关的模型
|
|
|
|
|
|
local userModel = model:new('sys_user')
|
|
|
|
|
|
|
2025-11-07 17:06:39 +08:00
|
|
|
|
local roles = require("dao.role")
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
2025-11-01 15:38:37 +08:00
|
|
|
|
local user = {
|
|
|
|
|
|
["ID"] = "",
|
|
|
|
|
|
["type"] = 0,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--判断用户是否存在
|
|
|
|
|
|
local function isExistUser(id)
|
|
|
|
|
|
--根据用户id进行验证用户是否存在
|
|
|
|
|
|
local code, res = userModel:find(id)
|
|
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return false
|
|
|
|
|
|
end
|
|
|
|
|
|
local num = 0
|
|
|
|
|
|
if res ~= nil then
|
|
|
|
|
|
num = table.getn(res)
|
|
|
|
|
|
end
|
|
|
|
|
|
--用户不存在返回错误
|
|
|
|
|
|
if num <= 0 then
|
|
|
|
|
|
return false
|
|
|
|
|
|
end
|
|
|
|
|
|
return true
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 查询数据表中的所有用户信息
|
2025-10-29 23:00:17 +08:00
|
|
|
|
function _M.getSystemUsers(pageNum, pageSize)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
return userModel:paginate(pageNum, pageSize)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据用户id获取用户信息
|
2025-10-29 23:00:17 +08:00
|
|
|
|
function _M.getSystemUser(id)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
return userModel:find(id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--增加用户信息到数据表
|
2025-10-29 23:00:17 +08:00
|
|
|
|
function _M.addSystemUser(jsonData)
|
2025-10-31 21:32:39 +08:00
|
|
|
|
if jsonData == nil or jsonData == "" then
|
|
|
|
|
|
return 0x000001, nil
|
|
|
|
|
|
end
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--解析json中的键和数据值
|
|
|
|
|
|
local userName = jsonData['username']
|
|
|
|
|
|
local phone = jsonData['phone']
|
|
|
|
|
|
local email = jsonData['email']
|
|
|
|
|
|
|
|
|
|
|
|
--根据用户、手机号、邮箱进行验证用户是否存在
|
2025-10-31 21:32:39 +08:00
|
|
|
|
local code, res = userModel:where("username", "=", userName):orwhere("phone", "=", phone):orwhere("email", "=", email):get()
|
2025-10-29 17:29:17 +08:00
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return 0x000001,res
|
|
|
|
|
|
end
|
|
|
|
|
|
local num = 0
|
|
|
|
|
|
if res ~= nil then
|
|
|
|
|
|
num = table.getn(res)
|
|
|
|
|
|
end
|
|
|
|
|
|
--用户存在时返回用户已经存在
|
|
|
|
|
|
if num > 0 then
|
|
|
|
|
|
return 0x01000C,nil
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--键值为id产生uuid数据值,增加到json中
|
|
|
|
|
|
jsonData.id = helpers.getUuid()
|
2025-11-04 21:57:42 +08:00
|
|
|
|
--用户密码暂时使用md5进行加密
|
|
|
|
|
|
local pwd = jsonData['password']
|
2025-11-04 22:24:08 +08:00
|
|
|
|
jsonData.password = ngx.md5(pwd)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
-- 创建一个用户
|
2025-10-31 21:32:39 +08:00
|
|
|
|
return userModel:create(jsonData)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--删除用户信息到数据表
|
2025-10-29 23:00:17 +08:00
|
|
|
|
function _M:deleteSystemUser(id)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--根据用户id进行验证用户是否存在
|
|
|
|
|
|
local ok = isExistUser(id)
|
|
|
|
|
|
--用户不存在则返回
|
|
|
|
|
|
if ok == false then
|
|
|
|
|
|
return 0x000001,nil
|
|
|
|
|
|
end
|
|
|
|
|
|
return userModel:delete(id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--更新用户信息到数据表
|
2025-10-29 23:00:17 +08:00
|
|
|
|
function _M:updateSystemUser(id, jsonData)
|
2025-10-29 17:29:17 +08:00
|
|
|
|
--根据用户id进行验证用户是否存在
|
|
|
|
|
|
local ok = isExistUser(id)
|
|
|
|
|
|
--用户不存在则返回
|
|
|
|
|
|
if ok == false then
|
|
|
|
|
|
return 0x000001,nil
|
|
|
|
|
|
end
|
|
|
|
|
|
--对数据内容进行更新
|
|
|
|
|
|
return userModel:where('id', '=', id):update(jsonData)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-11-07 17:06:39 +08:00
|
|
|
|
--通过用户名和密码验证用户是否存在
|
|
|
|
|
|
function _M:adjustUser(name, passwd)
|
|
|
|
|
|
if name == nil or passwd == nil then
|
|
|
|
|
|
return 0x010003, nil
|
|
|
|
|
|
end
|
|
|
|
|
|
local pwdMd5 = ngx.md5(passwd)
|
|
|
|
|
|
--根据用户进行验证用户是否存在
|
|
|
|
|
|
local code, res = userModel:where("username", "=", name):where("password", "=", pwdMd5):get()
|
|
|
|
|
|
if code == 0 and res ~= nil then
|
|
|
|
|
|
return code, res
|
|
|
|
|
|
end
|
|
|
|
|
|
--根据手机号进行验证用户是否存在
|
|
|
|
|
|
code, res = userModel:where("phone", "=", name):where("password", "=", pwdMd5):get()
|
|
|
|
|
|
if code == 0 and res ~= nil then
|
|
|
|
|
|
return code, res
|
|
|
|
|
|
end
|
|
|
|
|
|
--根据邮箱进行验证用户是否存在
|
|
|
|
|
|
code, res = userModel:where("email", "=", name):where("password", "=", pwdMd5):get()
|
|
|
|
|
|
if code == 0 and res ~= nil then
|
|
|
|
|
|
return code, res
|
|
|
|
|
|
end
|
|
|
|
|
|
--查询不到用户信息
|
|
|
|
|
|
return 0x010003, nil
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--通过用户id获取角色的角色id和角色名称
|
|
|
|
|
|
function _M:userRole(id)
|
|
|
|
|
|
local sql = [[SELECT "a"."id","a".username,b."id" AS role_id,b.role_name FROM
|
|
|
|
|
|
sys_user AS "a" INNER JOIN sys_user_role AS "c" ON "a"."id" = "c".user_id
|
|
|
|
|
|
INNER JOIN sys_role AS b ON "c".role_id = b."id" WHERE
|
|
|
|
|
|
"a"."id" = ']]..id.."'"
|
|
|
|
|
|
return userModel:exec(sql)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
return _M
|