| @@ -2,10 +2,7 @@ package com.iformall.mp.controller; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.WxAuthorizerInfo; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.enums.EnumAppType; | |||
| import com.iformall.mapper.WxAuthorizerInfoMapper; | |||
| import com.iformall.mapper.WxCUserMapper; | |||
| import com.iformall.mp.manager.WxMpManager; | |||
| import com.iformall.service.WxCUserService; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| @@ -21,7 +18,6 @@ import org.springframework.web.bind.annotation.PathVariable; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| /** | |||
| @@ -42,10 +38,9 @@ public class WxMemController { | |||
| private WxCUserService userService; | |||
| @GetMapping("/syncAccountFansList") | |||
| public ResultData syncAccountFansList(@PathVariable String appId) throws WxErrorException { | |||
| public ResultData syncAccountFansList(@PathVariable String appId) { | |||
| final WxMpService wxService = wxMpManager.getMpService(appId); | |||
| WxAuthorizerInfo authQ = new WxAuthorizerInfo(); | |||
| authQ.setAuthorizerAppid(appId); | |||
| WxAuthorizerInfo authorizerInfo = wxAuthorizerInfoMapper.findWeChatMp(authQ); | |||
| @@ -55,9 +50,7 @@ public class WxMemController { | |||
| while (true) { | |||
| WxMpUserList mpUserList = wxService.getUserService().userList(nextOpenId); | |||
| logger.info(mpUserList.toString()); | |||
| List<WxMpUser> userList = wxService.getUserService().userInfoList(mpUserList.getOpenids()); | |||
| logger.info(userList.toString()); | |||
| saveToDB(userList, authorizerInfo); | |||
| getBatchUserInfos(wxService, mpUserList, authorizerInfo); | |||
| nextOpenId = mpUserList.getNextOpenid(); | |||
| if(StringUtils.isBlank(nextOpenId)) { | |||
| break; | |||
| @@ -66,9 +59,32 @@ public class WxMemController { | |||
| return new ResultData(); | |||
| } | |||
| private void saveToDB(List<WxMpUser> userList, WxAuthorizerInfo authorizerInfo) { | |||
| for(WxMpUser mpUser: userList) { | |||
| userService.saveOrUpdateMpUser(mpUser, authorizerInfo); | |||
| private void getBatchUserInfos(WxMpService wxService, WxMpUserList mpUserList, WxAuthorizerInfo authorizerInfo) { | |||
| List<String> openIds = mpUserList.getOpenids(); | |||
| if(openIds.size() > 100) { | |||
| int index = 0; | |||
| while(true) { | |||
| getBatchUserInfo(wxService, openIds.subList(index, index + 99), authorizerInfo); | |||
| index += 100; | |||
| } | |||
| } else { | |||
| getBatchUserInfo(wxService, openIds, authorizerInfo); | |||
| } | |||
| } | |||
| private void getBatchUserInfo(WxMpService wxService, List<String> openIds, WxAuthorizerInfo authorizerInfo) { | |||
| List<WxMpUser> userList = null; | |||
| try{ | |||
| userList = wxService.getUserService().userInfoList(openIds); | |||
| } catch (WxErrorException e) { | |||
| logger.error("batchget userinfo list error: " + e.getMessage()); | |||
| return; | |||
| } | |||
| if(userList != null) { | |||
| logger.info(userList.toString()); | |||
| for(WxMpUser mpUser: userList) { | |||
| userService.saveOrUpdateMpUser(mpUser, authorizerInfo); | |||
| } | |||
| } | |||
| } | |||
| } | |||