12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328 |
- <?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\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,
- 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->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 / 86400;
- $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
- ]);
- }
- /**
- * 评论列表
- */
- 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)
- {
- $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;
- } else {
- $post->is_suggest = 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 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' => 'post_delete',
- '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']);
- }
- }
|