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
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
2020-06-13 23:27:43 +08:00
|
|
|
$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']);
|
|
|
|
}
|
2019-12-31 17:49:24 +08:00
|
|
|
return response([
|
2020-06-13 23:27:43 +08:00
|
|
|
'data' => $coupons
|
2019-12-31 17:49:24 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function save(CouponSave $request)
|
|
|
|
{
|
2020-06-13 23:27:43 +08:00
|
|
|
$params = $request->only(array_keys(CouponSave::RULES));
|
|
|
|
if (isset($params['limit_plan_ids'])) {
|
|
|
|
$params['limit_plan_ids'] = json_encode($params['limit_plan_ids']);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
if (!$request->input('id')) {
|
2020-06-20 00:50:21 +08:00
|
|
|
if (!$params['code']) {
|
|
|
|
$params['code'] = Helper::randomChar(8);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
if (!Coupon::create($params)) {
|
|
|
|
abort(500, '创建失败');
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-17 14:28:47 +08:00
|
|
|
try {
|
|
|
|
Coupon::find($request->input('id'))->update($params);
|
|
|
|
} catch (\Exception $e) {
|
2020-01-11 13:36:52 +08:00
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 17:49:24 +08:00
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-01-03 01:12:23 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function drop(Request $request)
|
|
|
|
{
|
2020-01-03 01:12:23 +08:00
|
|
|
if (empty($request->input('id'))) {
|
|
|
|
abort(500, '参数有误');
|
|
|
|
}
|
|
|
|
$coupon = Coupon::find($request->input('id'));
|
|
|
|
if (!$coupon) {
|
|
|
|
abort(500, '优惠券不存在');
|
|
|
|
}
|
|
|
|
if (!$coupon->delete()) {
|
|
|
|
abort(500, '删除失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
}
|