|
@@ -1,16 +1,8 @@
|
|
package captcha
|
|
package captcha
|
|
|
|
|
|
import (
|
|
import (
|
|
- "app.yhyue.com/moapp/jybase/captcha/helper"
|
|
|
|
"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
|
|
"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
|
|
- "app.yhyue.com/moapp/jybase/redis"
|
|
|
|
- "encoding/json"
|
|
|
|
"fmt"
|
|
"fmt"
|
|
- "github.com/wenlng/go-captcha-assets/resources/images"
|
|
|
|
- "github.com/wenlng/go-captcha-assets/resources/tiles"
|
|
|
|
- "github.com/wenlng/go-captcha/v2/slide"
|
|
|
|
- "image"
|
|
|
|
- "log"
|
|
|
|
"net/http"
|
|
"net/http"
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
@@ -27,150 +19,35 @@ type (
|
|
CaptchaKey interface{} `json:"captcha_key"`
|
|
CaptchaKey interface{} `json:"captcha_key"`
|
|
ImageBase64 interface{} `json:"image_base64"`
|
|
ImageBase64 interface{} `json:"image_base64"`
|
|
TileBase64 interface{} `json:"tile_base64"`
|
|
TileBase64 interface{} `json:"tile_base64"`
|
|
|
|
+ ThumbBase64 interface{} `json:"thumb_base64"`
|
|
TileWidth interface{} `json:"tile_width"`
|
|
TileWidth interface{} `json:"tile_width"`
|
|
TileHeight interface{} `json:"tile_height"`
|
|
TileHeight interface{} `json:"tile_height"`
|
|
TileX interface{} `json:"tile_x"`
|
|
TileX interface{} `json:"tile_x"`
|
|
TileY interface{} `json:"tile_y"`
|
|
TileY interface{} `json:"tile_y"`
|
|
|
|
+ ThumbSize int `json:"thumb_size"`
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
-var (
|
|
|
|
- captFuncS []slide.Captcha
|
|
|
|
- rd = RandomNumberGenerator()
|
|
|
|
- redisTtl = 10 * 60
|
|
|
|
- redisKey = "captcha_slide_%s_%s"
|
|
|
|
- padding int64 = 4
|
|
|
|
- RedisCode = "other"
|
|
|
|
- SendCodeKey = "send_phone_check_%s_%s"
|
|
|
|
- MobileCacheKey = "mobile_captcha_%s"
|
|
|
|
- SendCodeTtl = 20 * 60
|
|
|
|
- SendCodeCacheValue = "send_phone_check_%s_%sValue"
|
|
|
|
- SendCodeCacheKey = "send_phone_check_%s_%sKey"
|
|
|
|
- SendCodeCacheTime = "send_phone_check_%s_%sTime"
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-func InitCaptcha(n int) {
|
|
|
|
- for i := 1; i <= n; i++ {
|
|
|
|
- if captcha := slideCaptcha(i); captcha != nil {
|
|
|
|
- captFuncS = append(captFuncS, captcha)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if len(captFuncS) == 0 {
|
|
|
|
- fmt.Println("InitCaptcha False")
|
|
|
|
- }
|
|
|
|
- //cache.RunTimedTask()
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 滑动 Basic=1 || 拖拽 Region=2
|
|
|
|
-func slideCaptcha(n int) slide.Captcha {
|
|
|
|
- var builder slide.Builder
|
|
|
|
- switch n {
|
|
|
|
- case 2:
|
|
|
|
- builder = slide.NewBuilder(
|
|
|
|
- slide.WithGenGraphNumber(2),
|
|
|
|
- slide.WithEnableGraphVerticalRandom(true),
|
|
|
|
- )
|
|
|
|
- default:
|
|
|
|
- builder = slide.NewBuilder(
|
|
|
|
- slide.WithGenGraphNumber(2),
|
|
|
|
- )
|
|
|
|
- }
|
|
|
|
- // background images
|
|
|
|
- var (
|
|
|
|
- imgS []image.Image
|
|
|
|
- err error
|
|
|
|
- )
|
|
|
|
- imgS, err = images.GetImages()
|
|
|
|
- if err != nil {
|
|
|
|
- log.Fatalln(err)
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
- var graphs []*tiles.GraphImage
|
|
|
|
- graphs, err = tiles.GetTiles()
|
|
|
|
- if err != nil {
|
|
|
|
- log.Fatalln(err)
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
- 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),
|
|
|
|
- )
|
|
|
|
- switch n {
|
|
|
|
- case 2:
|
|
|
|
- return builder.MakeWithRegion()
|
|
|
|
- default:
|
|
|
|
- return builder.Make()
|
|
|
|
- }
|
|
|
|
|
|
+func InitCaptcha() {
|
|
|
|
+ SlideCaptcha()
|
|
|
|
+ ClickCaptcha()
|
|
|
|
+ RotateCaptcha()
|
|
}
|
|
}
|
|
|
|
|
|
-// GetCaptchaData .
|
|
|
|
func (c *GetCaptcha) GetCaptchaData() (cd CaptRes, err error) {
|
|
func (c *GetCaptcha) GetCaptchaData() (cd CaptRes, err error) {
|
|
if strings.TrimSpace(c.Phone) == "" || !PhoneCheck(c.Phone) {
|
|
if strings.TrimSpace(c.Phone) == "" || !PhoneCheck(c.Phone) {
|
|
err = fmt.Errorf("phone param is empty or incorrect")
|
|
err = fmt.Errorf("phone param is empty or incorrect")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- if len(captFuncS) == 0 {
|
|
|
|
- err = fmt.Errorf("get generate failed")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- var (
|
|
|
|
- randomIndex int
|
|
|
|
- captData slide.CaptchaData
|
|
|
|
- )
|
|
|
|
- if cf := len(captFuncS); cf > 1 {
|
|
|
|
- randomIndex = rd.Intn(cf)
|
|
|
|
- }
|
|
|
|
- captData, err = captFuncS[randomIndex].Generate()
|
|
|
|
- if err != nil {
|
|
|
|
- log.Fatalln(err)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- blockData := captData.GetData()
|
|
|
|
- if blockData == nil {
|
|
|
|
- err = fmt.Errorf("gen captcha data failed")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- var masterImageBase64, tileImageBase64 string
|
|
|
|
- masterImageBase64, err = captData.GetMasterImage().ToBase64()
|
|
|
|
- if err != nil {
|
|
|
|
- err = fmt.Errorf("masterImage base64 data failed")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- tileImageBase64, err = captData.GetTileImage().ToBase64()
|
|
|
|
- if err != nil {
|
|
|
|
- err = fmt.Errorf("tileImage base64 data failed")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- dotsByte, _ := json.Marshal(blockData)
|
|
|
|
- //key := helper.GenUniqueId()
|
|
|
|
- captchaKey := helper.StringToMD5(string(dotsByte))
|
|
|
|
- cacheKey := fmt.Sprintf(redisKey, captchaKey, c.Phone)
|
|
|
|
- //cache.WriteCache(fmt.Sprintf("%s#%s", captchaKey, c.Phone), dotsByte)
|
|
|
|
- err = redis.PutBytes(RedisCode, cacheKey, &dotsByte, redisTtl)
|
|
|
|
- if err != nil {
|
|
|
|
- err = fmt.Errorf("cache save value failed")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- randomIndex++
|
|
|
|
- cd = CaptRes{
|
|
|
|
- Code: randomIndex,
|
|
|
|
- CaptchaKey: captchaKey,
|
|
|
|
- ImageBase64: masterImageBase64,
|
|
|
|
- TileBase64: tileImageBase64,
|
|
|
|
- TileWidth: blockData.Width,
|
|
|
|
- TileHeight: blockData.Height,
|
|
|
|
- TileX: blockData.TileX,
|
|
|
|
- TileY: blockData.TileY,
|
|
|
|
|
|
+ rn := 1 // rd.Intn(num) + 1
|
|
|
|
+ switch rn {
|
|
|
|
+ case 1, 2:
|
|
|
|
+ return c.GetSlideCaptchaData(rn)
|
|
|
|
+ case 3, 4:
|
|
|
|
+ return c.GetClickCaptchaData(rn)
|
|
|
|
+ case 5:
|
|
|
|
+ return c.GetRotateCaptchaData()
|
|
}
|
|
}
|
|
|
|
+ err = fmt.Errorf("get captcha data false")
|
|
return
|
|
return
|
|
}
|
|
}
|