DynamicService.php 10 KB

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