用户登录业务逻辑,接收前端用户名从数据表中获取数据,然后取出密码进行md5加密,再与前端传的密码进行比对,相等则进入系统

This commit is contained in:
wanglei 2025-11-12 15:27:52 +08:00
parent ece64e90f3
commit 0508599f34
2 changed files with 31 additions and 12 deletions

View File

@ -9,17 +9,22 @@ local _M = {}
--认证用户返回用户数据信息
local function authenticate(name, passwd)
--验证用户名是否为空
if name == "" then
--验证用户名或者密码是否为空
if name == "" or passwd == "" then
return 0x010003, nil
end
--验证密码是否为空
if passwd == "" then
return 0x010002, nil
end
return userDao:adjustUser(name, passwd)
end
--根据用户名称获取用户信息
local function getUserByUsername(username)
--验证用户名否为空
if username == "" then
return 0x010003, nil
end
return userDao:getUserByUsername(username)
end
--用户登录业务逻辑处理
function _M.login(jsonData)
--解析json中的键和数据值
@ -28,7 +33,7 @@ function _M.login(jsonData)
local captcha = jsonData["captcha"]
local checkKey = jsonData["checkKey"]
--验证用户名是否为空
local code, res = authenticate(name, passwd)
local code, res = userDao:getUserByUsername(name) --authenticate(name, passwd)
if code ~= 0 then
return 0x000001,res
end
@ -36,10 +41,15 @@ function _M.login(jsonData)
if res ~= nil then
num = table.getn(res)
end
--用户存在时返回用户已经存在
--用户存在时返回
if num <= 0 then
return 0x01000C,nil
end
--进行密码验证 获取数据库的密码
local pwdMd5 = ngx.md5(res[1].password)
if pwdMd5 ~= passwd then
return 0x000001,res --密码错误
end
local userid = res[1].id
--获取用户id查询角色信息
local err, rest = userDao:userRole(userid)

View File

@ -37,6 +37,15 @@ local function isExistUser(id)
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)
@ -58,7 +67,7 @@ function _M.addSystemUser(jsonData)
local email = jsonData['email']
--根据用户、手机号、邮箱进行验证用户是否存在
local code, res = userModel:where("username", "=", userName):orwhere("phone", "=", phone):orwhere("email", "=", email):get()
local code, res = self.getUserByUsername(userName)
if code ~= 0 then
return 0x000001,res
end
@ -73,9 +82,9 @@ function _M.addSystemUser(jsonData)
--键值为id产生uuid数据值增加到json中
jsonData.id = helpers.getUuid()
--用户密码暂时使用md5进行加密
local pwd = jsonData['password']
jsonData.password = ngx.md5(pwd)
--用户密码暂时使用md5进行加密 使用明文暂时注释掉加密
--local pwd = jsonData['password']
--jsonData.password = ngx.md5(pwd)
-- 创建一个用户
return userModel:create(jsonData)
end