PostRepository.php 33 KB

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