UserDynamic.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. ->where(function ($query) use ($userId, $param) {
  95. if ($userId > 0) {
  96. // 获取粉丝用户ID,并验证
  97. $query->where(function ($query) use ($userId) {
  98. // 粉丝只能看公开或好友可看
  99. $query->whereIn($this->name . '.user_id', \app\api\model\UserFans::getFansUid($userId))
  100. ->whereIn($this->name . '.look_type', [1, 2]);
  101. })->whereOr(function ($query) use ($userId) {
  102. // 用户自己所有
  103. $query->where([$this->name . '.user_id' => $userId]);
  104. })->whereOr(function ($query) {
  105. // 公开对所有
  106. $query->where([$this->name . '.look_type' => 1]);
  107. });
  108. }
  109. })
  110. ->field($this->name . '.*, ui.school_id')
  111. ->order($sort)
  112. ->paginate($listRows);
  113. // echo $query->getLastSql();
  114. // 整理列表数据并返回
  115. return $list;
  116. }
  117. /**
  118. * 设置商品展示的数据
  119. * @param Collection|Paginator $list 商品列表
  120. * @param callable|null $callback 回调函数
  121. * @return mixed
  122. */
  123. protected function setListData($list, callable $callback = null)
  124. {
  125. if ($list->isEmpty()) return $list;
  126. // 遍历商品列表整理数据
  127. foreach ($list as &$item) {
  128. $data = $this->setData($item, $callback);
  129. }
  130. return $list;
  131. }
  132. /**
  133. * 整理数据
  134. * @param Collection|static $info
  135. * @param callable|null $callback
  136. * @return mixed
  137. */
  138. protected function setData($info, callable $callback = null)
  139. {
  140. // 回调函数
  141. is_callable($callback) && call_user_func($callback, $info);
  142. return $info->hidden(array_merge($this->hidden, ['status']));
  143. }
  144. /**
  145. * 检索查询条件
  146. * @param array $params
  147. * @return \think\db\BaseQuery
  148. */
  149. private function getQueryFilter(array $params)
  150. {
  151. $filter = [];
  152. // 实例化新查询对象
  153. $query = $this->getNewQuery();
  154. // 学校
  155. !empty($params['ui.school_id']) && $filter[] = ['ui.school_id', '=', "{$params['ui.school_id']}"];
  156. // 状态
  157. !empty($params[$this->name . '.status']) && $filter[] = [$this->name . '.status', '=', "{$params[$this->name.'.status']}"];
  158. // 浏览类型
  159. !empty($params['look_type']) && $filter[] = ['look_type', '=', "{$params['look_type']}"];
  160. // 实例化新查询对象
  161. return $query->where($filter)->where(function ($query) use ($params) {
  162. // 关键词
  163. if (!empty($params['keyword'])) {
  164. $query->where('content', 'like', "%{$params['keyword']}%");
  165. }
  166. if (!empty($params['tag'])) {
  167. $query->where('content', 'like', "%#{$params['tag']}%");
  168. }
  169. // 主页分类动态
  170. $type = isset($params['type'])? $params['type'] : 0;
  171. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  172. if($type == 1){
  173. // 我发布的
  174. $query->where($this->name . '.user_id','=', $userId);
  175. $query->whereIn('look_type',[1,2]);
  176. }else if($type == 2){
  177. // 我私密的
  178. $query->where($this->name . '.user_id','=', $userId);
  179. $query->where('look_type','=',3);
  180. }else if($type == 3 && $userId>0){
  181. // 我关注收藏的
  182. $query->whereIn('id', \app\api\model\UserDynamicCollect::getCollectDynamicIds($userId, 1));
  183. }else if($type == 4 && $userId){
  184. // 我点赞喜欢的
  185. $query->whereIn('id', \app\api\model\UserDynamicCollect::getCollectDynamicIds($userId, 2));
  186. }else if($userId>0){
  187. // 发布用户
  188. $query->where($this->name . '.user_id','=', $userId);
  189. }
  190. });
  191. }
  192. /**
  193. * 检索排序条件
  194. * @param array $param
  195. * @return array|string[]
  196. */
  197. private function setQuerySort(array $param = [])
  198. {
  199. $params = $this->setQueryDefaultValue($param, [
  200. 'sortType' => 'all', // 排序类型
  201. $this->name . '.create_time' => false, // 热门排序 (true高到低 false低到高)
  202. ]);
  203. // 排序规则
  204. $sort = [];
  205. if ($params['sortType'] === 'all') {
  206. $sort = [$this->name . '.create_time' => 'desc'];
  207. } elseif ($params['sortType'] === 'view') {
  208. $sort = [$this->name . '.views' => 'desc'];
  209. }
  210. return array_merge($sort, [$this->getPk() => 'desc']);
  211. }
  212. /**
  213. * 封面览图
  214. * @param $value
  215. * @return string|string[]|null
  216. */
  217. public function getImageAttr($value): string
  218. {
  219. return $value ? getPreview($value) : '';
  220. }
  221. /**
  222. * 资源文件
  223. * @param $value
  224. * @return string|string[]|null
  225. */
  226. public function getFileUrlAttr($value): string
  227. {
  228. return $value ? getPreview($value) : '';
  229. }
  230. /**
  231. * 详情
  232. * @param $where
  233. * @param array $with
  234. * @return array|null|static
  235. */
  236. public static function detail($where, array $with = [])
  237. {
  238. $filter = [];
  239. if (is_array($where)) {
  240. $filter = array_merge($filter, $where);
  241. } else {
  242. $filter['id'] = (int)$where;
  243. }
  244. return static::get($filter, $with);
  245. }
  246. /**
  247. * 今日新增数量
  248. * @return int
  249. */
  250. public static function getTodayCounts()
  251. {
  252. return self::where(['status'=> 1])
  253. ->where('create_time','>=', strtotime(date('Y-m-d')))
  254. ->count('id');
  255. }
  256. }