Chapterendrecommend.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\CacheConstants;
  6. use app\main\service\BookService;
  7. use think\Controller;
  8. use think\Request;
  9. /**
  10. * 章末推荐书库
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Chapterendrecommend extends Backend
  15. {
  16. /**
  17. * Chapterendrecommend模型对象
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('Chapterendrecommend');
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax())
  39. {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('pkey_name'))
  42. {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams('', true);
  46. $total = $this->model
  47. ->join('book', 'book.id=chapter_end_recommend.book_id')
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->join('book', 'book.id=chapter_end_recommend.book_id')
  53. ->field(['chapter_end_recommend.*','book.cansee','book.state'])
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. $book_ids = array_column($list, 'book_id');
  59. $book_ids = array_merge($book_ids, array_column($list, 'relation_book_id'));
  60. $books = BookService::instance()->getBookModel()->getBooksInfo($book_ids);
  61. foreach($list as $index=>$item){
  62. if (array_key_exists($item['book_id'], $books)) {
  63. $list[$index]['book_name'] = $books[$item['book_id']]['name'];
  64. } else {
  65. $list[$index]['book_name'] = '';
  66. }
  67. if (array_key_exists($item['relation_book_id'], $books)) {
  68. $list[$index]['relation_book_name'] = $books[$item['relation_book_id']]['name'];
  69. } else {
  70. $list[$index]['relation_book_name'] = '';
  71. }
  72. }
  73. $result = array("total" => $total, "rows" => $list);
  74. return json($result);
  75. }
  76. return $this->view->fetch();
  77. }
  78. /**
  79. * 添加
  80. */
  81. public function add()
  82. {
  83. if ($this->request->isPost())
  84. {
  85. $params = $this->request->post("row/a");
  86. if ($params)
  87. {
  88. /*
  89. * 已经弃用,如果为了兼容老版可取消注释
  90. foreach ($params as $k => &$v)
  91. {
  92. $v = is_array($v) ? implode(',', $v) : $v;
  93. }
  94. */
  95. if ($this->dataLimit)
  96. {
  97. $params[$this->dataLimitField] = $this->auth->id;
  98. }
  99. try
  100. {
  101. //是否采用模型验证
  102. if ($this->modelValidate)
  103. {
  104. $name = basename(str_replace('\\', '/', get_class($this->model)));
  105. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  106. $this->model->validate($validate);
  107. }
  108. if ($params['recommend'] == '<p><br></p>') {
  109. $params['recommend'] = '';
  110. }
  111. if ($params['book_id'] == $params['relation_book_id']) {
  112. $this->error('书籍与关联书籍不可重复');
  113. }
  114. $result = $this->model->allowField(true)->save($params);
  115. if ($result !== false)
  116. {
  117. $this->success();
  118. }
  119. else
  120. {
  121. $this->error($this->model->getError());
  122. }
  123. }
  124. catch (\think\exception\PDOException $e)
  125. {
  126. $this->error($e->getMessage());
  127. }
  128. }
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. return $this->view->fetch();
  132. }
  133. /**
  134. * 编辑
  135. */
  136. public function edit($ids = NULL)
  137. {
  138. $row = $this->model->get($ids);
  139. if (!$row)
  140. $this->error(__('No Results were found'));
  141. $adminIds = $this->getDataLimitAdminIds();
  142. if (is_array($adminIds))
  143. {
  144. if (!in_array($row[$this->dataLimitField], $adminIds))
  145. {
  146. $this->error(__('You have no permission'));
  147. }
  148. }
  149. if ($this->request->isPost())
  150. {
  151. $params = $this->request->post("row/a");
  152. if ($params)
  153. {
  154. /*
  155. * 已经弃用,如果为了兼容老版可取消注释
  156. foreach ($params as $k => &$v)
  157. {
  158. $v = is_array($v) ? implode(',', $v) : $v;
  159. }
  160. */
  161. try
  162. {
  163. //是否采用模型验证
  164. if ($this->modelValidate)
  165. {
  166. $name = basename(str_replace('\\', '/', get_class($this->model)));
  167. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  168. $row->validate($validate);
  169. }
  170. if ($params['book_id'] == $params['relation_book_id']) {
  171. $this->error('书籍与关联书籍不可重复');
  172. }
  173. if ($params['recommend'] == '<p><br></p>') {
  174. $params['recommend'] = '';
  175. }
  176. $result = $row->allowField(true)->save($params);
  177. if ($result !== false)
  178. {
  179. $cacheKey = CacheConstants::getChapterEndRecommendIds($params['book_id']);
  180. Redis::instance()->del($cacheKey);
  181. $this->success();
  182. }
  183. else
  184. {
  185. $this->error($row->getError());
  186. }
  187. }
  188. catch (\think\exception\PDOException $e)
  189. {
  190. $this->error($e->getMessage());
  191. }
  192. }
  193. $this->error(__('Parameter %s can not be empty', ''));
  194. }
  195. $this->view->assign("row", $row);
  196. return $this->view->fetch();
  197. }
  198. /**
  199. * 删除
  200. */
  201. public function del($ids = "")
  202. {
  203. if ($ids)
  204. {
  205. $pk = $this->model->getPk();
  206. $adminIds = $this->getDataLimitAdminIds();
  207. if (is_array($adminIds))
  208. {
  209. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  210. }
  211. $list = $this->model->where($pk, 'in', $ids)->select();
  212. $count = 0;
  213. foreach ($list as $k => $v)
  214. {
  215. $cacheKey = CacheConstants::getChapterEndRecommendIds($v['book_id']);
  216. Redis::instance()->del($cacheKey);
  217. $count += $v->delete();
  218. }
  219. if ($count)
  220. {
  221. $this->success();
  222. }
  223. else
  224. {
  225. $this->error(__('No rows were deleted'));
  226. }
  227. }
  228. $this->error(__('Parameter %s can not be empty', 'ids'));
  229. }
  230. }