飞哥教你微信公众号开发-6-用户管理

开发者可以使用接口,对公众平台的分组进行查询、创建、修改、删除等操作,也可以使用接口在需要时移动用户到某个分组。

目录


开发文档: 微信公众号开发-用户管理


package com.fly.wechat.mpdemo.api.session;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fly.wechat.mpdemo.api.BaseController;
import com.fly.wechat.mpdemo.common.secure.SignUtils;
import com.fly.wechat.mpdemo.config.MchInfo;

@Controller
@RequestMapping("/custom")
public class UserController extends BaseController {
	/**
	 * 获取用户列表
	 */
	public static final String USELIST_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN";
	/**
	 * 设置备注名
	 */
	public static final String UPDATEREMARK_URL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN";
	/**
	 * 获取用户基本信息(包括UnionID机制)
	 */
	public static final String USERINFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN";
	/**
	 * 批量获取用户基本信息
	 */
	public static final String USERINFOS_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";

	/**
	 * 创建分组
	 */
	public static final String CREATEGROUP_URL = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN";

	/**
	 * 查询所有分组
	 */
	public static final String GROUPS_URL = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN";

	/**
	 * 查询用户所在分组
	 */
	public static final String GROUPS_ID_URL = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN";

	/**
	 * 修改分组名
	 */
	public static final String GROUPS_UPDATE_URL = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN";

	/**
	 * 移动用户分组
	 */
	public static final String MEMBERS_UPDATE_URL = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN";
	/**
	 * 批量移动用户分组
	 */
	public static final String MEMBERS_UPDATE_batch_URL = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=ACCESS_TOKEN";

	/**
	 * 删除分组
	 */
	public static final String DELETE_GROUP_URL = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=ACCESS_TOKEN";

	/**
	 * 用户列表
	 * 
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/userList.do")
	public String userList() throws Throwable {
		return httpGet(USELIST_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), "", d_timeout);
	}

	/**
	 * 设置备注名
	 * 
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/updateremark.do")
	public String updateremark() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("openid", "oK6W9wNQ9zyR5uwhLkNQ61i0ak08");
		map.put("remark", "中国大飞");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(UPDATEREMARK_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}

	/**
	 * 获取用户基本信息(包括UnionID机制)
	 * 
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/userinfo.do")
	public String userinfo() throws Throwable {
		Map<String, String> map = new HashMap<String, String>();
		map.put("openid", "oK6W9wNQ9zyR5uwhLkNQ61i0ak08");
		log.info("postArg:" + map);
		return httpGet(USERINFO_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), SignUtils.createLinkString(map), d_timeout);
	}
	
	/**
	 * 批量获取用户基本信息
	 * 
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/userinfos.do")
	public String userinfos() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> subMap = new HashMap<String, String>();
		subMap.put("openid", "oK6W9wNQ9zyR5uwhLkNQ61i0ak08");
		List<Object> list=new ArrayList<Object>();
		list.add(subMap);
		map.put("user_list", list);
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(USERINFOS_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	
	
	/**
	 * 创建分组
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/createGroup.do")
	public String createGroup() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> subMap = new HashMap<String, String>();
		subMap.put("name", "test");
		map.put("group", subMap);
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(CREATEGROUP_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 查询所有分组
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/groups.do")
	public String groups() throws Throwable {
		return httpPost(GROUPS_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), "", d_timeout);
	}
	/**
	 * 查询用户所在分组
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/groupsId.do")
	public String groupsId() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("openid", "oK6W9wNQ9zyR5uwhLkNQ61i0ak08");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(GROUPS_ID_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 修改分组名
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/groupUpdate.do")
	public String groupUpdate() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> subMap = new HashMap<String, String>();
		subMap.put("id", "101");
		subMap.put("name", "爱好");
		map.put("group", subMap);
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(GROUPS_UPDATE_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	
	/**
	 * 移动用户分组
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/membersUpdate.do")
	public String membersUpdate() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("openid", "oK6W9wNQ9zyR5uwhLkNQ61i0ak08");
		map.put("to_groupid", "101");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(MEMBERS_UPDATE_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 批量移动用户分组
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/groupUpdateBatch.do")
	public String groupUpdateBatch() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> openids=new ArrayList<String>();
		openids.add("oK6W9wNQ9zyR5uwhLkNQ61i0ak08");
		map.put("openid_list", openids);
		map.put("to_groupid", "102");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(MEMBERS_UPDATE_batch_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 删除分组
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/groupDelete.do")
	public String groupDelete() throws Throwable {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> subMap = new HashMap<String, String>();
		subMap.put("id", "103");
		map.put("group", subMap);
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(DELETE_GROUP_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}

	public static void main(String[] args) throws Throwable {
		UserController uc = new UserController();
//		uc.userList();MEMBERS_UPDATE_URL
//		uc.createGroup();
//		uc.updateremark();
//		uc.userinfo();
//		uc.userinfos();
//		uc.groups();
//		uc.groupsId();
//		uc.groupUpdate();
//		uc.membersUpdate();
//		uc.groupUpdateBatch();
//		uc.groupDelete();
		uc.groups();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值