CouponController.php 864 B

12345678910111213141516171819202122232425262728293031
  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. if (empty($request->input('code'))) {
  9. abort(500, '优惠券码不能为空');
  10. }
  11. $coupon = Coupon::where('code', $request->input('code'))->first();
  12. if (!$coupon) {
  13. abort(500, '优惠券无效');
  14. }
  15. if ($coupon->limit_use <= 0 && $coupon->limit_use !== NULL) {
  16. abort(500, '优惠券已无可用次数');
  17. }
  18. if (time() < $coupon->started_at) {
  19. abort(500, '优惠券还未到可用时间');
  20. }
  21. if (time() > $coupon->ended_at) {
  22. abort(500, '优惠券已过期');
  23. }
  24. return response([
  25. 'data' => $coupon
  26. ]);
  27. }
  28. }