Przeglądaj źródła

add(operation-backend & SQL )
[增加标签表]

wangxiao 4 lat temu
rodzic
commit
381af81b21

+ 29 - 0
operation-backend/src/main/java/com/idiot/operationbackend/config/CorsConfig.java

@@ -0,0 +1,29 @@
+//package com.idiot.operationbackend.config;
+//
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//import org.springframework.web.cors.CorsConfiguration;
+//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+//import org.springframework.web.filter.CorsFilter;
+//
+///**
+// * @author wang xiao
+// * @date Created in 15:08 2020/9/14
+// */
+//@Configuration
+//public class CorsConfig {
+//
+//
+//
+//    @Bean
+//    public CorsFilter corsFilter() {
+//        CorsConfiguration config = new CorsConfiguration();
+//        config.addAllowedOrigin("*");
+//        config.addAllowedMethod("*");
+//        config.addAllowedHeader("*");
+//        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
+//        configSource.registerCorsConfiguration("/**", config);
+//        return new CorsFilter(configSource);
+//    }
+//
+//}

+ 10 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/facade/WeChatService.java

@@ -151,6 +151,16 @@ public interface WeChatService {
   void cacheAuthorizerAccessToken(String accountId,String authorizerAccessToken);
 
 
+  /**
+   *  同步用户
+   * @author wangxiao
+   * @date 14:10 2020/9/14
+   * @param accountId
+   * @return int
+   */
+  int syncAccountUser(String accountId);
+
+
   /**
    *  微信消息转换成map(非加密)
    * @author wangxiao

+ 48 - 7
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/WeChatServiceImpl.java

@@ -7,6 +7,7 @@ import com.idiot.operationbackend.entity.Account;
 import com.idiot.operationbackend.service.facade.AccountService;
 import com.idiot.operationbackend.service.facade.WeChatService;
 import com.idiot.operationbackend.support.Constants;
+import com.idiot.operationbackend.support.CustomException;
 import com.idiot.operationbackend.support.wechat.AesException;
 import com.idiot.operationbackend.support.wechat.WXBizMsgCrypt;
 import org.slf4j.Logger;
@@ -21,6 +22,7 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
+import java.time.LocalDateTime;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
@@ -67,7 +69,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
         String componentAppId = param.get("AppId");
         String ticket = param.get("ComponentVerifyTicket");
         logger.info("接受微信验证票据数据,infoType:{},第三方平台appId:{},ticket:{}",infoType,componentAppId,ticket);
-        cache.put(String.format("component_verify_ticket_%s",componentAppId),ticket);
+        cache.put(String.format(Constants.COMPONENT_VERIFY_TICKET,componentAppId),ticket);
         return Constants.SUCCESS;
     }
 
@@ -97,7 +99,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
 
     @Override
     public void cacheAuthorizerAccessToken(String accountId, String authorizerAccessToken) {
-        cache.put(String.format("authorizer_access_token_%s",accountId),authorizerAccessToken);
+        cache.put(String.format(Constants.AUTHORIZER_ACCESS_TOKEN,accountId),authorizerAccessToken);
     }
 
     @Override
@@ -109,7 +111,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
     public String getComponentAccessToken() {
         logger.info("获取第三方平台令牌 component_access_token start");
         String componentAppId = properties.getAppId();
-        String componentAccessToken = cache.getIfPresent(String.format("component_access_token_%s",componentAppId));
+        String componentAccessToken = cache.getIfPresent(String.format(Constants.COMPONENT_ACCESS_TOKEN,componentAppId));
         if ( !StringUtils.isEmpty(componentAccessToken)) {
             return componentAccessToken;
         }
@@ -117,7 +119,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
         HttpHeaders headers = new HttpHeaders();
         MultiValueMap<String,String> params = new LinkedMultiValueMap<>(3);
         headers.setContentType(MediaType.APPLICATION_JSON);
-        String ticket = cache.getIfPresent(String.format("component_verify_ticket_%s",componentAppId));
+        String ticket = cache.getIfPresent(String.format(Constants.COMPONENT_VERIFY_TICKET,componentAppId));
         params.add("component_appid",componentAppId);
         params.add("component_appsecret",properties.getAppSecret());
         params.add("component_verify_ticket",ticket);
@@ -127,7 +129,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
         logger.info("请求微信获取令牌component_access_token 第三方平台appId:{},ticket:{},微信返回结果:{}",componentAppId,ticket,jsonStr);
         JSONObject jsonObject = JSONObject.parseObject(jsonStr);
         componentAccessToken = jsonObject.getString("component_access_token");
-        cache.put(String.format("component_access_token_%s",componentAppId),componentAccessToken);
+        cache.put(String.format(Constants.COMPONENT_ACCESS_TOKEN,componentAppId),componentAccessToken);
         return componentAccessToken;
     }
 
@@ -135,7 +137,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
     public String getAuthorizerAccessToken(String accountId) {
 
         logger.info("获取授权方令牌 authorizer_access_token start");
-        String authorizerAccessToken = cache.getIfPresent(String.format("authorizer_access_token_%s",accountId));
+        String authorizerAccessToken = cache.getIfPresent(String.format(Constants.AUTHORIZER_ACCESS_TOKEN,accountId));
         if ( !StringUtils.isEmpty(authorizerAccessToken)) {
             return authorizerAccessToken;
         }
@@ -165,7 +167,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
         String authorizerRefreshToken  = jsonObject.getString("authorizer_refresh_token");
         account.setAuthorizerAccessToken(authorizerAccessToken);
         account.setAuthorizerRefreshToken(authorizerRefreshToken);
-        cache.put(String.format("authorizer_access_token_%s",accountId),authorizerAccessToken);
+        cache.put(String.format(Constants.AUTHORIZER_ACCESS_TOKEN,accountId),authorizerAccessToken);
         accountService.saveOrUpdate(account);
         return authorizerAccessToken;
     }
@@ -247,4 +249,43 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
         logger.info("请求微信获取授权方信息 第三方平台appId:{},授权方appId:{},微信返回结果:{}",componentAppId,authorizerAppId,jsonStr);
         return jsonStr;
     }
+
+
+    @Override
+    public int syncAccountUser(String accountId) {
+
+        String lockKey = String.format(Constants.LOCK_SYNC_USER,accountId);
+        String lockValue = cache.getIfPresent(lockKey);
+        if (StringUtils.isEmpty(lockValue)) {
+            throw new CustomException(500,"当前公众号正在后台同步粉丝数据,请您稍等一会!");
+        }
+        cache.put(lockKey,lockValue);
+        logger.info("公众号:{}同步粉丝数据,时间:{}",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);
+        int count = 0;
+        int total = 0;
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        HttpEntity<MultiValueMap<String, String>>  entity = new HttpEntity<> (headers);
+        String jsonStr = null;
+        JSONObject respJsonBody = null;
+        do {
+            if (StringUtils.isEmpty(nextOpenId)) {
+                nextOpenId = "";
+            }
+            requestUrl = String.format(requestUrl,accessToken,nextOpenId);
+            ResponseEntity<String> responseEntity = restTemplate.exchange(requestUrl,HttpMethod.GET,entity,String.class);
+            jsonStr = responseEntity.getBody();
+            respJsonBody = JSONObject.parseObject(jsonStr);
+            total = respJsonBody.getInteger("total");
+            count += respJsonBody.getInteger("count");
+            nextOpenId = respJsonBody.getString("next_openid");
+        }while (count < total);
+        cache.invalidate(lockKey);
+        return count;
+    }
+
+
 }

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

@@ -19,4 +19,22 @@ public class Constants {
     public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd",DEFAULT_LOCALE);
 
     public static final String DEFAULT_HEAD_IMG = "http://pic.51yuansu.com/pic3/cover/01/69/80/595f67c2aff1e_610.jpg";
+
+    /**
+     * 第三方 ticket
+     */
+    public static final String COMPONENT_VERIFY_TICKET ="COMPONENT_VERIFY_TICKET_%s";
+    /**
+     * 第三方 ACCESS_TOKEN
+     */
+    public static final String COMPONENT_ACCESS_TOKEN = "COMPONENT_ACCESS_TOKEN_%s";
+    /**
+     * 授权方 ACCESS_TOKEN
+     */
+    public static final String AUTHORIZER_ACCESS_TOKEN = "AUTHORIZER_ACCESS_TOKEN_%s";
+
+    /**
+     * 同步用户锁
+     */
+    public static final String LOCK_SYNC_USER = "LOCK_SYNC_USER_%s";
 }

