| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\model;
- use app\common\model\UserDynamicCollect as UserDynamicCollectModel;
- /**
- * 用户动态点赞收藏模型类
- * Class UserDynamicCollect
- * @package app\api\model
- */
- class UserDynamicCollect extends UserDynamicCollectModel
- {
- protected $globalScope = [''];
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'update_time'
- ];
- /**
- * 获取列表
- * @param array $param
- * @param int $listRows
- * @return mixed
- * @throws \think\db\exception\DbException
- */
- public function getList(array $param = [], int $listRows = 15)
- {
- // 整理查询参数
- $params = array_merge($param, ['status' => 1]);
- // 获取商品列表
- $list = parent::getList($params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 隐藏冗余的字段
- $list->hidden(array_merge($this->hidden, ['status']));
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info)
- {
- return $this->setListData($info, function ($data) {
- // 整理数据 api模块
- $this->setDataFromApi($data);
- // 隐藏冗余的字段
- $this->hidden(array_merge($this->hidden, ['avatar_id']));
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info)
- {
- return $this->setData($info, function ($data) {
- $data['image'] = $data['image']? getPreview($data['image']) : '';
- $data['create_time_text'] = $data['create_time']? getTimeText(strtotime($data['create_time'])) : '';
- $avatarData = $data['avatar_id']? UploadFile::detail($data['avatar_id']) : [];
- $data['avatar_url'] = isset($avatarData['preview_url'])? $avatarData['preview_url'] : '';
- // 最新点赞用户
- $data['users'] = self::getUsers($data['dynamic_id'], $data['type']);
- // 未读数量
- $data['unread_num'] = self::getCounts($data['dynamic_id'], $data['type'], 2);
- });
- }
- /**
- * 获取学校动态点赞用户列表
- * @param int $schoolId
- * @param array $param
- * @param int $listRows
- * @return mixed
- */
- public function getListByUser(int $userId = 0, array $param = [], int $listRows = 15)
- {
- // 整理查询参数
- $params = array_merge($param, [$this->name.'.status' => 1]);
- // 获取列表
- $list = parent::alias($this->name)
- ->leftJoin('user_dynamic d','d.id='.$this->name.'.dynamic_id')
- ->leftJoin('user u','u.user_id='.'d.user_id')
- ->where([$this->name.'.status'=>1,'d.user_id'=> $userId,'d.status'=>1])
- ->where(function($query) use($param){
- // 关键词
- if(!empty($param['keyword'])){
- $query->where('content','like', "%{$param['keyword']}%");
- }
- if(!empty($param['type'])){
- $query->where($this->name.'.type','=', intval($param['type']));
- }
- })
- ->field('d.id as dynamic_id,d.image,d.content,d.user_id,u.nick_name,u.avatar_id,'.$this->name.'.create_time,'.$this->name.'.type')
- ->group($this->name.'.dynamic_id')
- ->order($this->name.'.create_time desc, d.id desc')
- ->paginate($listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 隐藏冗余的字段
- $list->hidden(array_merge($this->hidden, ['status']));
- // 整理列表数据并返回
- return $this->setListDataFromApi($list, $params);
- }
- /**
- * 获取点赞收藏数
- * @param $dynamicId
- * @param int $type
- * @param int $limit
- * @return int
- */
- public static function getCounts($dynamicId, $type=1, $isRead=2)
- {
- $where = ['dynamic_id'=> $dynamicId,'type'=> $type,'status'=>1];
- if($isRead){
- $where['is_read'] = $isRead;
- }
- return static::where($where)->count('user_id');
- }
- /**
- * 获取我收藏或点赞的动态数量,过滤收藏的已不可访问作品
- * @param $userId 用户ID
- * @param int $type 类型:1-收藏关注,2-点赞喜欢
- * @return mixed
- */
- public static function getCountsByType($userId, $type=1)
- {
- $count = self::alias('a')
- ->leftJoin('user_dynamic d','d.id=a.dynamic_id')
- ->where(['a.type'=> $type,'a.user_id'=> $userId,'a.status'=>1,'d.status'=> 1])
- ->where(function($query) use($userId){
- // 获取粉丝用户ID,并验证
- $query->where(function($query) use($userId){
- // 粉丝只能看公开或好友可看
- $query->whereIn('d.user_id', \app\api\model\UserFans::getFansUid($userId))
- ->whereIn('d.look_type',[1,2]);
- })->whereOr(function($query) use ($userId){
- // 用户自己所有
- $query->where(['d.user_id'=> $userId]);
- })->whereOr(function($query){
- // 公开对所有
- $query->where(['d.look_type'=> 1]);
- });
- })->count('a.dynamic_id');
- return $count;
- }
- /**
- * 最新收藏点赞用户
- * @param $dynamicId
- * @param int $limit
- * @return UserDynamicCollect[]|array|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getUsers($dynamicId, $type=1, $limit=6)
- {
- return static::alias('a')
- ->leftJoin('user u','u.user_id=a.user_id')
- ->where(['a.dynamic_id'=> $dynamicId,'a.type'=> $type ,'a.status'=>1,'u.is_delete'=>0])
- ->field('a.id,a.dynamic_id,a.type,u.user_id,u.nick_name,u.avatar_id')
- ->order('a.is_read desc, a.create_time desc')
- ->limit($limit)
- ->select()
- ->each(function($item, $k){
- $avatarData = $item['avatar_id']? UploadFile::detail($item['avatar_id']) : [];
- $item['avatar_url'] = isset($avatarData['preview_url'])? $avatarData['preview_url'] : '';
- unset($item['avatar_id']);
- });
- }
- /**
- * 我关注的动态
- * @param $userId
- * @param $type 类型:1-关注收藏,2-点赞喜欢
- * @return array
- */
- public static function getCollectDynamicIds($userId, $type=1)
- {
- $where = ['user_id'=> $userId,'status'=> 1];
- if($type>0){
- $where['type'] = $type;
- }
- return static::where($where)->column('dynamic_id');
- }
- }
|