package com.muyu.wechat.util;

import com.alibaba.fastjson2.JSON;

import com.muyu.wechat.domain.AccessToken;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: YZL
 * @Created: 2024/9/19 20:26
 */
@Component
public class TokenUtil {
    private final static String APP_ID = "wx1d843111e24945c4";

    private final static String APP_SECRET = "f625fc9e03e6bb5f4959b51f0650f1a8";

    private static final 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());
        }
    }

    public static String getAccessToken() {
        getToken();
        return accessToken.getAccess_token();
    }

}
