row->title, ]; } /* * 处理请求 */ public function handle(Request $request) { $appearanceId = $this->getKey(); $folder = $request->get('folder'); //导入模板 $path = resource_path().'/appearance/'.$folder; if (!is_dir($path)) { return $this->response()->error("The directory does not exist")->refresh(); } $this->sourcePath = $path; $this->appearanceId = $appearanceId; // 清空旧的 DistAppearanceTemplate::deleteTemplates(0, $appearanceId); // 导入模板 $this->readDirectory($path); // 更新状态 DistAppearance::setStatusToImported($appearanceId); // 返回响应结果并刷新页面 return $this->response()->success("Successfully imported")->refresh(); } /** * 设置要POST到接口的数据 * * @return array */ public function parameters() { return [ // 发送当前行 username 字段数据到接口 'title' => $this->row->title, ]; } private function readDirectory($dirPath,$parentId = 0) { // 检查目录是否存在 if (!is_dir($dirPath)) { return; } // 打开目录 $dir = opendir($dirPath); // 读取目录内容 while (($file = readdir($dir)) !== false) { // 忽略当前目录和上级目录 if ($file == '.' || $file == '..') { continue; } // 获取文件或文件夹的完整路径 $fullPath = $dirPath . '/' . $file; // 如果是文件夹,递归读取 if (is_dir($fullPath)) { //echo "文件夹: $fullPath
"; //插数据库 $filePath = str_replace($this->sourcePath, '', $fullPath); $fileName = basename($fullPath); $newParentId = DistAppearanceTemplate::insertTemplateFolder(0, $this->appearanceId, $filePath,$fileName, $parentId); $this->readDirectory($fullPath,$newParentId); // 递归调用 } else { // 如果是文件,读取文件内容 $content = file_get_contents($fullPath); $filePath = str_replace($this->sourcePath, '', $fullPath); $fileName = basename($fullPath); // 插入数据库 DistAppearanceTemplate::insertTemplateContent(0, $this->appearanceId, $filePath,$fileName, $parentId, $content); } } // 关闭目录 closedir($dir); } }