ThinkPhp购物车设计
jianfly.com 2019-01-31 1603次浏览
//注:请先自行设计商品表
1.数据库设计
CREATE TABLE IF NOT EXISTS `qf_cart` ( `id` int(11) NOT NULL AUTO_INCREMENT, `gid` int(11) NOT NULL COMMENT '商品id', `mid` int(11) NOT NULL COMMENT '用户id', `num` int(11) NOT NULL COMMENT '数量', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
2.php设计
/** * 展示购物车 */ public function index() { $this->checkLogin();//检测是否登录 $mid = $_SESSION['member']['id']; $cart = M('cart')->where(array('mid'=>$mid))->order(' `id` desc ')->select(); foreach ($cart as $key=>$val) { if($val['num'] <= 0) { M('cart')->where(array('id'=>$val['id']))->delete(); } $cart["$key"]["pro"] = M('goods')->where(array('id'=>$val['gid']))->find(); } $this->assign('cart',$cart);//购物车数据 $this->display(); } /** * ajax购物车 */ public function ajaxCart() { $mid = $_SESSION['member']['id']; $cart = M('cart')->where(array('mid'=>$mid))->order(' `id` desc ')->select(); foreach ($cart as $key=>$val) { $cart["$key"]["pro"] = M('goods')->where(array('id'=>$val['gid']))->find(); } $this->ajaxReturn($cart); } /** * ajax加入购物车 * @param id 商品id * @param num 数量 * @return json($data) {state: true/false , msg: 'string' , code: ''} */ public function addCart() { $id = intval($_POST['id']); $num = intval($_POST['num'])?intval($_POST['num']):1;//默认1个 $data = array(); //验证id if(!$id) { $data['state'] = false; $data['msg'] = '未接收到id'; $data['code'] = 1; $this->ajaxReturn($data); exit; } //验证登录 if(!$_SESSION['member']) { $data['state'] = false; $data['msg'] = '未登录'; $data['code'] = 2; $this->ajaxReturn($data); exit; } //验证商品是否存在 $goods = M('goods')->where(array('id'=>$id))->find(); if(!$goods) { $data['state'] = false; $data['msg'] = '商品不存在'; $data['code'] = 3; $this->ajaxReturn($data); exit; } //加入购物车 if(!$this->doAddCart( $id , $_SESSION['member']['id'] , $num ) ) { $data['state'] = false; $data['msg'] = '系统繁忙,请稍后重试'; $data['code'] = 4; $this->ajaxReturn($data); exit; }else{ $data['state'] = true; $data['msg'] = '加入成功'; $data['code'] = 0; $this->ajaxReturn($data); exit; } } /** * ajax修改商品数量 * @param id 商品id * @param num 数量 * @return json($data) {state: true/false , msg: 'string' , code: ''} */ public function changeCart() { $id = intval($_POST['id']); $num = intval($_POST['num']); $data = array(); //验证id if(!$id) { $data['state'] = false; $data['msg'] = '未接收到id'; $data['code'] = 1; $this->ajaxReturn($data); exit; } //验证登录 if(!$_SESSION['member']) { $data['state'] = false; $data['msg'] = '未登录'; $data['code'] = 2; $this->ajaxReturn($data); exit; } //验证信息是否存在 $cart = M('cart')->where(array('id'=>$id,'mid'=>$_SESSION['member']['id']))->find();//同时验证是否属于当前用户 if(!$cart) { $data['state'] = false; $data['msg'] = '信息不存在'; $data['code'] = 3; $this->ajaxReturn($data); exit; } //验证商品是否存在 $goods = M('goods')->where(array('id'=>$cart['gid']))->find(); if(!$goods) { $data['state'] = false; $data['msg'] = '商品已下架'; $data['code'] = 4; $this->ajaxReturn($data); exit; } M('cart')->where(array('id'=>$id))->save(array('num'=>$num)); $data['state'] = true; $data['msg'] = '修改成功'; $data['code'] = 0; $this->ajaxReturn($data); exit; } /** * 处理加入购物车 * @param $gid 商品id * @param $mid 用户id * @param $num 数量 * @return true/false */ private function doAddCart( $gid , $mid , $num ) { //获取值 $gid = intval($gid); $mid = intval($mid); $num = intval($num)?intval($num):1; if( (!$gid) || (!$mid) ) { return false;//商品id或用户id没传过来 exit; } $member = M('member')->where(array('id'=>$mid))->find(); if(!$member) { return false; exit;//用户不存在 } //查看购物车是否已有 $cart = M('cart')->where(array('gid'=>$gid,'mid'=>$mid))->find(); if($cart)//存在 { $data['num'] = $cart['num'] + $num;//在原来数量的基础上增加 $ret = M('cart')->where(array('id'=>$cart['id']))->save($data);//保存数据 return $ret;//返回保存状态 }else{//不存在 $data['gid'] = $gid; $data['mid'] = $mid; $data['num'] = $num; $ret = M("cart")->add($data);//新增 return $ret;//返回保存状态 } } /** * ajax删除购物车 * @param id 购物车id * @param num 数量 * @return json($data) {state: true/false , msg: 'string' , code: ''} */ public function delCart() { //获取值 $id = intval($_POST['id']); $data = array(); //验证id if(!$id) { $data['state'] = false; $data['msg'] = '未接收到id'; $data['code'] = 1; $this->ajaxReturn($data); exit; } //验证登录 if(!$_SESSION['member']) { $data['state'] = false; $data['msg'] = '未登录'; $data['code'] = 2; $this->ajaxReturn($data); exit; } //验证信息是否存在 $cart = M('cart')->where(array('id'=>$id,'mid'=>$_SESSION['member']['id']))->find();//同时验证是否属于当前用户 if(!$cart) { $data['state'] = false; $data['msg'] = '信息不存在'; $data['code'] = 3; $this->ajaxReturn($data); exit; } //开始删除购物车 $state = M('cart')->where(array('id'=>$id))->delete(); if($state) { $data['state'] = true; $data['msg'] = '删除成功'; $data['code'] = 0; }else{ $data['state'] = false; $data['msg'] = '系统繁忙,请稍后重试'; $data['code'] = 4; } $this->ajaxReturn($data); }
3.html摘要
<a href="javaScript:void (0)" onclick="pop_cart({$list.id})">加入购物车</a> <div class="mask"></div> <div class="select-num"> <div class="goods container clearfix"> <div class="left img"> <img src="__ROOT__/__UPLOAD__/m_{$list.img}" height="105" alt=""> </div> <div class="left price">¥{$list.price}</div> <a href="javaScript:void(0)" class="right close"></a> </div> <div class="num"> <div class="container clearfix"> <div class="left">数量</div> <div class="right cnum"> <div class="left jian gjian">-</div> <input type="number" value="1" class="ds-b left gnum"> <div class="left add gadd">+</div> </div> </div> </div> <a href="javaScript:void(0)" class="num-selected margin-center">确定</a> </div> //jian $(".gjian").click(function(){ var gnum=parseInt($(this).parent().find(".gnum").val()); gnum -= 1; if(gnum <= 0) { alert("已经是最小数量了"); return false; }else { $(this).parent().find(".gnum").val(gnum); } }) //加 $(".gadd").click(function(){ var gnum=parseInt($(this).parent().find(".gnum").val()) ; gnum += 1; $(this).parent().find(".gnum").val(gnum); }) //商品数量取整 $(".gnum").blur(function(){ var gnum=parseInt($(this).val()); if(gnum <= 0) gnum = 1; $(this).val(gnum); }) //打开选择数量面板 function open_num_box() { $(".mask").fadeIn(); $(".select-num").fadeIn(); } //关闭选择数量面板 $(".mask").click(function(){ $(".mask").fadeOut(); $(".select-num").fadeOut(); }) $(".select-num a.close").click(function(){ $(".mask").fadeOut(); $(".select-num").fadeOut(); }) //确定选择数量 $(".num-selected").click(function(){ var gnum=parseInt($(".gnum").val()); if(gnum <= 0) { $(".gnum").val(1); alert("最少数量为1"); return false; }else { $("#num_to_select").val(gnum); $(".pro-detail .info .choose a").html("已选择数量"+gnum); $(".mask").fadeOut(); $(".select-num").fadeOut(); } }) //加入购物车 function pop_cart( id ) { var num=$("#num_to_select").val(); if(num <= 0 ) { alert("请选择商品数量"); return false; }else { $.ajax({ type: 'POST', url: "/Cart/addCart", data: {id:id, num:num }, dataType: 'json', async: false, success:function(data){ if( data['state'] == true){ alert("加入购物车成功"); window.location.reload(); }else { alert(data.msg); if( data['code'] == 2){ //跳转到登陆页面 window.location.href = "/User/login"; } return false; } }, false:function(){ alert("系统繁忙,稍后再试"); } }) } } <div class="main"> <div class="cart"> <div class="title top-title"> 购物车 <a href="__APP__/product"></a> </div> <ul class="list"> <volist name="cart" id="vo"> <li> <input type="checkbox" value="{$vo.id}" class="ds-b left checkbox"/> <input type="hidden" name="price" value="{$vo.pro.price}"> <input type="hidden" name="num" value="{$vo.num}"> <img src="__ROOT__/__UPLOAD__/m_{$vo.pro.img}" width="160" alt=""/> <div class="right text"> <div class="tit">{$vo.pro.title|mb_substr=0,30,"utf-8"}</div> <div class="clearfix"> <div class="left price">¥{$vo.pro.price}</div> <div class="right num">X{$vo.num}</div> </div> <div class="clearfix"> <a href="javaScript:void(0)" class="ds-b left open-bnum">编辑</a> </div> <div class="bj-cart"> <div class="lpart left"> <div class="cnum margin-center"> <div class="left jian gjian">-</div> <input type="number" value="{$vo.num}" class="ds-b left gnum"> <div class="left add gadd">+</div> </div> <a href="javaScript:void(0)" class="ds-b delete" onclick="del_cart({$vo.id})">删除</a> </div> <a href="javaScript:void(0)" class="rpart right ds-b" onclick="change_cart({$vo.id} , this)">确定</a> </div> </div> </li> </volist> </ul> </div> </div> <div class="cart-footer"> <a href="javaScript:void(0)" class="ds-b left cart-select-all">全选</a> <a href="javaScript:void(0)" class="ds-b right todo-order" onclick="doorder()">结算(<span>0</span>)</a> <small>合计:¥<span>0.00</span>元</small> </div> //购物车页面展开编辑 $(".open-bnum").click(function(){ $(this).parents("li").find(".bj-cart").fadeIn(); }) //删除购物车中的一条数据 function del_cart( id ) { $.ajax({ type: 'POST', url: "/Cart/delCart", data: {id:id}, dataType: 'json', async: false, success:function(data){ alert(data.msg); window.location.reload(); }, false:function(){ alert("系统繁忙,请稍后重试") } }) } //修改购物车中一条数据的数量 function change_cart( id , doc) { var num = parseInt($(doc).parents(".bj-cart").find(".gnum").val()); if(num <= 0) { alert("数量至少为1"); return false; }else { $.ajax({ type: 'POST', url: "/Cart/changeCart", data: {id:id , num:num }, dataType: 'json', async: false, success:function(data){ if(data.state != true) { alert(data.msg); } window.location.reload(); }, false:function(){ alert("系统繁忙,请稍后重试") } }) } } //购物车计算总价和数量 $(".cart input.checkbox").change(function(){ //获取选中的数量 var num = $(".cart input.checkbox:checked").length; $(".cart-footer .todo-order span").html(num); if(num == 0) { $(".cart-footer .todo-order").removeClass("on"); }else { $(".cart-footer .todo-order").addClass("on"); } //总价 var price = 0; $(".cart input.checkbox:checked").each(function(i){ var price1 = parseInt($(this).parents("li").find("input[name=price]").val()*100); var num = parseInt($(this).parents("li").find("input[name=num]").val()); price += (price1*num); }) price = price/100; $(".cart-footer small span").html(price); }) $(".cart-select-all").click(function(){ $(".cart input.checkbox").attr('checked', true); $(".cart input.checkbox").eq(0).trigger("change"); }) //提交购物车 function doorder() { var length = $(".cart input.checkbox:checked").length; if(length == 0) { alert("请选择商品"); return false; } var cartid = ""; $(".cart input.checkbox:checked").each(function(){ cartid += $(this).val()+","; }); //此处要判断cartid是否为空 $.ajax({ type: "POST", url : "/Orders/addOrder", data: {string: cartid }, dataType: 'json', async: false, success: function(data){ if(data.state != true) { alert(data.msg); return false; }else { //跳转到下单页面 window.location.href = "/Orders/doorder"; } }, false: function() { alert("系统繁忙,请稍后重试"); return false; } }) }
- 上一篇:Thinkphp订单设计
- 下一篇:ThinkPhp会员地址管理设计