Преглед изворни кода

Merge branch 'zouxuan' of http://git.dayaedu.com/yonge/cooleshow

zouxuan пре 3 година
родитељ
комит
752bbf5f24

+ 4 - 0
cooleshow-api/src/main/java/com/yonge/cooleshow/api/feign/AdminFeignService.java

@@ -45,4 +45,8 @@ public interface AdminFeignService {
     //老师课酬
     @PostMapping(value = "/task/teacherEarning")
     HttpResponseResult<Object> sendTeacherEarning();
+
+    //老师课酬
+    @PostMapping(value = "/task/refreshBlackList")
+    HttpResponseResult refreshBlackList();
 }

+ 5 - 0
cooleshow-api/src/main/java/com/yonge/cooleshow/api/feign/fallback/AdminFeignServiceFallback.java

@@ -29,4 +29,9 @@ public class AdminFeignServiceFallback implements AdminFeignService {
     public HttpResponseResult<Object> sendTeacherEarning() {
         return null;
     }
+
+    @Override
+    public HttpResponseResult refreshBlackList() {
+        return null;
+    }
 }

+ 9 - 0
cooleshow-common/src/main/java/com/yonge/cooleshow/common/constant/SysConfigConstant.java

@@ -220,6 +220,15 @@ public interface SysConfigConstant {
      * 敏感词-黑名单
      */
     String BLACK_LIST = "black_list";
+    /**
+     * 敏感词-黑名单
+     */
+    String BLACK_LIST_FILE_PATH = "/root/blacklist.txt";
+
+    /**
+     * 敏感词-黑名单-更新时间
+     */
+    String BLACK_LIST_UPDATE_TIME = "black_list_update_time";
 
     /**
      * 敏感词-白名单

+ 19 - 0
cooleshow-task/src/main/java/com/yonge/cooleshow/task/jobs/RefreshBlackListTask.java

@@ -0,0 +1,19 @@
+package com.yonge.cooleshow.task.jobs;
+
+import com.yonge.cooleshow.api.feign.AdminFeignService;
+import com.yonge.cooleshow.task.core.BaseTask;
+import com.yonge.cooleshow.task.core.TaskException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class RefreshBlackListTask extends BaseTask {
+
+    @Autowired
+    private AdminFeignService adminFeignService;
+
+    @Override
+    public void execute() throws TaskException {
+        adminFeignService.refreshBlackList();
+    }
+}

+ 2 - 0
cooleshow-user/user-admin/src/main/java/com/yonge/cooleshow/admin/AdminApplication.java

@@ -11,6 +11,7 @@ import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 
 import com.spring4all.swagger.EnableSwagger2Doc;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 @SpringBootApplication
 @EnableDiscoveryClient
@@ -19,6 +20,7 @@ import com.spring4all.swagger.EnableSwagger2Doc;
 @ComponentScan(basePackages = {"com.yonge.cooleshow", "com.yonge.toolset"})
 @Configuration
 @EnableSwagger2Doc
+@EnableScheduling
 public class AdminApplication {
     public static void main(String[] args) {
         BaseApplication.run(AppConstant.APPLICATION_ADMIN, AdminApplication.class, args);

+ 18 - 0
cooleshow-user/user-admin/src/main/java/com/yonge/cooleshow/admin/task/TaskController.java

@@ -1,9 +1,14 @@
 package com.yonge.cooleshow.admin.task;
 
 import com.yonge.cooleshow.biz.dal.service.*;
+import com.yonge.cooleshow.biz.dal.wordfilter.WordContext;
+import com.yonge.cooleshow.biz.dal.wordfilter.WordFilter;
+import com.yonge.cooleshow.common.constant.SysConfigConstant;
 import com.yonge.cooleshow.common.controller.BaseController;
 import com.yonge.cooleshow.common.entity.HttpResponseResult;
 import io.swagger.annotations.Api;
+import org.redisson.api.RBucket;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -11,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import springfox.documentation.annotations.ApiIgnore;
 
+import java.io.File;
+
 /**
  * @Author: liweifan
  * @Data: 2022/3/28 15:50
@@ -27,6 +34,8 @@ public class TaskController extends BaseController {
     private CourseScheduleService courseScheduleService;
     @Autowired
     private UserAccountRecordService userAccountRecordService;
+    @Autowired
+    private RedissonClient redissonClient;
 
     /***
      * 轮询用户订单
@@ -66,4 +75,13 @@ public class TaskController extends BaseController {
         userAccountRecordService.sendTeacherEarning();
         return HttpResponseResult.succeed();
     }
+
+    //刷新黑名单
+    @PostMapping(value = "/refreshBlackList")
+    public HttpResponseResult refreshBlackList() {
+        RBucket<Long> bucket = redissonClient.getBucket(SysConfigConstant.BLACK_LIST_UPDATE_TIME);
+        File file = new File(SysConfigConstant.BLACK_LIST_FILE_PATH);
+        bucket.set(file.lastModified());
+        return HttpResponseResult.succeed();
+    }
 }

+ 19 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/ScheduleManager.java

@@ -0,0 +1,19 @@
+package com.yonge.cooleshow.biz.dal.service;
+
+import com.yonge.cooleshow.biz.dal.wordfilter.WordContext;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ScheduleManager{
+
+    @Autowired
+    private WordContext wordContext;
+
+    @Scheduled(cron = "0 0/10 * * * ?")
+    public void refreshBlackList() {
+        if (wordContext.isInit())
+            wordContext.mapInit();
+    }
+}

+ 35 - 62
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/wordfilter/WordContext.java

@@ -5,6 +5,8 @@ import com.yonge.cooleshow.common.constant.SysConfigConstant;
 import com.yonge.toolset.base.exception.BizException;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.redisson.api.RBucket;
+import org.redisson.api.RedissonClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +32,8 @@ public class WordContext {
 
     @Autowired
     private SysConfigService sysConfigService;
+    @Autowired
+    private RedissonClient redissonClient;
 
     /**
      * 敏感词字典
@@ -49,16 +53,39 @@ public class WordContext {
      */
     private String whiteList;
 
+    //上传更新时间戳
+    private Long updateTime;
+
+    public boolean isInit(){
+        RBucket<Long> bucket = redissonClient.getBucket(SysConfigConstant.BLACK_LIST_UPDATE_TIME);
+        boolean exists = bucket.isExists();
+        if(!exists){
+            return true;
+        }else {
+            Long lastModified = bucket.get();
+            if(!Objects.equals(updateTime,lastModified)){
+                updateTime = lastModified;
+                return true;
+            }
+        }
+        return false;
+    }
+
     @PostConstruct
     public void init() {
+        initKeyWord1();
+    }
+
+    private void initKeyWord1() {
         try {
-            this.blackList = FileUtils.readFileToString(new File("/root/blacklist.txt"), "UTF-8");
+            RBucket<Long> bucket = redissonClient.getBucket(SysConfigConstant.BLACK_LIST_UPDATE_TIME);
+            File file = new File(SysConfigConstant.BLACK_LIST_FILE_PATH);
+            this.blackList = FileUtils.readFileToString(file, "UTF-8");
+            bucket.set(file.lastModified());
+            initKeyWord();
         } catch (IOException e) {
             log.error("读取黑名单词库失败", e);
         }
-//        this.blackList = sysConfigService.findConfigValue(SysConfigConstant.BLACK_LIST);
-//        this.whiteList = sysConfigService.findConfigValue(SysConfigConstant.WHITE_LIST);
-        initKeyWord();
     }
 
     /**
@@ -131,65 +158,11 @@ public class WordContext {
      */
     public void mapInit() {
         init = false;
-        this.blackList = sysConfigService.findConfigValue(SysConfigConstant.BLACK_LIST);
-        this.whiteList = sysConfigService.findConfigValue(SysConfigConstant.WHITE_LIST);
         wordMap.clear();
-        initKeyWord();
-        /*Map nowMap;
-        for (String key : wordList) {
-            List<Map> cacheList = new ArrayList<>();
-            nowMap = wordMap;
-            for (int i = 0; i < key.length(); i++) {
-                char keyChar = key.charAt(i);
-
-                Object map = nowMap.get(keyChar);
-                if (map != null) {
-                    nowMap = (Map) map;
-                    cacheList.add(nowMap);
-                } else {
-                    return;
-                }
-
-                if (i == key.length() - 1) {
-                    char[] keys = key.toCharArray();
-                    boolean cleanable = false;
-                    char lastChar = 0;
-                    for (int j = cacheList.size() - 1; j >= 0; j--) {
-                        Map cacheMap = cacheList.get(j);
-                        if (j == cacheList.size() - 1) {
-                            if (String.valueOf(WordType.BLACK.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
-                                if (wordType == WordType.WHITE) {
-                                    return;
-                                }
-                            }
-                            if (String.valueOf(WordType.WHITE.ordinal()).equals(cacheMap.get("isWhiteWord"))) {
-                                if (wordType == WordType.BLACK) {
-                                    return;
-                                }
-                            }
-                            cacheMap.remove("isWhiteWord");
-                            cacheMap.remove("isEnd");
-                            if (cacheMap.size() == 0) {
-                                cleanable = true;
-                                continue;
-                            }
-                        }
-                        if (cleanable) {
-                            Object isEnd = cacheMap.get("isEnd");
-                            if (String.valueOf(EndType.IS_END.ordinal()).equals(isEnd)) {
-                                cleanable = false;
-                            }
-                            cacheMap.remove(lastChar);
-                        }
-                        lastChar = keys[j];
-                    }
-
-                    if (cleanable) {
-                        wordMap.remove(lastChar);
-                    }
-                }
-            }
-        }*/
+        initKeyWord1();
+//        this.blackList = sysConfigService.findConfigValue(SysConfigConstant.BLACK_LIST);
+//        this.whiteList = sysConfigService.findConfigValue(SysConfigConstant.WHITE_LIST);
+//        initKeyWord();
     }
 
     /**

+ 1 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/wordfilter/WordFilter.java

@@ -76,6 +76,7 @@ public class WordFilter {
      * @param text 输入文本
      */
     public boolean include(final String text) {
+
         return include(text, 0);
     }
 

+ 2 - 0
cooleshow-user/user-student/src/main/java/com/yonge/cooleshow/student/StudentApplication.java

@@ -9,6 +9,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
  * @Author: liweifan
@@ -21,6 +22,7 @@ import org.springframework.context.annotation.Configuration;
 @ComponentScan(basePackages = {"com.yonge.cooleshow", "com.yonge.toolset"})
 @Configuration
 @EnableSwagger2Doc
+@EnableScheduling
 public class StudentApplication {
     public static void main(String[] args) {
         BaseApplication.run(AppConstant.APPLICATION_STUDENT, StudentApplication.class, args);

+ 2 - 0
cooleshow-user/user-teacher/src/main/java/com/yonge/cooleshow/teacher/TeacherApplication.java

@@ -10,6 +10,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
  * @Author: liweifan
@@ -22,6 +23,7 @@ import org.springframework.context.annotation.Configuration;
 @ComponentScan(basePackages = {"com.yonge.cooleshow","com.yonge.toolset"})
 @Configuration
 @EnableSwagger2Doc
+@EnableScheduling
 public class TeacherApplication {
     public static void main(String[] args) {
         BaseApplication.run(AppConstant.APPLICATION_TEACHER, TeacherApplication.class, args);