package com.yeshi.fanli.util.aitaoker;
|
|
import java.io.UnsupportedEncodingException;
|
import java.lang.reflect.Type;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.yeshi.utils.HttpUtil;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.fanli.dto.aitaoker.QrcodeLoginDTO;
|
import com.yeshi.fanli.dto.aitaoker.RobotInfoDTO;
|
import com.yeshi.fanli.dto.aitaoker.WeiXinGroupDTO;
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class AitaokerApiUtil {
|
|
public static String APP_KEY = "1077080250";
|
public static String SECRET_KEY = "7c6118bd-7aa5-65b8-c6d4-058728e9446f";
|
|
// 请求连接
|
private static String SERVER_URL = "http://router.itaoke.org/api";
|
|
|
|
|
private static String baseRequest(String method, Map<String, String> param) {
|
Map<String, String> baseMap = new HashMap<>();
|
baseMap.put("app_key", APP_KEY);
|
baseMap.put("v", "1.0");
|
baseMap.put("format", "json");
|
baseMap.put("sign_method", "md5");
|
baseMap.put("timestamp", System.currentTimeMillis()+ "");
|
baseMap.put("method", method);
|
baseMap.put("domain", "hi.flqapp.com");
|
baseMap.put("client", "113.249.194.232");
|
baseMap.put("partner_id", "top-sdk-php-20190618");
|
String url = combinedUrl(baseMap);
|
|
if (param != null) {
|
Iterator<String> its = param.keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
baseMap.put(key, param.get(key));
|
}
|
}
|
|
url+= "sign=" + getSign(baseMap);
|
|
return HttpUtil.post(url, param);
|
}
|
|
/**
|
* 拼接所有系统参数包括sign的url
|
* @param params
|
* @return
|
*/
|
private static String combinedUrl (Map<String, String> params) {
|
String url = SERVER_URL + "?";
|
Iterator<String> its = params.keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
try {
|
url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
return url;
|
}
|
|
/**
|
* 获取签名
|
* @param params
|
* @return
|
*/
|
private static String getSign(Map<String, String> params) {
|
List<String> list = new ArrayList<>();
|
Iterator<String> its = params.keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
list.add(key + params.get(key));
|
}
|
Collections.sort(list);
|
|
String str = "";
|
for (String st : list) {
|
str += st;
|
}
|
return StringUtil.Md5(SECRET_KEY + str + SECRET_KEY).toUpperCase();
|
}
|
|
/**
|
* 获取登录二维码
|
* @param robotId
|
* @return
|
*/
|
public static QrcodeLoginDTO getQrcodeMaclogin(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.qrcode.maclogin", map);
|
|
JSONObject resultJson = JSONObject.fromObject(result);
|
resultJson = resultJson.optJSONObject("data");
|
if (resultJson != null) {
|
Type type = new TypeToken<QrcodeLoginDTO>() {}.getType();
|
return new Gson().fromJson(resultJson.toString(), type);
|
}
|
return null;
|
}
|
|
/**
|
* 检查是否扫码
|
* @param robotId
|
* @return
|
*/
|
public static Boolean getQrcodeStatus(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.qrcode.status", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
resultJson = resultJson.optJSONObject("data");
|
if (resultJson != null && !StringUtil.isNullOrEmpty(resultJson.optString("status"))) {
|
int optInt = resultJson.optInt("status");
|
if (optInt == 1) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
return null;
|
}
|
|
|
/**
|
* 检查是否登陆(真正的登录接口)
|
* @param robotId 机器人id
|
* @param wId 获取二维码接口返回的微信实例id
|
* @return
|
*/
|
public static QrcodeLoginDTO getQrcodeMacloginCheck(int robotId, String wId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("uuid", wId);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.check.maclogin", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
resultJson = resultJson.optJSONObject("data");
|
if (resultJson != null && !StringUtil.isNullOrEmpty(resultJson.optString("wcId"))) {
|
Type type = new TypeToken<QrcodeLoginDTO>() {}.getType();
|
return new Gson().fromJson(resultJson.toString(), type);
|
}
|
return null;
|
}
|
|
/**
|
* 检查是否在线
|
* @param robotId 机器人id
|
* @return
|
*/
|
public static boolean onlineCheck(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.check.online", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
return true;
|
}
|
return false;
|
}
|
|
|
/**
|
* 4.强制下线
|
* @param robotId 机器人id
|
* @return
|
*/
|
public static boolean macloginOffline(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.force.offline", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
return true;
|
}
|
return false;
|
}
|
|
|
/**
|
* 掉线重连
|
* @param robotId 机器人id
|
* @return
|
*/
|
public static boolean loseReconnet(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.lose.reconnet", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 创建机器人
|
* @param month 月数
|
* @param robotType 机器人类型 1 发单机器人 2转发机器人 3 返利机器人 4全能机器人 5小型机器人 6发圈机器人
|
* @param wechatrobot 微信号
|
* @param agentUid 代理id
|
* @return
|
*/
|
public static RobotInfoDTO robotCreate(int month , int robotType, String wechatrobot, String agentUid) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("month", month +"");
|
map.put("robot_type", robotType + "");
|
map.put("wechatrobot", wechatrobot);
|
if(!StringUtil.isNullOrEmpty(agentUid))
|
map.put("agent_uid", agentUid);
|
|
// 请求结果
|
String result = baseRequest("itaoke.robot.create.get", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
resultJson = resultJson.optJSONObject("data");
|
Type type = new TypeToken<RobotInfoDTO>() {}.getType();
|
return new Gson().fromJson(resultJson.toString(), type);
|
}
|
return null;
|
}
|
|
|
|
/**
|
* 机器人更换微信号
|
* @param robotId 机器人id
|
* @param wxid 微信号
|
* @return
|
*/
|
public static RobotInfoDTO robotChangeWeiXin(int robotId, String wxid) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("wechatrobot", wxid);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.change.get", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
resultJson = resultJson.optJSONObject("data");
|
Type type = new TypeToken<RobotInfoDTO>() {}.getType();
|
return new Gson().fromJson(resultJson.toString(), type);
|
}
|
return null;
|
}
|
|
|
/**
|
* 机器人续费
|
* @param robotId 机器人id
|
* @param wxid 微信号
|
* @return
|
*/
|
public static RobotInfoDTO robotRenewals(int robotId, int month) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("month", month +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.change.get", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
resultJson = resultJson.optJSONObject("data");
|
Type type = new TypeToken<RobotInfoDTO>() {}.getType();
|
return new Gson().fromJson(resultJson.toString(), type);
|
}
|
return null;
|
}
|
|
|
/**
|
* 删除机器人
|
* @param robotId
|
* @return
|
*/
|
public static boolean robotDelete(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.delete.get", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
return true;
|
}
|
return false;
|
}
|
|
|
/**
|
* 获取好友-群列表
|
* @param robotId
|
* @return
|
*/
|
public static List<WeiXinGroupDTO> getContract(int robotId) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
// 请求结果
|
String result = baseRequest("itaoke.robot.get.contract", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("0000".equals(resultJson.optString("code"))) {
|
resultJson = resultJson.optJSONObject("data");
|
// 订阅号
|
//JSONArray publicArray = resultJson.optJSONArray("public");
|
|
// 好友列表
|
//JSONArray friendArray = resultJson.optJSONArray("friend");
|
|
// 群列表
|
JSONArray groupArray = resultJson.optJSONArray("group");
|
if (groupArray != null && groupArray.size() > 0) {
|
List<WeiXinGroupDTO> listGroup = new ArrayList<>();
|
Type type = new TypeToken<RobotInfoDTO>() {}.getType();
|
Gson gson = new Gson();
|
for (int i = 0 ;i < groupArray.size(); i ++) {
|
listGroup.add(gson.fromJson(groupArray.get(i).toString(), type));
|
}
|
}
|
}
|
return null;
|
}
|
|
|
/**
|
* 发朋友圈
|
* @param robotId
|
* @param content
|
* @param picUrl 图片url,多个请用;分隔
|
* @return
|
*/
|
public static String macsendCircle(int robotId, String content, String picUrl) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("pic_url", picUrl);
|
map.put("content", content);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.macsend.circle", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("1000".equals(resultJson.optString("code"))) {
|
resultJson = resultJson.optJSONObject("data");
|
return resultJson.optString("id");
|
}
|
return null;
|
}
|
|
|
/**
|
* 朋友圈发送视频
|
* @param robotId
|
* @param videoPath 视频地址
|
* @param thumbPath 封面url
|
* @return
|
*/
|
public static String macsendCircleVideo(int robotId, String videoPath, String thumbPath) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("video_path", videoPath);
|
map.put("thumb_path", thumbPath);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.macsend.videocircle", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("1000".equals(resultJson.optString("code"))) {
|
resultJson = resultJson.optJSONObject("data");
|
return resultJson.optString("id");
|
}
|
return null;
|
}
|
|
|
/**
|
* 朋友圈发送视频
|
* @param robotId
|
* @param videoPath 视频地址
|
* @param thumbPath 封面url
|
* @return
|
*/
|
public static boolean macsendCircleComment(int robotId, String wxId, String msgId, String content) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("wx_id", wxId);
|
map.put("msg_id", msgId);
|
map.put("content", content);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.macsend.circlecomment", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if ("1000".equals(resultJson.optString("code"))) {
|
return true;
|
}
|
return false;
|
}
|
|
|
|
/**
|
* 发文本消息
|
* @param robotId
|
* @param wxId 微信群ID
|
* @param content 内容
|
* @return
|
*/
|
public static boolean macsendText(int robotId, String toWxId, String content) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("toWxId", toWxId );
|
map.put("content", content);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.macsend.text", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if (resultJson.optInt("code") == 1000) {
|
return true;
|
}
|
return false;
|
}
|
|
|
|
/**
|
* 发base64图
|
* @param robotId
|
* @param wxId
|
* @param imgBase64
|
* @return
|
*/
|
public static boolean macsendImgBase64(int robotId, String toWxId, String imgBase64) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("toWxId", toWxId );
|
map.put("base64_data", imgBase64);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.macsend.base64", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if (resultJson.optInt("code") == 1000) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 发链接消息
|
* @param robotId
|
* @param wxId 微信群ID
|
* @param title 标题
|
* @param url 链接
|
* @param description 描述
|
* @param thumbUrl 图片
|
* @return
|
*/
|
public static boolean macsendCard(int robotId, String wxId, String title, String url,
|
String description ,String thumbUrl) {
|
// 请求参数
|
Map<String, String> map = new HashMap<>();
|
map.put("robot_id", robotId +"");
|
map.put("wx_id", wxId);
|
map.put("title", title);
|
map.put("url", url);
|
map.put("description", description);
|
map.put("thumbUrl", thumbUrl);
|
// 请求结果
|
String result = baseRequest("itaoke.robot.macsend.card", map);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if (resultJson.optInt("code") == 1000) {
|
return true;
|
}
|
return false;
|
}
|
|
|
|
}
|