CouponController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\User;
  4. use App\Controllers\BaseController;
  5. use App\Models\Order;
  6. use App\Models\Product;
  7. use App\Models\UserCoupon;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Slim\Http\Response;
  10. use Slim\Http\ServerRequest;
  11. use voku\helper\AntiXSS;
  12. use function explode;
  13. use function in_array;
  14. use function json_decode;
  15. use function time;
  16. final class CouponController extends BaseController
  17. {
  18. public function check(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  19. {
  20. $antiXss = new AntiXSS();
  21. $coupon_raw = $antiXss->xss_clean($request->getParam('coupon'));
  22. $product_id = $antiXss->xss_clean($request->getParam('product_id'));
  23. $invalid_coupon_msg = '优惠码无效';
  24. if ($coupon_raw === '') {
  25. return $response->withJson([
  26. 'ret' => 0,
  27. 'msg' => $invalid_coupon_msg,
  28. ]);
  29. }
  30. $coupon = UserCoupon::where('code', $coupon_raw)->first();
  31. if ($coupon === null || ($coupon->expire_time !== 0 && $coupon->expire_time < time())) {
  32. return $response->withJson([
  33. 'ret' => 0,
  34. 'msg' => $invalid_coupon_msg,
  35. ]);
  36. }
  37. $product = Product::where('id', $product_id)->first();
  38. if ($product === null) {
  39. return $response->withJson([
  40. 'ret' => 0,
  41. 'msg' => $invalid_coupon_msg,
  42. ]);
  43. }
  44. $limit = json_decode($coupon->limit);
  45. if ($limit->disabled) {
  46. return $response->withJson([
  47. 'ret' => 0,
  48. 'msg' => $invalid_coupon_msg,
  49. ]);
  50. }
  51. if ($limit->product_id !== '' && ! in_array($product_id, explode(',', $limit->product_id))) {
  52. return $response->withJson([
  53. 'ret' => 0,
  54. 'msg' => $invalid_coupon_msg,
  55. ]);
  56. }
  57. $user = $this->user;
  58. $use_limit = $limit->use_time;
  59. if ($use_limit > 0) {
  60. $user_use_count = Order::where('user_id', $user->id)->where('coupon', $coupon->code)->count();
  61. if ($user_use_count >= $use_limit) {
  62. return $response->withJson([
  63. 'ret' => 0,
  64. 'msg' => $invalid_coupon_msg,
  65. ]);
  66. }
  67. }
  68. $total_use_limit = $limit->total_use_time;
  69. if ($total_use_limit > 0 && $coupon->use_count >= $total_use_limit) {
  70. return $response->withJson([
  71. 'ret' => 0,
  72. 'msg' => $invalid_coupon_msg,
  73. ]);
  74. }
  75. $content = json_decode($coupon->content);
  76. if ($content->type === 'percentage') {
  77. $discount = $product->price * $content->value / 100;
  78. } else {
  79. $discount = $content->value;
  80. }
  81. $buy_price = $product->price - $discount;
  82. return $response->withJson([
  83. 'ret' => 1,
  84. 'msg' => '优惠码可用',
  85. 'discount' => $discount,
  86. 'buy_price' => $buy_price,
  87. ]);
  88. }
  89. }