PostsLogic.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * PostLogic.php UTF-8
  4. * 文章逻辑
  5. *
  6. * @date : 2017/11/23 21:22
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\posts;
  13. use huo\model\common\CommonModel;
  14. use huo\model\posts\PostsModel;
  15. use huolib\constant\CommonConst;
  16. use huolib\constant\NewsConst;
  17. use huolib\status\CommonStatus;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\exception\DbException;
  21. use think\Log;
  22. class PostsLogic extends CommonModel {
  23. protected $base_field
  24. = [
  25. 'id' => 'news_id',
  26. 'post_title' => 'title',
  27. 'post_excerpt' => 'excerpt',
  28. 'app_id' => 'game_id',
  29. 'published_time' => 'pub_time',
  30. 'more' => 'more',
  31. 'post_type' => 'type',
  32. 'start_time' => 'start_time',
  33. 'end_time' => 'end_time',
  34. 'url' => 'url',
  35. 'cost_integral' => 'cost_integral',
  36. 'is_top' => 'is_top',
  37. ];
  38. /**
  39. * 获取资讯列表
  40. *
  41. * @param array $where
  42. * @param string $page
  43. * @param string $order
  44. *
  45. * @return array|int
  46. */
  47. public function getList($where = [], $page = '1,10', $order = '') {
  48. $_rdata = [
  49. 'count' => 0,
  50. 'list' => []
  51. ];
  52. $_map = [];
  53. if (!empty($where['app_id'])) {
  54. $_map['app_id'] = $where['app_id'];
  55. }
  56. if (!empty($where['post_type'])) {
  57. $_map['post_type'] = $where['post_type'];
  58. }
  59. $_map['id'] = array('neq', 0);
  60. $_param['where'] = $_map;
  61. $_param['page'] = $page;
  62. $_param['order'] = $order;
  63. $_posts_model = new PostsModel();
  64. $_rdata['count'] = $_posts_model->where($_map)->count();
  65. if (empty($_rdata['count'])) {
  66. return $_rdata;
  67. }
  68. $_field = $this->base_field;
  69. $_order = $this->orderFilter($order);
  70. // try {
  71. $_datas = $_posts_model->field($_field)->where($_map)->page($page)->order($_order)->select();
  72. if (is_object($_datas)) {
  73. $_datas = $_datas->toArray();
  74. }
  75. if (empty($_datas)) {
  76. return $_rdata;
  77. }
  78. foreach ($_datas as $_k => $_data) {
  79. $_datas[$_k]['img'] = isset($_data['more']['thumbnail']) ? $_data['more']['thumbnail'] : '';
  80. if ($_data['start_time'] > time()) {
  81. $_datas[$_k]['status'] = 1;
  82. } elseif ($_data['end_time'] < time()) {
  83. $_datas[$_k]['status'] = 2;
  84. } else {
  85. $_datas[$_k]['status'] = 3;
  86. }
  87. }
  88. $_rdata['list'] = $_datas;
  89. return $_rdata;
  90. // } catch (DataNotFoundException $_e) {
  91. // Log::write(
  92. // "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DATA_NOT_FOUND_EXCEPTION."&Exception"
  93. // .$_e->getMessage(),
  94. // LOG::ERROR
  95. // );
  96. //
  97. // return $_rdata;
  98. // } catch (ModelNotFoundException $_e) {
  99. // Log::write(
  100. // "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::MODEL_NOT_FOUND_EXCEPTION."&Exception"
  101. // .$_e->getMessage(),
  102. // LOG::ERROR
  103. // );
  104. //
  105. // return $_rdata;
  106. // } catch (DbException $_e) {
  107. // Log::write(
  108. // "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DB_EXCEPTION."&Exception"
  109. // .$_e->getMessage(),
  110. // LOG::ERROR
  111. // );
  112. //
  113. // return $_rdata;
  114. // }
  115. }
  116. /**
  117. * 获取详情,post_id和type必须传入一个
  118. *
  119. * @param int $post_id
  120. *
  121. * @param int $type
  122. *
  123. * @return int
  124. */
  125. public function getDetail($post_id = 0, $type = 0) {
  126. $_rdata = null;
  127. $_field = $this->base_field;
  128. $_field['post_content'] = 'content';
  129. $_field['post_status'] = 'status';
  130. $_map['id'] = $post_id;
  131. if (!empty($type)) {
  132. $_map['post_type'] = $type;
  133. }
  134. $_posts_model = new PostsModel();
  135. try {
  136. $_data = $_posts_model->useGlobalScope(false)->field($_field)->where($_map)->limit(1)->find();
  137. if (is_object($_data)) {
  138. $_data = $_data->toArray();
  139. }
  140. if (empty($_data)) {
  141. return $_rdata;
  142. }
  143. $_data['img'] = isset($_data['more']['thumbnail']) ? $_data['more']['thumbnail'] : '';
  144. $_data['pointer'] = isset($_data['more']['pointer']) ? $_data['more']['pointer'] : '';
  145. $_data['turntable'] = isset($_data['more']['turntable']) ? $_data['more']['turntable'] : '';
  146. if ($_data['start_time'] > time()) {
  147. $_data['status'] = 1;
  148. } elseif ($_data['end_time'] < time()) {
  149. $_data['status'] = 2;
  150. } else {
  151. $_data['status'] = 3;
  152. }
  153. return $_data;
  154. } catch (DataNotFoundException $_e) {
  155. Log::write(
  156. "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DATA_NOT_FOUND_EXCEPTION."&Exception"
  157. .$_e->getMessage(),
  158. LOG::ERROR
  159. );
  160. return $_rdata;
  161. } catch (ModelNotFoundException $_e) {
  162. Log::write(
  163. "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::MODEL_NOT_FOUND_EXCEPTION."&Exception"
  164. .$_e->getMessage(),
  165. LOG::ERROR
  166. );
  167. return $_rdata;
  168. } catch (DbException $_e) {
  169. Log::write(
  170. "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DB_EXCEPTION."&Exception"
  171. .$_e->getMessage(),
  172. LOG::ERROR
  173. );
  174. return $_rdata;
  175. }
  176. }
  177. /***
  178. * 后台获取资讯列表
  179. *
  180. * @param $where
  181. *
  182. * @return \think\Paginator
  183. * @throws DbException
  184. */
  185. public function getListByAdmin($where) {
  186. $_map = [];
  187. if (!empty($where['app_id'])) {
  188. $_map['posts_model.app_id'] = $where['app_id'];
  189. }
  190. if (!empty($where['post_type'])) {
  191. $_map['posts_model.post_type'] = $where['post_type'];
  192. }
  193. if (!empty($where['keyword'])) {
  194. $_map['posts_model.post_title'] = ['like', '%'.$where['keyword'].'%'];
  195. }
  196. if (!empty($where['start_time']) && !empty($where['end_time'])) {
  197. $_map['posts_model.published_time']
  198. = [
  199. 'between',
  200. [
  201. strtotime($where['start_time']),
  202. CommonConst::CONST_DAY_SECONDS + strtotime($where['end_time'])
  203. ]
  204. ];
  205. } elseif (!empty($where['start_time'])) {
  206. $_map['posts_model.published_time'] = ['egt', strtotime($where['start_time'])];
  207. } elseif (!empty($where['end_time'])) {
  208. $_map['posts_model.published_time'] = ['elt',
  209. CommonConst::CONST_DAY_SECONDS + strtotime($where['end_time'])];
  210. }
  211. if (!empty($where['id'])) {
  212. $_map['posts_model.id'] = $where['id'];
  213. } elseif (!empty($where['except_id'])) {
  214. $_map['posts_model.id'] = ['not in', [0, $where['except_id']]];
  215. }
  216. $_map['posts_model.delete_time'] = 0;
  217. $_posts_model = new PostsModel();
  218. $_datas = $_posts_model
  219. ->useGlobalScope(false)
  220. ->with('user')
  221. ->where($_map)
  222. ->order('post_status ASC,published_time DESC')
  223. ->paginate();
  224. return $_datas;
  225. }
  226. /***
  227. * 获取文章详情
  228. *
  229. * @param $id
  230. *
  231. * @return array|false|\PDOStatement|string|\think\Model
  232. */
  233. public function getInfoById($id) {
  234. $_data = (new PostsModel())->useGlobalScope(false)->where('id', $id)->limit(1)->find();
  235. if (is_object($_data)) {
  236. $_data = $_data->toArray();
  237. }
  238. return $_data;
  239. }
  240. /**
  241. * 通过ID获取ID 与title
  242. *
  243. * @param $app_id
  244. * @param array $types 类型
  245. *
  246. * @return array
  247. */
  248. public function getTitleByAppId(
  249. $app_id, $types
  250. = [NewsConst::NEWS_TYPE_NEWS, NewsConst::NEWS_TYPE_STRATEGY, NewsConst::NEWS_TYPE_ACTIVITY,
  251. NewsConst::NEWS_TYPE_NOTICE]
  252. ) {
  253. if (empty($app_id)) {
  254. return ['title' => '', 'news_id' => null];
  255. }
  256. $_field = 'post_title title, id news_id';
  257. $_map['app_id'] = $app_id;
  258. $_map['post_type'] = ['in', $types];
  259. $_data = (new PostsModel())->field($_field)->where($_map)->order('published_time desc')->find();
  260. if (empty($_data)) {
  261. return ['title' => '', 'news_id' => null];
  262. }
  263. return $_data;
  264. }
  265. /**
  266. * 获取上一篇 和下一篇 id,name
  267. *
  268. * @param $id
  269. * @param array $type
  270. * @param int $limit
  271. *
  272. * @return array
  273. */
  274. public function getTop($id, $type = [], $limit = 2) {
  275. $_map = [];
  276. if (!empty($type)) {
  277. $_map['post_type'] = ['in', $type];
  278. }
  279. $_map['id'] = ['neq', $id];
  280. $_data = (new PostsModel())->where($_map)
  281. ->order('published_time DESC')
  282. ->limit($limit)
  283. ->column('id,post_title,published_time');
  284. return $_data;
  285. }
  286. }