UserDynamic.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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['user_id']) ? $param['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['user_id']) && $filter[] = ['user_id', '=', "{$params['user_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. !empty($params['type']) && $filter[] = [$this->name . '.type', '=', "{$params['type']}"];
  163. // 实例化新查询对象
  164. return $query->where($filter)->where(function ($query) use ($params) {
  165. // 关键词
  166. if (!empty($params['keyword'])) {
  167. $query->where('content', 'like', "%{$params['keyword']}%");
  168. }
  169. if (!empty($params['tag'])) {
  170. $query->where('content', 'like', "%#{$params['tag']}%");
  171. }
  172. });
  173. }
  174. /**
  175. * 检索排序条件
  176. * @param array $param
  177. * @return array|string[]
  178. */
  179. private function setQuerySort(array $param = [])
  180. {
  181. $params = $this->setQueryDefaultValue($param, [
  182. 'sortType' => 'all', // 排序类型
  183. $this->name . '.create_time' => false, // 热门排序 (true高到低 false低到高)
  184. ]);
  185. // 排序规则
  186. $sort = [];
  187. if ($params['sortType'] === 'all') {
  188. $sort = [$this->name . '.create_time' => 'desc'];
  189. } elseif ($params['sortType'] === 'view') {
  190. $sort = [$this->name . '.views' => 'desc'];
  191. }
  192. return array_merge($sort, [$this->getPk() => 'desc']);
  193. }
  194. /**
  195. * 封面览图
  196. * @param $value
  197. * @return string|string[]|null
  198. */
  199. public function getImageAttr($value): string
  200. {
  201. return $value ? getPreview($value) : '';
  202. }
  203. /**
  204. * 资源文件
  205. * @param $value
  206. * @return string|string[]|null
  207. */
  208. public function getFileUrlAttr($value): string
  209. {
  210. return $value ? getPreview($value) : '';
  211. }
  212. }