PostRepository.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Post;
  9. use App\Models\CategoryTopic;
  10. use App\Models\Post;
  11. use App\Models\PostComment;
  12. use App\Models\PostData;
  13. use App\Models\PostImgs;
  14. use App\Models\PostLog;
  15. use App\Models\Topic;
  16. use App\Traits\PostTrait;
  17. use App\Traits\UserTrait;
  18. use Illuminate\Database\QueryException;
  19. use Dingo\Api\Http\Response;
  20. use Illuminate\Support\Carbon;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Log;
  23. use Illuminate\Support\Facades\Redis;
  24. use Symfony\Component\HttpKernel\Exception\HttpException;
  25. use Tymon\JWTAuth\Facades\JWTAuth;
  26. use League\Csv\Writer;
  27. use League\Csv\CannotInsertRecord;
  28. class PostRepository
  29. {
  30. use PostTrait;
  31. use UserTrait;
  32. public function __construct(Post $post,
  33. PostData $postData,
  34. PostComment $postComment,
  35. PostImgs $postImgs,
  36. PostLog $postLog,
  37. CategoryTopic $categoryTopic,
  38. Topic $topic)
  39. {
  40. $this->post = $post;
  41. $this->postData = $postData;
  42. $this->postComment = $postComment;
  43. $this->postImgs = $postImgs;
  44. $this->postLog = $postLog;
  45. $this->categoryTopic = $categoryTopic;
  46. $this->topic = $topic;
  47. }
  48. /**
  49. * 发布内容
  50. */
  51. public function create($request)
  52. {
  53. //验证小号
  54. $userInfo = $this->getUserInfo($request['uid']);
  55. if(!$userInfo || $userInfo['type'] != 1){
  56. return Response::create([
  57. 'message' => '所选小号信息有误',
  58. 'status_code' => 500
  59. ]);
  60. }
  61. //验证话题
  62. $topicIds = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  63. $topicCount = count($topicIds);
  64. if($topicCount == 0 || $topicCount > 5){
  65. return Response::create([
  66. 'message' => '所选话题必须1-5个',
  67. 'status_code' => 500
  68. ]);
  69. }
  70. $topicIds = implode(',', $topicIds);
  71. $data = [
  72. 'uid' => $request['uid'],
  73. 'username' => $userInfo['username'],
  74. 'mobile' => $userInfo['mobile'],
  75. 'avatar' => $userInfo['avatar'],
  76. 'type' => $request['type'],
  77. 'img' => $request['img'],
  78. 'video' => $request['video']??'',
  79. 'topic_ids' => $topicIds,
  80. 'title' => $request['title']??'',
  81. 'content' => $request['content'],
  82. 'location' => $request['location']??'',
  83. 'is_suggest' => $request['is_suggest'],
  84. 'is_hide' => 0
  85. ];
  86. $date = date('Y-m-d H:i:s');
  87. DB::beginTransaction();
  88. try{
  89. $post = $this->post->create($data);
  90. $this->postData->create([
  91. 'post_id' => $post->id,
  92. 'pv' => 0,
  93. 'pv_real' => 0,
  94. 'dislike_count' => 0,
  95. 'praise_count' => 0,
  96. 'praise_real_count' => 0,
  97. 'share_count' => 0,
  98. 'share_real_count' => 0,
  99. 'comment_count' => 0,
  100. 'comment_real_count' => 0,
  101. 'collect_count' => 0,
  102. 'collect_real_count' => 0,
  103. 'available_bean' => $this->availableBean(),
  104. 'will_collect_bean' => rand(100, 200),
  105. 'collect_bean' => 0,
  106. 'weight' => 0
  107. ]);
  108. if(!empty($request['imgs']) && $request['type'] == 'image'){
  109. $imgData = [];
  110. foreach($request['imgs'] as $img){
  111. $imgData[] = [
  112. 'post_id' => $post->id,
  113. 'img' => $img,
  114. 'created_at' => $date,
  115. 'updated_at' => $date
  116. ];
  117. }
  118. $this->postImgs->insert($imgData);
  119. }
  120. DB::commit();
  121. return Response::create();
  122. }catch (QueryException $exception){
  123. DB::rollBack();
  124. Log::debug('发布内容:'.$exception->getMessage());
  125. return Response::create([
  126. 'message' => '发布失败,请重试',
  127. 'error' => $exception->getMessage(),
  128. 'status_code' => 500
  129. ]);
  130. }
  131. }
  132. /**
  133. * 增加数据
  134. */
  135. public function addData($request)
  136. {
  137. $token = JWTAuth::decode(JWTAuth::getToken());
  138. if(!$token || $token['type'] != 1){
  139. return Response::create([
  140. 'message' => '获取登陆信息失败',
  141. 'status_code' => 500
  142. ]);
  143. }
  144. $uid = $token['user']->id;
  145. $username = $token['user']->username;
  146. //验证小号数量
  147. $postData = $this->postData->where('post_id', $request['post_id'])->first();
  148. if(!$postData){
  149. return Response::create([
  150. 'message' => '获取内容失败',
  151. 'status_code' => 500
  152. ]);
  153. }
  154. if($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0){
  155. return Response::create([
  156. 'message' => '增加数据不能同时为0',
  157. 'status_code' => 500
  158. ]);
  159. }
  160. $content = '';
  161. if($request['add_pv']){
  162. $postData->pv += $request['add_pv'];
  163. $content .= '浏览数增加'.$request['add_pv'].'、';
  164. }
  165. if($request['add_praise_count']){
  166. $postData->praise_count += $request['add_praise_count'];
  167. $content .= '点赞数增加'.$request['add_praise_count'].'、';
  168. }
  169. if($request['add_collect_count']){
  170. $postData->collect_count += $request['add_collect_count'];
  171. $content .= '收藏数增加'.$request['add_collect_count'].'、';
  172. }
  173. if($request['add_share_count']){
  174. $postData->share_count += $request['add_share_count'];
  175. $content .= '分享数增加'.$request['add_share_count'];
  176. }
  177. $content = rtrim($content, '、');
  178. DB::beginTransaction();
  179. try{
  180. $postData->save();
  181. $this->postLog->create([
  182. 'post_id' => $request['post_id'],
  183. 'uid' => $uid,
  184. 'username' => $username,
  185. 'log_type' => 'add_data',
  186. 'content' => $content
  187. ]);
  188. DB::commit();
  189. return Response::create();
  190. }catch (QueryException $exception){
  191. DB::rollBack();
  192. Log::debug('内容增加数据:'.$request['post_id'].$exception->getMessage());
  193. return Response::create([
  194. 'message' => '增加数据失败,请重试',
  195. 'error' => $exception->getMessage(),
  196. 'status_code' => 500
  197. ]);
  198. }
  199. }
  200. /**
  201. * 评论&回复
  202. */
  203. public function comment($request)
  204. {
  205. //验证小号
  206. $userInfo = $this->getUserInfo($request['uid']);
  207. if(!$userInfo || $userInfo['type'] != 1){
  208. return Response::create([
  209. 'message' => '所选小号信息有误',
  210. 'status_code' => 500
  211. ]);
  212. }
  213. $post = $this->post->find($request['post_id']);
  214. if(!$post){
  215. return Response::create([
  216. 'message' => '获取内容失败',
  217. 'status_code' => 500
  218. ]);
  219. }
  220. $data = [
  221. 'uid' => $request['uid'],
  222. 'post_id' => $request['post_id'],
  223. 'parent_id' => 0,
  224. 'username' => $userInfo['username'],
  225. 'avatar' => $userInfo['avatar'],
  226. 'content' => $request['content'],
  227. 'is_delete' => 0,
  228. ];
  229. if(isset($request['parent_id']) && $request['parent_id'] != 0){
  230. $comment = $this->postComment->find($request['parent_id']);
  231. if(!$comment){
  232. return Response::create([
  233. 'message' => '获取评论信息失败',
  234. 'status_code' => 500
  235. ]);
  236. }
  237. if($comment->parent_id){
  238. return Response::create([
  239. 'message' => '只能回复评论',
  240. 'status_code' => 500
  241. ]);
  242. }
  243. $data['parent_id'] = $request['parent_id'];
  244. }
  245. DB::beginTransaction();
  246. try{
  247. $this->postComment->create($data);
  248. $post->data->comment_count += 1;
  249. $post->data->comment_real_count += 1;
  250. $post->data->save();
  251. DB::commit();
  252. return Response::create();
  253. }catch (QueryException $exception){
  254. DB::rollBack();
  255. Log::debug('评论内容:'.$request['post_id'].$exception->getMessage());
  256. return Response::create([
  257. 'message' => '评论失败,请重试',
  258. 'error' => $exception->getMessage(),
  259. 'status_code' => 500
  260. ]);
  261. }
  262. }
  263. /**
  264. * 内容列表
  265. */
  266. public function lists($request)
  267. {
  268. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  269. $where = [];
  270. if(isset($request['content'])){
  271. $where[] = ['content', 'like', "%{$request['content']}%"];
  272. }
  273. if(isset($request['is_suggest'])){
  274. $where[] = ['is_suggest', $request['is_suggest']];
  275. }
  276. if(isset($request['type'])){
  277. $where[] = ['type', $request['type']];
  278. }
  279. $sort = 'post.id';
  280. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  281. $sort = $request['sort'];
  282. }
  283. $post = $this->post;
  284. if(isset($request['waste']) && $request['waste'] == 1){
  285. $post = $post->onlyTrashed();
  286. }
  287. return $post
  288. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  289. ->select('post.*')
  290. ->where($where)
  291. ->where(function($query) use ($request){
  292. if(isset($request['keyword'])){
  293. $query->where('uid', '=', $request['keyword'])
  294. ->orWhere('username', 'like', "%{$request['keyword']}%")
  295. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  296. }
  297. })
  298. ->where(function($query) use ($request){
  299. if(isset($request['created_at'])){
  300. $time = explode('_', $request['created_at']);
  301. $query->whereBetween('post.created_at', $time);
  302. }
  303. })
  304. ->where(function ($query) use ($request){
  305. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  306. $ids = [];
  307. if (isset($request['category_ids'])) {
  308. $categoryIds = explode('_', $request['category_ids']);
  309. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  310. }
  311. if (isset($request['topic_ids'])) {
  312. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  313. }
  314. foreach ($ids as $key=>$id) {
  315. if ($key==0) {
  316. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  317. } else {
  318. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  319. }
  320. }
  321. }
  322. })
  323. ->orderBy($sort,'desc')
  324. ->paginate($perPage);
  325. }
  326. /**
  327. * 内容详情
  328. */
  329. public function detail($request)
  330. {
  331. return $this->post->withTrashed()->find($request['id']);
  332. }
  333. /**
  334. * 评论列表
  335. */
  336. public function commentList($request)
  337. {
  338. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  339. return $this->postComment
  340. ->where('post_id', $request['post_id'])
  341. ->orderBy('id','desc')
  342. ->paginate($perPage);
  343. }
  344. /**
  345. * 推荐内容
  346. */
  347. public function suggest($request)
  348. {
  349. $post = $this->post->where('id', $request['id'])->first();
  350. if(!$post){
  351. return Response::create([
  352. 'message' => '获取内容信息失败',
  353. 'status_code' => 500
  354. ]);
  355. }
  356. if($post->is_suggest == 1){
  357. $post->is_suggest = 0;
  358. }else{
  359. $post->is_suggest = 1;
  360. }
  361. DB::beginTransaction();
  362. try{
  363. $post->save();
  364. DB::commit();
  365. return Response::create();
  366. }catch (QueryException $exception){
  367. DB::rollBack();
  368. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  369. return Response::create([
  370. 'message' => '操作失败,请重试',
  371. 'error' => $exception->getMessage(),
  372. 'status_code' => 500
  373. ]);
  374. }
  375. }
  376. /**
  377. * 删除内容
  378. */
  379. public function delete($request)
  380. {
  381. $post = $this->post->where('id', $request['id'])->first();
  382. if(!$post){
  383. return Response::create([
  384. 'message' => '获取内容信息失败',
  385. 'status_code' => 500
  386. ]);
  387. }
  388. DB::beginTransaction();
  389. try{
  390. $post->delete();
  391. DB::commit();
  392. return Response::create();
  393. }catch (QueryException $exception){
  394. DB::rollBack();
  395. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  396. return Response::create([
  397. 'message' => '操作失败,请重试',
  398. 'error' => $exception->getMessage(),
  399. 'status_code' => 500
  400. ]);
  401. }
  402. }
  403. /**
  404. * 复原内容
  405. */
  406. public function restore($request)
  407. {
  408. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  409. if(!$post){
  410. return Response::create([
  411. 'message' => '获取内容信息失败',
  412. 'status_code' => 500
  413. ]);
  414. }
  415. DB::beginTransaction();
  416. try{
  417. $post->restore();
  418. DB::commit();
  419. return Response::create();
  420. }catch (QueryException $exception){
  421. DB::rollBack();
  422. Log::debug('复原内容:'.$request['id'].$exception->getMessage());
  423. return Response::create([
  424. 'message' => '操作失败,请重试',
  425. 'error' => $exception->getMessage(),
  426. 'status_code' => 500
  427. ]);
  428. }
  429. }
  430. /**
  431. * 删除评论
  432. */
  433. public function commentDelete($request)
  434. {
  435. $comment = $this->postComment->find($request['id']);
  436. if(!$comment){
  437. return Response::create([
  438. 'message' => '获取评论信息失败',
  439. 'status_code' => 500
  440. ]);
  441. }
  442. if($comment->is_delete == 1){
  443. return Response::create([
  444. 'message' => '该评论已经删除',
  445. 'status_code' => 500
  446. ]);
  447. }
  448. DB::beginTransaction();
  449. try{
  450. $comment->is_delete = 1;
  451. $comment->save();
  452. DB::commit();
  453. return Response::create();
  454. }catch (QueryException $exception){
  455. DB::rollBack();
  456. Log::debug('删除评论:'.$request['id'].$exception->getMessage());
  457. return Response::create([
  458. 'message' => '操作失败,请重试',
  459. 'error' => $exception->getMessage(),
  460. 'status_code' => 500
  461. ]);
  462. }
  463. }
  464. /**
  465. * 隐藏内容
  466. */
  467. public function hide($request)
  468. {
  469. $post = $this->post->where('id', $request['id'])->first();
  470. if(!$post){
  471. return Response::create([
  472. 'message' => '获取内容信息失败',
  473. 'status_code' => 500
  474. ]);
  475. }
  476. if($post->is_hide == 1){
  477. $post->is_hide = 0;
  478. }else{
  479. $post->is_hide = 1;
  480. }
  481. DB::beginTransaction();
  482. try{
  483. $post->save();
  484. DB::commit();
  485. return Response::create();
  486. }catch (QueryException $exception){
  487. DB::rollBack();
  488. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  489. return Response::create([
  490. 'message' => '操作失败,请重试',
  491. 'error' => $exception->getMessage(),
  492. 'status_code' => 500
  493. ]);
  494. }
  495. }
  496. /**
  497. * 日志列表
  498. */
  499. public function log($request)
  500. {
  501. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  502. $where = [];
  503. if(isset($request['log_type'])){
  504. $where[] = ['log_type', $request['log_type']];
  505. }
  506. return $this->postLog
  507. ->where($where)
  508. ->where(function($query) use ($request){
  509. if(isset($request['created_at'])){
  510. $time = explode('_', $request['created_at']);
  511. $query->whereBetween('created_at', $time);
  512. }
  513. })
  514. ->orderBy('id','desc')
  515. ->paginate($perPage);
  516. }
  517. public function download($filePath, $type, $request)
  518. {
  519. try {
  520. set_time_limit(0);
  521. if (!ini_get("auto_detect_line_endings")) {
  522. ini_set("auto_detect_line_endings", '1');
  523. }
  524. // 文件路径
  525. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  526. // 设置标题
  527. if($type == 'post'){
  528. $title = [
  529. '内容', date('Y年m月d日')
  530. ];
  531. }else{
  532. $title = [
  533. '回收站内容', date('Y年m月d日')
  534. ];
  535. }
  536. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  537. $writer->insertone($title);
  538. // 内容
  539. if($type == 'post') {
  540. $header = [
  541. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  542. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  543. '真实收藏数', '总收藏数', '评论数'
  544. ];
  545. } else {
  546. $header = [
  547. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  548. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  549. ];
  550. }
  551. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  552. // $writer->setOutputBOM(Reader::BOM_UTF8);
  553. $writer->insertone($header);
  554. $where = [];
  555. if(isset($request['content'])){
  556. $where[] = ['content', 'like', "%{$request['content']}%"];
  557. }
  558. if(isset($request['is_suggest'])){
  559. $where[] = ['is_suggest', $request['is_suggest']];
  560. }
  561. if(isset($request['type'])){
  562. $where[] = ['type', $request['type']];
  563. }
  564. $sort = 'post.id';
  565. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  566. $sort = $request['sort'];
  567. }
  568. $post = $this->post;
  569. if($type == 'post_waste'){
  570. $post = $post->onlyTrashed();
  571. }
  572. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  573. ->select('post.*')
  574. ->where($where)
  575. ->where(function($query) use ($request){
  576. if(isset($request['keyword'])){
  577. $query->where('uid', '=', $request['keyword'])
  578. ->orWhere('username', 'like', "%{$request['keyword']}%")
  579. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  580. }
  581. })
  582. ->where(function($query) use ($request){
  583. if(isset($request['created_at'])){
  584. $time = explode('_', $request['created_at']);
  585. $query->whereBetween('post.created_at', $time);
  586. }
  587. })
  588. ->where(function ($query) use ($request){
  589. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  590. $ids = [];
  591. if (isset($request['category_ids'])) {
  592. $categoryIds = explode('_', $request['category_ids']);
  593. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  594. }
  595. if (isset($request['topic_ids'])) {
  596. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  597. }
  598. Log::debug('话题ids:'.json_encode($ids));
  599. foreach ($ids as $key=>$id) {
  600. if ($key==0) {
  601. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  602. } else {
  603. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  604. }
  605. }
  606. }
  607. })
  608. ->orderBy($sort,'desc')
  609. ->chunk(1, function ($posts) use ($writer, $type){
  610. $data = [];
  611. foreach ($posts as $post) {
  612. if($type == 'post'){
  613. $tmp = [
  614. $post->id,
  615. Carbon::parse($post->created_at)->toDateTimeString(),
  616. $post->username,
  617. $post->location,
  618. implode(' ', $post->topic()->toArray()),
  619. subtext($post->content, 20),
  620. $post->data->pv_real,
  621. $post->data->pv,
  622. $post->data->praise_real_count,
  623. $post->data->praise_count,
  624. $post->data->share_real_count,
  625. $post->data->share_count,
  626. $post->data->collect_real_count,
  627. $post->data->collect_count,
  628. $post->data->comment_count
  629. ];
  630. }else{
  631. $tmp = [
  632. $post->id,
  633. Carbon::parse($post->created_at)->toDateTimeString(),
  634. $post->username,
  635. Carbon::parse($post->created_at)->toDateTimeString(),
  636. subtext($post->content, 20),
  637. $post->data->pv_real,
  638. $post->data->praise_real_count,
  639. $post->data->share_real_count,
  640. $post->data->collect_real_count,
  641. $post->data->comment_count
  642. ];
  643. }
  644. foreach ($tmp as $key => $value) {
  645. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  646. }
  647. $data[] = $tmp;
  648. }
  649. $writer->insertAll($data);
  650. });
  651. Log::channel('download')->info('内容导出成功!');
  652. } catch (CannotInsertRecord $e) {
  653. $e->getRecord();
  654. Log::channel('download')->info('内容导出失败!');
  655. }
  656. }
  657. }