作者 赵彬吉

update

... ... @@ -23,7 +23,7 @@ class FileController extends BaseController
// 上传文件
$files = Upload::puts('files', $this->param['config']);
foreach ($files as &$file){
$file = Storage::disk('upload')->url($file);
$file = Upload::path2url($file);
}
return $this->success($files);
}
... ...
... ... @@ -7,6 +7,7 @@ use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Models\Product\Product;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
/**
... ... @@ -42,6 +43,9 @@ class ProductRequest extends FormRequest
if (empty($v['url'])) {
$fail('图片链接不能为空');
}
if (Str::contains($v['url'], env('APP_URL'))) {
$fail('图片链接不正确');
}
}
}],
'attrs' => ['required', 'array', function ($attribute, $value, $fail) {
... ...
... ... @@ -2,7 +2,9 @@
namespace App\Models\Product;
use App\Models\Base;
use App\Services\Facades\Upload;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Base
... ... @@ -12,4 +14,13 @@ class Category extends Base
//设置关联表名
protected $table = 'gl_product_category';
public function getImageAttribute($value)
{
return Upload::path2url($value);
}
public function setImageAttribute($value)
{
$this->attributes['image'] = Upload::url2path($value);
}
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Models\Product;
use App\Helper\Arr;
use App\Models\Base;
use App\Services\Facades\Upload;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Base
... ... @@ -27,19 +28,29 @@ class Product extends Base
}
public function setThumbAttribute($value){
$value['url'] = Upload::url2path($value['url']);
$this->attributes['thumb'] = Arr::a2s($value);
}
public function getThumbAttribute($value){
return Arr::s2a($value);
$value = Arr::s2a($value);
$value['url'] = Upload::path2url($value['url']);
return $value;
}
public function setGalleryAttribute($value){
foreach ($value as &$v){
$v['url'] = Upload::url2path($v['url']);
}
$this->attributes['gallery'] = Arr::a2s($value);
}
public function getGalleryAttribute($value){
return Arr::s2a($value);
$value = Arr::s2a($value);
foreach ($value as &$v){
$v['url'] = Upload::path2url($v['url']);
}
return $value;
}
public function setAttrsAttribute($value){
... ...
... ... @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Facade;
* @method static array puts(string $input_name="file", string|array $config="default") 批量获取上传的文件
* @method static array filePut(string $filename, string $content, string|array $config="default")
* @method static string url2path(string $url, string|array $disk="upload")
* @method static string path2url(string $path, string|array $disk="upload")
*/
class Upload extends Facade
{
... ...
... ... @@ -260,4 +260,16 @@ class UploadService extends BaseService
$upload_url = config('filesystems')['disks'][$disk]['url'];
return str_replace($upload_url . '/', '', $url);
}
/**
* 本地路径转链接
* @param $path
* @param string $disk
* @return string
* @author zbj
* @date 2023/4/20
*/
public function path2url($path, $disk = 'upload'){
return Storage::disk('upload')->url($path);
}
}
... ...