55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
|
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
|
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
|
"git.hlsq.asia/mmorpg/service-user/internal/dao/model"
|
|
"git.hlsq.asia/mmorpg/service-user/internal/dao/repository"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (s *Server) Login(ctx context.Context, req *grpc_pb.LoginReq) (*grpc_pb.LoginResp, error) {
|
|
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_%v", user.Sn)
|
|
_ = userDao.Updates(user)
|
|
} else {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
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.GetUserInfoResp{
|
|
USN: user.Sn,
|
|
Name: user.Name,
|
|
}, nil
|
|
}
|