| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- 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 $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')
- ->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 . '.*, 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');
- }
- }
|