package com.ai.da.common.httpdata.token;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class TokenQuery {

    private static final String GET_TOKEN_DOMAIN = "https://www.szsige.com";
    private static final String GENERATE_USER_DOMAIN = "https://www.szsige.com";

    public static JSONObject getToken(String session) {
        String url = GET_TOKEN_DOMAIN + TokenApis.GET_TOKEN.getUrl() + session;
        log.info("获取用户token接口请求url:" + url);
        HttpResponse httpResponse = HttpUtil.createPost(url).execute();
        log.info("获取用户token接口响应：" + httpResponse);
        if (httpResponse.isOk() && StrUtil.isNotEmpty(httpResponse.body())) {
            return JSONObject.parseObject(httpResponse.body());
        }
        return null;
    }

    public static JSONObject generateUser(Map<String, Object> param, String token) {
        HttpResponse httpResponse = HttpUtil.createPost(GENERATE_USER_DOMAIN + TokenApis.GENERATE_USER.getUrl())
                .body(JSONObject.toJSONString(param != null ? param : new HashMap<>()))
                .header("Authorization", "Bearer " + token)
                .header("X-Promiss", "9672233956")
                .execute();
        log.info("生成用户信息接口响应：" + httpResponse);
        if (httpResponse.isOk() && StrUtil.isNotEmpty(httpResponse.body())) {
            return JSONObject.parseObject(httpResponse.body());
        }
        return null;
    }

}
