51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package captcha
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
v1 "management/internal/erpserver/service/v1"
|
|
"management/internal/pkg/config"
|
|
"management/internal/pkg/render"
|
|
)
|
|
|
|
type app struct {
|
|
config *config.Config
|
|
render render.Renderer
|
|
captchaService v1.CaptchaService
|
|
}
|
|
|
|
func newApp(config *config.Config, render render.Renderer, captchaService v1.CaptchaService) *app {
|
|
return &app{
|
|
config: config,
|
|
render: render,
|
|
captchaService: captchaService,
|
|
}
|
|
}
|
|
|
|
type Response struct {
|
|
CaptchaID string `json:"captcha_id"`
|
|
PicPath string `json:"pic_path"`
|
|
CaptchaLength int `json:"captcha_length"`
|
|
OpenCaptcha int `json:"open_captcha"`
|
|
}
|
|
|
|
func (a *app) captcha(w http.ResponseWriter, _ *http.Request) {
|
|
id, b64s, _, err := a.captchaService.Generate(
|
|
a.config.Captcha.ImgHeight,
|
|
a.config.Captcha.ImgWidth,
|
|
a.config.Captcha.KeyLong,
|
|
0.7, 80)
|
|
if err != nil {
|
|
a.render.JSONErr(w, "获取验证码失败")
|
|
return
|
|
}
|
|
|
|
rsp := Response{
|
|
CaptchaID: id,
|
|
PicPath: b64s,
|
|
CaptchaLength: a.config.Captcha.KeyLong,
|
|
OpenCaptcha: a.config.Captcha.OpenCaptcha,
|
|
}
|
|
a.render.JSONObj(w, "ok", rsp)
|
|
}
|