PostRepository.php 25 KB

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