作者 赵彬吉

update

@@ -23,7 +23,7 @@ class FileController extends BaseController @@ -23,7 +23,7 @@ class FileController extends BaseController
23 // 上传文件 23 // 上传文件
24 $files = Upload::puts('files', $this->param['config']); 24 $files = Upload::puts('files', $this->param['config']);
25 foreach ($files as &$file){ 25 foreach ($files as &$file){
26 - $file = Storage::disk('upload')->url($file); 26 + $file = Upload::path2url($file);
27 } 27 }
28 return $this->success($files); 28 return $this->success($files);
29 } 29 }
@@ -7,6 +7,7 @@ use App\Exceptions\BsideGlobalException; @@ -7,6 +7,7 @@ use App\Exceptions\BsideGlobalException;
7 use App\Helper\Arr; 7 use App\Helper\Arr;
8 use App\Models\Product\Product; 8 use App\Models\Product\Product;
9 use Illuminate\Foundation\Http\FormRequest; 9 use Illuminate\Foundation\Http\FormRequest;
  10 +use Illuminate\Support\Str;
10 use Illuminate\Validation\Rule; 11 use Illuminate\Validation\Rule;
11 12
12 /** 13 /**
@@ -42,6 +43,9 @@ class ProductRequest extends FormRequest @@ -42,6 +43,9 @@ class ProductRequest extends FormRequest
42 if (empty($v['url'])) { 43 if (empty($v['url'])) {
43 $fail('图片链接不能为空'); 44 $fail('图片链接不能为空');
44 } 45 }
  46 + if (Str::contains($v['url'], env('APP_URL'))) {
  47 + $fail('图片链接不正确');
  48 + }
45 } 49 }
46 }], 50 }],
47 'attrs' => ['required', 'array', function ($attribute, $value, $fail) { 51 'attrs' => ['required', 'array', function ($attribute, $value, $fail) {
@@ -2,7 +2,9 @@ @@ -2,7 +2,9 @@
2 2
3 namespace App\Models\Product; 3 namespace App\Models\Product;
4 4
  5 +
5 use App\Models\Base; 6 use App\Models\Base;
  7 +use App\Services\Facades\Upload;
6 use Illuminate\Database\Eloquent\SoftDeletes; 8 use Illuminate\Database\Eloquent\SoftDeletes;
7 9
8 class Category extends Base 10 class Category extends Base
@@ -12,4 +14,13 @@ class Category extends Base @@ -12,4 +14,13 @@ class Category extends Base
12 //设置关联表名 14 //设置关联表名
13 protected $table = 'gl_product_category'; 15 protected $table = 'gl_product_category';
14 16
  17 + public function getImageAttribute($value)
  18 + {
  19 + return Upload::path2url($value);
  20 + }
  21 +
  22 + public function setImageAttribute($value)
  23 + {
  24 + $this->attributes['image'] = Upload::url2path($value);
  25 + }
15 } 26 }
@@ -4,6 +4,7 @@ namespace App\Models\Product; @@ -4,6 +4,7 @@ namespace App\Models\Product;
4 4
5 use App\Helper\Arr; 5 use App\Helper\Arr;
6 use App\Models\Base; 6 use App\Models\Base;
  7 +use App\Services\Facades\Upload;
7 use Illuminate\Database\Eloquent\SoftDeletes; 8 use Illuminate\Database\Eloquent\SoftDeletes;
8 9
9 class Product extends Base 10 class Product extends Base
@@ -27,19 +28,29 @@ class Product extends Base @@ -27,19 +28,29 @@ class Product extends Base
27 } 28 }
28 29
29 public function setThumbAttribute($value){ 30 public function setThumbAttribute($value){
  31 + $value['url'] = Upload::url2path($value['url']);
30 $this->attributes['thumb'] = Arr::a2s($value); 32 $this->attributes['thumb'] = Arr::a2s($value);
31 } 33 }
32 34
33 public function getThumbAttribute($value){ 35 public function getThumbAttribute($value){
34 - return Arr::s2a($value); 36 + $value = Arr::s2a($value);
  37 + $value['url'] = Upload::path2url($value['url']);
  38 + return $value;
35 } 39 }
36 40
37 public function setGalleryAttribute($value){ 41 public function setGalleryAttribute($value){
  42 + foreach ($value as &$v){
  43 + $v['url'] = Upload::url2path($v['url']);
  44 + }
38 $this->attributes['gallery'] = Arr::a2s($value); 45 $this->attributes['gallery'] = Arr::a2s($value);
39 } 46 }
40 47
41 public function getGalleryAttribute($value){ 48 public function getGalleryAttribute($value){
42 - return Arr::s2a($value); 49 + $value = Arr::s2a($value);
  50 + foreach ($value as &$v){
  51 + $v['url'] = Upload::path2url($v['url']);
  52 + }
  53 + return $value;
43 } 54 }
44 55
45 public function setAttrsAttribute($value){ 56 public function setAttrsAttribute($value){
@@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Facade; @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Facade;
10 * @method static array puts(string $input_name="file", string|array $config="default") 批量获取上传的文件 10 * @method static array puts(string $input_name="file", string|array $config="default") 批量获取上传的文件
11 * @method static array filePut(string $filename, string $content, string|array $config="default") 11 * @method static array filePut(string $filename, string $content, string|array $config="default")
12 * @method static string url2path(string $url, string|array $disk="upload") 12 * @method static string url2path(string $url, string|array $disk="upload")
  13 + * @method static string path2url(string $path, string|array $disk="upload")
13 */ 14 */
14 class Upload extends Facade 15 class Upload extends Facade
15 { 16 {
@@ -260,4 +260,16 @@ class UploadService extends BaseService @@ -260,4 +260,16 @@ class UploadService extends BaseService
260 $upload_url = config('filesystems')['disks'][$disk]['url']; 260 $upload_url = config('filesystems')['disks'][$disk]['url'];
261 return str_replace($upload_url . '/', '', $url); 261 return str_replace($upload_url . '/', '', $url);
262 } 262 }
  263 +
  264 + /**
  265 + * 本地路径转链接
  266 + * @param $path
  267 + * @param string $disk
  268 + * @return string
  269 + * @author zbj
  270 + * @date 2023/4/20
  271 + */
  272 + public function path2url($path, $disk = 'upload'){
  273 + return Storage::disk('upload')->url($path);
  274 + }
263 } 275 }