diff --git a/app/admin/controller/product_image/ProductImageController.php b/app/admin/controller/product_image/ProductImageController.php new file mode 100644 index 000000000..8e8fa88ce --- /dev/null +++ b/app/admin/controller/product_image/ProductImageController.php @@ -0,0 +1,95 @@ +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); + } + + +} \ No newline at end of file diff --git a/app/admin/controller/store_product/StoreProductController.php b/app/admin/controller/store_product/StoreProductController.php index e04d8c518..21ef23437 100644 --- a/app/admin/controller/store_product/StoreProductController.php +++ b/app/admin/controller/store_product/StoreProductController.php @@ -19,6 +19,11 @@ use TencentCloud\Tiia\V20190529\Models\SearchImageRequest; use TencentCloud\Tiia\V20190529\TiiaClient; use Webman\RedisQueue\Redis; +use app\common\model\product_image\ProductImage; + +use app\admin\logic\product_image\ProductImageLogic; + + /** * 商品列表控制器 * Class StoreProductController @@ -160,7 +165,13 @@ class StoreProductController extends BaseAdminController 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')); $httpProfile = new HttpProfile(); $httpProfile->setEndpoint("tiia.tencentcloudapi.com"); @@ -168,21 +179,36 @@ class StoreProductController extends BaseAdminController $clientProfile->setHttpProfile($httpProfile); $client = new TiiaClient($cred, "ap-guangzhou", $clientProfile); $req = new CreateImageRequest(); - $file = file_get_contents(public_path() . '/可口可乐.jpg'); - $params = [ - 'GroupId' => 'default', - 'EntityId' => '可口可乐_330ml_听装', - 'PicName' => '可口可乐.jpg', + $file = $this->request->file('file'); + $file_path = $file->getRealPath(); + $file = file_get_contents($file_path); + // $fileName ="矿泉水7"; + // $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) ]; - $req->fromJsonString(json_encode($params)); + $req->fromJsonString(json_encode($ImageParams)); $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); return $this->data($result); } public function recognition() { + $params = $this->request->get(); + // $params = [ + // 'GroupId' => 'default', + // ]; $cred = new Credential(getenv('TENCENT_SECRET_ID'), getenv('TENCENT_SECRET_KEY')); $httpProfile = new HttpProfile(); $httpProfile->setEndpoint("tiia.tencentcloudapi.com"); @@ -190,17 +216,24 @@ class StoreProductController extends BaseAdminController $clientProfile->setHttpProfile($httpProfile); $client = new TiiaClient($cred, "ap-guangzhou", $clientProfile); $req = new SearchImageRequest(); - $file = file_get_contents(public_path() . '/可口可乐.jpg'); -// $file = file_get_contents(public_path() . '/可口可乐2.jpg'); -// $file = file_get_contents(public_path() . '/百事可乐.jpg'); - $params = [ - 'GroupId' => 'default', + $file = $this->request->file('file'); + $file_path = $file->getRealPath(); + $file = file_get_contents($file_path); + // $fileName ="鱿鱼"; + // $file = file_get_contents(public_path() . '/' . $fileName. '.jpg'); + $ImageParams = [ + 'GroupId' => $params['GroupId'], 'ImageBase64' => base64_encode($file) ]; - $req->fromJsonString(json_encode($params)); + $req->fromJsonString(json_encode($ImageParams)); $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); return $this->data($result); } - } diff --git a/app/admin/lists/product_image/ProductImageLists.php b/app/admin/lists/product_image/ProductImageLists.php new file mode 100644 index 000000000..677046f74 --- /dev/null +++ b/app/admin/lists/product_image/ProductImageLists.php @@ -0,0 +1,65 @@ + ['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(); + } + +} \ No newline at end of file diff --git a/app/admin/logic/product_image/ProductImageLogic.php b/app/admin/logic/product_image/ProductImageLogic.php new file mode 100644 index 000000000..219264778 --- /dev/null +++ b/app/admin/logic/product_image/ProductImageLogic.php @@ -0,0 +1,102 @@ + $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(); + } +} \ No newline at end of file diff --git a/app/admin/validate/product_image/ProductImageValidate.php b/app/admin/validate/product_image/ProductImageValidate.php new file mode 100644 index 000000000..8bb91c3d1 --- /dev/null +++ b/app/admin/validate/product_image/ProductImageValidate.php @@ -0,0 +1,82 @@ + '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']); + } + +} \ No newline at end of file diff --git a/app/common/model/product_image/ProductImage.php b/app/common/model/product_image/ProductImage.php new file mode 100644 index 000000000..15f31e7e4 --- /dev/null +++ b/app/common/model/product_image/ProductImage.php @@ -0,0 +1,22 @@ +