+ 20 - 0
operation-backend/src/test/java/com/idiot/operationbackend/OperationBackendApplicationTests.java

@@ -1,9 +1,13 @@
 package com.idiot.operationbackend;
 
+import com.idiot.operationbackend.entity.AuthUser;
+import com.idiot.operationbackend.service.facade.AuthUserService;
 import com.idiot.operationbackend.support.wechat.AesException;
 import com.idiot.operationbackend.support.wechat.WXBizMsgCrypt;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.util.DigestUtils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -22,6 +26,9 @@ import static org.junit.jupiter.api.Assertions.fail;
 @SpringBootTest
 class OperationBackendApplicationTests {
 
+    @Autowired
+    private AuthUserService userService;
+
     @Test
     void contextLoads() {
     }
@@ -141,4 +148,17 @@ class OperationBackendApplicationTests {
         // 只要不抛出异常就好
     }
 
+
+    @Test
+    public void  queryUser () {
+        String userCode ="admin";
+        String password = "admin123!@#";
+        password = DigestUtils.md5DigestAsHex(password.getBytes());
+        System.out.println(password);
+        password = DigestUtils.md5DigestAsHex(password.getBytes());
+        System.out.println(password);
+        AuthUser authUser = userService.queryByUserCodeAndPassword(password,userCode);
+        System.out.println(authUser);
+    }
+
 }