如何實(shí)現(xiàn)開發(fā)平臺同步微信公眾號小程序?
- 編輯:微信公眾開發(fā)平臺 -1、舉個例子,某公司平臺下有多個微信公眾號、小程序,用戶關(guān)注了不同的微信公眾號以及小程序,想要實(shí)現(xiàn)這些用戶統(tǒng)一管理。這個時候就可以使用微信開放平臺提供的功能了。 2、在微信開放平臺提供了將微信掃碼登錄、微信公眾號、小程序、第三方平臺、移動應(yīng)用同步用戶的接口。大家都知道微信公眾號,小程序會提供openid。但是每個應(yīng)用同一個用戶的openid是不一樣的,因此微信提供了unionid這個是用戶唯一的值,只需在微信開放平臺上綁定公眾號、以及小程序。根據(jù)上篇文檔,登錄到微信開放平臺
1、舉個例子,某公司平臺下有多個微信公眾號、小程序,用戶關(guān)注了不同的微信公眾號以及小程序,想要實(shí)現(xiàn)這些用戶統(tǒng)一管理。這個時候就可以使用微信開放平臺提供的功能了。
2、在微信開放平臺提供了將微信掃碼登錄、微信公眾號、小程序、第三方平臺、移動應(yīng)用同步用戶的接口。大家都知道微信公眾號,小程序會提供openid。但是每個應(yīng)用同一個用戶的openid是不一樣的,因此微信提供了unionid這個是用戶唯一的值,只需在微信開放平臺上綁定公眾號、以及小程序。根據(jù)上篇文檔,登錄到微信開放平臺
3、微信公眾平臺開發(fā)登錄微信開放平臺,打開管理中心可以綁定公眾號以及小程序。必須在開放平臺上綁定微信公眾號,才可以獲取到用戶的唯一unionid值

4、微信公眾開發(fā)平臺代碼參考,該代碼絕對無誤。測試已通過,如有問題,請檢查是否綁定公眾號,或者小程序。微信提供了獲取批量用戶的unionid,限制條件每次只能獲取100個,下面程序代碼是根據(jù)自身項(xiàng)目調(diào)整。可參考
接口部分
@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);
//這里手動將查詢的數(shù)據(jù)進(jìn)行分割成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;
}
//切割查出來進(jìn)行同步的用戶數(shù)據(jù)
public static Map spiltList(List<WeiUser> historyList) {
int listSize = historyList.size();
int toIndex = 100;
Map map = new HashMap (); //用map存起來新的分組后數(shù)據(jù)
int keyToken = 0;
for (int i = 0; i < historyList.size(); i += toIndex) {
if (i + toIndex > listSize) { //作用為toIndex最后沒有100條數(shù)據(jù)則剩余幾條newList中就裝幾條
toIndex = listSize - i;
}
List newList = historyList.subList(i, i + toIndex);
map.put(keyToken, newList);
keyToken++;
}
System.out.println("一共切分了" + map.size() + "個list");
return map;
}




