DynamicService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ArticleModel;
  13. use App\Models\CollectModel;
  14. use App\Models\DynamicCommentModel;
  15. use App\Models\DynamicModel;
  16. use App\Models\FollowModel;
  17. use App\Models\MemberModel;
  18. /**
  19. * 动态管理-服务类
  20. * @author wesmiler
  21. * @since 2020/11/11
  22. * Class DynamicService
  23. * @package App\Services
  24. */
  25. class DynamicService extends BaseService
  26. {
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author wesmiler
  31. * @since 2020/11/11
  32. * DynamicService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new DynamicModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return DynamicService|null
  41. */
  42. public static function make(){
  43. if(!self::$instance){
  44. self::$instance = new DynamicService();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 获取列表
  50. * @return array
  51. * @since 2020/11/11
  52. * @author wesmiler
  53. */
  54. public function getList()
  55. {
  56. $params = request()->all();
  57. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  58. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  59. $dataList = $this->model::from('dynamic as a')
  60. ->where(function ($query) use ($params) {
  61. $query->where('a.mark', 1);
  62. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  63. if ($isRecommand > 0) {
  64. $query->where('a.is_recommand', $isRecommand);
  65. }
  66. $status = isset($params['status']) ? $params['status'] : 0;
  67. if ($status > 0) {
  68. $query->where('a.status', $status);
  69. } else {
  70. $query->whereIn('a.status', [1, 2]);
  71. }
  72. })
  73. ->select(['a.id', 'a.user_id', 'a.comment_close', 'a.source_id', 'a.is_recommand', 'a.content', 'a.albums', 'a.status', 'a.create_time', 'a.update_time'])
  74. ->orderBy('a.update_time', 'desc')
  75. ->paginate($pageSize);
  76. $dataList = $dataList ? $dataList->toArray() : [];
  77. if ($dataList) {
  78. foreach ($dataList['data'] as &$item) {
  79. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  80. }
  81. unset($item);
  82. }
  83. return [
  84. 'code' => 0,
  85. 'success'=> true,
  86. 'msg' => '操作成功',
  87. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  88. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  89. ];
  90. }
  91. /**
  92. * 获取列表
  93. * @return array
  94. * @since 2020/11/11
  95. * @author wesmiler
  96. */
  97. public function getDataList($params)
  98. {
  99. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  100. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  101. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  102. $where = ['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1];
  103. if($userId){
  104. $where['a.user_id'] = $userId;
  105. }
  106. $dataList = $this->model::from('dynamic as a')
  107. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  108. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  109. ->where($where)
  110. ->where('m.id','>',0)
  111. ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a.albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
  112. ->orderBy('a.update_time', 'desc')
  113. ->paginate($pageSize);
  114. $dataList = $dataList ? $dataList->toArray() : [];
  115. if ($dataList) {
  116. foreach ($dataList['data'] as &$item) {
  117. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  118. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  119. $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
  120. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  121. $item['create_time_text'] = $item['create_time']? format_time(strtotime($item['create_time'])) : '';
  122. $item['content'] = $item['content']? htmlspecialchars_decode($item['content']) : '';
  123. if($item['albums']){
  124. foreach ($item['albums'] as &$v){
  125. $v = $v? get_image_url($v) : '';
  126. }
  127. unset($v);
  128. }
  129. // 关注收藏数量
  130. $item['collect'] = 0;
  131. if($item['id']){
  132. $count = CollectModel::where(['source_id'=> $item['id'],'type'=>2,'mark'=> 1,'status'=> 1])->count('id');
  133. $item['collect'] = $count? $count : 0;
  134. }
  135. // 评论数量
  136. $item['comment'] = 0;
  137. if($item['id']){
  138. $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  139. $item['comment'] = $count? $count : 0;
  140. }
  141. // 是否已收藏
  142. $item['is_collect'] = 0;
  143. if($userId){
  144. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $item['id'],'type'=> 2,'mark'=> 1,'status'=> 1])->value('id')){
  145. $item['is_collect'] = 1;
  146. }
  147. }
  148. }
  149. unset($item);
  150. }
  151. return [
  152. 'code' => 0,
  153. 'success'=> true,
  154. 'msg' => '操作成功',
  155. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  156. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  157. ];
  158. }
  159. /**
  160. * 获取详情
  161. * @param $id
  162. */
  163. public function getDetail($id, $userId=0){
  164. $info = $this->model::from('dynamic as a')
  165. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  166. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  167. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1])
  168. ->where('m.id','>',0)
  169. ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a.albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
  170. ->first();
  171. $info = $info? $info->toArray() : [];
  172. if($info){
  173. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  174. $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
  175. $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : '';
  176. $info['create_time_text'] = $info['create_time']? format_time(strtotime($info['create_time'])) : '';
  177. $info['content'] = $info['content']? htmlspecialchars_decode($info['content']) : '';
  178. $info['albums'] = $info['albums']? json_decode($info['albums']) : [];
  179. if($info['albums']){
  180. foreach ($info['albums'] as &$v){
  181. $v = $v? get_image_url($v) : '';
  182. }
  183. unset($v);
  184. }
  185. $info['is_follow'] = 0;
  186. $info[''] = 0;
  187. if($info['user_id']){
  188. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  189. $info['author'] = $author? $author : '报恩寺';
  190. }
  191. // 是否已收藏
  192. $info['is_collect'] = 0;
  193. if($userId){
  194. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 2,'mark'=> 1,'status'=> 1])->value('id')){
  195. $info['is_collect'] = 1;
  196. }
  197. }
  198. }
  199. return $info;
  200. }
  201. /**
  202. * 添加或编辑
  203. * @return array
  204. * @since 2020/11/11
  205. * @author wesmiler
  206. */
  207. public function edit()
  208. {
  209. $data = request()->all();
  210. // 图片处理
  211. $type = isset($data['type'])? intval($data['type']) : 0;
  212. $image = $data['thumb']? trim($data['thumb']) : '';
  213. $id = isset($data['id']) ? $data['id'] : 0;
  214. if (!$id && !$image && $type == 1) {
  215. return message('请上传封面图片', false);
  216. }
  217. if (strpos($image, "temp")) {
  218. $data['thumb'] = save_image($image, 'item');
  219. } else {
  220. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  221. }
  222. $data['update_time'] = time();
  223. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  224. return parent::edit($data); // TODO: Change the autogenerated stub
  225. }
  226. /**
  227. * 发布动态
  228. * @param $userId
  229. * @return array
  230. */
  231. public function publish($userId){
  232. $params = request()->all();
  233. $content = isset($params['content'])? htmlspecialchars($params['content']) : '';
  234. $albums = isset($params['fileList'])? $params['fileList'] : [];
  235. $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0;
  236. if($sourceId && !ArticleModel::where(['id'=> $sourceId, 'mark'=> 1,'status'=> 1])->value('id')){
  237. return message('抱歉,当前文章状态不可分享到动态', false);
  238. }
  239. // 验证用户
  240. $memberInfo = MemberModel::where(['id'=> $userId, 'mark'=> 1,'status'=> 1])
  241. ->select(['id','openid','nickname'])
  242. ->first();
  243. if(!$memberInfo){
  244. return message('账户不可操作或已冻结,请联系客服', false);
  245. }
  246. $confirm = ConfigService::make()->getConfigByCode('dynamic_confirm');
  247. $confirm = intval($confirm)? intval($confirm) : 2;
  248. $data = [
  249. 'user_id'=> $userId,
  250. 'source_id'=> $sourceId,
  251. 'comment_close'=> isset($params['comment_close']) && $params['comment_close']? 1 : 2,
  252. 'content'=> $content,
  253. 'albums'=> $albums? json_encode($albums, 256) : '',
  254. 'create_time'=> time(),
  255. 'update_time'=> time(),
  256. 'status'=> $confirm,
  257. ];
  258. if($id = $this->model::insertGetId($data)){
  259. return message('发布成功', true, ['id'=> $id]);
  260. }
  261. return message('发布失败', false);
  262. }
  263. }