123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Post;
- use App\Models\Behavior;
- use App\Models\CategoryTopic;
- use App\Models\MemberFollowTopic;
- use App\Models\Post;
- use App\Models\PostComment;
- use App\Models\PostData;
- use App\Models\PostImgs;
- use App\Models\PostLog;
- use App\Models\PostStatistics;
- use App\Models\PostStore;
- use App\Models\PostStoreImgs;
- use App\Models\Topic;
- use App\Service\RabbitMqUtil;
- use App\Traits\PostTrait;
- use App\Traits\UserTrait;
- use Illuminate\Database\QueryException;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Tymon\JWTAuth\Facades\JWTAuth;
- use League\Csv\Writer;
- use League\Csv\CannotInsertRecord;
- class PostRepository
- {
- use PostTrait;
- use UserTrait;
- public function __construct(Post $post,
- PostData $postData,
- PostComment $postComment,
- PostImgs $postImgs,
- PostLog $postLog,
- RabbitMqUtil $rabbitMqUtil,
- Behavior $behavior,
- CategoryTopic $categoryTopic,
- MemberFollowTopic $memberFollowTopic,
- PostStatistics $postStatistics,
- PostStore $postStore,
- PostStoreImgs $postStoreImgs,
- Topic $topic)
- {
- $this->post = $post;
- $this->postData = $postData;
- $this->postComment = $postComment;
- $this->postImgs = $postImgs;
- $this->postLog = $postLog;
- $this->rabbitMqUtil = $rabbitMqUtil;
- $this->behavior = $behavior;
- $this->categoryTopic = $categoryTopic;
- $this->memberFollowTopic = $memberFollowTopic;
- $this->topic = $topic;
- $this->postStatistics = $postStatistics;
- $this->postStore = $postStore;
- $this->postStoreImgs = $postStoreImgs;
- }
- /**
- * 发布内容
- */
- public function create($request)
- {
- //富文本标题
- if ($request['type'] == 'html' && (!isset($request['title']) || !$request['title'])) {
- return Response::create([
- 'message' => '当类型是富文本时,标题不能为空',
- 'status_code' => 500
- ]);
- }
- //验证小号
- $userInfo = $this->getUserInfo($request['uid']);
- Log::debug('发布内容小号信息:' . json_encode($userInfo));
- if (!$userInfo || $userInfo['type'] != 1) {
- return Response::create([
- 'message' => '所选小号信息有误',
- 'status_code' => 500
- ]);
- }
- if (isset($request['is_point']) && $request['is_point']) {
- $res = $this->uploadImage($request['img']);
- if ($res && isset($res['data']) && isset($res['data']['url'])) {
- $request['img'] = config('customer.chxq_oss_url') . $res['data']['url'];
- Log::debug('内容上传图片成功结果' . json_encode($res));
- } else {
- Log::debug('内容上传图片失败结果' . json_encode($res));
- return Response::create([
- 'message' => '图片格式有误,请重试',
- 'status_code' => 500
- ]);
- }
- }
- //验证话题
- $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
- $topicCount = count($topicIdsArray);
- if ($topicCount == 0 || $topicCount > 5) {
- return Response::create([
- 'message' => '所选话题必须1-5个',
- 'status_code' => 500
- ]);
- }
- $topicIds = implode(',', $topicIdsArray);
- //验证内容字数
- if ($request['type'] != 'html') {
- $html = strip_tags($request['content']);
- if (mb_strlen($html, 'UTF8') > 1000) {
- return Response::create([
- 'message' => '所传内容不能超过1000字',
- 'status_code' => 500
- ]);
- }
- }
- $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
- $score = $fresh / 43200;
- $data = [
- 'uid' => $userInfo['uid'],
- 'username' => $userInfo['username'],
- 'mobile' => $userInfo['mobile'],
- 'avatar' => $userInfo['avatar'] ?? '',
- 'type' => $request['type'],
- 'img' => $request['img'],
- 'video' => $request['video'] ?? '',
- 'video_id' => $request['video_id'] ?? '',
- 'topic_ids' => $topicIds,
- 'title' => $request['title'] ?? '',
- 'content' => $request['content'],
- 'location' => $request['location'] ?? '',
- 'is_suggest' => $request['is_suggest'],
- 'is_hide' => 0,
- 'weight' => $score
- ];
- $date = date('Y-m-d H:i:s');
- DB::beginTransaction();
- try {
- $post = $this->post->create($data);
- $postData = $this->postData->create([
- 'post_id' => $post->id,
- 'pv' => 0,
- 'pv_real' => 0,
- 'dislike_count' => 0,
- 'praise_count' => 0,
- 'praise_real_count' => 0,
- 'share_count' => 0,
- 'share_real_count' => 0,
- 'comment_count' => 0,
- 'collect_count' => 0,
- 'collect_real_count' => 0,
- 'available_bean' => $this->availableBean(),
- 'will_collect_bean' => rand(100, 200),
- 'collect_bean' => 0
- ]);
- $imgs = [];
- if (!empty($request['imgs']) && $request['type'] == 'image') {
- $imgData = [];
- foreach ($request['imgs'] as $img) {
- $imgs[] = $img;
- $imgData[] = [
- 'post_id' => $post->id,
- 'img' => $img,
- 'created_at' => $date,
- 'updated_at' => $date
- ];
- }
- $this->postImgs->insert($imgData);
- }
- if (isset($request['store_id']) && $request['store_id']) {
- $this->postStore->where('id', $request['store_id'])->update(['is_used' => 1]);
- }
- DB::commit();
- Redis::zadd('post_trigger_type', 0, $post->id);
- foreach ($topicIdsArray as $id) {
- Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
- Redis::zincrby('topic.just_use_count', 1, $id);
- }
- $virus = $this->behavior->where('behavior_identification', 'publish')->first();
- if ($virus) {
- if ($post->title) {
- $desc = $post->title;
- } else {
- $desc = subtext(strip_tags($post->content), 20);
- }
- $this->rabbitMqUtil->push('virus_add', [
- 'behavior_id' => $virus->virus_behavior_id,
- 'behavior_flag' => 'publish',
- 'post_id' => $post->id,
- 'post_type' => $post->type,
- 'post_desc' => $desc,
- 'post_cover' => $post->img,
- 'target_id' => $post->uid,
- 'action_id' => $post->id,
- ]);
- }
- Redis::HSET('post_info_' . $post->id,
- 'id', $post->id,
- 'uid', $post->uid,
- 'type', $post->type,
- 'img', $post->img,
- 'imgs', json_encode($imgs),
- 'video', $post->video,
- 'topic_ids', $post->topic_ids,
- 'title', $post->title,
- 'content', $post->content,
- 'location', $post->location,
- 'pv', $postData->pv,
- 'dislike_count', $postData->dislike_count,
- 'praise_count', $postData->praise_count,
- 'share_count', $postData->share_count,
- 'comment_count', $postData->comment_count,
- 'collect_count', $postData->collect_count,
- 'available_bean', $postData->available_bean,
- 'will_collect_bean', $postData->will_collect_bean,
- 'create_bean', $postData->create_bean,
- 'collect_bean', $postData->collect_bean,
- 'created_at', $post->created_at);
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('发布内容:' . $exception->getMessage());
- return Response::create([
- 'message' => '发布失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 增加数据
- */
- public function addData($request)
- {
- $token = JWTAuth::decode(JWTAuth::getToken());
- if (!$token || $token['type'] != 1) {
- return Response::create([
- 'message' => '获取登陆信息失败',
- 'status_code' => 500
- ]);
- }
- $uid = $token['user']->id;
- $username = $token['user']->username;
- //验证小号数量
- $number = max([
- $request['add_pv'],
- $request['add_praise_count'],
- $request['add_collect_count'],
- $request['add_share_count']
- ]);
- $members = $this->getSystemMember($number);
- if (!$members || $members['status_code'] != 200) {
- return Response::create([
- 'message' => $members['message'],
- 'status_code' => 500
- ]);
- }
- $post = $this->post->find($request['post_id']);
- $postData = $this->postData->where('post_id', $request['post_id'])->first();
- if (!$postData || !$post) {
- return Response::create([
- 'message' => '获取内容失败',
- 'status_code' => 500
- ]);
- }
- if ($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0) {
- return Response::create([
- 'message' => '增加数据不能同时为0',
- 'status_code' => 500
- ]);
- }
- $content = [
- 'add_pv' => 0,
- 'add_praise_count' => 0,
- 'add_collect_count' => 0,
- 'add_share_count' => 0,
- ];
- if ($request['add_pv']) {
- $postData->pv += $request['add_pv'];
- $content['add_pv'] = $request['add_pv'];
- }
- if ($request['add_praise_count']) {
- $postData->praise_count += $request['add_praise_count'];
- $content['add_praise_count'] = $request['add_praise_count'];
- }
- if ($request['add_collect_count']) {
- $postData->collect_count += $request['add_collect_count'];
- $content['add_collect_count'] = $request['add_collect_count'];
- }
- if ($request['add_share_count']) {
- $postData->share_count += $request['add_share_count'];
- $content['add_share_count'] = $request['add_share_count'];
- }
- DB::beginTransaction();
- try {
- $postData->save();
- $this->postLog->create([
- 'post_id' => $request['post_id'],
- 'uid' => $uid,
- 'username' => $username,
- 'log_type' => 'add_data',
- 'content' => json_encode($content)
- ]);
- DB::commit();
- $virus = $this->behavior
- ->whereIn('behavior_identification', ['read', 'forward', 'like', 'collect'])
- ->pluck('virus_behavior_id', 'behavior_identification');
- if ($post->title) {
- $desc = $post->title;
- } else {
- $desc = subtext(strip_tags($post->content), 20);
- }
- $data = [
- 'behavior_value' => 1,
- 'post_id' => $post->id,
- 'post_type' => $post->type,
- 'post_author_uid' => $post->uid,
- 'post_desc' => $desc,
- 'post_cover' => $post->img,
- 'action_id' => $post->id,
- ];
- foreach ($members['data'] as $key => $member) {
- if (isset($virus['read']) && $request['add_pv'] > $key) {
- $newData = array_merge($data, [
- 'behavior_id' => $virus['read'],
- 'behavior_flag' => 'read',
- 'target_id' => $member['uid'],
- ]);
- $this->rabbitMqUtil->push('virus_add', $newData);
- }
- if (isset($virus['like']) && $request['add_praise_count'] > $key) {
- $newData = array_merge($data, [
- 'behavior_id' => $virus['like'],
- 'behavior_flag' => 'like',
- 'target_id' => $member['uid'],
- ]);
- $this->rabbitMqUtil->push('virus_add', $newData);
- }
- if (isset($virus['collect']) && $request['add_collect_count'] > $key) {
- $newData = array_merge($data, [
- 'behavior_id' => $virus['collect'],
- 'behavior_flag' => 'collect',
- 'target_id' => $member['uid'],
- ]);
- $this->rabbitMqUtil->push('virus_add', $newData);
- }
- if (isset($virus['forward']) && $request['add_share_count'] > $key) {
- $newData = array_merge($data, [
- 'behavior_id' => $virus['forward'],
- 'behavior_flag' => 'forward',
- 'target_id' => $member['uid'],
- ]);
- $this->rabbitMqUtil->push('virus_add', $newData);
- }
- }
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('内容增加数据:' . $request['post_id'] . $exception->getMessage());
- return Response::create([
- 'message' => '增加数据失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 评论&回复
- */
- public function comment($request)
- {
- //验证小号
- $userInfo = $this->getUserInfo($request['uid']);
- Log::debug('评论&回复小号' . json_encode($userInfo));
- if (!$userInfo || $userInfo['type'] != 1) {
- return Response::create([
- 'message' => '所选小号信息有误',
- 'status_code' => 500
- ]);
- }
- $post = $this->post->find($request['post_id']);
- if (!$post) {
- return Response::create([
- 'message' => '获取内容失败',
- 'status_code' => 500
- ]);
- }
- $data = [
- 'uid' => $request['uid'],
- 'post_id' => $request['post_id'],
- 'parent_id' => 0,
- 'username' => $userInfo['username'],
- 'reply_uid' => 0,
- 'reply_username' => '',
- 'avatar' => $userInfo['avatar'] ?? '',
- 'content' => $request['content'],
- 'is_delete' => 0,
- ];
- $parentCommentContent = '';
- $parentCommentUid = 0;
- $parentCommentTime = '';
- if (isset($request['parent_id']) && $request['parent_id'] != 0) {
- $comment = $this->postComment->find($request['parent_id']);
- if (!$comment) {
- return Response::create([
- 'message' => '获取评论信息失败',
- 'status_code' => 500
- ]);
- }
- if ($comment->parent_id) {
- return Response::create([
- 'message' => '只能回复评论',
- 'status_code' => 500
- ]);
- }
- $data['parent_id'] = $request['parent_id'];
- $data['reply_uid'] = $comment->uid;
- $data['reply_username'] = $comment->username;
- $parentCommentContent = $comment->content;
- $parentCommentUid = $comment->uid;
- $parentCommentTime = Carbon::parse($comment->created_at)->toDateTimeString();
- }
- DB::beginTransaction();
- try {
- $newComment = $this->postComment->create($data);
- if ($newComment->parent_id) {
- $this->postComment->where('id', $newComment->parent_id)->increment('reply_count');
- }
- DB::commit();
- if ($newComment->parent_id) {
- Redis::DEL('post_new_reply_' . $newComment->parent_id);
- } else {
- Redis::DEL('post_new_comment_' . $newComment->post_id);
- }
- $virus = $this->behavior->where('behavior_identification', 'comment')->first();
- if ($virus) {
- if ($post->title) {
- $desc = $post->title;
- } else {
- $desc = subtext(strip_tags($post->content), 20);
- }
- $this->rabbitMqUtil->push('virus_add', [
- 'behavior_id' => $virus->virus_behavior_id,
- 'behavior_flag' => 'comment',
- 'post_id' => $post->id,
- 'post_type' => $post->type,
- 'post_author_uid' => $post->uid,
- 'post_desc' => $desc,
- 'post_cover' => $post->img,
- 'comment_id' => $newComment->id,
- 'comment_content' => $newComment->content,
- 'parent_comment_id' => $newComment->parent_id,
- 'parent_comment_content' => $parentCommentContent,
- 'parent_comment_uid' => $parentCommentUid,
- 'parent_comment_time' => $parentCommentTime,
- 'reply_uid' => $newComment->reply_uid,
- 'reply_username' => $newComment->reply_username,
- 'target_id' => $newComment->uid,
- 'action_id' => $newComment->id,
- ]);
- }
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
- return Response::create([
- 'message' => '评论失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 内容列表
- */
- public function lists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['is_suggest'])) {
- $where[] = ['is_suggest', $request['is_suggest']];
- }
- if (isset($request['type'])) {
- $where[] = ['type', $request['type']];
- }
- if (isset($request['uid'])) {
- $where[] = ['uid', $request['uid']];
- }
- $sort = 'post.id';
- if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
- $sort = $request['sort'];
- }
- $post = $this->post;
- if (isset($request['waste']) && $request['waste'] == 1) {
- $post = $post->onlyTrashed();
- }
- return $post
- ->join('post_data', 'post_data.post_id', '=', 'post.id')
- ->with('data')
- ->select('post.*')
- ->where($where)
- ->where(function ($query) use ($request) {
- if (isset($request['keyword'])) {
- $query->where('uid', '=', $request['keyword'])
- ->orWhere('username', 'like', "%{$request['keyword']}%")
- ->orWhere('mobile', 'like', "%{$request['keyword']}%");
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['content'])) {
- $query->where('title', 'like', "%{$request['content']}%")
- ->orWhere('content', 'like', "%{$request['content']}%");
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['created_at'])) {
- $time = explode('_', $request['created_at']);
- $query->whereBetween('post.created_at', $time);
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['category_ids']) || isset($request['topic_ids'])) {
- $ids = [];
- if (isset($request['category_ids'])) {
- $categoryIds = explode('_', $request['category_ids']);
- $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
- }
- if (isset($request['topic_ids'])) {
- $ids = array_merge($ids, explode('_', $request['topic_ids']));
- }
- foreach ($ids as $key => $id) {
- if ($key == 0) {
- $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
- } else {
- $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
- }
- }
- }
- })
- ->orderBy($sort, 'desc')
- ->paginate($perPage);
- }
- /**
- * 内容详情
- */
- public function detail($request)
- {
- return $this->post->withTrashed()->find($request['id']);
- }
- /**
- * 内容类型
- */
- public function getType($request)
- {
- $type = $this->post->where('id', $request['id'])->value('type');
- return Response::create([
- 'message' => '成功',
- 'status_code' => 200,
- 'data' => $type
- ]);
- }
- /**
- * 获取关注分类用户uids
- */
- public function getCategoryUids($request)
- {
- $categoryIds = json_decode($request['category'], true);
- $topicIds = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
- $uids = $this->memberFollowTopic->whereIn('topic_id', $topicIds)->pluck('uid')->toArray();
- $uids = array_unique($uids);
- sort($uids);
- return Response::create([
- 'message' => '成功',
- 'status_code' => 200,
- 'data' => $uids
- ]);
- }
- /**
- * 评论列表
- */
- public function commentList($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['post_id'])) {
- $where[] = ['post_id', $request['post_id']];
- }
- if (isset($request['uid'])) {
- $where[] = ['uid', $request['uid']];
- }
- return $this->postComment
- ->where($where)
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 推荐内容
- */
- public function suggest($request)
- {
- $token = JWTAuth::decode(JWTAuth::getToken());
- if ($token['type'] != 1) {
- return Response::create([
- 'message' => '只有运营才能修改推荐',
- 'status_code' => 500
- ]);
- }
- $post = $this->post->where('id', $request['id'])->first();
- if (!$post) {
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- if ($post->is_suggest == 1) {
- $post->is_suggest = 0;
- $logData = [
- 'uid' => $token['user']->id,
- 'operator_type' => 'admin',
- 'post_id' => $request['id'],
- 'username' => $token['user']->username,
- 'log_type' => 'cancel_suggest',
- 'content' => json_encode(['cancel_suggest' => $request['id']]),
- ];
- } else {
- $post->is_suggest = 1;
- $logData = [
- 'uid' => $token['user']->id,
- 'operator_type' => 'admin',
- 'post_id' => $request['id'],
- 'username' => $token['user']->username,
- 'log_type' => 'suggest',
- 'content' => json_encode(['suggest' => $request['id']]),
- ];
- }
- $date = Carbon::now()->toDateTimeString();
- DB::beginTransaction();
- try {
- $post->save();
- $this->postLog->create($logData);
- DB::commit();
- //内容被设为推荐时,发送推送消息
- if($post->is_suggest==1){
- $title = $post['title'] ? $post['title'] : subtext($post['content'], 18);
- $push['uid'] = $post->uid;
- $push['title'] = '【由你内容】推荐通知';
- $push['text'] = '你的内容《'.$title.'》被小编推荐全平台优先展示喽,获得推荐位后你的内容会被更多人看到。记得多来和大家互动赚取更多U米~';
- Redis::LPUSH('send_push_msg_queue',json_encode($push));
- $rabbitmq = new RabbitMqUtil();
- $rabbitmq->push('add_message_one', [
- 'uid' => $post->uid,
- 'message_rule_id' => 0,
- 'message_type' => 1,
- 'message_show_type' => 'system_common',
- 'param' => [
- 'title' => $push['title'],
- 'content' => $push['text'],
- 'cover' => '',
- 'activity_url' => 0,
- 'activity_time' => '',
- ],
- 'is_read' => 0,
- 'created_at' => $date,
- 'updated_at' => $date,
- ]);
- }
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 删除内容
- */
- public function delete($request)
- {
- $token = JWTAuth::decode(JWTAuth::getToken());
- if ($token['type'] != 1) {
- return Response::create([
- 'message' => '只有运营才能删除内容',
- 'status_code' => 500
- ]);
- }
- $post = $this->post->where('id', $request['id'])->first();
- if (!$post) {
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- $uid = $post->uid;
- $title = $post->title;
- if (!$title) {
- $title = subtext(strip_tags($post->content), 20);
- }
- $content = "经核实您的内容“{$title}”涉及违规,现已被删除,有任何问题请联系由你管理员";
- $logData = [
- 'uid' => $token['user']->id,
- 'operator_type' => 'admin',
- 'post_id' => $request['id'],
- 'username' => $token['user']->username,
- 'log_type' => 'delete',
- 'content' => json_encode(['delete' => $request['id']]),
- ];
- $date = Carbon::now()->toDateTimeString();
- DB::beginTransaction();
- try {
- $post->delete();
- $this->postLog->create($logData);
- DB::commit();
- Redis::SADD('delete_post_ids', $request['id']);
- $this->rabbitMqUtil->push('add_message_one', [
- 'uid' => $uid,
- 'message_rule_id' => 0,
- 'message_type' => 1,
- 'message_show_type' => 'system_common',
- 'param' => [
- 'title' => '内容删除',
- 'content' => $content,
- 'cover' => '',
- 'activity_url' => 0,
- 'activity_time' => '',
- ],
- 'is_read' => 0,
- 'created_at' => $date,
- 'updated_at' => $date,
- ]);
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 复原内容
- */
- public function restore($request)
- {
- $post = $this->post->withTrashed()->where('id', $request['id'])->first();
- if (!$post) {
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $post->restore();
- DB::commit();
- Redis::SREM('delete_post_ids', $request['id']);
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 删除评论
- */
- public function commentDelete($request)
- {
- $comment = $this->postComment->find($request['id']);
- if (!$comment) {
- return Response::create([
- 'message' => '获取评论信息失败',
- 'status_code' => 500
- ]);
- }
- if ($comment->is_delete == 1) {
- return Response::create([
- 'message' => '该评论已经删除',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $comment->is_delete = 1;
- $comment->save();
- DB::commit();
- Redis::SADD('delete_post_comment_ids', $comment->id);
- if (!$comment->parent_id) {
- Redis::DEL('post_new_comment_' . $comment->post_id);
- } else {
- Redis::DEL('post_new_reply_' . $comment->id);
- }
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 隐藏内容
- */
- public function hide($request)
- {
- $post = $this->post->where('id', $request['id'])->first();
- if (!$post) {
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- if ($post->is_hide == 1) {
- $post->is_hide = 0;
- } else {
- $post->is_hide = 1;
- }
- DB::beginTransaction();
- try {
- $post->save();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 日志列表
- */
- public function log($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['log_type'])) {
- $where[] = ['log_type', $request['log_type']];
- }
- return $this->postLog
- ->where($where)
- ->where(function ($query) use ($request) {
- if (isset($request['created_at'])) {
- $time = explode('_', $request['created_at']);
- $query->whereBetween('created_at', $time);
- }
- })
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- public function download($filePath, $type, $request)
- {
- try {
- set_time_limit(0);
- if (!ini_get("auto_detect_line_endings")) {
- ini_set("auto_detect_line_endings", '1');
- }
- // 文件路径
- $writer = Writer::createFromPath(public_path($filePath), 'w+');
- // 设置标题
- if ($type == 'post') {
- $title = [
- '内容', date('Y年m月d日')
- ];
- } else {
- $title = [
- '回收站内容', date('Y年m月d日')
- ];
- }
- $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
- $writer->insertone($title);
- // 内容
- if ($type == 'post') {
- $header = [
- '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
- '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
- '真实收藏数', '总收藏数', '评论数'
- ];
- } else {
- $header = [
- '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
- '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
- ];
- }
- $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
- // $writer->setOutputBOM(Reader::BOM_UTF8);
- $writer->insertone($header);
- $where = [];
- if (isset($request['content'])) {
- $where[] = ['content', 'like', "%{$request['content']}%"];
- }
- if (isset($request['is_suggest'])) {
- $where[] = ['is_suggest', $request['is_suggest']];
- }
- if (isset($request['type'])) {
- $where[] = ['type', $request['type']];
- }
- $sort = 'post.id';
- if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
- $sort = $request['sort'];
- }
- $post = $this->post;
- if ($type == 'post_waste') {
- $post = $post->onlyTrashed();
- }
- $post->join('post_data', 'post_data.post_id', '=', 'post.id')
- ->select('post.*')
- ->where($where)
- ->where(function ($query) use ($request) {
- if (isset($request['keyword'])) {
- $query->where('uid', '=', $request['keyword'])
- ->orWhere('username', 'like', "%{$request['keyword']}%")
- ->orWhere('mobile', 'like', "%{$request['keyword']}%");
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['created_at'])) {
- $time = explode('_', $request['created_at']);
- $query->whereBetween('post.created_at', $time);
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['category_ids']) || isset($request['topic_ids'])) {
- $ids = [];
- if (isset($request['category_ids'])) {
- $categoryIds = explode('_', $request['category_ids']);
- $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
- }
- if (isset($request['topic_ids'])) {
- $ids = array_merge($ids, explode('_', $request['topic_ids']));
- }
- Log::debug('话题ids:' . json_encode($ids));
- foreach ($ids as $key => $id) {
- if ($key == 0) {
- $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
- } else {
- $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
- }
- }
- }
- })
- ->orderBy($sort, 'desc')
- ->chunk(1, function ($posts) use ($writer, $type) {
- $data = [];
- foreach ($posts as $post) {
- if ($type == 'post') {
- $tmp = [
- $post->id,
- Carbon::parse($post->created_at)->toDateTimeString(),
- $post->username,
- $post->location,
- implode(' ', $this->getTopic($post->topic_ids)),
- subtext(strip_tags($post->content), 20),
- $post->data->pv_real,
- $post->data->pv,
- $post->data->praise_real_count,
- $post->data->praise_count,
- $post->data->share_real_count,
- $post->data->share_count,
- $post->data->collect_real_count,
- $post->data->collect_count,
- $post->data->comment_count
- ];
- } else {
- $tmp = [
- $post->id,
- Carbon::parse($post->created_at)->toDateTimeString(),
- $post->username,
- Carbon::parse($post->created_at)->toDateTimeString(),
- subtext(strip_tags($post->content), 20),
- $post->data->pv_real,
- $post->data->praise_real_count,
- $post->data->share_real_count,
- $post->data->collect_real_count,
- $post->data->comment_count
- ];
- }
- foreach ($tmp as $key => $value) {
- $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
- }
- $data[] = $tmp;
- }
- $writer->insertAll($data);
- });
- Log::info('内容导出成功!');
- } catch (QueryException $e) {
- Log::debug('内容导出失败!' . $e->getMessage());
- }
- }
- /**
- * 统计社区内容
- * @param $start
- * @param $end
- * @return array
- */
- public function statistics($start, $end)
- {
- $result = $this->postStatistics
- ->where('created_at', '>=', $start)
- ->where('created_at', '<=', $end)
- ->get()->toArray();
- $stimestamp = strtotime($start);
- $etimestamp = strtotime($end);
- $days = ($etimestamp - $stimestamp) / 86400;
- $date = array();
- for ($i = 0; $i < $days; $i++) {
- $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
- }
- $totalRead = 0;
- $totalPost = 0;
- $totalShare = 0;
- $totalLike = 0;
- $totalCollect = 0;
- $totalComment = 0;
- $info = [];
- foreach ($date as $key => $value) {
- $info[$value] = [
- 'read' => 0,
- 'post' => 0,
- 'share' => 0,
- 'like' => 0,
- 'collect' => 0,
- 'comment' => 0,
- ];
- foreach ($result as $row) {
- if ($value == date('Y-m-d', strtotime($row['created_at']))) {
- $info[$value]['read'] = $row['read_count'];
- $info[$value]['post'] = $row['post_count'];
- $info[$value]['share'] = $row['share_count'];
- $info[$value]['like'] = $row['like_count'];
- $info[$value]['collect'] = $row['collect_count'];
- $info[$value]['comment'] = $row['comment_count'];
- $totalRead += $row['read_count'];
- $totalPost += $row['post_count'];
- $totalShare += $row['share_count'];
- $totalLike += $row['like_count'];
- $totalCollect += $row['collect_count'];
- $totalComment += $row['comment_count'];
- }
- }
- }
- $info['data']['total_read'] = $totalRead;
- $info['data']['total_post'] = $totalPost;
- $info['data']['total_share'] = $totalShare;
- $info['data']['total_like'] = $totalLike;
- $info['data']['total_collect'] = $totalCollect;
- $info['data']['total_comment'] = $totalComment;
- return $info;
- }
- /**
- * 编辑内容话题
- */
- public function updateTopic($request)
- {
- $token = JWTAuth::decode(JWTAuth::getToken());
- if ($token['type'] != 1) {
- return Response::create([
- 'message' => '只有运营才能编辑内容话题',
- 'status_code' => 500
- ]);
- }
- $post = $this->post->where('id', $request['id'])->first();
- if (!$post) {
- return Response::create([
- 'message' => '获取内容信息失败',
- 'status_code' => 500
- ]);
- }
- //验证话题
- $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
- $topicCount = count($topicIdsArray);
- if ($topicCount == 0 || $topicCount > 5) {
- return Response::create([
- 'message' => '所选话题必须1-5个',
- 'status_code' => 500
- ]);
- }
- $topicIds = implode(',', $topicIdsArray);
- $logData = [
- 'uid' => $token['user']->id,
- 'operator_type' => 'admin',
- 'post_id' => $request['id'],
- 'username' => $token['user']->username,
- 'log_type' => 'update_topic',
- 'content' => json_encode(['update_topic' => $request['id'] . '-' . $topicIds]),
- ];
- DB::beginTransaction();
- try {
- $post->topic_ids = $topicIds;
- $post->save();
- $this->postLog->create($logData);
- DB::commit();
- Redis::HSET('post_info_' . $request['id'], 'topic_ids', $topicIds);
- foreach ($topicIdsArray as $id) {
- Redis::zincrby('topic.just_use_count', 1, $id);
- }
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('编辑内容话题:' . $post->id . $exception->getMessage());
- return Response::create([
- 'message' => '编辑失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 添加网站内容
- */
- public function createStore($startPage, $endPage, $size, $categoryId)
- {
- set_time_limit(1800);
- for ($i = $startPage; $i <= $endPage; $i++) {
- $this->addContent($i, $size, $categoryId);
- usleep(100000);
- }
- }
- /**
- * 获取网站内容
- */
- public function addContent($page, $size, $categoryId)
- {
- $lists = $this->getAcFunList($page, $size, $categoryId);
- if (empty($lists)) {
- return Response::create([
- 'message' => '获取数据失败',
- 'status_code' => 500
- ]);
- }
- $data = [];
- foreach ($lists as $item) {
- $res = $this->getData($item['id'], $item['title']);
- if ($res && $res['content'] && $res['images']) {
- $data[] = array_merge(['source_id' => $item['id']], $res);
- }
- }
- if (empty($data)) {
- return Response::create([
- 'message' => '处理数据失败',
- 'status_code' => 500
- ]);
- }
- $date = Carbon::now()->toDateTimeString();
- $successNo = 0;
- $failNo = 0;
- DB::beginTransaction();
- try {
- foreach ($data as $item) {
- if ($this->postStore->where('source', 'AcFun')->where('source_id', $item['source_id'])->exists()) {
- $failNo++;
- continue;
- }
- $storeData = [
- 'source' => 'Acfun',
- 'source_id' => $item['source_id'],
- 'category_id' => $categoryId,
- 'title' => $item['title'],
- 'content' => $item['content'],
- 'img' => $item['images'][0],
- ];
- $postStore = $this->postStore->create($storeData);
- $storeImgsData = [];
- foreach ($item['images'] as $img) {
- $storeImgsData[] = [
- 'post_store_id' => $postStore->id,
- 'img' => $img,
- 'created_at' => $date,
- 'updated_at' => $date,
- ];
- }
- $this->postStoreImgs->insert($storeImgsData);
- $successNo++;
- }
- DB::commit();
- Log::info('第' . $page . '页添加内容数据成功' . $successNo . ',失败' . $failNo);
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('添加内容数据失败:' . $exception->getMessage());
- return Response::create([
- 'message' => '添加内容数据失败',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 获取网站内容
- */
- public function getData($id, $title = '')
- {
- $url = 'https://www.acfun.cn/a/ac' . $id;
- $webpage = file_get_contents($url);
- preg_match_all('/parts[\s\S]*?}]/i', $webpage, $res);
- if (!isset($res[0][0])) {
- return [];
- }
- $data = $res[0][0];
- preg_match_all('/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i ', $data, $contents);
- if (isset($contents[0])) {
- $content = implode($contents[0]);
- } else {
- return [];
- }
- $content = strip_tags($content);
- $content = str_replace([' '], '', $content);
- preg_match_all('/src=(.*)>/U', $data, $imageArray);
- $images = [];
- if (isset($imageArray[0])) {
- foreach ($imageArray[0] as $img) {
- $imgArr = explode('"', $img);
- if (isset($imgArr[1])) {
- $imgArr[1] = trim($imgArr[1], '\\');
- $images[] = $imgArr[1];
- } else {
- return [];
- }
- }
- }
- return [
- 'title' => $title,
- 'content' => $content,
- 'images' => $images,
- ];
- }
- /**
- * 获取网站内容
- */
- public function getStore($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['type'])) {
- $where[] = ['type', $request['type']];
- }
- if (isset($request['category_id'])) {
- $where[] = ['category_id', $request['category_id']];
- }
- if (isset($request['title'])) {
- $where[] = ['title', 'like', "%{$request['title']}%"];
- }
- if (isset($request['content'])) {
- $where[] = ['content', 'like', "%{$request['content']}%"];
- }
- if (isset($request['source'])) {
- $where[] = ['source', $request['source']];
- }
- if (isset($request['is_used'])) {
- $where[] = ['is_used', $request['is_used']];
- }
- return $this->postStore
- ->where($where)
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 网站内容详情
- */
- public function getStoreDetail($request)
- {
- return $this->postStore->find($request['id']);
- }
- /**
- * 内容列表(批量替换话题)
- */
- public function topicList($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- return $this->post
- ->where(function ($query) use ($request) {
- if (isset($request['topic_id']) && $request['topic_id']) {
- $query->whereRaw('FIND_IN_SET(' . $request['topic_id'] . ',topic_ids)');
- }else{
- $query->where('id', 0);
- }
- })
- ->orderBy('id', 'desc')
- ->paginate($perPage);
- }
- /**
- * 批量替换话题
- */
- public function TopicUpdate($request)
- {
- $token = JWTAuth::decode(JWTAuth::getToken());
- if ($token['type'] != 1) {
- return Response::create([
- 'message' => '只有运营才能替换内容话题',
- 'status_code' => 500
- ]);
- }
- $uid = $token['user']->id;
- $username = $token['user']->username;
- $topicId = $request['topic_id'];
- $toTopicId = $request['to_topic_id'];
- $this->post
- ->whereIn('id',$request['ids'])
- ->whereRaw('FIND_IN_SET(' . $topicId . ',topic_ids)')
- ->chunk(100, function ($posts) use ($uid, $username, $topicId, $toTopicId) {
- foreach($posts as $post){
- DB::beginTransaction();
- try {
- $topicIds = explode(',', $post->topic_ids);
- if(in_array($toTopicId, $topicIds)) continue;
- foreach($topicIds as &$val){
- if($val == $topicId) $val = $toTopicId;
- }
- $post->topic_ids = implode(',', $topicIds);
- $post->save();
- $this->postLog->create([
- 'uid' => $uid,
- 'operator_type' => 'admin',
- 'post_id' => $post->id,
- 'username' => $username,
- 'log_type' => 'change_topic',
- 'content' => json_encode(['change_topic' => "{$post->id}:{$topicId} => {$toTopicId}"]),
- ]);
- Log::debug("post_id".$post->id);
- DB::commit();
- Redis::HSET('post_info_' . $post->id, 'topic_ids', $post->topic_ids);
- Redis::zincrby('topic.just_use_count', 1, $toTopicId);
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('替换内容话题失败:' . $exception->getMessage());
- }
- }
- return Response::create();
- });
- }
- }
|