package com.muyu.util;

import com.alibaba.fastjson2.JSON;
import com.muyu.domain.AccessToken;
import org.springframework.stereotype.Component;

/**
 * @className: TokenUtil  ️✈️
 * @author: Yang 鹏  🦅
 * @date: 2024/9/17 20:26 ⏰
 * @Version: 1.0
 * @description:
 */
@Component
public class TokenUtil {
    public final static String APP_ID = "xyra10c6f55da3c0553";

    public final static String APP_SECRET ="72d285981f3f51274e79dcda5e498a81";

    //域名
    public final static String REDIRECT_DOMAIN ="u95ryi.natappfree.cc";

    private static AccessToken accessToken = new AccessToken();




    private static void getToken(){
        String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",APP_ID,APP_SECRET);
        String request = OkHttpUtils.sendGetRequest(url);
            AccessToken wechatToken = JSON.parseObject(request, AccessToken.class);
            //redisService.setCacheObject("WECHAT_TOKEN",wechatToken.getAccessToken(),wechatToken.getExpiresIn(), TimeUnit.SECONDS);
        if (wechatToken != null) {
            accessToken.setExpiresTime(wechatToken.getExpires_in());
            accessToken.setAccess_token(wechatToken.getAccess_token());
        }
    }
    public static String getAccessToken(){
            getToken();
        return accessToken.getAccess_token();
    }

}
