123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-10-24
- * Time: 上午11:14
- */
- namespace App\Http\Controllers\Api\V1;
- use App\Models\Coupon;
- use App\Models\UserCoupon;
- class CouponController extends AuthController
- {
- public function couponList()
- {
- $data = Coupon::where('position_type', 1)->where('num', '>', 0)->orderBy('id', 'desc')->paginate();
- return out($data);
- }
- public function receiveCoupon()
- {
- $req = request()->post();
- $this->validate(request(), [
- 'coupon_id' => 'required|integer'
- ]);
- $user = $this->user;
- $coupon = Coupon::select(['num', 'position_type'])->where('id', $req['coupon_id'])->first();
- if ($coupon['num'] <= 0) {
- return out(null, 10001, '该优惠券数量不足');
- }
- if ($coupon['position_type'] == 2) {
- return out(null, 10002, '该优惠券不能领取');
- }
- if (UserCoupon::where('user_id', $user['id'])->where('coupon_id', $req['coupon_id'])->exists()) {
- return out(null, 10003, '您已经领取过该优惠券了');
- }
- $add = Coupon::select(['id as coupon_id', 'name', 'title', 'desc', 'rules', 'icon', 'type', 'usable_type', 'money', 'discount', 'min_consume_amount', 'max_reduce_amount', 'expire_type', 'effective_days', 'start_time', 'end_time'])->where('id', $req['coupon_id'])->first()->getOriginal();
- $add['user_id'] = $user['id'];
- $expire_time = $add['end_time'];
- if ($add['expire_type'] == 1) {
- $expire_time = time() + $add['effective_days']*24*3600;
- }
- $add['expire_time'] = $expire_time;
- UserCoupon::create($add);
- Coupon::where('id', $req['coupon_id'])->decrement('num');
- return out();
- }
- public function userCouponList()
- {
- $user = $this->user;
- UserCoupon::where('user_id', $user['id'])->where('status', 1)->where('expire_time', '<=', time())->update(['status' => 3]);
- $data = UserCoupon::with('couponType')->where('user_id', $user['id'])->orderBy('id', 'desc')->paginate();
- return out($data);
- }
- }
|