浏览代码

网站内容

wzq 5 年之前
父节点
当前提交
741f5e5e6f

+ 65 - 0
app/Http/Controllers/Post/PostController.php

@@ -12,7 +12,9 @@ use App\Repositories\Post\PostRepository;
 use App\Transformers\Post\CommentTransformer;
 use App\Transformers\Post\DetailTransformer;
 use App\Transformers\Post\LogTransformer;
+use App\Transformers\Post\PostStoreTransformer;
 use App\Transformers\Post\PostTransformer;
+use App\Transformers\Post\StoreDetailTransformer;
 use Carbon\Carbon;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Validator;
@@ -346,4 +348,67 @@ class PostController extends Controller
         }
         return  $this->postRepository->updateTopic($request->all());
     }
+
+    /**
+     * 获取网站内容
+     */
+    public function createStore(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'start_page' => 'required|integer',
+            'end_page' => 'required|integer',
+            'size' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return $this->postRepository->createStore($request['start_page'], $request['end_page'], $request['size']);
+    }
+
+    /**
+     * 获取网站内容
+     */
+    public function getStore(Request $request)
+    {
+        $productList = $this->postRepository->getStore($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($productList, new PostStoreTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'title',
+                'content',
+                'source',
+                'is_used',
+            ],
+            'columns' => [
+                'id',
+                'source',
+                'title',
+                'content',
+                'img',
+                'is_used',
+                'created_at',
+            ]
+        ];
+        return $data;
+    }
+
+    /**
+     * 网站内容详情
+     */
+    public function getStoreDetail(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:post_store'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        $post = $this->postRepository->getStoreDetail($request->all());
+        return $this->response->item($post, new StoreDetailTransformer());
+    }
 }

+ 22 - 0
app/Models/PostStore.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/5
+ * Time: 16:24
+ */
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class PostStore extends Model
+{
+    protected $table = 'post_store';
+    protected $guarded = [];
+
+    public function imgs()
+    {
+        return $this->hasMany('App\Models\PostStoreImgs', 'post_store_id', 'id');
+    }
+
+}

+ 17 - 0
app/Models/PostStoreImgs.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 11:21
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostStoreImgs extends Model
+{
+//
+    protected $table = 'post_store_imgs';
+    protected $guarded = [];
+}

+ 167 - 0
app/Repositories/Post/PostRepository.php

@@ -17,6 +17,8 @@ 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;
@@ -46,6 +48,8 @@ class PostRepository
                                 Behavior $behavior,
                                 CategoryTopic $categoryTopic,
                                 PostStatistics $postStatistics,
+                                PostStore $postStore,
+                                PostStoreImgs $postStoreImgs,
                                 Topic $topic)
     {
         $this->post = $post;
@@ -58,6 +62,8 @@ class PostRepository
         $this->categoryTopic = $categoryTopic;
         $this->topic = $topic;
         $this->postStatistics = $postStatistics;
+        $this->postStore = $postStore;
+        $this->postStoreImgs = $postStoreImgs;
     }
 
     /**
@@ -1143,5 +1149,166 @@ class PostRepository
         }
     }
 
+    /**
+     * 添加网站内容
+     */
+    public function createStore($startPage, $endPage, $size)
+    {
+        set_time_limit(1800);
+        for($i=$startPage;$i<=$endPage;$i++){
+            $this->addContent($i, $size);
+            usleep(100000);
+        }
+
+}
+
+    /**
+     * 获取网站内容
+     */
+    public function addContent($page, $size)
+    {
+        $lists = $this->getAcFunList($page, $size);
+        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'],
+                    '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(['&nbsp;'],'',$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['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']);
+    }
 }

+ 11 - 0
app/Traits/PostTrait.php

@@ -53,4 +53,15 @@ trait PostTrait
         }
         return $topic;
     }
