CouponController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Http\Resources\Pay\CouponCollection;
  6. use App\Models\CouponModel;
  7. use Illuminate\Http\Request;
  8. class CouponController extends Controller
  9. {
  10. //
  11. /**
  12. * 优惠券
  13. * @param Request $request
  14. * @return CouponCollection
  15. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  16. * @deprecated Payment/GetCouponList
  17. */
  18. public function index(Request $request)
  19. {
  20. $uid = Auth::auth();
  21. $coupons = CouponModel::existing($uid)
  22. ->when($request->filled('type'), function ($query) use ($request) {
  23. return $query->where('type', $request->input('type'));
  24. })
  25. ->when($request->filled('goods_scope'), function ($query) use ($request) {
  26. return $query->where('goods_scope', $request->get('goods_scope'));
  27. })
  28. ->orderBy('get_at', 'asc')
  29. ->paginate($request->get('pre_page', 20));
  30. CouponModel::whereIn('id', $coupons->pluck('id'))->where('looked_at', 0)->update(['looked_at' => time()]);
  31. return new CouponCollection($coupons);
  32. }
  33. /**
  34. * 优惠券抵扣信息
  35. * @param Request $request
  36. * @param $id
  37. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  38. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  39. * @deprecated Payment/GetCouponDetail
  40. */
  41. public function show(Request $request, $id)
  42. {
  43. $uid = Auth::auth();
  44. $coupon = CouponModel::find($id);
  45. if (!$coupon) {
  46. throw new AlertException("优惠券不存在或已过期");
  47. }
  48. if (!$coupon->isOwner($uid)) {
  49. throw new AlertException("优惠券不存在或已过期");
  50. }
  51. // 查看状态
  52. $coupon->look();
  53. if ($request->filled('goods_id')) {
  54. $goodsId = $request->goods_id;
  55. $settlement_amount = $coupon->getSettlementAmount($goodsId);
  56. $coupon->total_amount = $settlement_amount['total_amount'];
  57. $coupon->settlement_coupon_amount = $settlement_amount['settlement_coupon_amount'];
  58. $coupon->settlement_total_amount = $settlement_amount['settlement_total_amount'];
  59. }
  60. return response([
  61. 'code' => 200,
  62. 'message' => 'OK',
  63. 'data' => $coupon,
  64. ]);
  65. }
  66. /**
  67. * 兑换优惠券
  68. * @param Request $request
  69. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  70. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  71. */
  72. public function exchange(Request $request)
  73. {
  74. $this->validate($request, [
  75. 'code' => 'required',
  76. ], [
  77. 'code.required' => '兑换码不能为空哦',
  78. ]);
  79. $code = $request->input('code');
  80. $uid = Auth::auth();
  81. $coupon = CouponModel::where('code', $code)->first();
  82. if (!$coupon) {
  83. throw new AlertException('兑换码不正确哦');
  84. }
  85. if ($coupon->uid) {
  86. throw new AlertException('兑换码已被兑换');
  87. }
  88. if ($coupon->isOverdue()) {
  89. throw new AlertException('兑换码已过期');
  90. }
  91. $coupon->uid = $uid;
  92. $coupon->get_at = time();
  93. $coupon->save();
  94. return response([
  95. 'code' => 200,
  96. 'message' => 'OK',
  97. 'data' => $coupon
  98. ]);
  99. }
  100. }