package card_info_t_mall_game

import (
	"context"
	"kami/api/card_info_t_mall_game/v1"
	"kami/internal/consts"
	"kami/internal/errHandler"
	tmall "kami/utility/integration/tmall/api"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/glog"
)

func (c *ControllerV1) TMallGameAccountAuthorizeCallback(ctx context.Context, req *v1.TMallGameAccountAuthorizeCallbackReq) (res *v1.TMallGameAccountAuthorizeCallbackRes, err error) {
	switch req.Channel {
	case consts.TMallGameThirdPartyAgiso:
		glog.Info(ctx, "开始授权阿奇索")
	case consts.TMallGameThirdPartyTMall:
		glog.Info(ctx, "开始授权天猫店铺")
		_, err = tmall.NewClient().OAuth2Login(ctx, req.Code)
		if err != nil {
			glog.Error(ctx, "授权天猫店铺失败", err)
		}
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "天猫授权错误")
	default:
		err = gerror.NewCode(gcode.CodeNotSupported, "不支持的渠道")
	}
	return
}
