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'; $sort = $request->input('sort') ? $request->input('sort') : 'created_at'; $builder = Coupon::orderBy($sort, $sortType); $total = $builder->count(); $coupons = $builder->forPage($current, $pageSize) ->get(); 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' => $coupons, 'total' => $total ]); } public function save(CouponSave $request) { $params = $request->validated(); if (isset($params['limit_plan_ids'])) { $params['limit_plan_ids'] = json_encode($params['limit_plan_ids']); } 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 ]); } public function generate(CouponGenerate $request) { if ($request->input('generate_count')) { $this->multiGenerate($request); return; } $params = $request->validated(); if (isset($params['limit_plan_ids'])) { $params['limit_plan_ids'] = json_encode($params['limit_plan_ids']); } 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 = []; for ($i = 0;$i < $request->input('generate_count');$i++) { $coupon = $request->validated(); $coupon['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']); $coupon['code'] = Helper::randomChar(8); $coupon['created_at'] = $coupon['updated_at'] = time(); unset($coupon['generate_count']); array_push($coupons, $coupon); } DB::beginTransaction(); if (!Coupon::insert($coupons)) { DB::rollBack(); abort(500, '生成失败'); } DB::commit(); $data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n"; foreach($coupons as $coupon) { $type = ['', '金额', '比例'][$coupon['type']]; $value = ['', ($coupon['value'] / 100),$coupon['value']][$coupon['type']]; $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']); $data .= "{$coupon['name']},{$type},{$value},{$startTime},{$endTime},{$limitUse},{$coupon['limit_plan_ids']},{$coupon['code']},{$createTime}\r\n"; } echo $data; } public function drop(Request $request) { 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 ]); } }