package com.muyu.warning.util;

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

/**
 * @Author： Chen //redisService.setCacheObject("WECHAT_TOKEN",wechatToken.getAccessToken(),wechatToken.getExpiresIn(), TimeUnit.SECONDS);
 * @name：TokenUtil
 */
@Component
public class TokenUtil {
    private final static String APP_ID = "wx591e88abd208c297";

    private final static String APP_SECRET = "64d92a751ef24acc8d37b2e6d7672491";

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

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

}
