* @version : HUOSDK 8.0 */ namespace huo\model\posts; use huo\model\common\CommonModel; use huolib\constant\NewsConst; class PostsModel extends CommonModel { protected $name = 'posts'; //可查询字段 // protected $visible // = [ // 'id', 'app_id', 'user_id', 'post_id', 'post_type', 'comment_status', // 'is_top', 'recommended', 'post_hits', 'post_like', 'comment_count', // 'create_time', 'update_time', 'published_time', 'post_title', 'post_keywords', // 'post_excerpt', 'post_source', 'post_content', 'more', 'user_nickname', // 'user', 'category_id' // ]; //设置只读字段 protected $readonly = ['user_id']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; //类型转换 protected $type = [ 'more' => 'array', ]; /** * 基础查询 */ protected function base($query) { $query->where('delete_time', 0) ->where('post_status', 2) ->whereTime('published_time', 'between', [1, time()]); } /** * 关联game表 * * @return \think\model\relation\BelongsTo */ public function game() { return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name'); } /** * 关联user表 * * @return \think\model\relation\HasOne */ public function user() { return $this->hasOne('huo\model\user\UserModel', 'id', 'user_id', [], 'left')->setEagerlyType(0); } /** * post_content 自动转化 * * @param $value * * @return string */ public function getPostContentAttr($value) { return cmf_replace_content_file_url(htmlspecialchars_decode($value)); } /** * post_content 自动转化 * * @param $value * * @return string */ public function getContentAttr($value) { return cmf_replace_content_file_url(htmlspecialchars_decode($value)); } /** * post_content 自动转化 * * @param $value * * @return string */ public function setPostContentAttr($value) { return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true)); } /** * @param $value * * @return array|bool|mixed */ public function getTypeAttr($value) { $_rs = NewsConst::getNewsMsg($value); if (false == $_rs) { return $value; } return $_rs; } /** * more 自动转化 * * @param $value * * @return array */ public function getMoreAttr($value) { $more = json_decode($value, true); if (!empty($more['thumbnail'])) { $more['thumbnail'] = cmf_get_image_url($more['thumbnail']); } if (!empty($more['photos'])) { foreach ($more['photos'] as $key => $value) { $more['photos'][$key]['url'] = cmf_get_image_url($value['url']); } } if (!empty($more['files'])) { foreach ($more['files'] as $key => $value) { $more['files'][$key]['url'] = cmf_get_image_url($value['url']); } } //积分抽奖 if (!empty($more['turntable'])) { $more['turntable'] = cmf_get_image_url($more['turntable']); } if (!empty($more['pointer'])) { $more['pointer'] = cmf_get_image_url($more['pointer']); } return $more; } /** * published_time 自动完成 * * @param $value * * @return false|int */ public function setPublishedTimeAttr($value) { return strtotime($value); } /** * published_time 自动完成 * * @param $value * * @return false|int */ public function setStartTimeAttr($value) { return strtotime($value); } /** * published_time 自动完成 * * @param $value * * @return false|int */ public function setEndTimeAttr($value) { return strtotime($value); } /** * 后台管理添加页面 * * @param array $data 页面数据 * * @return false|int */ public function adminAddPage($data) { $data['user_id'] = cmf_get_current_admin_id(); $data['post_status'] = empty($data['post_status']) ? 1 : 2; $_rs = $this->allowField(true)->data($data, true)->save(); return $_rs; } /** * 后台管理编辑页面 * * @param array $data 页面数据 * * @return false|int */ public function adminEditPage($data) { $data['user_id'] = cmf_get_current_admin_id(); $data['post_status'] = empty($data['post_status']) ? 1 : 2; $data['update_time'] = time(); $_rs = $this->allowField(true)->isUpdate(true)->data($data, true)->save(); return $_rs; } /** * 删除文章 * * @param $data * * @return bool */ public function adminDeletePage($data) { if (isset($data['id'])) { $id = $data['id']; //获取删除id $_rs = $this->useGlobalScope(false)->where(['id' => $id])->update( [ 'delete_time' => time() ] ); } elseif (isset($data['ids'])) { $ids = $data['ids']; $_rs = $this->useGlobalScope(false)->where(['id' => ['in', $ids]]) ->update( [ 'delete_time' => time() ] ); } else { return false; } return $_rs; } /** * 根据条件获取idName * * @param array $_map * * @return array */ public function getIdName($_map = []) { $_rs = $this->where($_map)->column('post_title', 'id'); return $_rs; } }