PostRepository.php 25 KB

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