+
+    public function getAcFunList($page = 1, $size = 10) {
+        $client = new \GuzzleHttp\Client();
+        $response = $client->request('get', "https://webapi.acfun.cn/query/article/list?pageNo={$page}&size={$size}", []);
+        $result = json_decode($response->getBody()->getContents(), true);
+        if(isset($result['data']['articleList'])){
+            return $result['data']['articleList'];
+        }else{
+            return [];
+        }
+    }
 }

+ 31 - 0
app/Transformers/Post/PostStoreTransformer.php

@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 14:08
+ */
+namespace App\Transformers\Post;
+
+use App\Models\PostStore;
+use App\Traits\PostTrait;
+use Illuminate\Support\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class PostStoreTransformer extends TransformerAbstract
+{
+    use PostTrait;
+    public function transform(PostStore $postStore)
+    {
+        return [
+            'id' => $postStore['id'],
+            'source' => $postStore['source'],
+            'title' => $postStore['title'],
+            'content' => subtext($postStore['content'], 20),
+            'img' => $postStore['img'],
+            'is_used' => $postStore['is_used'],
+            'created_at' => Carbon::parse($postStore['created_at'])->toDateTimeString(),
+        ];
+    }
+}

+ 29 - 0
app/Transformers/Post/StoreDetailTransformer.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/10
+ * Time: 9:17
+ */
+
+namespace App\Transformers\Post;
+use App\Models\PostStore;
+use Illuminate\Support\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class StoreDetailTransformer extends  TransformerAbstract
+{
+    public function transform(PostStore $postStore)
+    {
+        return [
+            'id' => $postStore['id'],
+            'source' => $postStore['source'],
+            'title' => $postStore['title'],
+            'content' => $postStore['content'],
+            'img' => $postStore['img'],
+            'imgs' => array_column($postStore->imgs->toArray(), 'img'),
+            'is_used' => $postStore['is_used'],
+            'created_at' => Carbon::parse($postStore['created_at'])->toDateTimeString(),
+        ];
+    }
+}

+ 65 - 0
database/migrations/2019_08_22_113812_create_table_post_store.php

@@ -0,0 +1,65 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateTablePostStore extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('post_store', function (Blueprint $table) {
+            $table->bigIncrements('id');
+
+            $table->string('source', 32)
+                ->default('未知')
+                ->comment('来源');
+
+            $table->integer('source_id')
+                ->default(0)
+                ->comment('来源id');
+
+            $table->string('type', 16)
+                ->default('image')
+                ->comment('类型:image图文,video视频,html富文本');
+
+            $table->string('img', 255)
+                ->comment('主图');
+
+            $table->string('video', 255)
+                ->default('')
+                ->comment('视频');
+
+            $table->string('title', 128)
+                ->default('')
+                ->comment('标题');
+
+            $table->text('content')
+                ->nullable()
+                ->comment('内容');
+
+            $table->tinyInteger('is_used')
+                ->default(0)
+                ->comment('是否使用:0否,1是');
+
+            $table->timestamps();
+
+            $table->index(['source', 'source_id'], 'idx_source_source_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('post_store');
+    }
+}

+ 40 - 0
database/migrations/2019_08_22_113848_create_table_post_store_imgs.php

@@ -0,0 +1,40 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateTablePostStoreImgs extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('post_store_imgs', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            
+            $table->integer('post_store_id')
+                ->index('post_store_id')
+                ->comment('内容仓库id');
+
+            $table->string('img', 255)
+                ->default('')
+                ->comment('图片');
+            
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('post_store_imgs');
+    }
+}

+ 7 - 0
routes/api.php

@@ -73,6 +73,13 @@ $api->version('v1', [
             //日志列表
             $api->get('post/log', 'PostController@log');
 
+            //添加网站内容
+            $api->post('post/store', 'PostController@createStore');
+            //获取网站内容
+            $api->get('post/store', 'PostController@getStore');
+            //网站内容详情
+            $api->get('post/store/detail', 'PostController@getStoreDetail');
+
         });
         $api->group(['namespace' => 'Topic'], function ($api) {
             //重置话题redis