10 changed files with 222 additions and 7 deletions
-
6app/Constants/v3/ErrorCode.php
-
13app/Model/v3/Goods.php
-
12app/Model/v3/GoodsActivity.php
-
87app/Service/v3/Implementations/AttachmentService.php
-
6app/Service/v3/Implementations/WxLoginService.php
-
25app/Service/v3/Interfaces/AttachmentServiceInterface.php
-
3composer.json
-
75composer.lock
-
1config/autoload/dependencies.php
-
1config/config.php
@ -0,0 +1,87 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Service\v3\Implementations; |
||||
|
|
||||
|
use App\Constants\v3\ErrorCode; |
||||
|
use App\Service\v3\Interfaces\AttachmentServiceInterface; |
||||
|
use League\Flysystem\FilesystemNotFoundException; |
||||
|
|
||||
|
class AttachmentService implements AttachmentServiceInterface |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @inheritDoc |
||||
|
*/ |
||||
|
public function formUpload($file, $path, $filesystem, $attachmenttype = 'image') |
||||
|
{ |
||||
|
|
||||
|
$fileRealPath = $file->getRealPath(); |
||||
|
$fileHash = md5_file($fileRealPath); |
||||
|
|
||||
|
$path = $this->getBasePath($path, $attachmenttype); |
||||
|
$fileName = $path . '/' . $fileHash . '.' . $file->getExtension(); |
||||
|
|
||||
|
$stream = fopen($fileRealPath, 'r+'); |
||||
|
$filesystem->writeStream($fileName, $stream); |
||||
|
fclose($stream); |
||||
|
|
||||
|
return $fileName; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @inheritDoc |
||||
|
*/ |
||||
|
public function base64Upload($contents, $path, $filesystem) |
||||
|
{ |
||||
|
|
||||
|
preg_match('/^(data:\s*image\/(\w+);base64,)/', $contents, $result); |
||||
|
if (empty($result)) { |
||||
|
throw new FilesystemNotFoundException(ErrorCode::getMessage(ErrorCode::UPLOAD_INVALID),ErrorCode::UPLOAD_INVALID); |
||||
|
} |
||||
|
|
||||
|
$contents = base64_decode(str_replace($result[1], '', $contents)); |
||||
|
|
||||
|
$fileHash = md5($contents); |
||||
|
$path = $this->getBasePath($path); |
||||
|
$fileName = $path . '/' . $fileHash . '.' . $result[2]; |
||||
|
|
||||
|
$filesystem->write($fileName, $contents); |
||||
|
|
||||
|
return $fileName; |
||||
|
} |
||||
|
|
||||
|
protected function getBasePath($path, $attachmenttype = 'image') |
||||
|
{ |
||||
|
switch ($attachmenttype) { |
||||
|
case 'image': |
||||
|
$baseDir = env('IMAGE_BASE', '/attachment/images'); |
||||
|
break; |
||||
|
|
||||
|
case 'file': |
||||
|
$baseDir = env('FILES_BASE', '/attachment/files'); |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
$baseDir = env('FILES_BASE', '/attachment'); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
$path = $path ? '/'.$path : ''; |
||||
|
$path .= '/'.date('Y').'/'.date('m').'/'.date('d'); |
||||
|
return $baseDir.$path; |
||||
|
} |
||||
|
|
||||
|
public function switchImgToAliOss($path, $bucket = 'thumbnail_q50') |
||||
|
{ |
||||
|
if (strpos($path, 'http') === false || strpos($path, 'https') === false) { |
||||
|
$path = config('alioss.img_host') . '/' . $path; |
||||
|
} else { |
||||
|
$temp = explode('//', $path); |
||||
|
$temp = explode('/', $temp[1]); |
||||
|
unset($temp[0]); |
||||
|
$path = config('alioss.img_host') . '/' . implode('/', $temp); |
||||
|
} |
||||
|
return $path . '!' . $bucket; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service\v3\Interfaces; |
||||
|
|
||||
|
interface AttachmentServiceInterface |
||||
|
{ |
||||
|
/** |
||||
|
* 表单上传,单文件 |
||||
|
* @param $file |
||||
|
* @param $path |
||||
|
* @param $filesystem |
||||
|
* @param string $attachmenttype |
||||
|
*/ |
||||
|
public function formUpload($file, $path, $filesystem, $attachmenttype = 'image'); |
||||
|
|
||||
|
/** |
||||
|
* base64code上传,单文件 |
||||
|
* @param $contents |
||||
|
* @param $path |
||||
|
* @param $filesystem |
||||
|
*/ |
||||
|
public function base64Upload($contents, $path, $filesystem); |
||||
|
|
||||
|
public function switchImgToAliOss($path, $bucket = ''); |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue