Files

135 lines
4.4 KiB
PHP
Raw Permalink Normal View History

2026-09-17 16:25:31 +08:00
<?php
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;
/**
* 命令行管理
*
* @icon fa fa-circle-o
*/
class Commands extends Backend
{
/**
* Commands模型对象
2026-09-24 15:58:01 +08:00
* @var \app\common\model\auto\Commands
2026-09-17 16:25:31 +08:00
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
2026-09-24 15:58:01 +08:00
$this->model = new \app\common\model\auto\Commands;
2026-09-17 16:25:31 +08:00
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
public function getTypes()
{
$confList = config('site.general_config_cmd_types') ?? [];
foreach ($confList as $key => $value) {
$list[] = ['id' => $key, 'name' => $value];
}
$result = [
'list' => $list ?? [],
'total' => count($list),
];
$response = Response::create($result, 'json', 200);
throw new HttpResponseException($response);
}
public function getTags()
{
$search = $this->request->request("query");
$confList = config('site.general_config_cmd_tags') ?? [];
$list = explode(',', $confList);
$result = [
'query' => $search,
'suggestions' => $list,
];
$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();
}
}