From 5b37d81626c4de126d9aefd3af6ab380f9002318 Mon Sep 17 00:00:00 2001 From: weigang Date: Mon, 7 Sep 2020 21:39:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=95=86=E5=93=81=E5=9B=BE?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Constants/v3/ErrorCode.php | 6 ++ app/Model/v3/Goods.php | 13 +-- app/Model/v3/GoodsActivity.php | 12 +++ .../v3/Implementations/AttachmentService.php | 87 +++++++++++++++++++ .../v3/Implementations/WxLoginService.php | 6 ++ .../Interfaces/AttachmentServiceInterface.php | 25 ++++++ composer.json | 3 +- composer.lock | 75 +++++++++++++++- config/autoload/dependencies.php | 1 + config/config.php | 1 + 10 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 app/Service/v3/Implementations/AttachmentService.php create mode 100644 app/Service/v3/Interfaces/AttachmentServiceInterface.php diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index faa2df5..0c2605f 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -156,6 +156,12 @@ class ErrorCode extends AbstractConstants */ const SMS_SEND_FAILURE = 1003; + /** + * 文件上传失败 + * @Message("文件上传失败") + */ + const UPLOAD_INVALID = 1004; + /************************************/ /* 商品相关 1101-1150 */ /************************************/ diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index 84daf35..352fb93 100644 --- a/app/Model/v3/Goods.php +++ b/app/Model/v3/Goods.php @@ -5,6 +5,7 @@ namespace App\Model\v3; use App\Constants\v3\SsdbKeys; use App\Model\Model; +use App\Service\v3\Interfaces\AttachmentServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface; use App\TaskWorker\SSDBTask; use Hyperf\Database\Model\Builder; @@ -26,6 +27,12 @@ class Goods extends Model */ protected $shopCartService; + /** + * @Inject + * @var AttachmentServiceInterface + */ + protected $attachmentService; + /** * The table associated with the model. * @@ -82,11 +89,7 @@ class Goods extends Model public function getCoverImgAttribute($value) { - if(strripos($value,"http") === false){ - return config('alioss.img_host').'/'.$value; - }else{ - return $value; - } + return $this->attachmentService->switchImgToAliOss($value); } public function getMonthSalesAttribute() diff --git a/app/Model/v3/GoodsActivity.php b/app/Model/v3/GoodsActivity.php index df2f6a2..e523d6f 100644 --- a/app/Model/v3/GoodsActivity.php +++ b/app/Model/v3/GoodsActivity.php @@ -5,6 +5,7 @@ namespace App\Model\v3; use App\Constants\v3\Goods as GoodsConstants; use App\Constants\v3\SsdbKeys; use App\Model\Model; +use App\Service\v3\Interfaces\AttachmentServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface; use App\TaskWorker\SSDBTask; use Hyperf\Database\Model\Builder; @@ -23,6 +24,12 @@ class GoodsActivity extends Model */ protected $shopCartService; + /** + * @Inject + * @var AttachmentServiceInterface + */ + protected $attachmentService; + protected $table = 'lanzu_goods_activity'; protected $casts = [ @@ -78,6 +85,11 @@ class GoodsActivity extends Model return $this->attributes['expire_time'] - time(); } + public function getCoverImgAttribute($value) + { + return $this->attachmentService->switchImgToAliOss($value); + } + public function store() { return $this->belongsTo(Store::class, 'store_id', 'id'); diff --git a/app/Service/v3/Implementations/AttachmentService.php b/app/Service/v3/Implementations/AttachmentService.php new file mode 100644 index 0000000..9a82593 --- /dev/null +++ b/app/Service/v3/Implementations/AttachmentService.php @@ -0,0 +1,87 @@ +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; + } +} \ No newline at end of file diff --git a/app/Service/v3/Implementations/WxLoginService.php b/app/Service/v3/Implementations/WxLoginService.php index 5c32f7a..11f8e69 100644 --- a/app/Service/v3/Implementations/WxLoginService.php +++ b/app/Service/v3/Implementations/WxLoginService.php @@ -10,6 +10,7 @@ use App\TaskWorker\SSDBTask; use EasyWeChat\Factory; use Hyperf\Guzzle\CoroutineHandler; use Hyperf\Utils\ApplicationContext; +use Hashids\Hashids; class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterface { @@ -33,6 +34,11 @@ class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterfa ['unionid' => $result['unionid']] )->toArray(); + // 登录成功 + $hash = new Hashids(config('hash_ids_secret')); + $hashIds = $hash->encode((int)$user['id']); + $user['user_token'] = $hashIds; + $return = array_merge($user, $result); $kvs = []; foreach ($return as $k => $v) { diff --git a/app/Service/v3/Interfaces/AttachmentServiceInterface.php b/app/Service/v3/Interfaces/AttachmentServiceInterface.php new file mode 100644 index 0000000..c5b4310 --- /dev/null +++ b/app/Service/v3/Interfaces/AttachmentServiceInterface.php @@ -0,0 +1,25 @@ + \App\Service\v3\Implementations\UserService::class, \App\Service\v3\Interfaces\CouponRebateServiceInterface::class => \App\Service\v3\Implementations\CouponRebateService::class, \App\Service\v3\Interfaces\SmsServiceInterface::class => \App\Service\v3\Implementations\SmsAliService::class, + \App\Service\v3\Interfaces\AttachmentServiceInterface::class => \App\Service\v3\Implementations\AttachmentService::class, ]; diff --git a/config/config.php b/config/config.php index ad49d28..26b4014 100644 --- a/config/config.php +++ b/config/config.php @@ -52,4 +52,5 @@ return [ 'alioss' => [ 'img_host' => env('OSS_IMG_HOST', ''), ], + 'hash_ids_secret' => env('HASH_IDS_SECRET'), ];