UserDynamicComment.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\UserDynamicComment as UserDynamicCommentModel;
  14. /**
  15. * 用户动态评论模型类
  16. * Class UserDynamicComment
  17. * @package app\api\model
  18. */
  19. class UserDynamicComment extends UserDynamicCommentModel
  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'], 2);
  78. // 未读数量
  79. $data['unread_num'] = self::getCounts($data['dynamic_id'], 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. ->leftJoin('user cu','cu.user_id='.$this->name.'.user_id')
  98. ->where([$this->name.'.status'=>1,'d.user_id'=> $userId,'d.status'=>1])
  99. ->where(function($query) use($param){
  100. // 关键词
  101. if(!empty($param['keyword'])){
  102. $query->where('content','like', "%{$param['keyword']}%");
  103. }
  104. })
  105. ->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')
  106. ->group($this->name.'.dynamic_id')
  107. ->order($this->name.'.create_time desc, d.id desc')
  108. ->paginate($listRows);
  109. if ($list->isEmpty()) {
  110. return $list;
  111. }
  112. // 隐藏冗余的字段
  113. $list->hidden(array_merge($this->hidden, ['status']));
  114. // 整理列表数据并返回
  115. return $this->setListDataFromApi($list, $params);
  116. }
  117. /**
  118. * 获取点赞收藏数
  119. * @param $dynamicId
  120. * @param int $limit
  121. * @return int
  122. */
  123. public static function getCounts($dynamicId, $isRead=2)
  124. {
  125. $where = ['dynamic_id'=> $dynamicId,'status'=>1];
  126. if($isRead){
  127. $where['is_read'] = $isRead;
  128. }
  129. return static::where($where)->count('user_id');
  130. }
  131. /**
  132. * 最新收藏点赞用户
  133. * @param $dynamicId
  134. * @param int $limit
  135. * @return UserDynamicCollect[]|array|\think\Collection
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public static function getUsers($dynamicId, $limit=6)
  141. {
  142. return static::alias('a')
  143. ->leftJoin('user u','u.user_id=a.user_id')
  144. ->where(['a.dynamic_id'=> $dynamicId,'a.status'=>1,'u.is_delete'=>0])
  145. ->field('a.id,a.dynamic_id,u.user_id,u.nick_name,u.avatar_id')
  146. ->order('a.is_read desc, a.create_time desc')
  147. ->limit($limit)
  148. ->select()
  149. ->each(function($item, $k){
  150. $avatarData = $item['avatar_id']? UploadFile::detail($item['avatar_id']) : [];
  151. $item['avatar_url'] = isset($avatarData['preview_url'])? $avatarData['preview_url'] : '';
  152. unset($item['avatar_id']);
  153. });
  154. }
  155. }