Announcement.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\base\Permissions;
  4. use think\Db;
  5. class Announcement extends Permissions
  6. {
  7. private function getModel()
  8. {
  9. return new \app\common\model\Announcement();
  10. }
  11. public function index()
  12. {
  13. if ($this->request->isAjax()) {
  14. $post = $this->request->param();
  15. $where = [];
  16. if (isset($post['ids']) and !empty($post['ids'])) {
  17. $where['id'] = ['in', $post['ids']];
  18. }
  19. if (!empty($post["title"])) {
  20. $where["title"] = ['like', '%' . $post["title"] . '%'];
  21. }
  22. if (isset($post["admin_id"]) and "" != $post["admin_id"]) {
  23. $where["admin_id"] = $post["admin_id"];
  24. }
  25. if (isset($post["create_time"]) and !empty($post["create_time"])) {
  26. $timerang = explode(' - ', $post["create_time"]);
  27. $min_time = strtotime($timerang[0]);
  28. $max_time = $timerang[0] == $timerang[1] ? $min_time + 24 * 3600 - 1 : strtotime($timerang[1]??'');
  29. $where["create_time"] = [['>=', $min_time], ['<=', $max_time]];
  30. }
  31. $model = $this->getModel();
  32. $count = $model->where($where)->count();
  33. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
  34. return array('code' => 0, 'count' => $count, 'data' => $data);
  35. } else {
  36. return $this->fetch();
  37. }
  38. }
  39. public function publish()
  40. {
  41. $id = $this->request->param('id', 0, 'intval');
  42. $model = $this->getModel();
  43. $post = $this->request->post();
  44. if ($this->request->isPost()) {
  45. //验证
  46. $validate = new \think\Validate([
  47. ['title|标题', 'max:50'],
  48. ['content|内容', 'max:5000'],
  49. ]);
  50. if (!$validate->check($post)) {
  51. $this->error('提交失败:' . $validate->getError());
  52. }
  53. }
  54. if ($id > 0) {
  55. //修改
  56. $data = $model->where('id', $id)->find();
  57. if (empty($data)) {
  58. $this->error('id不正确');
  59. }
  60. if ($this->request->isPost()) {
  61. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  62. $this->error('修改失败');
  63. } else {
  64. $this->success('修改成功');
  65. }
  66. } else {
  67. $this->assign('data', $data);
  68. return $this->fetch();
  69. }
  70. } else {
  71. //新增
  72. if ($this->request->isPost()) {
  73. if (false == $model->allowField(true)->save($post)) {
  74. $this->error('添加失败');
  75. } else {
  76. $this->success('添加成功', 'index');
  77. }
  78. } else {
  79. return $this->fetch();
  80. }
  81. }
  82. }
  83. public function delete()
  84. {
  85. if ($this->request->isAjax()) {
  86. $id = $this->request->param('id', 0, 'intval');
  87. if (false == $this->getModel()->where('id', $id)->delete()) {
  88. $this->error('删除失败');
  89. } else {
  90. $this->success('删除成功', 'index');
  91. }
  92. }
  93. }
  94. public function deletes()
  95. {
  96. if ($this->request->isAjax()) {
  97. $post = $this->request->param();
  98. $ids = $post['ids'];
  99. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  100. $this->success('删除成功');
  101. }
  102. }
  103. }
  104. }