PostRepository.php 33 KB

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