package com.ningdatech.pmapi.irs.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ningdatech.pmapi.common.util.CryptUtils;
import com.ningdatech.pmapi.common.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @Classname RefreshTokenManage
 * @Description
 * @Date 2023/7/20 15:53
 * @Author PoffyZhang
 */
@Slf4j
public class RefreshTokenUtil {

    public static String refrshByAppScret() throws NoSuchAlgorithmException {
        long timestamp = System.currentTimeMillis();
        String appSecret = "e066f496194540afa829c66983d4710e";
        String appKey = "8fb070a1f1194b2ebffdc6f596bb8a96";
        String sign = CryptUtils.MD5Encode(appKey + appSecret + timestamp);
        HttpComponentsClientHttpRequestFactory factory = HttpUtil.generateHttpRequestFactory();
        RestTemplate restTemplate;
        if(Objects.nonNull(factory)){
            restTemplate = new RestTemplate(factory);
        }else{
            restTemplate = new RestTemplate();
        }
        String url = "https://interface.ls.local/a/api/refreshTokenKey?appKey=" + appKey +
                "&requestTime=" + timestamp + "&sign=" + sign;
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);

        String body = forEntity.getBody();
        log.info(body);

        JSONObject bodyJson = JSON.parseObject(body);
        JSONObject datas = bodyJson.getJSONObject("datas");
        String requestSecret = datas.getString("requestSecret");
        log.info("requestSecret :{}",requestSecret);
        return requestSecret;
    }

}
