change router struct
This commit is contained in:
50
internal/erpserver/handler/captcha/captcha.go
Normal file
50
internal/erpserver/handler/captcha/captcha.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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)
|
||||
}
|
||||
20
internal/erpserver/handler/captcha/route.go
Normal file
20
internal/erpserver/handler/captcha/route.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
v1 "management/internal/erpserver/service/v1"
|
||||
"management/internal/pkg/config"
|
||||
"management/internal/pkg/render"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Conf *config.Config
|
||||
Render render.Renderer
|
||||
CaptchaService v1.CaptchaService
|
||||
}
|
||||
|
||||
func Routes(r chi.Router, cfg Config) {
|
||||
app := NewApp(cfg.Conf, cfg.Render, cfg.CaptchaService)
|
||||
r.Get("/captcha", app.captcha)
|
||||
}
|
||||
Reference in New Issue
Block a user