CouponController.php 868 B

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