PostRepository.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. 'avatar' => $userInfo['avatar'],
  225. 'content' => $request['content'],
  226. 'is_delete' => 0,
  227. ];
  228. if(isset($request['parent_id']) && $request['parent_id'] != 0){
  229. $comment = $this->postComment->find($request['parent_id']);
  230. if(!$comment){
  231. return Response::create([
  232. 'message' => '获取评论信息失败',
  233. 'status_code' => 500
  234. ]);
  235. }
  236. if($comment->parent_id){
  237. return Response::create([
  238. 'message' => '只能回复评论',
  239. 'status_code' => 500
  240. ]);
  241. }
  242. $data['parent_id'] = $request['parent_id'];
  243. }
  244. DB::beginTransaction();
  245. try{
  246. $this->postComment->create($data);
  247. $post->data->comment_count += 1;
  248. $post->data->save();
  249. DB::commit();
  250. return Response::create();
  251. }catch (QueryException $exception){
  252. DB::rollBack();
  253. Log::debug('评论内容:'.$request['post_id'].$exception->getMessage());
  254. return Response::create([
  255. 'message' => '评论失败,请重试',
  256. 'error' => $exception->getMessage(),
  257. 'status_code' => 500
  258. ]);
  259. }
  260. }
  261. /**
  262. * 内容列表
  263. */
  264. public function lists($request)
  265. {
  266. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  267. $where = [];
  268. if(isset($request['content'])){
  269. $where[] = ['content', 'like', "%{$request['content']}%"];
  270. }
  271. if(isset($request['is_suggest'])){
  272. $where[] = ['is_suggest', $request['is_suggest']];
  273. }
  274. if(isset($request['type'])){
  275. $where[] = ['type', $request['type']];
  276. }
  277. if(isset($request['uid'])){
  278. $where[] = ['uid', $request['uid']];
  279. }
  280. $sort = 'post.id';
  281. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  282. $sort = $request['sort'];
  283. }
  284. $post = $this->post;
  285. if(isset($request['waste']) && $request['waste'] == 1){
  286. $post = $post->onlyTrashed();
  287. }
  288. return $post
  289. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  290. ->select('post.*')
  291. ->where($where)
  292. ->where(function($query) use ($request){
  293. if(isset($request['keyword'])){
  294. $query->where('uid', '=', $request['keyword'])
  295. ->orWhere('username', 'like', "%{$request['keyword']}%")
  296. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  297. }
  298. })
  299. ->where(function($query) use ($request){
  300. if(isset($request['created_at'])){
  301. $time = explode('_', $request['created_at']);
  302. $query->whereBetween('post.created_at', $time);
  303. }
  304. })
  305. ->where(function ($query) use ($request){
  306. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  307. $ids = [];
  308. if (isset($request['category_ids'])) {
  309. $categoryIds = explode('_', $request['category_ids']);
  310. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  311. }
  312. if (isset($request['topic_ids'])) {
  313. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  314. }
  315. foreach ($ids as $key=>$id) {
  316. if ($key==0) {
  317. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  318. } else {
  319. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  320. }
  321. }
  322. }
  323. })
  324. ->orderBy($sort,'desc')
  325. ->paginate($perPage);
  326. }
  327. /**
  328. * 内容详情
  329. */
  330. public function detail($request)
  331. {
  332. return $this->post->withTrashed()->find($request['id']);
  333. }
  334. /**
  335. * 评论列表
  336. */
  337. public function commentList($request)
  338. {
  339. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  340. $where = [];
  341. if(isset($request['post_id'])){
  342. $where[] = ['post_id', $request['post_id']];
  343. }
  344. if(isset($request['uid'])){
  345. $where[] = ['uid', $request['uid']];
  346. }
  347. return $this->postComment
  348. ->where($where)
  349. ->orderBy('id','desc')
  350. ->paginate($perPage);
  351. }
  352. /**
  353. * 推荐内容
  354. */
  355. public function suggest($request)
  356. {
  357. $post = $this->post->where('id', $request['id'])->first();
  358. if(!$post){
  359. return Response::create([
  360. 'message' => '获取内容信息失败',
  361. 'status_code' => 500
  362. ]);
  363. }
  364. if($post->is_suggest == 1){
  365. $post->is_suggest = 0;
  366. }else{
  367. $post->is_suggest = 1;
  368. }
  369. DB::beginTransaction();
  370. try{
  371. $post->save();
  372. DB::commit();
  373. return Response::create();
  374. }catch (QueryException $exception){
  375. DB::rollBack();
  376. Log::debug('推荐内容:'.$request['id'].$exception->getMessage());
  377. return Response::create([
  378. 'message' => '操作失败,请重试',
  379. 'error' => $exception->getMessage(),
  380. 'status_code' => 500
  381. ]);
  382. }
  383. }
  384. /**
  385. * 删除内容
  386. */
  387. public function delete($request)
  388. {
  389. $post = $this->post->where('id', $request['id'])->first();
  390. if(!$post){
  391. return Response::create([
  392. 'message' => '获取内容信息失败',
  393. 'status_code' => 500
  394. ]);
  395. }
  396. DB::beginTransaction();
  397. try{
  398. $post->delete();
  399. DB::commit();
  400. return Response::create();
  401. }catch (QueryException $exception){
  402. DB::rollBack();
  403. Log::debug('删除内容:'.$request['id'].$exception->getMessage());
  404. return Response::create([
  405. 'message' => '操作失败,请重试',
  406. 'error' => $exception->getMessage(),
  407. 'status_code' => 500
  408. ]);
  409. }
  410. }
  411. /**
  412. * 复原内容
  413. */
  414. public function restore($request)
  415. {
  416. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  417. if(!$post){
  418. return Response::create([
  419. 'message' => '获取内容信息失败',
  420. 'status_code' => 500
  421. ]);
  422. }
  423. DB::beginTransaction();
  424. try{
  425. $post->restore();
  426. DB::commit();
  427. return Response::create();
  428. }catch (QueryException $exception){
  429. DB::rollBack();
  430. Log::debug('复原内容:'.$request['id'].$exception->getMessage());
  431. return Response::create([
  432. 'message' => '操作失败,请重试',
  433. 'error' => $exception->getMessage(),
  434. 'status_code' => 500
  435. ]);
  436. }
  437. }
  438. /**
  439. * 删除评论
  440. */
  441. public function commentDelete($request)
  442. {
  443. $comment = $this->postComment->find($request['id']);
  444. if(!$comment){
  445. return Response::create([
  446. 'message' => '获取评论信息失败',
  447. 'status_code' => 500
  448. ]);
  449. }
  450. if($comment->is_delete == 1){
  451. return Response::create([
  452. 'message' => '该评论已经删除',
  453. 'status_code' => 500
  454. ]);
  455. }
  456. DB::beginTransaction();
  457. try{
  458. $comment->is_delete = 1;
  459. $comment->save();
  460. DB::commit();
  461. return Response::create();
  462. }catch (QueryException $exception){
  463. DB::rollBack();
  464. Log::debug('删除评论:'.$request['id'].$exception->getMessage());
  465. return Response::create([
  466. 'message' => '操作失败,请重试',
  467. 'error' => $exception->getMessage(),
  468. 'status_code' => 500
  469. ]);
  470. }
  471. }
  472. /**
  473. * 隐藏内容
  474. */
  475. public function hide($request)
  476. {
  477. $post = $this->post->where('id', $request['id'])->first();
  478. if(!$post){
  479. return Response::create([
  480. 'message' => '获取内容信息失败',
  481. 'status_code' => 500
  482. ]);
  483. }
  484. if($post->is_hide == 1){
  485. $post->is_hide = 0;
  486. }else{
  487. $post->is_hide = 1;
  488. }
  489. DB::beginTransaction();
  490. try{
  491. $post->save();
  492. DB::commit();
  493. return Response::create();
  494. }catch (QueryException $exception){
  495. DB::rollBack();
  496. Log::debug('隐藏内容:'.$request['id'].$exception->getMessage());
  497. return Response::create([
  498. 'message' => '操作失败,请重试',
  499. 'error' => $exception->getMessage(),
  500. 'status_code' => 500
  501. ]);
  502. }
  503. }
  504. /**
  505. * 日志列表
  506. */
  507. public function log($request)
  508. {
  509. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  510. $where = [];
  511. if(isset($request['log_type'])){
  512. $where[] = ['log_type', $request['log_type']];
  513. }
  514. return $this->postLog
  515. ->where($where)
  516. ->where(function($query) use ($request){
  517. if(isset($request['created_at'])){
  518. $time = explode('_', $request['created_at']);
  519. $query->whereBetween('created_at', $time);
  520. }
  521. })
  522. ->orderBy('id','desc')
  523. ->paginate($perPage);
  524. }
  525. public function download($filePath, $type, $request)
  526. {
  527. try {
  528. set_time_limit(0);
  529. if (!ini_get("auto_detect_line_endings")) {
  530. ini_set("auto_detect_line_endings", '1');
  531. }
  532. // 文件路径
  533. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  534. // 设置标题
  535. if($type == 'post'){
  536. $title = [
  537. '内容', date('Y年m月d日')
  538. ];
  539. }else{
  540. $title = [
  541. '回收站内容', date('Y年m月d日')
  542. ];
  543. }
  544. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  545. $writer->insertone($title);
  546. // 内容
  547. if($type == 'post') {
  548. $header = [
  549. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  550. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  551. '真实收藏数', '总收藏数', '评论数'
  552. ];
  553. } else {
  554. $header = [
  555. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  556. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  557. ];
  558. }
  559. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  560. // $writer->setOutputBOM(Reader::BOM_UTF8);
  561. $writer->insertone($header);
  562. $where = [];
  563. if(isset($request['content'])){
  564. $where[] = ['content', 'like', "%{$request['content']}%"];
  565. }
  566. if(isset($request['is_suggest'])){
  567. $where[] = ['is_suggest', $request['is_suggest']];
  568. }
  569. if(isset($request['type'])){
  570. $where[] = ['type', $request['type']];
  571. }
  572. $sort = 'post.id';
  573. if(isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])){
  574. $sort = $request['sort'];
  575. }
  576. $post = $this->post;
  577. if($type == 'post_waste'){
  578. $post = $post->onlyTrashed();
  579. }
  580. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  581. ->select('post.*')
  582. ->where($where)
  583. ->where(function($query) use ($request){
  584. if(isset($request['keyword'])){
  585. $query->where('uid', '=', $request['keyword'])
  586. ->orWhere('username', 'like', "%{$request['keyword']}%")
  587. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  588. }
  589. })
  590. ->where(function($query) use ($request){
  591. if(isset($request['created_at'])){
  592. $time = explode('_', $request['created_at']);
  593. $query->whereBetween('post.created_at', $time);
  594. }
  595. })
  596. ->where(function ($query) use ($request){
  597. if(isset($request['category_ids']) || isset($request['topic_ids'])){
  598. $ids = [];
  599. if (isset($request['category_ids'])) {
  600. $categoryIds = explode('_', $request['category_ids']);
  601. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  602. }
  603. if (isset($request['topic_ids'])) {
  604. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  605. }
  606. Log::debug('话题ids:'.json_encode($ids));
  607. foreach ($ids as $key=>$id) {
  608. if ($key==0) {
  609. $query = $query->whereRaw('FIND_IN_SET('.$id.',topic_ids)');
  610. } else {
  611. $query = $query->orWhereRaw('FIND_IN_SET('.$id.',topic_ids)');
  612. }
  613. }
  614. }
  615. })
  616. ->orderBy($sort,'desc')
  617. ->chunk(1, function ($posts) use ($writer, $type){
  618. $data = [];
  619. foreach ($posts as $post) {
  620. if($type == 'post'){
  621. $tmp = [
  622. $post->id,
  623. Carbon::parse($post->created_at)->toDateTimeString(),
  624. $post->username,
  625. $post->location,
  626. implode(' ', $post->topic()->toArray()),
  627. subtext($post->content, 20),
  628. $post->data->pv_real,
  629. $post->data->pv,
  630. $post->data->praise_real_count,
  631. $post->data->praise_count,
  632. $post->data->share_real_count,
  633. $post->data->share_count,
  634. $post->data->collect_real_count,
  635. $post->data->collect_count,
  636. $post->data->comment_count
  637. ];
  638. }else{
  639. $tmp = [
  640. $post->id,
  641. Carbon::parse($post->created_at)->toDateTimeString(),
  642. $post->username,
  643. Carbon::parse($post->created_at)->toDateTimeString(),
  644. subtext($post->content, 20),
  645. $post->data->pv_real,
  646. $post->data->praise_real_count,
  647. $post->data->share_real_count,
  648. $post->data->collect_real_count,
  649. $post->data->comment_count
  650. ];
  651. }
  652. foreach ($tmp as $key => $value) {
  653. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  654. }
  655. $data[] = $tmp;
  656. }
  657. $writer->insertAll($data);
  658. });
  659. Log::channel('download')->info('内容导出成功!');
  660. } catch (CannotInsertRecord $e) {
  661. $e->getRecord();
  662. Log::channel('download')->info('内容导出失败!');
  663. }
  664. }
  665. }