package com.muyu.util;

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

/**
 * @ClassName AccessToken
 * @Description 描述
 * @Author YiBo.Liu
 * @Date 2024/9/18 15:48
 */
@Component
public class TokenUtil {
    private final static String APP_ID = "wx74741af76decd078";

    private final static String APP_SECRET ="540af183133e701051b726f936ebbfe9";

    private static AccessToken accessToken = new AccessToken();


    public 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);
        if (wechatToken != null) {
            accessToken.setExpiresTime(wechatToken.getExpires_in());
            accessToken.setAccess_token(wechatToken.getAccess_token());
        }
    }

    /**
     * 获取AccessToken
     * @return
     */
    public static String getAccessToken(){
            getToken();
        return accessToken.getAccess_token();
    }

}
