package com.seeyon.apps.bip.util;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.kg.commons.utils.MD5Util;
import com.seeyon.apps.bip.vo.BipResponse;
import com.seeyon.ctp.util.json.JSONUtil;
import org.springframework.util.DigestUtils;

import java.util.Base64;

/**
 * 功能描述:<br>
 * <pre>
 *
 * </pre>
 *
 * @Author: FanGaowei
 * @Date: 2022/11/30
 */
public class BipHttpUtil {

    private BipHttpUtil() {

    }

    public static String doPost(String url, String body, String clientId, String security) throws Exception {
        String ts = System.currentTimeMillis() + "";
        String signature = ts + security;
        byte[] bytes = DigestUtils.md5Digest(signature.getBytes("UTF-8"));
        signature = Base64.getEncoder().encodeToString(bytes);
        return HttpRequest.post(url).header("Content-Type", "application/json;charset=utf-8")
                .header("ts", ts)
                .header("signature", signature)
                .header("client_id", clientId)
                .body(body).execute().body();
    }

    public static BipResponse parseResponse(String response) throws Exception {
        try {
            JSONObject object = JSONUtil.parseJSONString(response, JSONObject.class);
            return new BipResponse(object);
        } catch (Exception e) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Status", "0");
            jsonObject.put("Msg", "解析json异常");
            jsonObject.put("Data", response);
            return new BipResponse(jsonObject);
        }
    }



    public static void main(String[] args) {
        String url = "http://10.1.90.21:9083/servlet/NCTokenServlet?dsname=BIPcs&usercode=UFYJY&system=OA&busicentercode=sxbip";
    }

}
