Browse Source

购物车结算

master
林一峰 7 years ago
parent
commit
e71cb0ef47
  1. 15
      app/Http/Controllers/Api/CartController.php
  2. 8
      app/Http/Controllers/Api/UserAddressController.php
  3. 13
      app/Http/Controllers/Weixin/CartController.php
  4. 34
      app/Http/Model/Cart.php
  5. 10
      app/Http/Model/UserAddress.php
  6. 3
      resources/views/weixin/address/userAddressAdd.blade.php
  7. 266
      resources/views/weixin/cart/cartCheckout.blade.php
  8. 2
      routes/web.php

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

@ -75,4 +75,19 @@ class CartController extends CommonController
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//购物车结算商品列表
public function cartCheckoutGoodsList(Request $request)
{
//参数
$data['ids'] = $request->input('ids','');
$data['user_id'] = Token::$uid;
if($data['ids']=='')
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
return Cart::cartCheckoutGoodsList($data);
}
}

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

@ -84,13 +84,7 @@ class UserAddressController extends CommonController
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserAddress::add($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
return UserAddress::add($data);
}
//修改收货地址

13
app/Http/Controllers/Weixin/CartController.php

@ -35,4 +35,17 @@ class CartController extends CommonController
return view('weixin.cart.index', $data);
}
//购物车结算
public function cartCheckout($ids)
{
$postdata = array(
'ids' => $ids,
'access_token' => $_SESSION['weixin_user_info']['access_token']
);
$url = env('APP_API_URL')."/cart_checkout_goods_list";
$res = curl_request($url,$postdata,'GET');
$data['list'] = $res['data']['list'];
return view('weixin.cart.cartCheckout', $data);
}
}

34
app/Http/Model/Cart.php

@ -188,4 +188,38 @@ class Cart extends BaseModel
{
return self::where('user_id',$user_id)->sum('goods_number');
}
//购物车结算商品列表
public static function cartCheckoutGoodsList(array $param)
{
extract($param);
$cartIds = explode("_",$ids);
// 获取购物车列表
$cartList = self::where(array('user_id'=>$user_id))->whereIn('id', $cartIds)->get();
if(!empty($cartList))
{
$resultList = array();
$checkArr = array();
foreach($cartList as $k=>$v)
{
$goods = Goods::where(array('id'=>$v['goods_id']))->first();
$cartList[$k]->is_promote = 0;
if(Goods::bargain_price($goods->price,$goods->promote_start_date,$goods->promote_end_date) > 0){$cartList[$k]->is_promote = 1;}
$cartList[$k]->final_price = Goods::get_final_price($v['goods_id']); //商品最终价格
$cartList[$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v['goods_id']));
$cartList[$k]->title = $goods->title;
$cartList[$k]->litpic = $goods->litpic;
}
}
$res['list'] = $cartList;
return ReturnData::create(ReturnData::SUCCESS,$res);
}
}

10
app/Http/Model/UserAddress.php

@ -1,5 +1,6 @@
<?php
namespace App\Http\Model;
use App\Common\ReturnData;
class UserAddress extends BaseModel
{
@ -96,6 +97,11 @@ class UserAddress extends BaseModel
{
extract($param);
if(UserAddress::where('user_id', $user_id)->count() >= 3)
{
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'最多10个收货地址');
}
$model = new UserAddress;
$model->user_id = $user_id;
$model->name = $name;
@ -119,10 +125,10 @@ class UserAddress extends BaseModel
self::setDefault($model->id,$user_id);
}
return $model->toArray();
return ReturnData::create(ReturnData::SUCCESS,$model);
}
return false;
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
public static function modify(array $param)

3
resources/views/weixin/address/userAddressAdd.blade.php

@ -260,8 +260,7 @@ function adr_dosubmit()
,time: 2 //2秒后自动关闭
});
var url = "http://www.baidu.com";
location.href = url;
location.href = "<?php echo route('weixin_user_address_list'); ?>";
}
},'json');
}

266
resources/views/weixin/cart/cartCheckout.blade.php

