Commands.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller\auto;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\HttpResponseException;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Response;
  9. /**
  10. * 命令行管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Commands extends Backend
  15. {
  16. /**
  17. * Commands模型对象
  18. * @var \app\admin\model\auto\Commands
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\auto\Commands;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. public function getTypes()
  33. {
  34. $confList = config('site.general_config_cmd_types') ?? [];
  35. foreach ($confList as $key => $value) {
  36. $list[] = ['id' => $key, 'name' => $value];
  37. }
  38. $result = [
  39. 'list' => $list ?? [],
  40. 'total' => count($list),
  41. ];
  42. $response = Response::create($result, 'json', 200);
  43. throw new HttpResponseException($response);
  44. }
  45. public function getTags()
  46. {
  47. $search = $this->request->request("query");
  48. $confList = config('site.general_config_cmd_tags') ?? [];
  49. $list = explode(',', $confList);
  50. $result = [
  51. 'query' => $search,
  52. 'suggestions' => $list,
  53. ];
  54. $response = Response::create($result, 'json', 200);
  55. throw new HttpResponseException($response);
  56. }
  57. /**
  58. * 添加
  59. *
  60. * @return string
  61. * @throws \think\Exception
  62. */
  63. public function addmulti()
  64. {
  65. if (false === $this->request->isPost()) {
  66. return $this->view->fetch();
  67. }
  68. $params = $this->request->post('row/a');
  69. if (empty($params)) {
  70. $this->error(__('Parameter %s can not be empty', ''));
  71. }
  72. $params = $this->preExcludeFields($params);
  73. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  74. $params[$this->dataLimitField] = $this->auth->id;
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. $commands = trim($params['command']);
  80. if (empty($commands)) {
  81. throw new \Exception('命令不能为空');
  82. }
  83. $commandList = explode("\r\n", $commands);
  84. $dataList = [];
  85. foreach ($commandList as $key => $commend) {
  86. $colonPos = strpos($commend, ':');
  87. $hashPos = strrpos($commend, '#');
  88. $subPos = strrpos($commend, '-');
  89. $type_id = trim(substr($commend, 0, $colonPos));
  90. $command = trim(substr($commend, $colonPos + 1, $hashPos - $colonPos - 1));
  91. $note = trim(substr($commend, $hashPos + 1, $subPos - $hashPos - 1));
  92. $cmd_tags = trim(substr($commend, $subPos + 1));
  93. $dataList[] = [
  94. 'type_id' => $type_id,
  95. 'command' => $command,
  96. 'note' => $note,
  97. 'cmd_tags' => $cmd_tags,
  98. 'full_text' => $commend,
  99. 'weigh' => $params['weigh'],
  100. 'status' => $params['status'],
  101. ];
  102. }
  103. $commandArr = array_column($dataList, 'command');
  104. $existsData = $this->model->where('command', 'in', $commandArr)->column('command');
  105. $newData = array_filter($dataList, function ($item) use ($existsData) {
  106. return !in_array($item['command'], $existsData);
  107. });
  108. if (!empty($newData)) {
  109. $result = $this->model->saveAll($newData);
  110. } else {
  111. throw new \Exception('数据已存在,无需添加');
  112. }
  113. Db::commit();
  114. } catch (ValidateException|PDOException|\Exception $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. }
  118. if ($result === false) {
  119. $this->error(__('No rows were inserted'));
  120. }
  121. $this->success();
  122. }
  123. }