Announcement.php 4.2 KB

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