LiuJ 1 month ago
parent
commit
6b5b410515

+ 12 - 9
app/Constants/ErrorCode.php

@@ -1,25 +1,28 @@
 <?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
- */
 
 namespace App\Constants;
 
 use Hyperf\Constants\AbstractConstants;
 use Hyperf\Constants\Annotation\Constants;
 
+/**
+ * @Constants
+ */
 #[Constants]
 class ErrorCode extends AbstractConstants
 {
     /**
      * @Message("Server Error!")
      */
-    public const SERVER_ERROR = 500;
+    const SERVER_ERROR = 500;
+    /**
+     * @Message("success")
+     */
+    public const SUCCESS = 200;
+    /**
+     * @Message("error")
+     */
+    public const ERROR = 0;
 }

+ 279 - 1
app/JsonRpc/ClientService.php

@@ -10,10 +10,12 @@ use App\Model\Complaint;
 use App\Model\JobEnum;
 use App\Model\User;
 use App\Model\webisitecategory;
+use App\Model\WebsiteTemplate;
 use Hyperf\Coroutine\Coroutine;
 use Hyperf\Redis\Redis;
 use Hyperf\Di\Annotation\Inject;
-
+use function Hyperf\Support\env;
+use Symfony\Component\Filesystem\Filesystem;
 
 #[RpcService(name: "ClientService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 
@@ -751,4 +753,280 @@ class ClientService implements ClientServiceInterface
         $templateData['template']['index'][0]['componentList'] = $componentList;
         return $templateData;
     }
