PostRepository.php 25 KB

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