xuhy
2023-10-19 78851285202a040f8ddf998c067cd462874c1cb9
UserTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/ChinaMobileUtil.java
@@ -1,12 +1,12 @@
package com.stylefeng.guns.modular.system.util;
import com.alibaba.fastjson.JSON;
import com.aliyun.dyplsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.modular.system.util.httpClinet.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -16,98 +16,114 @@
@Component
public class ChinaMobileUtil {
    private String accessKeyId = "LTAI5tCeHnZXREQBcVf3NHLB";
    private String APIKey = "zj42494b1bdd416b9762229af6b5cbbd";
    private String accessKeySecret = "OEX6AtRb3qOCyp53xOFdDEYgBjzZzS";
    private String SecretKey = "30323561316534653735613230316339";
    private String poolKey = "FC100000145494178";//号码池KEY
    /**
     * 使用AK&SK初始化账号Client
     * @return Client
     * @throws Exception
     */
    public com.aliyun.dyplsapi20170525.Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dyplsapi.aliyuncs.com";
        return new com.aliyun.dyplsapi20170525.Client(config);
    }
    @Autowired
    private HttpClientUtil httpClientUtil;
    /**
     * 绑定隐私号
     * 绑定小号
     * @param phoneA
     * @param phoneB
     * @param expiration    "2021-12-14 12:00:00"
     * @param areaCode
     * @return
     * @throws Exception
     */
    public Map<String, String> midAxbBindSend(String phoneA, String phoneB, Long expiration) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        com.aliyun.dyplsapi20170525.Client client = createClient();
        BindAxbRequest bindAxbRequest = new BindAxbRequest()
                .setPhoneNoA(phoneA)
                .setExpiration(sdf.format(new Date(expiration)))
                .setPoolKey(poolKey)
                .setPhoneNoB(phoneB);
        // 复制代码运行请自行打印 API 的返回值
        BindAxbResponse bindAxbResponse = client.bindAxb(bindAxbRequest);
        Map<String, String> map = new HashMap<>();
        if(!bindAxbResponse.getBody().getCode().equals("OK")){
            map.put("code", "-1");
    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").getData();
        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{
            map.put("code", "200");
            map.put("telX", bindAxbResponse.getBody().getSecretBindDTO().getSecretNo());
            map.put("bindId", bindAxbResponse.getBody().getSecretBindDTO().getSubsId());
            map1.put("code", "-1");
            map1.put("msg", post);
        }
        return map;
        return map1;
    }
    /**
     * 修改绑定关系
     * 解绑小号关系
     * @param bindId    绑定关系id
     * @param phone     隐私号
     * @return
     * @throws Exception
     */
    public Map<String, String> midAxbUnBindSend(String bindId, String phone, Long expiration) throws Exception{
        com.aliyun.dyplsapi20170525.Client client = createClient();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        UpdateSubscriptionRequest updateSubscriptionRequest = new UpdateSubscriptionRequest()
                .setPoolKey(poolKey)
                .setSubsId(bindId)
                .setPhoneNoX(phone)
                .setOperateType("updateExpire")
                .setExpiration(sdf.format(new Date(expiration)));
        // 复制代码运行请自行打印 API 的返回值
        UpdateSubscriptionResponse updateSubscriptionResponse = client.updateSubscription(updateSubscriptionRequest);
        Map<String, String> map = new HashMap<>();
        if(!updateSubscriptionResponse.getBody().getCode().equals("OK")){
            map.put("code", "-1");
    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").getData();
        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{
            map.put("code", "200");
            map1.put("code", jsonObject.getString("code"));
            map1.put("msg", jsonObject.getString("message"));
        }
        return map;
        return map1;
    }
    public static void main(String[] ages){
        try {
            ChinaMobileUtil chinaMobileUtil = new ChinaMobileUtil();
            Map<String, String> map = chinaMobileUtil.midAxbBindSend("15828353127", "18382330577", System.currentTimeMillis() + 100000);
            System.err.println(JSON.toJSONString(map));
            chinaMobileUtil.midAxbUnBindSend(map.get("bindId"), map.get("telX"), System.currentTimeMillis() + 60000);
        }catch (Exception e){
            e.printStackTrace();
        }
    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;
    }
}