|
@@ -48,9 +48,51 @@ func (l *loginAuth) AuthToken(ctx context.Context, token string) (*redis_model.A
|
|
|
return auth, nil
|
|
|
|
|
|
}
|
|
|
-func (l *loginAuth) AuthPassword(ctx context.Context, phone, password string) (string, error) {
|
|
|
+
|
|
|
+func (l *loginAuth) AuthMSG(ctx context.Context, phone string, vcode string) (string, error) {
|
|
|
+ // 验证是否存在
|
|
|
+ user, err := db.GetUserByPhone(ctx, phone)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ code, err := l.getSessionCode(ctx, phone)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ if user == nil {
|
|
|
+ fmt.Printf("user == nil\n")
|
|
|
+ }
|
|
|
+ if string(user.Role) != consts.BRole {
|
|
|
+ fmt.Printf("%+v\n", string(user.Role))
|
|
|
+ }
|
|
|
+ if *code != vcode {
|
|
|
+ fmt.Printf("code:%+v, vcode:%+v\n", *code, vcode)
|
|
|
+ }
|
|
|
+ if user == nil || string(user.Role) != consts.BRole || *code != vcode { // 登录失败
|
|
|
+ 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 {
|
|
|
+ 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, password)
|
|
|
+ user, err := db.GetUserByPhone(ctx, phone)
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
@@ -76,6 +118,7 @@ func (l *loginAuth) AuthPassword(ctx context.Context, phone, password string) (s
|
|
|
}
|
|
|
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)
|
|
@@ -85,6 +128,18 @@ func (l *loginAuth) setSession(ctx context.Context, phone string, auth *redis_mo
|
|
|
}
|
|
|
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) getSession(ctx context.Context, phone string) (*redis_model.Auth, error) {
|
|
|
value, err := redis.Get(ctx, l.getRedisKey(phone))
|
|
|
if err != nil {
|