rkljw 3 viikkoa sitten
vanhempi
sitoutus
8fbeaf6eed

+ 28 - 0
app/Controller/PageController.php

@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Controller;
+
+use Hyperf\HttpServer\Annotation\Controller;
+use Hyperf\HttpServer\Annotation\GetMapping;
+use Hyperf\View\RenderInterface;
+
+//#[Controller(prefix: "page")]
+class PageController
+{
+//    #[GetMapping(path: "home")]
+    public function index(RenderInterface $render)
+    {
+        var_dump("======###");
+
+        return $render->render('index', [
+            'title' => '页面标题',
+            'shared_data' => [
+                'app_name' => 'My App',
+                'current_year' => date('Y')
+            ],
+            // 其他页面特定变量...
+        ]);
+    }
+}

+ 0 - 95
app/Middleware/Auth/PublicMiddleware - 副本.php

@@ -1,95 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace App\Middleware\Auth;
-
-use Hyperf\Di\Annotation\Inject;
-use Hyperf\HttpServer\Contract\RequestInterface;
-use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
-use Psr\Container\ContainerInterface;
-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Server\MiddlewareInterface;
-use Psr\Http\Server\RequestHandlerInterface;
-use Hyperf\Context\Context;
-use App\JsonRpc\WebsiteServiceInterface;
-use Phper666\JWTAuth\JWT;
-class PublicMiddleware implements MiddlewareInterface
-{
-    protected ContainerInterface $container;
-
-    protected RequestInterface $request;
-
-    protected HttpResponse $response;
-    protected JWT $JWT;
-    /**
-     * @var WebsiteServiceInterface
-     */
-    #[Inject]
-    private $websiteServiceClient;
-    public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request,Jwt $JWT)
-    {
-        $this->container = $container;
-        $this->response = $response;
-        $this->request = $request;
-        $this->JWT = $JWT;
-
-    }
-
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
-    {
-
-        $header = $request->getHeaders();
-        try {
-          
-            if($header &&  isset($header['userurl']) && $header['userurl'][0]!='null' && isset($header['userurl'][0])){
-                $origin = $header['userurl'][0];
-                $data = [
-                    'website_url'=>$origin
-                ];
-
-                $result = $this->websiteServiceClient->getWebsiteId($data);
-                if(!isset($result['data']['id']) || !$result['data']['id']){
-                    return $this->response->json(
-                        [
-                            'code' => -1,
-                            'data' => [],
-
-                            'message' => '网站不存在...',
-                        ]
-                    );
-                }
-//                var_dump($result['data']);
-                // var_dump("获取站点id:",$result);
-                Context::set("SiteId",$result['data']['id']);
-                if($result['data']['id']==1){
-                    Context::set("SiteId",0);
-                }
-
-                if ($result) {
-                    return $handler->handle($request);
-                }
-            }else{
-                return $this->response->json(
-                    [
-                        'code' => -1,
-                        'data' => [],
-                        'message' => 'userurl:必填',
-                    ]
-                );
-            }
-
-        }catch (\Exception $e){
-//            var_dump("错误消息:",$e->getMessage(),$e->getCode());
-            return $this->response->json(
-                [
-                    'code' => $e->getCode(),
-                    'data' => [],
-                    'message' => 'userurl必填:'.$e->getMessage(),
-                ]
-            );
-        }
-        return false;
-    }
-}

+ 30 - 0
app/Middleware/ViewShareMiddleware.php

@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Middleware;
+
+use Hyperf\View\RenderInterface;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+class ViewShareMiddleware implements MiddlewareInterface
+{
+    public function __construct(protected RenderInterface $render)
+    {
+    }
+
+    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+    {
+        // 通过 RenderInterface 共享变量
+        $this->render->assign([
+            'app_name' => 'Hyperf-Smarty-Demo',
+            'app_version' => '3.1.0',
+            'current_year' => date('Y'),
+        ]);
+
+        return $handler->handle($request);
+    }
+}

+ 16 - 0
app/View/Composers/GlobalComposer.php

@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\View\Composers;
+
+use Hyperf\View\Contract\EngineInterface;
+
+class GlobalComposer
+{
+    public function compose(EngineInterface $engine)
+    {
+        $engine->share('app_name', 'My Hyperf App');
+        $engine->share('current_year', date('Y'));
+    }
+}

+ 58 - 0
app/View/Engine/SmartyEngine.php

