// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model; use cores\BaseModel; use think\model\Collection; use think\model\relation\HasOne; use think\Paginator; /** * 用户动态模型类 * Class UserDynamic * @package app\common\model */ class UserDynamic extends BaseModel { protected $globalScope = ['']; // 定义表名 protected $name = 'user_dynamic'; // 定义主键 protected $pk = 'id'; /** * 关联用户 * @return HasOne */ public function user(): HasOne { return $this->HasOne('User', 'user_id', 'user_id') ->field('user_id,nick_name, avatar_id,user_type') ->bind(['nick_name']); } /** * 获取列表 * @param array $param 查询条件 * @param int $listRows 分页数量 * @return mixed * @throws \think\db\exception\DbException */ public function getList(array $param = [], int $listRows = 15) { // 筛选条件 $query = $this->getQueryFilter($param); // 排序条件 $sort = $this->setQuerySort($param); // 执行查询 $list = $query->alias($this->name) ->where(function ($query) use ($param) { // 访问权限 $userId = isset($param['access_user_id']) ? $param['access_user_id'] : 0; if ($userId > 0) { // 获取粉丝用户ID,并验证 $query->where(function ($query) use ($userId) { // 粉丝只能看公开或好友可看 $query->whereIn($this->name . '.user_id', \app\api\model\UserFans::getFansUid($userId)) ->whereIn($this->name . '.look_type', [1, 2]); })->whereOr(function ($query) use ($userId) { // 用户自己所有 $query->where([$this->name . '.user_id' => $userId]); })->whereOr(function ($query) { // 公开对所有 $query->where([$this->name . '.look_type' => 1]); }); } }) ->order($sort) ->paginate($listRows); // 整理列表数据并返回 return $list; } /** * 获取学校下的列表 * @param int $schoolId * @param int 当前用户ID,用户过滤好友可看动态,需param传lookUids字段过滤 * @param array $param * @param int $listRows * @return mixed */ public function getListBySchool(int $schoolId, int $userId = 0, array $param = [], int $listRows = 15) { // 筛选条件 $query = $this->getQueryFilter($param); // 排序条件 $sort = $this->setQuerySort($param); // 执行查询 $list = $query->alias($this->name) ->leftJoin('user_info ui', 'ui.user_id=' . $this->name . '.user_id') ->leftJoin('user u', 'u.user_id=' . $this->name . '.user_id') ->where(function ($query) use ($userId, $param) { if ($userId > 0) { // 获取粉丝用户ID,并验证 $query->where(function ($query) use ($userId) { // 粉丝只能看公开或好友可看 $query->whereIn($this->name . '.user_id', \app\api\model\UserFans::getFansUid($userId)) ->whereIn($this->name . '.look_type', [1, 2]); })->whereOr(function ($query) use ($userId) { // 用户自己所有 $query->where([$this->name . '.user_id' => $userId]); })->whereOr(function ($query) { // 公开对所有 $query->where([$this->name . '.look_type' => 1]); }); } }) ->field($this->name . '.*,u.nick_name,u.avatar_id, ui.school_id') ->order($sort) ->paginate($listRows); // echo $query->getLastSql(); // 整理列表数据并返回 return $list; } /** * 设置商品展示的数据 * @param Collection|Paginator $list 商品列表 * @param callable|null $callback 回调函数 * @return mixed */ protected function setListData($list, callable $callback = null) { if ($list->isEmpty()) return $list; // 遍历商品列表整理数据 foreach ($list as &$item) { $data = $this->setData($item, $callback); } return $list; } /** * 整理数据 * @param Collection|static $info * @param callable|null $callback * @return mixed */ protected function setData($info, callable $callback = null) { // 回调函数 is_callable($callback) && call_user_func($callback, $info); return $info->hidden(array_merge($this->hidden, ['status'])); } /** * 检索查询条件 * @param array $params * @return \think\db\BaseQuery */ private function getQueryFilter(array $params) { $filter = []; // 实例化新查询对象 $query = $this->getNewQuery(); // 学校 !empty($params['ui.school_id']) && $filter[] = ['ui.school_id', '=', "{$params['ui.school_id']}"]; // 状态 !empty($params[$this->name . '.status']) && $filter[] = [$this->name . '.status', '=', "{$params[$this->name.'.status']}"]; // 浏览类型 !empty($params['look_type']) && $filter[] = ['look_type', '=', "{$params['look_type']}"]; // 实例化新查询对象 return $query->where($filter)->where(function ($query) use ($params) { // 关键词 if (!empty($params['keyword'])) { $query->where('content', 'like', "%{$params['keyword']}%"); } if (!empty($params['tag'])) { $query->where('content', 'like', "%#{$params['tag']}%"); } // 主页分类动态 $type = isset($params['type'])? $params['type'] : 0; $userId = isset($params['user_id'])? $params['user_id'] : 0; if($type == 1){ // 我发布的 $query->where($this->name . '.user_id','=', $userId); $query->whereIn('look_type',[1,2]); }else if($type == 2){ // 我私密的 $query->where($this->name . '.user_id','=', $userId); $query->where('look_type','=',3); }else if($type == 3 && $userId>0){ // 我关注收藏的 $query->whereIn('id', \app\api\model\UserDynamicCollect::getCollectDynamicIds($userId, 1)); }else if($type == 4 && $userId){ // 我点赞喜欢的 $query->whereIn('id', \app\api\model\UserDynamicCollect::getCollectDynamicIds($userId, 2)); }else if($userId>0){ // 发布用户 $query->where($this->name . '.user_id','=', $userId); } }); } /** * 检索排序条件 * @param array $param * @return array|string[] */ private function setQuerySort(array $param = []) { $params = $this->setQueryDefaultValue($param, [ 'sortType' => 'all', // 排序类型 $this->name . '.create_time' => false, // 热门排序 (true高到低 false低到高) ]); // 排序规则 $sort = []; if ($params['sortType'] === 'all') { $sort = [$this->name . '.create_time' => 'desc']; } elseif ($params['sortType'] === 'view') { $sort = [$this->name . '.views' => 'desc']; } return array_merge($sort, [$this->getPk() => 'desc']); } /** * 封面览图 * @param $value * @return string|string[]|null */ public function getImageAttr($value): string { return $value ? getPreview($value) : ''; } /** * 资源文件 * @param $value * @return string|string[]|null */ public function getFileUrlAttr($value): string { return $value ? getPreview($value) : ''; } /** * 详情 * @param $where * @param array $with * @return array|null|static */ public static function detail($where, array $with = []) { $filter = []; if (is_array($where)) { $filter = array_merge($filter, $where); } else { $filter['id'] = (int)$where; } return static::get($filter, $with); } /** * 今日新增数量 * @return int */ public static function getTodayCounts() { return self::where(['status'=> 1]) ->where('create_time','>=', strtotime(date('Y-m-d'))) ->count('id'); } }