修复图片验证码异常

pull/701/head
miaowuawa 1 year ago
parent c72f395722
commit f9093a51a6

@ -7,12 +7,10 @@ package cache
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"unsafe"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/redis/rueidis" "github.com/redis/rueidis"
"github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/core"
"time"
) )
var ( var (
@ -105,7 +103,10 @@ func (r *redisCache) SetImgCaptcha(ctx context.Context, id string, value string)
func (r *redisCache) GetImgCaptcha(ctx context.Context, id string) (string, error) { func (r *redisCache) GetImgCaptcha(ctx context.Context, id string) (string, error) {
res, err := r.c.Do(ctx, r.c.B().Get().Key(_imgCaptchaKey+id).Build()).AsBytes() res, err := r.c.Do(ctx, r.c.B().Get().Key(_imgCaptchaKey+id).Build()).AsBytes()
return unsafe.String(&res[0], len(res)), err if len(res) == 0 {
return "", err
}
return string(res), err
} }
func (r *redisCache) DelImgCaptcha(ctx context.Context, id string) error { func (r *redisCache) DelImgCaptcha(ctx context.Context, id string) error {

@ -45,8 +45,8 @@ func (s *pubSrv) SendCaptcha(req *web.SendCaptchaReq) error {
ctx := context.Background() ctx := context.Background()
// 验证图片验证码 // 验证图片验证码
if captcha, err := s.Redis.GetImgCaptcha(ctx, req.ImgCaptchaID); err != nil || string(captcha) != req.ImgCaptcha { if imgCaptcha, err := s.Redis.GetImgCaptcha(ctx, req.ImgCaptchaID); err != nil || imgCaptcha != req.ImgCaptcha {
logrus.Debugf("get captcha err:%s expect:%s got:%s", err, captcha, req.ImgCaptcha) logrus.Debugf("get imgCaptcha err:%s expect:%s got:%s", err, imgCaptcha, req.ImgCaptcha)
return web.ErrErrorCaptchaPassword return web.ErrErrorCaptchaPassword
} }
s.Redis.DelImgCaptcha(ctx, req.ImgCaptchaID) s.Redis.DelImgCaptcha(ctx, req.ImgCaptchaID)
@ -66,16 +66,16 @@ func (s *pubSrv) SendCaptcha(req *web.SendCaptchaReq) error {
} }
func (s *pubSrv) GetCaptcha() (*web.GetCaptchaResp, error) { func (s *pubSrv) GetCaptcha() (*web.GetCaptchaResp, error) {
cap := captcha.New() capt := captcha.New()
if err := cap.AddFontFromBytes(assets.ComicBytes); err != nil { if err := capt.AddFontFromBytes(assets.ComicBytes); err != nil {
logrus.Errorf("cap.AddFontFromBytes err:%s", err) logrus.Errorf("captcha.AddFontFromBytes err:%s", err)
return nil, xerror.ServerError return nil, xerror.ServerError
} }
cap.SetSize(160, 64) capt.SetSize(160, 64)
cap.SetDisturbance(captcha.MEDIUM) capt.SetDisturbance(captcha.MEDIUM)
cap.SetFrontColor(color.RGBA{0, 0, 0, 255}) capt.SetFrontColor(color.RGBA{0, 0, 0, 255})
cap.SetBkgColor(color.RGBA{218, 240, 228, 255}) capt.SetBkgColor(color.RGBA{218, 240, 228, 255})
img, password := cap.Create(6, captcha.NUM) img, password := capt.Create(6, captcha.NUM)
emptyBuff := bytes.NewBuffer(nil) emptyBuff := bytes.NewBuffer(nil)
if err := png.Encode(emptyBuff, img); err != nil { if err := png.Encode(emptyBuff, img); err != nil {
logrus.Errorf("png.Encode err:%s", err) logrus.Errorf("png.Encode err:%s", err)
@ -159,6 +159,7 @@ func (s *pubSrv) Login(req *web.LoginReq) (*web.LoginResp, error) {
return &web.LoginResp{ return &web.LoginResp{
Token: token, Token: token,
}, nil }, nil
} }
func (s *pubSrv) Version() (*web.VersionResp, error) { func (s *pubSrv) Version() (*web.VersionResp, error) {

Loading…
Cancel
Save