@ -0,0 +1,266 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>确认订单</title><meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<link href="<?php echo env('APP_URL'); ?>/css/weixin/style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/weixin/mobile.js"></script>
<meta name="keywords" content="关键词"><meta name="description" content="描述"></head><body>
<div class="classreturn loginsignup">
<div class="ds-in-bl return"><a href="javascript:history.back(-1);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/return.png" alt="返回"></a></div>
<div class="ds-in-bl tit center"><span>购物车</span></div>
<div class="ds-in-bl nav_menu"><a href="javascript:void(0);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/class1.png" alt="菜单"></a></div>
</div>
<div class="flool tpnavf cl">
<div class="nav_list">
<ul>
<a href="<?php echo route('weixin'); ?>"><li><img src="<?php echo env('APP_URL'); ?>/images/weixin/home_icon.png"><p>首页</p></li></a>
<a href="/Weixin/index.php?m=Store&amp;a=index"><li><img src="<?php echo env('APP_URL'); ?>/images/weixin/brand_icon.png"><p>分类</p></li></a>
<a href="/Weixin/index.php?m=Cart&amp;a=index"><li><img src="<?php echo env('APP_URL'); ?>/images/weixin/car_icon.png"><p>购物车</p></li></a>
<a href="/Weixin/index.php?m=User&amp;a=index"><li><img src="<?php echo env('APP_URL'); ?>/images/weixin/center_icon.png"><p>个人中心</p></li></a></ul>
<div class="cl"></div>
</div>
</div>
<div class="cart_list">
<!--商品列表-s-->
<?php if($list){foreach($list as $k=>$v){ ?>
<div class="sc_list" id="cart_list_<?php echo $v['id']; ?>">
<div class="radio-img">
<div class="radio fl ">
<!--商品勾选按钮-->
<span onclick="checkGoods(this)" class="che">
<i>
<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>
<div class="shopimg fl">
<a href="<?php echo $v['goods_detail_url']; ?>">
<img src="<?php echo $v['litpic']; ?>">
</a>
</div>
</div>
<div class="deleshow">
<div class="deletes">
<!--商品名-->
<p class="tit"><?php echo $v['title']; ?></p>
<!--删除按钮-->
<a href="javascript:void(0);" class="delescj deleteGoods" data-cart-id="<?php echo $v['id']; ?>"><img src="<?php echo env('APP_URL'); ?>/images/weixin/dele.png"></a>
</div>
<!--商品属性,规格-->
<p class="weight"></p>
<div class="prices">
<p class="sc_pri fl">
<!--商品单价-->
<span></span><span id="goods_price<?php echo $v['goods_id']; ?>"><?php echo $v['final_price']; ?></span>
</p>
<!--加减数量-->
<div class="plus fr get_mp">
<span class="mp_minous" onClick="change_goods_number(1,<?php echo $v['goods_id']; ?>)">-</span>
<span class="mp_mp"><input name="goods_number<?php echo $v['goods_id']; ?>" type="text" id="goods_number<?php echo $v['goods_id']; ?>" value="<?php echo $v['goods_number']; ?>" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" autocomplete="off" value="1" onblur="change_goods_number(2,<?php echo $v['goods_id']; ?>)" class="input-num"></span>
<span class="mp_plus" onClick="change_goods_number(3,<?php echo $v['goods_id']; ?>)">+</span>
</div>
</div>
</div>
</div>
<?php }} ?>
<!--商品列表-e-->
<!--提交栏-s-->
<div class="foohi foohiext">
<div class="payit ma-to-20 payallb">
<div class="radio fl">
<span class="che alltoggle checkFull" onclick="checkGoods(this)">
<i></i>
</span>
<span class="all">全选</span>
</div>
<div class="fr">
<a href="javascript:void(0);" onclick="cart_submit();">去结算</a>
</div>
<div class="youbia">
<p><span class="pmo">总计:</span><span></span><span id="total_fee">0</span></p>
</div>
</div>
</div>
<!--提交栏-e-->
</div>
<?php if(!$list){ ?>
<!--购物车没有商品-start-->
<div class="cart_list">
<div class="nonenothing">
<img src="<?php echo env('APP_URL'); ?>/images/weixin/nothing.png">
<p>购物车暂无商品</p>
<a href="<?php echo route('weixin'); ?>">去逛逛</a>
</div>
</div>
<br><br>
<!--猜你喜欢-start-->
<div class="floor">
<div class="banner_headline">
<div class="tit">
<h4>猜你喜欢</h4>
</div>
</div>
<div class="likeshop">
<ul class="goods_list">
<?php if($like_goods_list){foreach($like_goods_list as $k=>$v){ ?>
<li><a href="<?php echo $v['goods_detail_url']; ?>"><img alt="<?php echo $v['title']; ?>" src="<?php echo $v['litpic']; ?>"><div class="goods_info"><p class="goods_tit"><?php echo $v['title']; ?></p><div class="goods_price">¥<b><?php echo $v['price']; ?></b></div></div></a></li>
<?php }} ?>
</ul>
</div>
</div>
<!--购物车没有商品-end-->
<?php } ?>
<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;
if(type != 2)
{
var goods_number = document.getElementById('goods_number'+id).value;
document.getElementById('goods_number'+id).value = goods_number;
}
if(type == 1){goods_number--;}
if(type == 3){goods_number++;}
if(goods_number <= 0){goods_number=1;}
if(!/^[0-9]*$/.test(goods_number)){goods_number = document.getElementById('goods_number'+id).value;}
document.getElementById('goods_number'+id).value = goods_number;
var access_token = '<?php echo $_SESSION['weixin_user_info']['access_token']; ?>';
var url = '<?php echo env('APP_API_URL').'/cart_add'; ?>';
$.post(url,{access_token:access_token,goods_id:id,goods_number:goods_number},function(res)
{
if (res.code == 0)
{
changeCartTotalPrice();
}
else if (res.msg != '')
{
//提示
layer.open({
content: '姓名不能为空'
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
var goods_number = document.getElementById('goods_number'+id).value;
document.getElementById('goods_number'+id).value = goods_number;
}
}, 'json');
}
//删除购物车商品
$(function () {
//删除购物车商品事件
$(document).on("click", '.deleteGoods', function (e) {
var access_token = '<?php echo $_SESSION['weixin_user_info']['access_token']; ?>';
var cart_ids = new Array();
cart_ids.push($(this).attr('data-cart-id'));
layer.open({
content: '确定要删除此商品吗'
,btn: ['确定', '取消']
,yes: function(index){
layer.close(index);
$.ajax({
type : "POST",
url:"<?php echo env('APP_API_URL').'/cart_delete'; ?>",
dataType:'json',
data: {access_token:access_token,id:cart_ids},
success: function(res){
layer.open({
content: res.msg
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
window.location.reload();
}
});
}
});
})
});
//勾选商品
function checkGoods(obj)
{
if($(obj).hasClass('check_t'))
{
//改变颜色
$(obj).removeClass('check_t');
//取消选中
$(obj).find('input').attr('checked',false);
}
else
{
//改变颜色
$(obj).addClass('check_t');
//勾选选中
$(obj).find('input').attr('checked',true);
}
//选中全选多选框
if($(obj).hasClass('checkFull'))
{
if($(obj).hasClass('check_t'))
{
$(".che").each(function(i,o){
$(this).addClass('check_t');
$(this).find('input').attr('checked',true);
});
}
else
{
$(".che").each(function(i,o){
$(this).removeClass('check_t');
$(this).find('input').attr('checked',false);
});
}
}
changeCartTotalPrice();
}
//修改选中商品总价
function changeCartTotalPrice()
{
var total_price = 0;
$('[name="checkItem"][checked]').each(function(){
var goods_id = $(this).attr('data-goods-id');
total_price = total_price + $('#goods_number'+goods_id).val() * $('#goods_price'+goods_id).text();
});
$('#total_fee').text(total_price);
}
</script>
</body></html>

2
routes/web.php

@ -164,6 +164,8 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web
Route::post('/cart_clear', 'CartController@cartClear'); //清空购物车
Route::post('/cart_add', 'CartController@cartAdd'); //添加购物车
Route::post('/cart_delete', 'CartController@cartDelete'); //删除购物
Route::get('/cart_checkout_goods_list', 'CartController@cartCheckoutGoodsList'); //购物车结算商品列表
//分销
//积分

Loading…
Cancel
Save