+    public function addWeb($data)
+    {
+        $result = WebsiteTemplate::insertGetId($data);
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('添加失败');
+        }
+    }
+    public function deleteWeb($data)
+    {
+        $result = WebsiteTemplate::where('id', $data['id'])->delete();
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('删除失败');
+        }
+    }
+    public function updateWeb($data)
+    {
+        $result = WebsiteTemplate::where('id', $data['id'])->update($data);
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('更新失败');
+        }
+    }
+    public function getWebInfo($data)
+    {
+        $result = WebsiteTemplate::where('website_template.id', $data['id'])
+            ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
+            ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
+            ->first();
+        return Result::success($result);
+    }
+    public function getWebList($data)
+    {
+        var_dump($data, 'data');
+        $where = [];
+        if (isset($data['website_name'])) {
+            $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
+        }
+        if (isset($data['status'])) {
+            $where[] = ['website_template.status', '=', $data['status']];
+        }
+        $result = WebsiteTemplate::where([])
+            ->where($where)
+            ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
+            ->select('website_template.*', 'website.website_name as website_name', 'website.website_url as website_url')
+            ->get();
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('获取列表失败');
+        }
+    }
+    public function updateWebConfig($data)
+    {
+        // 先根据
+        // 根据是否测试环境配置不同 的vue项目的 配置文件
+        //获取根目录下  yuan  源代码,开始处理
+        //获取目前目录,获取根目录
+        $root = dirname(dirname(__DIR__));
+        $yuan = $root . '/yuan';
+        //取出envAPP_ENV=dev
+        $APP_ENV = env('APP_ENV');
+        switch ($APP_ENV) {
+            case 'dev':
+                $file = $yuan . '/plugins/category.ts';
+                break;
+            case 'prod':
+                $file = $yuan . '/plugins/category.ts';
+                break;
+            default:
+                $file = $yuan . '/plugins/category.ts';
+        }
+        //处理文件plugins\category.ts
+        //找到 export default defineNuxtPlugin((nuxtApp) => {,处理到文件结尾
+        /*
+        export default defineNuxtPlugin((nuxtApp) => {
+        //master 环境
+        nuxtApp.provide('pageNav', navMaster)
+        //pre 环境
+        // nuxtApp.provide('pageNav', navPre)
+        }) 
+        */
+        if (0 == 1) {
+            $content = file_get_contents($file);
+            $content = str_replace("nuxtApp.provide('pageNav', navPre)", "//nuxtApp.provide('pageNav', navPre)", $content);
+            $content = str_replace("//nuxtApp.provide('pageNav', navMaster)", "nuxtApp.provide('pageNav', navMaster)", $content);
+            //写入文件
+            $res = file_put_contents($file, $content);
+            var_dump('已经处理/plugins/category.ts');
+            //处理文件\plugins\globals.js
+            $file = $yuan . '/plugins/globals.js';
+            $content = file_get_contents($file);
+            // 1️⃣ 将没有注释的 nuxtApp.provide(...) 行加上注释
+            $content = preg_replace('/^(?!\s*\/\/)(?=.*nuxtApp\.provide)/m', '// ', $content);
+
+            // 2️⃣ 找到 "//乡村网正式环境" 下面的注释行并取消注释
+            $lines = explode("\n", $content);
+            $output = [];
+            $foundSection = false;
+
+            foreach ($lines as $line) {
+                // 判断是否进入目标区域
+                if (preg_match('/\/\/乡村网正式环境/', $line)) {
+                    $foundSection = true;
+                    $output[] = $line;
+                    continue;
+                }
+
+                // 在目标区域内,去掉注释
+                if ($foundSection && preg_match('/^\s*\/\/\s*(nuxtApp\.provide)/', $line, $matches)) {
+                    $output[] = preg_replace('/^\s*\/\/\s*/', '', $line, 1);
+                } else {
+                    // 如果遇到下一个注释块或空白行,结束当前处理
+                    if ($foundSection && preg_match('/^\s*$/', $line)) {
+                        $foundSection = false;
+                    }
+                    $output[] = $line;
+                }
+            }
+
+            // 合并回字符串
+            $newContent = implode("\n", $output);
+            // 写入文件
+            $res1 =  file_put_contents($file, $newContent);
+            var_dump($res1, '已经处理/plugins/globals.js');
+
+            //处理文件\plugins\request.js
+            $file = $yuan . '/plugins/request.js';
+            $content = file_get_contents($file);
+            $lines = explode("\n", $content);
+
+            $in_prod = false;
+            $in_pre = false;
+
+            for ($i = 0; $i < count($lines); $i++) {
+                $line = $lines[$i];
+
+                // 匹配 "//正式环境"
+                if (preg_match('/\/\/正式环境/', $line)) {
+                    $in_prod = true;
+                    $in_pre = false;
+                    continue;
+                }
+
+                // 匹配 "//pre环境"
+                if (preg_match('/\/\/pre环境/', $line)) {
+                    $in_pre = true;
+                    $in_prod = false;
+                    continue;
+                }
+
+                // 处理正式环境下的两行:去注释
+                if ($in_prod && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
+                    for ($j = $i + 0; $j <= $i + 1; $j++) {
+                        if (isset($lines[$j])) {
+                            $lines[$j] = preg_replace('/^\s*\/\/\s*/', '', $lines[$j]);
+                        }
+                    }
+                    $in_prod = false; // 只处理一次
+                }
+
+                // 处理 pre 环境下的两行:添加注释
+                if ($in_pre && $i + 0 <= count($lines) && $i + 1 <= count($lines)) {
+                    for ($j = $i + 1; $j <= $i + 2; $j++) {
+                        if (isset($lines[$j]) && !preg_match('/^\s*\/\//', $lines[$j])) {
+                            // $lines[$j] = '// ' . $lines[$j];
+                        }
+                    }
+                    $in_pre = false; // 只处理一次
+                }
+            }
+
+            // 写回文件
+            $res2 = file_put_contents($file, implode("\n", $lines));
+            var_dump($res2, '已经处理/plugins/request.js');
+            echo "✅ 环境配置切换完成!";
+
+
+            //第三步 定位到yuan目录,执行 npm run build   生成output目录
+            chdir($yuan);
+            $APP_ENV = env('APP_ENV');
+            $res3 = exec("npm run build");
+            var_dump($res, '执行 npm run build');
+            echo "✅ 构建完成!";
+
+            //第四步
+            //修改 .output\server\chunks\routes\index.mjs ,
+            // 根据网站的id的port更改端口
+            //第五步
+            //x
+
+
+            $file4 = $yuan . '/.output/server/chunks/routes/index.mjs';
+            $content = file_get_contents($file4);
+            // 使用正则将 "destr(...) || 3e3" 替换为 "destr(...) || 9000"
+            $content = preg_replace(
+                '/(destr$[^)]+$$\s*\|\|\s*)3e3/i',
+                '$19000',
+                $content
+            );
+
+            // 写回文件
+            file_put_contents($file, $content);
+        }
+        //第六步  yuan压缩成压缩包,复制到/hyperf-skeleton/web1/,解压
+        $dir_yuan = $yuan . '/.output/';
+        // 移动到web  没有就创建
+        $dir_web = '/hyperf-skeleton/' . '/web1/';
+        if (!is_dir($dir_web)) {
+            mkdir($dir_web, 0755, true);
+        }
+        $fs =  new Filesystem();
+
+        $fs->rename($dir_yuan, $dir_web . 'output', true);
+        var_dump('已经移动到/hyperf-skeleton/web1/output');
+        $result = [
+            'res' => $res,
+            'res1' => $res1,
+            'res2' => $res2,
+            'res3' => $res3,
+
+            'root' => $root,
+            'yuan' => $yuan,
+            'APP_ENV' => $APP_ENV,
+            'website_id' => $data['website_id'],
+        ];
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('更新配置失败');
+        }
+    }
+    public function updateWebOutput($data)
+    {
+        //根据源程序, 复制出来,更改配置文件,打包, 
+        $result = WebsiteTemplate::where('id', $data['id'])->update($data);
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('更新输出失败');
+        }
+    }
+    public function updateWebMove($data)
+    {
+
+        //更改到dir目录,
+        $result = WebsiteTemplate::where('id', $data['id'])->update($data);
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('更新移动失败');
+        }
+    }
+    public function runWeb($data)
+    {
+        //运行网站, 先检查是否有配置文件, pm2启动
+        $result = WebsiteTemplate::where('id', $data['id'])->update($data);
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('运行失败');
+        }
+    }
+    public function stopWeb($data)
+    {
+        $result = WebsiteTemplate::where('id', $data['id'])->update($data);
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('停止失败');
+        }
+    }
 }

