Procházet zdrojové kódy

add(weChat-Operation)
[readme]

wangxiao1002 před 4 roky
rodič
revize
17a7064f6b

+ 11 - 0
operation-backend/src/main/java/com/idiot/operationbackend/mappers/AccountTagMapper.java

@@ -0,0 +1,11 @@
+package com.idiot.operationbackend.mappers;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.idiot.operationbackend.entity.AccountTag;
+
+
+/**
+ *   @author wang xiao
+ */
+public interface AccountTagMapper extends BaseMapper<AccountTag> {
+}

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

@@ -0,0 +1,11 @@
+package com.idiot.operationbackend.service.facade;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.idiot.operationbackend.entity.AccountTag;
+
+/**
+ * 用户标签
+ * @author wangxiao
+ */
+public interface AccountTagService extends IService<AccountTag> {
+}

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

@@ -0,0 +1,17 @@
+package com.idiot.operationbackend.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.idiot.operationbackend.entity.AccountTag;
+import com.idiot.operationbackend.mappers.AccountTagMapper;
+import com.idiot.operationbackend.service.facade.AccountTagService;
+import org.springframework.stereotype.Service;
+
+
+/**
+ * @author wang xiao
+ */
+
+@Service
+public class AccountTagServiceImpl  extends ServiceImpl<AccountTagMapper,AccountTag>
+        implements AccountTagService {
+}

+ 25 - 3
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/WeChatServiceImpl.java

@@ -268,10 +268,10 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
 
         String lockKey = String.format(Constants.LOCK_SYNC_USER,accountId);
         String lockValue = cache.getIfPresent(lockKey);
-        if (StringUtils.isEmpty(lockValue)) {
+        if (!StringUtils.isEmpty(lockValue)) {
             throw new CustomException(500,"当前公众号正在后台同步粉丝数据,请您稍等一会!");
         }
-        cache.put(lockKey,lockValue);
+        cache.put(lockKey,lockKey);
         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 = "";
@@ -340,6 +340,12 @@ 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);
         String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=%s";
         String accessToken =  getAuthorizerAccessToken(accountId);
         HttpHeaders headers = new HttpHeaders();
@@ -347,7 +353,23 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
         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);
+        JSONArray jsonArray = JSONObject.parseObject(respStr.getBody()).getJSONArray("tags");
+        int size = jsonArray.size();
+        List<AccountTag> accountTags = new ArrayList<>(size);
+        JSONObject temp = null;
+        AccountTag tempTag = null;
+        String createTime  = Constants.DATE_TIME_FORMATTER.format(LocalDateTime.now());
+        for (int i = 0; i < size; i++) {
+            temp = jsonArray.getJSONObject(i);
+            tempTag = new AccountTag();
+            tempTag.setAccountId(accountId);
+            tempTag.setFansCount(temp.getInteger("count"));
+            tempTag.setName(temp.getString("name"));
+            tempTag.setWxId(temp.getInteger("id"));
+            tempTag.setCreateTime(createTime);
+            accountTags.add(tempTag);
+        }
+        cache.invalidate(lockKey);
 
     }
 

+ 2 - 0
operation-backend/src/main/java/com/idiot/operationbackend/support/Constants.java

@@ -37,4 +37,6 @@ public class Constants {
      * 同步用户锁
      */
     public static final String LOCK_SYNC_USER = "LOCK_SYNC_USER_%s";
+
+    public static final String LOCK_SYNC_TAG = "LOCK_SYNC_TAG_%s";
 }