PostRepository.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. $type = $this->post->where('id', $request['id'])->value('type');
  493. return Response::create([
  494. 'message' => '成功',
  495. 'status_code' => 200,
  496. 'data' => $type
  497. ]);
  498. }
  499. /**
  500. * 评论列表
  501. */
  502. public function commentList($request)
  503. {
  504. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  505. $where = [];
  506. if (isset($request['post_id'])) {
  507. $where[] = ['post_id', $request['post_id']];
  508. }
  509. if (isset($request['uid'])) {
  510. $where[] = ['uid', $request['uid']];
  511. }
  512. return $this->postComment
  513. ->where($where)
  514. ->orderBy('id', 'desc')
  515. ->paginate($perPage);
  516. }
  517. /**
  518. * 推荐内容
  519. */
  520. public function suggest($request)
  521. {
  522. $post = $this->post->where('id', $request['id'])->first();
  523. if (!$post) {
  524. return Response::create([
  525. 'message' => '获取内容信息失败',
  526. 'status_code' => 500
  527. ]);
  528. }
  529. if ($post->is_suggest == 1) {
  530. $post->is_suggest = 0;
  531. } else {
  532. $post->is_suggest = 1;
  533. }
  534. DB::beginTransaction();
  535. try {
  536. $post->save();
  537. DB::commit();
  538. return Response::create();
  539. } catch (QueryException $exception) {
  540. DB::rollBack();
  541. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  542. return Response::create([
  543. 'message' => '操作失败,请重试',
  544. 'error' => $exception->getMessage(),
  545. 'status_code' => 500
  546. ]);
  547. }
  548. }
  549. /**
  550. * 删除内容
  551. */
  552. public function delete($request)
  553. {
  554. $post = $this->post->where('id', $request['id'])->first();
  555. if (!$post) {
  556. return Response::create([
  557. 'message' => '获取内容信息失败',
  558. 'status_code' => 500
  559. ]);
  560. }
  561. $uid = $post->uid;
  562. $title = $post->title;
  563. if(!$title){
  564. $title = subtext(strip_tags($post->content), 20);
  565. }
  566. $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系彩虹管理员";
  567. $date = Carbon::now()->toDateTimeString();
  568. DB::beginTransaction();
  569. try {
  570. $post->delete();
  571. DB::commit();
  572. $this->rabbitMqUtil->push('add_message_one', [
  573. 'uid' => $uid,
  574. 'message_rule_id' => 0,
  575. 'message_type' => 1,
  576. 'message_show_type' => 'post_delete',
  577. 'param' => [
  578. 'title' => '内容删除',
  579. 'content' => $content,
  580. 'cover' => '',
  581. 'activity_url' => 0,
  582. 'activity_time' => '',
  583. ],
  584. 'is_read' => 0,
  585. 'created_at' => $date,
  586. 'updated_at' => $date,
  587. ]);
  588. return Response::create();
  589. } catch (QueryException $exception) {
  590. DB::rollBack();
  591. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  592. return Response::create([
  593. 'message' => '操作失败,请重试',
  594. 'error' => $exception->getMessage(),
  595. 'status_code' => 500
  596. ]);
  597. }
  598. }
  599. /**
  600. * 复原内容
  601. */
  602. public function restore($request)
  603. {
  604. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  605. if (!$post) {
  606. return Response::create([
  607. 'message' => '获取内容信息失败',
  608. 'status_code' => 500
  609. ]);
  610. }
  611. DB::beginTransaction();
  612. try {
  613. $post->restore();
  614. DB::commit();
  615. return Response::create();
  616. } catch (QueryException $exception) {
  617. DB::rollBack();
  618. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  619. return Response::create([
  620. 'message' => '操作失败,请重试',
  621. 'error' => $exception->getMessage(),
  622. 'status_code' => 500
  623. ]);
  624. }
  625. }
  626. /**
  627. * 删除评论
  628. */
  629. public function commentDelete($request)
  630. {
  631. $comment = $this->postComment->find($request['id']);
  632. if (!$comment) {
  633. return Response::create([
  634. 'message' => '获取评论信息失败',
  635. 'status_code' => 500
  636. ]);
  637. }
  638. if ($comment->is_delete == 1) {
  639. return Response::create([
  640. 'message' => '该评论已经删除',
  641. 'status_code' => 500
  642. ]);
  643. }
  644. DB::beginTransaction();
  645. try {
  646. $comment->is_delete = 1;
  647. $comment->save();
  648. DB::commit();
  649. return Response::create();
  650. } catch (QueryException $exception) {
  651. DB::rollBack();
  652. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  653. return Response::create([
  654. 'message' => '操作失败,请重试',
  655. 'error' => $exception->getMessage(),
  656. 'status_code' => 500
  657. ]);
  658. }
  659. }
  660. /**
  661. * 隐藏内容
  662. */
  663. public function hide($request)
  664. {
  665. $post = $this->post->where('id', $request['id'])->first();
  666. if (!$post) {
  667. return Response::create([
  668. 'message' => '获取内容信息失败',
  669. 'status_code' => 500
  670. ]);
  671. }
  672. if ($post->is_hide == 1) {
  673. $post->is_hide = 0;
  674. } else {
  675. $post->is_hide = 1;
  676. }
  677. DB::beginTransaction();
  678. try {
  679. $post->save();
  680. DB::commit();
  681. return Response::create();
  682. } catch (QueryException $exception) {
  683. DB::rollBack();
  684. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  685. return Response::create([
  686. 'message' => '操作失败,请重试',
  687. 'error' => $exception->getMessage(),
  688. 'status_code' => 500
  689. ]);
  690. }
  691. }
  692. /**
  693. * 日志列表
  694. */
  695. public function log($request)
  696. {
  697. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  698. $where = [];
  699. if (isset($request['log_type'])) {
  700. $where[] = ['log_type', $request['log_type']];
  701. }
  702. return $this->postLog
  703. ->where($where)
  704. ->where(function ($query) use ($request) {
  705. if (isset($request['created_at'])) {
  706. $time = explode('_', $request['created_at']);
  707. $query->whereBetween('created_at', $time);
  708. }
  709. })
  710. ->orderBy('id', 'desc')
  711. ->paginate($perPage);
  712. }
  713. public function download($filePath, $type, $request)
  714. {
  715. try {
  716. set_time_limit(0);
  717. if (!ini_get("auto_detect_line_endings")) {
  718. ini_set("auto_detect_line_endings", '1');
  719. }
  720. // 文件路径
  721. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  722. // 设置标题
  723. if ($type == 'post') {
  724. $title = [
  725. '内容', date('Y年m月d日')
  726. ];
  727. } else {
  728. $title = [
  729. '回收站内容', date('Y年m月d日')
  730. ];
  731. }
  732. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  733. $writer->insertone($title);
  734. // 内容
  735. if ($type == 'post') {
  736. $header = [
  737. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  738. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  739. '真实收藏数', '总收藏数', '评论数'
  740. ];
  741. } else {
  742. $header = [
  743. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  744. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  745. ];
  746. }
  747. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  748. // $writer->setOutputBOM(Reader::BOM_UTF8);
  749. $writer->insertone($header);
  750. $where = [];
  751. if (isset($request['content'])) {
  752. $where[] = ['content', 'like', "%{$request['content']}%"];
  753. }
  754. if (isset($request['is_suggest'])) {
  755. $where[] = ['is_suggest', $request['is_suggest']];
  756. }
  757. if (isset($request['type'])) {
  758. $where[] = ['type', $request['type']];
  759. }
  760. $sort = 'post.id';
  761. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  762. $sort = $request['sort'];
  763. }
  764. $post = $this->post;
  765. if ($type == 'post_waste') {
  766. $post = $post->onlyTrashed();
  767. }
  768. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  769. ->select('post.*')
  770. ->where($where)
  771. ->where(function ($query) use ($request) {
  772. if (isset($request['keyword'])) {
  773. $query->where('uid', '=', $request['keyword'])
  774. ->orWhere('username', 'like', "%{$request['keyword']}%")
  775. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  776. }
  777. })
  778. ->where(function ($query) use ($request) {
  779. if (isset($request['created_at'])) {
  780. $time = explode('_', $request['created_at']);
  781. $query->whereBetween('post.created_at', $time);
  782. }
  783. })
  784. ->where(function ($query) use ($request) {
  785. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  786. $ids = [];
  787. if (isset($request['category_ids'])) {
  788. $categoryIds = explode('_', $request['category_ids']);
  789. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  790. }
  791. if (isset($request['topic_ids'])) {
  792. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  793. }
  794. Log::debug('话题ids:' . json_encode($ids));
  795. foreach ($ids as $key => $id) {
  796. if ($key == 0) {
  797. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  798. } else {
  799. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  800. }
  801. }
  802. }
  803. })
  804. ->orderBy($sort, 'desc')
  805. ->chunk(1, function ($posts) use ($writer, $type) {
  806. $data = [];
  807. foreach ($posts as $post) {
  808. if ($type == 'post') {
  809. $tmp = [
  810. $post->id,
  811. Carbon::parse($post->created_at)->toDateTimeString(),
  812. $post->username,
  813. $post->location,
  814. implode(' ', $post->topic()->toArray()),
  815. subtext(strip_tags($post->content), 20),
  816. $post->data->pv_real,
  817. $post->data->pv,
  818. $post->data->praise_real_count,
  819. $post->data->praise_count,
  820. $post->data->share_real_count,
  821. $post->data->share_count,
  822. $post->data->collect_real_count,
  823. $post->data->collect_count,
  824. $post->data->comment_count
  825. ];
  826. } else {
  827. $tmp = [
  828. $post->id,
  829. Carbon::parse($post->created_at)->toDateTimeString(),
  830. $post->username,
  831. Carbon::parse($post->created_at)->toDateTimeString(),
  832. subtext(strip_tags($post->content), 20),
  833. $post->data->pv_real,
  834. $post->data->praise_real_count,
  835. $post->data->share_real_count,
  836. $post->data->collect_real_count,
  837. $post->data->comment_count
  838. ];
  839. }
  840. foreach ($tmp as $key => $value) {
  841. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  842. }
  843. $data[] = $tmp;
  844. }
  845. $writer->insertAll($data);
  846. });
  847. Log::info('内容导出成功!');
  848. } catch (QueryException $e) {
  849. Log::debug('内容导出失败!'.$e->getMessage());
  850. }
  851. }
  852. /**
  853. * 统计社区内容
  854. * @param $start
  855. * @param $end
  856. * @return array
  857. */
  858. public function statistics($start, $end)
  859. {
  860. $result = $this->postStatistics
  861. ->where('created_at', '>=', $start)
  862. ->where('created_at', '<=', $end)
  863. ->get()->toArray();
  864. $stimestamp = strtotime($start);
  865. $etimestamp = strtotime($end);
  866. $days = ($etimestamp - $stimestamp) / 86400;
  867. $date = array();
  868. for ($i = 0; $i < $days; $i++) {
  869. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  870. }
  871. $totalRead = 0;
  872. $totalPost = 0;
  873. $totalShare = 0;
  874. $totalLike = 0;
  875. $totalCollect = 0;
  876. $totalComment = 0;
  877. $info = [];
  878. foreach ($date as $key => $value) {
  879. $info[$value] = [
  880. 'read' => 0,
  881. 'post' => 0,
  882. 'share' => 0,
  883. 'like' => 0,
  884. 'collect' => 0,
  885. 'comment' => 0,
  886. ];
  887. foreach ($result as $row) {
  888. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  889. $info[$value]['read'] = $row['read_count'];
  890. $info[$value]['post'] = $row['post_count'];
  891. $info[$value]['share'] = $row['share_count'];
  892. $info[$value]['like'] = $row['like_count'];
  893. $info[$value]['collect'] = $row['collect_count'];
  894. $info[$value]['comment'] = $row['comment_count'];
  895. $totalRead += $row['read_count'];
  896. $totalPost += $row['post_count'];
  897. $totalShare += $row['share_count'];
  898. $totalLike += $row['like_count'];
  899. $totalCollect += $row['collect_count'];
  900. $totalComment += $row['comment_count'];
  901. }
  902. }
  903. }
  904. $info['data']['total_read'] = $totalRead;
  905. $info['data']['total_post'] = $totalPost;
  906. $info['data']['total_share'] = $totalShare;
  907. $info['data']['total_like'] = $totalLike;
  908. $info['data']['total_collect'] = $totalCollect;
  909. $info['data']['total_comment'] = $totalComment;
  910. return $info;
  911. }
  912. }