PostRepository.php 24 KB

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