From 62754116a37e9b31d0ddb6385262e8bdaad0e230 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Mon, 11 Jul 2022 14:00:28 +0800 Subject: [PATCH] =?UTF-8?q?Admin=20=E4=BC=9A=E5=91=98=E5=8D=A1-=E4=BC=98?= =?UTF-8?q?=E6=83=A0=E5=88=B8-=E5=8F=AF=E9=85=8D=E7=BD=AE=E6=95=B0?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/VipController.php | 25 ++++++++++++++++++++++-- app/Services/CouponService.php | 1 + app/Services/VipService.php | 13 ++++++++---- resources/views/admin/vip/gift.blade.php | 17 ++++++++-------- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/app/Admin/Controllers/VipController.php b/app/Admin/Controllers/VipController.php index 1a32fabd..34e76b65 100644 --- a/app/Admin/Controllers/VipController.php +++ b/app/Admin/Controllers/VipController.php @@ -86,8 +86,29 @@ class VipController extends AdminController $form->number('sort')->min(0); $form->switch('status')->default(1); $form->textarea('description'); - $form->embeds('gift', function ($form) { - $form->multipleSelect('coupon')->options(Coupon::class)->ajax('api/coupons'); + + $form->table('coupon', function ($table) { + $table->select('id', '优惠券')->options(Coupon::class)->ajax('api/coupons'); + $table->number('amount', '数量')->min(1)->default(1); + })->customFormat(fn() => data_get($this->gift, 'coupon')); + + $form->hidden('gift')->customFormat(fn($v) => json_encode($v)); + + $form->saving(function (Form $form) { + $gift = $form->model()?->gift ?: []; + if ($form->coupon) { + // [id, amount, name] + $couponList = []; + foreach($form->coupon as $item) { + if (!data_get($item, '_remove_') && $item['amount'] > 0) { + $coupon = Coupon::findOrFail($item['id']); + array_push($couponList, ['id' => $coupon->id, 'amount' => $item['amount'], 'name' => $coupon->name]); + } + } + $gift['coupon'] = $couponList; + } + $form->gift = $gift; + $form->deleteInput('coupon'); }); }); } diff --git a/app/Services/CouponService.php b/app/Services/CouponService.php index bfeb39e6..4e106409 100644 --- a/app/Services/CouponService.php +++ b/app/Services/CouponService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Exceptions\BizException; use App\Models\Activity; use App\Models\Coupon; use App\Models\ProductPart; diff --git a/app/Services/VipService.php b/app/Services/VipService.php index 14cacb38..d5189392 100644 --- a/app/Services/VipService.php +++ b/app/Services/VipService.php @@ -102,11 +102,16 @@ class VipService $user = $userVip->user; $gift = $userVip->gift; // 优惠券 - if (isset($gift['coupon'])) { + if (isset($gift['coupon']) && $gift['coupon']) { $coupon_service = new CouponService(); - foreach(Coupon::whereIn('id', $gift['coupon'])->get() as $item) { + $couponId = array_column($gift['coupon'], 'id'); + foreach(Coupon::whereIn('id', $couponId)->get() as $item) { try { - $coupon_service->sendCoupon($user, $item); + $couponItem = array_filter($gift['coupon'], fn($v) => $v['id'] == $item->id); + $amount = data_get($couponItem, 'amount', 1); + if ($amount > 0) { + $coupon_service->sendCoupon($user, $item); + } } catch (BizException $e) { } } @@ -116,7 +121,7 @@ class VipService /** * 延长时限 - * + * * @param int $num 数量 * @param string $unit 单位 * @return Carbon diff --git a/resources/views/admin/vip/gift.blade.php b/resources/views/admin/vip/gift.blade.php index 5eb16d67..d8847ba0 100644 --- a/resources/views/admin/vip/gift.blade.php +++ b/resources/views/admin/vip/gift.blade.php @@ -1,4 +1,4 @@ - @if($value) -