Browse Source

视频分类

moshaorui 5 months ago
parent
commit
422cd8bc21

+ 7 - 0
.env.dev

@@ -56,3 +56,10 @@ VITE_PUSHER_HOST="${PUSHER_HOST}"
 VITE_PUSHER_PORT="${PUSHER_PORT}"
 VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
 VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
+
+
+OSS_ACCESS_KEY_ID=LTAI5tCYd6zdBrSN9W9hZRUd
+OSS_ACCESS_KEY_SECRET=q0pp2qlDXdxi490qMDbDuWBd7SPMNn
+OSS_BUCKET=mietubl-dev
+OSS_ENDPOINT=oss-cn-hongkong.aliyuncs.com
+

+ 18 - 14
app/Admin/Controllers/BaseProductController.php

@@ -7,6 +7,7 @@ use Dcat\Admin\Admin;
 use Dcat\Admin\Form;
 use Dcat\Admin\Form\NestedForm;
 use Dcat\Admin\Grid;
+use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
 use App\Admin\Repositories\BaseProductCategory;
@@ -15,6 +16,14 @@ use Illuminate\Http\Request;
 use App\Libraries\CommonHelper;
 class BaseProductController extends AdminController
 {
+    public function index(Content $content)
+    {
+        return $content
+            ->header(' Product Management')
+            ->description('all')
+            ->breadcrumb(['text'=>'Product Management','url'=>''])
+            ->body($this->grid());
+    }
 
     /**
      * Make a grid builder.
@@ -32,7 +41,7 @@ class BaseProductController extends AdminController
             $grid->column('images')->display(function ($images) {
                 $images = $images->toArray();
                 $dataImages = array_column($images, 'image_url');
-                return CommonHelper::displayImage($dataImages,80);
+                return CommonHelper::displayImage($dataImages,150);
             });
             $grid->column('order')->orderable();
             $grid->column('is_pinned')->switch();
@@ -131,28 +140,23 @@ class BaseProductController extends AdminController
                 ->sortable() // 可拖动排序
                 ->removable() // 可移除图片
                 ->autoUpload() // 自动上传
+                ->uniqueName()
+                ->accept('jpg,png,gif,jpeg,webp')
+                ->move('images/product/'.date("Ymd"))
                 ->customFormat(function () {
-                    // 数据格式化为数组['1.jpg','2.jpg']
+                    // 数据格式化为数组['1.jpg','2.jpg'] 编辑时用到
                     return array_column($this->images, 'image_url');
                 })
-                ->saving(function ($paths) {
-                    $paths =  array_column($paths, 'id');
-                    return $paths;
+                ->saving(function ($images) {
+                    return array_map(function($image) {
+                        return ['image_url' => $image];
+                    }, $images);
                 });
             $form->editor('content');
             $form->switch('is_pinned')->default(0);
             $form->switch('enabled')->default(1);
             //插入JS
             $this->addParametersJs();
-            //保存后执行
-            $form->saved(function (Form $form) {
-                $productId =  $form->getKey();
-                $images = explode(',',$form->input('images'));
-                //删除旧的图片记录
-                BaseProductImage::deleteByProductId($productId);
-                //插入新的图片记录
-                BaseProductImage::saveProductImages($productId, $images);
-            });
         });
     }
 

+ 12 - 0
app/Admin/Controllers/BaseVideoController.php

@@ -5,6 +5,7 @@ namespace App\Admin\Controllers;
 use App\Admin\Repositories\BaseProductCategory;
 use App\Admin\Repositories\BaseVideo;
 use App\Admin\Repositories\BaseVideoCategory;
+use App\Libraries\CommonHelper;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
@@ -36,6 +37,11 @@ class BaseVideoController extends AdminController
             $grid->column('id')->sortable();
             $grid->column('title');
             $grid->column('base_video_category.name','Category Name');
+            $grid->column('cover_image')->display(function ($image) {
+                // 开始生成 HTML
+                $dataImages = [$image];
+                return CommonHelper::displayImage($dataImages,150);
+            });
             $grid->column('order')->orderable();
             $grid->column('is_pinned')->switch();
             $grid->column('enabled')->switch();
@@ -71,6 +77,11 @@ class BaseVideoController extends AdminController
             $show->field('id');
             $show->field('title');
             $show->field('base_video_category.name','Category Name');
+            $show->field('cover_image')->as(function ($image) {
+                // 开始生成 HTML
+                $dataImages = [$image];
+                return CommonHelper::displayImage($dataImages,150);
+            })->unescape();
             $show->html(function () {
                 $content = $this->video_url;
                 return view('admin::show.field', [
@@ -114,6 +125,7 @@ class BaseVideoController extends AdminController
             $form->select('category_id', 'Category Name')
                 ->options(BaseVideoCategory::selectOptions())
                 ->required();
+            $form->image("cover_image")->autoUpload();//
             $form->url("video_url")->required();
             $form->editor('remark');
             $form->switch('is_pinned')->default(0);

+ 74 - 0
app/Admin/Controllers/TestController.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Admin\Controllers;
+
+use App\Admin\Repositories\Test;
+use Dcat\Admin\Form;
+use Dcat\Admin\Grid;
+use Dcat\Admin\Show;
+use Dcat\Admin\Http\Controllers\AdminController;
+use Carbon\Carbon;
+
+class TestController extends AdminController
+{
+    /**
+     * Make a grid builder.
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+
+
+        return Grid::make(new Test(), function (Grid $grid) {
+            $grid->column('id')->sortable();
+            $grid->column('name');
+            $grid->column('created_at');
+            $grid->column('updated_at')->sortable();
+
+            $grid->filter(function (Grid\Filter $filter) {
+                $filter->equal('id');
+
+            });
+        });
+    }
+
+    /**
+     * Make a show builder.
+     *
+     * @param mixed $id
+     *
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        return Show::make($id, new Test(), function (Show $show) {
+            $show->field('id');
+            $show->field('name');
+            $show->field('created_at');
+            $show->field('updated_at');
+        });
+    }
+
+    /**
+     * Make a form builder.
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        return Form::make(new Test(), function (Form $form) {
+            $form->display('id');
+
+            $form->multipleImage('name')->saving(function ($paths) {
+                return json_encode($paths);
+            })->move('images/product/'.date("Ymd").'/')
+                ->uniqueName()
+                ->autoUpload()
+                ->accept('jpg,png,gif,jpeg,webp');
+
+            $form->display('created_at');
+            $form->display('updated_at');
+        });
+    }
+}

+ 12 - 2
app/Libraries/CommonHelper.php

@@ -8,19 +8,29 @@ class CommonHelper
 
     /*
      * $images 格式:['image.jpg','image2.jpg']
-     * 返回显示的HTML
+     * 返回显示的HTML显示图片
      */
     public static function displayImage($images,$size=150)
     {
+        //默认用等比例缩放
+        $process = "?x-oss-process=image/resize,h_{$size},m_lfit";
         $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
         foreach ($images as $image) {
             $html .= "<div style='flex: 1 0 {$size}px; max-width: {$size}px; max-height: {$size}px; padding: 5px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
-                    <img  data-action='preview-img' src='" . asset('storage/' . $image) . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
+                    <img  data-action='preview-img'   src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
                   </div>";
         }
         $html .= '</div>';
         return $html;
     }
 
+    /*
+     * 返回oss的url
+     */
+    public static function ossUrl($image)
+    {
+        return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
+    }
+
 }
 

