Initial commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package smsser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"stat/common/log"
|
||||
"stat/common/redis"
|
||||
"stat/common/sms"
|
||||
"stat/common/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
CaptchaLen = 6
|
||||
MobileCaptchaExpire = 5 * time.Minute
|
||||
)
|
||||
|
||||
// SendSmsCode 发送验证码
|
||||
func SendSmsCode(mobile string) error {
|
||||
code := utils.RandDigits(CaptchaLen)
|
||||
//fmt.Println(code)
|
||||
redisKey := fmt.Sprintf("captcha:mobile:%s", mobile)
|
||||
err := redis.StatRds.Set(redisKey, code, MobileCaptchaExpire)
|
||||
if err != nil {
|
||||
log.Error("SendCaptcha Save Redis error", log.Any("mobile", mobile), log.E(err))
|
||||
return err
|
||||
}
|
||||
|
||||
err = sms.Send(sms.FengNiao, mobile, code)
|
||||
if err != nil {
|
||||
log.Error("SendCaptcha send err", log.Any("mobile", mobile), log.Any("Platform", sms.FengNiao), log.E(err))
|
||||
yunErr := sms.Send(sms.YunPian, mobile, code)
|
||||
if yunErr != nil {
|
||||
log.Error("SendCaptcha send err", log.Any("mobile", mobile), log.Any("Platform", sms.YunPian), log.E(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// VerifySmsCode 验证验证码
|
||||
func VerifySmsCode(mobile string, code string) error {
|
||||
redisKey := fmt.Sprintf("captcha:mobile:%s", mobile)
|
||||
redisCode, err := redis.StatRds.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Error("VerifySmsCode redis get err", log.E(err))
|
||||
return err
|
||||
}
|
||||
if redisCode != nil && *redisCode == code {
|
||||
return nil
|
||||
} else {
|
||||
log.Info("VerifySmsCode miss", log.Any("mobile", mobile), log.Any("code", code), log.Any("redisCode", redisCode))
|
||||
return fmt.Errorf("VerifySmsCode miss")
|
||||
}
|
||||
}
|
||||
|
||||
func GetSmsCode(mobile string) (string, error) {
|
||||
redisKey := fmt.Sprintf("captcha:mobile:%s", mobile)
|
||||
redisCode, err := redis.StatRds.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Error("VerifySmsCode redis get err", log.E(err))
|
||||
return "", err
|
||||
}
|
||||
if redisCode != nil {
|
||||
return *redisCode, nil
|
||||
} else {
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user