12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- <?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\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,
- 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;
- }
- /**
- * 发布内容
- */
- 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
- ]);
- }
- //验证话题
- $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
- ]);
- }
- }
- $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
- ];
- $date = date('Y-m-d H:i:s');
- $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
- $score = $fresh / 86400;
- DB::beginTransaction();
- try {
- $post = $this->post->create($data);
- $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,
- 'weight' => $score
- ]);
- if (!empty($request['imgs']) && $request['type'] == 'image') {
- $imgData = [];
- foreach ($request['imgs'] as $img) {
- $imgData[] = [
- 'post_id' => $post->id,
- 'img' => $img,
- 'created_at' => $date,
- 'updated_at' => $date
- ];
- }
- $this->postImgs->insert($imgData);
- }
- DB::commit();
- Redis::zadd('post_trigger_type', 0, $post->id);
- foreach ($topicIdsArray as $id) {
- Redis::zincrby('topic.user_uid' . $request['uid'], 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,
- ]);
- }
- 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 {
- $comment = $this->postComment->create($data);
- DB::commit();
- $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' => $comment->id,
- 'comment_content' => $comment->content,
- 'parent_comment_id' => $comment->parent_id,
- 'parent_comment_content' => $parentCommentContent,
- 'parent_comment_uid' => $parentCommentUid,
- 'parent_comment_time' => $parentCommentTime,
- 'reply_uid' => $comment->reply_uid,
- 'reply_username' => $comment->reply_username,
- 'target_id' => $comment->uid,
- 'action_id' => $comment->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')
- ->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)
- {
- $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}”涉及违规,现已被删除,有任何问题请联系由你管理员";
- $date = Carbon::now()->toDateTimeString();
- DB::beginTransaction();
- try {
- $post->delete();
- 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);
- 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(' ', $post->topic()->toArray()),
- 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;
- }
- }
|