Skip to content

Commit

Permalink
管理后台 - 新增、修改、删除优惠券
Browse files Browse the repository at this point in the history
  • Loading branch information
leo108 committed Jun 13, 2018
1 parent 8977037 commit b09a891
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
42 changes: 31 additions & 11 deletions app/Admin/Controllers/CouponCodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public function index()
public function edit($id)
{
return Admin::content(function (Content $content) use ($id) {

$content->header('header');
$content->description('description');

$content->header('编辑优惠券');
$content->body($this->form()->edit($id));
});
}
Expand All @@ -53,10 +50,7 @@ public function edit($id)
public function create()
{
return Admin::content(function (Content $content) {

$content->header('header');
$content->description('description');

$content->header('新增优惠券');
$content->body($this->form());
});
}
Expand Down Expand Up @@ -92,11 +86,37 @@ protected function grid()
protected function form()
{
return Admin::form(CouponCode::class, function (Form $form) {

$form->display('id', 'ID');
$form->text('name', '名称')->rules('required');
$form->text('code', '优惠码')->rules(function($form) {
// 如果 $form->model()->id 不为空,代表是编辑操作
if ($id = $form->model()->id) {
return 'nullable|unique:coupon_codes,id,'.$id;
} else {
return 'nullable|unique:coupon_codes';
}
});
$form->radio('type', '类型')->options(CouponCode::$typeMap)->rules('required');
$form->text('value', '折扣')->rules(function ($form) {
if ($form->type === CouponCode::TYPE_PERCENT) {
// 如果选择了百分比折扣类型,那么折扣范围只能是 1 ~ 99
return 'required|numeric|between:1,99';
} else {
// 否则只要大等于 0.01 即可
return 'required|numeric|min:0.01';
}
});
$form->text('total', '总量')->rules('required|numeric|min:0');
$form->text('min_amount', '最低金额')->rules('required|numeric|min:0');
$form->datetime('not_before', '开始时间');
$form->datetime('not_after', '结束时间');
$form->radio('enabled', '启用')->options(['1' => '', '0' => '']);

$form->display('created_at', 'Created At');
$form->display('updated_at', 'Updated At');
$form->saving(function (Form $form) {
if (!$form->code) {
$form->code = CouponCode::findAvailableCode();
}
});
});
}
}
5 changes: 5 additions & 0 deletions app/Admin/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@
$router->post('orders/{order}/ship', 'OrdersController@ship')->name('admin.orders.ship');
$router->post('orders/{order}/refund', 'OrdersController@handleRefund')->name('admin.orders.handle_refund');
$router->get('coupon_codes', 'CouponCodesController@index');
$router->post('coupon_codes', 'CouponCodesController@store');
$router->get('coupon_codes/create', 'CouponCodesController@create');
$router->get('coupon_codes/{id}/edit', 'CouponCodesController@edit');
$router->put('coupon_codes/{id}', 'CouponCodesController@update');
$router->delete('coupon_codes/{id}', 'CouponCodesController@destroy');
});

0 comments on commit b09a891

Please sign in to comment.