PostRepository.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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 > 5) {
  104. return Response::create([
  105. 'message' => '所选话题必须1-5个',
  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. $this->post
  1316. ->whereIn('id',$request['ids'])
  1317. ->whereRaw('FIND_IN_SET(' . $topicId . ',topic_ids)')
  1318. ->chunk(100, function ($posts) use ($uid, $username, $topicId, $toTopicId) {
  1319. foreach($posts as $post){
  1320. DB::beginTransaction();
  1321. try {
  1322. $topicIds = explode(',', $post->topic_ids);
  1323. if(in_array($toTopicId, $topicIds)){
  1324. DB::rollBack();
  1325. continue;
  1326. }
  1327. foreach($topicIds as &$val){
  1328. if($val == $topicId) $val = $toTopicId;
  1329. }
  1330. $post->topic_ids = implode(',', $topicIds);
  1331. $post->save();
  1332. $this->postLog->create([
  1333. 'uid' => $uid,
  1334. 'operator_type' => 'admin',
  1335. 'post_id' => $post->id,
  1336. 'username' => $username,
  1337. 'log_type' => 'change_topic',
  1338. 'content' => json_encode(['change_topic' => "{$post->id}:{$topicId} => {$toTopicId}"]),
  1339. ]);
  1340. Log::debug("post_id".$post->id);
  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. } catch (QueryException $exception) {
  1345. DB::rollBack();
  1346. Log::debug('替换内容话题失败:' . $exception->getMessage());
  1347. }
  1348. }
  1349. return Response::create();
  1350. });
  1351. }
  1352. }