12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?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)
- {
- Log::debug('添加下载数据:'.json_encode($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
- ]);
- }
- }
- }
|