DynamicService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $dataList = $this->model::from('dynamic as a')
  102. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  103. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  104. ->where(['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1])
  105. ->where('m.id','>',0)
  106. ->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'])
  107. ->orderBy('a.update_time', 'desc')
  108. ->paginate($pageSize);
  109. $dataList = $dataList ? $dataList->toArray() : [];
  110. if ($dataList) {
  111. foreach ($dataList['data'] as &$item) {
  112. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  113. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  114. $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
  115. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  116. // 关注数量
  117. $item['collect'] = 0;
  118. if($item['id']){
  119. $count = CollectModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  120. $item['collect'] = $count? $count : 0;
  121. }
  122. // 评论数量
  123. $item['comment'] = 0;
  124. if($item['id']){
  125. $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  126. $item['comment'] = $count? $count : 0;
  127. }
  128. }
  129. unset($item);
  130. }
  131. return [
  132. 'code' => 0,
  133. 'success'=> true,
  134. 'msg' => '操作成功',
  135. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  136. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  137. ];
  138. }
  139. /**
  140. * 获取详情
  141. * @param $id
  142. */
  143. public function getDetail($id, $userId=0){
  144. $info = $this->model::from('dynamic as a')
  145. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  146. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  147. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1])
  148. ->where('m.id','>',0)
  149. ->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'])
  150. ->first();
  151. $info = $info? $info->toArray() : [];
  152. if($info){
  153. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  154. $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
  155. $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : '';
  156. $info['is_follow'] = 0;
  157. $info[''] = 0;
  158. if($info['user_id']){
  159. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  160. $info['author'] = $author? $author : '报恩寺';
  161. }
  162. // 是否已收藏
  163. $info['is_collect'] = 0;
  164. if($userId){
  165. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
  166. $info['is_collect'] = 1;
  167. }
  168. }
  169. }
  170. return $info;
  171. }
  172. /**
  173. * 添加或编辑
  174. * @return array
  175. * @since 2020/11/11
  176. * @author wesmiler
  177. */
  178. public function edit()
  179. {
  180. $data = request()->all();
  181. // 图片处理
  182. $type = isset($data['type'])? intval($data['type']) : 0;
  183. $image = $data['thumb']? trim($data['thumb']) : '';
  184. $id = isset($data['id']) ? $data['id'] : 0;
  185. if (!$id && !$image && $type == 1) {
  186. return message('请上传封面图片', false);
  187. }
  188. if (strpos($image, "temp")) {
  189. $data['thumb'] = save_image($image, 'item');
  190. } else {
  191. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  192. }
  193. $data['update_time'] = time();
  194. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  195. return parent::edit($data); // TODO: Change the autogenerated stub
  196. }
  197. /**
  198. * 发布动态
  199. * @param $userId
  200. * @return array
  201. */
  202. public function publish($userId){
  203. $params = request()->all();
  204. $content = isset($params['content'])? htmlspecialchars($params['content']) : '';
  205. $albums = isset($params['fileList'])? $params['fileList'] : [];
  206. $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0;
  207. if($sourceId && !ArticleModel::where(['id'=> $sourceId, 'mark'=> 1,'status'=> 1])->value('id')){
  208. return message('抱歉,当前文章状态不可分享到动态', false);
  209. }
  210. // 验证用户
  211. $memberInfo = MemberModel::where(['id'=> $userId, 'mark'=> 1,'status'=> 1])
  212. ->select(['id','openid','nickname'])
  213. ->first();
  214. if(!$memberInfo){
  215. return message('账户不可操作或已冻结,请联系客服', false);
  216. }
  217. $confirm = ConfigService::make()->getConfigByCode('dynamic_confirm');
  218. $confirm = intval($confirm)? intval($confirm) : 2;
  219. $data = [
  220. 'user_id'=> $userId,
  221. 'source_id'=> $sourceId,
  222. 'comment_close'=> isset($params['comment_close']) && $params['comment_close']? 1 : 2,
  223. 'content'=> $content,
  224. 'albums'=> $albums? json_encode($albums, 256) : '',
  225. 'create_time'=> time(),
  226. 'status'=> $confirm,
  227. ];
  228. if($id = $this->model::insertGetId($data)){
  229. return message('发布成功', true, ['id'=> $id]);
  230. }
  231. return message('发布失败', false);
  232. }
  233. }