create_code.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "Ic_ouath/app/e"
  4. "Ic_ouath/configs"
  5. "Ic_ouath/global"
  6. "context"
  7. "fmt"
  8. "go.uber.org/zap"
  9. "math/rand"
  10. "time"
  11. )
  12. func CreatCode() string {
  13. const chars = "0123456789"
  14. result := make([]byte, 6)
  15. rand.Seed(time.Now().UnixNano())
  16. for i := range result {
  17. index := rand.Intn(len(chars))
  18. result[i] = chars[index]
  19. }
  20. return string(result)
  21. }
  22. func SendModel(phone string) e.Rescode {
  23. ctx := context.Background()
  24. ss := NewSMS(configs.Config.GetString("subMail.appid"), configs.Config.GetString("subMail.signature"))
  25. result, err := global.Rdb.Exists(ctx, phone).Result()
  26. if result == 1 {
  27. fmt.Println("验证码已经发送", zap.Error(err))
  28. return e.HasSend
  29. }
  30. if err != nil {
  31. fmt.Println("redis查询出现异常", zap.Error(err))
  32. return e.TheSystemIsAbnormal
  33. }
  34. code := CreatCode()
  35. content := fmt.Sprintf("【冷链智控系统】您的短信验证码:%s,请在1分钟内输入", code)
  36. res, err := ss.Send(phone, content)
  37. if err != nil || res.Status != SUCCESS {
  38. fmt.Println("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
  39. return e.AnExceptionOccursWhenSendingAnSMSVerificationCode
  40. } else {
  41. //验证码装入redis并且设置过期时间
  42. err = global.Rdb.Set(ctx, phone, code, 60*time.Second).Err()
  43. return e.SUCCESS
  44. }
  45. }