2019-12-31 17:49:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\Admin\CouponSave;
|
2020-10-24 23:08:14 +08:00
|
|
|
use App\Http\Requests\Admin\CouponGenerate;
|
|
|
|
use App\Models\Plan;
|
|
|
|
use App\Models\User;
|
2019-12-31 17:49:24 +08:00
|
|
|
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;
|
2020-10-24 23:08:14 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
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-10-24 23:08:14 +08:00
|
|
|
$current = $request->input('current') ? $request->input('current') : 1;
|
|
|
|
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
|
|
|
|
$sortType = in_array($request->input('sort_type'), ['ASC', 'DESC']) ? $request->input('sort_type') : 'DESC';
|
2021-10-16 20:47:27 +08:00
|
|
|
$sort = $request->input('sort') ? $request->input('sort') : 'id';
|
2020-10-24 23:08:14 +08:00
|
|
|
$builder = Coupon::orderBy($sort, $sortType);
|
|
|
|
$total = $builder->count();
|
|
|
|
$coupons = $builder->forPage($current, $pageSize)
|
|
|
|
->get();
|
2019-12-31 17:49:24 +08:00
|
|
|
return response([
|
2020-10-24 23:08:14 +08:00
|
|
|
'data' => $coupons,
|
|
|
|
'total' => $total
|
2019-12-31 17:49:24 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-01-22 02:30:05 +08:00
|
|
|
public function show(Request $request)
|
|
|
|
{
|
|
|
|
if (empty($request->input('id'))) {
|
|
|
|
abort(500, '参数有误');
|
|
|
|
}
|
|
|
|
$coupon = Coupon::find($request->input('id'));
|
|
|
|
if (!$coupon) {
|
|
|
|
abort(500, '优惠券不存在');
|
|
|
|
}
|
|
|
|
$coupon->show = $coupon->show ? 0 : 1;
|
|
|
|
if (!$coupon->save()) {
|
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-10-24 23:08:14 +08:00
|
|
|
public function generate(CouponGenerate $request)
|
|
|
|
{
|
|
|
|
if ($request->input('generate_count')) {
|
|
|
|
$this->multiGenerate($request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function multiGenerate(CouponGenerate $request)
|
|
|
|
{
|
|
|
|
$coupons = [];
|
2020-11-07 15:44:57 +08:00
|
|
|
$coupon = $request->validated();
|
|
|
|
$coupon['created_at'] = $coupon['updated_at'] = time();
|
2021-08-06 00:43:01 +08:00
|
|
|
$coupon['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']);
|
2021-12-28 01:59:59 +08:00
|
|
|
$coupon['limit_period'] = json_encode($coupon['limit_period']);
|
2020-11-07 15:44:57 +08:00
|
|
|
unset($coupon['generate_count']);
|
2020-10-24 23:08:14 +08:00
|
|
|
for ($i = 0;$i < $request->input('generate_count');$i++) {
|
|
|
|
$coupon['code'] = Helper::randomChar(8);
|
|
|
|
array_push($coupons, $coupon);
|
|
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
if (!Coupon::insert($coupons)) {
|
|
|
|
DB::rollBack();
|
|
|
|
abort(500, '生成失败');
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
$data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n";
|
|
|
|
foreach($coupons as $coupon) {
|
2020-10-24 23:26:59 +08:00
|
|
|
$type = ['', '金额', '比例'][$coupon['type']];
|
2020-10-25 00:04:36 +08:00
|
|
|
$value = ['', ($coupon['value'] / 100),$coupon['value']][$coupon['type']];
|
2020-10-24 23:08:14 +08:00
|
|
|
$startTime = date('Y-m-d H:i:s', $coupon['started_at']);
|
|
|
|
$endTime = date('Y-m-d H:i:s', $coupon['ended_at']);
|
|
|
|
$limitUse = $coupon['limit_use'] ?? '不限制';
|
|
|
|
$createTime = date('Y-m-d H:i:s', $coupon['created_at']);
|
2021-08-06 00:43:01 +08:00
|
|
|
$limitPlanIds = implode("/", json_decode($coupon['limit_plan_ids'], true)) ?? '不限制';
|
2021-01-14 23:35:36 +08:00
|
|
|
$data .= "{$coupon['name']},{$type},{$value},{$startTime},{$endTime},{$limitUse},{$limitPlanIds},{$coupon['code']},{$createTime}\r\n";
|
2020-10-24 23:08:14 +08:00
|
|
|
}
|
|
|
|
echo $data;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|