/** * 设置用户备注名 * * @param openid 用户openid * @param remark 新的备注名,长度必须小于30字符 * @return * @throws WeChatException */ public void updateRemark(String openId, String remark) throws WeChatException { JSONObject jsonObject = new JSONObject(); jsonObject.put("openid", openId); jsonObject.put("remark", remark); String requestData = jsonObject.toString(); logger.info("request data " + requestData); String resultStr = HttpUtils.post(USER_UPDATE_REMARK_POST_URL + this.accessToken, requestData); logger.info("return data " + resultStr); WeChatUtil.isSuccess(resultStr); }
/** * 删除分组 * * @param groupId * @throws WeChatException */ public void deleteGroup(int groupId) throws WeChatException { JSONObject idJson = new JSONObject(); idJson.put("id", groupId); JSONObject groupJson = new JSONObject(); groupJson.put("group", idJson); String requestData = groupJson.toJSONString(); logger.info("request data " + requestData); String resultStr = HttpUtils.post(GROUP_DELETE_POST_URL + this.accessToken, requestData); logger.info("return data " + resultStr); WeChatUtil.isSuccess(resultStr); }
/** * 批量移动用户分组 * * @param openids 用户唯一标识符openid的列表(size不能超过50) * @param toGroupid 分组id * @return 是否修改成功 * @throws WeChatException */ public void membersDatchUpdateGroup(String[] openIds, int groupId) throws WeChatException { JSONObject jsonObject = new JSONObject(); jsonObject.put("openid_list", openIds); jsonObject.put("to_groupid", groupId); String requestData = jsonObject.toString(); logger.info("request data " + requestData); String resultStr = HttpUtils.post(GROUP_MEMBERS_DATCHUPDATE_POST_URL + this.accessToken, requestData); logger.info("return data " + resultStr); WeChatUtil.isSuccess(resultStr); }
/** * 修改分组名 * * @param groupId 分组id * @param name 分组名称 * @throws WeChatException */ public void updateGroup(int groupId, String name) throws WeChatException { JSONObject nameJson = new JSONObject(); JSONObject groupJson = new JSONObject(); nameJson.put("id", groupId); nameJson.put("name", name); groupJson.put("group", nameJson); String requestData = groupJson.toString(); logger.info("request data " + requestData); String resultStr = HttpUtils.post(GROUP_UPDATE_POST_URL + this.accessToken, requestData); logger.info("return data " + resultStr); WeChatUtil.isSuccess(resultStr); }
/** * 创建分组 * * @param name 分组名字(30个字符以内) * @return * @throws WeChatException */ public Group createGroup(String name) throws WeChatException { JSONObject nameJson = new JSONObject(); JSONObject groupJson = new JSONObject(); nameJson.put("name", name); groupJson.put("group", nameJson); String requestData = groupJson.toString(); logger.info("request data " + requestData); String resultStr = HttpUtils.post(GROUP_CREATE_POST_URL + this.accessToken, requestData); logger.info("return data " + resultStr); WeChatUtil.isSuccess(resultStr); return JSONObject.parseObject(resultStr).getObject("group", Group.class); }
/** * 查询所有分组 * * @return */ public List<Group> getGroup() { String resultStr = HttpUtils.post(GROUP_GET_POST_URL + this.accessToken); logger.info("return data " + resultStr); try { WeChatUtil.isSuccess(resultStr); } catch (WeChatException e) { logger.error(e.getMessage()); e.printStackTrace(); return null; } JSONObject jsonObject = JSONObject.parseObject(resultStr); List<Group> groups = JSON.parseArray(jsonObject.getString("groups"), Group.class); return groups; }
/** * 获取帐号的关注者列表 * * @param nextOpenId * @return */ public Follwers subscriberList(String nextOpenId) { String url = USRE_GET_URL + accessToken; if (StringUtils.isNotBlank(nextOpenId)) { url += "&next_openid=" + nextOpenId; } String resultStr = HttpUtils.get(url); logger.info("return data " + resultStr); try { WeChatUtil.isSuccess(resultStr); } catch (WeChatException e) { logger.error(e.getMessage()); e.printStackTrace(); return null; } return JSONObject.parseObject(resultStr, Follwers.class); }
/** * 获取用户基本信息 * * @param openid 普通用户的标识,对当前公众号唯一 * @param lang 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 * @return */ public User getUserInfo(String openId, LanguageType lang) { String url = USER_INFO_GET_URL + this.accessToken + "&openid=" + openId; if (lang != null) { url += "&lang=" + lang.name(); } String resultStr = HttpUtils.get(url); logger.info("return data " + resultStr); try { WeChatUtil.isSuccess(resultStr); } catch (WeChatException e) { logger.error(e.getMessage()); e.printStackTrace(); return null; } User user = JSONObject.parseObject(resultStr, User.class); return user; }
/** * 查询用户所在分组 * * @param openId 用户的OpenID * @return 用户所属的groupid */ public Integer getIdGroup(String openId) { JSONObject jsonObject = new JSONObject(); jsonObject.put("openid", openId); String requestData = jsonObject.toString(); logger.info("request data " + requestData); String resultStr = HttpUtils.post(GROUP_GETID_POST_URL + this.accessToken, requestData); logger.info("return data " + resultStr); try { WeChatUtil.isSuccess(resultStr); } catch (WeChatException e) { logger.error(e.getMessage()); e.printStackTrace(); return null; } JSONObject resultJson = JSONObject.parseObject(resultStr); int groupId = resultJson.getIntValue("groupid"); return groupId; }