Browse Source

feat:添加中间件

igb 4 months ago
parent
commit
6f12db182d
1 changed files with 19 additions and 1 deletions
  1. 19 1
      app/Http/Middleware/LoadDistData.php

+ 19 - 1
app/Http/Middleware/LoadDistData.php

@@ -6,6 +6,7 @@ use Closure;
 use Illuminate\Http\Request;
 use Symfony\Component\HttpFoundation\Response;
 use App\Helpers\SiteCache;
+use Liquid\Liquid;
 /**
  * Class LoadDistData 用中间件来获取distm网店信息
  */
@@ -23,15 +24,32 @@ class LoadDistData
         // 获取请求的域名
         $domain = getHost();
 
-
         // 先检查 custom_domain,如果没有找到,再检查 secondary_domain
         $dist=SiteCache::getDist($domain);
 
+        // 如果缓存的结果是字符串,反序列化为对象
+        if ($dist) {
+            $dist = unserialize($dist);
+        }
+
+        //dd($dist);
+//// 如果是数组或对象,直接转换为对象
+//        if (is_array($dist)) {
+//            $dist = (object) $dist; // 将数组转换为对象
+//        } elseif (is_string($dist)) {
+//            // 处理字符串情况,假设字符串是 JSON 格式,可以尝试解析
+//            $dist = json_decode($dist);
+//        }
+
         // 如果找不到匹配的数据,直接返回404响应
         if (!$dist) {
             abort(404, 'site not found.');
         }
 
+        // 全局共享  数据
+        app()->instance('dist', $dist);
+
+
         // 将找到的 dist 数据添加到请求中,方便后续使用
         $request->attributes->set('dist', $dist);