xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
package com.nuvole.util.pay.allinPay.yunstNew;
 
import cn.hutool.crypto.SecureUtil;
 
import cn.hutool.http.HttpUtil;
 
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.nuvole.util.pay.allinPay.yunstNew.po.ResultVo;
import com.nuvole.util.pay.allinPay.yunstNew.po.YstPayUrl;
 
 
import java.util.*;
 
/**
 * Created by chenZhimin
 * Date:2020/8/24
 * time:10:09
 */
public class ApiPost {
 
    public static ResultVo apiPost(String url, Object obj) {
        System.out.println(url);
        System.out.println(getSignBody(obj));
        ResultVo result=new ResultVo();
        try {
             JSONObject data= JSONUtil.xmlToJson(HttpUtil.post(url,getSignBody(obj)));
             result=JSON.parseObject(JSON.toJSONString(data.get("ResultVo")),ResultVo.class);
            System.out.println("wwwwwwwwwwwwww"+result);
        } catch (Exception e) {
            System.out.println("wwwwwwwwwwwwww"+e);
            result.setCode("0");
            result.setMsg("接口请求失败");
            result.setData("");
        }
        return result;
    }
 
    /**
     * 组装带签名数据
     * @param body
     * @return
     */
 
    public static String getSignBody(Object body) {
         Map map= JSON.parseObject(JSON.toJSONString(body));
        String version=YstPayUrl.version;
        String terMerchantId= YstPayUrl.terMerchantId;
        map.put("version",version);
        map.put("terMerchantId",terMerchantId);
        String orgSign=createLinkStringByGet(map)+"&key="+YstPayUrl.ked;
        System.out.println("orgSign--------"+orgSign);
        String sign=SecureUtil.md5(orgSign);
        map.put("sign",sign.toUpperCase());
         return JSON.toJSONString(map);
    }
 
       /**
       * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
       * @param params 需要排序并参与字符拼接的参数组
       * @return 拼接后字符串
       * @throws UnsupportedEncodingExceptio
       */
 
    public static String createLinkStringByGet(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            Object value = params.get(key);
            if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;
    }
 
}