|
@@ -1,9 +1,15 @@
|
|
|
package com.idiot.operationbackend.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.cache.*;
|
|
|
import com.idiot.operationbackend.config.PlatformProperties;
|
|
|
import com.idiot.operationbackend.entity.Account;
|
|
|
+import com.idiot.operationbackend.entity.AccountFans;
|
|
|
+import com.idiot.operationbackend.entity.AccountTag;
|
|
|
+import com.idiot.operationbackend.handler.SyncUserTask;
|
|
|
+import com.idiot.operationbackend.service.facade.AccountFansService;
|
|
|
import com.idiot.operationbackend.service.facade.AccountService;
|
|
|
import com.idiot.operationbackend.service.facade.WeChatService;
|
|
|
import com.idiot.operationbackend.support.Constants;
|
|
@@ -15,6 +21,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.*;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
@@ -23,6 +30,8 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -59,6 +68,9 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
@Autowired
|
|
|
private AccountService accountService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private AccountFansService fansService;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public String notice(Map<String, String> param) {
|
|
@@ -260,7 +272,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
throw new CustomException(500,"当前公众号正在后台同步粉丝数据,请您稍等一会!");
|
|
|
}
|
|
|
cache.put(lockKey,lockValue);
|
|
|
- logger.info("公众号:{}同步粉丝数据,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ logger.info("公众号:{}同步粉丝数据---- start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";
|
|
|
String nextOpenId = "";
|
|
|
String accessToken = getAuthorizerAccessToken(accountId);
|
|
@@ -282,10 +294,81 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
total = respJsonBody.getInteger("total");
|
|
|
count += respJsonBody.getInteger("count");
|
|
|
nextOpenId = respJsonBody.getString("next_openid");
|
|
|
+ JSONArray openIdArray = respJsonBody.getJSONObject("data").getJSONArray("openid");
|
|
|
+ List<String> openIds = JSONArray.parseArray(openIdArray.toJSONString(),String.class);
|
|
|
+ new SyncUserTask(accountId,openIds,this);
|
|
|
}while (count < total);
|
|
|
cache.invalidate(lockKey);
|
|
|
+ logger.info("公众号:{}同步粉丝数据----end,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
+ @Async("asyncExecutor")
|
|
|
+ public void syncUserTask(String accountId, List<String> openIds) {
|
|
|
+
|
|
|
+ List<AccountFans> accountFans = new ArrayList<>(openIds.size());
|
|
|
+
|
|
|
+ for (String openId : openIds) {
|
|
|
+ AccountFans fans = fansService.queryByAccountIdAndOpenId(accountId,openId);
|
|
|
+ if (fans == null) {
|
|
|
+ fans = new AccountFans();
|
|
|
+ }
|
|
|
+ JSONObject fansObject = JSON.parseObject(getFansInfo(accountId,openId));
|
|
|
+ fans.setAccountId(accountId);
|
|
|
+ fans.setOpenId(openId);
|
|
|
+ fans.setNickName(fansObject.getString("nickname"));
|
|
|
+ fans.setHeadImgUrl(fansObject.getString("headimgurl"));
|
|
|
+ fans.setSex(fansObject.getInteger("sex"));
|
|
|
+ fans.setSubscribe(fansObject.getInteger("subscribe"));
|
|
|
+ fans.setCity(fansObject.getString("city"));
|
|
|
+ fans.setProvince(fansObject.getString("province"));
|
|
|
+ fans.setSubscribeTime(fansObject.getLong("subscribe_time"));
|
|
|
+ fans.setSubscribeScene(fansObject.getString("subscribe_scene"));
|
|
|
+ fans.setUnionId(fansObject.getString("unionid"));
|
|
|
+ fans.setRemark(fansObject.getString("remark"));
|
|
|
+ fans.setGroupId(fansObject.getInteger("groupid"));
|
|
|
+ fans.setTagIdList(JSONObject.toJSONString(fansObject.getJSONObject("tagid_list")));
|
|
|
+ accountFans.add(fans);
|
|
|
+ }
|
|
|
+ // 保存用户
|
|
|
+ fansService.saveOrUpdateBatch(accountFans,1000);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Async("asyncExecutor")
|
|
|
+ public void syncTag(String accountId) {
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=%s";
|
|
|
+ String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ requestUrl = String.format(requestUrl,accessToken);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (headers);
|
|
|
+ ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.GET,entity,String.class);
|
|
|
+ List<AccountTag> accountTags = JSONObject.parseArray(respStr.getBody(), AccountTag.class);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getFansInfo(String accountId, String openId) {
|
|
|
+ logger.info("查询粉丝信息,opendId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=OPENID&lang=zh_CN";
|
|
|
+ String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
+ requestUrl = String.format(requestUrl,accessToken);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (headers);
|
|
|
+ ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.GET,entity,String.class);
|
|
|
+ String jsonStr = respStr.getBody();
|
|
|
+ logger.info("查询粉丝信息,openId:{}----end,时间:{},微信返回:{}",accountId, LocalDateTime.now().toString(),jsonStr);
|
|
|
+ return jsonStr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean confirmAccount(String accountId, String userId) {
|
|
|
+ return accountService.updateUserIdByAccount(accountId,userId);
|
|
|
+ }
|
|
|
}
|