package com.muyu.server.token;

import com.alibaba.fastjson.JSON;
import com.muyu.domain.AccessToken;
import com.muyu.server.util.OkHttpUtils;
import org.springframework.stereotype.Component;

/**
 * 连接微信服务器(微信公众号)
 */
@Component
public class TokenUtil {
    public final static String APP_ID = "wx962013f3b1eb0a51";

    public final static String APP_SECRET ="5c4c0b2130e6fdf86d989237f9e201dc";

    public static void main(String[] args) {
        System.out.println(getAccessToken());
        System.out.println(getAccessToken());
    }

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

    private static AccessToken accessToken = new AccessToken();

//    {"access_token":"84_E1XHZXQpPj-K6mbCRmGn58iIPU3w7ViwtZRwAt5QN39FP9VBLPq57S_XjRQY88upoYyT4Boil2P6gY8uoKLHuth-W5OczJzr5EGZoWeh2PwhTKEfR1NcCWjqgTYJREcAIAZHU","expires_in":7200}
    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);
        System.out.println(request);
            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();
    }

}
