Browse Source

购物车

master
ZLW-PC\Administrator 7 years ago
parent
commit
88ecb571aa
  1. 110
      app/Http/Controllers/Api/CartController.php
  2. 3
      app/Http/Controllers/Api/GoodsController.php
  3. 105
      app/Http/Model/Cart.php
  4. 2
      app/Http/Model/Goods.php
  5. 1
      routes/web.php

110
app/Http/Controllers/Api/CartController.php

@ -0,0 +1,110 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\Common\ReturnData;
use App\Common\Token;
use App\Http\Model\Cart;
class CartController extends CommonController
{
public function __construct()
{
parent::__construct();
}
//购物车列表
public function cartList(Request $request)
{
//参数
$data['limit'] = $request->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);
}
}

3
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);

105
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 = [];
}
}
/**
* 清空购物车
*

2
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;
}

1
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'); //删除购物
//分销
//积分

Loading…
Cancel
Save