Task.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\admin\controller\task;
  3. use app\common\controller\Backend;
  4. use fast\Random;
  5. use think\Db;
  6. /**
  7. * 任务管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Task extends Backend
  12. {
  13. /**
  14. * Task模型对象
  15. * @var \app\admin\model\task\Task
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\task\Task;
  22. $this->view->assign("typeIdList", $this->model->getTypeIdList());
  23. $this->view->assign("isShowList", $this->model->getIsShowList());
  24. $this->view->assign("urlTypeList", $this->model->getUrlTypeList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 添加
  37. */
  38. public function add()
  39. {
  40. if ($this->request->isPost()) {
  41. $params = $this->request->post("row/a");
  42. if ($params) {
  43. $params = $this->preExcludeFields($params);
  44. $ids = \app\common\model\Task::column("task_no");
  45. $params["task_no"] = $this->getUinqueId(8,$ids);
  46. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  47. $params[$this->dataLimitField] = $this->auth->id;
  48. }
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException(true)->validate($validate);
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. Db::commit();
  60. } catch (ValidateException $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. } catch (PDOException $e) {
  64. Db::rollback();
  65. $this->error($e->getMessage());
  66. } catch (Exception $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. }
  70. if ($result !== false) {
  71. $this->success();
  72. } else {
  73. $this->error(__('No rows were inserted'));
  74. }
  75. }
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 生成不重复的随机数字
  82. */
  83. function getUinqueId($length = 8,$ids = []) {
  84. $newid = Random::build("alnum",$length);
  85. if(in_array($newid,$ids)) {
  86. $this->getUinqueId(8);
  87. }
  88. return $newid;
  89. }
  90. }