63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
|
package wechat
|
||
|
|
||
|
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 WechatCode2UserIdLogic struct {
|
||
|
logx.Logger
|
||
|
ctx context.Context
|
||
|
svcCtx *svc.ServiceContext
|
||
|
}
|
||
|
|
||
|
func NewWechatCode2UserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WechatCode2UserIdLogic {
|
||
|
app_api_helper.Init(ctx, svcCtx.AppAccount)
|
||
|
return &WechatCode2UserIdLogic{
|
||
|
Logger: logx.WithContext(ctx),
|
||
|
ctx: ctx,
|
||
|
svcCtx: svcCtx,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *WechatCode2UserIdLogic) WechatCode2UserId(req *types.WechatCode2UserIdRequest) (resp uint64, err error) {
|
||
|
wechatCli, err := app_api_helper.WechatCli.GetWechatOpenApi(req.AppId)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
res, err := wechatCli.GetAuth().Code2Session(req.Code)
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
|
||
|
//todo 校验唯一
|
||
|
|
||
|
_, err = l.svcCtx.AppUser.Insert(l.ctx, aui)
|
||
|
|
||
|
return
|
||
|
}
|