Browse Source

立即购买

master
林一峰 7 years ago
parent
commit
f7202fcf63
  1. 8
      app/Http/Controllers/Api/CartController.php
  2. 13
      app/Http/Model/Cart.php
  3. 24
      resources/views/weixin/cart/index.blade.php
  4. 11
      resources/views/weixin/goods/goodsDetail.blade.php
  5. 1
      routes/web.php

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

@ -42,13 +42,7 @@ class CartController extends CommonController
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = Cart::cartAdd($data);
if($res !== true)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL,null,$res);
}
return ReturnData::create(ReturnData::SUCCESS);
return Cart::cartAdd($data);
}
//删除购物车

13
app/Http/Model/Cart.php

@ -1,5 +1,6 @@
<?php
namespace App\Http\Model;
use App\Common\ReturnData;
class Cart extends BaseModel
{
@ -119,19 +120,19 @@ class Cart extends BaseModel
if (!$goods)
{
return '商品不存在';
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'商品不存在');
}
//判断库存 是否足够
if($goods['goods_number']<$goods_number)
{
return '库存不足';
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'库存不足');
}
//判断购物车商品数
if(Cart::where(['user_id'=>$user_id])->count() >= 20)
{
return '购物车商品最多20件';
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'购物车商品最多20件');
}
//查看是否已经有购物车插入记录
@ -151,6 +152,8 @@ class Cart extends BaseModel
);
self::where(array('id'=>$cart->id))->update($updateArr);
$cart_id = $cart->id;
}
else
{
@ -162,10 +165,10 @@ class Cart extends BaseModel
'add_time' => time(),
);
self::insertGetId($cartInsert);
$cart_id = self::insertGetId($cartInsert);
}
return true;
return ReturnData::create(ReturnData::SUCCESS,$cart_id,'购物车添加成功');
}
/**

24
resources/views/weixin/cart/index.blade.php

@ -31,7 +31,7 @@
<!--商品勾选按钮-->
<span onclick="checkGoods(this)" class="che">
<i>
<input name="checkItem" type="checkbox" style="display:none;" data-goods-id="<?php echo $v['goods_id']; ?>">
<input name="checkItem" type="checkbox" style="display:none;" data-goods-id="<?php echo $v['goods_id']; ?>" data-cart-id="<?php echo $v['id']; ?>">
</i>
</span>
</div>
@ -117,6 +117,28 @@
<br><br>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/layer/mobile/layer.js"></script>
<script>
function cart_submit()
{
var cart_goods_ids = '';
$('[name="checkItem"][checked]').each(function(){
var goods_id = $(this).attr('data-cart-id');
if(cart_goods_ids){cart_goods_ids = cart_goods_ids+'_'+goods_id;}else{cart_goods_ids = cart_goods_ids+goods_id;}
});
if(cart_goods_ids == '')
{
layer.open({
content: '请选择商品'
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
return false;
}
location.href = '<?php echo substr(route('weixin_cart_checkout',array('ids'=>1)), 0, -1); ?>' + cart_goods_ids;
}
function change_goods_number(type, id)
{
var goods_number = document.getElementById('goods_number'+id).value;

11
resources/views/weixin/goods/goodsDetail.blade.php

@ -197,17 +197,18 @@ function dosubmit()
{
if(res.code==0)
{
if(cart_type == 2)
{
location.href = '<?php echo substr(route('weixin_cart_checkout',array('ids'=>1)),0,-1); ?>' + res.data;
return false;
}
//提示
layer.open({
content: res.msg
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
/* if(cart_type == 2)
{
location.href = '<?php echo substr(route('weixin_order_pay',array('id'=>1)),0,strlen(route('weixin_order_pay',array('id'=>1)))-1); ?>' + res.data['id'];
} */
}
else
{

1
routes/web.php

@ -92,6 +92,7 @@ Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin', 'middleware' => ['w
Route::get('/collect_goods', 'CollectGoodsController@index')->name('weixin_user_collect_goods');
//购物车
Route::get('/cart', 'CartController@index')->name('weixin_cart');
Route::get('/cart_checkout/{ids}', 'CartController@cartCheckout')->name('weixin_cart_checkout');
//订单
Route::get('/order_pay/{id}', 'OrderController@orderPay')->name('weixin_order_pay'); //订单支付
//收货地址

Loading…
Cancel
Save