zbb378@sohu.com
2024-11-04 b2bad51be3c8d3e78d7f81a19415faeac2d0297c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.nova.sankuai.domain.api.tongbao;
 
import com.alibaba.fastjson.JSON;
import com.nova.sankuai.domain.api.yinsheng.SerialGenerator;
import com.nova.sankuai.domain.entity.CustomerUser;
import com.nova.sankuai.security.HttpClientUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
 
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
 
@Data
@Slf4j
public class TBUtil {
 
    /**
     * 日期格式化模板
     */
    private static ThreadLocal<SimpleDateFormat> dateFormatFactory = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmssSSS"));
 
 
    public static final String url = "http://api.mixuannet.cn/api/order/create";
    public static final String h5url = "https://h5.mixuannet.cn/#/login";
    public static final String payAmt = "3.2";
    public static final String appkey = "daodaohuanianka";
    public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU9dKONZdLK2nKvtNHGq3xzNou93GKDW/e9kX9K4f1hyif5612GdxbVUq+CaOkgChtqfcAbvrefbelRxJmr0Ujvv55BlpBGGxxtbMtI+B9EbceTseEzRj47BOIjIGgSlNC8V5J+QJJRzij/8tQK8p9oQzT25yqGGzkUOQjwp2PxwIDAQAB";
 
    /**
     * 数字签名,密钥算法
     */
    private static final String RSA_KEY_ALGORITHM = "RSA";
 
    /**
     * 公钥加密
     *
     * @param data      加密前的字符串
     * @param publicKey 公钥
     * @return 加密后的字符串
     * @throws Exception
     */
    public static String encryptByPubKey(String data, String publicKey) throws Exception {
        byte[] pubKey = decodeBase64(publicKey);
        byte[] enSign = encryptByPubKey(data.getBytes(), pubKey);
        return Base64.encodeBase64String(enSign);
    }
 
    /**
     * 密钥转成byte[]
     *
     * @param key
     * @return
     */
    public static byte[] decodeBase64(String key) {
        return Base64.decodeBase64(key);
    }
 
    /**
     * 公钥加密
     *
     * @param data   待加密数据
     * @param pubKey 公钥
     * @return
     * @throws Exception
     */
    public static byte[] encryptByPubKey(byte[] data, byte[] pubKey) throws Exception {
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
        PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    }
 
 
    public static String getJsonString(String timestamp,String payAmt,String channelUserId,String serialNo){
        HashMap<String, String> map = new HashMap<>();
        map.put("timestamp",timestamp);
        map.put("payAmt",payAmt);
        map.put("channelUserId",channelUserId);
        map.put("serialNo",serialNo);
        return JSON.toJSONString(map);
    }
 
    public static String getJsonString(String sign,String appkey){
        HashMap<String, String> map = new HashMap<>();
        map.put("sign",sign);
        map.put("appkey",appkey);
        return JSON.toJSONString(map);
    }
 
    /**
     * 生成规则 17位日期毫秒数 + 消息类型(10) + 随机数补齐32位
     *
     */
    public static String getOrder() {
        StringBuilder sb = new StringBuilder();
        sb.append(dateFormatFactory.get().format(new Date())).append("10")
                .append(SerialGenerator.generateRandomSerial(29 - sb.length()));
        return sb.toString();
    }
 
 
    public static TBResposeInfo createOrder(CustomerUser customerUser,String serialNo){
        String channelUserId = customerUser.getPhone();
 
        //传输数据
        String dateString = TBUtil.getJsonString(System.currentTimeMillis() + "", TBUtil.payAmt, channelUserId, serialNo);
        String result = "" ;
        try {
            String sign = TBUtil.encryptByPubKey(dateString, TBUtil.publicKey);
            String jsonString = TBUtil.getJsonString(sign, appkey);
            result = HttpClientUtil.getInstance().postJsonData(TBUtil.url, jsonString);
            log.info("通宝会员权益:通宝返回:{}", result);
        } catch (Exception e) {
            log.error("通宝会员权益:调用第三方接口失败",e);
            e.printStackTrace();
            return null;
        }
        try {
            TBRespose tbRespose = JSON.parseObject(result, TBRespose.class);
            if (!"200".equals(tbRespose.getCode())) {
                log.error("通宝会员权益:创建订单失败",tbRespose.toString());
                return null;
            }else {
                return tbRespose.getInfo();
            }
        }catch (Exception e){
            log.error("通宝会员权益:JSON转换失败",e);
            return null;
        }
    }
 
    public static String getH5Url(String orderNumber,CustomerUser customerUser){
        //返回给前端H5请求地址
        String h5url = TBUtil.h5url + "?oId=" + orderNumber + "&cId=" + customerUser.getPhone() + "&qd=" + TBUtil.appkey + "&ts=" + System.currentTimeMillis();
        return h5url;
    }
 
}