package service import ( "context" "encoding/json" "errors" "fmt" "strconv" "strings" "time" "youngee_b_api/consts" "youngee_b_api/db" "youngee_b_api/model/redis_model" "youngee_b_api/model/system_model" "youngee_b_api/redis" "youngee_b_api/util" "github.com/sirupsen/logrus" ) 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) { phone, err := l.parseToken(ctx, token) if err != nil { logrus.Debug("token格式错误:%+v", token) return nil, err } auth, err := l.getSessionAuth(ctx, phone) if err != nil { logrus.Debug("获取session redis错误: token:%+v,err:%+v", token, err) return nil, err } if auth.Token != token { logrus.Debug("获取session time过期错误: token:%+v", token) return nil, errors.New("auth failed") } return auth, nil } func (l *loginAuth) AuthCode(ctx context.Context, phone string, code string) (string, error) { user, err := db.GetUserByPhone(ctx, phone) fmt.Println("login_auth", user, err) if err != nil { return "", err } else if user == nil { // 账号不存在,则注册账号 _, err = Enterprise.CreateEnterprise(ctx, phone) if err != nil { return "账号创建失败", err } user, err = db.GetUserByPhone(ctx, phone) fmt.Println("login_auth", user, err) if err != nil { return "", err } } else if string(user.Role) != consts.BRole { // 账号权限有误 logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone) return "权限错误,请登录企业账号", errors.New("auth fail") } vcode, err := l.getSessionCode(ctx, phone) if err != nil { return "", err } if *vcode != code { // 验证码错误 logrus.Debugf("[AuthCode] auth fail,phone:%+v", phone) return "验证码有误", errors.New("auth fail") } token := l.getToken(ctx, phone) enterprise, err := db.GetEnterpriseByUID(ctx, user.ID) if err != nil { return "", err } auth := &redis_model.Auth{ Phone: phone, ID: user.ID, User: user.User, Username: user.Username, RealName: user.RealName, Role: user.Role, Email: user.Email, Token: token, EnterpriseID: enterprise.EnterpriseID, } if err := l.setSession(ctx, phone, auth); err != nil { fmt.Printf("setSession error\n") return "", err } return token, nil } // func (l *loginAuth) AuthPassword(ctx context.Context, phone string, password string) (string, error) { // // 验证是否存在 // user, err := db.GetUserByPhone(ctx, phone) // if err != nil { // return "", err // } // // 验证正确性 // if user == nil || user.Role != consts.BRole || user.Password != l.encryptPassword(password) { // // 登录失败 // logrus.Debugf("[AuthPassword] auth fail,phone:%+v", phone) // return "", errors.New("auth fail") // } // token := l.getToken(ctx, phone) // auth := &redis_model.Auth{ // Phone: phone, // ID: user.ID, // User: user.User, // Username: user.Username, // RealName: user.RealName, // Role: user.Role, // Email: user.Email, // Token: token, // } // if err := l.setSession(ctx, phone, auth); err != nil { // return "", err // } // return token, nil // } func (l *loginAuth) setSession(ctx context.Context, phone string, auth *redis_model.Auth) error { if authJson, err := json.Marshal(auth); err == nil { err = redis.Set(ctx, l.getRedisKey(phone), string(authJson), l.sessionTTL) if err == nil { return err } } return nil } func (l *loginAuth) getSessionCode(ctx context.Context, phone string) (*string, error) { value, err := redis.Get(ctx, l.getRedisKey(phone)) if err != nil { if err == consts.RedisNil { return nil, fmt.Errorf("not found in redis,phone:%+v", phone) } return nil, err } return &value, nil } func (l *loginAuth) getSessionAuth(ctx context.Context, phone string) (*redis_model.Auth, error) { value, err := redis.Get(ctx, l.getRedisKey(phone)) if err != nil { if err == consts.RedisNil { return nil, fmt.Errorf("not found in redis,phone:%+v", phone) } 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) getToken(ctx context.Context, phone string) string { timeSeed := strconv.FormatInt(time.Now().Unix(), 10) token := phone + "." + timeSeed + "." + util.MD5(phone, timeSeed, consts.AuthSalt) return token } func (l *loginAuth) parseToken(ctx context.Context, token string) (string, error) { parts := strings.Split(token, ".") if len(parts) == 3 { phone := parts[0] timeSeed := parts[1] if parts[2] == util.MD5(phone, timeSeed, consts.AuthSalt) { return phone, nil } } return "", errors.New("token invalid") } func (l *loginAuth) encryptPassword(password string) string { return util.MD5(password) } func (l *loginAuth) getRedisKey(key string) string { return fmt.Sprintf("%s%s", consts.SessionRedisPrefix, key) }