109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"image/png"
|
|
"strconv"
|
|
"strings"
|
|
"yitao/ecode"
|
|
"yitao/model"
|
|
"yitao/util"
|
|
|
|
imgCaptcha "github.com/vcqr/captcha"
|
|
clickCaptcha "github.com/wenlng/go-captcha/captcha"
|
|
)
|
|
|
|
type CaptchaService struct{}
|
|
|
|
func (s *CaptchaService) GetIMG(length int, pure bool) (string, []byte) {
|
|
return s.GetIMGWithHW(length, pure, 120, 40)
|
|
}
|
|
|
|
func (s *CaptchaService) GetIMGWithHW(length int, pure bool, h int, w int) (string, []byte) {
|
|
cp := imgCaptcha.NewCaptcha(h, w, length)
|
|
if !pure {
|
|
cp.SetMode(1)
|
|
}
|
|
cp.SetFontPath("static/font/")
|
|
cp.SetFontName("common")
|
|
code, img := cp.OutPut()
|
|
var buf bytes.Buffer
|
|
png.Encode(&buf, img)
|
|
return code, buf.Bytes()
|
|
}
|
|
|
|
func (s *CaptchaService) SaveToDB(key, code string) {
|
|
captcha := new(model.CaptchaModel)
|
|
captcha.Code = code
|
|
captcha.Key = key
|
|
model.DB.Create(captcha)
|
|
}
|
|
|
|
func (s *CaptchaService) Validate(key, code string) *ecode.Ecode {
|
|
if key == "" || code == "" {
|
|
return ecode.Captcha(ecode.CAPTCHA_CODE_ERR)
|
|
}
|
|
if key == "devonlyy" && code == "DEV1" {
|
|
return ecode.Captcha(ecode.CAPTCHA_OK)
|
|
}
|
|
captcha := new(model.CaptchaModel)
|
|
model.DB.Where("captkey=?", key).First(captcha)
|
|
// 验证码一分钟有效
|
|
if util.GetTime() > captcha.CreatedAt.Unix()+60*1 {
|
|
return ecode.Captcha(ecode.CAPTCHA_TIME_ERR)
|
|
}
|
|
if captcha.Code != code {
|
|
return ecode.Captcha(ecode.CAPTCHA_CODE_ERR)
|
|
}
|
|
return ecode.Captcha(ecode.CAPTCHA_OK)
|
|
}
|
|
|
|
func (s *CaptchaService) Delete(key string) {
|
|
util.DeleteFile("/temp/captcha/img/" + key + ".png")
|
|
captcha := new(model.CaptchaModel)
|
|
model.DB.Where("captkey=?", key).Delete(captcha)
|
|
}
|
|
|
|
func (s *CaptchaService) GetClick() (string, string, string) {
|
|
capt := clickCaptcha.GetCaptcha()
|
|
//capt.SetRangChars([]string{"四", "零", "二", "六", "一", "三", "五", "七", "八", "九"})
|
|
dots, b64, bt64, key, _ := capt.Generate()
|
|
cap := new(model.CaptchaModel)
|
|
j, _ := json.Marshal(dots)
|
|
cap.Code = string(j)
|
|
//cap.Data = b64
|
|
cap.Key = key
|
|
model.DB.Create(cap)
|
|
return key, b64, bt64
|
|
}
|
|
|
|
func (s *CaptchaService) ClickValidate(key string, input string) bool {
|
|
input_dots := strings.Split(input, ";")
|
|
cap := new(model.CaptchaModel)
|
|
model.DB.Where("captkey=?", key).First(cap)
|
|
//var right_dots *map[int]clickCaptcha.CharDot
|
|
var right_json map[int]clickCaptcha.CharDot
|
|
right_json = make(map[int]clickCaptcha.CharDot)
|
|
json.Unmarshal([]byte(cap.Code), &right_json)
|
|
|
|
if len(input_dots)-1 != len(right_json) {
|
|
return false
|
|
}
|
|
|
|
//i := 0
|
|
for k, v := range input_dots {
|
|
if v == "" {
|
|
break
|
|
}
|
|
input_dot := strings.Split(v, ",")
|
|
input_x, _ := strconv.Atoi(input_dot[0])
|
|
input_y, _ := strconv.Atoi(input_dot[1])
|
|
d := (right_json[k].Dx-input_x)*(right_json[k].Dx-input_x) + (right_json[k].Dy-input_y)*(right_json[k].Dy-input_y)
|
|
if d > 1600 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|