Browse Source

处理商品图片

master
weigang 5 years ago
parent
commit
5b37d81626
  1. 6
      app/Constants/v3/ErrorCode.php
  2. 13
      app/Model/v3/Goods.php
  3. 12
      app/Model/v3/GoodsActivity.php
  4. 87
      app/Service/v3/Implementations/AttachmentService.php
  5. 6
      app/Service/v3/Implementations/WxLoginService.php
  6. 25
      app/Service/v3/Interfaces/AttachmentServiceInterface.php
  7. 3
      composer.json
  8. 75
      composer.lock
  9. 1
      config/autoload/dependencies.php
  10. 1
      config/config.php

6
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 */
/************************************/

13
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()

12
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');

87
app/Service/v3/Implementations/AttachmentService.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;
}
}

6
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) {

25
app/Service/v3/Interfaces/AttachmentServiceInterface.php

@ -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 = '');
}

3
composer.json

@ -41,7 +41,8 @@
"hyperf/rpc-server": "^2.0",
"hyperf/rpc-client": "^2.0",
"hyperf/consul": "^2.0",
"alibabacloud/client": "^1.5"
"alibabacloud/client": "^1.5",
"hashids/hashids": "^4.0"
},
"require-dev": {
"swoole/ide-helper": "^4.5",

75
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "37617310b51575e5a3bb79ed341b5726",
"content-hash": "9f238a6a5e556f4061e56600f28bacfd",
"packages": [
{
"name": "adbario/php-dot-notation",
@ -1075,6 +1075,79 @@
],
"time": "2019-07-01T23:21:34+00:00"
},
{
"name": "hashids/hashids",
"version": "4.0.0",
"source": {
"type": "git",
"url": "https://github.com/vinkla/hashids.git",
"reference": "43bb2407f16a631f0128f47bcb67ff986c63dde2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/vinkla/hashids/zipball/43bb2407f16a631f0128f47bcb67ff986c63dde2",
"reference": "43bb2407f16a631f0128f47bcb67ff986c63dde2",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"ext-mbstring": "*",
"php": "^7.2"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
},
"suggest": {
"ext-bcmath": "Required to use BC Math arbitrary precision mathematics (*).",
"ext-gmp": "Required to use GNU multiple precision mathematics (*)."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"autoload": {
"psr-4": {
"Hashids\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ivan Akimov",
"email": "ivan@barreleye.com",
"homepage": "https://twitter.com/IvanAkimov"
},
{
"name": "Vincent Klaiber",
"email": "hello@doubledip.se",
"homepage": "https://doubledip.se"
}
],
"description": "Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers",
"homepage": "http://hashids.org/php",
"keywords": [
"bitly",
"decode",
"encode",
"hash",
"hashid",
"hashids",
"ids",
"obfuscate",
"youtube"
],
"time": "2019-04-03T13:40:29+00:00"
},
{
"name": "hyperf/amqp",
"version": "v2.0.3",

1
config/autoload/dependencies.php

@ -78,4 +78,5 @@ return [
\App\Service\v3\Interfaces\UserServiceInterface::class => \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,
];

1
config/config.php

@ -52,4 +52,5 @@ return [
'alioss' => [
'img_host' => env('OSS_IMG_HOST', ''),
],
'hash_ids_secret' => env('HASH_IDS_SECRET'),
];
Loading…
Cancel
Save