package com.dudian.mall.http;
import com.alibaba.fastjson.JSON;
import com.dudian.base.ResponCode;
import com.dudian.constant.ImConfigConstant;
import com.dudian.enums.ImMessageTypeEnum;
import com.dudian.utils.RandomNoStr;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.*;
/**
* IM请求接口
*/
public class ImHttpRequest implements Serializable {
final static Logger log = LoggerFactory.getLogger(ImHttpRequest.class.getName());
/**
*导入单个帐号
* @param userId 导入的用户
* @param nickName 呢称
* @param faceUrl
* @return
*/
public static ResponCode accountImport (String userId, String nickName,String faceUrl){
Long sdkappid =Long.parseLong(ImConfigConstant.SDKAPPID);
String identifier=ImConfigConstant.identifier;
String random= RandomNoStr.getRandom();
/*TLSSigAPIv2 api = new TLSSigAPIv2(1400407378, "88d2eda154a3cf741f106ea77ebb82bb341e57d4da2d34810cd4796e9fcba95d");
System.out.print(api.genSig("C003", 180*86400));*/
//签名
TLSSigAPIv2 api = new TLSSigAPIv2(Long.parseLong(ImConfigConstant.SDKAPPID), ImConfigConstant.PASSWORD);
String userSign=api.genSig(identifier, Long.parseLong(ImConfigConstant.expire));
String url="https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid="+sdkappid+"&identifier="+identifier+"&usersig="+userSign+"&random="+random+"&contenttype=json";
JSONObject jsonObject=new JSONObject();
jsonObject.put("Identifier",userId);
jsonObject.put("Nick",nickName);
jsonObject.put("FaceUrl",faceUrl);
log.info("accountImport: url-->"+url+",jsonObject-->"+ JSON.toJSON(jsonObject));
String response=send(url,jsonObject);
log.info("accountImport response:-->"+response);
ResponCode code=new ResponCode();
JSONObject js =jsonObject.fromObject(response);
if(js.getInt("ErrorCode") == 0){
code.setActionStatus(js.get("ActionStatus").toString());
code.setErrorCode(js.getInt("ErrorCode"));
code.setErrorInfo(js.get("ErrorInfo").toString());
TLSSigAPIv2 accountApi = new TLSSigAPIv2(Long.parseLong(ImConfigConstant.SDKAPPID), ImConfigConstant.PASSWORD);
String accountSign=accountApi.genSig(userId, Long.parseLong(ImConfigConstant.expire));
code.setUserSign(accountSign);
code.setAccount(userId);
return code;
}else{
return null;
}
}
/**
* 用于查询自有帐号是否已导入即时通信 IM
* @param listmap
* @return
*/
public static ResponCode accountCheck(List<Map> listmap){
Long sdkappid =Long.parseLong(ImConfigConstant.SDKAPPID);
String identifier=ImConfigConstant.identifier;
/* Long sdkappid =1400407378L;
String identifier="admin";*/
String random=RandomNoStr.getRandom();
//签名
String password=ImConfigConstant.PASSWORD;
// String password="88d2eda154a3cf741f106ea77ebb82bb341e57d4da2d34810cd4796e9fcba95d";
String expire=ImConfigConstant.expire;
//String expire="15552000";
TLSSigAPIv2 api = new TLSSigAPIv2(sdkappid, password);
String userSign=api.genSigWithUserBuf(identifier, Long.parseLong(expire), null);
String url="https://console.tim.qq.com/v4/im_open_login_svc/account_check?sdkappid="+sdkappid+"&identifier="+identifier+"&usersig="+userSign+"&random="+random+"&contenttype=json";
JSONObject jsonObject=new JSONObject();
jsonObject.put("CheckItem",listmap);
log.info("accountCheck: url-->"+url+",jsonObject-->"+ JSON.toJSON(jsonObject));
String response=send(url,jsonObject);
log.info("accountCheck response:-->"+response);
if(response == null){
return null;
}
ResponCode code=new ResponCode();
JSONObject js =jsonObject.fromObject(response);
if(js.getInt("ErrorCode") == 0){
code.setActionStatus(js.get("ActionStatus").toString());
code.setErrorCode(js.getInt("ErrorCode"));
code.setErrorInfo(js.get("ErrorInfo").toString());
JSONArray ay= new JSONArray(js.get("ResultItem").toString());
String userId=ay.getJSONObject(0).get("UserID").toString();
String AccountStatus=ay.getJSONObject(0).get("AccountStatus").toString();
Map map=new HashMap();
map.put("resUserId",userId);
map.put("resAccountStatus",AccountStatus);
code.setResultItem(map);
code.setUserSign(userSign);
code.setAccount(userId);
return code;
}else{
return null;
}
}
/**
* 查询帐号在线状态
*/
public static ResponCode querystate(String[] account){
Long sdkappid =Long.parseLong(ImConfigConstant.SDKAPPID);
String identifier=ImConfigConstant.identifier;
String random=RandomNoStr.getRandom();
//签名
TLSSigAPIv2 api = new TLSSigAPIv2(Long.parseLong(ImConfigConstant.SDKAPPID), ImConfigConstant.PASSWORD);
String userSign=api.genSigWithUserBuf(ImConfigConstant.identifier, Long.parseLong(ImConfigConstant.expire), null);
String url="https://console.tim.qq.com/v4/openim/querystate?sdkappid="+sdkappid+"&identifier="+identifier+"&usersig="+userSign+"&random="+random+"&contenttype=json";
JSONObject jsonObject=new JSONObject();
jsonObject.put("To_Account",account);//
jsonObject.put("IsNeedDetail",0);
log.info("querystate: url-->"+url+",jsonObject-->"+ JSON.toJSON(jsonObject));
String response=send(url,jsonObject);
log.info("querystate response:-->"+response);
ResponCode code=new ResponCode();
JSONObject js =jsonObject.fromObject(response);
code.setActionStatus(js.getString("ActionStatus"));
code.setErrorCode(js.getInt("ErrorCode"));
code.setErrorInfo(js.getString("ErrorInfo"));
code.setQueryResult(js.getString("QueryResult"));
return code;
}
/**
* 发单聊消息之前回调
* @param fromAccount 消息发送者 UserID
* @param toAccount 消息接收者 UserID
* @param msgType "TIMTextElem", // 文本
* @param clientIp
* @param optplatform
* @param Text
* @return
*/
public static ResponCode beforSendMessageCallBack(String fromAccount,String toAccount,String msgType,String Text,String clientIp,String optplatform){
Long sdkappid =Long.parseLong(ImConfigConstant.SDKAPPID);
String random=RandomNoStr.getRandom();
String seqdom=RandomNoStr.getRandom();
/* 客户端平台,对应不同的平台类型,可能的取值有:
RESTAPI(使用 REST API 发送请求)、Web(使用 Web SDK 发送请求)、
Android、iOS、Windows、Mac、Unkown(使用未知类型的设备发送请求)*/
String url="https://www.example.com?SdkAppid="+sdkappid+"&CallbackCommand=C2C.CallbackBeforeSendMsg&contenttype=json&ClientIP="+clientIp+"&OptPlatform="+optplatform;
JSONObject jsonObject=new JSONObject();