xielin 6 年之前
父节点
当前提交
fafe5edf09
共有 8 个文件被更改,包括 126 次插入6 次删除
  1. 1 0
      .gitignore
  2. 36 0
      app/Http/Controllers/UploadController.php
  3. 1 0
      bootstrap/app.php
  4. 3 2
      composer.json
  5. 1 1
      config/api.php
  6. 82 0
      config/filesystems.php
  7. 1 1
      config/laravels.php
  8. 1 2
      routes/api.php

+ 1 - 0
.gitignore

@@ -5,3 +5,4 @@ Homestead.yaml
 .env
 /storage/*
 storage/laravels.json
+composer.lock

+ 36 - 0
app/Http/Controllers/UploadController.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Storage;
+
+class UploadController extends Controller
+{
+    public function uploadImage(Request $request)
+    {
+        if($request->hasFile('image') && $request->file('image')->isValid()){
+            $path = $request->get('path') ? $request->get('path').'/' : date('Ym').'/';
+            //获取文件的原文件名 包括扩展名
+//            $yuanname= $request->file('image')->getClientOriginalName();
+//            //获取文件的扩展名
+            $kuoname=$request->file('image')->getClientOriginalExtension();
+//            //获取文件的类型
+//            $type=$request->file('image')->getClientMimeType();
+//            //获取文件的绝对路径,但是获取到的在本地不能打开
+            $filePath=$request->file('image')->getRealPath();
+            //要保存的文件名 时间+扩展名
+            $filename=time() . '_' . uniqid() .'.'.$kuoname;
+            $imageUrl = Storage::put($path.$filename, file_get_contents($filePath));
+            if($imageUrl){
+                return [
+                    'data' => ['url' => $path.$filename]
+                ];
+            }else{
+                return $this->response->error('图片上传失败,请重试', 500);
+            }
+        }else{
+            return $this->response->error('图片上传失败,请重试', 500);
+        }
+    }
+}

+ 1 - 0
bootstrap/app.php

@@ -81,6 +81,7 @@ $app->register(App\Providers\AuthServiceProvider::class);
 
 $app->register(Dingo\Api\Provider\LumenServiceProvider::class);
 $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
+$app->register(\Junliuxian\AliOSS\AliOssServiceProvider::class);
 /*
 |--------------------------------------------------------------------------
 | Load The Application Routes

+ 3 - 2
composer.json

@@ -8,9 +8,10 @@
         "php": ">=7.1.3",
         "dingo/api": "^2.2",
         "hhxsv5/laravel-s": "~3.4.0",
+        "junliuxian/ali-oss-storage": "~2.0",
         "laravel/lumen-framework": "5.8.*",
-        "vlucas/phpdotenv": "^3.3",
-        "tymon/jwt-auth": "1.0.0-rc.4.1"
+        "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {
         "fzaninotto/faker": "^1.4",

+ 1 - 1
config/api.php

@@ -57,7 +57,7 @@ return [
     |
     */
 
-    'prefix' => env('API_PREFIX', 'api'),
+    'prefix' => env('API_PREFIX', '/'),
 
     /*
     |--------------------------------------------------------------------------

+ 82 - 0
config/filesystems.php

@@ -0,0 +1,82 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | Default Filesystem Disk
+    |--------------------------------------------------------------------------
+    |
+    | Here you may specify the default filesystem disk that should be used
+    | by the framework. The "local" disk, as well as a variety of cloud
+    | based disks are available to your application. Just store away!
+    |
+    */
+
+    'default' => env('FILESYSTEM_DRIVER', 'oss'),
+
+    /*
+    |--------------------------------------------------------------------------
+    | Default Cloud Filesystem Disk
+    |--------------------------------------------------------------------------
+    |
+    | Many applications store files both locally and in the cloud. For this
+    | reason, you may specify a default "cloud" driver here. This driver
+    | will be bound as the Cloud disk implementation in the container.
+    |
+    */
+
+    'cloud' => env('FILESYSTEM_CLOUD', 's3'),
+
+    /*
+    |--------------------------------------------------------------------------
+    | Filesystem Disks
+    |--------------------------------------------------------------------------
+    |
+    | Here you may configure as many filesystem "disks" as you wish, and you
+    | may even configure multiple disks of the same driver. Defaults have
+    | been setup for each driver as an example of the required options.
+    |
+    | Supported Drivers: "local", "ftp", "sftp", "s3", "rackspace"
+    |
+    */
+
+    'disks' => [
+
+        'local' => [
+            'driver' => 'local',
+            'root' => storage_path('app'),
+        ],
+
+        'public' => [
+            'driver' => 'local',
+            'root' => storage_path('app/public'),
+            'url' => env('APP_URL') . '/storage',
+            'visibility' => 'public',
+        ],
+
+        's3' => [
+            'driver' => 's3',
+            'key' => env('AWS_ACCESS_KEY_ID'),
+            'secret' => env('AWS_SECRET_ACCESS_KEY'),
+            'region' => env('AWS_DEFAULT_REGION'),
+            'bucket' => env('AWS_BUCKET'),
+            'url' => env('AWS_URL'),
+        ],
+
+        'oss' => [
+            'driver' => 'oss',
+            'access_id' => 'LTAIGTDKUOVQf2Ln',
+            'access_key' => 'HzY9r2gDPbURQ0Vp69A7THV0RmxMkb',
+            'bucket' => 'rainbowstar',
+            'endpoint' => 'oss-cn-zhangjiakou.aliyuncs.com', // OSS 外网节点或自定义外部域名
+//            'endpoint_internal' => '<internal endpoint [OSS内网节点] 如:oss-cn-shenzhen-internal.aliyuncs.com>', // v2.0.4 新增配置属性,如果为空,则默认使用 endpoint 配置(由于内网上传有点小问题未解决,请大家暂时不要使用内网节点上传,正在与阿里技术沟通中)
+            'cdnDomain' => '', // 如果isCName为true, getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn
+            'ssl' => false, // true to use 'https://' and false to use 'http://'. default is false,
+            'isCName' => false, // 是否使用自定义域名,true: 则Storage.url()会使用自定义的cdn或域名生成文件url, false: 则使用外部节点生成url
+            'debug' => true,
+        ],
+
+    ],
+
+];

+ 1 - 1
config/laravels.php

@@ -8,7 +8,7 @@ return [
     'listen_port'              => env('LARAVELS_LISTEN_PORT', 18207),
     'socket_type'              => defined('SWOOLE_SOCK_TCP') ? SWOOLE_SOCK_TCP : 1,
     'enable_coroutine_runtime' => false,
-    'server'                   => env('LARAVELS_SERVER', 'admin-manage'),
+    'server'                   => env('LARAVELS_SERVER', 'config-manage'),
     'handle_static'            => env('LARAVELS_HANDLE_STATIC', false),
     'laravel_base_path'        => env('LARAVEL_BASE_PATH', base_path()),
     'inotify_reload'           => [

+ 1 - 2
routes/api.php

@@ -16,8 +16,7 @@ $api = app('Dingo\Api\Routing\Router');
 $api->version('v1', [
     'namespace' => 'App\Http\Controllers',
 ], function ($api) {
-    //登录
-    $api->post('login', 'AuthController@authenticate');
+    $api->post('upload', 'UploadController@uploadImage');
     $api->group(['middleware' => 'auth:api'], function ($api) {
 
         //注册