PostRepository.php 37 KB

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