PostRepository.php 40 KB

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