+ 15 - 2
app/JsonRpc/ClientServiceInterface.php

@@ -4,6 +4,19 @@ namespace App\JsonRpc;
 
 interface ClientServiceInterface
 {
-    public function test(array $params): array;
-    public function indexData(array $params);
+    /**
+     * @param array $data
+     */
+    public function test(array $data);
+    public function indexData(array $data);
+    public function addWeb(array $data);
+    public function getWebInfo(array $data);
+    public function deleteWeb(array $data);
+    public function updateWeb(array $data);
+    public function getWebList(array $data);
+    public function updateWebConfig(array $data);
+    public function updateWebOutput(array $data);
+    public function updateWebMove(array $data);
+    public function runWeb(array $data);
+    public function stopWeb(array $data);
 }

+ 19 - 0
app/Model/Model.php

@@ -0,0 +1,19 @@
+<?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
+ */
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model as BaseModel;
+
+abstract class Model extends BaseModel
+{
+}

+ 27 - 0
app/Model/WebsiteTemplate.php

@@ -0,0 +1,27 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class WebsiteTemplate extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'website_template';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}

+ 54 - 0
app/Tools/PublicData.php

@@ -0,0 +1,54 @@
+<?php
+namespace App\Tools;
+use App\Constants\ErrorCode;
+use Hyperf\Snowflake\IdGeneratorInterface;
+use Hyperf\Context\ApplicationContext;
+class PublicData
+{
+    /**
+     * 递归查询
+     * @param $menuItems
+     * @param $parentId
+     * @return array
+     */
+    public static  function buildMenuTree($menuItems, $parentId = 0) {
+        $tree = [];
+        foreach ($menuItems as $item) {
+            if ($item['pid'] == $parentId) {
+                // 找到子菜单
+                $children = self::buildMenuTree($menuItems, $item['id']);
+                // 如果子菜单存在,则添加到当前菜单的children中
+                if ($children) {
+                    $item['children'] = $children;
+                }
+                // 将当前菜单添加到树中
+                $tree[] = $item;
+            }
+        }
+        return $tree;
+    }
+
+    /**
+     * 计算两个时间差的天数
+     * @param $sTime
+     * @param $eTime
+     * @return float
+     */
+    public static function residueDay($sTime,$eTime)
+    {
+        $startTime = strtotime($sTime);
+        $endTime = strtotime($eTime);
+        $diffDays = floor(($endTime - $startTime) / (60 * 60 * 24));
+        return $diffDays;
+    }
+
+    public static function uuid()
+    {
+        var_dump("uuid==========");
+        $container = ApplicationContext::getContainer();
+        $generator = $container->get(IdGeneratorInterface::class);
+        var_dump("uuid==+++++++++++",$generator->generate());
+        return $generator->generate();
+    }
+
+}

+ 26 - 0
app/Tools/Result.php

@@ -0,0 +1,26 @@
+<?php
+namespace App\Tools;
+use App\Constants\ErrorCode;
+class Result
+{
+    public static function success($data = [])
+    {
+        return static::result(ErrorCode::SUCCESS, ErrorCode::getMessage(ErrorCode::SUCCESS), $data);
+    }
+    public static function error($message = '', $code = ErrorCode::ERROR, $data = [])
+    {
+        if (empty($message)) {
+            return static::result($code, ErrorCode::getMessage($code), $data);
+        } else {
+            return static::result($code, $message, $data);
+        }
+    }
+    protected static function result($code, $message, $data)
+    {
+        return [
+            'code' => $code,
+            'message' => $message,
+            'data' => $data,
+        ];
+    }
+}

