如何實(shí)現(xiàn)開(kāi)發(fā)平臺(tái)同步微信公眾號(hào)小程序?
- 編輯:微信公眾開(kāi)發(fā)平臺(tái) -1、舉個(gè)例子,某公司平臺(tái)下有多個(gè)微信公眾號(hào)、小程序,用戶(hù)關(guān)注了不同的微信公眾號(hào)以及小程序,想要實(shí)現(xiàn)這些用戶(hù)統(tǒng)一管理。這個(gè)時(shí)候就可以使用微信開(kāi)放平臺(tái)提供的功能了。 2、在微信開(kāi)放平臺(tái)提供了將微信掃碼登錄、微信公眾號(hào)、小程序、第三方平臺(tái)、移動(dòng)應(yīng)用同步用戶(hù)的接口。大家都知道微信公眾號(hào),小程序會(huì)提供openid。但是每個(gè)應(yīng)用同一個(gè)用戶(hù)的openid是不一樣的,因此微信提供了unionid這個(gè)是用戶(hù)唯一的值,只需在微信開(kāi)放平臺(tái)上綁定公眾號(hào)、以及小程序。根據(jù)上篇文檔,登錄到微信開(kāi)放平臺(tái)
1、舉個(gè)例子,某公司平臺(tái)下有多個(gè)微信公眾號(hào)、小程序,用戶(hù)關(guān)注了不同的微信公眾號(hào)以及小程序,想要實(shí)現(xiàn)這些用戶(hù)統(tǒng)一管理。這個(gè)時(shí)候就可以使用微信開(kāi)放平臺(tái)提供的功能了。
2、在微信開(kāi)放平臺(tái)提供了將微信掃碼登錄、微信公眾號(hào)、小程序、第三方平臺(tái)、移動(dòng)應(yīng)用同步用戶(hù)的接口。大家都知道微信公眾號(hào),小程序會(huì)提供openid。但是每個(gè)應(yīng)用同一個(gè)用戶(hù)的openid是不一樣的,因此微信提供了unionid這個(gè)是用戶(hù)唯一的值,只需在微信開(kāi)放平臺(tái)上綁定公眾號(hào)、以及小程序。根據(jù)上篇文檔,登錄到微信開(kāi)放平臺(tái)
3、微信公眾平臺(tái)開(kāi)發(fā)登錄微信開(kāi)放平臺(tái),打開(kāi)管理中心可以綁定公眾號(hào)以及小程序。必須在開(kāi)放平臺(tái)上綁定微信公眾號(hào),才可以獲取到用戶(hù)的唯一unionid值
4、微信公眾開(kāi)發(fā)平臺(tái)代碼參考,該代碼絕對(duì)無(wú)誤。測(cè)試已通過(guò),如有問(wèn)題,請(qǐng)檢查是否綁定公眾號(hào),或者小程序。微信提供了獲取批量用戶(hù)的unionid,限制條件每次只能獲取100個(gè),下面程序代碼是根據(jù)自身項(xiàng)目調(diào)整。可參考
接口部分
@RequestMapping(value = "/WeiXinTest")
public @ResponseBody Map<String,Object> get(HttpServletRequest request, HttpServletResponse response) throws Exception {
//同步用戶(hù)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","微信用戶(hù)更新成功" );
}else{
resut.put ( "msg","微信用戶(hù)更新失敗" );
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);
//這里手動(dòng)將查詢(xún)的數(shù)據(jù)進(jìn)行分割成100為一個(gè)map,由于微信提供的接口每次只能獲取100個(gè)用戶(hù)
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 );
}
//獲取用戶(hù)的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;
}
//切割查出來(lái)進(jìn)行同步的用戶(hù)數(shù)據(jù)
public static Map spiltList(List<WeiUser> historyList) {
int listSize = historyList.size();
int toIndex = 100;
Map map = new HashMap (); //用map存起來(lái)新的分組后數(shù)據(jù)
int keyToken = 0;
for (int i = 0; i < historyList.size(); i += toIndex) {
if (i + toIndex > listSize) { //作用為toIndex最后沒(méi)有100條數(shù)據(jù)則剩余幾條newList中就裝幾條
toIndex = listSize - i;
}
List newList = historyList.subList(i, i + toIndex);
map.put(keyToken, newList);
keyToken++;
}
System.out.println("一共切分了" + map.size() + "個(gè)list");
return map;
}