Browse Source

变量管理

moshaorui 5 months ago
parent
commit
0838317f41

+ 3 - 12
app/Admin/Controllers/DistAppearanceTemplateController.php

@@ -10,20 +10,19 @@ use Dcat\Admin\Http\Controllers\AdminController;
 use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Admin;
 use App\Admin\Forms\AceLeft;
-use App\Admin\Forms\AceRight;
 use Illuminate\Http\Request;
 use Illuminate\Support\Str;
+use Dcat\Admin\Widgets\Card;
 
 
 class DistAppearanceTemplateController extends AdminController
 {
-
     /*
      * monaco editor 编辑代码
      */
     public function ace(Content $content,Request $request) {
         $content->view('admin.pages-custom.ace_content');
-        if ($request->isMethod('post')) {
+        if ($request->isMethod('post') || false == empty($request->get('act'))) {
             if($request->get('act') == 'tree') {
                 //获取代码树形结构
                 $appearance_id = $request->get('appearance_id');
@@ -33,22 +32,14 @@ class DistAppearanceTemplateController extends AdminController
                 //得到文件内容
                 $id = $request->get('id');
                 return DistAppearanceTemplate::getContent($id);
-            } elseif ($request->get('act') == 'save') {
+            } elseif ($request->get('act') == 'content_save') {
                 //保存文件内容
                 $template_id = $request->get('template_id');
                 $content = $request->get('content');
                 return DistAppearanceTemplate::saveContent($template_id, $content);
-            } elseif ($request->get('act') == 'aceRight') {
-                //右侧代码编辑器
-                $distId = $request->get('dist_id');
-                $appearanceId = $request->get('appearance_id');
-                $templateId = $request->get('template_id');
-                return new AceRight($distId,$appearanceId,$templateId);
             }
         }
-
         $leftForm = new AceLeft();
-        //
         return $content
             ->header('Template Editor')
             ->body(admin_view('admin.pages-custom.ace',['leftForm'=>$leftForm]));

+ 109 - 41
app/Admin/Controllers/DistAppearanceVariableController.php

@@ -2,6 +2,8 @@
 
 namespace App\Admin\Controllers;
 
+use App\Admin\Renderable\DistDistributorTable;
+use App\Admin\Repositories\DistAppearance;
 use App\Admin\Repositories\DistAppearanceVariable;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
@@ -9,19 +11,15 @@ use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
 use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Admin;
+use Dcat\Admin\Widgets\Dropdown;
+
 
 class DistAppearanceVariableController extends AdminController
 {
-    /**
-     * page index
-     */
+
     public function index(Content $content)
     {
-        return $content
-            ->header('列表')
-            ->description('全部')
-            ->breadcrumb(['text'=>'列表','url'=>''])
-            ->body($this->grid());
+        return $content->full()->body($this->grid());
     }
 
     /**
@@ -31,20 +29,62 @@ class DistAppearanceVariableController extends AdminController
      */
     protected function grid()
     {
-        return Grid::make(new DistAppearanceVariable(), function (Grid $grid) {
+        $templateId = isset($_GET['templateId']) ? intval($_GET['templateId']) : 0;
+        $appearanceId = isset($_GET['appearanceId']) ? intval($_GET['appearanceId']) : 0;
+        $distId = isset($_GET['distId']) ? intval($_GET['distId']) : 0;
+        if (empty($templateId) || empty($appearanceId)) {
+            die(' templateId or appearanceId is empty');
+        }
+        return Grid::make(DistAppearanceVariable::with(['distributor','appearance']), function (Grid $grid) use ($templateId, $appearanceId, $distId) {
             $grid->column('id')->sortable();
-            $grid->column('dist_id');
-            $grid->column('appearance_id');
-            $grid->column('variable_name');
-            $grid->column('variable_value');
-            $grid->column('variable_type');
-            $grid->column('created_at');
-            $grid->column('updated_at')->sortable();
-        
-            $grid->filter(function (Grid\Filter $filter) {
-                $filter->equal('id');
-        
+            $grid->column('distributor.company_name','Distributor Name')->display(function ($company_name) {
+                if (empty($company_name)) {
+                    return 'Raw Template Variables';
+                }
+                return $company_name;
+            })->help('If the distributor is empty, then use the Raw Template Variables.');
+            $grid->column('appearance.title','Appearance Title');
+            $grid->column('template_ids','site variable')->display(function ($templateIds) {
+                if ($templateIds == 0) {
+                    return 'Yes';
+                } else {
+                    return 'No';
+                }
             });
+            $grid->column('variable_name');
+            $grid->column('variable_type')->using(['1' => 'text', '2' => 'textarea', '3' => 'json']);
+            //搜索
+            $grid->quickSearch(['variable_name']);
+            //工具栏
+            $grid->disableViewButton();
+            $grid->showQuickEditButton();
+            $grid->enableDialogCreate();
+            $grid->disableEditButton();
+            $addUrl = '?templateId='. $templateId . '&appearanceId='. $appearanceId . '&distId='. $distId;
+            //排序
+            $grid->model()->where('appearance_id', $appearanceId)->where('dist_id', $distId)->whereIn('template_ids', [0, $templateId])->orderBy("id",'asc');
+            //增加js 向新增按钮添加参数还有编辑按钮添加参数
+            Admin::script(
+<<<JS
+var button = $('.dialog-create');
+var currentUrl = button.attr('data-url');
+if (currentUrl.indexOf('?') === -1) {
+    button.attr('data-url', currentUrl + '{$addUrl}');
+} else {
+    button.attr('data-url', currentUrl + '{$addUrl}');
+}
+
+$('.quick-edit').each(function() {
+    var currentUrl = $(this).attr('data-url');
+    if (currentUrl.indexOf('?') === -1) {
+        $(this).attr('data-url', currentUrl + '{$addUrl}');
+    } else {
+        // 如果已经有查询参数,添加 &id=123
+        $(this).attr('data-url', currentUrl + '{$addUrl}');
+    }
+});
+JS
+            );
         });
     }
 
@@ -55,19 +95,25 @@ class DistAppearanceVariableController extends AdminController
      *
      * @return Show
      */
-    protected function detail($id)
-    {
-        return Show::make($id, new DistAppearanceVariable(), function (Show $show) {
-            $show->field('id');
-            $show->field('dist_id');
-            $show->field('appearance_id');
-            $show->field('variable_name');
-            $show->field('variable_value');
-            $show->field('variable_type');
-            $show->field('created_at');
-            $show->field('updated_at');
-        });
-    }
+//    protected function detail($id)
+//    {
+//        return Show::make($id, DistAppearanceVariable::with(['distributor','appearance']), function (Show $show) {
+//            $show->field('id');
+//            $show->field('distributor.company_name','Distributor Name')->as(function ($company_name) {
+//                if (empty($company_name)) {
+//                    return 'Site Variable';
+//                }
+//                return $company_name;
+//            });
+//            $show->field('appearance.title','Appearance Title');
+//            $show->field('variable_type')->using(['1' => 'text', '2' => 'textarea', '3' => 'json']);
+//            $show->field('variable_name');
+//            $show->field('variable_value');
+//
+//            $show->field('created_at');
+//            $show->field('updated_at');
+//        });
+//    }
 
     /**
      * Make a form builder.
@@ -76,16 +122,38 @@ class DistAppearanceVariableController extends AdminController
      */
     protected function form()
     {
-        return Form::make(new DistAppearanceVariable(), function (Form $form) {
+        $templateId = isset($_GET['templateId']) ? intval($_GET['templateId']) : 0;
+        $appearanceId = isset($_GET['appearanceId']) ? intval($_GET['appearanceId']) : 0;
+        $distId = isset($_GET['distId']) ? intval($_GET['distId']) : 0;
+        return Form::make(new DistAppearanceVariable(), function (Form $form) use ($templateId, $appearanceId, $distId) {
             $form->display('id');
-            $form->text('dist_id');
-            $form->text('appearance_id');
+            $form->hidden('appearance_id')->value($appearanceId);
+            $form->hidden('dist_id')->value($distId);
+            $form->hidden('template_ids')->value($templateId);
+            //编辑时当template_ids为0时,site_variable为默认值1,否则为0
+            $variableDefaul = 0;
+            if (!$form->isCreating()) {
+                if ($form->model()->template_ids == 0) {
+                    $variableDefaul = 1;
+                }
+            }
+            $form->switch('site_variable')->value($variableDefaul);
+            $form->radio('variable_type')->options([
+                '1'=>'text',
+                '2'=>'textarea',
+                '3'=>'json'
+            ])->default(1);
             $form->text('variable_name');
-            $form->text('variable_value');
-            $form->text('variable_type');
-        
-            $form->display('created_at');
-            $form->display('updated_at');
+            $form->textarea('variable_value');
+
+            $form->ignore(['site_variable']);
+
+            $form->submitted(function (Form $form) {
+                if ($form->site_variable == 1) {
+                    $form->template_ids = 0;
+                }
+            });
+
         });
     }
 }

+ 0 - 77
app/Admin/Forms/AceRight.php

@@ -1,77 +0,0 @@
-<?php
-
-namespace App\Admin\Forms;
-
-use App\Admin\Renderable\DistDistributorTable;
-use App\Admin\Repositories\DistAppearance;
-use Dcat\Admin\Widgets\Form;
-use App\Admin\Repositories\DistAppearanceVariable;
-
-class AceRight extends Form
-{
-    public $distId;
-    public $appearanceId;
-    public $templateId;
-    public function __construct($distId,$appearanceId,$templateId)
-    {
-        $this->distId = empty($distId) ? 0 : $distId;
-        $this->appearanceId = empty($appearanceId) ? 0 : $appearanceId;
-        $this->templateId = empty($templateId) ? 0 : $templateId;
-        parent::__construct();
-    }
-
-    /*
-     * 表单设置
-     */
-    public function form()
-    {
-        $variableRow = DistAppearanceVariable::getVariableRow($this->distId,$this->appearanceId,$this->templateId);
-        if ($variableRow) {
-            $i = 0;
-            foreach ($variableRow as $key => $value) {
-                $this->hidden('field['.$i.'][id]',$value->id);
-                if ($value->variable_type == '1') {
-                    //文本类型
-                    $this->text('field['.$i.']['.$value->variable_name.']',$value->variable_name)
-                        ->default($value->variable_value)
-                        ->width(12,12)
-                        ->setLabelClass('d-flex');
-                } elseif ($value->variable_type == '2') {
-                    //长文本类型
-                    $this->textarea('field['.$i.']['.$value->variable_name.']',$value->variable_name)
-                        ->default($value->variable_value)
-                        ->width(12,12)
-                        ->setLabelClass('d-flex');
-                } elseif ($value->variable_type == '3') {
-                    //json类型
-                    $this->textarea('field['.$i.']['.$value->variable_name.']',$value->variable_name)
-                        ->default($value->variable_value)
-                        ->width(12,12)
-                        ->setLabelClass('d-flex');
-                }
-                $i++;
-            }
-        }
-        $this->disableSubmitButton();
-        $this->disableResetButton();
-
-        $this->html('<button type="button" class="btn btn-primary">add</button> <button type="button" class="btn btn-success">save</button>');
-    }
-
-    // 处理表单提交请求
-    public function handle(array $input)
-    {
-        return $this->response()->success('Processed successfully.')->refresh();
-    }
-
-    /**
-     * 返回表单数据,如不需要可以删除此方法
-     *
-     * @return array
-     */
-    public function default()
-    {
-        return [];
-    }
-
-}

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

@@ -3,6 +3,7 @@
 namespace App\Admin\Repositories;
 
 use App\Models\DistAppearanceVariable as Model;
+use Dcat\Admin\Form;
 use Dcat\Admin\Repositories\EloquentRepository;
 
 class DistAppearanceVariable extends EloquentRepository
@@ -26,4 +27,9 @@ class DistAppearanceVariable extends EloquentRepository
         }
     }
 
+
+
+
+
+
 }

+ 19 - 1
app/Models/DistAppearanceVariable.php

@@ -10,5 +10,23 @@ class DistAppearanceVariable extends Model
 {
 	use HasDateTimeFormatter;
     protected $table = 'dist_appearance_variable';
-    
+
+    protected $fillable = ['dist_id', 'appearance_id', 'variable_name','variable_value','variable_type','created_at','updated_at','template_ids'];
+
+    /*
+     * 关联到分销商表
+     */
+    public function distributor()
+    {
+        return $this->hasOne(DistAdminDistributor::class, 'id', 'dist_id');
+    }
+
+    /*
+     * 关联到外观表
+     */
+    public function appearance()
+    {
+        return $this->hasOne(DistAppearance::class, 'id', 'appearance_id');
+    }
+
 }

+ 46 - 29
resources/views/admin/pages-custom/ace.blade.php

@@ -6,13 +6,6 @@
         background-color: #fff; /* 左侧面板的背景色 */
         height: 100vh; /* 高度占满 */
     }
-    .right-panel {
-        background-color: #fff; /* 左侧面板的背景色 */
-        height: 100vh; /* 高度占满 */
-        max-height: 100vh; /* 设置最大高度 */
-        overflow-y: auto; /* 启用垂直滚动条 */
-        border-radius: 4px; /* 圆角 */
-    }
     .main-panel {
         background-color: #e9ecef; /* 右侧面板的背景色 */
         height: 97vh; /* 高度占满 */
@@ -49,20 +42,38 @@
             <div class="card-body"></div>
             <!-- 代码树 end-->
         </div>
-        <div class="col-8 main-panel"> <!-- 右侧80% -->
+        <div class="col-10 main-panel"> <!-- 右侧80% -->
             <div class="main-panel-header">
                 <button type="button" class="btn btn-primary btn-sm" id="save-btn">保存</button>
+                <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#iframeModal" style="margin-left: 10px;">变量管理</button>
             </div>
             <div class="main-panel-content" id="editor" style="width: 100%; height: 100%;"></div>
             <input type="hidden" name="template_id"  id="template_id" />
         </div>
-        <div class="col-2 right-panel" id="variables-list">
+    </div>
+</div>
+
+<!-- 变量管理弹窗 -->
+<div class="modal fade" id="iframeModal" tabindex="-1" aria-labelledby="iframeModalLabel" aria-hidden="true">
+    <div class="modal-dialog modal-dialog-centered modal-xl">
+        <div class="modal-content">
+            <div class="modal-header">
+                <h5 class="modal-title" id="iframeModalLabel">变量管理</h5>
+                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                    <span aria-hidden="true">&times;</span>
+                </button>
+            </div>
+            <div class="modal-body">
+                <iframe src="/prime-control/dist-template-var" width="100%" height="700px" frameborder="0"></iframe>
+            </div>
+            <div class="modal-footer">
+                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
+            </div>
         </div>
     </div>
 </div>
 
 <script src="/vendor/ace/ace.js" type="text/javascript"></script>
-
 <script>
     // 创建 Ace 编辑器实例
     var editor = ace.edit("editor");
@@ -77,42 +88,46 @@
     });
 
     $(document).ready(function() {
-        // 创建 Ace 编辑器实例
+        var fileId = '';
+        var changeIframeUrl = function() {
+            appearance_id = $('select[name="appearance_id"]').val();
+            dist_id = $('input[name="dist_id"]').val();
+            var iframeUrl = '/prime-control/dist-template-var?templateId=' + fileId + '&appearanceId=' + appearance_id+'&distId=' + dist_id;
+            $('#iframeModal iframe').attr('src', iframeUrl);
+        }
+        //中间的Ace编辑器,内容展示
         var actionclick = function() {
             $('.file-action').click(function() {
-                var id = $(this).attr('file_id');
+                var loadingIndex = layer.load(1, {
+                    shade: [0.5, '#000'] // 设置遮罩层
+                });
+
+                fileId = $(this).attr('file_id');
+                //改变iframe的url
+                changeIframeUrl();
                 //编辑代码
                 $.ajax({
                     url: '/prime-control/dist-template/ace',
                     type: 'POST',
                     data: {
                         act:'content',
-                        id: id
+                        id: fileId
                     },
                     success: function(response) {
-                        $("#template_id").val(id);
+                        $("#template_id").val(fileId);
                         editor.setValue(response);
+                        layer.close(loadingIndex);
                     }
                 });
-                //显示右边变量列表
-                $.ajax({
-                    url: '/prime-control/dist-template/ace',
-                    type: 'POST',
-                    data: {
-                        act:'aceRight',
-                        template_id: id,
-                        dist_id: $('input[name="dist_id"]').val(),
-                        appearance_id: $('select[name="appearance_id"]').val()
-                    },
-                    success: function(response) {
-                        $("#variables-list").html(response);
-                    }
-                });
+
             });
         }
+        // 加载代码树
         var postData = function () {
             var appearance_id = $('select[name="appearance_id"]').val();
             var dist_id = $('input[name="dist_id"]').val();
+            //改变iframe的url
+            changeIframeUrl();
             $.ajax({
                 url: '/prime-control/dist-template/ace',
                 type: 'POST',
@@ -127,12 +142,14 @@
                 }
             });
         }
+        //左边外观选择与分销商选择联动
         $('select[name="appearance_id"]').change(function() {
             postData();
         });
         $('input[name="dist_id"]').change(function() {
             postData();
         });
+        // 模版编辑保存
         $("#save-btn").click(function() {
             var template_id = $("#template_id").val();
             var content = editor.getValue();
@@ -144,7 +161,7 @@
                 url: '/prime-control/dist-template/ace',
                 type: 'POST',
                 data: {
-                    act:'save',
+                    act:'content_save',
                     template_id: template_id,
                     content: content
                 },