package service import ( "context" "fmt" "math/rand" "time" "youngee_b_api/consts" "youngee_b_api/model/system_model" "youngee_b_api/redis" ) var SendCode *sendCode func SendCodeInit(config *system_model.Session) { sendCode := new(sendCode) sendCode.sessionTTL = time.Duration(config.TTL) * time.Minute SendCode = sendCode } type sendCode struct { sessionTTL time.Duration } func (s *sendCode) GetCode(ctx context.Context) string { rnd := rand.New(rand.NewSource(time.Now().UnixNano())) vcode := fmt.Sprintf("%06v", rnd.Int31n(1000000)) return vcode } func (s *sendCode) SetSession(ctx context.Context, phone string, vcode string) error { err := redis.Set(ctx, s.getRedisKey(phone), vcode, s.sessionTTL) if err != nil { return err } return nil } func (s *sendCode) getRedisKey(key string) string { return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key) }