Explorar el Código

微信短信登陆

jyq hace 4 años
padre
commit
37b71dc37c
Se han modificado 3 ficheros con 142 adiciones y 1 borrados
  1. 51 0
      assembly/base/repository/redis/captchaStore.go
  2. 89 0
      assembly/base/service/verifyCode.go
  3. 2 1
      go.mod

+ 51 - 0
assembly/base/repository/redis/captchaStore.go

@@ -0,0 +1,51 @@
+// Package redis
+/**
+ * @ File:
+ * @ Date: 2021/5/12 10:29
+ * @ Author: JYQ
+ * @ Description:
+ */
+package redis
+
+import (
+	"sportfitness/base/errors"
+	"time"
+
+	"github.com/go-redis/redis/v8"
+)
+
+type CaptchaStore struct {
+}
+
+func (c CaptchaStore) genKey(id string) string {
+	return prefix + ":captcha:" + id
+}
+
+func (c CaptchaStore) Set(id string, value string) {
+
+	handleErr(getClient().Set(ctx, c.genKey(id), value, time.Minute*2).Err())
+}
+
+func (c CaptchaStore) Get(id string, clear bool) string {
+	cmds, _ := getClient().TxPipelined(ctx, func(pipe redis.Pipeliner) error {
+		key := c.genKey(id)
+		pipe.Get(ctx, key)
+		if clear {
+			pipe.Del(ctx, key)
+		}
+		return nil
+	})
+
+	cmd := cmds[0].(*redis.StringCmd)
+	r, err := cmd.Result()
+	if err == redis.Nil {
+		panic(errors.ErrVerifyCode)
+	}
+
+	return r
+}
+
+func (c CaptchaStore) Verify(id, answer string, clear bool) bool {
+	v := c.Get(id, clear)
+	return v == answer
+}

+ 89 - 0
assembly/base/service/verifyCode.go

@@ -0,0 +1,89 @@
+// Package service
+/**
+ * @ File: verifyCode
+ * @ Date: 2021/5/12 10:15
+ * @ Author: JYQ
+ * @ Description: 图片验证码相关
+ */
+package service
+
+import (
+	"sportfitness/base/assembly/base/repository/redis"
+	"sportfitness/base/errors"
+
+	"github.com/mojocn/base64Captcha"
+)
+
+type VerifyCode struct {
+	base
+}
+
+func (VerifyCode) GenImage(height, width int) (id, pic string) {
+	if height == 0 {
+		height = 80
+	}
+
+	if width == 0 {
+		width = 240
+	}
+
+	if height < 80 || width < 80 {
+		panic(errors.ParamError("height or width", "小于80"))
+	}
+
+	captcha := base64Captcha.NewCaptcha(
+		base64Captcha.NewDriverDigit(
+			height,
+			width,
+			4,
+			0.7,
+			20,
+		),
+		redis.CaptchaStore{})
+
+	id, pic, err := captcha.Generate()
+	if err != nil {
+		panic(err)
+	}
+
+	return
+}
+
+//
+//func (v VerifyCode) GetPhoneVFCode(sysToken, account, ip, code, templateCode string, verifyType im.VerifyType, expireDuration time.Duration) {
+//	sysId := model.SysInfoThisId
+//	if sysToken != "" {
+//		sysId = v.getSysIdBySysToken(sysToken)
+//	}
+//	now := time.Now()
+//	vf := &model.VerifyCode{
+//		SysId:    sysId,
+//		Account:  account,
+//		SendType: model.VFCodeTypePhone,
+//		CodeType: verifyType,
+//		Code:     code,
+//		Ip:       ip,
+//		CreateAt: now,
+//		ExpireAt: now.Add(expireDuration),
+//	}
+//
+//	postgre.VerifyCode{}.Create(vf)
+//	http.PhoneCode{}.SendCode(vf, templateCode)
+//}
+//
+//func (v VerifyCode) CheckPhoneVFCode(sysToken, account, code string, verifyType im.VerifyType) {
+//	sysId := model.SysInfoThisId
+//	if sysToken != "" {
+//		sysId = v.getSysIdBySysToken(sysToken)
+//	}
+//
+//	vf := &model.VerifyCode{
+//		SysId:    sysId,
+//		Account:  account,
+//		SendType: model.VFCodeTypePhone,
+//		CodeType: verifyType,
+//		Code:     code,
+//	}
+//
+//	postgre.VerifyCode{}.Check(vf)
+//}

+ 2 - 1
go.mod

@@ -8,9 +8,10 @@ require (
 	github.com/go-redis/redis/v8 v8.8.0
 	github.com/go-sql-driver/mysql v1.6.0 // indirect
 	github.com/golang/protobuf v1.5.2
-	github.com/qiniu/api.v7/v7 v7.6.0
 	github.com/hashicorp/go-hclog v0.16.0 // indirect
 	github.com/klauspost/compress v1.11.13 // indirect
+	github.com/mojocn/base64Captcha v1.3.4
+	github.com/qiniu/api.v7/v7 v7.6.0
 	github.com/sirupsen/logrus v1.8.1
 	golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
 	golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect