46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"sundynix-micro-go/app/user/model"
|
|
"sundynix-micro-go/app/user/rpc/internal/svc"
|
|
"sundynix-micro-go/app/user/rpc/user"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type GetUserByOpenIdLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetUserByOpenIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByOpenIdLogic {
|
|
return &GetUserByOpenIdLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// 根据OpenId获取用户信息
|
|
func (l *GetUserByOpenIdLogic) GetUserByOpenId(in *user.GetUserByOpenIdReq) (*user.GetUserByOpenIdResp, error) {
|
|
var u model.SundynixUser
|
|
err := l.svcCtx.DB.Where("open_id = ?", in.OpenId).First(&u).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, status.Error(codes.NotFound, "用户不存在")
|
|
}
|
|
l.Errorf("查询用户失败: %v", err)
|
|
return nil, status.Error(codes.Internal, "查询用户失败")
|
|
}
|
|
|
|
return &user.GetUserByOpenIdResp{
|
|
User: convertUserToProto(&u),
|
|
}, nil
|
|
}
|