个人微信自动化发送信息提供基础接口示例(E云管家)

注意事项

手写的,如有不规范的地方或有优化的地方请多多指正
1.必须购买e云管家服务(有3天测试版)
2.效果是在ipad的远程登录,通过接口方式管理账号
3.可加微信私聊,互相指正(yy421438814)

post请求示例

private static RequestConfig requestConfig;
static {
 		requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(2000).build();
    }
public static JSONObject httpPost(String url, JSONObject jsonParam, String headerParam) {
        // post请求返回结果
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonResult = null;
        HttpPost httpPost = new HttpPost(url);
        // 设置请求和传输超时时间
        httpPost.setConfig(requestConfig);
        try {
            if (null != jsonParam) {
                // 解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            if (StringUtils.hasText(headerParam)){
                httpPost.setHeader("Authorization",headerParam);
            }
            CloseableHttpResponse result = httpClient.execute(httpPost);
            // 请求发送成功,并得到响应
            if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String str = "";
                try {
                    // 读取服务器返回过来的json字符串数据
                    str = EntityUtils.toString(result.getEntity(), "utf-8");
                    // 把json字符串转换成json对象
                    jsonResult = JSONObject.parseObject(str);
                } catch (Exception e) {
                    logger.error("post请求提交失败:" + url, e);
                }
            }
        } catch (IOException e) {
            logger.error("post请求提交失败:" + url, e);
        } finally {
            httpPost.releaseConnection();
        }
        return jsonResult;
    }

其他模块的通配

    /**
     * post请求传输json参数
     *
     * @param url  url地址
     * @param jsonParam 参数
     * @return
     */
    public static JSONObject httpPost(String url, JSONObject jsonParam) {
        return httpPost(url, jsonParam, null);
    }

全局路径

    public static final String CRM_URL = "购买后在aip信息里面查询";

登录接口

//获取登录token
 public  static JSONObject postResponse(String account, String password){
        JSONObject json = new JSONObject();
        json.put("account",account);
        json.put("password",password);
        return HttpClientUtils.httpPost(CRM_URL + "/member/login", json);
    }

获取二维码

  public static JSONObject iPadLogin(EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        json.put("wcId",edSchoolWx.getEdSchoolWxWcid());
        json.put("proxy","18"); //根据地区来适配官方文档有
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            System.out.println(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()).toString());
            JSONObject jsonObject = HttpClientUtils.httpPost(CRM_URL + "/iPadLogin",json,RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
            return jsonObject;
        }else {
            throw new BusinessException("缺少登录信息");
        }
    }

执行登录(确认登录)

    public static JSONObject getIPadLoginInfo(EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("请重新获取微信信息");
        }
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            //特殊超时时间
            JSONObject jsonObject = HttpClientUtils.httpPosts(CRM_URL + "/getIPadLoginInfo", json,RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
            return jsonObject;
        }else {
            throw new BusinessException("缺少登录信息");
        }
    }

初始化通讯录列表

 public static JSONObject initAddressList(EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("请重新获取微信信息");
        }
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            JSONObject jsonObject = HttpClientUtils.httpPost(CRM_URL + "/initAddressList", json,RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
            return jsonObject;
        }else {
            throw new BusinessException("缺少登录信息");
        }
    }

获取通讯录列表

public static  JSONObject getAddressList(EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("请重新获取微信信息");
        }
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            JSONObject jsonObject = HttpClientUtils.httpPost(CRM_URL + "/getAddressList", json,RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
            return jsonObject;
        }else {
            throw new BusinessException("缺少登录信息");
        }

    }

获取联系人信息

 public static List<CrmFriend> getContact(List<String> wcIds, EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("请重新获取微信信息");
        }
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            List<CrmFriend> data = new ArrayList<>();
            if (wcIds != null && !wcIds.isEmpty()){
                //一次最多获取20个联系人的数据
                List<List<String>> lists = SysUtils.averageAssign(wcIds, 20);
                lists.stream().forEach(itme ->{
                    StringBuffer resultBuffer = new StringBuffer();
                    for (int m=0;m<itme.size();m++){
                        String bondNo=itme.get(m);
                        if (m == 0) {
                            //只有一个值的时候输出
                            resultBuffer.append(bondNo);
                        }else{
                            //有多个值的时候分号分割
                            resultBuffer.append("," + bondNo);
                        }
                    }
                    String	testResult = resultBuffer.toString();
                    json.put("wcId",testResult);
                    try {
                        sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    JSONObject httpPost = HttpClientUtils.httpPost(CRM_URL + "/getContact", json,RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
                    //组装数据
                    if ("1000".equals(httpPost.get("code"))){
                        if (null != httpPost.get("data")){
                            List<CrmFriend> jsonArray = JSON.parseArray(httpPost.getString("data"),CrmFriend.class);
                            data.addAll(jsonArray);
                        }
                    }else {
                        throw new BusinessException("获取联系人列表失败");
                    }
                });
            }
            return data;
        }else {
            throw new BusinessException("缺少登录信息");
        }
    }

发送文本消息

 public static void sendText(List<String> wcIds,String content,EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("请重新获取微信信息");
        }
        json.put("content",content);
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            wcIds.stream().forEach(imte -> {
                if (json.containsKey("wcId")) {
                    json.remove("wcId");
                }
                json.put("wcId", imte);
                HttpClientUtils.httpPost(CRM_URL + "/sendText", json, RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
            });
        }
    }

发送图片消息

 public static  void sendImage(List<String> wcIds,String content,EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("请重新获取微信信息");
        }
        json.put("content",content);
        if (StringUtils.hasText(RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()))) {
            wcIds.stream().forEach(imte -> {
                if (json.containsKey("wcId")) {
                    json.remove("wcId");
                }
                json.put("wcId", imte);
                JSONObject jsonObject = HttpClientUtils.httpPost(CRM_URL + "/sendImage", json, RedisUtils.getCacheObject("school-" + edSchoolWx.getEdSchoolId()));
                System.out.println(jsonObject);
            });
        }
    }

判断是否登录

  public static  boolean isOnline(EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        if (StringUtils.hasText(edSchoolWx.getEdSchoolWxWid())){
            json.put("wId",edSchoolWx.getEdSchoolWxWid());
        }else {
            throw new BusinessException("当前登录信息有误");
        }
        if (StringUtils.hasText(RedisUtils.getCacheObject("school"+edSchoolWx.getEdSchoolId()))) {
            JSONObject jsonObject = HttpClientUtils.httpPost(CRM_URL + "/isOnline", json, RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
            JSONObject obj = (JSONObject) jsonObject.get("data");
            return (boolean) obj.get("isOnline");

        }else {
            return false;
        }
    }

退出登录

public static JSONObject offline(List<String> wcIds,EdSchoolWx edSchoolWx){
        JSONObject json = new JSONObject();
        json.put("wcIds",wcIds);
        return HttpClientUtils.httpPost(CRM_URL + "/member/offline", json, RedisUtils.getCacheObject("school-"+edSchoolWx.getEdSchoolId()));
    }

你可能感兴趣的:(微信,自动化,java)