🏗️ microservice fabric splitting
This commit is contained in:
50
common/captcha/generate/generate_basic_text_captcha.go
Normal file
50
common/captcha/generate/generate_basic_text_captcha.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha-assets/helper"
|
||||
"github.com/wenlng/go-captcha/v2/click"
|
||||
)
|
||||
|
||||
// GenerateBasicTextCaptcha generates a basic text captcha and saves it to redis.
|
||||
func GenerateBasicTextCaptcha(capt click.Captcha, redis redis.Client, ctx context.Context) (map[string]interface{}, error) {
|
||||
captData, err := capt.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dotData := captData.GetData()
|
||||
if dotData == nil {
|
||||
return nil, errors.New("captcha data is nil")
|
||||
}
|
||||
var masterImageBase64, thumbImageBase64 string
|
||||
masterImageBase64, err = captData.GetMasterImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
thumbImageBase64, err = captData.GetThumbImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dotsByte, err := json.Marshal(dotData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := helper.StringToMD5(string(dotsByte))
|
||||
err = redis.Set(ctx, constant.UserCaptchaPrefix+key, dotsByte, time.Minute).Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"key": key,
|
||||
"image": masterImageBase64,
|
||||
"thumb": thumbImageBase64,
|
||||
}, nil
|
||||
|
||||
}
|
49
common/captcha/generate/generate_click_shape_captcha.go
Normal file
49
common/captcha/generate/generate_click_shape_captcha.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha-assets/helper"
|
||||
"github.com/wenlng/go-captcha/v2/click"
|
||||
)
|
||||
|
||||
// GenerateClickShapeCaptcha generate click shape captcha
|
||||
func GenerateClickShapeCaptcha(click click.Captcha, redis redis.Client, ctx context.Context) (map[string]interface{}, error) {
|
||||
captData, err := click.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dotData := captData.GetData()
|
||||
if dotData == nil {
|
||||
return nil, errors.New("captcha data is nil")
|
||||
}
|
||||
var masterImageBase64, thumbImageBase64 string
|
||||
masterImageBase64, err = captData.GetMasterImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
thumbImageBase64, err = captData.GetThumbImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dotsByte, err := json.Marshal(dotData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := helper.StringToMD5(string(dotsByte))
|
||||
err = redis.Set(ctx, constant.UserCaptchaPrefix+key, dotsByte, time.Minute).Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"key": key,
|
||||
"image": masterImageBase64,
|
||||
"thumb": thumbImageBase64,
|
||||
}, nil
|
||||
}
|
46
common/captcha/generate/generate_rotate_captcha.go
Normal file
46
common/captcha/generate/generate_rotate_captcha.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha-assets/helper"
|
||||
"github.com/wenlng/go-captcha/v2/rotate"
|
||||
)
|
||||
|
||||
// GenerateRotateCaptcha generate rotate captcha
|
||||
func GenerateRotateCaptcha(captcha rotate.Captcha, redis *redis.Client, ctx context.Context) (map[string]interface{}, error) {
|
||||
captchaData, err := captcha.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blockData := captchaData.GetData()
|
||||
if blockData == nil {
|
||||
return nil, nil
|
||||
}
|
||||
masterImageBase64, err := captchaData.GetMasterImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
thumbImageBase64, err := captchaData.GetThumbImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dotsByte, err := json.Marshal(blockData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := helper.StringToMD5(string(dotsByte))
|
||||
err = redis.Set(ctx, constant.UserCaptchaPrefix+key, dotsByte, time.Minute).Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"key": key,
|
||||
"image": masterImageBase64,
|
||||
"thumb": thumbImageBase64,
|
||||
}, nil
|
||||
}
|
54
common/captcha/generate/generate_slide_basic_captcha.go
Normal file
54
common/captcha/generate/generate_slide_basic_captcha.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha-assets/helper"
|
||||
"github.com/wenlng/go-captcha/v2/slide"
|
||||
)
|
||||
|
||||
// GenerateSlideBasicCaptcha generate slide basic captcha
|
||||
func GenerateSlideBasicCaptcha(slide slide.Captcha, redis *redis.Client, ctx context.Context) (map[string]interface{}, error) {
|
||||
captData, err := slide.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blockData := captData.GetData()
|
||||
if blockData == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var masterImageBase64, tileImageBase64 string
|
||||
masterImageBase64, err = captData.GetMasterImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tileImageBase64, err = captData.GetTileImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dotsByte, err := json.Marshal(blockData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := helper.StringToMD5(string(dotsByte))
|
||||
err = redis.Set(ctx, constant.UserCaptchaPrefix+key, dotsByte, time.Minute).Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"key": key,
|
||||
"image": masterImageBase64,
|
||||
"thumb": tileImageBase64,
|
||||
"thumb_width": blockData.Width,
|
||||
"thumb_height": blockData.Height,
|
||||
"thumb_x": blockData.TileX,
|
||||
"thumb_y": blockData.TileY,
|
||||
}, nil
|
||||
|
||||
}
|
56
common/captcha/generate/generate_slide_region_captcha.go
Normal file
56
common/captcha/generate/generate_slide_region_captcha.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha-assets/helper"
|
||||
"github.com/wenlng/go-captcha/v2/slide"
|
||||
)
|
||||
|
||||
// GenerateSlideRegionCaptcha generate slide region captcha
|
||||
func GenerateSlideRegionCaptcha(slide slide.Captcha, redis redis.Client, ctx context.Context) (map[string]interface{}, error) {
|
||||
captData, err := slide.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockData := captData.GetData()
|
||||
if blockData == nil {
|
||||
return nil, errors.New("block data is nil")
|
||||
}
|
||||
|
||||
var masterImageBase64, tileImageBase64 string
|
||||
masterImageBase64, err = captData.GetMasterImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tileImageBase64, err = captData.GetTileImage().ToBase64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockByte, err := json.Marshal(blockData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := helper.StringToMD5(string(blockByte))
|
||||
err = redis.Set(ctx, constant.UserCaptchaPrefix+key, blockByte, time.Minute).Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"code": 0,
|
||||
"key": key,
|
||||
"image": masterImageBase64,
|
||||
"tile": tileImageBase64,
|
||||
"tile_width": blockData.Width,
|
||||
"tile_height": blockData.Height,
|
||||
"tile_x": blockData.TileX,
|
||||
"tile_y": blockData.TileY,
|
||||
}, nil
|
||||
}
|
38
common/captcha/initialize/click_shape_captcha.go
Normal file
38
common/captcha/initialize/click_shape_captcha.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"github.com/wenlng/go-captcha-assets/resources/images"
|
||||
"github.com/wenlng/go-captcha-assets/resources/shapes"
|
||||
"github.com/wenlng/go-captcha/v2/base/option"
|
||||
"github.com/wenlng/go-captcha/v2/click"
|
||||
)
|
||||
|
||||
// NewClickShapeCaptcha 初始化点击形状验证码
|
||||
func NewClickShapeCaptcha() click.Captcha {
|
||||
builder := click.NewBuilder(
|
||||
click.WithRangeLen(option.RangeVal{Min: 3, Max: 6}),
|
||||
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 3}),
|
||||
click.WithRangeThumbBgDistort(1),
|
||||
click.WithIsThumbNonDeformAbility(true),
|
||||
)
|
||||
|
||||
// shape
|
||||
// click.WithUseShapeOriginalColor(false) -> Random rewriting of graphic colors
|
||||
shapeMaps, err := shapes.GetShapes()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// background images
|
||||
imgs, err := images.GetImages()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// set resources
|
||||
builder.SetResources(
|
||||
click.WithShapes(shapeMaps),
|
||||
click.WithBackgrounds(imgs),
|
||||
)
|
||||
return builder.MakeWithShape()
|
||||
}
|
26
common/captcha/initialize/rotate_captcha.go
Normal file
26
common/captcha/initialize/rotate_captcha.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"github.com/wenlng/go-captcha-assets/resources/images"
|
||||
"github.com/wenlng/go-captcha/v2/base/option"
|
||||
"github.com/wenlng/go-captcha/v2/rotate"
|
||||
)
|
||||
|
||||
// NewRotateCaptcha 初始化旋转验证码
|
||||
func NewRotateCaptcha() rotate.Captcha {
|
||||
builder := rotate.NewBuilder(rotate.WithRangeAnglePos([]option.RangeVal{
|
||||
{Min: 20, Max: 330},
|
||||
}))
|
||||
|
||||
// background images
|
||||
imgs, err := images.GetImages()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// set resources
|
||||
builder.SetResources(
|
||||
rotate.WithImages(imgs),
|
||||
)
|
||||
return builder.Make()
|
||||
}
|
44
common/captcha/initialize/slide_captcha.go
Normal file
44
common/captcha/initialize/slide_captcha.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"github.com/wenlng/go-captcha-assets/resources/images"
|
||||
"github.com/wenlng/go-captcha-assets/resources/tiles"
|
||||
"github.com/wenlng/go-captcha/v2/slide"
|
||||
)
|
||||
|
||||
// NewSlideCaptcha 初始化滑动验证码
|
||||
func NewSlideCaptcha() slide.Captcha {
|
||||
builder := slide.NewBuilder(
|
||||
// slide.WithGenGraphNumber(2),
|
||||
slide.WithEnableGraphVerticalRandom(true),
|
||||
)
|
||||
|
||||
// background images
|
||||
imgs, err := images.GetImages()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
graphs, err := tiles.GetTiles()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
|
||||
for i := 0; i < len(graphs); i++ {
|
||||
graph := graphs[i]
|
||||
newGraphs = append(newGraphs, &slide.GraphImage{
|
||||
OverlayImage: graph.OverlayImage,
|
||||
MaskImage: graph.MaskImage,
|
||||
ShadowImage: graph.ShadowImage,
|
||||
})
|
||||
}
|
||||
|
||||
// set resources
|
||||
builder.SetResources(
|
||||
slide.WithGraphImages(newGraphs),
|
||||
slide.WithBackgrounds(imgs),
|
||||
)
|
||||
|
||||
return builder.Make()
|
||||
}
|
43
common/captcha/initialize/slide_region_captcha.go
Normal file
43
common/captcha/initialize/slide_region_captcha.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"github.com/wenlng/go-captcha-assets/resources/images"
|
||||
"github.com/wenlng/go-captcha-assets/resources/tiles"
|
||||
"github.com/wenlng/go-captcha/v2/slide"
|
||||
)
|
||||
|
||||
// NewSlideRegionCaptcha 初始化滑动区域验证码
|
||||
func NewSlideRegionCaptcha() slide.Captcha {
|
||||
builder := slide.NewBuilder(
|
||||
slide.WithGenGraphNumber(2),
|
||||
slide.WithEnableGraphVerticalRandom(true),
|
||||
)
|
||||
|
||||
// background image
|
||||
imgs, err := images.GetImages()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
graphs, err := tiles.GetTiles()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var newGraphs = make([]*slide.GraphImage, 0, len(graphs))
|
||||
for i := 0; i < len(graphs); i++ {
|
||||
graph := graphs[i]
|
||||
newGraphs = append(newGraphs, &slide.GraphImage{
|
||||
OverlayImage: graph.OverlayImage,
|
||||
MaskImage: graph.MaskImage,
|
||||
ShadowImage: graph.ShadowImage,
|
||||
})
|
||||
}
|
||||
|
||||
// set resources
|
||||
builder.SetResources(
|
||||
slide.WithGraphImages(newGraphs),
|
||||
slide.WithBackgrounds(imgs),
|
||||
)
|
||||
|
||||
return builder.MakeWithRegion()
|
||||
}
|
97
common/captcha/initialize/text_captcha.go
Normal file
97
common/captcha/initialize/text_captcha.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
|
||||
"github.com/wenlng/go-captcha-assets/resources/images"
|
||||
"github.com/wenlng/go-captcha-assets/sourcedata/chars"
|
||||
"github.com/wenlng/go-captcha/v2/base/option"
|
||||
"github.com/wenlng/go-captcha/v2/click"
|
||||
)
|
||||
|
||||
// NewTextCaptcha 初始化点选验证码
|
||||
func NewTextCaptcha() click.Captcha {
|
||||
builder := click.NewBuilder(
|
||||
click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
|
||||
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
|
||||
click.WithRangeThumbColors([]string{
|
||||
"#1f55c4",
|
||||
"#780592",
|
||||
"#2f6b00",
|
||||
"#910000",
|
||||
"#864401",
|
||||
"#675901",
|
||||
"#016e5c",
|
||||
}),
|
||||
click.WithRangeColors([]string{
|
||||
"#fde98e",
|
||||
"#60c1ff",
|
||||
"#fcb08e",
|
||||
"#fb88ff",
|
||||
"#b4fed4",
|
||||
"#cbfaa9",
|
||||
"#78d6f8",
|
||||
}),
|
||||
)
|
||||
|
||||
// fonts
|
||||
fonts, err := fzshengsksjw.GetFont()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// background images
|
||||
imgs, err := images.GetImages()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// thumb images
|
||||
// thumbImages, err := thumbs.GetThumbs()
|
||||
// if err != nil {
|
||||
// log.Fatalln(err)
|
||||
// }
|
||||
|
||||
// set resources
|
||||
builder.SetResources(
|
||||
click.WithChars(chars.GetChineseChars()),
|
||||
// click.WithChars([]string{
|
||||
// "1A",
|
||||
// "5E",
|
||||
// "3d",
|
||||
// "0p",
|
||||
// "78",
|
||||
// "DL",
|
||||
// "CB",
|
||||
// "9M",
|
||||
// }),
|
||||
// click.WithChars(chars.GetAlphaChars()),
|
||||
click.WithFonts([]*truetype.Font{fonts}),
|
||||
click.WithBackgrounds(imgs),
|
||||
// click.WithThumbBackgrounds(thumbImages),
|
||||
)
|
||||
// global.TextCaptcha = builder.Make()
|
||||
|
||||
// ============================
|
||||
|
||||
builder.Clear()
|
||||
builder.SetOptions(
|
||||
click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}),
|
||||
click.WithRangeVerifyLen(option.RangeVal{Min: 2, Max: 4}),
|
||||
click.WithRangeThumbColors([]string{
|
||||
"#4a85fb",
|
||||
"#d93ffb",
|
||||
"#56be01",
|
||||
"#ee2b2b",
|
||||
"#cd6904",
|
||||
"#b49b03",
|
||||
"#01ad90",
|
||||
}),
|
||||
)
|
||||
builder.SetResources(
|
||||
click.WithChars(chars.GetChineseChars()),
|
||||
click.WithFonts([]*truetype.Font{fonts}),
|
||||
click.WithBackgrounds(imgs),
|
||||
)
|
||||
return builder.Make()
|
||||
}
|
47
common/captcha/verify/verify_basic_text_captcha.go
Normal file
47
common/captcha/verify/verify_basic_text_captcha.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package verify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha/v2/click"
|
||||
)
|
||||
|
||||
// VerifyBasicTextCaptcha verify basic text captcha
|
||||
func VerifyBasicTextCaptcha(dots string, key string, redis *redis.Client, ctx context.Context) bool {
|
||||
cacheDataByte, err := redis.Get(ctx, constant.UserCaptchaPrefix+key).Bytes()
|
||||
if len(cacheDataByte) == 0 || err != nil {
|
||||
return false
|
||||
}
|
||||
src := strings.Split(dots, ",")
|
||||
|
||||
var dct map[int]*click.Dot
|
||||
if err := json.Unmarshal(cacheDataByte, &dct); err != nil {
|
||||
|
||||
return false
|
||||
}
|
||||
chkRet := false
|
||||
if (len(dct) * 2) == len(src) {
|
||||
for i := 0; i < len(dct); i++ {
|
||||
dot := dct[i]
|
||||
j := i * 2
|
||||
k := i*2 + 1
|
||||
sx, _ := strconv.ParseFloat(fmt.Sprintf("%v", src[j]), 64)
|
||||
sy, _ := strconv.ParseFloat(fmt.Sprintf("%v", src[k]), 64)
|
||||
|
||||
chkRet = click.CheckPoint(int64(sx), int64(sy), int64(dot.X), int64(dot.Y), int64(dot.Width), int64(dot.Height), 0)
|
||||
if !chkRet {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if chkRet {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
34
common/captcha/verify/verify_rotate_captcha.go
Normal file
34
common/captcha/verify/verify_rotate_captcha.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package verify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"strconv"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha/v2/rotate"
|
||||
)
|
||||
|
||||
// VerifyRotateCaptcha verify rotate captcha
|
||||
func VerifyRotateCaptcha(context context.Context, redis *redis.Client, angle int64, key string) bool {
|
||||
cacheDataByte := redis.Get(context, constant.UserCaptchaPrefix+key).Val()
|
||||
if len(cacheDataByte) == 0 {
|
||||
return false
|
||||
}
|
||||
var dct *rotate.Block
|
||||
if err := json.Unmarshal([]byte(cacheDataByte), &dct); err != nil {
|
||||
return false
|
||||
}
|
||||
sAngle, err := strconv.ParseFloat(fmt.Sprintf("%v", angle), 64)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
chkRet := rotate.CheckAngle(int64(sAngle), int64(dct.Angle), 2)
|
||||
if chkRet {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
}
|
35
common/captcha/verify/verify_slide_captcha.go
Normal file
35
common/captcha/verify/verify_slide_captcha.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package verify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"strconv"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wenlng/go-captcha/v2/slide"
|
||||
)
|
||||
|
||||
// VerifySlideCaptcha verify slide captcha
|
||||
func VerifySlideCaptcha(context context.Context, redis *redis.Client, point []int64, key string) bool {
|
||||
cacheDataByte := redis.Get(context, constant.UserCaptchaPrefix+key).Val()
|
||||
if len(cacheDataByte) == 0 {
|
||||
return false
|
||||
}
|
||||
var dct *slide.Block
|
||||
if err := json.Unmarshal([]byte(cacheDataByte), &dct); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
chkRet := false
|
||||
if 2 == len(point) {
|
||||
sx, _ := strconv.ParseFloat(fmt.Sprintf("%v", point[0]), 64)
|
||||
sy, _ := strconv.ParseFloat(fmt.Sprintf("%v", point[1]), 64)
|
||||
chkRet = slide.CheckPoint(int64(sx), int64(sy), int64(dct.X), int64(dct.Y), 4)
|
||||
}
|
||||
if chkRet {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user