Просмотр исходного кода

fix:“优化微信公众号推送功能“

soobin 1 год назад
Родитель
Сommit
499b41bbd9

+ 1 - 0
src/main/java/com/szwl/controller/ADIndexController.java

@@ -41,6 +41,7 @@ public class ADIndexController {
 
     @Autowired
     private TEquipmentService equipmentService;
+
     @Resource
     private RedisTemplate redisTemplate;
     /**

+ 21 - 4
src/main/java/com/szwl/controller/ScheduledService.java

@@ -2,10 +2,7 @@ package com.szwl.controller;
 
 import com.szwl.mapper.TLocationCheckMapper;
 import com.szwl.model.entity.TLocationCheck;
-import com.szwl.service.TDepartmentService;
-import com.szwl.service.TEquipmentService;
-import com.szwl.service.THotUpdateService;
-import com.szwl.service.TLocationCheckService;
+import com.szwl.service.*;
 import com.szwl.service.es.EsTCoinOrderService;
 import com.szwl.service.es.EsTOrderService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,22 +24,31 @@ import java.util.List;
 @Component
 @EnableScheduling // 2.开启定时任务
 public class ScheduledService {
+
     @Autowired
     private TDepartmentService tDepartmentService;
+
     @Autowired
     EsTCoinOrderService esTCoinOrderService;
+
     @Autowired
     EsTOrderService esTOrderService;
+
     @Autowired
     THotUpdateService hotUpdateService;
+
     @Autowired
     TEquipmentService equipmentService;
 
     @Resource
     TLocationCheckMapper locationCheckMapper;
+
     @Resource
     TLocationCheckService locationCheckService;
 
+    @Resource
+    TWechatService wechatService;
+
     // 每周二上午8点10分去校验设备位置
     @Scheduled(cron = "0 10 8 ? * TUE")
     public void equipmentLocCheck() throws IOException {
@@ -145,4 +151,15 @@ public class ScheduledService {
             equipmentService.checkEquipmentStatus();
         }
     }
+
+    /**
+     *  定时更新access_token
+     */
+    @Scheduled(cron = "0 0 */1 * * ?")
+    public void updateAccessToken() {
+        if (isDo()) {
+            wechatService.getAccessToken("0");
+            wechatService.getAccessToken("1");
+        }
+    }
 }

+ 7 - 0
src/main/java/com/szwl/service/TWechatService.java

@@ -37,4 +37,11 @@ public interface TWechatService extends IService<TWechat> {
      * @return
      */
      void sendAlarmMessage(String openId, String clientId, String name, String companyType, String alarmContent, Date occurrenceTime);
+
+    /**
+     * 获取access_token
+     * @param companyType
+     * @return
+     */
+     String getAccessToken(String companyType);
 }

+ 84 - 62
src/main/java/com/szwl/service/impl/TWechatServiceImpl.java

@@ -3,6 +3,7 @@ package com.szwl.service.impl;
 import com.alibaba.fastjson.JSONObject;
 import com.szwl.constant.ConfigConsts;
 import com.szwl.model.bean.WeChatTemplateMsg;
+import com.szwl.model.entity.TAdmin;
 import com.szwl.model.entity.TWechat;
 import com.szwl.mapper.TWechatMapper;
 import com.szwl.service.TAdminService;
@@ -16,6 +17,7 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
+import javax.annotation.Resource;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -43,7 +45,7 @@ public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> impl
     @Autowired
     TAdminService adminService;
 
-    @Autowired
+    @Resource
     private StringRedisTemplate redisTemplate;
 
     private static final String SZ_ACCESS_TOKEN_KEY = "sz_wechat_access_token";
@@ -102,6 +104,82 @@ public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> impl
         sendWechatMessage(openId, sendMag, companyType);
     }
 
