LiuJ 5 miesięcy temu
rodzic
commit
4b87bedd74
1 zmienionych plików z 310 dodań i 1 usunięć
  1. 310 1
      app/JsonRpc/ClientService.php

+ 310 - 1
app/JsonRpc/ClientService.php

@@ -839,6 +839,152 @@ class ClientService implements ClientServiceInterface
         }
     }
     public function updateWebConfig($data)
+    {
+        var_dump($data, 'data');
+
+        $where = [];
+
+        if (isset($data['website_id'])) {
+            $where['website_template.website_id'] = $data['website_id'];
+        }
+        $result = WebsiteTemplate::where([])
+            ->where($where)
+            ->leftJoin('website', 'website.id', '=', 'website_template.website_id')
+            ->select('website_template.id', 'website_template.port', 'website.website_name as website_name', 'website.website_url as website_url')
+            ->first()->toArray();
+        var_dump($result);
+        $domain = json_decode($result['website_url'], true);
+        $domainlist = $domain;
+
+        $domain = $domain[3];
+        if (empty($domain)) {
+            return Result::error(500, '域名不存在');
+        }
+        $port = $result['port'];
+        if (empty($port)) {
+            return Result::error(500, '端口不存在');
+        }
+        $postData = [
+            'domain' => $domain,
+            'port' => $port
+        ];
+        if (empty($result['siteId'])) {
+            //实例化对象
+            $api = new bt_api();
+            //获取面板日志
+            $r_data = $api->GetLogs();
+            //输出JSON数据到浏览器
+            echo json_encode($r_data);
+            $webname = [
+                'domain' => $domain,
+                'domainlist' => $domainlist,
+                'count' => count($domainlist) - 1
+            ];
+            $siteData = [
+                'path' => '/www2/www/wwwroot/' . $domain,
+                'ftp' => 'false',
+                'type' => 'PHP',
+                'type_id' => 0,
+                'ps' => $domain,
+                'port' => 80,
+                'version' => '00',
+                'need_index' => 0,
+                'need_404' => 0,
+                'sql' => 'false',
+                'codeing' => 'utf8mb4',
+                'webname' => json_encode($webname),
+                'add_dns_record' => 'false'
+            ];
+
+            $r_data = $api->addSite($siteData);
+            /*
+            {
+            "siteStatus": true,
+            "siteId": 20,
+            "ftpStatus": false,
+            "databaseStatus": false,
+            "gitStatus": false
+             }
+            */
+            if ($r_data['siteStatus'] == false) {
+                return Result::error('宝塔添加失败');
+            } elseif ($r_data['siteStatus'] == true) {
+                //记录siteId
+                $siteId = $r_data['siteId'];
+                //更新网站模板的siteId     WebsiteTemplate  
+                WebsiteTemplate::update(['siteId' => $siteId, 'dir' => '/www2/www/wwwroot/' . $domain], ['id' => $data['website_id']]);
+            }
+            // return Result::success($r_data);
+        }
+        // 使用Guzzle HTTP客户端发送POST请求
+        try {
+            $client = new \GuzzleHttp\Client();
+            $response = $client->post('http://adminpre.bjzxtw.org.cn/create.php', [
+                'json' => $postData,
+                'timeout' => 30,
+                'headers' => [
+                    'Content-Type' => 'application/json',
+                ]
+            ]);
+
+            // 获取响应内容
+            $responseBody = $response->getBody()->getContents();
+            $responseData = json_decode($responseBody, true);
+
+            // 检查响应是否为有效的JSON
+            if (json_last_error() === JSON_ERROR_NONE) {
+                // 如果远程服务器返回成功结果
+                if (isset($responseData['success']) && $responseData['success']) {
+                    return Result::success([
+                        'message' => '远程服务器处理成功',
+                        'domain' => $domain,
+                        'port' => $port,
+                        'remote_response' => $responseData
+                    ]);
+                } else {
+                    // 远程服务器返回错误
+                    return Result::error(500, '远程服务器处理失败', [
+                        'remote_response' => $responseData
+                    ]);
+                }
+            } else {
+                // 响应不是有效的JSON
+                return Result::success([
+                    'message' => '收到远程服务器响应(非JSON格式)',
+                    'domain' => $domain,
+                    'port' => $port,
+                    'raw_response' => $responseBody
+                ]);
+            }
+        } catch (\GuzzleHttp\Exception\RequestException $e) {
+            // HTTP请求异常
+            $errorMessage = 'HTTP请求失败';
+            if ($e->hasResponse()) {
+                $response = $e->getResponse();
+                $errorMessage .= ': ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase();
+            } else {
+                $errorMessage .= ': ' . $e->getMessage();
+            }
+
+            return Result::error(500, $errorMessage, [
+                'exception' => $e->getMessage()
+            ]);
+        } catch (\Exception $e) {
+            // 其他异常
+            return Result::error(500, '请求处理异常: ' . $e->getMessage(), [
+                'exception' => $e->getMessage()
+            ]);
+        }
+
+
+        if ($result) {
+            return Result::success($result);
+        } else {
+            return Result::error('获取列表失败');
+        }
+    }
+    private function bt($data) {}
+    public function updateWebConfig1($data)
     {
         // 先根据
         // 根据是否测试环境配置不同 的vue项目的 配置文件
@@ -1020,8 +1166,24 @@ class ClientService implements ClientServiceInterface
     }
     public function updateWebOutput($data)
     {
+
+        // return Result::error('更新输出失败');
         //根据源程序, 复制出来,更改配置文件,打包, 
-        $result = WebsiteTemplate::where('id', $data['id'])->update($data);
+        // $result = 1;
+        $projectRoot = dirname(dirname(__DIR__)); // 从当前文件向上两级目录
+        $projectDir = $projectRoot . '/../zizhujianzhan_web';
+        $command = "cd " . escapeshellarg($projectDir) . " && npm run build";
+
+        // 执行命令并获取输出
+        exec($command, $output, $returnCode);
+
+        // 输出结果
+        echo "Return Code: " . $returnCode . "\n";
+        echo "Output:\n";
+        foreach ($output as $line) {
+            echo $line . "\n";
+        }
+        $result = $returnCode;
         if ($result) {
             return Result::success($result);
         } else {
@@ -1059,3 +1221,150 @@ class ClientService implements ClientServiceInterface
         }
     }
 }
