CouponController.php 910 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\Coupon;
  6. class CouponController extends Controller
  7. {
  8. public function check(Request $request)
  9. {
  10. if (empty($request->input('code'))) {
  11. abort(500, '优惠券码不能为空');
  12. }
  13. $coupon = Coupon::where('code', $request->input('code'))->first();
  14. if (!$coupon) {
  15. abort(500, '优惠券无效');
  16. }
  17. if ($coupon->limit_use <= 0 && $coupon->limit_use !== NULL) {
  18. abort(500, '优惠券已无可用次数');
  19. }
  20. if (time() < $coupon->started_at) {
  21. abort(500, '优惠券还未到可用时间');
  22. }
  23. if (time() > $coupon->ended_at) {
  24. abort(500, '优惠券已过期');
  25. }
  26. return response([
  27. 'data' => $coupon
  28. ]);
  29. }
  30. }