Bladeren bron

添加批量添加命令

Devin 1 maand geleden
bovenliggende
commit
8a4e125caf

+ 74 - 4
application/admin/controller/auto/Commands.php

@@ -3,7 +3,10 @@
 namespace app\admin\controller\auto;
 
 use app\common\controller\Backend;
+use think\Db;
 use think\exception\HttpResponseException;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 use think\Response;
 
 /**
@@ -37,13 +40,12 @@ class Commands extends Backend
     public function getTypes()
     {
         $confList = config('site.general_config_cmd_types') ?? [];
-        foreach ($confList as $key => $value)
-        {
+        foreach ($confList as $key => $value) {
             $list[] = ['id' => $key, 'name' => $value];
         }
         $result = [
-          'list' => $list ?? [],
-          'total' => count($list),
+            'list' => $list ?? [],
+            'total' => count($list),
         ];
         $response = Response::create($result, 'json', 200);
         throw new HttpResponseException($response);
@@ -61,4 +63,72 @@ class Commands extends Backend
         $response = Response::create($result, 'json', 200);
         throw new HttpResponseException($response);
     }
+
+    /**
+     * 添加
+     *
+     * @return string
+     * @throws \think\Exception
+     */
+    public function addmulti()
+    {
+        if (false === $this->request->isPost()) {
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
+
+        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+            $params[$this->dataLimitField] = $this->auth->id;
+        }
+        $result = false;
+        Db::startTrans();
+        try {
+            $commands = trim($params['command']);
+            if (empty($commands)) {
+                throw new \Exception('命令不能为空');
+            }
+            $commandList = explode("\r\n", $commands);
+            $dataList = [];
+            foreach ($commandList as $key => $commend) {
+                $colonPos = strpos($commend, ':');
+                $hashPos = strrpos($commend, '#');
+                $subPos = strrpos($commend, '-');
+                $type_id = trim(substr($commend, 0, $colonPos));
+                $command = trim(substr($commend, $colonPos + 1, $hashPos - $colonPos - 1));
+                $note = trim(substr($commend, $hashPos + 1, $subPos - $hashPos - 1));
+                $cmd_tags = trim(substr($commend, $subPos + 1));
+                $dataList[] = [
+                    'type_id' => $type_id,
+                    'command' => $command,
+                    'note' => $note,
+                    'cmd_tags' => $cmd_tags,
+                    'full_text' => $commend,
+                    'weigh' => $params['weigh'],
+                    'status' => $params['status'],
+                ];
+            }
+            $commandArr = array_column($dataList, 'command');
+            $existsData = $this->model->where('command', 'in', $commandArr)->column('command');
+            $newData = array_filter($dataList, function ($item) use ($existsData) {
+                return !in_array($item['command'], $existsData);
+            });
+            if (!empty($newData)) {
+                $result = $this->model->saveAll($newData);
+            } else {
+                throw new \Exception('数据已存在,无需添加');
+            }
+            Db::commit();
+        } catch (ValidateException|PDOException|\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('No rows were inserted'));
+        }
+        $this->success();
+    }
 }

+ 1 - 0
application/admin/lang/zh-cn.php

@@ -15,6 +15,7 @@ return [
     'Welcome'                                               => '%s,你好!',
     'View'                                                  => '查看',
     'Add'                                                   => '添加',
+    'AddMulti'                                              => '批量添加',
     'Edit'                                                  => '编辑',
     'Del'                                                   => '删除',
     'Delete'                                                => '删除',

+ 5 - 4
application/admin/model/auto/Commands.php

@@ -11,7 +11,7 @@ class Commands extends Model
 
     // 表名
     protected $name = 'auto_commands';
-    
+
     // 自动写入时间戳字段
     protected $autoWriteTimestamp = 'integer';
 
@@ -24,7 +24,7 @@ class Commands extends Model
     protected $append = [
         'status_text'
     ];
-    
+
 
     protected static function init()
     {
@@ -36,7 +36,7 @@ class Commands extends Model
         });
     }
 