+class bt_api
+{
+    private $BT_KEY = "RN0wTXLHpKKBXXxyDJoovHMpBhyLVFWZ";  //接口密钥
+    private $BT_PANEL = "http://192.168.1.234:18888";       //面板地址
+
+    //如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
+    public function __construct($bt_panel = null, $bt_key = null)
+    {
+        if ($bt_panel) $this->BT_PANEL = $bt_panel;
+        if ($bt_key) $this->BT_KEY = $bt_key;
+    }
+
+    //示例取面板日志	
+    public function GetLogs()
+    {
+        //拼接URL地址
+        $url = $this->BT_PANEL . '/data?action=getData';
+
+        //准备POST数据
+        $p_data = $this->GetKeyData();        //取签名
+        $p_data['table'] = 'logs';
+        $p_data['limit'] = 10;
+        $p_data['tojs'] = 'test';
+
+        //请求面板接口
+        $result = $this->HttpPostCookie($url, $p_data);
+
+        //解析JSON数据
+        $data = json_decode($result, true);
+        return $data;
+    }
+    public function addSite(array $siteData): array
+    {
+        //拼接URL地址
+        $url = $this->BT_PANEL . '/site?action=AddSite';
+
+        //准备POST数据
+        $p_data = $this->GetKeyData(); //取签名
+
+        // Merge site data with key data
+        $p_data = array_merge($p_data, $siteData);
+
+        //请求面板接口
+        $result = $this->HttpPostCookie($url, $p_data);
+
+        //解析JSON数据
+        $data = json_decode($result, true);
+        return $data;
+    }
+    /**
+     * 检查删除数据
+     * ids [21]
+     */
+
+
+    public function checkDelSite(array $siteData): array
+
+    {
+        //拼接URL地址
+        $url = $this->BT_PANEL . '/site?action=check_del_data';
+
+        //准备POST数据
+        $p_data = $this->GetKeyData(); //取签名
+
+        // Merge site data with key data
+        $p_data = array_merge($p_data, $siteData);
+
+        //请求面板接口
+        $result = $this->HttpPostCookie($url, $p_data);
+
+        //解析JSON数据
+        $data = json_decode($result, true);
+        return $data;
+    }
+    /**
+     * 删除网站
+     * id 21
+     * webname t2.lj
+     * path 1
+     */
+    public function delSite(array $siteData): array
+
+
+    {
+        //拼接URL地址
+        $url = $this->BT_PANEL . '/site?action=DeleteSite';
+
+        //准备POST数据
+        $p_data = $this->GetKeyData(); //取签名
+
+        // Merge site data with key data
+        $p_data = array_merge($p_data, $siteData);
+
+        //请求面板接口
+        $result = $this->HttpPostCookie($url, $p_data);
+
+        //解析JSON数据
+        $data = json_decode($result, true);
+        return $data;
+    }
+
+
+    /**
+     * 构造带有签名的关联数组
+     */
+    private function GetKeyData()
+    {
+        $now_time = time();
+        $p_data = array(
+            'request_token'    =>    md5($now_time . '' . md5($this->BT_KEY)),
+            'request_time'    =>    $now_time
+        );
+        return $p_data;
+    }
+
+
+    /**
+     * 发起POST请求
+     * @param String $url 目标网填,带http://
+     * @param Array|String $data 欲提交的数据
+     * @return string
+     */
+    private function HttpPostCookie($url, $data, $timeout = 60)
+    {
+        //定义cookie保存位置
+        $cookie_file = './' . md5($this->BT_PANEL) . '.cookie';
+        if (!file_exists($cookie_file)) {
+            $fp = fopen($cookie_file, 'w+');
+            fclose($fp);
+        }
+
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
+        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_HEADER, 0);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        $output = curl_exec($ch);
+        curl_close($ch);
+        return $output;
+    }
+}