wesley.chen 7 éve
szülő
commit
48d867c89a

+ 1 - 1
app/Http/Controllers/Api/V1/Controller.php

xqd
@@ -21,7 +21,7 @@ class Controller extends BaseController
         $this->middleware('auth:api', [
             'except' => [
                 'upload', 'getCode', 'reset', 'login', 'get', 'register', 'alipayNotify', 'wechatpayNotify',
-                'get', 'area', 'get_province', 'get_city', 'get_county', 'test','getProducts','getProduct','getStores','getSchedule','createOrder'
+                'get', 'area', 'get_province', 'get_city', 'get_county', 'test','getProducts','getProduct','getStores','getSchedule','createOrder','couponList','myCoupon','getCoupon'
 
             ]
         ]);

+ 255 - 24
app/Http/Controllers/Api/V1/HomeController.php

xqd xqd xqd xqd xqd xqd xqd xqd xqd
@@ -4,13 +4,17 @@ namespace App\Http\Controllers\Api\V1;
 
 use App\Helper\AttachmentHelper;
 use App\Models\BaseAttachmentModel;
+use App\Models\CouponInfoModel;
 use App\Models\OrderInfoModel;
 use App\Models\ProductCategoryModel;
 use App\Models\ProductInfoModel;
 use App\Models\ProductScheduleModel;
 use App\Models\StoreInfoModel;
+use App\Models\UserCouponRelationModel;
 use App\Models\UserScheduleModel;
+use Carbon\Carbon;
 use EasyWeChat\Factory;
+use function foo\func;
 use Illuminate\Http\Request;
 use App\Services\Base\Attachment;
 use App\Services\Base\ErrorCode;
@@ -40,9 +44,9 @@ class HomeController extends Controller
      * @apiGroup photo
      * @apiPermission none
      * @apiVersion 0.1.0
-     * @apiParam {string}    [code](必填)
-     * @apiParam {string}    [nickName](必填)
-     * @apiParam {string}    [avatar](必填)
+     * @apiParam {string}    code               小程序登陆后返回的code
+     * @apiParam {string}    nickName           微信昵称
+     * @apiParam {string}    avatar             微信头像
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -134,7 +138,7 @@ class HomeController extends Controller
      * @apiGroup Photo
      * @apiPermission none
      * @apiVersion 0.1.0
-     * @apiParam {string}    [id](必填)
+     * @apiParam {string}    id             产品的id
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -238,8 +242,8 @@ class HomeController extends Controller
      * @apiGroup Photo
      * @apiPermission None
      * @apiVersion 0.1.0
-     * @apiParam {string}    [day](必填)
-     * @apiParam {string}    [storeid](必填)
+     * @apiParam {string}     day             要预约的日期
+     * @apiParam {string}     storeid         要预约的店铺ID
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -312,15 +316,15 @@ class HomeController extends Controller
      * @apiGroup Photo
      * @apiPermission None
      * @apiVersion 0.1.0
-     * @apiParam {string}    [user_id](必填)
-     * @apiParam {string}    [store_id](必填)
-     * @apiParam {string}    [product_id](必填)
-     * @apiParam {string}    [schedule_time](必填)
-     * @apiParam {string}    [username](必填)
-     * @apiParam {string}    [phone](必填)
-     * @apiParam {string}    [email](必填)
-     * @apiParam {string}    [sex](必填)
-     * @apiParam {string}    [comment](可填)
+     * @apiParam {string}    user_id         user_id
+     * @apiParam {string}    store_id        预约得店铺ID
+     * @apiParam {string}    product_id      产品规格ID
+     * @apiParam {string}    schedule_time   预约时间
+     * @apiParam {string}    username        到店人姓名
+     * @apiParam {string}    phone           联系方式
+     * @apiParam {string}    email           邮箱(接受照片用)
+     * @apiParam {string}    sex             性别
+     * @apiParam {string}    [comment]       备注
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -422,10 +426,10 @@ class HomeController extends Controller
      * @apiGroup Photo
      * @apiPermission none
      * @apiVersion 0.1.0
