企业微信接口封装代码-部分

private final static long access_token_time_step = 90 * 60 * 1000;
    private static String access_token;
    private static long access_token_time;

    public static String getToken() {
        String token = access_token;
        if (System.currentTimeMillis() - access_token_time > access_token_time_step) {
            String query = "corpid=" + corpid + "&corpsecret=" + corpsecret;
            String chat_token = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
            RestTemplate rest = new RestTemplate();
            JSONObject result = rest.getForObject(chat_token + "?" + query, JSONObject.class);
            if (result.getInt("errcode") == 0) {
                token = result.getStr("access_token");
                access_token = token;
                access_token_time = System.currentTimeMillis();
            } else {
                throw new ApiException("TencChat.getToken error:" + result.getStr("errmsg"));
            }
        }
        return token;
    }

    /**
     * @param chatid
     * @param name
     * @param owner
     * @param userlist
     * @return
     */
    public static String createChat(String chatid, String name, String owner, List<String> userlist) {
        if (StringUtils.isEmpty(name)) {
            throw new ApiException("TencChat.createChat error: name is null");
        }
        if (StringUtils.isEmpty(chatid)) {
            throw new ApiException("TencChat.createChat error: chatid is null");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject body = new JSONObject();
        body.set("name", name);
        if (StringUtils.isNotEmpty(owner)) {
            body.set("owner", owner);
        }
        body.set("userlist", userlist);
        body.set("chatid", chatid);
        HttpEntity<String> entity = new HttpEntity<String>(body.toString(), headers);
        String chat_create = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create";
        String query = "access_token=" + getToken();
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.postForObject(chat_create + "?" + query, entity, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return result.getStr("chatid");
        } else {
            throw new ApiException("TencChat.createChat error:" + result.getStr("errmsg"));
        }
    }

    public static Boolean updateChat(String chatid, String name, String owner, List<String> add_user_list, List<String> del_user_list) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject body = new JSONObject();
        body.set("chatid", chatid);
        if (StringUtils.isNotEmpty(name)) {
            body.set("name", name);
        }
        if (StringUtils.isNotEmpty(owner)) {
            body.set("owner", owner);
        }
        if (add_user_list != null && add_user_list.size() > 0) {
            body.set("add_user_list", add_user_list);
        }
        if (del_user_list != null && del_user_list.size() > 0) {
            body.set("del_user_list", del_user_list);
        }
        HttpEntity<String> entity = new HttpEntity<String>(body.toString(), headers);
        String query = "access_token=" + getToken();
        String chat_update = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update";
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.postForObject(chat_update + "?" + query, entity, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return true;
        } else {
            throw new ApiException("TencChat.updateChat error:" + result.getStr("errmsg"));
        }
    }

    public static JSONObject getChat(String chatid) {
        String query = "access_token=" + getToken() + "&chatid=" + chatid;
        String chat_get = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get";
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.getForObject(chat_get + "?" + query, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return result.getJSONObject("chat_info");
        } else {
            throw new ApiException("TencChat.getChat error:" + result.getStr("errmsg"));
        }
    }


    public static boolean sendText(String chatid, String content) {
        JSONObject text = new JSONObject();
        text.set("content", content);
        return send(chatid, "text", text);
    }


    public static boolean send(String chatid, String msgtype, JSONObject object) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject body = new JSONObject();
        body.set("chatid", chatid);
        body.set("msgtype", msgtype);
        body.set(msgtype, object);
        body.set("safe", 1);
        HttpEntity<String> entity = new HttpEntity<String>(body.toString(), headers);
        String query = "access_token=" + getToken();
        String chat_send = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send";
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.postForObject(chat_send + "?" + query, entity, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return true;
        } else {
            throw new ApiException("TencChat.chatSendText error:" + result.getStr("errmsg"));
        }
    }

    /**
     * @param chatid  群聊ID
     * @param content 发送文本内容
     * @return
     */
    public static JSONObject sendTextByRobot(String chatid, String content) {
        JSONObject text = new JSONObject();
        text.set("content", content);
        //text.set("mentioned_list", new String[]{"@all"});
        return sendByRobot(chatid, "text", text);
    }

    /**
     * @param chatid 群聊ID
     * @param obj    发送内容
     * @return
     */
    public static JSONObject sendByRobot(String chatid, String msgtype, JSONObject obj) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject body = new JSONObject();
        body.set("msgtype", msgtype);
        body.set(msgtype, obj);
        HttpEntity<String> entity = new HttpEntity<String>(body.toString(), headers);
        String url = robotMap.get(chatid);
        RestTemplate rest = new RestTemplate();
        return rest.postForObject(url, entity, JSONObject.class);
    }

    /**
     * 获取全量部门列表
     *
     * @return
     */
    public static JSONArray getDepartmentList() {
        String query = "access_token=" + getToken();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.getForObject(url + "?" + query, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return result.getJSONArray("department");
        } else {
            throw new ApiException("TencChat.getDepartmentList error:" + result.getStr("errmsg"));
        }
    }

    /**
     * 获取全量部门ID列表
     *
     * @return
     */
    public static JSONArray getDepartmentIdList() {
        String query = "access_token=" + getToken();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist";
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.getForObject(url + "?" + query, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return result.getJSONArray("department_id");
        } else {
            throw new ApiException("TencChat.getDepartmentIdList error:" + result.getStr("errmsg"));
        }
    }

    /**
     * 获取部门成员
     *
     * @return
     */
    public static JSONArray getDepartmentMemberList(String department_id) {
        String query = "access_token=" + getToken() + "&department_id=" + department_id;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
        RestTemplate rest = new RestTemplate();
        JSONObject result = rest.getForObject(url + "?" + query, JSONObject.class);
        if (result.getInt("errcode") == 0) {
            return result.getJSONArray("userlist");
        } else {
            throw new ApiException("TencChat.getDepartmentMemberList error:" + result.getStr("errmsg"));
        }
    }

    /**
     * 获取全量成员D
     *
     * @return
     */
    public static JSONObject getMemberIdList(String cursor) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject body = new JSONObject();
        if (StringUtils.isNotEmpty(cursor)) {
            body.set("cursor", cursor);
        }
        body.set("limit", 300);
        HttpEntity<String> entity = new HttpEntity<>(body.toString(), headers);
        String query = "access_token=" + getToken();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list_id";
        RestTemplate rest = new RestTemplate();
        return rest.postForObject(url + "?" + query, entity, JSONObject.class);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值