| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?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\UserDynamicComment as UserDynamicCommentModel;
- /**
- * 用户动态评论模型类
- * Class UserDynamicComment
- * @package app\api\model
- */
- class UserDynamicComment extends UserDynamicCommentModel
- {
- 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'], 2);
- // 未读数量
- $data['unread_num'] = self::getCounts($data['dynamic_id'], 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')
- ->leftJoin('user cu','cu.user_id='.$this->name.'.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']}%");
- }
- })
- ->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.'.content as comment,cu.nick_name as comment_user_nickname')
- ->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 $limit
- * @return int
- */
- public static function getCounts($dynamicId, $isRead=2)
- {
- $where = ['dynamic_id'=> $dynamicId,'status'=>1];
- if($isRead){
- $where['is_read'] = $isRead;
- }
- return static::where($where)->count('user_id');
- }
- /**
- * 最新收藏点赞用户
- * @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, $limit=6)
- {
- return static::alias('a')
- ->leftJoin('user u','u.user_id=a.user_id')
- ->where(['a.dynamic_id'=> $dynamicId,'a.status'=>1,'u.is_delete'=>0])
- ->field('a.id,a.dynamic_id,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']);
- });
- }
- }
|