From 6cf22969edd33c7f5565a57f965d38a31caf331a Mon Sep 17 00:00:00 2001 From: devil Date: Wed, 19 Aug 2020 22:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=B0=81=E9=9D=A2=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/service/BuyService.php | 6 +++ application/service/GoodsService.php | 58 +++++++++++++++++++++++----- application/service/OrderService.php | 7 ++++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/application/service/BuyService.php b/application/service/BuyService.php index 5666cf7db..829d56e31 100755 --- a/application/service/BuyService.php +++ b/application/service/BuyService.php @@ -85,6 +85,12 @@ class BuyService return DataReturn('商品不存在或已删除', -2); } + // 无封面图片 + if(empty($goods['images'])) + { + $goods['images'] = ResourcesService::AttachmentPathHandle(GoodsService::GoodsImagesCoverHandle($goods_id)); + } + // 是否支持购物车操作 $ret = GoodsService::IsGoodsSiteTypeConsistent($goods_id, $goods['site_type']); if($ret['code'] != 0) diff --git a/application/service/GoodsService.php b/application/service/GoodsService.php index e279a7461..58561c124 100755 --- a/application/service/GoodsService.php +++ b/application/service/GoodsService.php @@ -378,7 +378,7 @@ class GoodsService // 获取相册 if($is_photo && !empty($v['id'])) { - $v['photo'] = Db::name('GoodsPhoto')->where(['goods_id'=>$v['id'], 'is_show'=>1])->order('sort asc')->select(); + $v['photo'] = self::GoodsPhotoData($v['id']); if(!empty($v['photo'])) { foreach($v['photo'] as &$vs) @@ -392,16 +392,11 @@ class GoodsService // 商品封面图片 if(isset($v['images'])) { - // 不存在则读取相册第一张图片 + // 无封面图片 if(empty($v['images'])) { - // 是否已存在相册 - if(empty($v['photo']) || empty($v['photo'][0]) || empty($v['photo'][0]['images_old'])) - { - $v['images'] = Db::name('GoodsPhoto')->where(['goods_id'=>$v['id'], 'is_show'=>1])->order('sort asc')->value('images'); - } else { - $v['images'] = $v['photo'][0]['images_old']; - } + // 获取商品封面图片 + $v['images'] = ResourcesService::AttachmentPathHandle(self::GoodsImagesCoverHandle($v['id'], $v['photo'])); } $v['images_old'] = $v['images']; $v['images'] = ResourcesService::AttachmentPathViewHandle($v['images']); @@ -496,6 +491,51 @@ class GoodsService return DataReturn('success', 0, $data); } + /** + * 获取商品封面图片 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2020-08-19 + * @desc description + * @param [int] $goods_id [商品id] + * @param [array] $photo [商品相册] + */ + public static function GoodsImagesCoverHandle($goods_id = 0, $photo = []) + { + // 是否已存在相册 + if(!empty($photo)) + { + $photo = self::GoodsPhotoData($goods_id); + if(!empty($photo[0]) && !empty($photo[0]['images'])) + { + $images = $photo[0]['images']; + } + } + + // 无主图,并且有商品id + if(empty($images) && !empty($goods_id)) + { + $images = Db::name('GoodsPhoto')->where(['goods_id'=>$goods_id, 'is_show'=>1])->order('sort asc')->value('images'); + } + + return isset($images) ? $images : ''; + } + + /** + * 商品相册 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2020-08-19 + * @desc description + * @param [int] $goods_id [商品id] + */ + public static function GoodsPhotoData($goods_id) + { + return Db::name('GoodsPhoto')->where(['goods_id'=>$goods_id, 'is_show'=>1])->order('sort asc')->select(); + } + /** * 获取商品手机详情 * @author Devil diff --git a/application/service/OrderService.php b/application/service/OrderService.php index cc5e87544..1f6476f44 100755 --- a/application/service/OrderService.php +++ b/application/service/OrderService.php @@ -20,6 +20,7 @@ use app\service\ExpressService; use app\service\ResourcesService; use app\service\PayLogService; use app\service\UserService; +use app\service\GoodsService; use app\service\OrderAftersaleService; /** @@ -1202,6 +1203,12 @@ class OrderService { foreach($items as &$vs) { + // 商品是否无封面图片 + if(empty($vs['images'])) + { + $vs['images'] = ResourcesService::AttachmentPathHandle(GoodsService::GoodsImagesCoverHandle($vs['goods_id'])); + } + // 商品信息 $vs['images'] = ResourcesService::AttachmentPathViewHandle($vs['images']); $vs['goods_url'] = MyUrl('index/goods/index', ['id'=>$vs['goods_id']]);