update: fix coupon

This commit is contained in:
tokumeikoi 2021-08-29 12:54:15 +09:00
parent b38f7979d6
commit e5e7a06514
4 changed files with 5 additions and 72 deletions

View File

@ -30,29 +30,6 @@ class CouponController extends Controller
]); ]);
} }
public function save(CouponSave $request)
{
$params = $request->validated();
if (!$request->input('id')) {
if (!isset($params['code'])) {
$params['code'] = Helper::randomChar(8);
}
if (!Coupon::create($params)) {
abort(500, '创建失败');
}
} else {
try {
Coupon::find($request->input('id'))->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
}
}
return response([
'data' => true
]);
}
public function generate(CouponGenerate $request) public function generate(CouponGenerate $request)
{ {
if ($request->input('generate_count')) { if ($request->input('generate_count')) {

View File

@ -21,6 +21,7 @@ class CouponGenerate extends FormRequest
'started_at' => 'required|integer', 'started_at' => 'required|integer',
'ended_at' => 'required|integer', 'ended_at' => 'required|integer',
'limit_use' => 'nullable|integer', 'limit_use' => 'nullable|integer',
'limit_use_with_user' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array', 'limit_plan_ids' => 'nullable|array',
'code' => '' 'code' => ''
]; ];
@ -40,7 +41,8 @@ class CouponGenerate extends FormRequest
'started_at.integer' => '开始时间格式有误', 'started_at.integer' => '开始时间格式有误',
'ended_at.required' => '结束时间不能为空', 'ended_at.required' => '结束时间不能为空',
'ended_at.integer' => '结束时间格式有误', 'ended_at.integer' => '结束时间格式有误',
'limit_use.integer' => '使用次数格式有误', 'limit_use.integer' => '最大使用次数格式有误',
'limit_use_with_user.integer' => '限制用户使用次数格式有误',
'limit_plan_ids.array' => '指定订阅格式有误' 'limit_plan_ids.array' => '指定订阅格式有误'
]; ];
} }

View File

@ -1,46 +0,0 @@
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class CouponSave extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'type' => 'required|in:1,2',
'value' => 'required|integer',
'started_at' => 'required|integer',
'ended_at' => 'required|integer',
'limit_use' => 'nullable|integer',
'limit_use_with_user' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array',
'code' => ''
];
}
public function messages()
{
return [
'name.required' => '名称不能为空',
'type.required' => '类型不能为空',
'type.in' => '类型格式有误',
'value.required' => '金额或比例不能为空',
'value.integer' => '金额或比例格式有误',
'started_at.required' => '开始时间不能为空',
'started_at.integer' => '开始时间格式有误',
'ended_at.required' => '结束时间不能为空',
'ended_at.integer' => '结束时间格式有误',
'limit_use.integer' => '最大使用次数格式有误',
'limit_use_with_user.integer' => '限制用户使用次数格式有误',
'limit_plan_ids.array' => '指定订阅格式有误'
];
}
}

View File

@ -17,7 +17,7 @@ class CouponService
$this->coupon = Coupon::where('code', $code)->first(); $this->coupon = Coupon::where('code', $code)->first();
} }
public function use(Order $order) public function use(Order $order):bool
{ {
$this->setPlanId($order->plan_id); $this->setPlanId($order->plan_id);
$this->setUserId($order->user_id); $this->setUserId($order->user_id);
@ -59,7 +59,7 @@ class CouponService
$this->userId = $userId; $this->userId = $userId;
} }
public function checkLimitUseWithUser() public function checkLimitUseWithUser():bool
{ {
$usedCount = Order::where('coupon_id', $this->coupon->id) $usedCount = Order::where('coupon_id', $this->coupon->id)
->where('user_id', $this->userId) ->where('user_id', $this->userId)