123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package utils
- import (
- "Ic_ouath/app/e"
- "Ic_ouath/configs"
- "Ic_ouath/global"
- "context"
- "fmt"
- "go.uber.org/zap"
- "math/rand"
- "time"
- )
- func CreatCode() string {
- const chars = "0123456789"
- result := make([]byte, 6)
- rand.Seed(time.Now().UnixNano())
- for i := range result {
- index := rand.Intn(len(chars))
- result[i] = chars[index]
- }
- return string(result)
- }
- func SendModel(phone string) e.Rescode {
- ctx := context.Background()
- ss := NewSMS(configs.Config.GetString("subMail.appid"), configs.Config.GetString("subMail.signature"))
- result, err := global.Rdb.Exists(ctx, phone).Result()
- if result == 1 {
- fmt.Println("验证码已经发送", zap.Error(err))
- return e.HasSend
- }
- if err != nil {
- fmt.Println("redis查询出现异常", zap.Error(err))
- return e.TheSystemIsAbnormal
- }
- code := CreatCode()
- content := fmt.Sprintf("【冷链智控系统】您的短信验证码:%s,请在1分钟内输入", code)
- res, err := ss.Send(phone, content)
- if err != nil || res.Status != SUCCESS {
- fmt.Println("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
- return e.AnExceptionOccursWhenSendingAnSMSVerificationCode
- } else {
- //验证码装入redis并且设置过期时间
- err = global.Rdb.Set(ctx, phone, code, 60*time.Second).Err()
- return e.SUCCESS
- }
- }
|