package service import ( "context" "encoding/json" "fmt" "time" "youngee_b_api/consts" "youngee_b_api/model/redis_model" "youngee_b_api/model/system_model" "youngee_b_api/redis" ) var LoginAuth *loginAuth func LoginAuthInit(config *system_model.Session) { auth := new(loginAuth) auth.sessionTTL = time.Duration(config.TTL) * time.Minute LoginAuth = auth } type loginAuth struct { sessionTTL time.Duration } func (l *loginAuth) AuthToken(ctx context.Context, token string) (*redis_model.Auth, error) { value, err := redis.Get(ctx, l.getRedisKey(token)) if err != nil { if err == consts.RedisNil { return nil, fmt.Errorf("not found in redis,token:%+v", token) } return nil, err } auth := new(redis_model.Auth) if err = json.Unmarshal([]byte(value), auth); err != nil { return nil, err } return auth, nil } func (l *loginAuth) AuthPassword(ctx context.Context, phone, password string) *redis_model.Auth { //需要添加mysql库认证逻辑 // todo auth := &redis_model.Auth{ Phone: phone, } if authJson, err := json.Marshal(auth); err == nil { err = redis.Set(ctx, l.getRedisKey(phone), string(authJson), l.sessionTTL) if err == nil { return auth } } return nil } func (l *loginAuth) getRedisKey(key string) string { return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key) }