DynamicService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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\Http\Controllers\Api\BaseController;
  13. use App\Models\ArticleModel;
  14. use App\Models\CollectModel;
  15. use App\Models\DynamicCommentModel;
  16. use App\Models\DynamicModel;
  17. use App\Models\DynamicNoticeModel;
  18. use App\Models\MemberModel;
  19. /**
  20. * 动态管理-服务类
  21. * @author wesmiler
  22. * @since 2020/11/11
  23. * Class DynamicService
  24. * @package App\Services
  25. */
  26. class DynamicService extends BaseController
  27. {
  28. protected static $instance = null;
  29. /**
  30. * 构造函数
  31. * @author wesmiler
  32. * @since 2020/11/11
  33. * DynamicService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new DynamicModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return DynamicService|null
  42. */
  43. public static function make(){
  44. if(!self::$instance){
  45. self::$instance = new DynamicService();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 获取列表
  51. * @return array
  52. * @since 2020/11/11
  53. * @author wesmiler
  54. */
  55. public function getList()
  56. {
  57. $params = request()->all();
  58. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  59. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  60. $dataList = $this->model::from('dynamic as a')
  61. ->where(function ($query) use ($params) {
  62. $query->where('a.mark', 1);
  63. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  64. if ($isRecommand > 0) {
  65. $query->where('a.is_recommand', $isRecommand);
  66. }
  67. $status = isset($params['status']) ? $params['status'] : 0;
  68. if ($status > 0) {
  69. $query->where('a.status', $status);
  70. } else {
  71. $query->whereIn('a.status', [1, 2]);
  72. }
  73. })
  74. ->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'])
  75. ->orderBy('a.update_time', 'desc')
  76. ->paginate($pageSize);
  77. $dataList = $dataList ? $dataList->toArray() : [];
  78. if ($dataList) {
  79. foreach ($dataList['data'] as &$item) {
  80. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  81. }
  82. unset($item);
  83. }
  84. return [
  85. 'code' => 0,
  86. 'success'=> true,
  87. 'msg' => '操作成功',
  88. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  89. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  90. ];
  91. }
  92. /**
  93. * 获取列表
  94. * @return array
  95. * @since 2020/11/11
  96. * @author wesmiler
  97. */
  98. public function getDataList($params)
  99. {
  100. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  101. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  102. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  103. $where = ['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1];
  104. if($userId){
  105. $where['a.user_id'] = $userId;
  106. }
  107. $dataList = $this->model::from('dynamic as a')
  108. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  109. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  110. ->where($where)
  111. ->where('m.id','>',0)
  112. ->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'])
  113. ->orderBy('a.update_time', 'desc')
  114. ->paginate($pageSize);
  115. $dataList = $dataList ? $dataList->toArray() : [];
  116. if ($dataList) {
  117. foreach ($dataList['data'] as &$item) {
  118. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  119. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  120. $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
  121. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  122. $item['create_time_text'] = $item['create_time']? format_time(strtotime($item['create_time'])) : '';
  123. $item['content'] = $item['content']? htmlspecialchars_decode($item['content']) : '';
  124. if($item['albums']){
  125. foreach ($item['albums'] as &$v){
  126. $v = $v? get_image_url($v) : '';
  127. }
  128. unset($v);
  129. }
  130. // 关注收藏数量
  131. $item['collect'] = 0;
  132. if($item['id']){
  133. $count = CollectModel::where(['source_id'=> $item['id'],'type'=>2,'mark'=> 1,'status'=> 1])->count('id');
  134. $item['collect'] = $count? $count : 0;
  135. }
  136. // 评论数量
  137. $item['comment'] = 0;
  138. if($item['id']){
  139. $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  140. $item['comment'] = $count? $count : 0;
  141. }
  142. // 是否已收藏
  143. $item['is_collect'] = 0;
  144. if($userId){
  145. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $item['id'],'type'=> 2,'mark'=> 1,'status'=> 1])->value('id')){
  146. $item['is_collect'] = 1;
  147. }
  148. }
  149. }
  150. unset($item);
  151. }
  152. return [
  153. 'code' => 0,
  154. 'success'=> true,
  155. 'msg' => '操作成功',
  156. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  157. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  158. ];
  159. }
  160. /**
  161. * 获取列表
  162. * @return array
  163. * @since 2020/11/11
  164. * @author wesmiler
  165. */
  166. public function getCollectList($params)
  167. {
  168. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  169. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  170. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  171. $where = ['a.mark'=>1,'a.status'=> 1,'c.user_id'=> $userId,'c.mark'=> 1,'c.status'=> 1,'c.type'=> 2];
  172. $dataList = $this->model::from('collect as c')
  173. ->leftJoin('dynamic as a', 'a.id','=','c.source_id')
  174. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  175. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  176. ->where($where)
  177. ->where('a.id','>',0)
  178. ->where('m.id','>',0)
  179. ->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'])
  180. ->orderBy('a.update_time', 'desc')
  181. ->paginate($pageSize);
  182. $dataList = $dataList ? $dataList->toArray() : [];
  183. if ($dataList) {
  184. foreach ($dataList['data'] as &$item) {
  185. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  186. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  187. $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
  188. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  189. $item['create_time_text'] = $item['create_time']? format_time(strtotime($item['create_time'])) : '';
  190. $item['content'] = $item['content']? htmlspecialchars_decode($item['content']) : '';
  191. if($item['albums']){
  192. foreach ($item['albums'] as &$v){
  193. $v = $v? get_image_url($v) : '';
  194. }
  195. unset($v);
  196. }
  197. // 关注收藏数量
  198. $item['collect'] = 0;
  199. if($item['id']){
  200. $count = CollectModel::where(['source_id'=> $item['id'],'type'=>2,'mark'=> 1,'status'=> 1])->count('id');
  201. $item['collect'] = $count? $count : 0;
  202. }
  203. // 评论数量
  204. $item['comment'] = 0;
  205. if($item['id']){
  206. $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
  207. $item['comment'] = $count? $count : 0;
  208. }
  209. // 是否已收藏
  210. $item['is_collect'] = 0;
  211. if($userId){
  212. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $item['id'],'type'=> 2,'mark'=> 1,'status'=> 1])->value('id')){
  213. $item['is_collect'] = 1;
  214. }
  215. }
  216. }
  217. unset($item);
  218. }
  219. return [
  220. 'code' => 0,
  221. 'success'=> true,
  222. 'msg' => '操作成功',
  223. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  224. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  225. ];
  226. }
  227. /**
  228. * 获取详情
  229. * @param $id
  230. */
  231. public function getDetail($id, $userId=0){
  232. $info = $this->model::from('dynamic as a')
  233. ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
  234. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  235. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1])
  236. ->where('m.id','>',0)
  237. ->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'])
  238. ->first();
  239. $info = $info? $info->toArray() : [];
  240. if($info){
  241. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  242. $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
  243. $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : '';
  244. $info['create_time_text'] = $info['create_time']? format_time(strtotime($info['create_time'])) : '';
  245. $info['content'] = $info['content']? htmlspecialchars_decode($info['content']) : '';
  246. $info['albums'] = $info['albums']? json_decode($info['albums']) : [];
  247. if($info['albums']){
  248. foreach ($info['albums'] as &$v){
  249. $v = $v? get_image_url($v) : '';
  250. }
  251. unset($v);
  252. }
  253. $info['is_follow'] = 0;
  254. $info[''] = 0;
  255. if($info['user_id']){
  256. $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
  257. $info['author'] = $author? $author : '报恩寺';
  258. }
  259. // 是否已收藏
  260. $info['is_collect'] = 0;
  261. if($userId){
  262. if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 2,'mark'=> 1,'status'=> 1])->value('id')){
  263. $info['is_collect'] = 1;
  264. }
  265. }
  266. }
  267. return $info;
  268. }
  269. /**
  270. * 添加或编辑
  271. * @return array
  272. * @since 2020/11/11
  273. * @author wesmiler
  274. */
  275. public function edit()
  276. {
  277. $data = request()->all();
  278. // 图片处理
  279. $type = isset($data['type'])? intval($data['type']) : 0;
  280. $image = $data['thumb']? trim($data['thumb']) : '';
  281. $id = isset($data['id']) ? $data['id'] : 0;
  282. if (!$id && !$image && $type == 1) {
  283. return message('请上传封面图片', false);
  284. }
  285. if (strpos($image, "temp")) {
  286. $data['thumb'] = save_image($image, 'item');
  287. } else {
  288. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  289. }
  290. $data['update_time'] = time();
  291. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  292. return parent::edit($data); // TODO: Change the autogenerated stub
  293. }
  294. /**
  295. * 发布动态
  296. * @param $userId
  297. * @return array
  298. */
  299. public function publish($userId){
  300. $params = request()->all();
  301. $content = isset($params['content'])? htmlspecialchars($params['content']) : '';
  302. $albums = isset($params['fileList'])? $params['fileList'] : [];
  303. $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0;
  304. if($sourceId && !ArticleModel::where(['id'=> $sourceId, 'mark'=> 1,'status'=> 1])->value('id')){
  305. return message('抱歉,当前文章状态不可分享到动态', false);
  306. }
  307. // 验证用户
  308. $memberInfo = MemberModel::where(['id'=> $userId, 'mark'=> 1,'status'=> 1])
  309. ->select(['id','openid','nickname'])
  310. ->first();
  311. if(!$memberInfo){
  312. return message('账户不可操作或已冻结,请联系客服', false);
  313. }
  314. $confirm = ConfigService::make()->getConfigByCode('dynamic_confirm');
  315. $confirm = intval($confirm)? intval($confirm) : 2;
  316. $data = [
  317. 'user_id'=> $userId,
  318. 'source_id'=> $sourceId,
  319. 'comment_close'=> isset($params['comment_close']) && $params['comment_close']? 1 : 2,
  320. 'content'=> $content,
  321. 'albums'=> $albums? json_encode($albums, 256) : '',
  322. 'create_time'=> time(),
  323. 'update_time'=> time(),
  324. 'status'=> $confirm,
  325. ];
  326. if($id = $this->model::insertGetId($data)){
  327. return message('发布成功', true, ['id'=> $id]);
  328. }
  329. return message('发布失败', false);
  330. }
  331. /**
  332. * 动态通知
  333. * @return array
  334. */
  335. public function notice(){
  336. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  337. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  338. $dataList = DynamicNoticeModel::from('dynamic_notice as dn')
  339. ->leftJoin('member as m','m.id','=','dn.user_id')
  340. ->where(['dn.mark'=> 1,'dn.status'=> 1,'m.mark'=> 1,'m.status'=> 1])
  341. ->where('m.id','>', 0)
  342. ->select('dn.id','dn.user_id','dn.remark','dn.create_time','m.avatar','m.nickname')
  343. ->orderBy('dn.create_time', 'desc')
  344. ->paginate($pageSize);
  345. $dataList = $dataList ? $dataList->toArray() : [];
  346. if ($dataList) {
  347. foreach ($dataList['data'] as &$item) {
  348. $item['nickname'] = $item['nickname']? formatName($item['nickname']) : '';
  349. $item['avatar'] = $item['avatar']? get_image_url($item['avatar']):'';
  350. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  351. $item['time_text'] = $item['create_time']? format_time(strtotime($item['create_time'])) : '';
  352. $item['time'] = $item['create_time']? date('H:i',strtotime($item['create_time'])) : '';
  353. }
  354. }
  355. return [
  356. 'code' => 0,
  357. 'success'=> true,
  358. 'msg' => '操作成功',
  359. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  360. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  361. ];
  362. }
  363. }