Crontab.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\common\controller\Backend;
  4. /**
  5. * 定时任务
  6. *
  7. * @icon fa fa-tasks
  8. * @remark 类似于Linux的Crontab定时任务,可以按照设定的时间进行任务的执行,目前仅支持三种任务:请求URL、执行SQL、执行Shell
  9. */
  10. class Crontab extends Backend
  11. {
  12. protected $model = null;
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->model = model('Crontab');
  17. $this->view->assign('typedata', [
  18. 'url' => __('Request Url'),
  19. 'sql' => __('Execute Sql Script'),
  20. 'shell' => __('Execute Shell'),
  21. ]);
  22. }
  23. /**
  24. * 查看
  25. */
  26. public function index()
  27. {
  28. if ($this->request->isAjax())
  29. {
  30. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  31. $total = $this->model
  32. ->where($where)
  33. ->order($sort, $order)
  34. ->count();
  35. $list = $this->model
  36. ->where($where)
  37. ->order($sort, $order)
  38. ->limit($offset, $limit)
  39. ->select();
  40. $result = array("total" => $total, "rows" => $list);
  41. return json($result);
  42. }
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 添加
  47. */
  48. public function add()
  49. {
  50. if ($this->request->isPost())
  51. {
  52. $this->code = -1;
  53. $params = $this->request->post("row/a");
  54. if ($params)
  55. {
  56. $this->model->create($params);
  57. $this->code = 1;
  58. }
  59. return;
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 编辑
  65. */
  66. public function edit($ids = NULL)
  67. {
  68. $row = $this->model->get(['id' => $ids]);
  69. if (!$row)
  70. $this->error(__('No Results were found'));
  71. if ($this->request->isPost())
  72. {
  73. $this->code = -1;
  74. $params = $this->request->post("row/a");
  75. if ($params)
  76. {
  77. $row->save($params);
  78. $this->code = 1;
  79. }
  80. return;
  81. }
  82. $this->view->assign("row", $row);
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 删除
  87. */
  88. public function del($ids = "")
  89. {
  90. $this->code = -1;
  91. if ($ids)
  92. {
  93. $count = $this->model->where('id', 'in', $ids)->delete();
  94. if ($count)
  95. {
  96. $this->code = 1;
  97. }
  98. }
  99. return;
  100. }
  101. /**
  102. * 批量更新
  103. */
  104. public function multi($ids = "")
  105. {
  106. $this->code = -1;
  107. $ids = $ids ? $ids : $this->request->param("ids");
  108. if ($ids)
  109. {
  110. if ($this->request->has('params'))
  111. {
  112. parse_str($this->request->post("params"), $values);
  113. $values = array_intersect_key($values, array_flip(array('status')));
  114. if ($values)
  115. {
  116. $count = $this->model->where('id', 'in', $ids)->update($values);
  117. if ($count)
  118. {
  119. $this->code = 1;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. $this->code = 1;
  126. }
  127. }
  128. return;
  129. }
  130. }