package card_info_t_mall_game

import (
	"context"
	"kami/utility/integration/tmall"

	"github.com/duke-git/lancet/v2/pointer"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/os/gtime"

	"kami/api/card_info_t_mall_game/v1"
)

func (c *ControllerV1) TMallGameAccountTMallAuthStatus(ctx context.Context, req *v1.TMallGameAccountTMallAuthStatusReq) (res *v1.TMallGameAccountTMallAuthStatusRes, err error) {
	//检查OAuth Token是否正常
	res = &v1.TMallGameAccountTMallAuthStatusRes{
		Status: false,
	}
	tokenInfo, err := tmall.GetCachedOAuthToken(ctx)
	if err != nil {
		glog.Error(ctx, "获取缓存授权信息失败", err)
		return
	}
	if tokenInfo == nil || pointer.IsNil(tokenInfo) {
		return
	}
	if gtime.NewFromTimeStamp(tokenInfo.ExpireTime).Sub(gtime.Now()) > 0 {
		res.Status = true
		res.ExpiredAt = gtime.NewFromTimeStamp(tokenInfo.ExpireTime).String()
	}
	return
}
