PostRepository.php 52 KB

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