zilong 4 年之前
父节点
当前提交
5ec23bc4bf

+ 36 - 0
app/Http/Controllers/Api/V1/CouponController.php

xqd
@@ -9,12 +9,48 @@
 namespace App\Http\Controllers\Api\V1;
 
 use App\Models\Coupon;
+use App\Models\UserCoupon;
 
 class CouponController extends AuthController
 {
     public function couponList()
     {
         $data = Coupon::orderBy('id', 'desc')->paginate();
+
+        return out($data);
+    }
+
+    public function receiveCoupon()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'coupon_id' => 'required|integer'
+        ]);
+        $user = $this->user;
+
+        if (UserCoupon::where('user_id', $user['id'])->where('coupon_id', $req['coupon_id'])->exists()) {
+            return out(null, 10001, '您已经领取过该优惠券了');
+        }
+
+        $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()->toArray();
+        unset($add['is_receive']);
+        $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);
+
+        return out();
+    }
+
+    public function userCouponList()
+    {
+        $user = $this->user;
+
+        $data = UserCoupon::where('user_id', $user['id'])->where('status', 1)->where('expire_time', '>', time())->orderBy('id', 'desc')->paginate();
+
         return out($data);
     }
 }

+ 3 - 0
app/Http/Controllers/Api/V1/EvaluateController.php

xqd xqd
@@ -15,7 +15,9 @@ class EvaluateController extends AuthController
     public function evaluateList()
     {
         $user = $this->user;
+
         $data = Evaluate::with('order.docter')->where('user_id', $user['id'])->orderBy('id', 'desc')->paginate();
+
         return out($data);
     }
 
@@ -27,6 +29,7 @@ class EvaluateController extends AuthController
         ]);
 
         $data = Evaluate::with('order.docter')->where('id', $req['evaluate_id'])->first();
+
         return out($data);
     }
 }

+ 0 - 9
app/Http/Controllers/Api/V1/UserController.php

xqd
@@ -82,13 +82,4 @@ class UserController extends AuthController
 
         return out($data);
     }
-
-    public function userCouponList()
-    {
-        $user = $this->user;
-
-        $data = UserCoupon::where('user_id', $user['id'])->where('status', 1)->where('expire_time', '>', time())->paginate();
-
-        return out($data);
-    }
 }

+ 12 - 0
app/Models/Coupon.php

xqd
@@ -10,5 +10,17 @@ namespace App\Models;
 
 class Coupon extends BaseModel
 {
+    protected $appends = ['is_receive'];
 
+    public function getIsReceiveAttribute()
+    {
+        $user = User::getUserByToken(false);
+        if (!empty($user)) {
+            if (UserCoupon::where('user_id', $user['id'])->where('coupon_id', $this->id)->exists()) {
+                return 1;
+            }
+        }
+
+        return 0;
+    }
 }

+ 5 - 0
app/Models/Evaluate.php

xqd
@@ -19,4 +19,9 @@ class Evaluate extends BaseModel
     {
         return $this->belongsTo(Order::class);
     }
+
+    public function docter()
+    {
+        return $this->belongsTo(Docter::class);
+    }
 }

+ 10 - 1
app/Models/User.php

xqd
@@ -16,20 +16,29 @@ class User extends BaseModel
     }
 
     //通过token获取用户信息
-    public static function getUserByToken()
+    public static function getUserByToken($is_exit = true)
     {
         $auth = request()->header('token');
         if (empty($auth)) {
+            if (!$is_exit) {
+                return '';
+            }
             exit_out(null, 401, '认证失效,请重新登录');
         }
 
         $arr = aes_decrypt($auth);
         if (empty($arr['id'])) {
+            if (!$is_exit) {
+                return '';
+            }
             exit_out(null, 401, '认证失效,请重新登录');
         }
 
         $user = User::where('id', $arr['id'])->first();
         if (empty($user)){
+            if (!$is_exit) {
+                return '';
+            }
             exit_out(null, 601, '该账号已被删除');
         }
         $user = $user->toArray();