youtu_server/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go

60 lines
1.3 KiB
Go
Raw Normal View History

2025-01-20 01:55:44 +08:00
package douyin
import (
"context"
"errors"
"youtu_server/game_open_api/internal/app_api_helper"
"youtu_server/game_open_api/model"
"youtu_server/game_open_api/internal/svc"
"youtu_server/game_open_api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DouyinCode2UserIdLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDouyinCode2UserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DouyinCode2UserIdLogic {
app_api_helper.Init(ctx, svcCtx.AppAccount)
return &DouyinCode2UserIdLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DouyinCode2UserIdLogic) DouyinCode2UserId(req *types.DouyinCode2UserIdRequest) (resp int64, err error) {
douyinCli, err := app_api_helper.DouyinCli.GetDouYinOpenApi(req.AppId)
if err != nil {
return
}
res, err := douyinCli.Api.Code2Session(req.Code, req.AnonymousCode)
if err != nil {
return
}
if res.Errcode != 0 {
err = errors.New(res.Errmsg)
return
}
accountId, err := l.svcCtx.AppAccount.FindIdByAppId(l.ctx, req.AppId)
if err != nil {
return
}
aui := &model.AppUser{
AppAccountId: accountId,
Openid: res.Openid,
Unionid: res.Unionid,
AnonymousOpenid: res.AnonymousOpenid,
}
_, err = l.svcCtx.AppUser.Insert(l.ctx, aui)
return
}