|
@@ -18,11 +18,14 @@ use App\Models\Topic;
|
|
use App\Traits\PostTrait;
|
|
use App\Traits\PostTrait;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Database\QueryException;
|
|
use Dingo\Api\Http\Response;
|
|
use Dingo\Api\Http\Response;
|
|
|
|
+use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
|
|
+use League\Csv\Writer;
|
|
|
|
+use League\Csv\CannotInsertRecord;
|
|
|
|
|
|
class PostRepository
|
|
class PostRepository
|
|
{
|
|
{
|
|
@@ -551,4 +554,156 @@ class PostRepository
|
|
->paginate($perPage);
|
|
->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($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,
|
|
|
|
+ '标签',
|
|
|
|
+ subtext($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::channel('download')->info('内容导出成功!');
|
|
|
|
+
|
|
|
|
+ } catch (CannotInsertRecord $e) {
|
|
|
|
+ $e->getRecord();
|
|
|
|
+ Log::channel('download')->info('内容导出失败!');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|