PostRepository.php 35 KB

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