| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?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\UserDynamic as UserDynamicModel;
- use think\facade\Cache;
- /**
- * 用户动态模型类
- * Class UserDynamic
- * @package app\api\model
- */
- class UserDynamic extends UserDynamicModel
- {
- 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);
- }
- /**
- * 获取列表
- * @param array $param
- * @param int $listRows
- * @return mixed
- * @throws \think\db\exception\DbException
- */
- public function getIndexList(int $userId, array $param = [], int $listRows = 15)
- {
- // 排序
- $sort = "views desc,like_num desc,collect_num desc";
- if($this->getNewCount($userId)){
- $sort = "create_time desc,views desc, id desc";
- }
- // 获取商品列表
- $list = parent::alias($this->name)
- ->leftJoin('user u','u.user_id='.$this->name.'.user_id')
- ->leftJoin('user_info ui','ui.user_id='.$this->name.'.user_id')
- ->where([$this->name.'.status'=> 1])
- ->where(function($query) use($userId){
- // 访问权限
- 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]);
- });
- }
- })
- ->where(function($query) use($param){
- if(!empty($param['keyword'])){
- $query->where(function($query) use($param){
- $query->where($this->name.'.content','like',"%{$param['keyword']}%")
- ->whereOr('u.nick_name','like', "%{$param['keyword']}%");
- });
- }
- if(!empty($param['school_id'])){
- $query->where('ui.school_id','=', intval($param['school_id']));
- }
- })
- ->field($this->name.'.*,u.user_type,u.avatar_id,u.nick_name,u.user_type,ui.school_id,ui.admission_year')
- ->order($sort)
- ->paginate($listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 隐藏冗余的字段
- $list->hidden(array_merge($this->hidden, ['status']));
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- /**
- * 验证是否最新发布有动态,以便于优先推送
- * @param $userId
- * @return int|mixed
- */
- private function getNewCount($userId)
- {
- if($userId<=0){
- return false;
- }
- $cacheKey = "caches:dynamic:news:{$userId}";
- if($data = Cache::get($cacheKey)){
- return $data;
- }
- $data = $this->where(['user_id'=> $userId,'status'=> 1])
- ->where('create_time','>=', time() - 60)
- ->order('create_time desc, id desc')
- ->count('id');
- if($data){
- Cache::set($cacheKey, ['user_id'=> $userId,'count'=> $data], rand(30, 60));
- }
- return $data;
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info)
- {
- return $this->setListData($info, function ($data) {
- // 整理数据 api模块
- $this->setDataFromApi($data);
- $this->hidden(['update_time','avatar_id']);
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info)
- {
- return $this->setData($info, function ($data) {
- // logo封面
- $data['image'] = $data['image']? getPreview($data['image']) : '';
- $data['file_url'] = $data['file_url']? getPreview($data['file_url']) : '';
- $data['create_time_text'] = $data['create_time']? getTimeText(strtotime($data['create_time'])) : '';
- if(isset($data['avatar_id'])){
- $uploadData = $data['avatar_id']? UploadFile::detail($data['avatar_id']) : [];
- $data['avatar_url'] = isset($uploadData['preview_url'])? $uploadData['preview_url'] : '';
- }
- // 学校名称
- if(isset($data['user_type'])){
- $userType = intval($data['user_type']);
- $admissionYear = isset($data['admission_year'])? $data['admission_year'] : '';
- $data['school_name'] = '';
- $data['user_type_text'] = $userType==3? '招生老师' : ($admissionYear? $admissionYear.'级' :'学生');
- if($userType == 3){
- $data['school_name'] = $data['school_id']? School::getSchoolField($data['school_id']) : '';
- }else{
- $data['school_name'] = $data['school_id']? SourceShool::getSchoolField($data['school_id']) : '';
- }
- }
- // 点赞喜欢数
- if(!is_null($data['like_num'])){
- $data['like_num'] = $data['like_num']? ($data['like_num']<10000? "{$data['like_num']}" : round($data['like_num']/10000,1).'w') :'';
- }
- // 收藏数
- if(!is_null($data['collect_num'])){
- $data['collect_num'] = $data['collect_num']? ($data['collect_num']<10000? "{$data['collect_num']}" : round($data['collect_num']/10000,1).'w') :'';
- }
- // 浏览数
- if(!is_null($data['views'])){
- $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
- }
- });
- }
- /**
- * 获取详情信息
- * @param $where
- * @param array $with
- * @return static|array|false|null
- */
- 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);
- }
- /**
- * 获取学校动态列表
- * @param int $schoolId
- * @param array $param
- * @param int $listRows
- * @return mixed
- */
- public function getListBySchool(int $schoolId,int $userId = 0, array $param = [], int $listRows = 15)
- {
- // 整理查询参数
- $params = array_merge($param, [$this->name.'.status' => 1, 'ui.school_id'=> $schoolId]);
- // 获取商品列表
- $list = parent::getListBySchool($schoolId, $userId, $params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 隐藏冗余的字段
- $list->hidden(array_merge($this->hidden, ['status']));
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- }
|