admin
2024-10-16 62a447d89331aee1feae7724c7616aa1bb2cfe79
fanli/src/main/java/com/yeshi/fanli/util/aitaoker/AitaokerApiUtil.java
@@ -1,489 +1,605 @@
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 机器人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.async.mlogin", map);
      JSONObject resultJson = JSONObject.fromObject(result);
      if ("0000".equals(resultJson.optString("status"))) {
         resultJson = resultJson.optJSONObject("data");
         if (resultJson != null) {
            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("status"))) {
         resultJson = resultJson.optJSONObject("data");
         if (resultJson != null) {
            return resultJson.optBoolean("isOnline");
         }
      }
      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("status"))) {
         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);
//      String result = "{\r\n" +
//            "    \"status\":\"0000\",\r\n" +
//            "    \"msg\":\"亲,添加成功\",\r\n" +
//            "    \"data\":{\r\n" +
//            "        \"id\":11770,\r\n" +
//            "        \"uid\":1625,\r\n" +
//            "        \"wechatrobot\":\"wechatrobot\",\r\n" +
//            "        \"wx_id\":\"\",\r\n" +
//            "        \"amount_used\":0,\r\n" +
//            "        \"group_num\":20,\r\n" +
//            "        \"passwd\":\"\",\r\n" +
//            "        \"nickname\":\"\",\r\n" +
//            "        \"c_uid\":1625,\r\n" +
//            "        \"login_status\":0,\r\n" +
//            "        \"end_time\":1594785534,\r\n" +
//            "        \"remark\":null,\r\n" +
//            "        \"wc_id\":\"\",\r\n" +
//            "        \"agent_uid\":null,\r\n" +
//            "        \"is_enabled\":0,\r\n" +
//            "        \"robot_type\":4,\r\n" +
//            "        \"ip\":\"http://49.234.36.129:10002/\",\r\n" +
//            "        \"is_new\":1\r\n" +
//            "    }\r\n" +
//            "}\r\n" +
//            "";
      JSONObject resultJson = JSONObject.fromObject(result);
      if ("0000".equals(resultJson.optString("status"))) {
         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);
