Browse Source

调用微信接口耗时日志打印

liuam 7 years ago
parent
commit
29ef6a92cb

+ 11 - 1
src/main/java/com/uas/platform/b2c/common/weixin/service/impl/WeChatServiceImpl.java

@@ -58,6 +58,8 @@ public class WeChatServiceImpl implements WeChatService{
     @Override
     public ModelMap getWxUserInfo(String code, String state, String openId) {
         logger.info("得到用户信息 code: {}, state: {}, openId: {}", code, state, openId);
+
+        long start = System.currentTimeMillis();
         ModelMap result = new ModelMap();
         if (!StringUtils.isEmpty(code)) {
             AuthUserInfo userInfo = null;
@@ -87,6 +89,7 @@ public class WeChatServiceImpl implements WeChatService{
             result.put("userAccount", userAccount);
             result.put("enterprises", enterprises);
         }
+        logger.info("微信:得到用户信息总耗时: {}ms", System.currentTimeMillis() - start);
         return result;
     }
 
@@ -205,12 +208,15 @@ public class WeChatServiceImpl implements WeChatService{
      */
     private AuthUserInfo getAccessTokenByCode(String code) throws Exception {
         logger.info("通过code获取用户openId, code: {}", code);
+        long start = System.currentTimeMillis();
         AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET, code, "authorization_code");
         String json = HttpReqUtil.doGet(WeChatUtil.GET_OAUTH_TOKEN_URL, authTokenParams.getParams());
         AuthAccessToken authAccessToken = FlexJsonUtils.fromJson(json, AuthAccessToken.class);
         if (authAccessToken.getOpenid() == null) {
             throw new WeChatException(json);
         }
+
+        logger.info("请求微信接口,得到 AuthAccessToken: {}, 耗时: {}ms", json, System.currentTimeMillis() - start);
         String accessToken = authAccessToken.getAccess_token();
         String openId = authAccessToken.getOpenid();
         // 可能抛出异常,向上抛
@@ -228,11 +234,14 @@ public class WeChatServiceImpl implements WeChatService{
         logger.info("获取用户信息 accessToken: {}, openId: {}", accessToken, openId);
         // 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
         AuthUserParams authUserParams = new AuthUserParams(accessToken, openId, "zh_CN");
+
+        long start = System.currentTimeMillis();
         String json = HttpReqUtil.doGet(WeChatUtil.SNS_USERINFO_URL, authUserParams.getParams());
         AuthUserInfo authUserInfo = FlexJsonUtils.fromJson(json, AuthUserInfo.class);
         if (authUserInfo.getOpenid() == null) {
             throw new WeChatException(json);
         }
+        logger.info("请求微信接口,得到用户信息: {}, 耗时: {}ms", json, System.currentTimeMillis() - start);
         return authUserInfo;
     }
 
@@ -246,11 +255,12 @@ public class WeChatServiceImpl implements WeChatService{
             logger.info("从缓存中得到 access_token: {}", access_token);
             return access_token;
         }
+        long start = System.currentTimeMillis();
         AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET,"client_credential");
         String json = HttpReqUtil.doGet(WeChatUtil.GET_ACCESS_TOKEN, authTokenParams.getParams());
         AccessToken accessToken = FlexJsonUtils.fromJson(json, AccessToken.class);
         access_token = accessToken.getAccess_token();
-        logger.info("请求微信接口,得到 access_token: {}", access_token);
+        logger.info("请求微信接口,得到 access_token: {}, 耗时: {}ms", access_token, System.currentTimeMillis() - start);
         redisTemplate.opsForValue().set("WX_ACCESS_TOKEN", access_token, ACCESS_TOKEN_EXPIRES_IN, TimeUnit.SECONDS);
         return access_token;
     }