package com.hfkj.meituan;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.config.CommonSysConst;

import com.meituan.sqt.utils.EncryptUtil;
import com.meituan.sqt.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
  * @ClassName MeiTuanService
  * @Author Sum1Dream
  * @Description 美团接口
  * @Date 2024/8/26 下午4:52
 **/
public class MeiTuanService {


    private static Logger log = LoggerFactory.getLogger(MeiTuanService.class);


    /**
     * @MethodName loginFree
     * @Description: H5免登接口
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/27 上午11:16
     */
    public static JSONObject loginFree(Map<String , Object> map) throws Exception {

        JSONObject object = request("" , map);

        if (object.getBoolean("success") && object.getInteger("code") == 10000) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }

    }

    /**
     * 请求
     * @param postUrl  接口请求地址
     * @param param       参数
     * @return
     * @throws Exception
     */
    public static JSONObject request(String postUrl, Map<String,Object> param) throws Exception {



        log.info("============ 千猪请求-START =============");
        param.put("accessKey", CommonSysConst.getSysConfig().getMtAccessKey());
        param.put("content", EncryptUtil.aesEncrypt(JsonUtil.object2Json(param), CommonSysConst.getSysConfig().getMtSecretKey()));

        log.info("请求接口：" + postUrl);
        log.info("请求参数：" + JSONObject.toJSONString(param));

        JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getMtPostUrl() + postUrl, JSONObject.toJSONString(param));

        log.info("响应参数：" + response.toJSONString());
        log.info("============ 千猪请求-END ==============");
        return response;

    }
}