//      String result = "{\r\n" +
//            "    \"status\":\"0000\",\r\n" +
//            "    \"msg\":\"\\u4eb2,\\u6dfb\\u52a0\\u6210\\u529f\",\r\n" +
//            "    \"data\":{\r\n" +
//            "        \"id\":11770,\r\n" +
//            "        \"uid\":1625,\r\n" +
//            "        \"wechatrobot\":\"fxdaka\",\r\n" +
//            "        \"wx_id\":\"wxid_vm6eb5i0in0622\",\r\n" +
//            "        \"amount_used\":0,\r\n" +
//            "        \"group_num\":20,\r\n" +
//            "        \"passwd\":\"\",\r\n" +
//            "        \"nickname\":\"\\u5206\\u4eab\\u5927\\u5496??\",\r\n" +
//            "        \"c_uid\":1625,\r\n" +
//            "        \"login_status\":1,\r\n" +
//            "        \"end_time\":1597377534,\r\n" +
//            "        \"remark\":null,\r\n" +
//            "        \"wc_id\":\"246124e9-9a8c-4da6-a77e-7f7f88049731\",\r\n" +
//            "        \"agent_uid\":null,\r\n" +
//            "        \"is_enabled\":0,\r\n" +
//            "        \"robot_type\":4,\r\n" +
//            "        \"ip\":\"http:\\/\\/49.234.36.129:10002\\/\",\r\n" +
//            "        \"is_new\":1\r\n" +
//            "    }\r\n" +
//            "}";
      JSONObject resultJson = JSONObject.fromObject(result);
      if ("0000".equals(resultJson.optString("status"))) {
         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("status"))) {
         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.room.list", map);
      JSONObject resultJson = JSONObject.fromObject(result);
      if ("0000".equals(resultJson.optString("status"))) {
         JSONArray groupArray = resultJson.optJSONArray("data");
         if (groupArray != null && groupArray.size() > 0) {
            Type type = new TypeToken<ArrayList<WeiXinGroupDTO>>() {
            }.getType();
            List<WeiXinGroupDTO> goodsList = new Gson().fromJson(groupArray.toString(), type);
            return goodsList;
         }
      }
      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 ("0000".equals(resultJson.optString("status"))) {
         resultJson = resultJson.optJSONObject("data");
         if (resultJson != null) {
            resultJson = resultJson.optJSONObject("object");
            if (resultJson != null) {
               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 ("0000".equals(resultJson.optString("status"))) {
         resultJson = resultJson.optJSONObject("data");
         if (resultJson != null) {
            resultJson = resultJson.optJSONObject("object");
            if (resultJson != null) {
               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);
      map.put("comment_id", "0");
      // 请求结果
      String result = baseRequest("itaoke.robot.macsend.circlecomment", map);
      JSONObject resultJson = JSONObject.fromObject(result);
      if ("0000".equals(resultJson.optString("status"))) {
         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 ("0000".equals(resultJson.optString("status"))) {
         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 ("0000".equals(resultJson.optString("status"))) {
         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 ("0000".equals(resultJson.optString("status"))) {
         return true;
      }
      return false;
   }
}
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 com.yeshi.fanli.log.LogHelper;
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);
        String result = HttpUtil.post(url, param);
        LogHelper.cloudInfo(method + ":" + result);
        return result;
    }
    /**
     * 拼接所有系统参数包括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 机器人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.async.mlogin", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            resultJson = resultJson.optJSONObject("data");
            if (resultJson != null) {
                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("status"))) {
            return true;
//            resultJson = resultJson.optJSONObject("data");
//            if (resultJson != null) {
//                return resultJson.optBoolean("isOnline");
//            }
        }
        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("status"))) {
            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);
//      String result = "{\r\n" +
//            "    \"status\":\"0000\",\r\n" +
//            "    \"msg\":\"亲,添加成功\",\r\n" +
//            "    \"data\":{\r\n" +
//            "        \"id\":11770,\r\n" +
//            "        \"uid\":1625,\r\n" +
//            "        \"wechatrobot\":\"wechatrobot\",\r\n" +
//            "        \"wx_id\":\"\",\r\n" +
//            "        \"amount_used\":0,\r\n" +
//            "        \"group_num\":20,\r\n" +
//            "        \"passwd\":\"\",\r\n" +
//            "        \"nickname\":\"\",\r\n" +
//            "        \"c_uid\":1625,\r\n" +
//            "        \"login_status\":0,\r\n" +
//            "        \"end_time\":1594785534,\r\n" +
//            "        \"remark\":null,\r\n" +
//            "        \"wc_id\":\"\",\r\n" +
//            "        \"agent_uid\":null,\r\n" +
//            "        \"is_enabled\":0,\r\n" +
//            "        \"robot_type\":4,\r\n" +
//            "        \"ip\":\"http://49.234.36.129:10002/\",\r\n" +
//            "        \"is_new\":1\r\n" +
//            "    }\r\n" +
//            "}\r\n" +
//            "";
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            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);
//      String result = "{\r\n" +
//            "    \"status\":\"0000\",\r\n" +
//            "    \"msg\":\"\\u4eb2,\\u6dfb\\u52a0\\u6210\\u529f\",\r\n" +
//            "    \"data\":{\r\n" +
//            "        \"id\":11770,\r\n" +
//            "        \"uid\":1625,\r\n" +
//            "        \"wechatrobot\":\"fxdaka\",\r\n" +
//            "        \"wx_id\":\"wxid_vm6eb5i0in0622\",\r\n" +
//            "        \"amount_used\":0,\r\n" +
//            "        \"group_num\":20,\r\n" +
//            "        \"passwd\":\"\",\r\n" +
//            "        \"nickname\":\"\\u5206\\u4eab\\u5927\\u5496??\",\r\n" +
//            "        \"c_uid\":1625,\r\n" +
//            "        \"login_status\":1,\r\n" +
//            "        \"end_time\":1597377534,\r\n" +
//            "        \"remark\":null,\r\n" +
//            "        \"wc_id\":\"246124e9-9a8c-4da6-a77e-7f7f88049731\",\r\n" +
//            "        \"agent_uid\":null,\r\n" +
//            "        \"is_enabled\":0,\r\n" +
//            "        \"robot_type\":4,\r\n" +
//            "        \"ip\":\"http:\\/\\/49.234.36.129:10002\\/\",\r\n" +
//            "        \"is_new\":1\r\n" +
//            "    }\r\n" +
//            "}";
//
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            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("status"))) {
            return true;
        }
        return false;
    }
    /**
     * 获取群列表
     *
     * @param robotId
     * @return
     */
    public static List<String> getContract(int robotId) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId + "");
        // 请求结果
        String result = baseRequest("itaoke.robot.room.list", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            JSONArray groupArray = resultJson.optJSONArray("data");
            if (groupArray != null && groupArray.size() > 0) {
                Type type = new TypeToken<ArrayList<String>>() {
                }.getType();
                List<String> list = new Gson().fromJson(groupArray.toString(), type);
                return list;
            }
        }
        return null;
    }
    /**
     * 获取群列表
     *
     * @param robotId
     * @param roomId
     * @return
     */
    public static WeiXinGroupDTO getGroupDetail(int robotId, String roomId) {
        if (StringUtil.isNullOrEmpty(roomId)) {
            return null;
        }
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId + "");
        map.put("room_id", roomId + ""); //  群的id ,多个用逗号隔开
        // 请求结果
        String result = baseRequest("itaoke.robot.room.detail", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            JSONArray groupArray = resultJson.optJSONArray("data");
            if (groupArray != null && groupArray.size() > 0) {
                Type type = new TypeToken<ArrayList<WeiXinGroupDTO>>() {
                }.getType();
                List<WeiXinGroupDTO> goodsList = new Gson().fromJson(groupArray.toString(), type);
                if (goodsList == null || goodsList.size() == 0) {
                    return null;
                } else {
                    return goodsList.get(0);
                }
            }
        }
        return null;
    }
    /**
     * 获取群列表
     *
     * @param robotId
     * @param roomId
     * @return
     */
    public static List<WeiXinGroupDTO> getGroupDetail(int robotId, List<String> listRoomId) {
        if (listRoomId == null || listRoomId.size() == 0) {
            return null;
        }
        String roomIds = "";
        for (String roomID : listRoomId) {
            roomIds += roomID + ",";
        }
        if (roomIds.endsWith(","))
            roomIds = roomIds.substring(0, roomIds.length() - 1);
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId + "");
        map.put("room_id", roomIds); //  群的id ,多个用逗号隔开
        // 请求结果
        String result = baseRequest("itaoke.robot.room.detail", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            JSONArray groupArray = resultJson.optJSONArray("data");
            if (groupArray != null && groupArray.size() > 0) {
                Type type = new TypeToken<ArrayList<WeiXinGroupDTO>>() {
                }.getType();
                List<WeiXinGroupDTO> goodsList = new Gson().fromJson(groupArray.toString(), type);
                return goodsList;
            }
        }
        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.circle.send", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            resultJson = resultJson.optJSONObject("data");
            if (resultJson != null) {
                resultJson = resultJson.optJSONObject("object");
                if (resultJson != null) {
                    return resultJson.optString("id");
                }
            }
        }
        return null;
    }
    /**
     * 发朋友圈
     *
     * @param robotId
     * @param content
     * @param picUrl  图片url,多个请用;分隔
     * @return
     */
    public static String macsendUpload(int robotId, String picUrl) {
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId + "");
        map.put("pic_url", picUrl);
        map.put("type", "2");
        String result = baseRequest("itaoke.robot.circle.upload", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            return resultJson.optString("data");
        }
        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 ("0000".equals(resultJson.optString("status"))) {
            resultJson = resultJson.optJSONObject("data");
            if (resultJson != null) {
                resultJson = resultJson.optJSONObject("object");
                if (resultJson != null) {
                    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);
        map.put("comment_id", "0");
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.circlecomment", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            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 ("0000".equals(resultJson.optString("status"))) {
            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 ("0000".equals(resultJson.optString("status"))) {
            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 ("0000".equals(resultJson.optString("status"))) {
            return true;
        }
        return false;
    }
}