From 88ecb571aa6635e53ffb866512e732d0a3148e06 Mon Sep 17 00:00:00 2001 From: "ZLW-PC\\Administrator" <374861669@qq.com> Date: Mon, 23 Oct 2017 17:33:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/CartController.php | 110 +++++++++++++++++++ app/Http/Controllers/Api/GoodsController.php | 3 +- app/Http/Model/Cart.php | 105 +++++++++--------- app/Http/Model/Goods.php | 2 +- routes/web.php | 1 + 5 files changed, 169 insertions(+), 52 deletions(-) create mode 100644 app/Http/Controllers/Api/CartController.php diff --git a/app/Http/Controllers/Api/CartController.php b/app/Http/Controllers/Api/CartController.php new file mode 100644 index 0000000..c297708 --- /dev/null +++ b/app/Http/Controllers/Api/CartController.php @@ -0,0 +1,110 @@ +input('limit', 10); + $data['offset'] = $request->input('offset', 0); + $data['user_id'] = Token::$uid; + + $res = Cart::getList($data); + + return ReturnData::create(ReturnData::SUCCESS,$res); + } + + //添加购物车 + public function cartAdd(Request $request) + { + //参数 + $data['type'] = $request->input('type',null); + $data['money'] = $request->input('money',null); + $data['des'] = $request->input('des',null); + if($request->input('user_money', null) !== null){$data['user_money'] = $request->input('user_money');} + $data['add_time'] = time(); + $data['user_id'] = Token::$uid; + + if($data['type']===null || $data['money']===null || $data['des']===null) + { + return ReturnData::create(ReturnData::PARAMS_ERROR); + } + + $res = UserMoney::add($data); + if(!$res) + { + return ReturnData::create(ReturnData::SYSTEM_FAIL); + } + + return ReturnData::create(ReturnData::SUCCESS,$res); + } + + //修改购物车 + public function cartUpdate(Request $request) + { + //参数 + $data['type'] = $request->input('type',null); + $data['money'] = $request->input('money',null); + $data['des'] = $request->input('des',null); + if($request->input('user_money', null) !== null){$data['user_money'] = $request->input('user_money');} + $data['add_time'] = time(); + $data['user_id'] = Token::$uid; + + if($data['type']===null || $data['money']===null || $data['des']===null) + { + return ReturnData::create(ReturnData::PARAMS_ERROR); + } + + $res = UserMoney::add($data); + if($res != true) + { + return ReturnData::create(ReturnData::SYSTEM_FAIL); + } + + return ReturnData::create(ReturnData::SUCCESS,$res); + } + + //删除购物车 + public function cartDelete(Request $request) + { + $id = $request->input('id',''); + + if($id=='') + { + return ReturnData::create(ReturnData::PARAMS_ERROR); + } + + $res = Cart::remove($id,Token::$uid); + if($res == true) + { + return ReturnData::create(ReturnData::SUCCESS,$res); + } + + return ReturnData::create(ReturnData::SYSTEM_FAIL); + } + + //清空购物车 + public function cartClear(Request $request) + { + $res = Cart::clearCart(Token::$uid); + if(!$res) + { + return ReturnData::create(ReturnData::SYSTEM_FAIL); + } + + return ReturnData::create(ReturnData::SUCCESS,$res); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/GoodsController.php b/app/Http/Controllers/Api/GoodsController.php index 0afafe3..e877c2a 100644 --- a/app/Http/Controllers/Api/GoodsController.php +++ b/app/Http/Controllers/Api/GoodsController.php @@ -17,7 +17,8 @@ class GoodsController extends CommonController public function goodsDetail(Request $request) { //参数 - $data['id'] = $request->input('id'); + $data['id'] = $request->input('id',''); + if($data['id']==''){return ReturnData::create(ReturnData::PARAMS_ERROR);} $res = Goods::goodsDetail($data); diff --git a/app/Http/Model/Cart.php b/app/Http/Model/Cart.php index 6f521f2..51413ed 100644 --- a/app/Http/Model/Cart.php +++ b/app/Http/Model/Cart.php @@ -38,71 +38,45 @@ class Cart extends BaseModel const CART_EXCHANGE_GOODS = 4; // 积分商城 //获取列表 - public static function getList($uid) + public static function getList(array $param) { + extract($param); //参数:limit,offset + + $limit = isset($limit) ? $limit : 10; + $offset = isset($offset) ? $offset : 0; + $goods = self::join('goods', 'goods.id', '=', 'cart.goods_id') - ->where('cart.user_id', $uid) - ->where('goods.status', 0) - ->select('cart.*') + ->where('cart.user_id', $user_id) + ->where('goods.status', Goods::STATUS) + ->select('cart.*','goods.id as goods_id','goods.title','goods.sn','goods.price as goods_price','goods.market_price','goods.litpic as goods_thumb_img','goods.goods_number as stock','goods.promote_start_date','goods.promote_price','goods.promote_end_date') + ->skip($offset)->take($limit) ->get(); if($goods) { - foreach ($goods as $key => $value) + foreach ($goods as $k => $v) { + $goods[$k]->is_promote = 0; + if(Goods::bargain_price($v->goods_price,$v->promote_start_date,$v->promote_end_date) > 0){$goods[$k]->is_promote = 1;} + + //订货数量大于0 - if ($value->goods_number > 0) + if ($v->goods_number > 0) { - $goods->goods_price = $goods_price = Goods::get_final_price($value->goods_id); + $goods[$k]->price = $goods_price = Goods::get_final_price($v->goods_id); //更新购物车中的商品数量 - self::where('id', $value->id)->update(array('goods_price' => $goods_price)); - + self::where('id', $v->id)->update(array('price' => $goods_price)); } } } - return $goods->toArray(); - } - - /** - * 添加商品到购物车 - * - * @access public - * @param integer $goods_id 商品编号 - * @param integer $num 商品数量 - * @param json $property 规格值对应的id json数组 - * @return boolean - */ - public static function cartAdd(array $attributes) - { - extract($attributes); - - //获取商品信息 - $good = Goods::where(['goods_id' => $goods_id, 'status' => 0])->first(); - - if (!$good) - { - return '商品不存在'; - } - - if (isset($property) && json_decode($property,true)) - { - $property = json_decode($property,true); - } - else - { - $property = []; - } - - + return $goods; } - public static function getOne($id) + public static function getOne($where) { - $where['id'] = $id; - - $goods = self::where($where)->first()->toArray(); + $goods = self::where($where)->first(); return $goods; } @@ -113,7 +87,7 @@ class Cart extends BaseModel { return $id; } - + return false; } @@ -128,9 +102,9 @@ class Cart extends BaseModel } //删除一条记录 - public static function remove($id) + public static function remove($id,$user_id) { - if (!self::whereIn('id', explode(',', $id))->delete()) + if (self::whereIn('id', explode(',', $id))->where('user_id',$user_id)->delete() === false) { return false; } @@ -138,6 +112,37 @@ class Cart extends BaseModel return true; } + /** + * 添加商品到购物车 + * + * @access public + * @param integer $goods_id 商品编号 + * @param integer $num 商品数量 + * @param json $property 规格值对应的id json数组 + * @return boolean + */ + public static function cartAdd(array $attributes) + { + extract($attributes); + + //获取商品信息 + $goods = Goods::where(['goods_id' => $goods_id, 'status' => Goods::STATUS])->first(); + + if (!$goods) + { + return '商品不存在'; + } + + if (isset($property) && json_decode($property,true)) + { + $property = json_decode($property,true); + } + else + { + $property = []; + } + } + /** * 清空购物车 * diff --git a/app/Http/Model/Goods.php b/app/Http/Model/Goods.php index e27c08f..b2d7b7d 100644 --- a/app/Http/Model/Goods.php +++ b/app/Http/Model/Goods.php @@ -211,7 +211,7 @@ class Goods extends BaseModel */ public static function bargain_price($price, $start, $end) { - if ($price == 0) + if ($price <= 0) { return 0; } diff --git a/routes/web.php b/routes/web.php index 8cfe60a..cf9aa5c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -160,6 +160,7 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web Route::post('/cart_clear', 'CartController@cartClear'); //清空购物车 Route::post('/cart_add', 'CartController@cartAdd'); //添加购物车 Route::post('/cart_update', 'CartController@cartUpdate'); //修改购物车 + Route::post('/cart_delete', 'CartController@cartDelete'); //删除购物 //分销 //积分