GameArticle.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * 游戏文章管理
  4. * Created by PhpStorm.
  5. * User: cqingt
  6. * Date: 2018/6/28
  7. * Time: 10:59
  8. */
  9. namespace app\admin\controller;
  10. class GameArticle extends Admin
  11. {
  12. protected $model;
  13. public function _initialize()
  14. {
  15. $this->model = model('Common/GameArticle');
  16. }
  17. public function index()
  18. {
  19. $title = $this->request->param('title', '', 'trim');
  20. $isShow = $this->request->param('is_show');
  21. $isEject = $this->request->param('is_eject');
  22. $condition = [];
  23. if (! empty($title)) {
  24. $condition['title'] = ['LIKE', '%' . $title . '%'];
  25. }
  26. if (is_numeric($isShow)) {
  27. $condition['is_show'] = (int)$isShow;
  28. }
  29. if (is_numeric($isEject)) {
  30. $condition['is_eject'] = (int)$isEject;
  31. }
  32. $articles = $this->model
  33. ->field(['id', 'title', 'is_show', 'show_time', 'create_time' , 'is_eject'])
  34. ->where($condition)
  35. ->order('id', 'desc')
  36. ->paginate(8, false, ['query' => $condition]);
  37. $this->assign('list', $articles);
  38. $this->assign('page', $articles->render());
  39. return $this->fetch();
  40. }
  41. public function add()
  42. {
  43. if ($this->request->isPost()) {
  44. $data = [
  45. 'title' => input('title', '', 'trim'),
  46. 'is_show' => input('is_show'),
  47. 'is_eject' => input('is_eject'),
  48. 'show_time' => input('show_time', 0, 'strtotime'),
  49. 'content' => input('content')
  50. ];
  51. $result = $this->validate($data, [
  52. ['title', 'require', '标题不能为空'],
  53. ['is_show', 'require|integer', '请选择文章状态|必须为整数'],
  54. ['is_eject', 'require|integer', '请选择文章状态|必须为整数'],
  55. ['show_time', 'require', '显示时间不能为空'],
  56. ['content', 'require', '内容不能为空'],
  57. ]);
  58. if (true !== $result) {
  59. $this->error($result);
  60. } else {
  61. $data['is_show'] = (int)$data['is_show'];
  62. if ($this->model->save($data)) {
  63. $this->success('文章新增成功', 'gameArticle/index');
  64. } else {
  65. $this->error($this->model->getError());
  66. }
  67. }
  68. }
  69. $this->assign('showTime', date('Y-m-d H:i:s'));
  70. return $this->fetch();
  71. }
  72. public function edit($id)
  73. {
  74. $article = $this->model->find($id);
  75. if (empty($article)) {
  76. $this->error('文章不存在');
  77. }
  78. if ($this->request->isPost()) {
  79. $data = [
  80. 'title' => input('title', '', 'trim'),
  81. 'is_show' => input('is_show'),
  82. 'is_eject' => input('is_eject'),
  83. 'show_time' => input('show_time', 0, 'strtotime'),
  84. 'content' => input('content')
  85. ];
  86. $result = $this->validate($data, [
  87. ['title', 'require', '标题不能为空'],
  88. ['is_show', 'require|integer', '请选择文章状态|必须为整数'],
  89. ['is_eject', 'require|integer', '请选择文章状态|必须为整数'],
  90. ['show_time', 'require', '显示时间不能为空'],
  91. ['content', 'require', '内容不能为空'],
  92. ]);
  93. if (true !== $result) {
  94. $this->error($result);
  95. } else {
  96. $data['is_show'] = (int)$data['is_show'];
  97. if ($this->model->update($data, ['id' => $id])) {
  98. // 删除游戏公告已读记录
  99. model('GameArticleRead')->delRead($id);
  100. $this->success('文章修改成功', 'gameArticle/index');
  101. } else {
  102. $this->error($this->model->getError());
  103. }
  104. }
  105. }
  106. $this->assign('data', $article);
  107. return $this->fetch();
  108. }
  109. public function del($id)
  110. {
  111. $article = $this->model->find($id);
  112. if (empty($article)) {
  113. $this->error('文章不存在');
  114. }
  115. if ($this->model->where(['id' => $id])->delete()) {
  116. // 删除游戏公告已读记录
  117. model('GameArticleRead')->delRead($id);
  118. $this->success('删除成功', 'gameArticle/index');
  119. } else {
  120. $this->error($this->model->getError());
  121. }
  122. }
  123. // 游戏推荐
  124. public function recommend()
  125. {
  126. $article = $this->model->getRecommend();
  127. $this->assign('data', $article);
  128. return $this->fetch();
  129. }
  130. }