+    @Override
+    public String getAccessToken(String companyType) {
+        String url= "";
+        String accessToken = "";
+        if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
+            url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+                    + ConfigConsts.SZ_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SZ_WX_APP_SECRET;
+            TAdmin admin = adminService.getById(2738L);
+            admin.setApiKey(accessToken);
+            adminService.updateById(admin);
+        } else {
+            url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+                    + ConfigConsts.SC_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SC_WX_APP_SECRET;
+            TAdmin admin = adminService.getById(2739L);
+            admin.setApiKey(accessToken);
+            adminService.updateById(admin);
+        }
+        PrintWriter out = null;
+        BufferedReader in = null;
+        String line;
+        StringBuffer stringBuffer = new StringBuffer();
+        try {
+            URL realUrl = new URL(url);
+            // 打开和URL之间的连接
+            URLConnection conn = realUrl.openConnection();
+
+            // 设置通用的请求属性 设置请求格式
+            //设置返回类型
+            conn.setRequestProperty("contentType", "text/plain");
+            //设置请求类型
+            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
+            //设置超时时间
+            conn.setConnectTimeout(1000);
+            conn.setReadTimeout(1000);
+            conn.setDoOutput(true);
+            conn.connect();
+            // 获取URLConnection对象对应的输出流
+            out = new PrintWriter(conn.getOutputStream());
+            // flush输出流的缓冲
+            out.flush();
+            // 定义BufferedReader输入流来读取URL的响应    设置接收格式
+            in = new BufferedReader(
+                    new InputStreamReader(conn.getInputStream(), "UTF-8"));
+            while ((line = in.readLine()) != null) {
+                stringBuffer.append(line);
+            }
+            JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
+            log.info("获取token:{}",jsonObject.toString());
+            accessToken = jsonObject.getString("access_token");
+            if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
+                TAdmin admin = adminService.getById(2738L);
+                admin.setApiKey(accessToken);
+                adminService.updateById(admin);
+            } else {
+                TAdmin admin = adminService.getById(2739L);
+                admin.setApiKey(accessToken);
+                adminService.updateById(admin);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            //使用finally块来关闭输出流、输入流
+            try {
+                if (out != null) {
+                    out.close();
+                }
+                if (in != null) {
+                    in.close();
+                }
+            } catch (IOException ex) {
+                ex.printStackTrace();
+            }
+        }
+        return accessToken;
+    }
+
     /**
      * 获取accessToken推送消息
      * @param openId
@@ -114,73 +192,17 @@ public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> impl
         String accessToken = "";
         try {
             if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
-                accessToken = redisTemplate.opsForValue().get(SZ_ACCESS_TOKEN_KEY);
+                TAdmin admin = adminService.getById(2738L);
+                accessToken = admin.getApiKey();
             } else {
-                accessToken = redisTemplate.opsForValue().get(SC_ACCESS_TOKEN_KEY);
+                TAdmin admin = adminService.getById(2739L);
+                accessToken = admin.getApiKey();
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         if(StringUtils.isEmpty(accessToken)) {
-            if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
-                url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
-                        + ConfigConsts.SZ_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SZ_WX_APP_SECRET;
-            } else {
-                url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
-                        + ConfigConsts.SC_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SC_WX_APP_SECRET;
-            }
-            PrintWriter out = null;
-            BufferedReader in = null;
-            String line;
-            StringBuffer stringBuffer = new StringBuffer();
-            try {
-                URL realUrl = new URL(url);
-                // 打开和URL之间的连接
-                URLConnection conn = realUrl.openConnection();
-
-                // 设置通用的请求属性 设置请求格式
-                //设置返回类型
-                conn.setRequestProperty("contentType", "text/plain");
-                //设置请求类型
-                conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
-                //设置超时时间
-                conn.setConnectTimeout(1000);
-                conn.setReadTimeout(1000);
-                conn.setDoOutput(true);
-                conn.connect();
-                // 获取URLConnection对象对应的输出流
-                out = new PrintWriter(conn.getOutputStream());
-                // flush输出流的缓冲
-                out.flush();
-                // 定义BufferedReader输入流来读取URL的响应    设置接收格式
-                in = new BufferedReader(
-                        new InputStreamReader(conn.getInputStream(), "UTF-8"));
-                while ((line = in.readLine()) != null) {
-                    stringBuffer.append(line);
-                }
-                JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
-                log.info("获取token:{}",jsonObject.toString());
-                accessToken = jsonObject.getString("access_token");
-                if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
-                    redisTemplate.opsForValue().set(SZ_ACCESS_TOKEN_KEY, accessToken, EXPIRATION_TIME, TimeUnit.SECONDS);
-                } else {
-                    redisTemplate.opsForValue().set(SZ_ACCESS_TOKEN_KEY, accessToken, EXPIRATION_TIME, TimeUnit.SECONDS);
-                }
-            } catch (Exception e) {
-                e.printStackTrace();
-            } finally {
-                //使用finally块来关闭输出流、输入流
-                try {
-                    if (out != null) {
-                        out.close();
-                    }
-                    if (in != null) {
-                        in.close();
-                    }
-                } catch (IOException ex) {
-                    ex.printStackTrace();
-                }
-            }
+            accessToken = getAccessToken(url);
         }
         // 推送消息
         RestTemplate restTemplate = new RestTemplate();