UserDynamicCollect.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\UserDynamicCollect as UserDynamicCollectModel;
  14. /**
  15. * 用户动态点赞收藏模型类
  16. * Class UserDynamicCollect
  17. * @package app\api\model
  18. */
  19. class UserDynamicCollect extends UserDynamicCollectModel
  20. {
  21. protected $globalScope = [''];
  22. /**
  23. * 隐藏字段
  24. * @var array
  25. */
  26. protected $hidden = [
  27. 'update_time'
  28. ];
  29. /**
  30. * 获取列表
  31. * @param array $param
  32. * @param int $listRows
  33. * @return mixed
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function getList(array $param = [], int $listRows = 15)
  37. {
  38. // 整理查询参数
  39. $params = array_merge($param, ['status' => 1]);
  40. // 获取商品列表
  41. $list = parent::getList($params, $listRows);
  42. if ($list->isEmpty()) {
  43. return $list;
  44. }
  45. // 隐藏冗余的字段
  46. $list->hidden(array_merge($this->hidden, ['status']));
  47. // 整理列表数据并返回
  48. return $this->setListDataFromApi($list);
  49. }
  50. /**
  51. * 设置展示的数据 api模块
  52. * @param $info
  53. * @return mixed
  54. */
  55. private function setListDataFromApi($info)
  56. {
  57. return $this->setListData($info, function ($data) {
  58. // 整理数据 api模块
  59. $this->setDataFromApi($data);
  60. // 隐藏冗余的字段
  61. $this->hidden(array_merge($this->hidden, ['avatar_id']));
  62. });
  63. }
  64. /**
  65. * 整理数据 api模块
  66. * @param $info
  67. * @return mixed
  68. */
  69. private function setDataFromApi($info)
  70. {
  71. return $this->setData($info, function ($data) {
  72. $data['image'] = $data['image']? getPreview($data['image']) : '';
  73. $data['create_time_text'] = $data['create_time']? getTimeText(strtotime($data['create_time'])) : '';
  74. $avatarData = $data['avatar_id']? UploadFile::detail($data['avatar_id']) : [];
  75. $data['avatar_url'] = isset($avatarData['preview_url'])? $avatarData['preview_url'] : '';
  76. // 最新点赞用户
  77. $data['users'] = self::getUsers($data['dynamic_id'], $data['type']);
  78. // 未读数量
  79. $data['unread_num'] = self::getCounts($data['dynamic_id'], $data['type'], 2);
  80. });
  81. }
  82. /**
  83. * 获取学校动态点赞用户列表
  84. * @param int $schoolId
  85. * @param array $param
  86. * @param int $listRows
  87. * @return mixed
  88. */
  89. public function getListByUser(int $userId = 0, array $param = [], int $listRows = 15)
  90. {
  91. // 整理查询参数
  92. $params = array_merge($param, [$this->name.'.status' => 1]);
  93. // 获取列表
  94. $list = parent::alias($this->name)
  95. ->leftJoin('user_dynamic d','d.id='.$this->name.'.dynamic_id')
  96. ->leftJoin('user u','u.user_id='.'d.user_id')
  97. ->where([$this->name.'.status'=>1,'d.user_id'=> $userId,'d.status'=>1])
  98. ->where(function($query) use($param){
  99. // 关键词
  100. if(!empty($param['keyword'])){
  101. $query->where('content','like', "%{$param['keyword']}%");
  102. }
  103. if(!empty($param['type'])){
  104. $query->where($this->name.'.type','=', intval($param['type']));
  105. }
  106. })
  107. ->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')
  108. ->group($this->name.'.dynamic_id')
  109. ->order($this->name.'.create_time desc, d.id desc')
  110. ->paginate($listRows);
  111. if ($list->isEmpty()) {
  112. return $list;
  113. }
  114. // 隐藏冗余的字段
  115. $list->hidden(array_merge($this->hidden, ['status']));
  116. // 整理列表数据并返回
  117. return $this->setListDataFromApi($list, $params);
  118. }
  119. /**
  120. * 获取点赞收藏数
  121. * @param $dynamicId
  122. * @param int $type
  123. * @param int $limit
  124. * @return int
  125. */
  126. public static function getCounts($dynamicId, $type=1, $isRead=2)
  127. {
  128. $where = ['dynamic_id'=> $dynamicId,'type'=> $type,'status'=>1];
  129. if($isRead){
  130. $where['is_read'] = $isRead;
  131. }
  132. return static::where($where)->count('user_id');
  133. }
  134. /**
  135. * 获取我收藏或点赞的动态数量,过滤收藏的已不可访问作品
  136. * @param $userId 用户ID
  137. * @param int $type 类型:1-收藏关注,2-点赞喜欢
  138. * @return mixed
  139. */
  140. public static function getCountsByType($userId, $type=1)
  141. {
  142. $count = self::alias('a')
  143. ->leftJoin('user_dynamic d','d.id=a.dynamic_id')
  144. ->where(['a.type'=> $type,'a.user_id'=> $userId,'a.status'=>1,'d.status'=> 1])
  145. ->where(function($query) use($userId){
  146. // 获取粉丝用户ID,并验证
  147. $query->where(function($query) use($userId){
  148. // 粉丝只能看公开或好友可看
  149. $query->whereIn('d.user_id', \app\api\model\UserFans::getFansUid($userId))
  150. ->whereIn('d.look_type',[1,2]);
  151. })->whereOr(function($query) use ($userId){
  152. // 用户自己所有
  153. $query->where(['d.user_id'=> $userId]);
  154. })->whereOr(function($query){
  155. // 公开对所有
  156. $query->where(['d.look_type'=> 1]);
  157. });
  158. })->count('a.dynamic_id');
  159. return $count;
  160. }
  161. /**
  162. * 最新收藏点赞用户
  163. * @param $dynamicId
  164. * @param int $limit
  165. * @return UserDynamicCollect[]|array|\think\Collection
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public static function getUsers($dynamicId, $type=1, $limit=6)
  171. {
  172. return static::alias('a')
  173. ->leftJoin('user u','u.user_id=a.user_id')
  174. ->where(['a.dynamic_id'=> $dynamicId,'a.type'=> $type ,'a.status'=>1,'u.is_delete'=>0])
  175. ->field('a.id,a.dynamic_id,a.type,u.user_id,u.nick_name,u.avatar_id')
  176. ->order('a.is_read desc, a.create_time desc')
  177. ->limit($limit)
  178. ->select()
  179. ->each(function($item, $k){
  180. $avatarData = $item['avatar_id']? UploadFile::detail($item['avatar_id']) : [];
  181. $item['avatar_url'] = isset($avatarData['preview_url'])? $avatarData['preview_url'] : '';
  182. unset($item['avatar_id']);
  183. });
  184. }
  185. /**
  186. * 我关注的动态
  187. * @param $userId
  188. * @param $type 类型:1-关注收藏,2-点赞喜欢
  189. * @return array
  190. */
  191. public static function getCollectDynamicIds($userId, $type=1)
  192. {
  193. $where = ['user_id'=> $userId,'status'=> 1];
  194. if($type>0){
  195. $where['type'] = $type;
  196. }
  197. return static::where($where)->column('dynamic_id');
  198. }
  199. }