如何实现开发平台同步微信公众号小程序?
- 编辑:微信公众开发平台 -1、举个例子,某公司平台下有多个微信公众号、小程序,用户关注了不同的微信公众号以及小程序,想要实现这些用户统一管理。这个时候就可以使用微信开放平台提供的功能了。 2、在微信开放平台提供了将微信扫码登录、微信公众号、小程序、第三方平台、移动应用同步用户的接口。大家都知道微信公众号,小程序会提供openid。但是每个应用同一个用户的openid是不一样的,因此微信提供了unionid这个是用户唯一的值,只需在微信开放平台上绑定公众号、以及小程序。根据上篇文档,登录到微信开放平台
1、举个例子,某公司平台下有多个微信公众号、小程序,用户关注了不同的微信公众号以及小程序,想要实现这些用户统一管理。这个时候就可以使用微信开放平台提供的功能了。
2、在微信开放平台提供了将微信扫码登录、微信公众号、小程序、第三方平台、移动应用同步用户的接口。大家都知道微信公众号,小程序会提供openid。但是每个应用同一个用户的openid是不一样的,因此微信提供了unionid这个是用户唯一的值,只需在微信开放平台上绑定公众号、以及小程序。根据上篇文档,登录到微信开放平台
3、微信公众平台开发登录微信开放平台,打开管理中心可以绑定公众号以及小程序。必须在开放平台上绑定微信公众号,才可以获取到用户的唯一unionid值
4、微信公众开发平台代码参考,该代码绝对无误。测试已通过,如有问题,请检查是否绑定公众号,或者小程序。微信提供了获取批量用户的unionid,限制条件每次只能获取100个,下面程序代码是根据自身项目调整。可参考
接口部分
@RequestMapping(value = "/WeiXinTest")
public @ResponseBody Map<String,Object> get(HttpServletRequest request, HttpServletResponse response) throws Exception {
//同步用户unionid必须先获取token
String accessTokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
String appid = ApplicationConfiguration.getValue(BaseApplicationConstants.COMMON_WEIXIN_WEICHART_APPID_KEY);
String secret = ApplicationConfiguration.getValue(BaseApplicationConstants.COMMON_WEIXIN_WEICHART_SECRET_KEY);
accessTokenUrl= accessTokenUrl.replace ( "APPID",appid ).replace ( "SECRET",secret);
String accessInfoStr=HttpRequestUtils.httpGet(accessTokenUrl,null,null);
JSONObject accessInfoStrObject = JSON.parseObject(accessInfoStr);
WeiUser search=new WeiUser();
Map<String,Object>resut=new HashMap <> ( );
Boolean b = weiUserService.updateWeiUser (search,accessInfoStrObject.getString ( "access_token" ));
//Boolean b=true;
if(b){
resut.put("code","1");
resut.put ( "msg","微信用户更新成功" );
}else{
resut.put ( "msg","微信用户更新失败" );
resut.put("code","-1");
}
return resut;
}
@Override
public boolean updateWeiUser(WeiUser search,String access_token) throws Exception {
Map<String,Object> userMap=new HashMap ( );
boolean b;
b=true;
List<WeiUser>weiUser=weiUserService.findWeiUserList (search);
//这里手动将查询的数据进行分割成100为一个map,由于微信提供的接口每次只能获取100个用户
userMap=spiltList(weiUser);
List<JSONObject>list=new ArrayList <> ( );
try {
for(Map.Entry<String,Object> str : userMap.entrySet()){
List<WeiUser>weiUserList = (List <WeiUser>) str.getValue();
for(WeiUser entity:weiUserList){
JSONObject user = new JSONObject ();
user.put ( "openid",entity.getWeiOpenId () );
user.put("lang","zh_CN");
list.add ( user );
}
//获取用户的unionid
String userInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
userInfoUrl=userInfoUrl.replace ("ACCESS_TOKEN", access_token );
Map<String,Object> map=new HashMap ( );
map.put ( "user_list" ,list);
String user_info_list= HttpRequestUtils.httpPost (userInfoUrl,map);
JSONObject userInfoListObject = JSON.parseObject(user_info_list);
logger.info("userInfoListObject:{}",userInfoListObject);
JSONArray result=userInfoListObject.getJSONArray ( "user_info_list" );
for(int i=0;i<result.size ();i++){
String unionid=result.getJSONObject ( i ).getString ( "unionid" );
String openid=result.getJSONObject ( i ).getString ( "openid" );
//String openid="ojb3IwV-PTQtXp2bmusbQlFBM_Yc";
weiUserService.updateWeiUserUnionid ( openid,unionid );
}
list.clear ();
}
}catch (Exception e){
e.printStackTrace();
b=false;
}
return b;
}
//切割查出来进行同步的用户数据
public static Map spiltList(List<WeiUser> historyList) {
int listSize = historyList.size();
int toIndex = 100;
Map map = new HashMap (); //用map存起来新的分组后数据
int keyToken = 0;
for (int i = 0; i < historyList.size(); i += toIndex) {
if (i + toIndex > listSize) { //作用为toIndex最后没有100条数据则剩余几条newList中就装几条
toIndex = listSize - i;
}
List newList = historyList.subList(i, i + toIndex);
map.put(keyToken, newList);
keyToken++;
}
System.out.println("一共切分了" + map.size() + "个list");
return map;
}