48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package common
 | |
| 
 | |
| import (
 | |
| 	"net/http"
 | |
| 
 | |
| 	"management/internal/erpserver/handler"
 | |
| 	v1 "management/internal/erpserver/service/v1"
 | |
| )
 | |
| 
 | |
| type CaptchaHandler struct {
 | |
| 	*handler.Handler
 | |
| 	captchaService v1.CaptchaService
 | |
| }
 | |
| 
 | |
| func NewCaptchaHandler(handler *handler.Handler, captchaService v1.CaptchaService) *CaptchaHandler {
 | |
| 	return &CaptchaHandler{
 | |
| 		Handler:        handler,
 | |
| 		captchaService: captchaService,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| 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, _ *http.Request) {
 | |
| 	id, b64s, _, err := h.captchaService.Generate(
 | |
| 		h.Config.Captcha.ImgHeight,
 | |
| 		h.Config.Captcha.ImgWidth,
 | |
| 		h.Config.Captcha.KeyLong,
 | |
| 		0.7, 80)
 | |
| 	if err != nil {
 | |
| 		h.JSONErr(w, "获取验证码失败")
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	rsp := CaptchaResponse{
 | |
| 		CaptchaID:     id,
 | |
| 		PicPath:       b64s,
 | |
| 		CaptchaLength: h.Config.Captcha.KeyLong,
 | |
| 		OpenCaptcha:   h.Config.Captcha.OpenCaptcha,
 | |
| 	}
 | |
| 	h.JSONObj(w, "ok", rsp)
 | |
| }
 |