商品图片管理

This commit is contained in:
sjeam 2025-04-14 15:33:52 +08:00
parent daf4b858ce
commit c19ef6080a
6 changed files with 413 additions and 14 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace app\admin\controller\product_image;
use app\admin\controller\BaseAdminController;
use app\admin\lists\product_image\ProductImageLists;
use app\admin\logic\product_image\ProductImageLogic;
use app\admin\validate\product_image\ProductImageValidate;
/**
* 商品图库管理控制器
* Class ProductImageController
* @package app\admin\controller\product_image
*/
class ProductImageController extends BaseAdminController
{
/**
* @notes 获取商品图库管理列表
* @return \think\response\Json
* @author admin
* @date 2025/04/14 11:02
*/
public function lists()
{
return $this->dataLists(new ProductImageLists());
}
/**
* @notes 添加商品图库管理
* @return \think\response\Json
* @author admin
* @date 2025/04/14 11:02
*/
public function add()
{
$params = (new ProductImageValidate())->post()->goCheck('add');
$result = ProductImageLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(ProductImageLogic::getError());
}
/**
* @notes 编辑商品图库管理
* @return \think\response\Json
* @author admin
* @date 2025/04/14 11:02
*/
public function edit()
{
$params = (new ProductImageValidate())->post()->goCheck('edit');
$result = ProductImageLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(ProductImageLogic::getError());
}
/**
* @notes 删除商品图库管理
* @return \think\response\Json
* @author admin
* @date 2025/04/14 11:02
*/
public function delete()
{
$params = (new ProductImageValidate())->post()->goCheck('delete');
ProductImageLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取商品图库管理详情
* @return \think\response\Json
* @author admin
* @date 2025/04/14 11:02
*/
public function detail()
{
$params = (new ProductImageValidate())->goCheck('detail');
$result = ProductImageLogic::detail($params);
return $this->data($result);
}
}

View File

@ -19,6 +19,11 @@ use TencentCloud\Tiia\V20190529\Models\SearchImageRequest;
use TencentCloud\Tiia\V20190529\TiiaClient; use TencentCloud\Tiia\V20190529\TiiaClient;
use Webman\RedisQueue\Redis; use Webman\RedisQueue\Redis;
use app\common\model\product_image\ProductImage;
use app\admin\logic\product_image\ProductImageLogic;
/** /**
* 商品列表控制器 * 商品列表控制器
* Class StoreProductController * Class StoreProductController
@ -160,7 +165,13 @@ class StoreProductController extends BaseAdminController
public function upload() public function upload()
{ {
$file = $this->request->file('file'); $params = $this->request->get();
// $params = [
// 'ProductId' => '1024',
// 'GroupId' => 'default',
// ];
$fileName = StoreProduct::where('id', $params['ProductId'])->value('store_name');
$product_num = ProductImage::where('product_id', $params['ProductId'])->count('id');
$cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY')); $cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY'));
$httpProfile = new HttpProfile(); $httpProfile = new HttpProfile();
$httpProfile->setEndpoint("tiia.tencentcloudapi.com"); $httpProfile->setEndpoint("tiia.tencentcloudapi.com");
@ -168,21 +179,36 @@ class StoreProductController extends BaseAdminController
$clientProfile->setHttpProfile($httpProfile); $clientProfile->setHttpProfile($httpProfile);
$client = new TiiaClient($cred, "ap-guangzhou", $clientProfile); $client = new TiiaClient($cred, "ap-guangzhou", $clientProfile);
$req = new CreateImageRequest(); $req = new CreateImageRequest();
$file = file_get_contents(public_path() . '/可口可乐.jpg'); $file = $this->request->file('file');
$params = [ $file_path = $file->getRealPath();
'GroupId' => 'default', $file = file_get_contents($file_path);
'EntityId' => '可口可乐_330ml_听装', // $fileName ="矿泉水7";
'PicName' => '可口可乐.jpg', // $file = file_get_contents(public_path() . '/' . $fileName. '.jpg');
$ImageParams = [
'GroupId' => $params['GroupId'],
'EntityId' => $params['ProductId'].'_'.$fileName.'_'.intval($product_num+1),
'PicName' => $params['ProductId'].'_'.$fileName,
'ImageBase64' => base64_encode($file) 'ImageBase64' => base64_encode($file)
]; ];
$req->fromJsonString(json_encode($params)); $req->fromJsonString(json_encode($ImageParams));
$resp = $client->CreateImage($req); $resp = $client->CreateImage($req);
$CreateParams = [
'product_id' => $params['ProductId'],
'group_id' => $ImageParams['GroupId'],
'entity_id' => $ImageParams['EntityId'],
'pic_name' => $ImageParams['PicName'],
];
ProductImageLogic::add($CreateParams);
$result = json_decode($resp->toJsonString(), true); $result = json_decode($resp->toJsonString(), true);
return $this->data($result); return $this->data($result);
} }
public function recognition() public function recognition()
{ {
$params = $this->request->get();
// $params = [
// 'GroupId' => 'default',
// ];
$cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY')); $cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY'));
$httpProfile = new HttpProfile(); $httpProfile = new HttpProfile();
$httpProfile->setEndpoint("tiia.tencentcloudapi.com"); $httpProfile->setEndpoint("tiia.tencentcloudapi.com");
@ -190,17 +216,24 @@ class StoreProductController extends BaseAdminController
$clientProfile->setHttpProfile($httpProfile); $clientProfile->setHttpProfile($httpProfile);
$client = new TiiaClient($cred, "ap-guangzhou", $clientProfile); $client = new TiiaClient($cred, "ap-guangzhou", $clientProfile);
$req = new SearchImageRequest(); $req = new SearchImageRequest();
$file = file_get_contents(public_path() . '/可口可乐.jpg'); $file = $this->request->file('file');
// $file = file_get_contents(public_path() . '/可口可乐2.jpg'); $file_path = $file->getRealPath();
// $file = file_get_contents(public_path() . '/百事可乐.jpg'); $file = file_get_contents($file_path);
$params = [ // $fileName ="鱿鱼";
'GroupId' => 'default', // $file = file_get_contents(public_path() . '/' . $fileName. '.jpg');
$ImageParams = [
'GroupId' => $params['GroupId'],
'ImageBase64' => base64_encode($file) 'ImageBase64' => base64_encode($file)
]; ];
$req->fromJsonString(json_encode($params)); $req->fromJsonString(json_encode($ImageParams));
$resp = $client->SearchImage($req); $resp = $client->SearchImage($req);
if(!empty($resp->ImageInfos)){
$ids = array_column($resp->ImageInfos,'EntityId');
$data = ProductImage::whereIn('entity_id', $ids)->field('product_id,group_id,entity_id,pic_name')->group(['product_id', 'entity_id'])->select();
$resp->ImageInfos = $data->toArray();
$resp->Count = $data->count('product_id');
}
$result = json_decode($resp->toJsonString(), true); $result = json_decode($resp->toJsonString(), true);
return $this->data($result); return $this->data($result);
} }
} }

View File

@ -0,0 +1,65 @@
<?php
namespace app\admin\lists\product_image;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\product_image\ProductImage;
use app\common\lists\ListsSearchInterface;
/**
* 商品图库管理列表
* Class ProductImageLists
* @package app\admin\listsproduct_image
*/
class ProductImageLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author admin
* @date 2025/04/14 11:02
*/
public function setSearch(): array
{
return [
'=' => ['product_id', 'entity_id', 'group_id', 'pic_name'],
];
}
/**
* @notes 获取商品图库管理列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author admin
* @date 2025/04/14 11:02
*/
public function lists(): array
{
return ProductImage::where($this->searchWhere)
->field(['id', 'product_id', 'entity_id', 'group_id', 'pic_name'])
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
}
/**
* @notes 获取商品图库管理数量
* @return int
* @author admin
* @date 2025/04/14 11:02
*/
public function count(): int
{
return ProductImage::where($this->searchWhere)->count();
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace app\admin\logic\product_image;
use app\common\model\product_image\ProductImage;
use app\common\logic\BaseLogic;
use support\exception\BusinessException;
use think\facade\Db;
/**
* 商品图库管理逻辑
* Class ProductImageLogic
* @package app\admin\logic\product_image
*/
class ProductImageLogic extends BaseLogic
{
/**
* @notes 添加商品图库管理
* @param array $params
* @return bool
* @author admin
* @date 2025/04/14 11:02
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
ProductImage::create([
'product_id' => $params['product_id'],
'entity_id' => $params['entity_id'],
'group_id' => $params['group_id'],
'pic_name' => $params['pic_name'],
'create_time' => time(),
'update_time' => time(),
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑商品图库管理
* @param array $params
* @return bool
* @author admin
* @date 2025/04/14 11:02
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
ProductImage::where('id', $params['id'])->update([
'product_id' => $params['product_id'],
'entity_id' => $params['entity_id'],
'group_id' => $params['group_id'],
'pic_name' => $params['pic_name'],
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除商品图库管理
* @param array $params
* @return bool
* @author admin
* @date 2025/04/14 11:02
*/
public static function delete(array $params): bool
{
return ProductImage::destroy($params['id']);
}
/**
* @notes 获取商品图库管理详情
* @param $params
* @return array
* @author admin
* @date 2025/04/14 11:02
*/
public static function detail($params): array
{
return ProductImage::findOrEmpty($params['id'])->toArray();
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace app\admin\validate\product_image;
use app\common\validate\BaseValidate;
/**
* 商品图库管理验证器
* Class ProductImageValidate
* @package app\admin\validate\product_image
*/
class ProductImageValidate extends BaseValidate
{
/**
* 设置校验规则
* @var string[]
*/
protected $rule = [
'id' => 'require',
];
/**
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
];
/**
* @notes 添加场景
* @return ProductImageValidate
* @author admin
* @date 2025/04/14 11:02
*/
public function sceneAdd()
{
return $this->remove('id', true);
}
/**
* @notes 编辑场景
* @return ProductImageValidate
* @author admin
* @date 2025/04/14 11:02
*/
public function sceneEdit()
{
return $this->only(['id']);
}
/**
* @notes 删除场景
* @return ProductImageValidate
* @author admin
* @date 2025/04/14 11:02
*/
public function sceneDelete()
{
return $this->only(['id']);
}
/**
* @notes 详情场景
* @return ProductImageValidate
* @author admin
* @date 2025/04/14 11:02
*/
public function sceneDetail()
{
return $this->only(['id']);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace app\common\model\product_image;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
/**
* 商品图库管理模型
* Class ProductImage
* @package app\common\model\product_image
*/
class ProductImage extends BaseModel
{
use SoftDelete;
protected $name = 'product_image';
protected $deleteTime = 'delete_time';
}