PostRepository.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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\Behavior;
  10. use App\Models\CategoryTopic;
  11. use App\Models\Post;
  12. use App\Models\PostComment;
  13. use App\Models\PostData;
  14. use App\Models\PostImgs;
  15. use App\Models\PostLog;
  16. use App\Models\PostStatistics;
  17. use App\Models\PostStore;
  18. use App\Models\PostStoreImgs;
  19. use App\Models\Topic;
  20. use App\Service\RabbitMqUtil;
  21. use App\Traits\PostTrait;
  22. use App\Traits\UserTrait;
  23. use Illuminate\Database\QueryException;
  24. use Dingo\Api\Http\Response;
  25. use Illuminate\Support\Carbon;
  26. use Illuminate\Support\Facades\DB;
  27. use Illuminate\Support\Facades\Log;
  28. use Illuminate\Support\Facades\Redis;
  29. use Symfony\Component\HttpKernel\Exception\HttpException;
  30. use Tymon\JWTAuth\Facades\JWTAuth;
  31. use League\Csv\Writer;
  32. use League\Csv\CannotInsertRecord;
  33. class PostRepository
  34. {
  35. use PostTrait;
  36. use UserTrait;
  37. public function __construct(Post $post,
  38. PostData $postData,
  39. PostComment $postComment,
  40. PostImgs $postImgs,
  41. PostLog $postLog,
  42. RabbitMqUtil $rabbitMqUtil,
  43. Behavior $behavior,
  44. CategoryTopic $categoryTopic,
  45. PostStatistics $postStatistics,
  46. PostStore $postStore,
  47. PostStoreImgs $postStoreImgs,
  48. Topic $topic)
  49. {
  50. $this->post = $post;
  51. $this->postData = $postData;
  52. $this->postComment = $postComment;
  53. $this->postImgs = $postImgs;
  54. $this->postLog = $postLog;
  55. $this->rabbitMqUtil = $rabbitMqUtil;
  56. $this->behavior = $behavior;
  57. $this->categoryTopic = $categoryTopic;
  58. $this->topic = $topic;
  59. $this->postStatistics = $postStatistics;
  60. $this->postStore = $postStore;
  61. $this->postStoreImgs = $postStoreImgs;
  62. }
  63. /**
  64. * 发布内容
  65. */
  66. public function create($request)
  67. {
  68. //富文本标题
  69. if($request['type'] == 'html' && (!isset($request['title']) || !$request['title'])){
  70. return Response::create([
  71. 'message' => '当类型是富文本时,标题不能为空',
  72. 'status_code' => 500
  73. ]);
  74. }
  75. //验证小号
  76. $userInfo = $this->getUserInfo($request['uid']);
  77. Log::debug('发布内容小号信息:' . json_encode($userInfo));
  78. if (!$userInfo || $userInfo['type'] != 1) {
  79. return Response::create([
  80. 'message' => '所选小号信息有误',
  81. 'status_code' => 500
  82. ]);
  83. }
  84. if(isset($request['is_point']) && $request['is_point']){
  85. $res = $this->uploadImage($request['img']);
  86. if($res && isset($res['data']) && isset($res['data']['url'])){
  87. $request['img'] = config('customer.chxq_oss_url').$res['data']['url'];
  88. Log::debug('内容上传图片成功结果'.json_encode($res));
  89. }else{
  90. Log::debug('内容上传图片失败结果'.json_encode($res));
  91. return Response::create([
  92. 'message' => '图片格式有误,请重试',
  93. 'status_code' => 500
  94. ]);
  95. }
  96. }
  97. //验证话题
  98. $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  99. $topicCount = count($topicIdsArray);
  100. if ($topicCount == 0 || $topicCount > 5) {
  101. return Response::create([
  102. 'message' => '所选话题必须1-5个',
  103. 'status_code' => 500
  104. ]);
  105. }
  106. $topicIds = implode(',', $topicIdsArray);
  107. //验证内容字数
  108. if($request['type'] != 'html'){
  109. $html = strip_tags($request['content']);
  110. if (mb_strlen($html, 'UTF8') > 1000) {
  111. return Response::create([
  112. 'message' => '所传内容不能超过1000字',
  113. 'status_code' => 500
  114. ]);
  115. }
  116. }
  117. $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  118. $score = $fresh / 86400;
  119. $data = [
  120. 'uid' => $userInfo['uid'],
  121. 'username' => $userInfo['username'],
  122. 'mobile' => $userInfo['mobile'],
  123. 'avatar' => $userInfo['avatar'] ?? '',
  124. 'type' => $request['type'],
  125. 'img' => $request['img'],
  126. 'video' => $request['video'] ?? '',
  127. 'video_id' => $request['video_id'] ?? '',
  128. 'topic_ids' => $topicIds,
  129. 'title' => $request['title'] ?? '',
  130. 'content' => $request['content'],
  131. 'location' => $request['location'] ?? '',
  132. 'is_suggest' => $request['is_suggest'],
  133. 'is_hide' => 0,
  134. 'weight' => $score
  135. ];
  136. $date = date('Y-m-d H:i:s');
  137. DB::beginTransaction();
  138. try {
  139. $post = $this->post->create($data);
  140. $postData = $this->postData->create([
  141. 'post_id' => $post->id,
  142. 'pv' => 0,
  143. 'pv_real' => 0,
  144. 'dislike_count' => 0,
  145. 'praise_count' => 0,
  146. 'praise_real_count' => 0,
  147. 'share_count' => 0,
  148. 'share_real_count' => 0,
  149. 'comment_count' => 0,
  150. 'collect_count' => 0,
  151. 'collect_real_count' => 0,
  152. 'available_bean' => $this->availableBean(),
  153. 'will_collect_bean' => rand(100, 200),
  154. 'collect_bean' => 0
  155. ]);
  156. $imgs = [];
  157. if (!empty($request['imgs']) && $request['type'] == 'image') {
  158. $imgData = [];
  159. foreach ($request['imgs'] as $img) {
  160. $imgs[] = $img;
  161. $imgData[] = [
  162. 'post_id' => $post->id,
  163. 'img' => $img,
  164. 'created_at' => $date,
  165. 'updated_at' => $date
  166. ];
  167. }
  168. $this->postImgs->insert($imgData);
  169. }
  170. DB::commit();
  171. Redis::zadd('post_trigger_type', 0, $post->id);
  172. foreach ($topicIdsArray as $id) {
  173. Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
  174. Redis::zincrby('topic.just_use_count', 1, $id);
  175. }
  176. $virus = $this->behavior->where('behavior_identification', 'publish')->first();
  177. if ($virus) {
  178. if ($post->title) {
  179. $desc = $post->title;
  180. } else {
  181. $desc = subtext(strip_tags($post->content), 20);
  182. }
  183. $this->rabbitMqUtil->push('virus_add', [
  184. 'behavior_id' => $virus->virus_behavior_id,
  185. 'behavior_flag' => 'publish',
  186. 'post_id' => $post->id,
  187. 'post_type' => $post->type,
  188. 'post_desc' => $desc,
  189. 'post_cover' => $post->img,
  190. 'target_id' => $post->uid,
  191. 'action_id' => $post->id,
  192. ]);
  193. }
  194. Redis::HSET('post_info_'.$post->id,
  195. 'id', $post->id,
  196. 'uid', $post->uid,
  197. 'type', $post->type,
  198. 'img', $post->img,
  199. 'imgs', json_encode($imgs),
  200. 'video', $post->video,
  201. 'topic_ids', $post->topic_ids,
  202. 'title', $post->title,
  203. 'content', $post->content,
  204. 'location', $post->location,
  205. 'pv', $postData->pv,
  206. 'dislike_count', $postData->dislike_count,
  207. 'praise_count', $postData->praise_count,
  208. 'share_count', $postData->share_count,
  209. 'comment_count', $postData->comment_count,
  210. 'collect_count', $postData->collect_count,
  211. 'available_bean', $postData->available_bean,
  212. 'will_collect_bean', $postData->will_collect_bean,
  213. 'create_bean', $postData->create_bean,
  214. 'collect_bean', $postData->collect_bean,
  215. 'created_at', $post->created_at);
  216. return Response::create();
  217. } catch (QueryException $exception) {
  218. DB::rollBack();
  219. Log::debug('发布内容:' . $exception->getMessage());
  220. return Response::create([
  221. 'message' => '发布失败,请重试',
  222. 'error' => $exception->getMessage(),
  223. 'status_code' => 500
  224. ]);
  225. }
  226. }
  227. /**
  228. * 增加数据
  229. */
  230. public function addData($request)
  231. {
  232. $token = JWTAuth::decode(JWTAuth::getToken());
  233. if (!$token || $token['type'] != 1) {
  234. return Response::create([
  235. 'message' => '获取登陆信息失败',
  236. 'status_code' => 500
  237. ]);
  238. }
  239. $uid = $token['user']->id;
  240. $username = $token['user']->username;
  241. //验证小号数量
  242. $number = max([
  243. $request['add_pv'],
  244. $request['add_praise_count'],
  245. $request['add_collect_count'],
  246. $request['add_share_count']
  247. ]);
  248. $members = $this->getSystemMember($number);
  249. if (!$members || $members['status_code'] != 200) {
  250. return Response::create([
  251. 'message' => $members['message'],
  252. 'status_code' => 500
  253. ]);
  254. }
  255. $post = $this->post->find($request['post_id']);
  256. $postData = $this->postData->where('post_id', $request['post_id'])->first();
  257. if (!$postData || !$post) {
  258. return Response::create([
  259. 'message' => '获取内容失败',
  260. 'status_code' => 500
  261. ]);
  262. }
  263. if ($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0) {
  264. return Response::create([
  265. 'message' => '增加数据不能同时为0',
  266. 'status_code' => 500
  267. ]);
  268. }
  269. $content = [
  270. 'add_pv' => 0,
  271. 'add_praise_count' => 0,
  272. 'add_collect_count' => 0,
  273. 'add_share_count' => 0,
  274. ];
  275. if ($request['add_pv']) {
  276. $postData->pv += $request['add_pv'];
  277. $content['add_pv'] = $request['add_pv'];
  278. }
  279. if ($request['add_praise_count']) {
  280. $postData->praise_count += $request['add_praise_count'];
  281. $content['add_praise_count'] = $request['add_praise_count'];
  282. }
  283. if ($request['add_collect_count']) {
  284. $postData->collect_count += $request['add_collect_count'];
  285. $content['add_collect_count'] = $request['add_collect_count'];
  286. }
  287. if ($request['add_share_count']) {
  288. $postData->share_count += $request['add_share_count'];
  289. $content['add_share_count'] = $request['add_share_count'];
  290. }
  291. DB::beginTransaction();
  292. try {
  293. $postData->save();
  294. $this->postLog->create([
  295. 'post_id' => $request['post_id'],
  296. 'uid' => $uid,
  297. 'username' => $username,
  298. 'log_type' => 'add_data',
  299. 'content' => json_encode($content)
  300. ]);
  301. DB::commit();
  302. $virus = $this->behavior
  303. ->whereIn('behavior_identification', ['read', 'forward', 'like', 'collect'])
  304. ->pluck('virus_behavior_id', 'behavior_identification');
  305. if ($post->title) {
  306. $desc = $post->title;
  307. } else {
  308. $desc = subtext(strip_tags($post->content), 20);
  309. }
  310. $data = [
  311. 'behavior_value' => 1,
  312. 'post_id' => $post->id,
  313. 'post_type' => $post->type,
  314. 'post_author_uid' => $post->uid,
  315. 'post_desc' => $desc,
  316. 'post_cover' => $post->img,
  317. 'action_id' => $post->id,
  318. ];
  319. foreach ($members['data'] as $key => $member) {
  320. if (isset($virus['read']) && $request['add_pv'] > $key) {
  321. $newData = array_merge($data, [
  322. 'behavior_id' => $virus['read'],
  323. 'behavior_flag' => 'read',
  324. 'target_id' => $member['uid'],
  325. ]);
  326. $this->rabbitMqUtil->push('virus_add', $newData);
  327. }
  328. if (isset($virus['like']) && $request['add_praise_count'] > $key) {
  329. $newData = array_merge($data, [
  330. 'behavior_id' => $virus['like'],
  331. 'behavior_flag' => 'like',
  332. 'target_id' => $member['uid'],
  333. ]);
  334. $this->rabbitMqUtil->push('virus_add', $newData);
  335. }
  336. if (isset($virus['collect']) && $request['add_collect_count'] > $key) {
  337. $newData = array_merge($data, [
  338. 'behavior_id' => $virus['collect'],
  339. 'behavior_flag' => 'collect',
  340. 'target_id' => $member['uid'],
  341. ]);
  342. $this->rabbitMqUtil->push('virus_add', $newData);
  343. }
  344. if (isset($virus['forward']) && $request['add_share_count'] > $key) {
  345. $newData = array_merge($data, [
  346. 'behavior_id' => $virus['forward'],
  347. 'behavior_flag' => 'forward',
  348. 'target_id' => $member['uid'],
  349. ]);
  350. $this->rabbitMqUtil->push('virus_add', $newData);
  351. }
  352. }
  353. return Response::create();
  354. } catch (QueryException $exception) {
  355. DB::rollBack();
  356. Log::debug('内容增加数据:' . $request['post_id'] . $exception->getMessage());
  357. return Response::create([
  358. 'message' => '增加数据失败,请重试',
  359. 'error' => $exception->getMessage(),
  360. 'status_code' => 500
  361. ]);
  362. }
  363. }
  364. /**
  365. * 评论&回复
  366. */
  367. public function comment($request)
  368. {
  369. //验证小号
  370. $userInfo = $this->getUserInfo($request['uid']);
  371. Log::debug('评论&回复小号' . json_encode($userInfo));
  372. if (!$userInfo || $userInfo['type'] != 1) {
  373. return Response::create([
  374. 'message' => '所选小号信息有误',
  375. 'status_code' => 500
  376. ]);
  377. }
  378. $post = $this->post->find($request['post_id']);
  379. if (!$post) {
  380. return Response::create([
  381. 'message' => '获取内容失败',
  382. 'status_code' => 500
  383. ]);
  384. }
  385. $data = [
  386. 'uid' => $request['uid'],
  387. 'post_id' => $request['post_id'],
  388. 'parent_id' => 0,
  389. 'username' => $userInfo['username'],
  390. 'reply_uid' => 0,
  391. 'reply_username' => '',
  392. 'avatar' => $userInfo['avatar'] ?? '',
  393. 'content' => $request['content'],
  394. 'is_delete' => 0,
  395. ];
  396. $parentCommentContent = '';
  397. $parentCommentUid = 0;
  398. $parentCommentTime = '';
  399. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  400. $comment = $this->postComment->find($request['parent_id']);
  401. if (!$comment) {
  402. return Response::create([
  403. 'message' => '获取评论信息失败',
  404. 'status_code' => 500
  405. ]);
  406. }
  407. if ($comment->parent_id) {
  408. return Response::create([
  409. 'message' => '只能回复评论',
  410. 'status_code' => 500
  411. ]);
  412. }
  413. $data['parent_id'] = $request['parent_id'];
  414. $data['reply_uid'] = $comment->uid;
  415. $data['reply_username'] = $comment->username;
  416. $parentCommentContent = $comment->content;
  417. $parentCommentUid = $comment->uid;
  418. $parentCommentTime = Carbon::parse($comment->created_at)->toDateTimeString();
  419. }
  420. DB::beginTransaction();
  421. try {
  422. $newComment = $this->postComment->create($data);
  423. if($newComment->parent_id){
  424. $this->postComment->where('id', $newComment->parent_id)->increment('reply_count');
  425. }
  426. DB::commit();
  427. if($newComment->parent_id){
  428. Redis::DEL('post_new_reply_'.$newComment->parent_id);
  429. }else{
  430. Redis::DEL('post_new_comment_'.$newComment->post_id);
  431. }
  432. $virus = $this->behavior->where('behavior_identification', 'comment')->first();
  433. if ($virus) {
  434. if ($post->title) {
  435. $desc = $post->title;
  436. } else {
  437. $desc = subtext(strip_tags($post->content), 20);
  438. }
  439. $this->rabbitMqUtil->push('virus_add', [
  440. 'behavior_id' => $virus->virus_behavior_id,
  441. 'behavior_flag' => 'comment',
  442. 'post_id' => $post->id,
  443. 'post_type' => $post->type,
  444. 'post_author_uid' => $post->uid,
  445. 'post_desc' => $desc,
  446. 'post_cover' => $post->img,
  447. 'comment_id' => $newComment->id,
  448. 'comment_content' => $newComment->content,
  449. 'parent_comment_id' => $newComment->parent_id,
  450. 'parent_comment_content' => $parentCommentContent,
  451. 'parent_comment_uid' => $parentCommentUid,
  452. 'parent_comment_time' => $parentCommentTime,
  453. 'reply_uid' => $newComment->reply_uid,
  454. 'reply_username' => $newComment->reply_username,
  455. 'target_id' => $newComment->uid,
  456. 'action_id' => $newComment->id,
  457. ]);
  458. }
  459. return Response::create();
  460. } catch (QueryException $exception) {
  461. DB::rollBack();
  462. Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
  463. return Response::create([
  464. 'message' => '评论失败,请重试',
  465. 'error' => $exception->getMessage(),
  466. 'status_code' => 500
  467. ]);
  468. }
  469. }
  470. /**
  471. * 内容列表
  472. */
  473. public function lists($request)
  474. {
  475. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  476. $where = [];
  477. if (isset($request['is_suggest'])) {
  478. $where[] = ['is_suggest', $request['is_suggest']];
  479. }
  480. if (isset($request['type'])) {
  481. $where[] = ['type', $request['type']];
  482. }
  483. if (isset($request['uid'])) {
  484. $where[] = ['uid', $request['uid']];
  485. }
  486. $sort = 'post.id';
  487. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  488. $sort = $request['sort'];
  489. }
  490. $post = $this->post;
  491. if (isset($request['waste']) && $request['waste'] == 1) {
  492. $post = $post->onlyTrashed();
  493. }
  494. return $post
  495. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  496. ->with('data')
  497. ->select('post.*')
  498. ->where($where)
  499. ->where(function ($query) use ($request) {
  500. if (isset($request['keyword'])) {
  501. $query->where('uid', '=', $request['keyword'])
  502. ->orWhere('username', 'like', "%{$request['keyword']}%")
  503. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  504. }
  505. })
  506. ->where(function ($query) use ($request) {
  507. if (isset($request['content'])) {
  508. $query->where('title', 'like', "%{$request['content']}%")
  509. ->orWhere('content', 'like', "%{$request['content']}%");
  510. }
  511. })
  512. ->where(function ($query) use ($request) {
  513. if (isset($request['created_at'])) {
  514. $time = explode('_', $request['created_at']);
  515. $query->whereBetween('post.created_at', $time);
  516. }
  517. })
  518. ->where(function ($query) use ($request) {
  519. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  520. $ids = [];
  521. if (isset($request['category_ids'])) {
  522. $categoryIds = explode('_', $request['category_ids']);
  523. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  524. }
  525. if (isset($request['topic_ids'])) {
  526. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  527. }
  528. foreach ($ids as $key => $id) {
  529. if ($key == 0) {
  530. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  531. } else {
  532. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  533. }
  534. }
  535. }
  536. })
  537. ->orderBy($sort, 'desc')
  538. ->paginate($perPage);
  539. }
  540. /**
  541. * 内容详情
  542. */
  543. public function detail($request)
  544. {
  545. return $this->post->withTrashed()->find($request['id']);
  546. }
  547. /**
  548. * 内容类型
  549. */
  550. public function getType($request)
  551. {
  552. $type = $this->post->where('id', $request['id'])->value('type');
  553. return Response::create([
  554. 'message' => '成功',
  555. 'status_code' => 200,
  556. 'data' => $type
  557. ]);
  558. }
  559. /**
  560. * 评论列表
  561. */
  562. public function commentList($request)
  563. {
  564. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  565. $where = [];
  566. if (isset($request['post_id'])) {
  567. $where[] = ['post_id', $request['post_id']];
  568. }
  569. if (isset($request['uid'])) {
  570. $where[] = ['uid', $request['uid']];
  571. }
  572. return $this->postComment
  573. ->where($where)
  574. ->orderBy('id', 'desc')
  575. ->paginate($perPage);
  576. }
  577. /**
  578. * 推荐内容
  579. */
  580. public function suggest($request)
  581. {
  582. $post = $this->post->where('id', $request['id'])->first();
  583. if (!$post) {
  584. return Response::create([
  585. 'message' => '获取内容信息失败',
  586. 'status_code' => 500
  587. ]);
  588. }
  589. if ($post->is_suggest == 1) {
  590. $post->is_suggest = 0;
  591. } else {
  592. $post->is_suggest = 1;
  593. }
  594. DB::beginTransaction();
  595. try {
  596. $post->save();
  597. DB::commit();
  598. return Response::create();
  599. } catch (QueryException $exception) {
  600. DB::rollBack();
  601. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  602. return Response::create([
  603. 'message' => '操作失败,请重试',
  604. 'error' => $exception->getMessage(),
  605. 'status_code' => 500
  606. ]);
  607. }
  608. }
  609. /**
  610. * 删除内容
  611. */
  612. public function delete($request)
  613. {
  614. $token = JWTAuth::decode(JWTAuth::getToken());
  615. if($token['type'] != 1){
  616. return Response::create([
  617. 'message' => '只有运营才能删除内容',
  618. 'status_code' => 500
  619. ]);
  620. }
  621. $post = $this->post->where('id', $request['id'])->first();
  622. if (!$post) {
  623. return Response::create([
  624. 'message' => '获取内容信息失败',
  625. 'status_code' => 500
  626. ]);
  627. }
  628. $uid = $post->uid;
  629. $title = $post->title;
  630. if(!$title){
  631. $title = subtext(strip_tags($post->content), 20);
  632. }
  633. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
  634. $logData = [
  635. 'uid' => $token['user']->id,
  636. 'operator_type' => 'admin',
  637. 'post_id' => $request['id'],
  638. 'username' => $token['user']->username,
  639. 'log_type' => 'delete',
  640. 'content' => json_encode(['delete' => $request['id']]),
  641. ];
  642. $date = Carbon::now()->toDateTimeString();
  643. DB::beginTransaction();
  644. try {
  645. $post->delete();
  646. $this->postLog->create($logData);
  647. DB::commit();
  648. Redis::SADD('delete_post_ids', $request['id']);
  649. $this->rabbitMqUtil->push('add_message_one', [
  650. 'uid' => $uid,
  651. 'message_rule_id' => 0,
  652. 'message_type' => 1,
  653. 'message_show_type' => 'post_delete',
  654. 'param' => [
  655. 'title' => '内容删除',
  656. 'content' => $content,
  657. 'cover' => '',
  658. 'activity_url' => 0,
  659. 'activity_time' => '',
  660. ],
  661. 'is_read' => 0,
  662. 'created_at' => $date,
  663. 'updated_at' => $date,
  664. ]);
  665. return Response::create();
  666. } catch (QueryException $exception) {
  667. DB::rollBack();
  668. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  669. return Response::create([
  670. 'message' => '操作失败,请重试',
  671. 'error' => $exception->getMessage(),
  672. 'status_code' => 500
  673. ]);
  674. }
  675. }
  676. /**
  677. * 复原内容
  678. */
  679. public function restore($request)
  680. {
  681. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  682. if (!$post) {
  683. return Response::create([
  684. 'message' => '获取内容信息失败',
  685. 'status_code' => 500
  686. ]);
  687. }
  688. DB::beginTransaction();
  689. try {
  690. $post->restore();
  691. DB::commit();
  692. Redis::SREM('delete_post_ids', $request['id']);
  693. return Response::create();
  694. } catch (QueryException $exception) {
  695. DB::rollBack();
  696. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  697. return Response::create([
  698. 'message' => '操作失败,请重试',
  699. 'error' => $exception->getMessage(),
  700. 'status_code' => 500
  701. ]);
  702. }
  703. }
  704. /**
  705. * 删除评论
  706. */
  707. public function commentDelete($request)
  708. {
  709. $comment = $this->postComment->find($request['id']);
  710. if (!$comment) {
  711. return Response::create([
  712. 'message' => '获取评论信息失败',
  713. 'status_code' => 500
  714. ]);
  715. }
  716. if ($comment->is_delete == 1) {
  717. return Response::create([
  718. 'message' => '该评论已经删除',
  719. 'status_code' => 500
  720. ]);
  721. }
  722. DB::beginTransaction();
  723. try {
  724. $comment->is_delete = 1;
  725. $comment->save();
  726. DB::commit();
  727. Redis::SADD('delete_post_comment_ids', $comment->id);
  728. if(!$comment->parent_id){
  729. Redis::DEL('post_new_comment_'.$comment->post_id);
  730. }else{
  731. Redis::DEL('post_new_reply_'.$comment->id);
  732. }
  733. return Response::create();
  734. } catch (QueryException $exception) {
  735. DB::rollBack();
  736. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  737. return Response::create([
  738. 'message' => '操作失败,请重试',
  739. 'error' => $exception->getMessage(),
  740. 'status_code' => 500
  741. ]);
  742. }
  743. }
  744. /**
  745. * 隐藏内容
  746. */
  747. public function hide($request)
  748. {
  749. $post = $this->post->where('id', $request['id'])->first();
  750. if (!$post) {
  751. return Response::create([
  752. 'message' => '获取内容信息失败',
  753. 'status_code' => 500
  754. ]);
  755. }
  756. if ($post->is_hide == 1) {
  757. $post->is_hide = 0;
  758. } else {
  759. $post->is_hide = 1;
  760. }
  761. DB::beginTransaction();
  762. try {
  763. $post->save();
  764. DB::commit();
  765. return Response::create();
  766. } catch (QueryException $exception) {
  767. DB::rollBack();
  768. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  769. return Response::create([
  770. 'message' => '操作失败,请重试',
  771. 'error' => $exception->getMessage(),
  772. 'status_code' => 500
  773. ]);
  774. }
  775. }
  776. /**
  777. * 日志列表
  778. */
  779. public function log($request)
  780. {
  781. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  782. $where = [];
  783. if (isset($request['log_type'])) {
  784. $where[] = ['log_type', $request['log_type']];
  785. }
  786. return $this->postLog
  787. ->where($where)
  788. ->where(function ($query) use ($request) {
  789. if (isset($request['created_at'])) {
  790. $time = explode('_', $request['created_at']);
  791. $query->whereBetween('created_at', $time);
  792. }
  793. })
  794. ->orderBy('id', 'desc')
  795. ->paginate($perPage);
  796. }
  797. public function download($filePath, $type, $request)
  798. {
  799. try {
  800. set_time_limit(0);
  801. if (!ini_get("auto_detect_line_endings")) {
  802. ini_set("auto_detect_line_endings", '1');
  803. }
  804. // 文件路径
  805. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  806. // 设置标题
  807. if ($type == 'post') {
  808. $title = [
  809. '内容', date('Y年m月d日')
  810. ];
  811. } else {
  812. $title = [
  813. '回收站内容', date('Y年m月d日')
  814. ];
  815. }
  816. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  817. $writer->insertone($title);
  818. // 内容
  819. if ($type == 'post') {
  820. $header = [
  821. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  822. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  823. '真实收藏数', '总收藏数', '评论数'
  824. ];
  825. } else {
  826. $header = [
  827. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  828. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  829. ];
  830. }
  831. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  832. // $writer->setOutputBOM(Reader::BOM_UTF8);
  833. $writer->insertone($header);
  834. $where = [];
  835. if (isset($request['content'])) {
  836. $where[] = ['content', 'like', "%{$request['content']}%"];
  837. }
  838. if (isset($request['is_suggest'])) {
  839. $where[] = ['is_suggest', $request['is_suggest']];
  840. }
  841. if (isset($request['type'])) {
  842. $where[] = ['type', $request['type']];
  843. }
  844. $sort = 'post.id';
  845. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  846. $sort = $request['sort'];
  847. }
  848. $post = $this->post;
  849. if ($type == 'post_waste') {
  850. $post = $post->onlyTrashed();
  851. }
  852. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  853. ->select('post.*')
  854. ->where($where)
  855. ->where(function ($query) use ($request) {
  856. if (isset($request['keyword'])) {
  857. $query->where('uid', '=', $request['keyword'])
  858. ->orWhere('username', 'like', "%{$request['keyword']}%")
  859. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  860. }
  861. })
  862. ->where(function ($query) use ($request) {
  863. if (isset($request['created_at'])) {
  864. $time = explode('_', $request['created_at']);
  865. $query->whereBetween('post.created_at', $time);
  866. }
  867. })
  868. ->where(function ($query) use ($request) {
  869. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  870. $ids = [];
  871. if (isset($request['category_ids'])) {
  872. $categoryIds = explode('_', $request['category_ids']);
  873. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  874. }
  875. if (isset($request['topic_ids'])) {
  876. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  877. }
  878. Log::debug('话题ids:' . json_encode($ids));
  879. foreach ($ids as $key => $id) {
  880. if ($key == 0) {
  881. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  882. } else {
  883. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  884. }
  885. }
  886. }
  887. })
  888. ->orderBy($sort, 'desc')
  889. ->chunk(1, function ($posts) use ($writer, $type) {
  890. $data = [];
  891. foreach ($posts as $post) {
  892. if ($type == 'post') {
  893. $tmp = [
  894. $post->id,
  895. Carbon::parse($post->created_at)->toDateTimeString(),
  896. $post->username,
  897. $post->location,
  898. implode(' ', $this->getTopic($post->topic_ids)),
  899. subtext(strip_tags($post->content), 20),
  900. $post->data->pv_real,
  901. $post->data->pv,
  902. $post->data->praise_real_count,
  903. $post->data->praise_count,
  904. $post->data->share_real_count,
  905. $post->data->share_count,
  906. $post->data->collect_real_count,
  907. $post->data->collect_count,
  908. $post->data->comment_count
  909. ];
  910. } else {
  911. $tmp = [
  912. $post->id,
  913. Carbon::parse($post->created_at)->toDateTimeString(),
  914. $post->username,
  915. Carbon::parse($post->created_at)->toDateTimeString(),
  916. subtext(strip_tags($post->content), 20),
  917. $post->data->pv_real,
  918. $post->data->praise_real_count,
  919. $post->data->share_real_count,
  920. $post->data->collect_real_count,
  921. $post->data->comment_count
  922. ];
  923. }
  924. foreach ($tmp as $key => $value) {
  925. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  926. }
  927. $data[] = $tmp;
  928. }
  929. $writer->insertAll($data);
  930. });
  931. Log::info('内容导出成功!');
  932. } catch (QueryException $e) {
  933. Log::debug('内容导出失败!'.$e->getMessage());
  934. }
  935. }
  936. /**
  937. * 统计社区内容
  938. * @param $start
  939. * @param $end
  940. * @return array
  941. */
  942. public function statistics($start, $end)
  943. {
  944. $result = $this->postStatistics
  945. ->where('created_at', '>=', $start)
  946. ->where('created_at', '<=', $end)
  947. ->get()->toArray();
  948. $stimestamp = strtotime($start);
  949. $etimestamp = strtotime($end);
  950. $days = ($etimestamp - $stimestamp) / 86400;
  951. $date = array();
  952. for ($i = 0; $i < $days; $i++) {
  953. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  954. }
  955. $totalRead = 0;
  956. $totalPost = 0;
  957. $totalShare = 0;
  958. $totalLike = 0;
  959. $totalCollect = 0;
  960. $totalComment = 0;
  961. $info = [];
  962. foreach ($date as $key => $value) {
  963. $info[$value] = [
  964. 'read' => 0,
  965. 'post' => 0,
  966. 'share' => 0,
  967. 'like' => 0,
  968. 'collect' => 0,
  969. 'comment' => 0,
  970. ];
  971. foreach ($result as $row) {
  972. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  973. $info[$value]['read'] = $row['read_count'];
  974. $info[$value]['post'] = $row['post_count'];
  975. $info[$value]['share'] = $row['share_count'];
  976. $info[$value]['like'] = $row['like_count'];
  977. $info[$value]['collect'] = $row['collect_count'];
  978. $info[$value]['comment'] = $row['comment_count'];
  979. $totalRead += $row['read_count'];
  980. $totalPost += $row['post_count'];
  981. $totalShare += $row['share_count'];
  982. $totalLike += $row['like_count'];
  983. $totalCollect += $row['collect_count'];
  984. $totalComment += $row['comment_count'];
  985. }
  986. }
  987. }
  988. $info['data']['total_read'] = $totalRead;
  989. $info['data']['total_post'] = $totalPost;
  990. $info['data']['total_share'] = $totalShare;
  991. $info['data']['total_like'] = $totalLike;
  992. $info['data']['total_collect'] = $totalCollect;
  993. $info['data']['total_comment'] = $totalComment;
  994. return $info;
  995. }
  996. /**
  997. * 编辑内容话题
  998. */
  999. public function updateTopic($request)
  1000. {
  1001. $token = JWTAuth::decode(JWTAuth::getToken());
  1002. if($token['type'] != 1){
  1003. return Response::create([
  1004. 'message' => '只有运营才能删除内容',
  1005. 'status_code' => 500
  1006. ]);
  1007. }
  1008. $post = $this->post->where('id', $request['id'])->first();
  1009. if (!$post) {
  1010. return Response::create([
  1011. 'message' => '获取内容信息失败',
  1012. 'status_code' => 500
  1013. ]);
  1014. }
  1015. //验证话题
  1016. $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  1017. $topicCount = count($topicIdsArray);
  1018. if ($topicCount == 0 || $topicCount > 5) {
  1019. return Response::create([
  1020. 'message' => '所选话题必须1-5个',
  1021. 'status_code' => 500
  1022. ]);
  1023. }
  1024. $topicIds = implode(',', $topicIdsArray);
  1025. $logData = [
  1026. 'uid' => $token['user']->id,
  1027. 'operator_type' => 'admin',
  1028. 'post_id' => $request['id'],
  1029. 'username' => $token['user']->username,
  1030. 'log_type' => 'update_topic',
  1031. 'content' => json_encode(['update_topic' => $request['id'].'-'.$topicIds]),
  1032. ];
  1033. DB::beginTransaction();
  1034. try {
  1035. $post->topic_ids = $topicIds;
  1036. $post->save();
  1037. $this->postLog->create($logData);
  1038. DB::commit();
  1039. Redis::HSET('post_info_'.$request['id'], 'topic_ids', $topicIds);
  1040. foreach ($topicIdsArray as $id) {
  1041. Redis::zincrby('topic.just_use_count', 1, $id);
  1042. }
  1043. return Response::create();
  1044. } catch (QueryException $exception) {
  1045. DB::rollBack();
  1046. Log::debug('编辑内容话题:'.$post->id . $exception->getMessage());
  1047. return Response::create([
  1048. 'message' => '编辑失败,请重试',
  1049. 'error' => $exception->getMessage(),
  1050. 'status_code' => 500
  1051. ]);
  1052. }
  1053. }
  1054. /**
  1055. * 添加网站内容
  1056. */
  1057. public function createStore($startPage, $endPage, $size)
  1058. {
  1059. set_time_limit(1800);
  1060. for($i=$startPage;$i<=$endPage;$i++){
  1061. $this->addContent($i, $size);
  1062. usleep(100000);
  1063. }
  1064. }
  1065. /**
  1066. * 获取网站内容
  1067. */
  1068. public function addContent($page, $size)
  1069. {
  1070. $lists = $this->getAcFunList($page, $size);
  1071. if(empty($lists)){
  1072. return Response::create([
  1073. 'message' => '获取数据失败',
  1074. 'status_code' => 500
  1075. ]);
  1076. }
  1077. $data = [];
  1078. foreach($lists as $item){
  1079. $res = $this->getData($item['id'], $item['title']);
  1080. if($res && $res['content'] && $res['images']){
  1081. $data[] = array_merge(['source_id' => $item['id']], $res);
  1082. }
  1083. }
  1084. if(empty($data)){
  1085. return Response::create([
  1086. 'message' => '处理数据失败',
  1087. 'status_code' => 500
  1088. ]);
  1089. }
  1090. $date = Carbon::now()->toDateTimeString();
  1091. $successNo = 0;
  1092. $failNo = 0;
  1093. DB::beginTransaction();
  1094. try {
  1095. foreach($data as $item){
  1096. if($this->postStore->where('source', 'AcFun')->where('source_id', $item['source_id'])->exists()){
  1097. $failNo++;
  1098. continue;
  1099. }
  1100. $storeData = [
  1101. 'source' => 'Acfun',
  1102. 'source_id' => $item['source_id'],
  1103. 'title' => $item['title'],
  1104. 'content' => $item['content'],
  1105. 'img' => $item['images'][0],
  1106. ];
  1107. $postStore = $this->postStore->create($storeData);
  1108. $storeImgsData = [];
  1109. foreach($item['images'] as $img){
  1110. $storeImgsData[] = [
  1111. 'post_store_id' => $postStore->id,
  1112. 'img' => $img,
  1113. 'created_at' => $date,
  1114. 'updated_at' => $date,
  1115. ];
  1116. }
  1117. $this->postStoreImgs->insert($storeImgsData);
  1118. $successNo++;
  1119. }
  1120. DB::commit();
  1121. Log::info('第'.$page.'页添加内容数据成功'.$successNo.',失败'.$failNo);
  1122. return Response::create();
  1123. } catch (QueryException $exception) {
  1124. DB::rollBack();
  1125. Log::debug('添加内容数据失败:' . $exception->getMessage());
  1126. return Response::create([
  1127. 'message' => '添加内容数据失败',
  1128. 'error' => $exception->getMessage(),
  1129. 'status_code' => 500
  1130. ]);
  1131. }
  1132. }
  1133. /**
  1134. * 获取网站内容
  1135. */
  1136. public function getData($id, $title = '')
  1137. {
  1138. $url = 'https://www.acfun.cn/a/ac'.$id;
  1139. $webpage = file_get_contents($url);
  1140. preg_match_all('/parts[\s\S]*?}]/i', $webpage, $res);
  1141. if(!isset($res[0][0])){
  1142. return [];
  1143. }
  1144. $data = $res[0][0];
  1145. preg_match_all('/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i ', $data, $contents);
  1146. if(isset($contents[0])){
  1147. $content = implode($contents[0]);
  1148. }else{
  1149. return [];
  1150. }
  1151. $content = strip_tags($content);
  1152. $content = str_replace(['&nbsp;'],'',$content);
  1153. preg_match_all('/src=(.*)>/U', $data, $imageArray);
  1154. $images = [];
  1155. if(isset($imageArray[0])){
  1156. foreach($imageArray[0] as $img){
  1157. $imgArr = explode('"',$img);
  1158. if(isset($imgArr[1])){
  1159. $imgArr[1] = trim($imgArr[1], '\\');
  1160. $images[] = $imgArr[1];
  1161. }else{
  1162. return [];
  1163. }
  1164. }
  1165. }
  1166. return [
  1167. 'title' => $title,
  1168. 'content' => $content,
  1169. 'images' => $images,
  1170. ];
  1171. }
  1172. /**
  1173. * 获取网站内容
  1174. */
  1175. public function getStore($request)
  1176. {
  1177. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  1178. $where = [];
  1179. if (isset($request['title'])) {
  1180. $where[] = ['title', 'like', "%{$request['title']}%"];
  1181. }
  1182. if (isset($request['content'])) {
  1183. $where[] = ['content', 'like', "%{$request['content']}%"];
  1184. }
  1185. if (isset($request['source'])) {
  1186. $where[] = ['source', $request['source']];
  1187. }
  1188. if (isset($request['is_used'])) {
  1189. $where[] = ['is_used', $request['is_used']];
  1190. }
  1191. return $this->postStore
  1192. ->where($where)
  1193. ->orderBy('id', 'desc')
  1194. ->paginate($perPage);
  1195. }
  1196. /**
  1197. * 网站内容详情
  1198. */
  1199. public function getStoreDetail($request)
  1200. {
  1201. return $this->postStore->find($request['id']);
  1202. }
  1203. }