143 lines
4.1 KiB
Lua
143 lines
4.1 KiB
Lua
---
|
||
--- 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')
|
||
|
||
local roles = require("dao.system.role")
|
||
|
||
local _M = {}
|
||
|
||
local user = {
|
||
["ID"] = "",
|
||
["type"] = 0,
|
||
}
|
||
|
||
--判断用户是否存在
|
||
local function isExistUser(id)
|
||
--根据用户id进行验证用户是否存在
|
||
local code, res = userModel:find(id)
|
||
if code ~= 0 or res == nil then
|
||
return 0x000001, res
|
||
end
|
||
local num = table.getn(res)
|
||
--用户不存在返回错误
|
||
if num <= 0 then
|
||
return false
|
||
end
|
||
return true
|
||
end
|
||
|
||
--通过用户名验证用户是否存在
|
||
function _M:getUserByUsername(username)
|
||
local code, res = userModel:where("username", "=", username):orwhere("phone", "=", username):orwhere("email", "=", username):get()
|
||
if code ~= 0 then
|
||
return 0x000001,res
|
||
end
|
||
return code, res
|
||
end
|
||
|
||
-- 查询数据表中的所有用户信息
|
||
function _M:getSystemUsers(pageNum, pageSize)
|
||
return userModel:paginate(pageNum, pageSize)
|
||
end
|
||
|
||
--根据用户id获取用户信息
|
||
function _M:getSystemUser(id)
|
||
return userModel:find(id)
|
||
end
|
||
|
||
--增加用户信息到数据表
|
||
function _M:addSystemUser(jsonData)
|
||
if jsonData == nil or jsonData == "" then
|
||
return 0x000001, nil
|
||
end
|
||
--解析json中的键和数据值
|
||
local userName = jsonData['username']
|
||
local phone = jsonData['phone']
|
||
local email = jsonData['email']
|
||
|
||
--根据用户、手机号、邮箱进行验证用户是否存在
|
||
local code, res = self.getUserByUsername(userName)
|
||
if code ~= 0 or res == nil then
|
||
return 0x000001, res
|
||
end
|
||
local num = table.getn(res)
|
||
--用户存在时返回用户已经存在
|
||
if num > 0 then
|
||
return 0x01000C,nil
|
||
end
|
||
|
||
--键值为id产生uuid数据值,增加到json中
|
||
jsonData.id = helpers.getUuid()
|
||
--用户密码暂时使用md5进行加密 使用明文暂时注释掉加密
|
||
--local pwd = jsonData['password']
|
||
--jsonData.password = ngx.md5(pwd)
|
||
-- 创建一个用户
|
||
return userModel:create(jsonData)
|
||
end
|
||
|
||
--删除用户信息到数据表
|
||
function _M:deleteSystemUser(id)
|
||
--根据用户id进行验证用户是否存在
|
||
local ok = isExistUser(id)
|
||
--用户不存在则返回
|
||
if ok == false then
|
||
return 0x000001,nil
|
||
end
|
||
return userModel:delete(id)
|
||
end
|
||
|
||
--更新用户信息到数据表
|
||
function _M:updateSystemUser(id, jsonData)
|
||
--根据用户id进行验证用户是否存在
|
||
local ok = isExistUser(id)
|
||
--用户不存在则返回
|
||
if ok == false then
|
||
return 0x000001,nil
|
||
end
|
||
--对数据内容进行更新
|
||
return userModel:where('id', '=', id):update(jsonData)
|
||
end
|
||
|
||
--通过用户名和密码验证用户是否存在
|
||
function _M:adjustUser(name, passwd)
|
||
if name == nil or passwd == nil then
|
||
return 0x010003, nil
|
||
end
|
||
local pwdMd5 = passwd--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
|
||
|
||
return _M |