From ba168ff55cd5828c94daf205440c7e3eba31a2a3 Mon Sep 17 00:00:00 2001 From: Default Date: Mon, 7 Apr 2025 20:12:34 +0800 Subject: [PATCH] debug --- code/app/admin/controller/Site.php | 12 +++- code/app/model/AutoSwitchViewModel.php | 19 ++++-- code/app/model/DomainModel.php | 73 +++++++++++++++------ code/app/model/SubjectFomartModel.php | 39 +++++++++++ code/app/model/TemplatesModel.php | 56 +++++++++++----- code/extend/template/ziku/WordsEntities.php | 1 - 6 files changed, 159 insertions(+), 41 deletions(-) diff --git a/code/app/admin/controller/Site.php b/code/app/admin/controller/Site.php index cc28a91..d2a2862 100644 --- a/code/app/admin/controller/Site.php +++ b/code/app/admin/controller/Site.php @@ -78,10 +78,13 @@ class Site extends BaseController $arrDomain['t_cfg'] = json_encode($TCfgTemplate, JSON_UNESCAPED_UNICODE); + /** @var DomainModel **/ $DomainModel = DomainModel::where('d_domain', $arrDomain['d_domain'])->findOrEmpty(); $DomainModel->fill($arrDomain); $DomainModel->save(); + + DomainModel::flushAllDomianOnCache(); } @@ -179,6 +182,7 @@ class Site extends BaseController $this->validate($arrData, $arrRule); if ($arrData['d_id']) { + /** @var DomainModel **/ $DomainModel = DomainModel::where('d_id', $arrData['d_id'])->findOrEmpty(); } else { $DomainModel = new DomainModel; @@ -187,6 +191,8 @@ class Site extends BaseController $DomainModel->fill($arrData); $DomainModel->save(); + DomainModel::flushAllDomianOnCache(); + return $this->success(); } @@ -347,13 +353,15 @@ class Site extends BaseController SubjectFomartGroupModel::whereIn('sfg_id', $arrSfgId)->delete(); + SubjectFomartModel::whereIn('sfg_id', $arrSfgId)->delete(); + return $this->success(); } - /** + /** * 替换模板分组列表 * * @param Request $Request @@ -387,7 +395,7 @@ class Site extends BaseController }); } if (!empty($arrData['sfg_id'])) { - $SubjectFomartModel->where('sf.sfg_id',$arrData['sfg_id']); + $SubjectFomartModel->where('sf.sfg_id', $arrData['sfg_id']); } $Paginate = $SubjectFomartModel->paginate([ diff --git a/code/app/model/AutoSwitchViewModel.php b/code/app/model/AutoSwitchViewModel.php index 57a0ac2..aa86cab 100644 --- a/code/app/model/AutoSwitchViewModel.php +++ b/code/app/model/AutoSwitchViewModel.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace app\model; use think\Request; +use think\exception\HttpException; class AutoSwitchViewModel { @@ -14,14 +15,24 @@ class AutoSwitchViewModel $strDomain = $Request->rootDomain(); - // var_dump($strDomain); + $DomainModel = DomainModel::getDomainOnCacheByDomain($strDomain); - $DomainModel = DomainModel::getDomainByCache($strDomain); + if (empty($DomainModel)) { + throw new HttpException(404, 'Site config not Found'); + } - $TemplatesModel = TemplatesModel::getTemplateOnCache($DomainModel->t_id); + // echo $DomainModel->getFomartSubject('NV_SHENG_URL'); + + // exit; + + $TemplatesModel = TemplatesModel::getTemplateOnCacheByTId($DomainModel->t_id); + + if (empty($DomainModel)) { + throw new HttpException(500, 'Site template not Found'); + } $Request->DomainModel = $DomainModel; - + $Request->TemplatesModel = $TemplatesModel; return [ diff --git a/code/app/model/DomainModel.php b/code/app/model/DomainModel.php index 7801458..5157848 100644 --- a/code/app/model/DomainModel.php +++ b/code/app/model/DomainModel.php @@ -17,20 +17,57 @@ class DomainModel extends BaseModel protected $json = ['t_cfg']; + static public $arrAllDomain = []; - static public function getDomainByCache($strDomain) + /** + * Undocumented function + * + * @return array + */ + static public function getAllDomainOnCache(): array { - $strKey = "D:" . $strDomain; - return DomainModel::where('d_domain', $strDomain)->cache($strKey, 24 * 3600)->find(); + if (empty(self::$arrAllDomain)) { + $strKey = 'Domain:All'; + self::$arrAllDomain = Cache::get($strKey); + if (empty(self::$arrAllDomain)) { + self::flushAllDomianOnCache(); + } + } + return self::$arrAllDomain; } - static public function flushDomainCache($strDomain) + /** + * Undocumented function + * + * @return void + */ + static public function flushAllDomianOnCache() { - $strKey = "D:" . $strDomain; - return Cache::delete($strKey); + + self::$arrAllDomain = []; + $strKey = 'Domain:All'; + $DomainModelAll = DomainModel::select(); + foreach ($DomainModelAll as $DomainModel) { + self::$arrAllDomain[$DomainModel->d_domain] = $DomainModel; + } + + return Cache::set($strKey, self::$arrAllDomain); } + /** + * Undocumented function + * + * @param string $strDomain + * @return DomainModel|NULL + */ + static public function getDomainOnCacheByDomain($strDomain): DomainModel|NULL + { + if (key_exists($strDomain, self::getAllDomainOnCache())) { + return self::getAllDomainOnCache()[$strDomain]; + } + return NULL; + } /** * Undocumented function @@ -59,19 +96,20 @@ class DomainModel extends BaseModel */ public function getFomartSubject($strKey) //, $intNum = 1, $boolRand = false) { - // echo $strKey; - // exit; - // echo '
';
-        // var_dump($this->t_cfg);
-        // exit;
         $intSfgId = $this->t_cfg->{$strKey}['sfg_id'];