-     * @apiParam {int}    [userid]   用户ID(必填)
-     * @apiParam {string}    [orderid]  订单号(必填)
-     * @apiParam {int}    [couponid]  优惠券id(可填)
-     * @apiParam {int}    [cardid]    抵扣卡id(可填;抵扣卡和优惠券不能同时使用)
+     * @apiParam {int}       user_id      用户ID
+     * @apiParam {string}    orderid     订单号,创建订单后返回
+     * @apiParam {int}       [couponid]  优惠券id
+     * @apiParam {int}       [cardid]    抵扣卡id(抵扣卡和优惠券不能同时使用)
      * @apiSuccessExample {json} Success-Response:
      * HTTP/1.1 200 OK
      * {
@@ -456,11 +460,11 @@ class HomeController extends Controller
     {
         $validator = Validator::make($request->all(),
             [
-                'userid' => 'required',
+                'user_id' => 'required',
                 'orderid' => 'required',
             ],
             [
-                'userid.required' => 'userid不能为空!',
+                'user_id.required' => 'user_id不能为空!',
                 'orderid.required' => 'orderid不能为空!',
 
             ]
@@ -471,7 +475,7 @@ class HomeController extends Controller
             return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
         }
 
-        $user = UserInfoModel::find(request('userid'));
+        $user = UserInfoModel::find(request('user_id'));
         $order = OrderInfoModel::find($request->get('orderid'));
 
 
@@ -530,11 +534,238 @@ class HomeController extends Controller
 
     }
 
-    public function couponList(){
 
+    /**
+     * @api {get} /api/home/couponlist   可领取的优惠券列表
+     * @apiDescription  可领取的优惠券列表
+     * @apiGroup Photo
+     * @apiPermission none
+     * @apiVersion 0.1.0
+     * @apiParam {int}       user_id      用户ID
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     * {
+     *     "status": true,
+     *     "status_code": 0,
+     *     "message": "",
+     *     "data": {
+     *              "couponlist": [
+     *                  {
+     *                      "id": 1,
+     *                      "name": "证件照七折优惠",                 //优惠券名字
+     *                      "type": "1",                            //优惠券类型 0:固定金额优惠,1:折扣优惠
+     *                      "min_price": 100,                       //最低使用金额
+     *                      "discount": "7",                        //折扣
+     *                      "discount_price": null,                 //优惠金额
+     *                      "product_id": "1,2",                    //可使用的产品id
+     *                      "count": 10,                            //剩余数量
+     *                      "end_time": "2018-07-31",               //有效期
+     *                      "created_at": "2018-07-13 07:29:27",
+     *                      "updated_at": "2018-07-18 06:36:38"e
+     *                  }
+     *              ]
+     *          }
+     * }
+     * @apiErrorExample {json} Error-Response:
+     * HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     * 可能出现的错误代码:
+     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
+     */
+    public function couponList(Request $request)
+    {
+
+        $validator = Validator::make($request->all(),
+            [
+                'user_id' => 'required'
+            ],
+            [
+                'user_id.required' => 'user_id不能为空!'
+            ]
+        );
+
+
+        if ($validator->fails()) {
+            return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
+        }
+        $couponlist = CouponInfoModel::where('end_time', '>', Carbon::now('Asia/Shanghai'))->where('count', '>', 0)->get();
+        foreach ($couponlist as $key => $item) {
+            $had = UserCouponRelationModel::where('coupon_id', $item->id)->where('user_id', request('user_id'))->count();
+            if ($had) {
+                unset($couponlist[$key]);
+            }
+        }
+
+        return $this->api(compact('couponlist'));
     }
 
-    public function myCoupon(){
+    /**
+     * @api {get} /api/home/mycoupon   我的优惠券列表
+     * @apiDescription  我的优惠券列表
+     * @apiGroup Photo
+     * @apiPermission none
+     * @apiVersion 0.1.0
+     * @apiParam {int}       user_id      用户ID
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     * {
+     *     "status": true,
+     *     "status_code": 0,
+     *     "message": "",
+     *     "data": {
+     *              "mycoupons": [
+     *                  {
+     *                      "id": 1,
+     *                      "coupon_id": 1,
+     *                      "user_id": 1,
+     *                      "coupon": {
+     *                          "id": 1,
+     *                          "name": "证件照七折优惠",     //优惠券名字
+     *                          "type": "1",                //优惠券类型 0:固定金额优惠,1:折扣优惠
+     *                          "min_price": 100,           //最低使用金额
+     *                          "discount": "7",            //折扣
+     *                          "discount_price": null,      //优惠金额
+     *                          "product_id": "1,2",         //可使用的产品id
+     *                          "count": 10,
+     *                          "end_time": "2018-07-31",     //有效期
+     *                          "created_at": "2018-07-13 07:29:27",
+     *                          "updated_at": "2018-07-18 06:36:38"
+     *                          }
+     *                  }
+     *              ]
+     *          }
+     * }
+     * @apiErrorExample {json} Error-Response:
+     * HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     * 可能出现的错误代码:
+     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
+     */
+    public function myCoupon(Request $request)
+    {
+        $validator = Validator::make($request->all(),
+            [
+                'user_id' => 'required'
+            ],
+            [
+                'user_id.required' => 'user_id不能为空!'
+            ]
+        );
+
+
+        if ($validator->fails()) {
+            return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
+        }
+
+        $mycoupons = UserCouponRelationModel::where('user_id', request('user_id'))->with('coupon')->orderBy('created_at', 'desc')->get();
+        $mycoupons = $mycoupons->filter(function ($value) {
+            if (strtotime($value->coupon->end_time) < strtotime(Carbon::now('Asia/Shanghai'))) {
+                return false;
+            } else {
+                return true;
+            }
+
+        });
+        return $this->api(compact('mycoupons'));
+    }
+
+
+    /**
+     * @api {get} /api/home/mycoupon   我的优惠券列表
+     * @apiDescription  我的优惠券列表
+     * @apiGroup Photo
+     * @apiPermission none
+     * @apiVersion 0.1.0
+     * @apiParam {int}       user_id      用户ID
+     * @apiSuccessExample {json} Success-Response:
+     * HTTP/1.1 200 OK
+     * {
+     *     "status": true,
+     *     "status_code": 0,
+     *     "message": "",
+     *     "data": {
+     *              "res": true         //领取成功
+     * }
+     * }
+     * @apiErrorExample {json} Error-Response:
+     * HTTP/1.1 400 Bad Request
+     * {
+     *     "state": false,
+     *     "code": 1000,
+     *     "message": "传入参数不正确",
+     *     "data": null or []
+     * }
+     *
+     * {
+     *      "status": true,
+     *      "status_code": 0,
+     *      "message": "",
+     *      "data": {
+     *          "code": 10001,
+     *          "msg": "你已经领取过改优惠券,不能重复领取!"
+     *      }
+     * }
+     * 可能出现的错误代码:
+     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
+     *    1001    CLIENT_WRONG_PARAMS             你已经领取过改优惠券,不能重复领取
+     */
+    public function getCoupon(Request $request)
+    {
+        $validator = Validator::make($request->all(),
+            [
+                'user_id' => 'required',
+                'coupon_id' => 'required'
+
+            ],
+            [
+                'user_id.required' => 'user_id不能为空!',
+                'coupon_id.required' => 'coupon_id不能为空!'
+            ]
+        );
+
+        if ($validator->fails()) {
+            return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
+        }
+
+        $had = UserCouponRelationModel::where('coupon_id', request('coupon_id'))->where('user_id', request('user_id'))->count();
+        if ($had) {
+            $data = [
+                'code' => 10001,
+                'msg' => '你已经领取过改优惠券,不能重复领取!'
+            ];
+
+            return $this->api($data);
+        }
+
+        $coupon = CouponInfoModel::find(request('coupon_id'));
+        if ($coupon->count > 0) {
+            $coupon->count = $coupon->count - 1;
+            $res = $coupon->save();
+
+            $data = $request->all();
+            UserCouponRelationModel::create($data);
+
+            return $this->api(compact('res'));
+
+        } else {
+            $data = [
+                'code' => 10002,
+                'msg' => '改优惠券已被领取完毕!'
+            ];
+
+            return $this->api($data);
+        }
+
 
     }
 

+ 50 - 0
app/Models/UserCouponRelationModel.php

xqd
@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Models;
+
+use App\Models\BaseModel;
+
+/**
+ * @description 优惠券列表
+ * @author  system;
+ * @version    1.0
+ * @date 2018-07-12 08:35:24
+ *
+ */
+class UserCouponRelationModel extends BaseModel
+{
+    /**
+     * 数据表名
+     *
+     * @var string
+     *
+     */
+    protected $table = 'user_coupon_relation';
+    /**
+     * 主键
+     */
+    protected $primaryKey = 'id';
+
+    //分页
+    protected $perPage = PAGE_NUMS;
+
+    /**
+     * 可以被集体附值的表的字段
+     *
+     * @var string
+     */
+    protected $fillable = [
+        'coupon_id',
+        'user_id',
+        'status'
+    ];
+
+    public function coupon(){
+        return $this->belongsTo('App\Models\CouponInfoModel','coupon_id');
+    }
+
+    public function user(){
+        return $this->belongsTo('App\Models\UserInfoModel','user_id');
+    }
+
+}

+ 12 - 0
routes/api.php

xqd
@@ -49,5 +49,17 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($a
         'as' => 'home.createorder',
         'uses' => 'HomeController@createOrder',
     ]);
+    $api->get('home/couponlist', [
+        'as' => 'home.couponlist',
+        'uses' => 'HomeController@couponList',
+    ]);
+    $api->get('home/mycoupon', [
+        'as' => 'home.mycoupon',
+        'uses' => 'HomeController@myCoupon',
+    ]);
+    $api->get('home/getcoupon', [
+        'as' => 'home.getcoupon',
+        'uses' => 'HomeController@getCoupon',
+    ]);
 
 });