Browse Source

模版导入

moshaorui 5 months ago
parent
commit
cb122584ee

+ 108 - 0
app/Admin/Actions/Grid/AppearanceImport.php

@@ -0,0 +1,108 @@
+<?php
+
+namespace App\Admin\Actions\Grid;
+
+use App\Admin\Forms\InquiryAssignment as InquiryAssignmentForm;
+use Dcat\Admin\Grid\BatchAction;
+use Dcat\Admin\Grid\RowAction;
+use Dcat\Admin\Widgets\Modal;
+use Illuminate\Http\Request;
+use App\Admin\Repositories\DistAppearance;
+use App\Admin\Repositories\DistAppearanceTemplate;
+
+class AppearanceImport extends RowAction
+{
+    public $sourcePath;
+    public $appearanceId;
+    /**
+     * 返回字段标题
+     *
+     * @return string
+     */
+    public function title()
+    {
+        return 'Import Tmpl';
+    }
+
+    public function confirm()
+    {
+        return [
+            "Are you sure you want to import?",
+            $this->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<br />";
+                //插数据库
+                $filePath = str_replace($this->sourcePath, '', $fullPath);
+                $newParentId = DistAppearanceTemplate::insertTemplateFolder(0, $this->appearanceId, $filePath, $parentId);
+                $this->readDirectory($fullPath,$newParentId); // 递归调用
+            } else {
+                // 如果是文件,读取文件内容
+                $content = file_get_contents($fullPath);
+                $filePath = str_replace($this->sourcePath, '', $fullPath);
+                $filePath = str_replace('/' .$file, '', $filePath);
+                // 插入数据库
+                DistAppearanceTemplate::insertTemplateContent(0, $this->appearanceId, $filePath,$file, $parentId, $content);
+            }
+        }
+
+        // 关闭目录
+        closedir($dir);
+    }
+}

+ 20 - 1
app/Admin/Controllers/DistAppearanceController.php

@@ -2,6 +2,7 @@
 
 namespace App\Admin\Controllers;
 
+use App\Admin\Actions\Grid\AppearanceImport;
 use App\Admin\Repositories\DistAppearance;
 use App\Libraries\CommonHelper;
 use Dcat\Admin\Form;
@@ -34,14 +35,25 @@ class DistAppearanceController extends AdminController
     {
         return Grid::make(new DistAppearance(), function (Grid $grid) {
             $grid->column('id')->sortable();
+            // 标题
             $grid->column('title');
+            // 文件夹
+            $grid->column('folder')->help('Folder names under the directory:/resources/appearance');
+            // 封面图
             $grid->column('cover_image')->display(function ($image) {
-                // 开始生成 HTML
                 $dataImages = [$image];
                 return CommonHelper::displayImage($dataImages,100);
             });
+            // 排序
             $grid->column('order')->orderable();
+            //是否导入
+            $grid->column('imported')->using(config('dictionary.whether'))->label([
+                0 => 'default',
+                1 => 'success',
+            ]);
+            //是否启用
             $grid->column('enabled')->switch();
+            // 时间
             $grid->column('created_at');
             $grid->column('updated_at')->sortable();
             // 过滤器
@@ -51,6 +63,11 @@ class DistAppearanceController extends AdminController
                 $filter->equal('title')->width(2);
                 $filter->equal('enabled', 'enabled')->select(config('dictionary.enabled'))->width(2);
             });
+            // 操作
+            $grid->actions(function (Grid\Displayers\Actions $actions) {
+                $actions->disableDelete();
+                $actions->append(new AppearanceImport());
+            });
         });
     }
 
@@ -66,6 +83,7 @@ class DistAppearanceController extends AdminController
         return Show::make($id, new DistAppearance(), function (Show $show) {
             $show->field('id');
             $show->field('title');
+            $show->field('folder');
             $show->field('cover_image')->as(function ($image) {
                 // 开始生成 HTML
                 $dataImages = [$image];
@@ -88,6 +106,7 @@ class DistAppearanceController extends AdminController
         return Form::make(new DistAppearance(), function (Form $form) {
             $form->display('id');
             $form->text('title');
+            $form->text('folder');
             $form->image("cover_image")
                 ->autoUpload()
                 ->uniqueName()

+ 13 - 0
app/Admin/Repositories/DistAppearance.php

@@ -24,4 +24,17 @@ class DistAppearance extends EloquentRepository
         return $options;
     }
 
+    /*
+     * 设置状态为已导入
+     */
+    public static function setStatusToImported($id)
+    {
+        $row = Model::where('id', $id)->first();
+        if ($row) {
+            $row->imported = 1;
+            $row->save();
+        }
+        return true;
+    }
+
 }

+ 46 - 0
app/Admin/Repositories/DistAppearanceTemplate.php

@@ -57,4 +57,50 @@ class DistAppearanceTemplate extends EloquentRepository
         }
         return false;
     }
+
+    /*
+     * 请空指定模板
+     */
+    public static function deleteTemplates($distId,$appearanceId) {
+        $self = new self();
+        $self->model()->where('dist_id',$distId)->where('appearance_id',$appearanceId)->delete();
+        return true;
+    }
+
+    /*
+     * 插入模版文件夹
+     */
+    public static function insertTemplateFolder($distId,$appearanceId,$filePath,$parentId) {
+        $self = new self();
+        $model = $self->model();
+        $model->dist_id = $distId;
+        $model->appearance_id = $appearanceId;
+        $model->file_name = $filePath;
+        $model->parent_id = $parentId;
+        $model->file_type = 0;
+        $model->file_path = $filePath;
+        $model->save();
+        // 获取插入数据的 ID
+        $insertedId = $model->id;
+        return $insertedId;
+    }
+
+    /*
+     * 插入模版文件内容
+     */
+    public static function insertTemplateContent($distId,$appearanceId,$filePath,$fileName,$parentId,$content) {
+        $self = new self();
+        $model = $self->model();
+        $model->dist_id = $distId;
+        $model->appearance_id = $appearanceId;
+        $model->file_name = $fileName;
+        $model->parent_id = $parentId;
+        $model->file_type = 1;
+        $model->file_path = $filePath;
+        $model->content = $content;
+        $model->save();
+        // 获取插入数据的 ID
+        $insertedId = $model->id;
+        return $insertedId;
+    }
 }

+ 0 - 1
app/Admin/Repositories/DistAppearanceVariable.php

@@ -31,5 +31,4 @@ class DistAppearanceVariable extends EloquentRepository
 
 
 
-
 }

+ 2 - 0
app/Models/DistAppearance.php

@@ -27,4 +27,6 @@ class DistAppearance extends Model implements Sortable
         'sort_when_creating' => true,    // 创建时自动排序
     ];
 
+
+
 }

+ 5 - 0
config/dictionary.php

@@ -24,6 +24,11 @@ return [
         '1' => 'Yes',
         '0' => 'No',
     ],
+    //是否
+    'whether' => [
+        '1' => 'Yes',
+        '0' => 'No',
+    ],
     //询价状态
     'inquiryStatus' => [
         '0' => 'New',