youtu_grpc/app/auth_service/internal/logic/code2_session_logic.go
2025-02-10 17:30:29 +08:00

66 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"context"
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/auth_service/auth_service"
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/auth_service/internal/svc"
"gitea.youtukeji.com.cn/xiabin/youtu_grpc/app/user_management/user_management"
"github.com/zeromicro/go-zero/core/logx"
)
type Code2SessionLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewCode2SessionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Code2SessionLogic {
return &Code2SessionLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *Code2SessionLogic) Code2Session(in *auth_service.Code2SessionRequest) (*auth_service.Code2SessionResponse, error) {
//获取cli调用抖音或者微信api
cli, err := l.svcCtx.Cli.Get(in.AppId)
if err != nil {
return nil, err
}
res, err := cli.Code2Session(l.ctx, in.Code, in.AnonymousCode)
if err != nil {
return nil, err
}
//获取appId
ac := l.svcCtx.Query.AppAccount
acModel, err := ac.WithContext(l.ctx).Where(ac.AppID.Eq(in.AppId)).First()
if err != nil {
return nil, err
}
//获取用户id
au := l.svcCtx.Query.AppUser
auModel, err := au.WithContext(l.ctx).Where(au.AppAccountID.Eq(acModel.ID), au.Openid.Eq(res.OpenID)).FirstOrCreate()
if err != nil {
return nil, err
}
//如果没有用户id则创建一个空用户
if auModel.UserID == nil {
userId, err := l.svcCtx.UserManagerClient.CreateEmptyUser(l.ctx, &user_management.Empty{})
if err != nil {
return nil, err
}
auModel.UserID = &userId.UserId
}
return &auth_service.Code2SessionResponse{
OpenId: res.OpenID,
UnionId: res.UnionID,
UserId: *auModel.UserID,
}, nil
}