/** * 设置备注名 * * @param user 用户对象 * @param accessToken * @throws ServiceException ServiceException */ public static void updateRemark(WechatUserDto user, String accessToken) throws ServiceException { // url and params String url = getRequestUrl(WechatUrlConstants.USER_INFO_UPDATEREMARK, accessToken); JSONObject requestJson = new JSONObject(); requestJson.put("openid", user.getOpenId()); requestJson.put("remark", user.getRemark()); // call weixin service and get group object getWxJSONObjectResponseFromHttpPostMethod(url, requestJson); }
/** * 批量移动用户分组 * * @param userList 用户列表 * @param group 分组对象 * @throws ServiceException ServiceException */ public static void updateGroupOfUserList( List<WechatUserDto> userList, Group group, String accessToken) throws ServiceException { // url and params String url = getRequestUrl(WechatUrlConstants.GROUPS_MEMBERS_BATCH_UPDATE, accessToken); JSONObject requestJson = new JSONObject(); List<String> openIdList = new ArrayList<String>(); for (WechatUserDto user : userList) { openIdList.add(user.getOpenId()); } requestJson.put("openid_list", openIdList); requestJson.put("to_groupid", group.getId()); // call weixin service and get group object getWxJSONObjectResponseFromHttpPostMethod(url, requestJson); }
/** * 获取用户基本信息(UnionID机制) * * @param user 用户对象 * @return 用户基本信息 * @throws ServiceException ServiceException */ public static WechatUserDto getUserInfo(WechatUserDto user, String accessToken) throws ServiceException { // url and params String url = getRequestUrl(WechatUrlConstants.USER_INFO, accessToken); url = url.replace("OPENID", user.getOpenId()); JSONObject userJson = getWxJSONObjectResponseFromHttpGetMethod(url); WechatUserDto responseUser = jsonObject2User(userJson); return responseUser; }
/** * 移动用户分组 * * @param user 用户对象 * @param group 分组对象 * @throws ServiceException ServiceException */ public static void updateGroupOfUser(WechatUserDto user, Group group, String accessToken) throws ServiceException { // url and params String url = getRequestUrl(WechatUrlConstants.GROUPS_MEMBERS_UPDATE, accessToken); JSONObject requestJson = new JSONObject(); requestJson.put("openid", user.getOpenId()); requestJson.put("to_groupid", group.getId()); // call weixin service and get group object getWxJSONObjectResponseFromHttpPostMethod(url, requestJson); }
/** * 查询用户所在分组 * * @param user 用户对象 * @param accessToken * @return 分组对象 * @throws ServiceException ServiceException */ public static Group getGroupOfUser(WechatUserDto user, String accessToken) throws ServiceException { // url and params String url = getRequestUrl(WechatUrlConstants.GROUPS_GET_ID, accessToken); JSONObject requestJson = new JSONObject(); requestJson.put("openid", user.getOpenId()); // call weixin service and get group object JSONObject responseJson = getWxJSONObjectResponseFromHttpPostMethod(url, requestJson); int groupId = responseJson.getInteger("groupid"); Group group = new Group(); group.setId(groupId); return group; }
/** * 批量获取用户基本信息 * * @param userList 用户列表 * @return 用户基本信息 * @throws ServiceException ServiceException */ public static List<WechatUserDto> getUserInfoList( List<WechatUserDto> userList, String accessToken) throws ServiceException { // url and params String url = getRequestUrl(WechatUrlConstants.USER_INFO_BATCH_GET, accessToken); JSONArray userListArr = new JSONArray(); for (WechatUserDto user : userList) { JSONObject userJson = new JSONObject(); userJson.put("openid", user.getOpenId()); userListArr.add(userJson); } JSONObject requestJson = new JSONObject(); requestJson.put("user_list", userListArr); // call weixin service and get group object JSONObject responseJson = getWxJSONObjectResponseFromHttpPostMethod(url, requestJson); JSONArray userInfoListArr = responseJson.getJSONArray("user_info_list"); List<WechatUserDto> responseUserList = new ArrayList<WechatUserDto>(); for (int index = 0; index < userInfoListArr.size(); index++) { JSONObject responseUserJson = userInfoListArr.getJSONObject(index); WechatUserDto responseUser = jsonObject2User(responseUserJson); responseUserList.add(responseUser); } return responseUserList; }
/** * JsonObject 转换为User对象 * * @param userJson * @return */ private static WechatUserDto jsonObject2User(JSONObject userJson) { String subscribe = userJson.getString("subscribe"); String openId = userJson.getString("openid"); String nickname = userJson.getString("nickname"); int sex = userJson.getIntValue("sex"); String language = userJson.getString("language"); String city = userJson.getString("city"); String province = userJson.getString("province"); String country = userJson.getString("country"); String headImgUrl = userJson.getString("headimgurl"); long subscribeTime = userJson.getLong("subscribe_time"); String unionid = userJson.getString("unionid"); String remark = userJson.getString("remark"); int groupid = userJson.getIntValue("groupid"); WechatUserDto responseUser = new WechatUserDto(); responseUser.setCity(city); responseUser.setSubscribe(subscribe); responseUser.setOpenId(openId); responseUser.setNickname(nickname); responseUser.setSex(sex); responseUser.setLanguage(language); responseUser.setProvince(province); responseUser.setCountry(country); responseUser.setHeadImgUrl(headImgUrl); responseUser.setSubscribeTime(subscribeTime); responseUser.setUnionId(unionid); responseUser.setRemark(remark); responseUser.getGroup().setId(groupid); return responseUser; }