wzq 5 years ago
parent
commit
716eebbeed

+ 11 - 0
app/Http/Controllers/ConfigController.php

@@ -64,6 +64,17 @@ class ConfigController extends Controller
                 '1' => '是',
                 '0' => '否',
             ],
+            //下载类型
+            'download_type' => [
+                'post' => '内容',
+                'post_waste' => '内容回收站',
+            ],
+            //下载状态
+            'download_status' => [
+                '1' => '成功',
+                '0' => '生成中',
+                '2' => '失败',
+            ],
 
         ];
     }

+ 67 - 0
app/Http/Controllers/DownloadController.php

@@ -0,0 +1,67 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/11
+ * Time: 10:39
+ */
+
+namespace App\Http\Controllers;
+
+use App\Repositories\DownloadRepository;
+use App\Transformers\DownloadTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+use League\Fractal\Manager;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection;
+use Illuminate\Validation\Rule;
+
+
+class DownloadController extends Controller
+{
+    public function __construct(DownloadRepository $downloadRepository)
+    {
+        $this->downloadRepository = $downloadRepository;
+    }
+
+    /**
+     * 下载列表
+     */
+    public function index(Request $request)
+    {
+        $productList = $this->downloadRepository->lists($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($productList, new DownloadTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'download_type'
+            ],
+            'columns' => [
+                'id',
+                'username',
+                'download_type',
+                'download_status',
+                'url',
+                'created_at'
+            ]
+        ];
+        return $data;
+    }
+
+    /**
+     * 添加下载
+     */
+    public function create(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'download_type' => ['required',Rule::in('post', 'post_waste')],
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->downloadRepository->create($request->all());
+    }
+}

+ 16 - 0
app/Models/Download.php

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

+ 96 - 0
app/Repositories/DownloadRepository.php

@@ -0,0 +1,96 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/11
+ * Time: 10:45
+ */
+namespace App\Repositories;
+
+use App\Models\Download;
+use Illuminate\Database\QueryException;
+use Dingo\Api\Http\Response;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+use Tymon\JWTAuth\Facades\JWTAuth;
+class DownloadRepository
+{
+    public function __construct(Download $download)
+    {
+        $this->download = $download;
+    }
+
+    /**
+     * 下载列表
+     */
+    public function lists($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+        $where = [];
+        if(isset($request['content'])){
+            $where[] = ['content', 'like', "%{$request['content']}%"];
+        }
+        if(isset($request['download_type'])){
+            $where[] = ['download_type', $request['download_type']];
+        }
+
+        return $this->download
+            ->where($where)
+            ->orderBy('id','desc')
+            ->paginate($perPage);
+    }
+
+    /**
+     * 添加下载
+     */
+    public function create($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;
+
+        //下载中
+        if($this->download->where('uid', $uid)->where('download_status', 0)->where('download_type', $request['download_type'])->exists()){
+            return Response::create([
+                'message'  => '当前有任务未完成,请等待',
+                'status_code'   => 500
+            ]);
+        }
+
+        $data = [
+            'uid' => $uid,
+            'username' => $username,
+            'download_type' => $request['download_type'],
+            'download_status' => 0,
+            'params' => json_encode($request),
+            'url' => ''
+        ];
+
+
+
+        DB::beginTransaction();
+        try{
+            $this->download->create($data);
+
+            DB::commit();
+            return Response::create();
+
+        }catch (QueryException $exception){
+            DB::rollBack();
+            Log::debug('生成下载内容:'.$exception->getMessage());
+            return Response::create([
+                'message'  => '操作失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code'   => 500
+            ]);
+        }
+    }
+}

+ 28 - 0
app/Transformers/DownloadTransformer.php

@@ -0,0 +1,28 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/11
+ * Time: 10:48
+ */
+namespace App\Transformers;
+
+use App\Models\Download;
+use Carbon\Carbon;
+use League\Fractal\TransformerAbstract;
+
+class DownloadTransformer extends TransformerAbstract
+{
+    public function transform(Download $download)
+    {
+        return [
+            'id' => $download['id'],
+            'username' => $download['username'],
+            'download_type' => $download['download_type'],
+            'download_status' => $download['download_status'],
+            'url' => $download['url'],
+            'created_at' => Carbon::parse($download['created_at'])->toDateTimeString(),
+        ];
+    }
+}

+ 55 - 0
database/migrations/2019_06_11_095634_create_table_downloads.php

@@ -0,0 +1,55 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateTableDownloads extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('downloads', function (Blueprint $table) {
+            $table->bigIncrements('id');
+
+            $table->integer('uid')
+                ->unsigned()
+                ->comment('用户uid');
+
+            $table->string('username', 32)
+                ->default('')
+                ->comment('用户昵称');
+
+            $table->string('download_type', 32)
+                ->default('post')
+                ->comment('类型:post内容,post_waste内容回收站');
+
+            $table->tinyInteger('download_status')
+                ->default(0)
+                ->comment('状态:0生成中,1成功,2失败');
+
+            $table->mediumText('params')
+                ->comment('参数');
+
+            $table->string('url', 255)
+                ->default('')
+                ->comment('下载地址');
+
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('downloads');
+    }
+}

+ 2 - 0
resources/lang/zh-CN/validation.php

@@ -116,6 +116,8 @@ return [
         'add_praise_count' => '增加点赞数',
         'add_collect_count' => '增加收藏数',
         'add_share_count' => '增加分享数',
+        'download_type' => '下载类型',
+        'download_status' => '下载状态',
     ],
 
 ];

+ 5 - 0
routes/api.php

@@ -21,6 +21,11 @@ $api->version('v1', [
         //配置
         $api->get('config', 'ConfigController@index');
 
+        //下载列表
+        $api->get('download', 'DownloadController@index');
+        //新增下载
+        $api->post('download', 'DownloadController@create');
+
         $api->group(['namespace' => 'Post'], function ($api) {
             //发布内容
             $api->post('post', 'PostController@create');