PostRepository.php 37 KB

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