-        // $SubjectFomartCol = SubjectFomartModel::where('sfg_id', $intSfgId)->select();
 
-        $SubjectFomartModel =  SubjectFomartModel::where('sfg_id', $intSfgId)->select()->first();
-        // var_dump($SubjectFomartModel->toArray());
+        // $SubjectFomartModel = SubjectFomartModel::where('sfg_id', $intSfgId)->select()->first();
+        $arrAllSubjectFomartModel = SubjectFomartModel::getAllSubjectFomartOnCacheBySfgId($intSfgId);
+
+        $SubjectFomartModel =  (is_array($arrAllSubjectFomartModel) && !empty($arrAllSubjectFomartModel)) ? reset($arrAllSubjectFomartModel) : null;
+
+        return empty($SubjectFomartModel) ? '' : $SubjectFomartModel->sf_val;
+
+        // echo '
';
+        // print_r($SubjectFomartModel);
         // exit;
 
-        return  $SubjectFomartModel->sf_val;
+        // return  $SubjectFomartModel->sf_val;
     }
 
 
@@ -79,14 +117,11 @@ class DomainModel extends BaseModel
      * Undocumented function
      *
      * @param array $arrArgs
-     * @return void
+     * @return string
      */
     static public function getPageUrl($strKey, array $arrArgs): string
     {
         $Request = request();
-        // $strKey = $Request->controller(false, true) . '@' .
-        //     $Request->action(true) . '@URL';
-        // $strKey = strtoupper($strKey);
 
         $strSfVal =  $Request->DomainModel->getFomartSubject($strKey);
 
diff --git a/code/app/model/SubjectFomartModel.php b/code/app/model/SubjectFomartModel.php
index 9ed9f7c..4676a1c 100644
--- a/code/app/model/SubjectFomartModel.php
+++ b/code/app/model/SubjectFomartModel.php
@@ -14,4 +14,43 @@ class SubjectFomartModel extends BaseModel
 {
     protected $name = 'subject_fomart';
     protected $pk = 'sf_id';
+
+    static public $arrAllSubjectFomart = [];
+
+    /**
+     * Undocumented function
+     *
+     * @param integer $intSfgId
+     * @return SubjectFomartModel[]
+     */
+    static public function getAllSubjectFomartOnCacheBySfgId(int $intSfgId): array
+    {
+        if (empty(self::$arrAllSubjectFomart)) {
+            $strKey = 'SubjectFomart:All';
+            self::$arrAllSubjectFomart = Cache::get($strKey);
+            if (empty(self::$arrAllSubjectFomart)) {
+                self::flushAllSubjectFomartOnCache();
+            }
+        }
+
+        return self::$arrAllSubjectFomart[$intSfgId] ?? [];
+    }
+
+    /**
+     * Undocumented function
+     *
+     * @return void
+     */
+    static public function flushAllSubjectFomartOnCache()
+    {
+        self::$arrAllSubjectFomart = [];
+        $strKey = 'SubjectFomart:All';
+        $SubjectFomartModelAll = SubjectFomartModel::select();
+        foreach ($SubjectFomartModelAll as $SubjectFomartModel) {
+            self::$arrAllSubjectFomart[$SubjectFomartModel->sfg_id][] = $SubjectFomartModel;
+        }
+
+        return Cache::set($strKey, self::$arrAllSubjectFomart);
+    }
+
 }
diff --git a/code/app/model/TemplatesModel.php b/code/app/model/TemplatesModel.php
index 9b6199c..a562fd6 100644
--- a/code/app/model/TemplatesModel.php
+++ b/code/app/model/TemplatesModel.php
@@ -32,29 +32,55 @@ class TemplatesModel extends BaseModel
     //     }
     // }
 
+    static public $arrAllTemplate = [];
 
-    static $arrTemplates = [];
-
-    static public function getTemplateOnCache($intTId)
+    /**
+     * Undocumented function
+     *
+     * @return array
+     */
+    static public function getAllTemplateOnCache(): array
     {
-        if (self::$arrTemplates == []) {
-            self::$arrTemplates =  Cache::get('Templates');
-
-            if (empty(self::$arrTemplates)) {
-                self::flushCache();
-                self::$arrTemplates =  Cache::get('Templates');
+        if (empty(self::$arrAllTemplate)) {
+            $strKey = 'Template:All';
+            self::$arrAllTemplate = Cache::get($strKey);
+            if (empty(self::$arrAllTemplate)) {
+                self::flushAllTemplateOnCache();
             }
         }
-
-        return  self::$arrTemplates[$intTId] ?? NULL;
+        return  self::$arrAllTemplate;
     }
 
-    static public function flushCache()
+    /**
+     * Undocumented function
+     *
+     * @return void
+     */
+    static public function flushAllTemplateOnCache()
     {
-        $TemplatesModelAll = self::select();
 
-        $TemplatesCol = $TemplatesModelAll->column(null, 't_id');
+        self::$arrAllTemplate = [];
+        $strKey = 'Template:All';
+        $TemplateModelAll = self::select();
+        foreach ($TemplateModelAll as $TemplateModel) {
+            self::$arrAllTemplate[$TemplateModel->t_id] = $TemplateModel;
+        }
 
-        Cache::tag('Templates')->set('Templates', $TemplatesCol);
+        return Cache::set($strKey, self::$arrAllTemplate);
+    }
+
+    /**
+     * Undocumented function
+     *
+     * @param int $intTId
+     * @return TemplatesModel|NULL
+     */
+    static public function getTemplateOnCacheByTId(int $intTId): TemplatesModel|NULL
+    {
+        if (key_exists($intTId, self::getAllTemplateOnCache())) {
+            return self::getAllTemplateOnCache()[$intTId];
+        }
+
+        return NULL;
     }
 }
diff --git a/code/extend/template/ziku/WordsEntities.php b/code/extend/template/ziku/WordsEntities.php
index f08570f..a478f99 100644
--- a/code/extend/template/ziku/WordsEntities.php
+++ b/code/extend/template/ziku/WordsEntities.php
@@ -9,7 +9,6 @@ class WordsEntities
 
     static public function getWords($strWords)
     {
-
         if (!isset(self::$arrZiKu[$strWords])) {
             self::$arrZiKu[$strWords] = customEntities($strWords);
         }