coupon plan limit

This commit is contained in:
Tokumeikoi 2020-06-13 23:27:43 +08:00
parent 3cdfc69b5d
commit bbb42c0d46
6 changed files with 36 additions and 21 deletions

View File

@ -12,22 +12,21 @@ class CouponController extends Controller
{
public function fetch(Request $request)
{
$coupons = Coupon::all();
foreach ($coupons as $k => $v) {
if ($coupons[$k]['limit_plan_ids']) $coupons[$k]['limit_plan_ids'] = json_decode($coupons[$k]['limit_plan_ids']);
}
return response([
'data' => Coupon::all()
'data' => $coupons
]);
}
public function save(CouponSave $request)
{
$params = $request->only([
'name',
'type',
'value',
'started_at',
'ended_at',
'limit_use'
]);
$params = $request->only(array_keys(CouponSave::RULES));
if (isset($params['limit_plan_ids'])) {
$params['limit_plan_ids'] = json_encode($params['limit_plan_ids']);
}
if (!$request->input('id')) {
$params['code'] = Helper::randomChar(8);
if (!Coupon::create($params)) {

View File

@ -26,6 +26,13 @@ class CouponController extends Controller
if (time() > $coupon->ended_at) {
abort(500, '优惠券已过期');
}
if ($coupon->limit_plan_ids) {
$limitPlanIds = json_decode($coupon->limit_plan_ids);
info($limitPlanIds);
if (!in_array($request->input('plan_id'), $limitPlanIds)) {
abort(500, '这个计划无法使用该优惠码');
}
}
return response([
'data' => $coupon
]);

View File

@ -6,6 +6,15 @@ use Illuminate\Foundation\Http\FormRequest;
class CouponSave extends FormRequest
{
const RULES = [
'name' => 'required',
'type' => 'required|in:1,2',
'value' => 'required|integer',
'started_at' => 'required|integer',
'ended_at' => 'required|integer',
'limit_use' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array'
];
/**
* Get the validation rules that apply to the request.
*
@ -13,14 +22,7 @@ class CouponSave extends FormRequest
*/
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'
];
return self::RULES;
}
public function messages()
@ -35,7 +37,8 @@ class CouponSave extends FormRequest
'started_at.integer' => '开始时间格式有误',
'ended_at.required' => '结束时间不能为空',
'ended_at.integer' => '结束时间格式有误',
'limit_use.integer' => '使用次数格式有误'
'limit_use.integer' => '使用次数格式有误',
'limit_plan_ids.array' => '指定订阅格式有误'
];
}
}

View File

@ -43,6 +43,12 @@ class CouponService
return false;
}
}
if ($this->coupon->limit_plan_ids) {
$limitPlanIds = json_decode($this->coupon->limit_plan_ids);
if (!in_array($order->plan_id, $limitPlanIds)) {
return false;
}
}
return true;
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long