+ 2 - 1
composer.json

@@ -33,7 +33,8 @@
         "hyperf/rpc-client": "~3.1.0",
         "hyperf/rpc-server": "~3.1.0",
         "hyperf/service-governance": "~3.1.0",
-        "hyperf/tracer": "^3.1"
+        "hyperf/tracer": "^3.1",
+        "symfony/filesystem": "^6.4"
     },
     "require-dev": {
         "friendsofphp/php-cs-fixer": "^3.0",

+ 67 - 67
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "c019bda3e4157a797b2c2ba6ac97fb5d",
+    "content-hash": "8fa15d6f0ec5a73e749e70d69618faf9",
     "packages": [
         {
             "name": "carbonphp/carbon-doctrine-types",
@@ -5513,6 +5513,72 @@
             ],
             "time": "2024-09-25T14:21:43+00:00"
         },
+        {
+            "name": "symfony/filesystem",
+            "version": "v6.4.13",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/filesystem.git",
+                "reference": "4856c9cf585d5a0313d8d35afd681a526f038dd3"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/filesystem/zipball/4856c9cf585d5a0313d8d35afd681a526f038dd3",
+                "reference": "4856c9cf585d5a0313d8d35afd681a526f038dd3",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=8.1",
+                "symfony/polyfill-ctype": "~1.8",
+                "symfony/polyfill-mbstring": "~1.8"
+            },
+            "require-dev": {
+                "symfony/process": "^5.4|^6.4|^7.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Component\\Filesystem\\": ""
+                },
+                "exclude-from-classmap": [
+                    "/Tests/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Fabien Potencier",
+                    "email": "fabien@symfony.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Provides basic utilities for the filesystem",
+            "homepage": "https://symfony.com",
+            "support": {
+                "source": "https://github.com/symfony/filesystem/tree/v6.4.13"
+            },
+            "funding": [
+                {
+                    "url": "https://symfony.com/sponsor",
+                    "type": "custom"
+                },
+                {
+                    "url": "https://github.com/fabpot",
+                    "type": "github"
+                },
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2024-10-25T15:07:50+00:00"
+        },
         {
             "name": "symfony/finder",
             "version": "v6.4.17",
@@ -9541,72 +9607,6 @@
             ],
             "time": "2024-09-25T14:21:43+00:00"
         },
-        {
-            "name": "symfony/filesystem",
-            "version": "v6.4.13",
-            "source": {
-                "type": "git",
-                "url": "https://github.com/symfony/filesystem.git",
-                "reference": "4856c9cf585d5a0313d8d35afd681a526f038dd3"
-            },
-            "dist": {
-                "type": "zip",
-                "url": "https://api.github.com/repos/symfony/filesystem/zipball/4856c9cf585d5a0313d8d35afd681a526f038dd3",
-                "reference": "4856c9cf585d5a0313d8d35afd681a526f038dd3",
-                "shasum": ""
-            },
-            "require": {
-                "php": ">=8.1",
-                "symfony/polyfill-ctype": "~1.8",
-                "symfony/polyfill-mbstring": "~1.8"
-            },
-            "require-dev": {
-                "symfony/process": "^5.4|^6.4|^7.0"
-            },
-            "type": "library",
-            "autoload": {
-                "psr-4": {
-                    "Symfony\\Component\\Filesystem\\": ""
-                },
-                "exclude-from-classmap": [
-                    "/Tests/"
-                ]
-            },
-            "notification-url": "https://packagist.org/downloads/",
-            "license": [
-                "MIT"
-            ],
-            "authors": [
-                {
-                    "name": "Fabien Potencier",
-                    "email": "fabien@symfony.com"
-                },
-                {
-                    "name": "Symfony Community",
-                    "homepage": "https://symfony.com/contributors"
-                }
-            ],
-            "description": "Provides basic utilities for the filesystem",
-            "homepage": "https://symfony.com",
-            "support": {
-                "source": "https://github.com/symfony/filesystem/tree/v6.4.13"
-            },
-            "funding": [
-                {
-                    "url": "https://symfony.com/sponsor",
-                    "type": "custom"
-                },
-                {
-                    "url": "https://github.com/fabpot",
-                    "type": "github"
-                },
-                {
-                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
-                    "type": "tidelift"
-                }
-            ],
-            "time": "2024-10-25T15:07:50+00:00"
-        },
         {
             "name": "symfony/http-foundation",
             "version": "v6.4.21",