puzhibing
2023-02-15 2811bab657aab4145b65a45a824fb63e93b58e30
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
package com.stylefeng.guns.modular.system.util;
 
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.MD5Util;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 中国移动工具类
 */
@Component
public class ChinaMobileUtil {
 
    private String APIKey = "zj42494b1bdd416b9762229af6b5cbbd";
 
    private String SecretKey = "30323561316534653735613230316339";
 
    @Autowired
    private HttpClientUtil httpClientUtil;
 
 
    /**
     * 绑定小号
     * @param phoneA
     * @param phoneB
     * @param areaCode
     * @return
     * @throws Exception
     */
    public Map<String, String> midAxbBindSend(String phoneA, String phoneB, Integer areaCode) throws Exception{
        //组装请求对象-具体描述见开发文档-订单小号-AXB接口规范部分内容
        Map<String, Object> request = new HashMap<>();
        request.put("APPID", APIKey);
        request.put("bindtype", "AXB");
        request.put("requestId", UUIDUtil.getRandomCode(16));
        request.put("record", "0");
        //用户号码,必填,格式遵循国际电信联盟定义的E.164标准
        request.put("telA", "86" + phoneA);
        //用户号码B,必填,格式遵循国际电信联盟定义的E.164标准
        request.put("telB", "86" + phoneB);
        //需要选择的小号所属区号,当telX不写时必填,例如:杭州(571)
        request.put("areaCode", 571);
        //订单小号,非必填,当指定小号绑定时填写,格式遵循国际电信联盟定义的E.164标准
//        request.setTelX("8618867110000");
        //绑定关系过期失效时间,秒,取值必须大于0且最大值不超过4294967296
        request.put("expiration", Integer.valueOf(7 * 24 * 60 * 60));
 
        Map<String, String> header = new HashMap<String, String>(3);
        header.put("Authorization", "Basic " + new String(Base64.encodeBase64((APIKey + ":" + SecretKey).getBytes())));
        header.put("Content-Type", "application/json;charset=utf-8");
        String post = httpClientUtil.pushHttpRequset("POST", "https://ct.open.10086.cn/ordernumber/v1/binding", request, header, "json");
        Map<String, String> map1 = new HashMap<>();
        if(post.indexOf("0000") != -1){
            JSONObject jsonObject = JSONObject.parseObject(post);
            if(jsonObject.getString("code").equals("0000")){
                map1.put("code", "200");
                map1.put("msg", jsonObject.getString("message"));
                map1.put("telX", jsonObject.getString("x_no").substring(2));
                map1.put("bindId", jsonObject.getString("bindId"));
            }else{
                map1.put("code", jsonObject.getString("code"));
                map1.put("msg", jsonObject.getString("message"));
            }
        }else{
            map1.put("code", "-1");
            map1.put("msg", post);
        }
 
        return map1;
    }
 
 
    /**
     * 解绑小号关系
     * @param bindId    绑定关系id
     * @return
     * @throws Exception
     */
    public Map<String, String> midAxbUnBindSend(String bindId) throws Exception{
        //组装请求对象-具体描述见开发文档-订单小号-AXB接口规范部分内容
        Map<String, Object> request = new HashMap<>();
        //绑定关系ID
        request.put("APPID", APIKey);
        request.put("bindId", bindId);
 
        Map<String, String> header = new HashMap<String, String>(3);
        header.put("Authorization", "Basic " + new String(Base64.encodeBase64((APIKey + ":" + SecretKey).getBytes())));
        header.put("Content-Type", "application/json;charset=utf-8");
        String post = httpClientUtil.pushHttpRequset("POST", "https://ct.open.10086.cn/ordernumber/v1/unbinding", request, header, "json");
        JSONObject jsonObject = JSONObject.parseObject(post);
        Map<String, String> map1 = new HashMap<>();
        if(jsonObject.getString("code").equals("0000")){
            map1.put("code", "200");
            map1.put("msg", jsonObject.getString("message"));
        }else{
            map1.put("code", jsonObject.getString("code"));
            map1.put("msg", jsonObject.getString("message"));
        }
        return map1;
    }
 
 
 
 
    public Map<String, String> HeaderUtils(String APIKey, String SecretKey) throws Exception{
        Map<String, String> header = new HashMap<>();
        long time = System.currentTimeMillis();
        String signStr = MD5Util.encrypt(APIKey + SecretKey + time);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apiKey", APIKey);
        jsonObject.put("time", time);
        jsonObject.put("sign", signStr);
        Base64 base64 = new Base64();
        String s = base64.encodeToString(jsonObject.toJSONString().getBytes("UTF-8"));
        header.put("header", s);
 
        jsonObject = new JSONObject();
        jsonObject.put("platformId", "");
        jsonObject.put("secret", "");
        s = base64.encodeToString(jsonObject.toJSONString().getBytes("UTF-8"));
        header.put("accessCode", s);
        return header;
    }
}