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();
}
}