PostRepository.php 41 KB

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