package com.nova.sankuai.domain.api.huizhongzhidai;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.nova.sankuai.domain.dto.CustomerCompareDto;
|
import com.nova.sankuai.domain.entity.Customer;
|
import com.nova.sankuai.security.MD5Encryption;
|
import io.swagger.annotations.ApiModel;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.codec.binary.Base64;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import javax.crypto.*;
|
import javax.crypto.spec.DESedeKeySpec;
|
import javax.crypto.spec.IvParameterSpec;
|
import java.nio.charset.StandardCharsets;
|
import java.security.Key;
|
|
/**
|
* <p>
|
* 惠众直贷接口公司对接
|
* </p>
|
*
|
* @author: zcj 2022/2/17
|
*/
|
@Data
|
@Slf4j
|
@Component
|
@ApiModel(value = "惠众直贷接口公司对接")
|
public class HuiZhongZhiDaiUtil {
|
@Value("${huizhongzhidai.hitUrl}")
|
public String hitUrl;
|
|
@Value("${huizhongzhidai.commitUrl}")
|
public String commitUrl;
|
|
@Value("${huizhongzhidai.source}")
|
public String source;
|
|
@Value("${huizhongzhidai.key}")
|
public String key;
|
|
public JSONObject parseHitParam(Customer customer) {
|
JSONObject object = new JSONObject();
|
object.put("registerFrom", this.source);
|
// TODO 分支合并后改加密
|
String phone = MD5Encryption.getMD5(customer.getPhone()) ;
|
object.put("phoneNo", phone);
|
return object;
|
}
|
|
public JSONObject parseCommitParam(Customer customer) {
|
CustomerCompareDto parse = CustomerCompareDto.parse(customer);
|
JSONObject json = new JSONObject();
|
json.put("channel_source", this.source);
|
json.put("cust_name", customer.getName());
|
json.put("mobile", customer.getPhone());
|
json.put("age", customer.getAge()==null?0:customer.getAge());
|
json.put("sex", "0");
|
json.put("city", customer.getCityName().substring(0, customer.getCityName().indexOf('市')));
|
json.put("apply_limit", parse.getQuota());
|
return json;
|
}
|
|
public String encrypt(String json){
|
byte[] bytes = tripleDES(Cipher.ENCRYPT_MODE, json.getBytes(StandardCharsets.UTF_8), key.getBytes());
|
byte[] base64 = Base64.encodeBase64(bytes);
|
return new String(base64);
|
}
|
|
private static byte[] tripleDES(int opmode, byte[] data, byte[] secretKey) {
|
return cipher("DESede", "DESede/CBC/PKCS5Padding", opmode, data, "01234567".getBytes(), secretKey);
|
}
|
|
private static byte[] cipher(String algorithm, String transformation, int opmode, byte[] data, byte[] iv, byte[] secretKey) {
|
try {
|
Key key = SecretKeyFactory.getInstance(algorithm).generateSecret(new DESedeKeySpec(secretKey));
|
IvParameterSpec spec = new IvParameterSpec(iv);
|
Cipher cipher = Cipher.getInstance(transformation);
|
cipher.init(opmode, key, spec);
|
return cipher.doFinal(data);
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
|
}
|