Selaa lähdekoodia

add(backend) [标签同步]

wangxiao 4 vuotta sitten
vanhempi
commit
20fc22e943

+ 1 - 3
operation-backend/src/main/java/com/idiot/operationbackend/controller/AccountController.java

@@ -38,14 +38,12 @@ public class AccountController {
     @Autowired
     private AuthUserService userService;
 
-    @Autowired
-    private WeChatService weChatService;
 
 
 
     @GetMapping
     @ApiOperation(value = "查询公众号列表")
-    public ResponseEntity<JsonResult<List<Account>>> list (@RequestHeader(value = "AUTH_TOKEN") String token) {
+    public ResponseEntity<JsonResult<List<Account>>> list (@RequestHeader String token) {
         String userId = JwtTokenUtil.getUserId(token);
         logger.info("用户:{}查询公众号列表",userId);
         List<Account> accounts  = accountService.queryAccountByUserId(userId);;

+ 12 - 0
operation-backend/src/main/java/com/idiot/operationbackend/controller/IndexController.java

@@ -0,0 +1,12 @@
+package com.idiot.operationbackend.controller;
+
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ *
+ * @author wang xiao
+ * @date Created in 10:55 2020/9/15
+ */
+@RestController
+public class IndexController {
+}

+ 42 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/facade/AccountTagService.java

@@ -3,9 +3,51 @@ package com.idiot.operationbackend.service.facade;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.idiot.operationbackend.entity.AccountTag;
 
+import java.util.List;
+
 /**
  * 用户标签
  * @author wangxiao
  */
 public interface AccountTagService extends IService<AccountTag> {
+
+    /**
+     *  覆盖之前的标签
+     * @author wangxiao
+     * @date 10:06 2020/9/15
+     * @param accountId
+     * @param newTags
+     * @return boolean
+     */
+    boolean overlayAccountTag(String accountId, List<AccountTag> newTags);
+
+
+    /**
+     *  查询标签
+     * @author wangxiao
+     * @date 10:10 2020/9/15
+     * @param accountId
+     * @return java.util.List<com.idiot.operationbackend.entity.AccountTag>
+     */
+    List<AccountTag> queryAccountTag(String accountId);
+
+
+    /**
+     *  新增
+     * @author wangxiao
+     * @date 10:29 2020/9/15
+     * @param accountTags
+     * @return boolean
+     */
+    boolean addAccountTag (List<AccountTag> accountTags);
+
+
+    /**
+     *  删除
+     * @author wangxiao
+     * @date 10:29 2020/9/15
+     * @param accountTags
+     * @return boolean
+     */
+    boolean delAccountTag (List<AccountTag> accountTags);
 }

+ 58 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/AccountTagServiceImpl.java

@@ -1,11 +1,19 @@
 package com.idiot.operationbackend.service.impl;
 
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.idiot.operationbackend.entity.Account;
 import com.idiot.operationbackend.entity.AccountTag;
 import com.idiot.operationbackend.mappers.AccountTagMapper;
 import com.idiot.operationbackend.service.facade.AccountTagService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
 
 /**
  * @author wang xiao
@@ -14,4 +22,54 @@ import org.springframework.stereotype.Service;
 @Service
 public class AccountTagServiceImpl  extends ServiceImpl<AccountTagMapper,AccountTag>
         implements AccountTagService {
+
+    private final Logger logger = LoggerFactory.getLogger(AccountTagServiceImpl.class);
+
+
+    @Override
+    public boolean overlayAccountTag(String accountId, List<AccountTag> newTags) {
+        logger.info("同步粉丝标签 保存/删除数据库 --------start");
+        List<AccountTag> preData = queryAccountTag(accountId);
+        List<AccountTag> addTag = newTags.stream().filter(e->!ifHave(preData,e.getWxId())).collect(Collectors.toList());
+        List<AccountTag> delTag = preData.stream().filter(e->!ifHave(newTags,e.getWxId())).collect(Collectors.toList());
+        boolean ifAdd = addAccountTag(addTag);
+        boolean ifDel = delAccountTag(delTag);
+        logger.info("同步粉丝标签 保存/删除数据库 ---------end,add 结果:{},del 结果:{}",ifAdd,ifDel);
+        return ifAdd & ifDel;
+    }
+
+
+    @Override
+    public List<AccountTag> queryAccountTag(String accountId) {
+        return list(Wrappers.<AccountTag>lambdaQuery().eq(AccountTag::getAccountId,accountId));
+    }
+
+
+    @Override
+    public boolean addAccountTag(List<AccountTag> accountTags) {
+        if (CollectionUtils.isEmpty(accountTags)) {
+            return false;
+        }
+        return saveBatch(accountTags);
+    }
+
+    @Override
+    public boolean delAccountTag(List<AccountTag> accountTags) {
+        if (CollectionUtils.isEmpty(accountTags)) {
+            return false;
+        }
+        List<String> ids = accountTags.stream().map(AccountTag::getId).collect(Collectors.toList());
+        return removeByIds(ids);
+    }
+
+    private boolean ifHave(List<AccountTag> accountTags, Integer wxId) {
+        if (CollectionUtils.isEmpty(accountTags)) {
+            return false;
+        }
+        return accountTags.stream().anyMatch(e->wxId.equals(e.getWxId()));
+    }
+
+
+
+
 }

+ 18 - 2
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/WeChatServiceImpl.java

@@ -3,6 +3,7 @@ package com.idiot.operationbackend.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.google.common.cache.*;
 import com.idiot.operationbackend.config.PlatformProperties;
 import com.idiot.operationbackend.entity.Account;
@@ -11,6 +12,7 @@ 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.AccountTagService;
 import com.idiot.operationbackend.service.facade.WeChatService;
 import com.idiot.operationbackend.support.Constants;
 import com.idiot.operationbackend.support.CustomException;
@@ -71,6 +73,9 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
     @Autowired
     private AccountFansService fansService;
 
+    @Autowired
+    private AccountTagService tagService;
+
 
     @Override
     public String notice(Map<String, String> param) {
@@ -308,7 +313,12 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
     @Async("asyncExecutor")
     public void syncUserTask(String accountId, List<String> openIds) {
 
-        List<AccountFans> accountFans =  new ArrayList<>(openIds.size());
+        if (CollectionUtils.isEmpty(openIds)) {
+            return;
+        }
+        int size = openIds.size();
+        logger.info("公众号:{}同步粉丝请求粉丝信息,openId 大小:{}---- start,时间:{}",accountId,size,LocalDateTime.now().toString());
+        List<AccountFans> accountFans =  new ArrayList<>(size);
 
         for (String openId : openIds) {
             AccountFans fans = fansService.queryByAccountIdAndOpenId(accountId,openId);
@@ -332,6 +342,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
             fans.setTagIdList(JSONObject.toJSONString(fansObject.getJSONObject("tagid_list")));
             accountFans.add(fans);
         }
+        logger.info("公众号:{}同步粉丝请求粉丝信息,openId 大小:{}---- end,时间:{}",accountId,size,LocalDateTime.now().toString());
         //  保存用户
         fansService.saveOrUpdateBatch(accountFans,1000);
     }
@@ -340,12 +351,14 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
     @Override
     @Async("asyncExecutor")
     public void syncTag(String accountId) {
+
         String lockKey = String.format(Constants.LOCK_SYNC_TAG,accountId);
         String cacheValue = cache.getIfPresent(lockKey);
         if (!StringUtils.isEmpty(cacheValue)) {
             throw new CustomException(500,"");
         }
         cache.put(lockKey,lockKey);
+        logger.info("公众号:{}同步标签 ---- start,时间:{}",accountId,LocalDateTime.now().toString());
         String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=%s";
         String accessToken =  getAuthorizerAccessToken(accountId);
         HttpHeaders headers = new HttpHeaders();
@@ -369,13 +382,16 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
             tempTag.setCreateTime(createTime);
             accountTags.add(tempTag);
         }
+        logger.info("公众号:{}同步标签 ---- end,时间:{}",accountId,LocalDateTime.now().toString());
+        // 新增 多余的 删掉缺少的 俗称覆盖
+        boolean ifOverlay = tagService.overlayAccountTag(accountId,accountTags);
         cache.invalidate(lockKey);
 
     }
 
     @Override
     public String getFansInfo(String accountId, String openId) {
-        logger.info("查询粉丝信息,opendId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
+        logger.info("查询粉丝信息,openId:{}----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);

+ 22 - 25
operation-backend/src/main/resources/application-test.yml

@@ -10,11 +10,12 @@ spring:
       pool-name: mysqlDataSourcePool
       maximum-pool-size: 20
       minimum-idle: 10
-      connection-timeout: 120000
-      validation-timeout: 6000
-      idle-timeout: 60000
+      connection-timeout: 30000
+      validation-timeout: 5000
+      idle-timeout: 600000
       login-timeout: 5
-      max-lifetime: 60000
+      max-lifetime: 1800000
+      connection-test-query:  select  1
 #  mybatis-plus
 mybatis-plus:
   global-config:
@@ -30,26 +31,22 @@ logging:
   level:
     root: info
     org.mybatis: debug
-#kaptcha 图形验证码配置
-kaptcha:
-  height: 50
-  width: 150
-  content:
-    length: 4
-    source: 0123456789abcdefghijklmnopqrstuvwxyz
-    space: 10
-  font:
-    color: blue
-    name: 宋体,楷体,微软雅黑
-    size: 40
-  background-color:
-    from: lightGray
-    to: white
-  border:
-    enabled: false
-weChat:
+wechat:
   # 微信第三方平台 (独立部署需要自主申请)
   platform:
-    appid: xxxxxxxxxxxx
-
-
+    # appid
+    appId: wxbc8cf6d177029077
+    # appsecret
+    appSecret: cad9594114c9473b5d9a697cfe154b33
+    # 授权事件接收URL
+    ticketUrl: http://luojigou.vip/wxoperate/wechat/notice
+    # 加解密key
+    secret: FDl8GfVXfGWwKs9LKc11xE6N2f8DM6MB8cyMm6xYsac
+    # 开发平台配置token
+    token: eNoUNRR4e7V85KLb
+    # authCallBackUrl 授权回调地址
+    authCallBack: http://luojigou.vip/wxoperate/wechat/authCallBack
+    # 消息接受地址
+    msgCallBack: http://luojigou.vip/wxoperate/wechat/msgCallBack
+# 授权确认界面地址 前端界面路由地址
+confirm-domain: https://www.xxxx