v2
This commit is contained in:
56
internal/erpserver/handler/common/captcha.go
Normal file
56
internal/erpserver/handler/common/captcha.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"management/internal/config"
|
||||
commonv1 "management/internal/erpserver/biz/v1/common"
|
||||
"management/internal/pkg/tpl"
|
||||
)
|
||||
|
||||
type CaptchaHandler interface {
|
||||
Captcha(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// captchaHandler 是 CaptchaHandler 接口的实现.
|
||||
type captchaHandler struct {
|
||||
conf *config.Captcha
|
||||
render tpl.Renderer
|
||||
biz commonv1.CaptchaBiz
|
||||
}
|
||||
|
||||
// 确保 captchaHandler 实现了 CaptchaHandler 接口.
|
||||
var _ CaptchaHandler = (*captchaHandler)(nil)
|
||||
|
||||
func NewCaptchaHandler(conf *config.Captcha, render tpl.Renderer, biz commonv1.CaptchaBiz) *captchaHandler {
|
||||
return &captchaHandler{
|
||||
conf: conf,
|
||||
render: render,
|
||||
biz: biz,
|
||||
}
|
||||
}
|
||||
|
||||
type CaptchaResponse struct {
|
||||
CaptchaID string `json:"captcha_id"`
|
||||
PicPath string `json:"pic_path"`
|
||||
CaptchaLength int `json:"captcha_length"`
|
||||
OpenCaptcha int `json:"open_captcha"`
|
||||
}
|
||||
|
||||
func (h *captchaHandler) Captcha(w http.ResponseWriter, r *http.Request) {
|
||||
keyLong := h.conf.KeyLong
|
||||
oc := h.conf.OpenCaptcha
|
||||
id, b64s, _, err := h.biz.Generate(h.conf.ImgHeight, h.conf.ImgWidth, keyLong, 0.7, 80)
|
||||
if err != nil {
|
||||
h.render.JSON(w, tpl.Response{Success: false, Message: "获取验证码失败"})
|
||||
return
|
||||
}
|
||||
|
||||
rsp := CaptchaResponse{
|
||||
CaptchaID: id,
|
||||
PicPath: b64s,
|
||||
CaptchaLength: keyLong,
|
||||
OpenCaptcha: oc,
|
||||
}
|
||||
h.render.JSON(w, tpl.Response{Success: true, Message: "ok", Data: rsp})
|
||||
}
|
||||
31
internal/erpserver/handler/common/common.go
Normal file
31
internal/erpserver/handler/common/common.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"management/internal/config"
|
||||
commonv1 "management/internal/erpserver/biz/v1/common"
|
||||
"management/internal/pkg/tpl"
|
||||
)
|
||||
|
||||
type CommonHandler interface {
|
||||
CaptchaHandler() CaptchaHandler
|
||||
}
|
||||
|
||||
type commonHandler struct {
|
||||
conf *config.Config
|
||||
render tpl.Renderer
|
||||
biz commonv1.CommonBiz
|
||||
}
|
||||
|
||||
var _ CommonHandler = (*commonHandler)(nil)
|
||||
|
||||
func NewCommonHandler(conf *config.Config, render tpl.Renderer, biz commonv1.CommonBiz) *commonHandler {
|
||||
return &commonHandler{
|
||||
conf: conf,
|
||||
render: render,
|
||||
biz: biz,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *commonHandler) CaptchaHandler() CaptchaHandler {
|
||||
return NewCaptchaHandler(&h.conf.Captcha, h.render, h.biz.CaptchaBiz())
|
||||
}
|
||||
Reference in New Issue
Block a user