ArticleBaseController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * ArticleBasecontroller.php UTF-8
  4. * 资讯文章基类
  5. *
  6. * @date : 2018/5/8 14:44
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace cmf\controller;
  13. use cmf\view\Filter;
  14. use huo\logic\posts\PostsLogic;
  15. use huo\model\posts\PostsModel;
  16. use huolib\constant\CommonConst;
  17. use huolib\constant\GameConst;
  18. use huolib\constant\NewsConst;
  19. use huolib\status\CommonStatus;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\Exception;
  23. use think\exception\DbException;
  24. class ArticleBaseController extends AdminBaseController {
  25. protected $param = array();
  26. protected $id;
  27. public function _initialize() {
  28. $this->param = $this->request->param();
  29. }
  30. /***
  31. * 资讯类型选择器
  32. */
  33. protected function _post_types() {
  34. $post_types = [
  35. NewsConst::NEWS_TYPE_NEWS => lang('news'),
  36. NewsConst::NEWS_TYPE_STRATEGY => lang('activity'),
  37. NewsConst::NEWS_TYPE_ACTIVITY => lang('strategy'),
  38. NewsConst::NEWS_TYPE_NOTICE => lang('announcement'),
  39. NewsConst::NEWS_TYPE_AWARD => lang('awards'),
  40. ];
  41. $this->assign('post_types_data', $post_types);
  42. return $post_types;
  43. }
  44. /***
  45. * 是否允许评论
  46. */
  47. protected function _comment_status() {
  48. $comment_status = [
  49. NewsConst::NEWS_COMMENT_STATUS_NOT_ALLOW => lang('not allow'),
  50. NewsConst::NEWS_COMMENT_STATUS_ALLOW => lang('allow')
  51. ];
  52. $this->assign('comment_status_data', $comment_status);
  53. return $comment_status;
  54. }
  55. /***
  56. * 是否置顶
  57. */
  58. protected function _is_top() {
  59. $is_top = [
  60. NewsConst::NEWS_IS_NOT_TOP => lang('not top'),
  61. NewsConst::NEWS_IS_TOP => lang('top')
  62. ];
  63. $this->assign('is_top_data', $is_top);
  64. return $is_top;
  65. }
  66. /***
  67. * 是否推荐
  68. */
  69. protected function _recommended() {
  70. $recommended = [
  71. NewsConst::NEWS_IS_NOT_RECOMMEND => lang('not recommend'),
  72. NewsConst::NEWS_IS_RECOMMEND => lang('recommend')
  73. ];
  74. $this->assign('recommended_data', $recommended);
  75. return $recommended;
  76. }
  77. /**
  78. * 是否站外活动
  79. */
  80. protected function _active_type() {
  81. $_type = array(
  82. NewsConst::NEWS_STATION_ACTIVITY => lang('station_activity'),
  83. NewsConst::NEWS_STATION_OUT_ACTIVITY => lang('station_out_activity')
  84. );
  85. $this->assign('active_type_data', $_type);
  86. return $_type;
  87. }
  88. public function index() {
  89. $param = $this->param;
  90. /*搜索*/
  91. $_keyword_input = Filter::text('keyword', $this->request->param('keyword/s', ''), lang('keywords'));
  92. $this->assign('keyword_input', $_keyword_input);
  93. $_param['start_time'] = $this->request->param('start_time', date('Y-m-d', strtotime('-1 month')));
  94. $_param['end_time'] = $this->request->param('end_time', date('Y-m-d'));
  95. $this->_time($_param['start_time'], $_param['end_time']);
  96. $param['keyword'] = isset($param['keyword']) ? trim($param['keyword']) : '';
  97. try {
  98. $data = (new PostsLogic())->getListByAdmin($param);
  99. } catch (DbException $e) {
  100. return $this->adminError(CommonStatus::getMsg(CommonStatus::DB_EXCEPTION));
  101. }
  102. $_post_type = isset($param['post_type']) ? $param['post_type'] : 0;
  103. $this->assign('pages', $data->items());
  104. $this->assign('page', $data->render());
  105. if (NewsConst::NEWS_TYPE_NOTICE != $_post_type && NewsConst::NEWS_TYPE_AWARD != $_post_type
  106. && NewsConst::NEWS_TYPE_AGENTNOTICE != $_post_type) {
  107. $_app_id = isset($param['app_id']) ? $param['app_id'] : 0;
  108. $this->assign('app_id', $_app_id);
  109. $this->_games($_app_id, GameConst::GAME_STATUS_ON, CommonConst::CONST_NOT_DELETE);
  110. }
  111. }
  112. public function add() {
  113. $_post_type = isset($this->param['post_type']) ? $this->param['post_type'] : 1;
  114. if (NewsConst::NEWS_TYPE_NOTICE != $_post_type && NewsConst::NEWS_TYPE_AWARD != $_post_type
  115. && NewsConst::NEWS_TYPE_AGENTNOTICE != $_post_type) {
  116. $this->_games(0, GameConst::GAME_STATUS_ON, CommonConst::CONST_NOT_DELETE);
  117. $_recommended = Filter::radioCommon($this->_recommended(), 'post[recommended]', 1);
  118. $this->assign('recommended', $_recommended);
  119. } elseif (NewsConst::NEWS_TYPE_AWARD == $_post_type) {
  120. $_type = Filter::radioCommon($this->_active_type(), 'post[active_type]', 1);
  121. $this->assign('active_type', $_type);
  122. }
  123. $_comment_status = Filter::radioCommon($this->_comment_status(), 'post[comment_status]', 1);
  124. $_is_top = Filter::radioCommon($this->_is_top(), 'post[is_top]', 2);
  125. $this->assign('comment_status', $_comment_status);
  126. $this->assign('is_top', $_is_top);
  127. $this->assign('post_type', $_post_type);
  128. }
  129. public function addPost() {
  130. $data = $this->param;
  131. $result = $this->validate($data['post'], 'AdminPage');
  132. if ($result !== true) {
  133. $this->adminError($result);
  134. }
  135. $data['post']['app_id'] = $this->request->param('app_id/d', 0);
  136. if (5 == $data['post']['post_type']) { //有奖类型
  137. if (1 == $data['post']['active_type']) {
  138. $data['post']['url'] = '';
  139. } else {
  140. $data['post']['cost_integral'] = 0;
  141. unset($data['post']['more']['pointer']);
  142. unset($data['post']['more']['turntable']);
  143. }
  144. }
  145. $PostModel = new PostsModel();
  146. $PostModel->adminAddPage($data['post']);
  147. }
  148. /**
  149. * 编辑页面
  150. */
  151. public function edit() {
  152. $id = $this->id;
  153. try {
  154. $post = (new PostsLogic())->getInfoById($id);
  155. } catch (DataNotFoundException $e) {
  156. return $this->adminError(CommonStatus::getMsg(CommonStatus::DATA_NOT_FOUND_EXCEPTION));
  157. } catch (ModelNotFoundException $e) {
  158. return $this->adminError(CommonStatus::getMsg(CommonStatus::MODEL_NOT_FOUND_EXCEPTION));
  159. } catch (DbException $e) {
  160. return $this->adminError(CommonStatus::getMsg(CommonStatus::DB_EXCEPTION));
  161. } catch (Exception $e) {
  162. return $this->adminError(CommonStatus::getMsg(CommonStatus::EXCEPTION));
  163. }
  164. $this->assign('post', $post);
  165. $post['post_excerpt'];
  166. $_desc_list=explode("\n",$post['post_excerpt']);
  167. if (NewsConst::NEWS_TYPE_NOTICE != $post['post_type'] && NewsConst::NEWS_TYPE_AWARD != $post['post_type']
  168. && NewsConst::NEWS_TYPE_AGENTNOTICE != $post['post_type']) {
  169. $this->_games($post['app_id'], GameConst::GAME_STATUS_ON, CommonConst::CONST_NOT_DELETE);
  170. $_recommended = Filter::radioCommon($this->_recommended(), 'post[recommended]', $post['recommended']);
  171. $this->assign('recommended', $_recommended);
  172. } elseif (NewsConst::NEWS_TYPE_AWARD == $post['post_type']) {
  173. $_activity_type = NewsConst::NEWS_STATION_ACTIVITY;
  174. if ($post['url'] != '') {
  175. $_activity_type = NewsConst::NEWS_STATION_OUT_ACTIVITY;
  176. }
  177. $_type = Filter::radioCommon($this->_active_type(), 'post[active_type]', $_activity_type);
  178. $this->assign('active_type', $_type);
  179. }
  180. $_comment_status = Filter::radioCommon(
  181. $this->_comment_status(), 'post[comment_status]', $post['comment_status']
  182. );
  183. $_is_top = Filter::radioCommon($this->_is_top(), 'post[is_top]', $post['is_top']);
  184. $this->assign('comment_status', $_comment_status);
  185. $this->assign('is_top', $_is_top);
  186. }
  187. /**
  188. * 编辑页面提交
  189. */
  190. public function editPost() {
  191. $data = $this->param;
  192. $result = $this->validate($data['post'], 'AdminPage');
  193. if ($result !== true) {
  194. $this->adminError($result);
  195. }
  196. $data['post']['app_id'] = $this->request->param('app_id/d', 0);
  197. if (5 == $data['post']['post_type']) { //有奖类型
  198. if (1 == $data['post']['active_type']) {
  199. $data['post']['url'] = '';
  200. } else {
  201. $data['post']['cost_integral'] = 0;
  202. unset($data['post']['more']['pointer']);
  203. unset($data['post']['more']['turntable']);
  204. }
  205. }
  206. $PostModel = new PostsModel();
  207. $PostModel->adminEditPage($data['post']);
  208. }
  209. /**
  210. * 删除页面
  211. *
  212. * @author iyting@foxmail.com
  213. * @adminMenu(
  214. * 'name' => '删除页面',
  215. * 'parent' => 'index',
  216. * 'display'=> false,
  217. * 'hasView'=> false,
  218. * 'order' => 10000,
  219. * 'icon' => '',
  220. * 'remark' => '删除页面',
  221. * 'param' => ''
  222. * )
  223. */
  224. public function delete() {
  225. $PostModel = new PostsModel();
  226. $data = $this->param;
  227. try {
  228. $result = $PostModel->adminDeletePage($data);
  229. } catch (DataNotFoundException $e) {
  230. return $this->adminError(CommonStatus::getMsg(CommonStatus::DATA_NOT_FOUND_EXCEPTION));
  231. } catch (ModelNotFoundException $e) {
  232. return $this->adminError(CommonStatus::getMsg(CommonStatus::MODEL_NOT_FOUND_EXCEPTION));
  233. } catch (DbException $e) {
  234. return $this->adminError(CommonStatus::getMsg(CommonStatus::DB_EXCEPTION));
  235. }
  236. if ($result) {
  237. $this->adminSuccess(lang('DELETE_SUCCESS'));
  238. } else {
  239. $this->adminError(lang('DELETE_FAILED'));
  240. }
  241. }
  242. /**
  243. * 修改发布状态
  244. */
  245. public function setPostStatus() {
  246. $_param = $this->request->param();
  247. $_map['id'] = $_param['id'];
  248. $_data['post_status'] = $_param['status'];
  249. if (2 == $_data['post_status']) {
  250. $_data['published_time'] = time();
  251. }
  252. $_data['update_time'] = time();
  253. $PostModel = new PostsModel();
  254. $_res = $PostModel->useGlobalScope(false)->where($_map)->setField($_data);
  255. if (true == $_res) {
  256. $this->adminSuccess(lang('edit success'));
  257. } else {
  258. $this->adminError(lang('edit failure'));
  259. }
  260. }
  261. }