PostRepository.php 40 KB

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