Announcement.php 4.2 KB

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