PostRepository.php 35 KB

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