PostRepository.php 33 KB

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