From c6c7c6b1e9b5e34844b9f861c04de79468ba8a3b Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Wed, 22 Nov 2017 17:33:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E7=AE=97=E6=97=B6=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/UserBonusController.php | 28 ++++++++++---- .../Controllers/Weixin/CartController.php | 9 +++++ app/Http/Model/UserBonus.php | 38 +++++++++++++++++-- resources/org/wxJsSdk/jssdk.php | 2 +- routes/web.php | 1 + 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Api/UserBonusController.php b/app/Http/Controllers/Api/UserBonusController.php index 3a7f8f1..2a6bdf3 100644 --- a/app/Http/Controllers/Api/UserBonusController.php +++ b/app/Http/Controllers/Api/UserBonusController.php @@ -44,13 +44,7 @@ class UserBonusController extends CommonController return ReturnData::create(ReturnData::PARAMS_ERROR); } - $res = UserBonus::add($data); - if($res == false) - { - return ReturnData::create(ReturnData::SYSTEM_FAIL); - } - - return ReturnData::create(ReturnData::SUCCESS,$res); + return UserBonus::add($data); } //用户删除优惠券 @@ -70,4 +64,24 @@ class UserBonusController extends CommonController return ReturnData::create(ReturnData::SUCCESS,$res); } + + public function userAvailableBonusList(Request $request) + { + //参数 + $data['user_id'] = Token::$uid; + + $data['min_amount'] = $request->input('min_amount',''); + if($data['min_amount']=='') + { + return ReturnData::create(ReturnData::PARAMS_ERROR); + } + + $res = UserBonus::getAvailableBonusList($data); + if($res == false) + { + return ReturnData::create(ReturnData::SYSTEM_FAIL); + } + + return ReturnData::create(ReturnData::SUCCESS,$res); + } } \ No newline at end of file diff --git a/app/Http/Controllers/Weixin/CartController.php b/app/Http/Controllers/Weixin/CartController.php index 910979c..11264c9 100644 --- a/app/Http/Controllers/Weixin/CartController.php +++ b/app/Http/Controllers/Weixin/CartController.php @@ -87,6 +87,15 @@ class CartController extends CommonController if($data['checkout_goods']['total_price']>$data['user_info']['money']){$is_balance_enough = 0;} $data['is_balance_enough'] = $is_balance_enough; + //获取用户优惠券列表 + $postdata = array( + 'min_amount' => $data['checkout_goods']['total_price'], + 'access_token' => $_SESSION['weixin_user_info']['access_token'] + ); + $url = env('APP_API_URL')."/user_available_bonus_list"; + $res = curl_request($url,$postdata,'GET'); + $data['bonus_list'] = $res['data']; + return view('weixin.cart.cartCheckout', $data); } } \ No newline at end of file diff --git a/app/Http/Model/UserBonus.php b/app/Http/Model/UserBonus.php index 7bcf5d6..f77abfd 100644 --- a/app/Http/Model/UserBonus.php +++ b/app/Http/Model/UserBonus.php @@ -1,5 +1,7 @@ $data['bonus_id'],'user_id'=>$data['user_id']])->first()){return '亲,您已获取!';} + if(!Bonus::where(['id'=>$data['bonus_id']])->where('num',-1)->first() && !Bonus::where(['id'=>$data['bonus_id']])->where('num','>',0)->first()) + { + return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您来晚了啦,已被抢光了'); + } + + if(self::where(['bonus_id'=>$data['bonus_id'],'user_id'=>$data['user_id']])->first()){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您已获取!');} if ($id = self::insertGetId($data)) { - return $id; + DB::table('bonus')->where(array('id'=>$data['bonus_id']))->decrement('num', 1); + + return ReturnData::create(ReturnData::SUCCESS,$id); } - - return false; + + return ReturnData::create(ReturnData::SYSTEM_FAIL); } public static function modify($where, array $data) @@ -91,4 +100,25 @@ class UserBonus extends BaseModel return true; } + + //商品结算时,获取优惠券列表 + public static function getAvailableBonusList(array $param) + { + extract($param); + + $where['user_bonus.user_id'] = $user_id; + if(isset($status)){$where['bonus.status'] = 0;} + + $model = new UserBonus; + if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount);} //满多少使用 + if(isset($bonus_end_time)){$model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s'));} //有效期 + + $bonus_list = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where) + ->select('bonus.*', 'user_bonus.user_id', 'user_bonus.used_time', 'user_bonus.get_time', 'user_bonus.status as user_bonus_status', 'user_bonus.id as user_bonus_id') + ->orderBy('bonus.money','desc')->get(); + + $res['list'] = $bonus_list; + + return $res; + } } \ No newline at end of file diff --git a/resources/org/wxJsSdk/jssdk.php b/resources/org/wxJsSdk/jssdk.php index fa1778d..ce694be 100644 --- a/resources/org/wxJsSdk/jssdk.php +++ b/resources/org/wxJsSdk/jssdk.php @@ -17,7 +17,7 @@ class JSSDK // 注意 URL 一定要动态获取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; - + $timestamp = time(); $nonceStr = $this->createNonceStr(); diff --git a/routes/web.php b/routes/web.php index 437ec56..de18739 100644 --- a/routes/web.php +++ b/routes/web.php @@ -183,6 +183,7 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web Route::get('/user_point_list', 'UserPointController@userPointList'); //用户积分列表 Route::post('/user_point_add', 'UserPointController@userPointAdd'); //优惠券 + Route::get('/user_available_bonus_list', 'UserBonusController@userAvailableBonusList'); //用户结算时获取可用优惠券列表 Route::get('/user_bonus_list', 'UserBonusController@userBonusList'); //用户优惠券列表 Route::post('/user_bonus_add', 'UserBonusController@userBonusAdd'); //用户获取优惠券 Route::get('/bonus_list', 'BonusController@bonusList'); //可用获取的优惠券列表