UserDynamic.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\model\relation\HasOne;
  16. use think\Paginator;
  17. /**
  18. * 用户动态模型类
  19. * Class UserDynamic
  20. * @package app\common\model
  21. */
  22. class UserDynamic extends BaseModel
  23. {
  24. // 定义表名
  25. protected $name = 'user_dynamic';
  26. // 定义主键
  27. protected $pk = 'id';
  28. /**
  29. * 关联用户
  30. * @return HasOne
  31. */
  32. public function user(): HasOne
  33. {
  34. return $this->HasOne('User', 'user_id', 'user_id')
  35. ->field('user_id,nick_name, avatar_id,user_type')
  36. ->bind(['nick_name']);
  37. }
  38. /**
  39. * 获取列表
  40. * @param array $param 查询条件
  41. * @param int $listRows 分页数量
  42. * @return mixed
  43. * @throws \think\db\exception\DbException
  44. */
  45. public function getList(array $param = [], int $listRows = 15)
  46. {
  47. // 筛选条件
  48. $query = $this->getQueryFilter($param);
  49. // 排序条件
  50. $sort = $this->setQuerySort($param);
  51. // 执行查询
  52. $list = $query->alias($this->name)
  53. ->where(function ($query) use ($param) {
  54. // 访问权限
  55. $userId = isset($param['access_user_id']) ? $param['access_user_id'] : 0;
  56. if ($userId > 0) {
  57. // 获取粉丝用户ID,并验证
  58. $query->where(function ($query) use ($userId) {
  59. // 粉丝只能看公开或好友可看
  60. $query->whereIn($this->name . '.user_id', \app\api\model\UserFans::getFansUid($userId))
  61. ->whereIn($this->name . '.look_type', [1, 2]);
  62. })->whereOr(function ($query) use ($userId) {
  63. // 用户自己所有
  64. $query->where([$this->name . '.user_id' => $userId]);
  65. })->whereOr(function ($query) {
  66. // 公开对所有
  67. $query->where([$this->name . '.look_type' => 1]);
  68. });
  69. }
  70. })
  71. ->order($sort)
  72. ->paginate($listRows);
  73. // 整理列表数据并返回
  74. return $list;
  75. }
  76. /**
  77. * 获取学校下的列表
  78. * @param int $schoolId
  79. * @param int 当前用户ID,用户过滤好友可看动态,需param传lookUids字段过滤
  80. * @param array $param
  81. * @param int $listRows
  82. * @return mixed
  83. */
  84. public function getListBySchool(int $schoolId, int $userId = 0, array $param = [], int $listRows = 15)
  85. {
  86. // 筛选条件
  87. $query = $this->getQueryFilter($param);
  88. // 排序条件
  89. $sort = $this->setQuerySort($param);
  90. // 执行查询
  91. $list = $query->alias($this->name)
  92. ->leftJoin('user_info ui', 'ui.user_id=' . $this->name . '.user_id')
  93. ->where(function ($query) use ($userId, $param) {
  94. if ($userId > 0) {
  95. // 获取粉丝用户ID,并验证
  96. $query->where(function ($query) use ($userId) {
  97. // 粉丝只能看公开或好友可看
  98. $query->whereIn($this->name . '.user_id', \app\api\model\UserFans::getFansUid($userId))
  99. ->whereIn($this->name . '.look_type', [1, 2]);
  100. })->whereOr(function ($query) use ($userId) {
  101. // 用户自己所有
  102. $query->where([$this->name . '.user_id' => $userId]);
  103. })->whereOr(function ($query) {
  104. // 公开对所有
  105. $query->where([$this->name . '.look_type' => 1]);
  106. });
  107. }
  108. })
  109. ->field($this->name . '.*, ui.school_id')
  110. ->order($sort)
  111. ->paginate($listRows);
  112. // echo $query->getLastSql();
  113. // 整理列表数据并返回
  114. return $list;
  115. }
  116. /**
  117. * 设置商品展示的数据
  118. * @param Collection|Paginator $list 商品列表
  119. * @param callable|null $callback 回调函数
  120. * @return mixed
  121. */
  122. protected function setListData($list, callable $callback = null)
  123. {
  124. if ($list->isEmpty()) return $list;
  125. // 遍历商品列表整理数据
  126. foreach ($list as &$item) {
  127. $data = $this->setData($item, $callback);
  128. }
  129. return $list;
  130. }
  131. /**
  132. * 整理数据
  133. * @param Collection|static $info
  134. * @param callable|null $callback
  135. * @return mixed
  136. */
  137. protected function setData($info, callable $callback = null)
  138. {
  139. // 回调函数
  140. is_callable($callback) && call_user_func($callback, $info);
  141. return $info->hidden(array_merge($this->hidden, ['status']));
  142. }
  143. /**
  144. * 检索查询条件
  145. * @param array $params
  146. * @return \think\db\BaseQuery
  147. */
  148. private function getQueryFilter(array $params)
  149. {
  150. $filter = [];
  151. // 实例化新查询对象
  152. $query = $this->getNewQuery();
  153. // 学校
  154. !empty($params['ui.school_id']) && $filter[] = ['ui.school_id', '=', "{$params['ui.school_id']}"];
  155. // 状态
  156. !empty($params[$this->name . '.status']) && $filter[] = [$this->name . '.status', '=', "{$params[$this->name.'.status']}"];
  157. // 浏览类型
  158. !empty($params['look_type']) && $filter[] = ['look_type', '=', "{$params['look_type']}"];
  159. // 实例化新查询对象
  160. return $query->where($filter)->where(function ($query) use ($params) {
  161. // 关键词
  162. if (!empty($params['keyword'])) {
  163. $query->where('content', 'like', "%{$params['keyword']}%");
  164. }
  165. if (!empty($params['tag'])) {
  166. $query->where('content', 'like', "%#{$params['tag']}%");
  167. }
  168. // 主页分类动态
  169. $type = isset($params['type'])? $params['type'] : 0;
  170. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  171. if($type == 1){
  172. // 我发布的
  173. $query->where($this->name . '.user_id','=', $userId);
  174. $query->whereIn('look_type',[1,2]);
  175. }else if($type == 2){
  176. // 我私密的
  177. $query->where($this->name . '.user_id','=', $userId);
  178. $query->where('look_type','=',3);
  179. }else if($type == 3 && $userId>0){
  180. // 我关注收藏的
  181. $query->whereIn('id', \app\api\model\UserDynamicCollect::getCollectDynamicIds($userId, 1));
  182. }else if($type == 4 && $userId){
  183. // 我点赞喜欢的
  184. $query->whereIn('id', \app\api\model\UserDynamicCollect::getCollectDynamicIds($userId, 2));
  185. }else if($userId>0){
  186. // 发布用户
  187. $query->where($this->name . '.user_id','=', $userId);
  188. }
  189. });
  190. }
  191. /**
  192. * 检索排序条件
  193. * @param array $param
  194. * @return array|string[]
  195. */
  196. private function setQuerySort(array $param = [])
  197. {
  198. $params = $this->setQueryDefaultValue($param, [
  199. 'sortType' => 'all', // 排序类型
  200. $this->name . '.create_time' => false, // 热门排序 (true高到低 false低到高)
  201. ]);
  202. // 排序规则
  203. $sort = [];
  204. if ($params['sortType'] === 'all') {
  205. $sort = [$this->name . '.create_time' => 'desc'];
  206. } elseif ($params['sortType'] === 'view') {
  207. $sort = [$this->name . '.views' => 'desc'];
  208. }
  209. return array_merge($sort, [$this->getPk() => 'desc']);
  210. }
  211. /**
  212. * 封面览图
  213. * @param $value
  214. * @return string|string[]|null
  215. */
  216. public function getImageAttr($value): string
  217. {
  218. return $value ? getPreview($value) : '';
  219. }
  220. /**
  221. * 资源文件
  222. * @param $value
  223. * @return string|string[]|null
  224. */
  225. public function getFileUrlAttr($value): string
  226. {
  227. return $value ? getPreview($value) : '';
  228. }
  229. /**
  230. * 详情
  231. * @param $where
  232. * @param array $with
  233. * @return array|null|static
  234. */
  235. public static function detail($where, array $with = [])
  236. {
  237. $filter = [];
  238. if (is_array($where)) {
  239. $filter = array_merge($filter, $where);
  240. } else {
  241. $filter['id'] = (int)$where;
  242. }
  243. return static::get($filter, $with);
  244. }
  245. /**
  246. * 今日新增数量
  247. * @return int
  248. */
  249. public static function getTodayCounts()
  250. {
  251. return self::where(['status'=> 1])
  252. ->where('create_time','>=', strtotime(date('Y-m-d')))
  253. ->count('id');
  254. }
  255. }