2019-12-31 17:49:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\Admin\CouponSave;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Coupon;
|
2019-12-31 18:13:42 +08:00
|
|
|
use App\Utils\Helper;
|
2019-12-31 17:49:24 +08:00
|
|
|
|
|
|
|
class CouponController extends Controller
|
|
|
|
{
|
|
|
|
public function fetch (Request $request) {
|
|
|
|
return response([
|
|
|
|
'data' => Coupon::all()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-31 18:16:43 +08:00
|
|
|
public function save (CouponSave $request) {
|
2019-12-31 17:49:24 +08:00
|
|
|
$params = $request->only([
|
|
|
|
'name',
|
|
|
|
'type',
|
|
|
|
'value',
|
2020-01-01 01:50:05 +08:00
|
|
|
'started_at',
|
|
|
|
'ended_at',
|
2019-12-31 17:49:24 +08:00
|
|
|
'limit_use'
|
|
|
|
]);
|
|
|
|
|
2020-01-02 00:43:25 +08:00
|
|
|
if (!$request->input('id')) {
|
|
|
|
$params['code'] = Helper::randomChar(8);
|
|
|
|
if (!Coupon::create($params)) {
|
|
|
|
abort(500, '创建失败');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!Coupon::find($request->input('id'))->update($params)) {
|
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 17:49:24 +08:00
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|