feat 网关鉴权
This commit is contained in:
@@ -2,39 +2,89 @@ package repository
|
||||
|
||||
import (
|
||||
"common/db/mysql"
|
||||
"common/db/redis"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"user/internal/dao/model"
|
||||
"user/internal/dao/query"
|
||||
)
|
||||
|
||||
var (
|
||||
cacheBySn = "c:user:s:%v"
|
||||
cacheByPhone = "c:user:p:%v"
|
||||
)
|
||||
|
||||
type UserDao struct {
|
||||
ctx context.Context
|
||||
query *query.Query
|
||||
cache *redis.CacheClient
|
||||
}
|
||||
|
||||
func NewUserDao(ctx context.Context) *UserDao {
|
||||
return &UserDao{
|
||||
func NewUserDao(ctx context.Context, cache ...*redis.CacheClient) *UserDao {
|
||||
dao := &UserDao{
|
||||
ctx: ctx,
|
||||
query: query.Use(mysql.GetDB("user_db")),
|
||||
}
|
||||
if len(cache) > 0 {
|
||||
dao.cache = cache[0]
|
||||
}
|
||||
return dao
|
||||
}
|
||||
|
||||
func (d *UserDao) Create(user *model.User) error {
|
||||
err := d.query.User.WithContext(d.ctx).
|
||||
Create(user)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *UserDao) Updates(user *model.User) error {
|
||||
info, _ := d.query.User.WithContext(d.ctx).
|
||||
Where(d.query.User.ID.Eq(user.ID)).
|
||||
Updates(user)
|
||||
if info.RowsAffected > 0 {
|
||||
if d.cache != nil {
|
||||
d.cache.Del(d.ctx, fmt.Sprintf(cacheBySn, user.Sn))
|
||||
d.cache.Del(d.ctx, fmt.Sprintf(cacheByPhone, user.Phone))
|
||||
}
|
||||
}
|
||||
return info.Error
|
||||
}
|
||||
|
||||
func (d *UserDao) FindBySn(sn int64) (*model.User, error) {
|
||||
if d.cache != nil {
|
||||
var user model.User
|
||||
if ok := d.cache.Get(d.ctx, fmt.Sprintf(cacheBySn, sn), &user); ok {
|
||||
return &user, nil
|
||||
}
|
||||
}
|
||||
first, err := d.query.User.WithContext(d.ctx).
|
||||
Where(d.query.User.Sn.Eq(sn)).
|
||||
First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.cache != nil {
|
||||
d.cache.Set(d.ctx, fmt.Sprintf(cacheBySn, sn), first, 5*time.Minute)
|
||||
}
|
||||
return first, nil
|
||||
}
|
||||
|
||||
func (d *UserDao) FindByPhone(phone string) (*model.User, error) {
|
||||
if d.cache != nil {
|
||||
var user model.User
|
||||
if ok := d.cache.Get(d.ctx, fmt.Sprintf(cacheByPhone, phone), &user); ok {
|
||||
return &user, nil
|
||||
}
|
||||
}
|
||||
first, err := d.query.User.WithContext(d.ctx).
|
||||
Where(d.query.User.Phone.Eq(phone)).
|
||||
First()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.cache != nil {
|
||||
d.cache.Set(d.ctx, fmt.Sprintf(cacheByPhone, phone), first, 5*time.Minute)
|
||||
}
|
||||
return first, nil
|
||||
}
|
||||
|
||||
@@ -1,21 +1,54 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"common/db/redis"
|
||||
"common/net/http/http_resp"
|
||||
"common/proto/ss/grpc_pb"
|
||||
"common/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"user/internal/dao/model"
|
||||
"user/internal/dao/repository"
|
||||
)
|
||||
|
||||
func (s *Server) Login(ctx context.Context, req *grpc_pb.LoginReq) (*grpc_pb.LoginResp, error) {
|
||||
log.Infof("Login req: %+v", req)
|
||||
userDao := repository.NewUserDao(ctx, redis.GetCacheClient())
|
||||
user, err := userDao.FindByPhone(req.Phone)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
user = &model.User{
|
||||
Phone: req.Phone,
|
||||
}
|
||||
if err := userDao.Create(user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Name = fmt.Sprintf("user_%d", user.Sn)
|
||||
_ = userDao.Updates(user)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
user, err := repository.NewUserDao(ctx).FindBySn(857527344353)
|
||||
return &grpc_pb.LoginResp{
|
||||
USN: user.Sn,
|
||||
Name: user.Name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) GetUserInfo(ctx context.Context, req *grpc_pb.GetUserInfoReq) (*grpc_pb.GetUserInfoResp, error) {
|
||||
if !utils.ShouldBindUsn(ctx, &req.USN) {
|
||||
return nil, http_resp.ParamError
|
||||
}
|
||||
userDao := repository.NewUserDao(ctx, redis.GetCacheClient())
|
||||
user, err := userDao.FindBySn(req.USN)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &grpc_pb.LoginResp{
|
||||
UID: int32(user.Sn),
|
||||
return &grpc_pb.GetUserInfoResp{
|
||||
USN: user.Sn,
|
||||
Name: user.Name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user