* @version : HUOSDK 8.0 */ namespace huo\logic\posts; use huo\model\common\CommonModel; use huo\model\posts\PostsModel; use huolib\constant\CommonConst; use huolib\constant\NewsConst; use huolib\status\CommonStatus; use think\db\exception\DataNotFoundException; use think\db\exception\ModelNotFoundException; use think\exception\DbException; use think\Log; class PostsLogic extends CommonModel { protected $base_field = [ 'id' => 'news_id', 'post_title' => 'title', 'post_excerpt' => 'excerpt', 'app_id' => 'game_id', 'published_time' => 'pub_time', 'more' => 'more', 'post_type' => 'type', 'start_time' => 'start_time', 'end_time' => 'end_time', 'url' => 'url', 'cost_integral' => 'cost_integral', 'is_top' => 'is_top', ]; /** * 获取资讯列表 * * @param array $where * @param string $page * @param string $order * * @return array|int */ public function getList($where = [], $page = '1,10', $order = '') { $_rdata = [ 'count' => 0, 'list' => [] ]; $_map = []; if (!empty($where['app_id'])) { $_map['app_id'] = $where['app_id']; } if (!empty($where['post_type'])) { $_map['post_type'] = $where['post_type']; } $_map['id'] = array('neq', 0); $_param['where'] = $_map; $_param['page'] = $page; $_param['order'] = $order; $_posts_model = new PostsModel(); $_rdata['count'] = $_posts_model->where($_map)->count(); if (empty($_rdata['count'])) { return $_rdata; } $_field = $this->base_field; $_order = $this->orderFilter($order); // try { $_datas = $_posts_model->field($_field)->where($_map)->page($page)->order($_order)->select(); if (is_object($_datas)) { $_datas = $_datas->toArray(); } if (empty($_datas)) { return $_rdata; } foreach ($_datas as $_k => $_data) { $_datas[$_k]['img'] = isset($_data['more']['thumbnail']) ? $_data['more']['thumbnail'] : ''; if ($_data['start_time'] > time()) { $_datas[$_k]['status'] = 1; } elseif ($_data['end_time'] < time()) { $_datas[$_k]['status'] = 2; } else { $_datas[$_k]['status'] = 3; } } $_rdata['list'] = $_datas; return $_rdata; // } catch (DataNotFoundException $_e) { // Log::write( // "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DATA_NOT_FOUND_EXCEPTION."&Exception" // .$_e->getMessage(), // LOG::ERROR // ); // // return $_rdata; // } catch (ModelNotFoundException $_e) { // Log::write( // "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::MODEL_NOT_FOUND_EXCEPTION."&Exception" // .$_e->getMessage(), // LOG::ERROR // ); // // return $_rdata; // } catch (DbException $_e) { // Log::write( // "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DB_EXCEPTION."&Exception" // .$_e->getMessage(), // LOG::ERROR // ); // // return $_rdata; // } } /** * 获取详情,post_id和type必须传入一个 * * @param int $post_id * * @param int $type * * @return int */ public function getDetail($post_id = 0, $type = 0) { $_rdata = null; $_field = $this->base_field; $_field['post_content'] = 'content'; $_field['post_status'] = 'status'; $_map['id'] = $post_id; if (!empty($type)) { $_map['post_type'] = $type; } $_posts_model = new PostsModel(); try { $_data = $_posts_model->useGlobalScope(false)->field($_field)->where($_map)->limit(1)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (empty($_data)) { return $_rdata; } $_data['img'] = isset($_data['more']['thumbnail']) ? $_data['more']['thumbnail'] : ''; $_data['pointer'] = isset($_data['more']['pointer']) ? $_data['more']['pointer'] : ''; $_data['turntable'] = isset($_data['more']['turntable']) ? $_data['more']['turntable'] : ''; if ($_data['start_time'] > time()) { $_data['status'] = 1; } elseif ($_data['end_time'] < time()) { $_data['status'] = 2; } else { $_data['status'] = 3; } return $_data; } catch (DataNotFoundException $_e) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DATA_NOT_FOUND_EXCEPTION."&Exception" .$_e->getMessage(), LOG::ERROR ); return $_rdata; } catch (ModelNotFoundException $_e) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::MODEL_NOT_FOUND_EXCEPTION."&Exception" .$_e->getMessage(), LOG::ERROR ); return $_rdata; } catch (DbException $_e) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&code=".CommonStatus::DB_EXCEPTION."&Exception" .$_e->getMessage(), LOG::ERROR ); return $_rdata; } } /*** * 后台获取资讯列表 * * @param $where * * @return \think\Paginator * @throws DbException */ public function getListByAdmin($where) { $_map = []; if (!empty($where['app_id'])) { $_map['posts_model.app_id'] = $where['app_id']; } if (!empty($where['post_type'])) { $_map['posts_model.post_type'] = $where['post_type']; } if (!empty($where['keyword'])) { $_map['posts_model.post_title'] = ['like', '%'.$where['keyword'].'%']; } if (!empty($where['start_time']) && !empty($where['end_time'])) { $_map['posts_model.published_time'] = [ 'between', [ strtotime($where['start_time']), CommonConst::CONST_DAY_SECONDS + strtotime($where['end_time']) ] ]; } elseif (!empty($where['start_time'])) { $_map['posts_model.published_time'] = ['egt', strtotime($where['start_time'])]; } elseif (!empty($where['end_time'])) { $_map['posts_model.published_time'] = ['elt', CommonConst::CONST_DAY_SECONDS + strtotime($where['end_time'])]; } if (!empty($where['id'])) { $_map['posts_model.id'] = $where['id']; } elseif (!empty($where['except_id'])) { $_map['posts_model.id'] = ['not in', [0, $where['except_id']]]; } $_map['posts_model.delete_time'] = 0; $_posts_model = new PostsModel(); $_datas = $_posts_model ->useGlobalScope(false) ->with('user') ->where($_map) ->order('post_status ASC,published_time DESC') ->paginate(); return $_datas; } /*** * 获取文章详情 * * @param $id * * @return array|false|\PDOStatement|string|\think\Model */ public function getInfoById($id) { $_data = (new PostsModel())->useGlobalScope(false)->where('id', $id)->limit(1)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } return $_data; } /** * 通过ID获取ID 与title * * @param $app_id * @param array $types 类型 * * @return array */ public function getTitleByAppId( $app_id, $types = [NewsConst::NEWS_TYPE_NEWS, NewsConst::NEWS_TYPE_STRATEGY, NewsConst::NEWS_TYPE_ACTIVITY, NewsConst::NEWS_TYPE_NOTICE] ) { if (empty($app_id)) { return ['title' => '', 'news_id' => null]; } $_field = 'post_title title, id news_id'; $_map['app_id'] = $app_id; $_map['post_type'] = ['in', $types]; $_data = (new PostsModel())->field($_field)->where($_map)->order('published_time desc')->find(); if (empty($_data)) { return ['title' => '', 'news_id' => null]; } return $_data; } /** * 获取上一篇 和下一篇 id,name * * @param $id * @param array $type * @param int $limit * * @return array */ public function getTop($id, $type = [], $limit = 2) { $_map = []; if (!empty($type)) { $_map['post_type'] = ['in', $type]; } $_map['id'] = ['neq', $id]; $_data = (new PostsModel())->where($_map) ->order('published_time DESC') ->limit($limit) ->column('id,post_title,published_time'); return $_data; } }