PostRepository.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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['text'] = '你的内容《'.$title.'》被小编推荐全平台优先展示喽,获得推荐位后你的内容会被更多人看到。记得多来和大家互动赚取更多U米~';
  608. Redis::LPUSH('send_push_msg_queue',json_encode($push));
  609. $rabbitmq = new RabbitMqUtil();
  610. $rabbitmq->push('add_message_one', [
  611. 'uid' => $post->uid,
  612. 'message_rule_id' => 0,
  613. 'message_type' => 1,
  614. 'message_show_type' => 'system_common',
  615. 'param' => [
  616. 'title' => $push['title'],
  617. 'content' => $push['text'],
  618. 'cover' => '',
  619. 'activity_url' => 0,
  620. 'activity_time' => '',
  621. ],
  622. 'is_read' => 0,
  623. 'created_at' => $date,
  624. 'updated_at' => $date,
  625. ]);
  626. }
  627. return Response::create();
  628. } catch (QueryException $exception) {
  629. DB::rollBack();
  630. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  631. return Response::create([
  632. 'message' => '操作失败,请重试',
  633. 'error' => $exception->getMessage(),
  634. 'status_code' => 500
  635. ]);
  636. }
  637. }
  638. /**
  639. * 删除内容
  640. */
  641. public function delete($request)
  642. {
  643. $token = JWTAuth::decode(JWTAuth::getToken());
  644. if ($token['type'] != 1) {
  645. return Response::create([
  646. 'message' => '只有运营才能删除内容',
  647. 'status_code' => 500
  648. ]);
  649. }
  650. $post = $this->post->where('id', $request['id'])->first();
  651. if (!$post) {
  652. return Response::create([
  653. 'message' => '获取内容信息失败',
  654. 'status_code' => 500
  655. ]);
  656. }
  657. $uid = $post->uid;
  658. $title = $post->title;
  659. if (!$title) {
  660. $title = subtext(strip_tags($post->content), 20);
  661. }
  662. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
  663. $logData = [
  664. 'uid' => $token['user']->id,
  665. 'operator_type' => 'admin',
  666. 'post_id' => $request['id'],
  667. 'username' => $token['user']->username,
  668. 'log_type' => 'delete',
  669. 'content' => json_encode(['delete' => $request['id']]),
  670. ];
  671. $date = Carbon::now()->toDateTimeString();
  672. DB::beginTransaction();
  673. try {
  674. $post->delete();
  675. $this->postLog->create($logData);
  676. DB::commit();
  677. Redis::SADD('delete_post_ids', $request['id']);
  678. $this->rabbitMqUtil->push('add_message_one', [
  679. 'uid' => $uid,
  680. 'message_rule_id' => 0,
  681. 'message_type' => 1,
  682. 'message_show_type' => 'system_common',
  683. 'param' => [
  684. 'title' => '内容删除',
  685. 'content' => $content,
  686. 'cover' => '',
  687. 'activity_url' => 0,
  688. 'activity_time' => '',
  689. ],
  690. 'is_read' => 0,
  691. 'created_at' => $date,
  692. 'updated_at' => $date,
  693. ]);
  694. return Response::create();
  695. } catch (QueryException $exception) {
  696. DB::rollBack();
  697. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  698. return Response::create([
  699. 'message' => '操作失败,请重试',
  700. 'error' => $exception->getMessage(),
  701. 'status_code' => 500
  702. ]);
  703. }
  704. }
  705. /**
  706. * 复原内容
  707. */
  708. public function restore($request)
  709. {
  710. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  711. if (!$post) {
  712. return Response::create([
  713. 'message' => '获取内容信息失败',
  714. 'status_code' => 500
  715. ]);
  716. }
  717. DB::beginTransaction();
  718. try {
  719. $post->restore();
  720. DB::commit();
  721. Redis::SREM('delete_post_ids', $request['id']);
  722. return Response::create();
  723. } catch (QueryException $exception) {
  724. DB::rollBack();
  725. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  726. return Response::create([
  727. 'message' => '操作失败,请重试',
  728. 'error' => $exception->getMessage(),
  729. 'status_code' => 500
  730. ]);
  731. }
  732. }
  733. /**
  734. * 删除评论
  735. */
  736. public function commentDelete($request)
  737. {
  738. $comment = $this->postComment->find($request['id']);
  739. if (!$comment) {
  740. return Response::create([
  741. 'message' => '获取评论信息失败',
  742. 'status_code' => 500
  743. ]);
  744. }
  745. if ($comment->is_delete == 1) {
  746. return Response::create([
  747. 'message' => '该评论已经删除',
  748. 'status_code' => 500
  749. ]);
  750. }
  751. DB::beginTransaction();
  752. try {
  753. $comment->is_delete = 1;
  754. $comment->save();
  755. DB::commit();
  756. Redis::SADD('delete_post_comment_ids', $comment->id);
  757. if (!$comment->parent_id) {
  758. Redis::DEL('post_new_comment_' . $comment->post_id);
  759. } else {
  760. Redis::DEL('post_new_reply_' . $comment->id);
  761. }
  762. return Response::create();
  763. } catch (QueryException $exception) {
  764. DB::rollBack();
  765. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  766. return Response::create([
  767. 'message' => '操作失败,请重试',
  768. 'error' => $exception->getMessage(),
  769. 'status_code' => 500
  770. ]);
  771. }
  772. }
  773. /**
  774. * 隐藏内容
  775. */
  776. public function hide($request)
  777. {
  778. $post = $this->post->where('id', $request['id'])->first();
  779. if (!$post) {
  780. return Response::create([
  781. 'message' => '获取内容信息失败',
  782. 'status_code' => 500
  783. ]);
  784. }
  785. if ($post->is_hide == 1) {
  786. $post->is_hide = 0;
  787. } else {
  788. $post->is_hide = 1;
  789. }
  790. DB::beginTransaction();
  791. try {
  792. $post->save();
  793. DB::commit();
  794. return Response::create();
  795. } catch (QueryException $exception) {
  796. DB::rollBack();
  797. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  798. return Response::create([
  799. 'message' => '操作失败,请重试',
  800. 'error' => $exception->getMessage(),
  801. 'status_code' => 500
  802. ]);
  803. }
  804. }
  805. /**
  806. * 日志列表
  807. */
  808. public function log($request)
  809. {
  810. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  811. $where = [];
  812. if (isset($request['log_type'])) {
  813. $where[] = ['log_type', $request['log_type']];
  814. }
  815. return $this->postLog
  816. ->where($where)
  817. ->where(function ($query) use ($request) {
  818. if (isset($request['created_at'])) {
  819. $time = explode('_', $request['created_at']);
  820. $query->whereBetween('created_at', $time);
  821. }
  822. })
  823. ->orderBy('id', 'desc')
  824. ->paginate($perPage);
  825. }
  826. public function download($filePath, $type, $request)
  827. {
  828. try {
  829. set_time_limit(0);
  830. if (!ini_get("auto_detect_line_endings")) {
  831. ini_set("auto_detect_line_endings", '1');
  832. }
  833. // 文件路径
  834. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  835. // 设置标题
  836. if ($type == 'post') {
  837. $title = [
  838. '内容', date('Y年m月d日')
  839. ];
  840. } else {
  841. $title = [
  842. '回收站内容', date('Y年m月d日')
  843. ];
  844. }
  845. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  846. $writer->insertone($title);
  847. // 内容
  848. if ($type == 'post') {
  849. $header = [
  850. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  851. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  852. '真实收藏数', '总收藏数', '评论数'
  853. ];
  854. } else {
  855. $header = [
  856. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  857. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  858. ];
  859. }
  860. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  861. // $writer->setOutputBOM(Reader::BOM_UTF8);
  862. $writer->insertone($header);
  863. $where = [];
  864. if (isset($request['content'])) {
  865. $where[] = ['content', 'like', "%{$request['content']}%"];
  866. }
  867. if (isset($request['is_suggest'])) {
  868. $where[] = ['is_suggest', $request['is_suggest']];
  869. }
  870. if (isset($request['type'])) {
  871. $where[] = ['type', $request['type']];
  872. }
  873. $sort = 'post.id';
  874. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  875. $sort = $request['sort'];
  876. }
  877. $post = $this->post;
  878. if ($type == 'post_waste') {
  879. $post = $post->onlyTrashed();
  880. }
  881. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  882. ->select('post.*')
  883. ->where($where)
  884. ->where(function ($query) use ($request) {
  885. if (isset($request['keyword'])) {
  886. $query->where('uid', '=', $request['keyword'])
  887. ->orWhere('username', 'like', "%{$request['keyword']}%")
  888. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  889. }
  890. })
  891. ->where(function ($query) use ($request) {
  892. if (isset($request['created_at'])) {
  893. $time = explode('_', $request['created_at']);
  894. $query->whereBetween('post.created_at', $time);
  895. }
  896. })
  897. ->where(function ($query) use ($request) {
  898. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  899. $ids = [];
  900. if (isset($request['category_ids'])) {
  901. $categoryIds = explode('_', $request['category_ids']);
  902. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  903. }
  904. if (isset($request['topic_ids'])) {
  905. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  906. }
  907. Log::debug('话题ids:' . json_encode($ids));
  908. foreach ($ids as $key => $id) {
  909. if ($key == 0) {
  910. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  911. } else {
  912. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  913. }
  914. }
  915. }
  916. })
  917. ->orderBy($sort, 'desc')
  918. ->chunk(1, function ($posts) use ($writer, $type) {
  919. $data = [];
  920. foreach ($posts as $post) {
  921. if ($type == 'post') {
  922. $tmp = [
  923. $post->id,
  924. Carbon::parse($post->created_at)->toDateTimeString(),
  925. $post->username,
  926. $post->location,
  927. implode(' ', $this->getTopic($post->topic_ids)),
  928. subtext(strip_tags($post->content), 20),
  929. $post->data->pv_real,
  930. $post->data->pv,
  931. $post->data->praise_real_count,
  932. $post->data->praise_count,
  933. $post->data->share_real_count,
  934. $post->data->share_count,
  935. $post->data->collect_real_count,
  936. $post->data->collect_count,
  937. $post->data->comment_count
  938. ];
  939. } else {
  940. $tmp = [
  941. $post->id,
  942. Carbon::parse($post->created_at)->toDateTimeString(),
  943. $post->username,
  944. Carbon::parse($post->created_at)->toDateTimeString(),
  945. subtext(strip_tags($post->content), 20),
  946. $post->data->pv_real,
  947. $post->data->praise_real_count,
  948. $post->data->share_real_count,
  949. $post->data->collect_real_count,
  950. $post->data->comment_count
  951. ];
  952. }
  953. foreach ($tmp as $key => $value) {
  954. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  955. }
  956. $data[] = $tmp;
  957. }
  958. $writer->insertAll($data);
  959. });
  960. Log::info('内容导出成功!');
  961. } catch (QueryException $e) {
  962. Log::debug('内容导出失败!' . $e->getMessage());
  963. }
  964. }
  965. /**
  966. * 统计社区内容
  967. * @param $start
  968. * @param $end
  969. * @return array
  970. */
  971. public function statistics($start, $end)
  972. {
  973. $result = $this->postStatistics
  974. ->where('created_at', '>=', $start)
  975. ->where('created_at', '<=', $end)
  976. ->get()->toArray();
  977. $stimestamp = strtotime($start);
  978. $etimestamp = strtotime($end);
  979. $days = ($etimestamp - $stimestamp) / 86400;
  980. $date = array();
  981. for ($i = 0; $i < $days; $i++) {
  982. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  983. }
  984. $totalRead = 0;
  985. $totalPost = 0;
  986. $totalShare = 0;
  987. $totalLike = 0;
  988. $totalCollect = 0;
  989. $totalComment = 0;
  990. $info = [];
  991. foreach ($date as $key => $value) {
  992. $info[$value] = [
  993. 'read' => 0,
  994. 'post' => 0,
  995. 'share' => 0,
  996. 'like' => 0,
  997. 'collect' => 0,
  998. 'comment' => 0,
  999. ];
  1000. foreach ($result as $row) {
  1001. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  1002. $info[$value]['read'] = $row['read_count'];
  1003. $info[$value]['post'] = $row['post_count'];
  1004. $info[$value]['share'] = $row['share_count'];
  1005. $info[$value]['like'] = $row['like_count'];
  1006. $info[$value]['collect'] = $row['collect_count'];
  1007. $info[$value]['comment'] = $row['comment_count'];
  1008. $totalRead += $row['read_count'];
  1009. $totalPost += $row['post_count'];
  1010. $totalShare += $row['share_count'];
  1011. $totalLike += $row['like_count'];
  1012. $totalCollect += $row['collect_count'];
  1013. $totalComment += $row['comment_count'];
  1014. }
  1015. }
  1016. }
  1017. $info['data']['total_read'] = $totalRead;
  1018. $info['data']['total_post'] = $totalPost;
  1019. $info['data']['total_share'] = $totalShare;
  1020. $info['data']['total_like'] = $totalLike;
  1021. $info['data']['total_collect'] = $totalCollect;
  1022. $info['data']['total_comment'] = $totalComment;
  1023. return $info;
  1024. }
  1025. /**
  1026. * 编辑内容话题
  1027. */
  1028. public function updateTopic($request)
  1029. {
  1030. $token = JWTAuth::decode(JWTAuth::getToken());
  1031. if ($token['type'] != 1) {
  1032. return Response::create([
  1033. 'message' => '只有运营才能删除内容',
  1034. 'status_code' => 500
  1035. ]);
  1036. }
  1037. $post = $this->post->where('id', $request['id'])->first();
  1038. if (!$post) {
  1039. return Response::create([
  1040. 'message' => '获取内容信息失败',
  1041. 'status_code' => 500
  1042. ]);
  1043. }
  1044. //验证话题
  1045. $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  1046. $topicCount = count($topicIdsArray);
  1047. if ($topicCount == 0 || $topicCount > 5) {
  1048. return Response::create([
  1049. 'message' => '所选话题必须1-5个',
  1050. 'status_code' => 500
  1051. ]);
  1052. }
  1053. $topicIds = implode(',', $topicIdsArray);
  1054. $logData = [
  1055. 'uid' => $token['user']->id,
  1056. 'operator_type' => 'admin',
  1057. 'post_id' => $request['id'],
  1058. 'username' => $token['user']->username,
  1059. 'log_type' => 'update_topic',
  1060. 'content' => json_encode(['update_topic' => $request['id'] . '-' . $topicIds]),
  1061. ];
  1062. DB::beginTransaction();
  1063. try {
  1064. $post->topic_ids = $topicIds;
  1065. $post->save();
  1066. $this->postLog->create($logData);
  1067. DB::commit();
  1068. Redis::HSET('post_info_' . $request['id'], 'topic_ids', $topicIds);
  1069. foreach ($topicIdsArray as $id) {
  1070. Redis::zincrby('topic.just_use_count', 1, $id);
  1071. }
  1072. return Response::create();
  1073. } catch (QueryException $exception) {
  1074. DB::rollBack();
  1075. Log::debug('编辑内容话题:' . $post->id . $exception->getMessage());
  1076. return Response::create([
  1077. 'message' => '编辑失败,请重试',
  1078. 'error' => $exception->getMessage(),
  1079. 'status_code' => 500
  1080. ]);
  1081. }
  1082. }
  1083. /**
  1084. * 添加网站内容
  1085. */
  1086. public function createStore($startPage, $endPage, $size, $categoryId)
  1087. {
  1088. set_time_limit(1800);
  1089. for ($i = $startPage; $i <= $endPage; $i++) {
  1090. $this->addContent($i, $size, $categoryId);
  1091. usleep(100000);
  1092. }
  1093. }
  1094. /**
  1095. * 获取网站内容
  1096. */
  1097. public function addContent($page, $size, $categoryId)
  1098. {
  1099. $lists = $this->getAcFunList($page, $size, $categoryId);
  1100. if (empty($lists)) {
  1101. return Response::create([
  1102. 'message' => '获取数据失败',
  1103. 'status_code' => 500
  1104. ]);
  1105. }
  1106. $data = [];
  1107. foreach ($lists as $item) {
  1108. $res = $this->getData($item['id'], $item['title']);
  1109. if ($res && $res['content'] && $res['images']) {
  1110. $data[] = array_merge(['source_id' => $item['id']], $res);
  1111. }
  1112. }
  1113. if (empty($data)) {
  1114. return Response::create([
  1115. 'message' => '处理数据失败',
  1116. 'status_code' => 500
  1117. ]);
  1118. }
  1119. $date = Carbon::now()->toDateTimeString();
  1120. $successNo = 0;
  1121. $failNo = 0;
  1122. DB::beginTransaction();
  1123. try {
  1124. foreach ($data as $item) {
  1125. if ($this->postStore->where('source', 'AcFun')->where('source_id', $item['source_id'])->exists()) {
  1126. $failNo++;
  1127. continue;
  1128. }
  1129. $storeData = [
  1130. 'source' => 'Acfun',
  1131. 'source_id' => $item['source_id'],
  1132. 'category_id' => $categoryId,
  1133. 'title' => $item['title'],
  1134. 'content' => $item['content'],
  1135. 'img' => $item['images'][0],
  1136. ];
  1137. $postStore = $this->postStore->create($storeData);
  1138. $storeImgsData = [];
  1139. foreach ($item['images'] as $img) {
  1140. $storeImgsData[] = [
  1141. 'post_store_id' => $postStore->id,
  1142. 'img' => $img,
  1143. 'created_at' => $date,
  1144. 'updated_at' => $date,
  1145. ];
  1146. }
  1147. $this->postStoreImgs->insert($storeImgsData);
  1148. $successNo++;
  1149. }
  1150. DB::commit();
  1151. Log::info('第' . $page . '页添加内容数据成功' . $successNo . ',失败' . $failNo);
  1152. return Response::create();
  1153. } catch (QueryException $exception) {
  1154. DB::rollBack();
  1155. Log::debug('添加内容数据失败:' . $exception->getMessage());
  1156. return Response::create([
  1157. 'message' => '添加内容数据失败',
  1158. 'error' => $exception->getMessage(),
  1159. 'status_code' => 500
  1160. ]);
  1161. }
  1162. }
  1163. /**
  1164. * 获取网站内容
  1165. */
  1166. public function getData($id, $title = '')
  1167. {
  1168. $url = 'https://www.acfun.cn/a/ac' . $id;
  1169. $webpage = file_get_contents($url);
  1170. preg_match_all('/parts[\s\S]*?}]/i', $webpage, $res);
  1171. if (!isset($res[0][0])) {
  1172. return [];
  1173. }
  1174. $data = $res[0][0];
  1175. preg_match_all('/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i ', $data, $contents);
  1176. if (isset($contents[0])) {
  1177. $content = implode($contents[0]);
  1178. } else {
  1179. return [];
  1180. }
  1181. $content = strip_tags($content);
  1182. $content = str_replace(['&nbsp;'], '', $content);
  1183. preg_match_all('/src=(.*)>/U', $data, $imageArray);
  1184. $images = [];
  1185. if (isset($imageArray[0])) {
  1186. foreach ($imageArray[0] as $img) {
  1187. $imgArr = explode('"', $img);
  1188. if (isset($imgArr[1])) {
  1189. $imgArr[1] = trim($imgArr[1], '\\');
  1190. $images[] = $imgArr[1];
  1191. } else {
  1192. return [];
  1193. }
  1194. }
  1195. }
  1196. return [
  1197. 'title' => $title,
  1198. 'content' => $content,
  1199. 'images' => $images,
  1200. ];
  1201. }
  1202. /**
  1203. * 获取网站内容
  1204. */
  1205. public function getStore($request)
  1206. {
  1207. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  1208. $where = [];
  1209. if (isset($request['type'])) {
  1210. $where[] = ['type', $request['type']];
  1211. }
  1212. if (isset($request['category_id'])) {
  1213. $where[] = ['category_id', $request['category_id']];
  1214. }
  1215. if (isset($request['title'])) {
  1216. $where[] = ['title', 'like', "%{$request['title']}%"];
  1217. }
  1218. if (isset($request['content'])) {
  1219. $where[] = ['content', 'like', "%{$request['content']}%"];
  1220. }
  1221. if (isset($request['source'])) {
  1222. $where[] = ['source', $request['source']];
  1223. }
  1224. if (isset($request['is_used'])) {
  1225. $where[] = ['is_used', $request['is_used']];
  1226. }
  1227. return $this->postStore
  1228. ->where($where)
  1229. ->orderBy('id', 'desc')
  1230. ->paginate($perPage);
  1231. }
  1232. /**
  1233. * 网站内容详情
  1234. */
  1235. public function getStoreDetail($request)
  1236. {
  1237. return $this->postStore->find($request['id']);
  1238. }
  1239. }