+ 3 - 0
app/Models/BaseProductImage.php

@@ -14,6 +14,9 @@ class BaseProductImage extends Model
         'created_at' => 'datetime:Y-m-d H:i:s',
         'updated_at' => 'datetime:Y-m-d H:i:s',
     ];
+    protected $fillable = [
+        'image_url',
+        'product_id'];
 
     // 反向关联,属于某个产品
     public function product()

+ 19 - 0
app/Models/Test.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Models;
+
+use Dcat\Admin\Traits\HasDateTimeFormatter;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+
+class Test extends Model
+{
+	use HasDateTimeFormatter;
+    protected $table = 'test';
+
+    public function __construct()
+    {
+        parent::__construct();
+    }
+}

+ 1 - 0
composer.json

@@ -6,6 +6,7 @@
     "license": "MIT",
     "require": {
         "php": "^8.0.2",
+        "alphasnow/aliyun-oss-laravel": "^4.7",
         "dcat-plus/laravel-admin": "^1.2",
         "guzzlehttp/guzzle": "^7.2",
         "laravel/framework": "^9.19",

+ 184 - 1
composer.lock

@@ -4,8 +4,191 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "3b60529e5647bb0eea71d8652f3498f4",
+    "content-hash": "844cf77b80234d7e06f624e36d3e2e9c",
     "packages": [
+        {
+            "name": "aliyuncs/oss-sdk-php",
+            "version": "v2.7.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/aliyun/aliyun-oss-php-sdk.git",
+                "reference": "ce5d34dae9868237a32248788ea175c7e9da14b1"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/aliyun/aliyun-oss-php-sdk/zipball/ce5d34dae9868237a32248788ea175c7e9da14b1",
+                "reference": "ce5d34dae9868237a32248788ea175c7e9da14b1",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3"
+            },
+            "require-dev": {
+                "php-coveralls/php-coveralls": "*",
+                "phpunit/phpunit": "*"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "OSS\\": "src/OSS"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Aliyuncs",
+                    "homepage": "http://www.aliyun.com"
+                }
+            ],
+            "description": "Aliyun OSS SDK for PHP",
+            "homepage": "http://www.aliyun.com/product/oss/",
+            "support": {
+                "issues": "https://github.com/aliyun/aliyun-oss-php-sdk/issues",
+                "source": "https://github.com/aliyun/aliyun-oss-php-sdk/tree/v2.7.1"
+            },
+            "time": "2024-02-28T11:22:18+00:00"
+        },
+        {
+            "name": "alphasnow/aliyun-oss-flysystem",
+            "version": "3.3.4",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/alphasnow/aliyun-oss-flysystem.git",
+                "reference": "b1712d9fcadb323f6286b40226887d3ea1ce339b"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/alphasnow/aliyun-oss-flysystem/zipball/b1712d9fcadb323f6286b40226887d3ea1ce339b",
+                "reference": "b1712d9fcadb323f6286b40226887d3ea1ce339b",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "aliyuncs/oss-sdk-php": "^2.5",
+                "league/flysystem": "^3.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "^3.6",
+                "mockery/mockery": "^1.5",
+                "php-coveralls/php-coveralls": "*",
+                "phpstan/phpstan": "^1.4",
+                "phpunit/phpunit": "^9.5",
+                "vlucas/phpdotenv": "^5.4"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "AlphaSnow\\Flysystem\\Aliyun\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Alpha Snow",
+                    "email": "wind91@foxmail.com"
+                }
+            ],
+            "description": "Flysystem adapter for the Aliyun storage",
+            "homepage": "https://alphasnow.github.io/aliyun-oss-flysystem/",
+            "keywords": [
+                "adapter",
+                "aliyun",
+                "filesystem",
+                "oss"
+            ],
+            "support": {
+                "issues": "https://github.com/alphasnow/aliyun-oss-flysystem/issues",
+                "source": "https://github.com/alphasnow/aliyun-oss-flysystem/tree/3.3.4"
+            },
+            "time": "2024-02-02T03:44:02+00:00"
+        },
+        {
+            "name": "alphasnow/aliyun-oss-laravel",
+            "version": "4.7.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/alphasnow/aliyun-oss-laravel.git",
+                "reference": "df66d22f7262a8f98792cb864302cc1c40108d07"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/alphasnow/aliyun-oss-laravel/zipball/df66d22f7262a8f98792cb864302cc1c40108d07",
+                "reference": "df66d22f7262a8f98792cb864302cc1c40108d07",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "alphasnow/aliyun-oss-flysystem": "^3.3",
+                "php": "^8.0.2"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "^3.12",
+                "mockery/mockery": "^1.5",
+                "orchestra/testbench": "^7.13",
+                "php-coveralls/php-coveralls": "*",
+                "phpstan/phpstan": "^1.9",
+                "phpunit/phpunit": "^9.5"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "AlphaSnow\\LaravelFilesystem\\Aliyun\\AliyunServiceProvider"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "AlphaSnow\\LaravelFilesystem\\Aliyun\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "alphasnow",
+                    "email": "wind91@foxmail.com"
+                }
+            ],
+            "description": "alibaba cloud object storage service for laravel",
+            "homepage": "https://alphasnow.github.io/aliyun-oss-laravel/",
+            "keywords": [
+                "aliyun",
+                "filesystems",
+                "laravel",
+                "oss",
+                "storage"
+            ],
+            "support": {
+                "issues": "https://github.com/alphasnow/aliyun-oss-laravel/issues",
+                "source": "https://github.com/alphasnow/aliyun-oss-laravel/tree/4.7.2"
+            },
+            "time": "2024-02-02T03:57:44+00:00"
+        },
         {
             "name": "box/spout",
             "version": "v3.3.0",

+ 1 - 1
config/admin.php

@@ -271,7 +271,7 @@ return [
     'upload' => [
 
         // Disk in `config/filesystem.php`.
-        'disk' => 'public',
+        'disk' => 'oss',
 
         // Image and file upload path under the disk above.
         'directory' => [

+ 19 - 0
config/filesystems.php

@@ -56,6 +56,25 @@ return [
             'throw' => false,
         ],
 
+        "oss" => [
+            "driver"            => "oss",
+            "access_key_id"     => env("OSS_ACCESS_KEY_ID"),           // 必填, 阿里云的AccessKeyId
+            "access_key_secret" => env("OSS_ACCESS_KEY_SECRET"),       // 必填, 阿里云的AccessKeySecret
+            "bucket"            => env("OSS_BUCKET"),                  // 必填, 对象存储的Bucket, 示例: my-bucket
+            "endpoint"          => env("OSS_ENDPOINT"),                // 必填, 对象存储的Endpoint, 示例: oss-cn-shanghai.aliyuncs.com
+
+            "internal"          => env("OSS_INTERNAL", null),          // 选填, 内网上传地址,填写即启用 示例: oss-cn-shanghai-internal.aliyuncs.com
+            "domain"            => env("OSS_DOMAIN", null),            // 选填, 绑定域名,填写即启用 示例: oss.my-domain.com
+            "is_cname"          => env("OSS_CNAME", false),            // 选填, 若Endpoint为自定义域名,此项要为true,见:https://github.com/aliyun/aliyun-oss-php-sdk/blob/572d0f8e099e8630ae7139ed3fdedb926c7a760f/src/OSS/OssClient.php#L113C1-L122C78
+            "prefix"            => env("OSS_PREFIX", ""),              // 选填, 统一存储地址前缀
+            "use_ssl"           => env("OSS_SSL", false),              // 选填, 是否使用HTTPS
+            "throw"             => env("OSS_THROW", true),            // 选填, 是否抛出引起错误的异常,默认出现错误时,不抛出异常仅返回false
+            "signatureVersion"  => env("OSS_SIGNATURE_VERSION", "v1"), // 选填, 选择使用v1或v4签名版本
+            "region"            => env("OSS_REGION", ""),              // 选填, 仅在使用v4签名版本时启用, 示例: cn-shanghai
+            "options"           => [],                                 // 选填, 添加全局配置参数, 示例: [\OSS\OssClient::OSS_CHECK_MD5 => false]
+            "macros"            => [],                                  // 选填, 添加自定义Macro, 示例: [\App\Macros\ListBuckets::class, \App\Macros\CreateBucket::class]
+        ],
+
     ],
 
     /*