@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\View\Engine;
+
+use Hyperf\View\Engine\EngineInterface;
+use Smarty;
+use RuntimeException;
+
+class SmartyEngine implements EngineInterface
+{
+    protected Smarty $smarty;
+    protected array $shared = [];
+
+    public function __construct()
+    {
+        $this->smarty = new Smarty();
+
+        // 基础配置
+        $this->smarty->setLeftDelimiter('{{');
+        $this->smarty->setRightDelimiter('}}');
+        $this->smarty->setCompileDir(BASE_PATH . '/runtime/smarty/compile/');
+        $this->smarty->setCacheDir(BASE_PATH . '/runtime/smarty/cache/');
+        $this->smarty->setTemplateDir(BASE_PATH . '/storage/view/');
+
+        $this->ensureDirectoryExists($this->smarty->getCompileDir());
+        $this->ensureDirectoryExists($this->smarty->getCacheDir());
+    }
+
+    protected function ensureDirectoryExists(string $path): void
+    {
+        if (!is_dir($path)) {
+            mkdir($path, 0755, true);
+        }
+    }
+
+    public function share(string $key, $value): void
+    {
+        $this->shared[$key] = $value;
+    }
+
+    public function render(string $template, array $data, array $config): string
+    {
+        // 合并共享变量
+        $data = array_merge($this->shared, $data);
+
+        foreach ($data as $key => $value) {
+            $this->smarty->assign($key, $value);
+        }
+
+        try {
+            return $this->smarty->fetch($template);
+        } catch (\SmartyException $e) {
+            throw new RuntimeException("Smarty render failed: " . $e->getMessage());
+        }
+    }
+}

+ 5 - 1
composer.json

@@ -44,11 +44,15 @@
         "hyperf/service-governance-consul": "^3.1",
         "hyperf/service-governance-nacos": "^3.1",
         "hyperf/snowflake": "^3.1",
+        "hyperf/task": "^3.1",
         "hyperf/translation": "^3.1",
         "hyperf/validation": "^3.1",
+        "hyperf/view": "^3.1",
+        "hyperf/view-engine": "^3.1",
         "hyperf/websocket-server": "^3.1",
         "openai-php/client": "^0.10.3",
-        "phper666/jwt-auth": "^4.0"
+        "phper666/jwt-auth": "^4.0",
+        "smarty/smarty": "^5.4"
     },
     "require-dev": {
         "friendsofphp/php-cs-fixer": "^3.0",

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 358 - 139
composer.lock


+ 0 - 15
config/autoload/dependencies.php

@@ -1,15 +0,0 @@
-<?php
-
-declare(strict_types=1);
-/**
- * This file is part of Hyperf.
- *
- * @link     https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact  group@hyperf.io
- * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
-return [
-
-];
-

+ 1 - 0
config/autoload/middlewares.php

@@ -4,6 +4,7 @@ return [
         \App\Middleware\CorsMiddleware::class,
 //        \App\Middleware\Auth\SensitiveMiddleware::class,
         \Hyperf\Validation\Middleware\ValidationMiddleware::class,
+//        App\Middleware\ViewShareMiddleware::class,
     ],
     'ws' => [
         \App\Middleware\WebSocketAuthMiddleware::class

+ 6 - 0
config/autoload/providers.php

@@ -0,0 +1,6 @@
+<?php
+declare(strict_types=1);
+return [
+    // 这里添加你的服务提供者类名
+
+];

+ 29 - 0
config/autoload/view.php

@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+
+return [
+    'engine' => \Hyperf\ViewEngine\HyperfViewEngine::class,
+    'mode' => \Hyperf\View\Mode::TASK,
+    'config' => [
+        'view_path' => BASE_PATH . '/storage/view/',
+        'cache_path' => BASE_PATH . '/runtime/view/',
+    ],
+    'components' => [
+        'smarty' => [
+            'class' => \App\View\Engine\SmartyEngine::class,
+            'constructor' => [
+                'options' => [
+                    'left_delimiter' => '{{',
+                    'right_delimiter' => '}}',
+                    'compile_dir' => BASE_PATH . '/runtime/smarty/compile/',
+                    'cache_dir' => BASE_PATH . '/runtime/smarty/cache/',
+                    'template_dir' => BASE_PATH . '/storage/view/',
+                ],
+            ],
+        ],
+    ],
+    'composers' => [
+        \App\View\Composers\GlobalComposer::class,
+    ],
+];

+ 1 - 0
config/routes.php

@@ -12,6 +12,7 @@ declare (strict_types = 1);
 use Hyperf\HttpServer\Router\Router;
 
 Router::addRoute(['GET', 'POST', 'HEAD'], '/index', 'App\Controller\IndexController@index');
+Router::addRoute(['GET', 'POST', 'HEAD'], '/demo/index', 'App\Controller\PageController@index');
 Router::addRoute(['GET', 'POST', 'HEAD'], '/download', 'App\Controller\IndexController@download');
 Router::get('/api/getData', 'App\Controller\LoginController@getData');
 

+ 16 - 0
storage/view/index.tpl

@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>{{$page_title}} - {{$app_name}}</title>
+</head>
+<body>
+<h1>欢迎, {{$user.name}}</h1>
+
+<div class="info">
+    <p>当前年份: {{$current_year}}</p>
+    <p>版本号: {{$version}}</p>
+    <p>请求处理时间: {{$request_time|number_format:4}}秒</p>
+    <p>客户端IP: {{$request->getAttribute('client_ip')}}</p>
+</div>
+</body>
+</html>

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä