package com.ningdatech.pmapi.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @Classname RefreshKeyUtil
 * @Description
 * @Date 2023/6/27 10:03
 * @Author PoffyZhang
 */
@Slf4j
public class RefreshKeyUtil {
    public static String getRequestSecret(String appKey, String appSecret) {
        Long requestTime  = System.currentTimeMillis();
        // 刷新秘钥
        HttpUtil httpUtil = HttpUtil.getInstance();
        log.info("请求密钥" + appSecret);
        String refreshSign = Md5Utils.hash (appKey + appSecret + requestTime);
        String refreshUrl = String.format ("http://59.202.38.178/gateway/app/refreshTokenByKey.htm" +
                "?appKey=%s&requestTime=%s&sign=%s", appKey, requestTime +"",refreshSign);
        log.info(refreshUrl);
        String result = httpUtil.sendHttpGet(refreshUrl);
        log.info(result);
        JSONObject dataJson = JSON.parseObject(result).getJSONObject("datas");
        if(Objects.nonNull(dataJson)){
            return dataJson.getString("requestSecret");
        }
        return result;
    }
}
