作者 v_bairong06

购物车前后台逻辑梳理、订单确认页功能梳理

... ... @@ -39,6 +39,24 @@ class CartModel extends CommonModel {
->select();
}
// 获取用户购物车某一商品
public function getOneByUser($user_id,$goods_id) {
$where['c.user_id'] = $user_id;
$where['c.goods_id'] = $goods_id;
return $this->field(array('c.*,g.num as goods_num,g.goods_price'))
->alias('c')
->join('__GOODS__ as g on c.goods_id = g.id')
->where($where)
->find();
}
// 获取用户购物车某一商品数
public function getOneNumByUser($user_id,$goods_id) {
$where['user_id'] = $user_id;
$where['goods_id'] = $goods_id;
return $this->where($where)->getField('num');
}
// 获取用户购物车总商品数
public function getCountByUser($user_id) {
$where['user_id'] = $user_id;
... ...
... ... @@ -63,4 +63,11 @@ class GoodsModel extends CommonModel {
$where['id'] = $id;
return $this->where($where)->find();
}
// 获取商品库存
public function getInfoNum($id) {
$where['is_del'] = 0;
$where['id'] = $id;
return $this->where($where)->getField('num');
}
}
\ No newline at end of file
... ...
... ... @@ -17,13 +17,13 @@ class OrderModel extends CommonModel {
//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
array('order_sn', 'require', '订单号不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('user_id', 'number', '用户ID不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('country', 'require', '国家不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('province', 'require', '省、城市不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('city', 'require', '城市不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('region', 'require', '地区不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('address', 'require', '详细地址不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('realname', 'require', '收货人姓名不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('mobile', 'require', '收货人电话不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('country', 'require', '国家不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('province', 'require', '省、城市不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('city', 'require', '城市不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('region', 'require', '地区不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('address', 'require', '详细地址不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('realname', 'require', '收货人姓名不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
// array('mobile', 'require', '收货人电话不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
array('pay_sort', array(1,2), '支付方式错误', 1, 'regex', CommonModel::MODEL_BOTH),
array('price_count', 'require', '订单价格不能为空', 1, 'regex', CommonModel::MODEL_BOTH),
);
... ...
... ... @@ -90,6 +90,15 @@ class GoodsController extends HomebaseController {
$this->ajaxReturn(array('status'=>false,'msg'=>'该商品不存在'));
}
$cartGoodsList = $this->cart_model->getListByUser($user_id);
$cartNum = $this->cart_model->getOneNumByUser($user_id,$goods_id);
$goodsNum = $this->goods_model->getInfoNum($goods_id);
if($cartNum >= $goodsNum) {
$this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->save(array('num'=>$goodsNum));
$this->ajaxReturn(array('status'=>false,'msg'=>'商品库存不足'));
}
if(($cartNum+$num) > $goodsNum) {
$this->ajaxReturn(array('status'=>false,'msg'=>'商品库存不足'));
}
if(in_array($goods_id,$cartGoodsList)) {
$result = $this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->setInc('num',$num);
} else {
... ...
... ... @@ -41,6 +41,89 @@ class CartController extends MemberbaseController {
}
/**
* 修改购物车商品数量
* @param id 商品id
* @param sort 类型(增加/减少)
*/
public function changeNum() {
if(IS_AJAX) {
$user_id = sp_get_current_userid();
$id = I('post.id',0,'intval');
$sort = I('post.sort');
$num = I('post.num',0,'intval');
if($sort == 'reduce') {
$num = -1;
}
// 判断传参是否正常
if(!$num) {
$this->ajaxReturn(array('status'=>false,'msg'=>'请输入正确的商品数量'));
}
if($sort != 'plus' && $sort != 'reduce' && $sort != 'change') {
$this->ajaxReturn(array('status'=>false,'msg'=>'参数错误'));
}
// 判断购物车及商品数据
$info = $this->cart_model->getOneByUser($user_id,$id);
if(!$info) {
$this->ajaxReturn(array('status'=>false,'msg'=>'购物车不存在该商品'));
}
if($sort == 'plus' || $sort == 'reduce') {
if(($info['num']+$num)>$info['goods_num']) {
$this->ajaxReturn(array('status'=>false,'msg'=>'商品库存不足'));
}
$info['num'] += $num;
}
if($sort == 'change') {
if(($num)>$info['goods_num']) {
$this->ajaxReturn(array('status'=>false,'msg'=>'商品库存不足'));
}
$info['num'] = $num;
}
// 更新购物车数据(价格及数量)
$this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$id))->save(array('num'=>$info['num'],'price'=>$info['goods_price']));
$info['amount'] = $info['num'] * $info['goods_price'];
$info['sort'] = $sort;
$this->ajaxReturn(array('status'=>true,'msg'=>'修改成功','data'=>$info));
} else {
$this->error('非法操作');
}
}
/**
* 删除(去付款)购物车商品
* @param ids 要删除(去付款)的商品
*/
public function del() {
if(IS_AJAX) {
$user_id = sp_get_current_userid();
$ids = I('post.ids');
$sort = I('post.sort');
$redirect_url = '';
if($sort != 'delete' && $sort != 'topay') {
$this->ajaxReturn(array('status'=>false,'msg'=>'参数错误'));
}
if($sort == 'delete') {
$name = '删除';
}
if($sort == 'topay') {
$redirect_url = U('User/Cart/confirm',array('ids'=>implode(',',$ids)));
$name = '购买';
}
if(!$ids) {
$this->ajaxReturn(array('status'=>false,'msg'=>'请选择要'.$name.'的商品'));
}
if($sort == 'delete') {
$result = $this->cart_model->where(array('goods_id'=>array('in',$ids),'user_id'=>$user_id))->delete();
if(!$result) {
$this->ajaxReturn(array('status'=>false,'msg'=>'删除失败'));
}
}
$this->ajaxReturn(array('status'=>true,'msg'=>$name.'成功','data'=>$redirect_url));
} else {
$this->error('非法操作');
}
}
/**
* 确认订单页面
*/
public function confirm() {
... ... @@ -55,11 +138,73 @@ class CartController extends MemberbaseController {
$cartList[$k]['amount'] = $v['goods_price']*$v['num'];
$count_amount += $cartList[$k]['amount'];
}
$this->assign('list',$cartList);
$this->assign('cartList',$cartList);
$this->assign('count_amount', $count_amount);
$this->display();
} else {
$this->error('非法操作');
}
}
/**
* 确认订单
* @param address_id 收货地址信息
* @param ids 购买的商品id
*/
public function createOrder() {
if(IS_AJAX) {
$user_id = $info['user_id'] = sp_get_current_userid();
if(!$user_id) {
$this->ajaxReturn(array('status'=>false,'msg'=>'未登录'));
}
$info['address_id'] = $address_id = I('post.address_id',0,'intval');
$ids = explode(',',I('post.ids'));
if(!$ids) {
$this->ajaxReturn(array('status'=>false,'msg'=>'参数错误'));
}
$list = $this->cart_model->where(array('goods_id'=>array('in',$ids),'user_id'=>$user_id))->select();
if(!$list) {
$this->ajaxReturn(array('status'=>false,'msg'=>'购物车出现变化,请重新购买'));
}
$count_amount = 0;
foreach($list as $k=>$v) {
$count_amount += $v['num'] * $v['goods_price'];
}
// 生成订单
M('Order')->startTrans();
$order_model = D('Common/Order');
$order_detail_model = D('Common/OrderDetail');
$info['order_sn'] = $detail['order_sn'] = sp_get_order_sn();
$info['status'] = 1;
$info['price_count'] = $count_amount;
$info['ctime'] = time();
if(!$order_model->create($info)) {
M('Order')->rollback();
$this->ajaxReturn(array('status'=>false,'msg'=>$order_model->getError()));
}
$order_id = $order_model->add($info);
if(!$order_id) {
M('Order')->rollback();
$this->ajaxReturn(array('status'=>false,'msg'=>'添加订单失败'));
}
foreach($list as $k=>$v) {
$detail['goods_id'] = $v['goods_id'];
$detail['goods_price'] = $v['goods_price'];
$detail['num'] = $v['num'];
$detail['amount'] = $v['num'] * $v['goods_price'];
$detail['ctime'] = time();
if(!$order_detail_model->create($detail)) {
M('Order')->rollback();
$this->ajaxReturn(array('status'=>false,'msg'=>$order_detail_model->getError()));
}
if(!$order_detail_model->create($detail)) {
M('Order')->rollback();
$this->ajaxReturn(array('status'=>false,'msg'=>'添加订单失败'));
}
}
$this->ajaxReturn(array('status'=>true,'msg'=>'添加订单成功','data'=>$order_id));
} else {
$this->error('非法操作');
}
}
}
\ No newline at end of file
... ...
... ... @@ -578,7 +578,6 @@ class PHPExcel_Calculation_Functions {
return 4;
} elseif(is_array($value)) {
return 64;
break;
} elseif(is_string($value)) {
// Errors
if ((strlen($value) > 0) && ($value{0} == '#')) {
... ...
... ... @@ -34,9 +34,8 @@ $(function(){
//单选
var length = $('.cart-check').length-1;
$('.cart-check').click(function() {
$(this).toggleClass('chrenActive');
var chrenLength = $('.chrenActive').length;
console.log(length,chrenLength)
$(this).toggleClass('chrenActive');
if(length==chrenLength){
$('#checkAll').prop('checked',true);
$('#checkAll').addClass('active');
... ... @@ -44,24 +43,157 @@ $(function(){
$('#checkAll').prop('checked',false);
$('#checkAll').removeClass('active');
}
coin();
});
//数字
$('.total-number').each(function(){
var sort = '';
var t = $(this).find(".numberbox");
$(this).find(".btn-plus").click(function(){
t.val(parseInt(t.val())+1)
if (parseInt(t.val())!=1){
$('.btn-less').attr('disabled',false);
}
})
var id = $(this).parents('.cart-cell').data('id');
$(this).find(".btn-plus").click(function(){
sort = 'plus';
if (parseInt(t.val())!=1){
$('.btn-less').attr('disabled',false);
}
change(id, sort, 1, $(this));
});
$(this).find(".btn-less").click(function(){
t.val(parseInt(t.val())-1);
if (parseInt(t.val())<=0){
if (parseInt(t.val())<=1){
$(this).siblings(".numberbox").val(1);
}
})
} else {
sort = 'reduce';
change(id, sort, 1, $(this));
}
});
});
// 数量输入框
$('.p-table-border').on('focus','.numberbox',function(){
var id = $(this).parents('.cart-cell').data('id');
var num = $(this).val();
$(this).data('old',num);
});
// 数量输入框
$('.p-table-border').on('blur','.numberbox',function(){
var id = $(this).parents('.cart-cell').data('id');
var num = $(this).val();
if(typeof num == 'undefined' || num<1) {
$(this).val($(this).data('old'));
} else {
change(id, 'change', num, $(this));
}
});
// 执行修改数量ajax
function change(id, sort, num, obj) {
$.ajax({
url:param.changeNum,
type:"POST",
data:{
id:id,
sort:sort,
num:num
},
dateType:"json",
success:function (data) {
if(data.status) {
if(data.data.sort == 'reduce') {
obj.parent().find(".numberbox").val(parseInt(obj.parent().find(".numberbox").val())-1);
}
if(data.data.sort == 'plus') {
obj.parent().find(".numberbox").val(parseInt(obj.parent().find(".numberbox").val())+1);
}
obj.parents('.cart-cell').find('.amount').text(data.data.amount);
coin();
} else {
alert(data.msg);
}
},
error: function (data, status, e) { //提交失败自动执行的处理函数
alert(e);
}
});
}
// 删除购物车商品
$('#delete').click(function(){
var chrenLength = $('.chrenActive').length;
if(chrenLength == 0) {
alert('请选择要删除的商品');
} else {
if(confirm('确认删除所选商品?')) {
var ids = [];
$('.chrenActive').each(function() {
ids.push($(this).parent().data('id'));
});
handleCart(ids,'delele');
}
}
});
// 去付款
$('.pay-button').click(function(){
var chrenLength = $('.chrenActive').length;
if(chrenLength == 0) {
alert('请选择要购买的商品');
} else {
var ids = [];
$('.chrenActive').each(function() {
ids.push($(this).parent().data('id'));
});
handleCart(ids,'topay');
}
});
// 删除(去付款)购物车商品
function handleCart(ids,sort) {
$.ajax({
url:param.del,
type:"POST",
data:{
ids:ids,
sort:sort
},
dateType:"json",
success:function (data) {
console.log(data);
if(data.status) {
if(data.data) {
window.location.href = data.data;
} else {
$('.chrenActive').each(function(){
$(this).parent().remove();
});
}
coin();
} else {
alert(data.msg);
}
},
error: function (data, status, e) { //提交失败自动执行的处理函数
alert(e);
}
});
}
//合计
function coin(){
Coin = 0;
Num = 0;
$(".chrenActive").each(function() {
Num = parseFloat(Num)+parseFloat($(this).parent().find("input[name=num]").val());
Coin = parseFloat(Coin)+parseFloat($(this).parent().find(".amount").text());
})
$("#choose").html(Num);
$(".all_price").html(Coin);
}
// 确认订单
$('#pay_confirm').click(function(){
});
//收藏
$(document).on('click','.btn-collect', function(){
... ...
... ... @@ -2,7 +2,7 @@
<ul class="nav">
<volist name="site_nav" id="vo">
<eq name='key' value='0'>
<li><a href="__ROOT__">{$vo.label}</a></li>
<li><a href="/">{$vo.label}</a></li>
<else/>
<li class="{$vo.active}"><a href="{:U($vo['href'])}">{$vo.label}</a></li>
</eq>
... ...
... ... @@ -80,7 +80,7 @@
</tr>
</thead>
<tbody>
<volist name="list" id="vo">
<volist name="cartList" id="vo">
<tr>
<td class="td-cell-title">
<div class="orders-inbox">
... ... @@ -97,11 +97,11 @@
</div>
</td>
<td class="td-cell-item">
<div class="total-num">×1</div>
<div class="total-num">×{$vo.num}</div>
</td>
<td class="td-cell-item">
<div class="dd-price">
¥{$vo.goods_price}
¥{$vo.amount}
</div>
</td>
</tr>
... ... @@ -115,7 +115,7 @@
</div>
</div>
<div class="pay-footer">
<a href="" class="pay-button">提交订单</a>
<a href="javascript:;" class="pay-button" id="pay_confirm">提交订单</a>
<div class="pay-price">应付总额:<span>¥<i>{$count_amount}</i></span></div>
</div>
</div>
... ...
... ... @@ -38,12 +38,12 @@
<input type="checkbox" id="checkAll" class="cart-check" />全选
</label>
<div class="checkaction">
<a href="">删除</a>
<a href="javascript:;" id="delete">删除</a>
</div>
</div>
<div class="cart-list">
<volist name="cartList" id="vo">
<div class="cart-cell" data-id="{$vo.id}">
<div class="cart-cell" data-id="{$vo.goods_id}">
<input type="checkbox" class="cart-check" name="cart" />
<table class="p-table p-table-border">
<thead>
... ... @@ -58,7 +58,7 @@
<tr>
<td class="td-cell-title">
<div class="orders-inbox">
<a href="" class="pic"><img src="{:sp_get_image_preview_url($vo['thumb'])}"></a>
<a href="{:U('Portal/Goods/detail',array('id'=>$vo['goods_id']))}" class="pic"><img src="{:sp_get_image_preview_url($vo['thumb'])}"></a>
<div class="auto">
<div class="title"><a href="{:U('Portal/Goods/detail',array('id'=>$vo['goods_id']))}">{$vo.goods_name}</a></div>
<div class="desc">{$vo.short_name}</div>
... ... @@ -67,19 +67,19 @@
</td>
<td class="td-cell-item">
<div class="dd-price">
¥{$vo.goods_price}<br><span class="dd-num">×1</span>
¥<span class="goods_price">{$vo.goods_price}</span><br><span class="dd-num">×1</span>
</div>
</td>
<td class="td-cell-item">
<div class="total-number">
<input type="button" class="btn-less" />
<input type="text" class="numberbox" value="{$vo.num}"/>
<input type="number" onkeyup="value=value.replace(/[^\d]/g,'')" name="num" class="numberbox" value="{$vo.num}" data-old="0" />
<input type="button" class="btn-plus" />
</div>
</td>
<td class="td-cell-item">
<div class="dd-price">
¥{$vo.amount}
¥<span class="amount">{$vo.amount}</span>
</div>
</td>
</tr>
... ... @@ -91,9 +91,9 @@
</div>
</div>
<div class="pay-footer">
<a href="" class="pay-button">去付款</a>
<a href="javascript:;" class="pay-button">去付款</a>
<div class="pay-price">
已选商品 <span id="choose">0</span> 件 合计:<span>¥<i>{$count_amount}</i></span>
已选商品 <span id="choose">0</span> 件 合计:<span>¥<i class="all_price">0</i></span>
</div>
</div>
</div>
... ... @@ -101,6 +101,13 @@
</div>
<tc_include file="Public:footer"/>
<!-- js -->
<script>
var param = {
changeNum : "{:U('User/Cart/changeNum')}",
del : "{:U('User/Cart/del')}",
topay : "{:U('User/Cart/topay')}",
};
</script>
<script src="__TMPL__Public/assets/js/jquery.min.js"></script>
<script src="__TMPL__Public/assets/js/shop.js"></script>
<tc_include file="Public:script"/>
... ...
... ... @@ -232,7 +232,7 @@
that.parent().parent().parent().remove()
}
});
})
});
//合计
function coin(){
Coin =0;
... ...