80 lines
2.1 KiB
Lua
80 lines
2.1 KiB
Lua
---
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
--- Created by admin.
|
|
--- DateTime: 2025/11/21 11:55
|
|
--- 验证码相关函数
|
|
|
|
local _M = {}
|
|
|
|
local status = require("util.status")
|
|
local resp = require("util.response")
|
|
local genpic = require("util.generatechaptcha")
|
|
local red = require("share.redis")
|
|
local validator = require("validator.system.captcha")
|
|
|
|
--生成图片文件
|
|
function _M.getCaptcha()
|
|
local filename, base64_data, captchaText = genpic.get_captcha_base64()
|
|
if base64_data == nil then
|
|
ngx.status = 500
|
|
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
|
|
end
|
|
--将文件名存储到redis中 超时时间5分钟
|
|
local key = "captcha-"..filename
|
|
red:set(key, captchaText)
|
|
red:expire(key, 5 * 60)
|
|
|
|
-- 4.返回结果
|
|
local ret = {
|
|
picgid = filename,
|
|
pic = base64_data
|
|
}
|
|
resp:response(status.SUCCESS, ret)
|
|
end
|
|
|
|
--验证图片上的验证码
|
|
function _M.checkCaptcha()
|
|
--获取请求中参数
|
|
local args = ngx.req.get_uri_args()
|
|
-- 校验客户端请求参数
|
|
local ok = validator.validateCheck(args)
|
|
--验证失败则返回
|
|
if not ok then
|
|
ngx.exit(ngx.HTTP_BAD_REQUEST)
|
|
end
|
|
local picgid = args["picgid"]
|
|
local code = args["code"]
|
|
local key = "captcha-"..picgid
|
|
local captchaText, err = red:get(key)
|
|
if not captchaText or captchaText == ngx.null then
|
|
resp:response(status.AUTH_CODE_INVALID)
|
|
return
|
|
end
|
|
if captchaText ~= string.upper(code) then
|
|
--print("captchaText:", captchaText, " code:", code)
|
|
resp:response(status.AUTH_CODE_ERROR)
|
|
red:del(key)
|
|
return
|
|
end
|
|
red:del(key)
|
|
resp:response(status.SUCCESS)
|
|
end
|
|
|
|
--验证码删除
|
|
function _M.deleteCaptcha()
|
|
--获取请求中参数
|
|
local args = ngx.req.get_uri_args()
|
|
-- 校验客户端请求参数
|
|
local ok = validator.validateAuthorize(args)
|
|
--验证失败则返回
|
|
if not ok then
|
|
ngx.exit(ngx.HTTP_BAD_REQUEST)
|
|
end
|
|
--其中image为要删除的验证码图片的picgid。
|
|
local picgid = args["picgid"]
|
|
local key = "captcha-"..picgid
|
|
red:del(key)
|
|
resp:response(status.SUCCESS)
|
|
end
|
|
|
|
return _M |