feat 包命名规范
This commit is contained in:
82
internal/grpcserver/server.go
Normal file
82
internal/grpcserver/server.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/http/httpresp"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"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"
|
||||
"git.hlsq.asia/mmorpg/service-user/internal/wechat"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (s *Server) PhoneLogin(ctx context.Context, req *rspb.PhoneLoginReq) (*rspb.PhoneLoginResp, error) {
|
||||
userDao := repository.NewUserDao(ctx, s.query, redis.GetCacheClient())
|
||||
user, err := userDao.FindByPhone(req.Phone)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if user, err = userDao.Create(&model.User{
|
||||
Phone: req.Phone,
|
||||
}); err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
user.Name = fmt.Sprintf("user_%v", user.Sn)
|
||||
_ = userDao.Updates(user)
|
||||
} else {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
return &rspb.PhoneLoginResp{
|
||||
USN: user.Sn,
|
||||
Name: user.Name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) WxMiniLogin(ctx context.Context, req *rspb.WxMiniLoginReq) (*rspb.WxMiniLoginResp, error) {
|
||||
session, err := wechat.MiniCode2Session(req.Code)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
userDao := repository.NewUserDao(ctx, s.query, redis.GetCacheClient())
|
||||
user, err := userDao.FindByWxUnionIDOrOpenID(session.UnionID, session.OpenID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if user, err = userDao.Create(&model.User{
|
||||
WxUnionID: session.UnionID,
|
||||
WxMiniOpenID: session.OpenID,
|
||||
}); err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
user.Name = fmt.Sprintf("user_%v", user.Sn)
|
||||
_ = userDao.Updates(user)
|
||||
} else {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
return &rspb.WxMiniLoginResp{
|
||||
USN: user.Sn,
|
||||
Name: user.Name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) GetUserInfo(ctx context.Context, req *rspb.GetUserInfoReq) (*rspb.GetUserInfoResp, error) {
|
||||
if !utils.ShouldBindUsn(ctx, &req.USN) {
|
||||
return nil, utils.ErrorsWrap(httpresp.ParamError)
|
||||
}
|
||||
userDao := repository.NewUserDao(ctx, s.query, redis.GetCacheClient())
|
||||
user, err := userDao.FindBySn(req.USN)
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
|
||||
return &rspb.GetUserInfoResp{
|
||||
USN: user.Sn,
|
||||
Name: user.Name,
|
||||
}, nil
|
||||
}
|
||||
43
internal/grpcserver/server_init.go
Normal file
43
internal/grpcserver/server_init.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/config"
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-user/internal/dao/query"
|
||||
"git.hlsq.asia/mmorpg/service-user/internal/dao/repository"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
rspb.UnimplementedUserServer
|
||||
service.Base
|
||||
query *query.Query
|
||||
}
|
||||
|
||||
func NewServer(cfg *config.GrpcConfig) *Server {
|
||||
s := &Server{
|
||||
Base: service.Base{
|
||||
Target: common.KeyDiscoverUser,
|
||||
ServiceName: common.KeyDiscoverServiceNameUser,
|
||||
Cfg: cfg,
|
||||
},
|
||||
}
|
||||
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption
|
||||
s.Base.OnInit = s.OnInit
|
||||
s.Base.OnClose = s.OnClose
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) OnInit(serve *grpc.Server) {
|
||||
rspb.RegisterUserServer(serve, s)
|
||||
s.query = repository.Query()
|
||||
}
|
||||
|
||||
func (s *Server) OnClose() {
|
||||
}
|
||||
Reference in New Issue
Block a user