package com.qs.mp.common.utils.http;

import com.qs.mp.common.utils.RandomUtils;
import com.qs.mp.common.utils.sign.Md5Utils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zhongcp
 * @Date 2022/6/2
 */
public class MpHttpRequestUtils {
  public static String request(String url, String params) {
    String nonce = RandomUtils.generateString(20);
    Date nowTime = new Date();
    String timestamp = String.valueOf(nowTime.getTime());

    String md5key = "3Jr8S1K18rcC1wAfv8";
    String l = params + "nonce" + timestamp + md5key;
    System.out.println("sign str:" + l);
    String sign = Md5Utils.hash(l);
    System.out.println("sign rst:" + sign);
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("content-type", "application/json; charset=UTF-8");
    headers.put("x-zz-timestamp", timestamp);
    // 本地登录状态
    String token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6Ijg3ZWM0OGMwLWQ4ZWYtNDcwYS05ZTQ5LThhOWI0NWMzOWM3MSJ9.q3l1Q0xoWBvXY6eZKfEAKXHZclb2ssMdGO4xd5IjvSNnIRdOLT8kA9jsGo8iR2KZOXjaEfN_DBxPfD6vGZpemg";

    if (!StringUtils.isBlank(token)) {
      headers.put("Authorization", "Bearer "+token);
    }
    String reqUrl = url + "sign=" + sign + "&nonce=" + nonce;
    return OkHttpUtil.post(reqUrl, params, headers);
  }

}
