AuthPlatform/src/dao/user.lua

140 lines
3.9 KiB
Lua
Raw Normal View History

---
--- 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.role")
local _M = {}
local user = {
["ID"] = "",
["type"] = 0,
}
--判断用户是否存在
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
-- 查询数据表中的所有用户信息
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 = userModel:where("username", "=", userName):orwhere("phone", "=", phone):orwhere("email", "=", email):get()
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()
--用户密码暂时使用md5进行加密
local pwd = jsonData['password']
2025-11-04 22:24:08 +08:00
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 = 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