Browse Source

验证码

tmp
huangjiajun 2 weeks ago
parent
commit
e267ab21b1
3 changed files with 36 additions and 1 deletions
  1. +4
    -0
      app/hdl/hdl_login.go
  2. +30
    -0
      app/svc/svc_sms.go
  3. +2
    -1
      go.mod

+ 4
- 0
app/hdl/hdl_login.go View File

@@ -81,6 +81,10 @@ func SmsSend(c *gin.Context) {
//}
err = svc.CommSmsSend(c, req.Mobile, req.Type)
if err != nil {
if err.Error() == "请1分钟后再试" {
e.OutErr(c, 400, e.NewErr(400, "请1分钟后再试"))
return
}
e.OutErr(c, 400, e.NewErr(400, "发送失败"))
return
}


+ 30
- 0
app/svc/svc_sms.go View File

@@ -2,13 +2,20 @@ package svc

import (
"applet/app/e"
"applet/app/utils/cache"
"code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun"
"code.fnuoos.com/EggPlanet/egg_system_rules.git/mob"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"math/rand"
"time"
)

func createCaptcha() string {
return fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
}

func CommSmsSend(c *gin.Context, mobile, types string) error {
smsPlatform := GetSysCfgStr("sms_platform")
var err error
@@ -18,6 +25,23 @@ func CommSmsSend(c *gin.Context, mobile, types string) error {
fmt.Println("短信错误:" + err.Error())
return err
}
} else if smsPlatform == "aliyun_own" {
data := AliyunSmsBase(c, types)
captcha := createCaptcha()
// 2、加锁 防止并发提取
mutexKey := fmt.Sprintf("sms_check:%s", mobile)
withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 60, "NX")
if err != nil {
return e.NewErr(400000, "请1分钟后再试")
}
if withdrawAvailable != "OK" {
return e.NewErr(400000, "请1分钟后再试")
}
err = aliyun.AliyunSendSmsOwn(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "{\"code\":\""+captcha+"\"}")
if err != nil {
return e.NewErr(400, "验证码错误,请重试")
}
cache.SetEx("sms:"+mobile, captcha, 300)
} else {
data := AliyunSmsBase(c, types)
//发送短信
@@ -44,6 +68,12 @@ func CommSmsCheck(c *gin.Context, mobile, code string) error {
if bools == false {
return e.NewErr(400, "验证码错误,请重试")
}
} else if smsPlatform == "aliyun_own" {
captcha, _ := cache.GetString("sms:" + mobile)
if captcha != code || code == "" {
return e.NewErr(400, "验证码错误,请重试")
}
cache.Del("sms:" + mobile)
} else {
data := AliyunSmsBase(c, "")
err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, code)


+ 2
- 1
go.mod View File

@@ -34,7 +34,7 @@ require (

require (
code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241216083317-8f7f975c0482
code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241217040418-3640cd7169f0
code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241217084154-72a590107d63
code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be
code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
@@ -62,6 +62,7 @@ require (
github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.10 // indirect
github.com/alibabacloud-go/debug v1.0.1 // indirect
github.com/alibabacloud-go/dypnsapi-20170525/v2 v2.2.3 // indirect
github.com/alibabacloud-go/dysmsapi-20170525/v4 v4.1.0 // indirect
github.com/alibabacloud-go/endpoint-util v1.1.0 // indirect
github.com/alibabacloud-go/openapi-util v0.1.0 // indirect
github.com/alibabacloud-go/tea v1.2.2 // indirect


Loading…
Cancel
Save