package com.hai.config; import cn.jiguang.common.utils.StringUtils; import com.alibaba.fastjson.JSONObject; import com.hai.common.utils.DateUtil; import com.hai.common.utils.HttpsUtils; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.stereotype.Component; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import java.util.*; /** * @serviceName .java * @author Sum1Dream * @version 1.0.0 * @Description // 四川中石油 * @createTime 18:33 2023/11/13 **/ @Component public class PetroConfig { private static String reqUrl; private static String petroAppKey; private static String petroAppid; private static String petroAesKey; private static final String version = "1.0"; private final static String charset = "UTF-8"; public static void init(Integer type) { if (type == 1) { reqUrl = CommonSysConst.getSysConfig().getScPetroUrl(); petroAppKey = CommonSysConst.getSysConfig().getScPetroAppKey(); petroAppid = CommonSysConst.getSysConfig().getScPetroAppid(); petroAesKey = CommonSysConst.getSysConfig().getScPetroAesKey(); } else { reqUrl = CommonSysConst.getSysConfig().getGzPetroUrl(); petroAppKey = CommonSysConst.getSysConfig().getGzPetroAppKey(); petroAppid = CommonSysConst.getSysConfig().getGzPetroAppid(); petroAesKey = CommonSysConst.getSysConfig().getGzPetroAesKey(); } } /** * @Author Sum1Dream * @Name synCouponRule * @Description // 获取券列表 * @Date 11:17 2023/11/17 * @return com.alibaba.fastjson.JSONObject */ public static JSONObject synCouponRule() throws Exception{ Map req = new HashMap<>(); req.put("version", version); req.put("appid", petroAppid); req.put("signtype", "MD5"); req.put("timestamp", DateUtil.date2String(new Date(), DateUtil.Y_M_D_HMS)); // 业务数据 JSONObject billBizContent = new JSONObject(); billBizContent.put("pageSize" , 100); //业务内容加密 String bizContent = JSONObject.toJSONString(billBizContent); bizContent = encrypt(bizContent, petroAesKey); //加密的内容放到参数中 req.put("biz_content", bizContent); //生成签名 String sign = createSign(req, petroAppKey); //签名放到参数中 req.put("sign", sign); // 请求接口 return HttpsUtils.doPost(reqUrl + "syn-coupon-rule/v1", req , new HashMap<>()); } /** * @Author Sum1Dream * @Name getCoupon * @Description // 获取券code * @Date 11:19 2023/11/17 * @Param code * @Param phone * @Param orderNo * @return com.alibaba.fastjson.JSONObject */ public static JSONObject getCoupon(String code , String phone , String orderNo) throws Exception{ Map req = new HashMap<>(); req.put("version", version); req.put("appid", petroAppid); req.put("signtype", "MD5"); req.put("timestamp", DateUtil.date2String(new Date(), DateUtil.Y_M_D_HMS)); // 业务数据 JSONObject billBizContent = new JSONObject(); billBizContent.put("request_code" , code); billBizContent.put("user_code" , phone); billBizContent.put("out_trade_no" , orderNo); //业务内容加密 String bizContent = JSONObject.toJSONString(billBizContent); bizContent = encrypt(bizContent, petroAesKey); //加密的内容放到参数中 req.put("biz_content", bizContent); //生成签名 String sign = createSign(req, petroAppKey); //签名放到参数中 req.put("sign", sign); // 请求接口 return HttpsUtils.doPost(reqUrl + "get-coupon", req , new HashMap<>()); } /** * @Author Sum1Dream * @Name couponDetail * @Description // 获取静态码 * @Date 11:20 2023/11/17 * @Param ticketNum * @return com.alibaba.fastjson.JSONObject */ public static JSONObject couponDetail(String ticketNum) throws Exception{ Map req = new HashMap<>(); req.put("version", version); req.put("appid", petroAppid); req.put("signtype", "MD5"); req.put("timestamp", DateUtil.date2String(new Date(), DateUtil.Y_M_D_HMS)); // 业务数据 JSONObject billBizContent = new JSONObject(); billBizContent.put("ticket_num" , ticketNum); //业务内容加密 String bizContent = JSONObject.toJSONString(billBizContent); bizContent = encrypt(bizContent, petroAesKey); //加密的内容放到参数中 req.put("biz_content", bizContent); //生成签名 String sign = createSign(req, petroAppKey); //签名放到参数中 req.put("sign", sign); // 请求接口 return HttpsUtils.doPost(reqUrl + "couponDetail", req , new HashMap<>()); } /** * @Author Sum1Dream * @Name cancelCoupon * @Description // 作废下单 * @Date 11:21 2023/11/17 * @Param ticketNum * @return com.alibaba.fastjson.JSONObject */ public static JSONObject cancelCoupon(String ticketNum) throws Exception{ Map req = new HashMap<>(); req.put("version", version); req.put("appid", petroAppid); req.put("signtype", "MD5"); req.put("timestamp", DateUtil.date2String(new Date(), DateUtil.Y_M_D_HMS)); // 业务数据 JSONObject billBizContent = new JSONObject(); billBizContent.put("ticket_num" , ticketNum); //业务内容加密 String bizContent = JSONObject.toJSONString(billBizContent); bizContent = encrypt(bizContent, petroAesKey); //加密的内容放到参数中 req.put("biz_content", bizContent); //生成签名 String sign = createSign(req, petroAppKey); //签名放到参数中 req.put("sign", sign); // 请求接口 return HttpsUtils.doPost(reqUrl + "cancelCoupon", req , new HashMap<>()); } /** * @param params 签名params null和""不参与签名 * @param key 密钥 * @return 签名code */ public static String createSign(Map params, String key) { if (StringUtils.isEmpty(key)) { return null; } if (params == null || params.keySet() == null || params.keySet().isEmpty()) { return null; } Object[] parKeys = params.keySet().toArray(); Arrays.sort(parKeys); StringBuffer temp = new StringBuffer(); boolean first = true; for (Object parKey : parKeys) { String parKeyStr = null; if(parKey != null){ parKeyStr = parKey.toString(); } if ("sign".equals(parKeyStr)) { continue; } if (first) { first = false; } else { temp.append("&"); } Object value = params.get(parKey); if (value != null) { String v = value.toString(); if (!"".equals(v)) { temp.append(parKey).append("=").append(value.toString()); } } } temp.append("&").append("key").append("=").append(key); return DigestUtils.md5Hex(temp.toString()); } /** * @param content AES加密前的明文 * @param key 秘钥 * @return AES加密后的内容 */ public static String encrypt(final String content,final String key) { try { byte[] raw = key.getBytes(charset); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); //"算法/模式/补码方式" Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(content.getBytes(charset)); //此处使用Hex做转码功能,同时能起到2次加密的作用。 return DatatypeConverter.printHexBinary(encrypted); } catch (Exception e) { e.printStackTrace(); return null; } } /** * @Author Sum1Dream * @Name decrypt * @Description // 解密 * @Date 16:19 2023/11/15 * @Param content * @Param key * @return java.lang.String */ public static String decrypt(final String content) { try { byte[] raw = petroAesKey.getBytes("UTF-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(2, skeySpec); byte[] hexBinary = DatatypeConverter.parseHexBinary(content); byte[] original = cipher.doFinal(hexBinary); return new String(original, "UTF-8"); } catch (Exception var7) { return null; } } }