package utils import ( "Ic_ouath/app/e" "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(global.SubMailSetting.Appid, global.SubMailSetting.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,请在5分钟内输入", 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, 300*time.Second).Err() return e.SUCCESS } }