企點第三方接口調用備忘

1.首先獲取accessToken(根據aid 和secret)
2.根據文檔中的url,發送對應請求到這個url,獲取返回結果
2.解析結果並封裝到自己代碼中的對象裏

public class QiDianCustInfoController {
    //獲取第400個之後的客戶基本信息
    public static void main(String[] args) {
       //這裏是調用其他服務中的寫好的生成accesstoken方法
        String result =HttpKit.get("http://172.16.2.221:8080/qidian/accessToken");
        JSONObject jsonObject = JSON.parseObject(result);
        String ACCESS_TOKEN = jsonObject.getString("data");      
       //先獲取本次要拉的所有客戶custId
        String nextCustId = "28521619600000000000000000000491";//自己的
        String getCustIdUrl = "https://api.qidian.qq.com/cgi-bin/cust/cust_info/getCustList?next_custid="+nextCustId+"&access_token="+ACCESS_TOKEN;
        String custListRest = HttpKit.get(getCustIdUrl);
        Map<String,Object> mapType = JSON.parseObject(custListRest, Map.class);
        System.out.println("mapType====="+mapType);
        Map<String,Object> map = (Map<String,Object>)mapType.get("data");
        List<String>  listcustIdList = (List<String>)map.get("cust_id");
        System.out.println("listcustIdList"+listcustIdList);

       List<CrmQidianCust> crmQidianCustList = new ArrayList<>();
       //開始循環
        for (int i = 0; i < listcustIdList.size(); i++) {
            CrmQidianCust crmQidianCust = new CrmQidianCust();
            String custId = listcustIdList.get(i).toString();
            String  getSingCustBaseIfoUrl = "https://api.qidian.qq.com/cgi-bin/cust/cust_info/getSingCustBaseInfo?access_token="+ACCESS_TOKEN;
            //頭信息
            Map<String, String> head = new HashMap<String, String>();
            head.put("accept", "*/*");
            head.put("connection", "Keep-Alive");
            head.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            head.put("Content-Type", "application/json;charset=utf8");

            Map<String,Object> mapOb = new HashMap<>();
            mapOb.put("cust_id",custId);
            List<String> lsData = new ArrayList<>();
            lsData.add("identity");
            lsData.add("contact");
            lsData.add("socialAccount");
            lsData.add("controlInfo");
            mapOb.put("data",lsData);
            JSONObject json = new JSONObject(mapOb);
            System.out.println("json===="+json);
            String jsonStr = JSON.toJSONString(json);

            String rlt = HttpKit.post(getSingCustBaseIfoUrl,jsonStr,head);
            System.out.println("rlt==="+rlt);
            Map<String,Object> mapType1 = JSON.parseObject(rlt, Map.class);
            Map<String,Object> mapType1Sub = (Map<String,Object>)mapType1.get("data");
            //客戶identity信息
            Map<String,Object> mapType1Identfy = (Map<String,Object>)mapType1Sub.get("identity");
            String name = mapType1Identfy.get("name").toString();
            System.out.println("name======"+name);
            crmQidianCust.setName(name);
            crmQidianCust.setUserid(custId);
            crmQidianCust.setGender(Integer.valueOf(mapType1Identfy.get("gender").toString()));
            if(mapType1Identfy.containsKey("location")){
                crmQidianCust.setAddress(mapType1Identfy.get("location").toString());
            }
            if(mapType1Identfy.containsKey("birthday")){
                crmQidianCust.setAddress(mapType1Identfy.get("birthday").toString());
            }
            if(mapType1Identfy.containsKey("education")){
                if(mapType1Identfy.get("education") != null){
                    crmQidianCust.setAddress(mapType1Identfy.get("education").toString());
                }
            }
            if(mapType1Identfy.containsKey("comment")){
                if(mapType1Identfy.get("comment") != null){
                    crmQidianCust.setAddress(mapType1Identfy.get("comment").toString());
                }
            }
            if(mapType1Identfy.containsKey("location")){
                if(mapType1Identfy.get("location") != null){
                    crmQidianCust.setAddress(mapType1Identfy.get("location").toString());
                }
            }
            //獲取職位信息
            List<JSONObject> titleList = (List<JSONObject>)mapType1Identfy.get("company");
            Map<String,Object> mapType2 = JSON.parseObject(titleList.get(0).toString(), Map.class);
           if(null != mapType2 && mapType2.containsKey("title")){
               crmQidianCust.setPosition(mapType2.get("title").toString());
           }
           // System.out.println(titleList.get(0));
            //客戶contact信息
            Map<String,Object> mapType1Contact = (Map<String,Object>)mapType1Sub.get("contact");
            if(mapType1Contact.containsKey("telephone")&& null != mapType1Contact.get("telephone")){
                crmQidianCust.setMobile(mapType1Contact.get("telephone").toString());
            }

            //telephoneInfo
            if(mapType1Contact.containsKey("telephoneInfo")&& null != mapType1Contact.get("telephoneInfo") ){
                String telephoneInfoSr = mapType1Contact.get("telephoneInfo").toString();
                if(telephoneInfoSr.contains("{")&&telephoneInfoSr.contains("}")){
                    int start = telephoneInfoSr.indexOf("{");
                    int end = telephoneInfoSr.indexOf("}");
                    String telephoneInfoSrt = telephoneInfoSr.substring(start,end+1);
                    JSONObject object = JSON.parseObject(telephoneInfoSrt);
                    System.out.println("object==="+object);
                    Map<String,Object> mapType3 = JSON.parseObject(object.toString(), Map.class);
                    long fid = Long.valueOf(mapType3.get("fid").toString());
                    crmQidianCust.setFid(fid);
                }
            }
            if(null != mapType1Contact.get("email")){                crmQidianCust.setEmail(mapType1Contact.get("email").toString());
            }
            //wxaccount
            if(null != mapType1Contact.get("wxaccount")){
                crmQidianCust.setUnionid(mapType1Contact.get("wxaccount").toString());
            }
            crmQidianCust.setEnable(1);
            crmQidianCust.setDeleteFlag(0);
            //controlInfo
            Map<String,Object> mapType1ControlInfo = (Map<String,Object>)mapType1Sub.get("controlInfo");
            if(mapType1ControlInfo.containsKey("createdTime")&&null != mapType1ControlInfo.get("createdTime")){
                //createdTime
                long  timeStamp = Long.valueOf(mapType1ControlInfo.get("createdTime").toString());

                long longTimeStamp = new Long(new Long(timeStamp) * 1000);
                Date date = new Date(longTimeStamp);
                System.out.println("date =========="+date);
                crmQidianCust.setCreateTime(date);
            }
            crmQidianCustList.add(crmQidianCust);
            System.out.println("crmQidianCust======"+crmQidianCust);
        }
        System.out.println("crmQidianCustList======"+crmQidianCustList);
    }
    private static String getStrInt(String result01) {
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(result01);
        String sub = m.replaceAll("").trim();
        return sub;
    }
    //Unicode轉中文
    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2,dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16進制parse整形字符串。   
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章