root 5 years ago
parent
commit
9eefb32a4c
4 changed files with 34 additions and 3 deletions
  1. 16 0
      app/Http/Controllers/CoponController.php
  2. 5 3
      install.sql
  3. 2 0
      routes/api.php
  4. 11 0
      update.sql

+ 16 - 0
app/Http/Controllers/CoponController.php

@@ -9,5 +9,21 @@ use App\Models\Coupon;
 class CouponController extends Controller
 {
     public function check (Request $request) {
+        if (empty($request->input('code'))) {
+            abort(500, '参数错误');
+        }
+        $coupon = Coupon::where('code', $request->input('code'))->first();
+        if (!$coupon) {
+            abort(500, '优惠券无效');
+        }
+        if (time() < $coupon->started_at) {
+            abort(500, '优惠券还未到可用时间');
+        }
+        if (time() > $coupon->ended_at) {
+            abort(500, '优惠券已过期');
+        }
+        return response([
+            'data' => $coupon
+        ]);
     }
 }

+ 5 - 3
install.sql

@@ -10,11 +10,13 @@ SET NAMES utf8mb4;
 DROP TABLE IF EXISTS `v2_coupon`;
 CREATE TABLE `v2_coupon` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
+  `code` char(8) NOT NULL,
   `name` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
   `type` tinyint(1) NOT NULL,
   `value` int(11) NOT NULL,
-  `status` tinyint(1) NOT NULL DEFAULT '0',
-  `expired_at` int(11) NOT NULL,
+  `limit_use` int(11) DEFAULT NULL,
+  `started_at` int(11) NOT NULL,
+  `ended_at` int(11) NOT NULL,
   `created_at` int(11) NOT NULL,
   `updated_at` int(11) NOT NULL,
   PRIMARY KEY (`id`)
@@ -200,4 +202,4 @@ CREATE TABLE `v2_user` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 
--- 2019-12-31 09:09:02
+-- 2019-12-31 17:47:41

+ 2 - 0
routes/api.php

@@ -98,6 +98,8 @@ Route::prefix('v1')
                 // Server
                 Route::get ('server/fetch', 'ServerController@fetch');
                 Route::get ('server/log/fetch', 'ServerController@logFetch');
+                // Coupon
+                Route::post('coupon/check', 'CouponController@check');
             });
 
         // Passport

+ 11 - 0
update.sql

@@ -102,4 +102,15 @@ CREATE TABLE `v2_coupon` (
   `ended_at` int(11) NOT NULL,
   `created_at` int(11) NOT NULL,
   `updated_at` int(11) NOT NULL,
+);
+
+CREATE TABLE `v2_coupon_log` (
+  `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
+  `coupon_id` int(11) NOT NULL,
+  `user_id` int(11) NOT NULL,
+  `order_id` int(11) NOT NULL,
+  `total_amount` int(11) NOT NULL,
+  `discount_amount` int(11) NOT NULL,
+  `created_at` int(11) NOT NULL,
+  `updated_at` int(11) NOT NULL
 );