LoadDistData.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use App\Helpers\SiteCache;
  7. use Liquid\Liquid;
  8. /**
  9. * Class LoadDistData 用中间件来获取distm网店信息
  10. */
  11. class LoadDistData
  12. {
  13. /**
  14. * Handle an incoming request.
  15. *
  16. * @param
  17. */
  18. public function handle(Request $request, Closure $next): Response
  19. {
  20. // 获取请求的域名
  21. $domain = getHost();
  22. // 先检查 custom_domain,如果没有找到,再检查 secondary_domain
  23. $dist=SiteCache::getDist($domain);
  24. // 如果缓存的结果是字符串,反序列化为对象
  25. if ($dist) {
  26. $dist = unserialize($dist);
  27. }
  28. //dd($dist);
  29. //// 如果是数组或对象,直接转换为对象
  30. // if (is_array($dist)) {
  31. // $dist = (object) $dist; // 将数组转换为对象
  32. // } elseif (is_string($dist)) {
  33. // // 处理字符串情况,假设字符串是 JSON 格式,可以尝试解析
  34. // $dist = json_decode($dist);
  35. // }
  36. // 如果找不到匹配的数据,直接返回404响应
  37. if (!$dist) {
  38. abort(404, 'site not found.');
  39. }
  40. // 全局共享 数据
  41. app()->instance('dist', $dist);
  42. // 将找到的 dist 数据添加到请求中,方便后续使用
  43. $request->attributes->set('dist', $dist);
  44. //保存到共享地方供其它地方用
  45. return $next($request);
  46. }
  47. }