PostRepository.php 46 KB

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