UserDynamic.php 9.7 KB

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