-    
+
     public function getStatusList()
     {
         return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
@@ -52,7 +52,8 @@ class Commands extends Model
 
     protected function setFullTextAttr()
     {
-        return implode(',', [$this->type_id, $this->command, $this->note, $this->cmd_tags]);
+        // general:docker-compose restart #重启容器-docker,compose
+        return sprintf('%s:%s #%s-%s', $this->type_id, $this->command, $this->note, $this->cmd_tags);
     }
 
 

+ 40 - 0
application/admin/view/auto/commands/addmulti.html

@@ -0,0 +1,40 @@
+<form id="addmulti-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
+    <div class="form-group">
+        <label class="control-label col-xs-12 col-sm-2">{:__('Command')}:</label>
+        <div class="col-xs-12 col-sm-8" style="color: rgb(31,31,31)">
+            <p>请输入多行命令,格式如下:</p>
+            <pre>类型(linux|cmd|general):命令 #注释-标签1,标签2</pre>
+            <pre>例如:general:docker top wizardly #查看容器进程-docker,compose</pre>
+        </div>
+    </div>
+    <div class="form-group">
+        <label class="control-label col-xs-12 col-sm-2"></label>
+        <div class="col-xs-12 col-sm-8">
+            <textarea class="form-control" id="c-command" name="row[command]" rows="10"></textarea>
+        </div>
+    </div>
+    <div class="form-group">
+        <label class="control-label col-xs-12 col-sm-2">{:__('Weigh')}:</label>
+        <div class="col-xs-12 col-sm-8">
+            <input id="c-weigh" class="form-control" name="row[weigh]" type="number" value="100">
+        </div>
+    </div>
+    <div class="form-group">
+        <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
+        <div class="col-xs-12 col-sm-8">
+            
+            <div class="radio">
+            {foreach name="statusList" item="vo"}
+            <label for="row[status]-{$key}"><input id="row[status]-{$key}" name="row[status]" type="radio" value="{$key}" {in name="key" value="1"}checked{/in} /> {$vo}</label>
+            {/foreach}
+            </div>
+
+        </div>
+    </div>
+    <div class="form-group layer-footer">
+        <label class="control-label col-xs-12 col-sm-2"></label>
+        <div class="col-xs-12 col-sm-8">
+            <button type="submit" class="btn btn-primary btn-embossed">{:__('OK')}</button>
+        </div>
+    </div>
+</form>

+ 1 - 0
application/admin/view/auto/commands/index.html

@@ -18,6 +18,7 @@
                     <div id="toolbar" class="toolbar">
                         <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
                         <a href="javascript:;" class="btn btn-success btn-add {:$auth->check('auto/commands/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
+                        <a href="auto/commands/addmulti" class="btn btn-success btn-dialog {:$auth->check('auto/commands/add')?'':'hide'}" title="{:__('AddMulti')}" ><i class="fa fa-plus"></i> {:__('AddMulti')}</a>
                         <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('auto/commands/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
                         <a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('auto/commands/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
                         

+ 5 - 1
public/assets/js/backend/auto/commands.js

@@ -7,6 +7,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                 extend: {
                     index_url: 'auto/commands/index' + location.search,
                     add_url: 'auto/commands/add',
+                    addmulti_url: 'auto/commands/addmulti',
                     edit_url: 'auto/commands/edit',
                     del_url: 'auto/commands/del',
                     multi_url: 'auto/commands/multi',
@@ -21,7 +22,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             table.bootstrapTable({
                 url: $.fn.bootstrapTable.defaults.extend.index_url,
                 pk: 'id',
-                sortName: 'weigh',
+                sortName: 'id',
                 fixedColumns: true,
                 fixedRightNumber: 1,
                 columns: [
@@ -47,6 +48,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
         add: function () {
             Controller.api.bindevent();
         },
+        addmulti: function () {
+            Controller.api.bindevent();
+        },
         edit: function () {
             Controller.api.bindevent();
         },