PostRepository.php 34 KB

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