Browse Source

关键词过滤,自动生成配置文件

lizhen_gitee 1 year ago
parent
commit
dc2714094c
2 changed files with 44 additions and 2 deletions
  1. 32 2
      application/admin/model/Keyworldfilter.php
  2. 12 0
      application/extra/keyworld.php

+ 32 - 2
application/admin/model/Keyworldfilter.php

@@ -27,9 +27,39 @@ class Keyworldfilter extends Model
     protected $append = [
 
     ];
-    
 
-    
+
+    protected static function init()
+    {
+        self::afterUpdate(function ($row) {
+            self::refreshFile();
+        });
+
+        self::afterInsert(function ($row){
+            self::refreshFile();
+        });
+
+        self::afterDelete(function ($row){
+            self::refreshFile();
+        });
+    }
+
+    //刷新文件缓存
+    public static function refreshFile()
+    {
+        $config = [];
+        $configList = self::all();
+        foreach ($configList as $k => $v) {
+            $value = $v->toArray();
+            $config[] = $value['keyworld'];
+        }
+
+        file_put_contents(
+            CONF_PATH . 'extra' . DS . 'keyworld.php',
+            '<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
+        );
+        return true;
+    }
 
 
 

+ 12 - 0
application/extra/keyworld.php

@@ -0,0 +1,12 @@
+<?php
+
+return [
+    '习近平',
+    '毛泽东',
+    '邓小平',
+    '江泽民',
+    '胡锦涛',
+    '共产党',
+    '张三',
+    '社会主义核心价值观',
+];