UserDynamic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\Paginator;
  16. /**
  17. * 用户动态模型类
  18. * Class UserDynamic
  19. * @package app\common\model
  20. */
  21. class UserDynamic extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'user_dynamic';
  25. // 定义主键
  26. protected $pk = 'id';
  27. /**
  28. * 获取列表
  29. * @param array $param 查询条件
  30. * @param int $listRows 分页数量
  31. * @return mixed
  32. * @throws \think\db\exception\DbException
  33. */
  34. public function getList(array $param = [], int $listRows = 15)
  35. {
  36. // 筛选条件
  37. $query = $this->getQueryFilter($param);
  38. // 排序条件
  39. $sort = $this->setQuerySort($param);
  40. // 执行查询
  41. $list = $query->alias($this->name)
  42. ->order($sort)
  43. ->paginate($listRows);
  44. // 整理列表数据并返回
  45. return $list;
  46. }
  47. /**
  48. * 获取学校下的列表
  49. * @param int $schoolId
  50. * @param int 当前用户ID,用户过滤好友可看动态,需param传lookUids字段过滤
  51. * @param array $param
  52. * @param int $listRows
  53. * @return mixed
  54. */
  55. public function getListBySchool(int $schoolId,int $userId = 0, array $param = [], int $listRows = 15)
  56. {
  57. // 筛选条件
  58. $query = $this->getQueryFilter($param);
  59. // 排序条件
  60. $sort = $this->setQuerySort($param);
  61. // 执行查询
  62. $list = $query->alias($this->name)
  63. ->leftJoin('user_info ui','ui.user_id='.$this->name.'.user_id')
  64. ->where(function($query) use($userId, $param){
  65. if($userId>0){
  66. // 获取粉丝用户ID,并验证
  67. $query->where(function($query) use($userId){
  68. // 粉丝只能看公开或好友可看
  69. $query->whereIn($this->name.'.user_id', \app\api\model\UserFans::getFansUid($userId))
  70. ->whereIn($this->name.'.look_type',[1,2]);
  71. })->whereOr(function($query) use ($userId){
  72. // 用户自己所有
  73. $query->where([$this->name.'.user_id'=> $userId]);
  74. })->whereOr(function($query){
  75. // 公开对所有
  76. $query->where([$this->name.'.look_type'=> 1]);
  77. });
  78. }
  79. })
  80. ->field($this->name.'.*, ui.school_id')
  81. ->order($sort)
  82. ->paginate($listRows);
  83. // echo $query->getLastSql();
  84. // 整理列表数据并返回
  85. return $list;
  86. }
  87. /**
  88. * 设置商品展示的数据
  89. * @param Collection|Paginator $list 商品列表
  90. * @param callable|null $callback 回调函数
  91. * @return mixed
  92. */
  93. protected function setListData($list, callable $callback = null)
  94. {
  95. if ($list->isEmpty()) return $list;
  96. // 遍历商品列表整理数据
  97. foreach ($list as &$item) {
  98. $data = $this->setData($item, $callback);
  99. }
  100. return $list;
  101. }
  102. /**
  103. * 整理数据
  104. * @param Collection|static $info
  105. * @param callable|null $callback
  106. * @return mixed
  107. */
  108. protected function setData($info, callable $callback = null)
  109. {
  110. // 回调函数
  111. is_callable($callback) && call_user_func($callback, $info);
  112. return $info->hidden(array_merge($this->hidden, ['status']));
  113. }
  114. /**
  115. * 检索查询条件
  116. * @param array $params
  117. * @return \think\db\BaseQuery
  118. */
  119. private function getQueryFilter(array $params)
  120. {
  121. $filter = [];
  122. // 实例化新查询对象
  123. $query = $this->getNewQuery();
  124. // 浏览类型
  125. !empty($params['ui.school_id']) && $filter[] = ['ui.school_id', '=', "{$params['ui.school_id']}"];
  126. // 状态
  127. !empty($params[$this->name.'.status']) && $filter[] = [$this->name.'.status', '=', "{$params[$this->name.'.status']}"];
  128. // 浏览类型
  129. !empty($params['look_type']) && $filter[] = ['look_type', '=', "{$params['look_type']}"];
  130. // 类型
  131. !empty($params['type']) && $filter[] = [$this->name.'.type', '=', "{$params['type']}"];
  132. // 实例化新查询对象
  133. return $query->where($filter)->where(function($query) use ($params){
  134. // 关键词
  135. if(!empty($params['keyword'])){
  136. $query->where('content','like', "%{$params['keyword']}%");
  137. }
  138. if(!empty($params['tag'])){
  139. $query->where('content','like', "%#{$params['tag']}%");
  140. }
  141. });
  142. }
  143. /**
  144. * 检索排序条件
  145. * @param array $param
  146. * @return array|string[]
  147. */
  148. private function setQuerySort(array $param = [])
  149. {
  150. $params = $this->setQueryDefaultValue($param, [
  151. 'sortType' => 'all', // 排序类型
  152. $this->name.'.create_time' => false, // 热门排序 (true高到低 false低到高)
  153. ]);
  154. // 排序规则
  155. $sort = [];
  156. if ($params['sortType'] === 'all') {
  157. $sort = [$this->name.'.create_time' => 'desc'];
  158. } elseif ($params['sortType'] === 'view') {
  159. $sort = [$this->name.'.views' => 'desc'];
  160. }
  161. return array_merge($sort, [$this->getPk() => 'desc']);
  162. }
  163. /**
  164. * 封面览图
  165. * @param $value
  166. * @return string|string[]|null
  167. */
  168. public function getImageAttr($value): string
  169. {
  170. return $value? getPreview($value) : '';
  171. }
  172. /**
  173. * 资源文件
  174. * @param $value
  175. * @return string|string[]|null
  176. */
  177. public function getFileUrlAttr($value): string
  178. {
  179. return $value? getPreview($value) : '';
  180. }
  181. }