PostRepository.php 33 KB

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