PostRepository.php 48 KB

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