UserDynamic.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\common\model\UserDynamic as UserDynamicModel;
  14. use think\facade\Cache;
  15. /**
  16. * 用户动态模型类
  17. * Class UserDynamic
  18. * @package app\api\model
  19. */
  20. class UserDynamic extends UserDynamicModel
  21. {
  22. protected $globalScope = [''];
  23. /**
  24. * 隐藏字段
  25. * @var array
  26. */
  27. protected $hidden = [
  28. 'update_time'
  29. ];
  30. /**
  31. * 获取列表
  32. * @param array $param
  33. * @param int $listRows
  34. * @return mixed
  35. * @throws \think\db\exception\DbException
  36. */
  37. public function getList(array $param = [], int $listRows = 15)
  38. {
  39. // 整理查询参数
  40. $params = array_merge($param, ['status' => 1]);
  41. // 获取商品列表
  42. $list = parent::getList($params, $listRows);
  43. if ($list->isEmpty()) {
  44. return $list;
  45. }
  46. // 隐藏冗余的字段
  47. $list->hidden(array_merge($this->hidden, ['status']));
  48. // 整理列表数据并返回
  49. return $this->setListDataFromApi($list);
  50. }
  51. /**
  52. * 获取列表
  53. * @param array $param
  54. * @param int $listRows
  55. * @return mixed
  56. * @throws \think\db\exception\DbException
  57. */
  58. public function getIndexList(int $userId, array $param = [], int $listRows = 15)
  59. {
  60. // 排序
  61. $sort = "views desc,like_num desc,collect_num desc";
  62. if($this->getNewCount($userId)){
  63. $sort = "create_time desc,views desc, id desc";
  64. }
  65. // 获取商品列表
  66. $list = parent::alias($this->name)
  67. ->leftJoin('user u','u.user_id='.$this->name.'.user_id')
  68. ->leftJoin('user_info ui','ui.user_id='.$this->name.'.user_id')
  69. ->where([$this->name.'.status'=> 1])
  70. ->where(function($query) use($userId){
  71. // 访问权限
  72. if($userId>0){
  73. // 获取粉丝用户ID,并验证
  74. $query->where(function($query) use($userId){
  75. // 粉丝只能看公开或好友可看
  76. $query->whereIn($this->name.'.user_id', \app\api\model\UserFans::getFansUid($userId))
  77. ->whereIn($this->name.'.look_type',[1,2]);
  78. })->whereOr(function($query) use ($userId){
  79. // 用户自己所有
  80. $query->where([$this->name.'.user_id'=> $userId]);
  81. })->whereOr(function($query){
  82. // 公开对所有
  83. $query->where([$this->name.'.look_type'=> 1]);
  84. });
  85. }
  86. })
  87. ->where(function($query) use($param){
  88. if(!empty($param['keyword'])){
  89. $query->where(function($query) use($param){
  90. $query->where($this->name.'.content','like',"%{$param['keyword']}%")
  91. ->whereOr('u.nick_name','like', "%{$param['keyword']}%");
  92. });
  93. }
  94. if(!empty($param['school_id'])){
  95. $query->where('ui.school_id','=', intval($param['school_id']));
  96. }
  97. })
  98. ->field($this->name.'.*,u.user_type,u.avatar_id,u.nick_name,u.user_type,ui.school_id,ui.admission_year')
  99. ->order($sort)
  100. ->paginate($listRows);
  101. if ($list->isEmpty()) {
  102. return $list;
  103. }
  104. // 隐藏冗余的字段
  105. $list->hidden(array_merge($this->hidden, ['status']));
  106. // 整理列表数据并返回
  107. return $this->setListDataFromApi($list);
  108. }
  109. /**
  110. * 验证是否最新发布有动态,以便于优先推送
  111. * @param $userId
  112. * @return int|mixed
  113. */
  114. private function getNewCount($userId)
  115. {
  116. if($userId<=0){
  117. return false;
  118. }
  119. $cacheKey = "caches:dynamic:news:{$userId}";
  120. if($data = Cache::get($cacheKey)){
  121. return $data;
  122. }
  123. $data = $this->where(['user_id'=> $userId,'status'=> 1])
  124. ->where('create_time','>=', time() - 60)
  125. ->order('create_time desc, id desc')
  126. ->count('id');
  127. if($data){
  128. Cache::set($cacheKey, ['user_id'=> $userId,'count'=> $data], rand(30, 60));
  129. }
  130. return $data;
  131. }
  132. /**
  133. * 设置展示的数据 api模块
  134. * @param $info
  135. * @return mixed
  136. */
  137. private function setListDataFromApi($info)
  138. {
  139. return $this->setListData($info, function ($data) {
  140. // 整理数据 api模块
  141. $this->setDataFromApi($data);
  142. $this->hidden(['update_time','avatar_id']);
  143. });
  144. }
  145. /**
  146. * 整理数据 api模块
  147. * @param $info
  148. * @return mixed
  149. */
  150. private function setDataFromApi($info)
  151. {
  152. return $this->setData($info, function ($data) {
  153. // logo封面
  154. $data['image'] = $data['image']? getPreview($data['image']) : '';
  155. $data['file_url'] = $data['file_url']? getPreview($data['file_url']) : '';
  156. $data['create_time_text'] = $data['create_time']? getTimeText(strtotime($data['create_time'])) : '';
  157. if(isset($data['avatar_id'])){
  158. $uploadData = $data['avatar_id']? UploadFile::detail($data['avatar_id']) : [];
  159. $data['avatar_url'] = isset($uploadData['preview_url'])? $uploadData['preview_url'] : '';
  160. }
  161. // 学校名称
  162. if(isset($data['user_type'])){
  163. $userType = intval($data['user_type']);
  164. $admissionYear = isset($data['admission_year'])? $data['admission_year'] : '';
  165. $data['school_name'] = '';
  166. $data['user_type_text'] = $userType==3? '招生老师' : ($admissionYear? $admissionYear.'级' :'学生');
  167. if($userType == 3){
  168. $data['school_name'] = $data['school_id']? School::getSchoolField($data['school_id']) : '';
  169. }else{
  170. $data['school_name'] = $data['school_id']? SourceShool::getSchoolField($data['school_id']) : '';
  171. }
  172. }
  173. // 点赞喜欢数
  174. if(!is_null($data['like_num'])){
  175. $data['like_num'] = $data['like_num']? ($data['like_num']<10000? "{$data['like_num']}" : round($data['like_num']/10000,1).'w') :'';
  176. }
  177. // 收藏数
  178. if(!is_null($data['collect_num'])){
  179. $data['collect_num'] = $data['collect_num']? ($data['collect_num']<10000? "{$data['collect_num']}" : round($data['collect_num']/10000,1).'w') :'';
  180. }
  181. // 浏览数
  182. if(!is_null($data['views'])){
  183. $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
  184. }
  185. });
  186. }
  187. /**
  188. * 获取详情信息
  189. * @param $where
  190. * @param array $with
  191. * @return static|array|false|null
  192. */
  193. public static function detail($where, array $with = [])
  194. {
  195. $filter = [];
  196. if (is_array($where)) {
  197. $filter = array_merge($filter, $where);
  198. } else {
  199. $filter['id'] = (int)$where;
  200. }
  201. return static::get($filter, $with);
  202. }
  203. /**
  204. * 获取学校动态列表
  205. * @param int $schoolId
  206. * @param array $param
  207. * @param int $listRows
  208. * @return mixed
  209. */
  210. public function getListBySchool(int $schoolId,int $userId = 0, array $param = [], int $listRows = 15)
  211. {
  212. // 整理查询参数
  213. $params = array_merge($param, [$this->name.'.status' => 1, 'ui.school_id'=> $schoolId]);
  214. // 获取商品列表
  215. $list = parent::getListBySchool($schoolId, $userId, $params, $listRows);
  216. if ($list->isEmpty()) {
  217. return $list;
  218. }
  219. // 隐藏冗余的字段
  220. $list->hidden(array_merge($this->hidden, ['status']));
  221. // 整理列表数据并返回
  222. return $this->setListDataFromApi($list);
  223. }
  224. }