Thinkphp订单设计
jianfly.com 2019-01-31 1722次浏览
//注:订单依赖于购物车和地址
1.数据库设计
CREATE TABLE IF NOT EXISTS `qf_orders` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单id', `mid` int(11) DEFAULT NULL COMMENT '用户id', `money` decimal(10,2) DEFAULT NULL COMMENT '总额', `time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '下单时间', `name` varchar(255) DEFAULT NULL COMMENT '收货人', `address` varchar(255) DEFAULT NULL COMMENT '收货地址', `tel` char(20) DEFAULT NULL COMMENT '联系方式', `zip` char(10) DEFAULT NULL COMMENT '邮编', `remark` varchar(255) DEFAULT NULL COMMENT '备注', `state` tinyint(2) DEFAULT NULL COMMENT '状态:0:未支付,1:已付款,2:已发货,3:已确认收货', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ; CREATE TABLE IF NOT EXISTS `qf_ordergoods` ( `id` int(11) NOT NULL AUTO_INCREMENT, `oid` int(11) DEFAULT NULL COMMENT '订单id,关联订单表', `title` char(255) DEFAULT NULL COMMENT '商品名', `img` char(255) DEFAULT NULL COMMENT '商品图片', `price` decimal(10,2) DEFAULT NULL COMMENT '单价', `num` int(11) DEFAULT NULL COMMENT '数量', `gid` int(11) NOT NULL COMMENT '产品id', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
2.公用方法
protected function money ( $cartid ) { $money = 0;//预设总金额 $cartid = htmlspecialchars($cartid); $cart = M('cart')->where(" `id` in ($cartid) ")->select(); if(!empty($cart)) { foreach ($cart as $key=>$val) { $gid = $val['gid']; $num = $val['num']; $goods = M('goods')->where(array('id'=>$gid))->find(); $money += ($num * ($goods['price'])); } } return $money; }
3.展示订单
//展示所有的订单 public function myorder() { $mid = $_SESSION['member']['id'];// mid //开始分页 import("ORG.Util.Page"); $count = M('orders')->where(array('mid'=>$mid))->count(); if($count){ $page = new \Page($count, $this->config('page_default')); $order = M('orders')->where(array('mid'=>$mid))->order(' `id` desc ')->limit($page->firstRow.','.$page->listRows)->select(); foreach ($order as $key=>$val ) { $order["$key"]["detail"] = M('ordergoods')->where(array('oid'=>$val['id']))->select(); } $this->assign("order",$order); // dump($order); $this->assign('page',$page->show()); } $this->display(); } <table class="tb-dd"> <colgroup> <col class="img-col"/> <col class="link-col"/> <col class="num-col"/> <col class="status-col"/> <col class="operate-col"/> </colgroup> <thead> <tr> <th>订单详情</th> <th>收货人</th> <th>金额</th> <th>全部状态</th> <th>操作</th> </tr> </thead> <volist name="order" id="vo"> <tbody> <tr class="sep-row"><td colspan="5"></td></tr> <tr class="sep-tit"> <td colspan="5"> <span>{$vo.time}</span> <span>订单号: <b>{$vo.id}</b></span> </td> </tr> <volist name="vo.detail" id="vo1"> <tr class="sep-img"> <td class="sep-td"><img src="__ROOT__/__UPLOAD__/{$vo1.img}" alt="{$vo1.title}"/><span> <a href="__APP__/product/1_{$vo1.gid}">{$vo1.title}</a></span> <small>*{$vo1.num}</small> </td> <td><b>{$vo.name}</b></td> <td><span>总额 ¥ <?php echo $vo1['num']*$vo1['price']; ?> </span><span>在线支付</span></td> <td><span>已完成</span><b><a href="">订单详情</a><a href="javaScript:void(0);" onclick="delorder({$vo.id})" style="display: block;">删除订单</a></b></td> <td><span><a href="__APP__/product/2_{$vo1.gid}" class="a_ljgm">再次购买</a></span></td> </tr> </volist> </tbody> </volist> </table>
4.下单,js参照购物车中方法
/** * 购物车传值下单操作 * @param string "1,2,3," 购物车id * @return data {'state':true/false, msg:'string', code:'int'} */ public function addOrder() { //接收和处理数据 $string = trim(htmlspecialchars($_POST['string']), ',');//去除最后一个逗号 $data = array();//定义返回值 if(!$_SESSION['member']['id']) { $data['state'] = false; $data['msg'] = '未登录'; $data['code'] = 1; $this->ajaxReturn($data); exit; } if($string == '') { $data['state'] = false; $data['msg'] = '未选中产品'; $data['code'] = 2; $this->ajaxReturn($data); exit; } $order = M('cart')->where(" `id` in ($string) and `mid` = ".$_SESSION['member']['id'])->select(); if(empty($order)) { $data['state'] = false; $data['msg'] = '查找购物车失败'; $data['code'] = 3; $this->ajaxReturn($data); exit; } $_SESSION['orderid'] = $string;//将选中的cartid放入session $data['state'] = true; $data['msg'] = '下单成功'; $data['code'] = 0; $this->ajaxReturn($data); }
5.下单后跳转的选择地址页面
/** * 下单页面 * @param mid 用户id from session * @param orderid string, from session, 购物车中待下单的id拼接的字符串 */ public function doorder () { $mid = $_SESSION['member']['id']; $orderid = $_SESSION['orderid']; $money = $this->money($orderid);//总额 $this->assign('money',$money); if($orderid == '') { $this->error('您未选中任何商品'); exit; } $order = M('cart')->where(" `id` in ($orderid) and `mid` = ".$mid)->select();//购物车中相关记录 if(empty($order)) { $this->error('未查找到商品'); exit; } foreach ($order as $key=>$val) { $order["$key"]["pro"] = M('goods')->where(array('id'=>$val['gid']))->find(); } $this->assign('order',$order);//购物车中选中的订单 $address = M('address')->where(array('mid'=>$mid))->order(" `default` desc , `id` desc ")->select(); $this->assign('address',$address);//用户地址,第一个为默认地址 $this->display(); } <div class="main"> <div class="doorder"> <div class="title top-title"> 确认订单 <a href="__APP__/cart"></a> </div> <div class="addr"> <ul class="list container"> <empty name="address"> <li class="empty"> <a href="__APP__/user/address">请添加收货地址!</a> </li> </empty> <volist name="address" id="vo"> <li> <input type="radio" name="addid" class="addid" value="{$vo.id}" <if condition="$i eq 1">checked="checked"</if> /> <div class="left text"> <div class="clearfix"> <div class="left man">收货人:{$vo.name}</div> <div class="right tel">{$vo.tel}</div> </div> <div class="detail"> 收货地址:{$vo.address} </div> </div> <a href="javaScript:void(0)" class="ds-b right select"></a> </li> </volist> </ul> </div> <div class="goods-list"> <volist name="order" id="vo"> <li> <a href="__APP__/product/{$vo.pro.pid}_{$vo.pro.id}" class="ds-b left"> <img src="__ROOT__/__UPLOAD__/m_{$vo.pro.img}" width="160" alt=""/> </a> <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> </li> </volist> </div> <div class="remark"> <textarea name="remark" placeholder="买家留言:选填,对本次交易的说明"></textarea> </div> <div class="order-sub"> <a href="javaScript:void(0)" class="ds-b right" onclick="conOrder()">提交订单</a> <small>合计:<span>¥{$money}</span></small> </div> </div> </div> //下单页面选择收货地址 $(".doorder .addr .list li a.select").click(function(){ $(".doorder .addr .list li").fadeIn(); }) $(".doorder .addr .list li input.addid").click(function(){ $(this).parents("li").show().siblings().hide(); })
6.确认提交订单
//提交订单 function conOrder() { var id = $(".doorder .addr input.addid:checked").val(); var remark = $(".doorder .remark textarea").val(); if(id == '') { alert("请选择地址"); return false; }else { $.ajax({ type: "POST", url : "/Orders/confirm", data: {addid: id , remark: remark }, dataType: 'json', async: false, success: function(data){ if(data.state != true) { alert(data.msg); return false; }else { //跳转到下单页面 window.location.href = "/Orders/myorder"; } }, false: function() { alert("系统繁忙,请稍后重试"); return false; } }) } } /** * 用户确认下单的处理方法 * @param mid 用户id * @param money 总金额 * @param time 下单时间 * @param name 收货人 * @param address 地址 * @param tel 联系电话 * @param zip 邮编 * @param remark 备注 * @param state 状态 0:未支付,1:已付款,2:已发货,3:已确认收货 */ public function confirm() { $data = array();//定义返回值 if(!$_SESSION['member']['id']) { $data['state'] = false; $data['msg'] = '未登录'; $data['code'] = 1; $this->ajaxReturn($data); exit; } $mid = $_SESSION['member']['id'];// mid $orderid = $_SESSION['orderid']; $money = $this->money($orderid);//总额 money if($orderid == '') { $data['state'] = false; $data['msg'] = '您未选中任何商品'; $data['code'] = 2; $this->ajaxReturn($data); exit; } $order = M('cart')->where(" `id` in ($orderid) and `mid` = ".$mid)->select();//购物车中相关记录 if(empty($order)) { $data['state'] = false; $data['msg'] = '未查找到商品';//查找购物车记录,能防止重复下单 $data['code'] = 2; $this->ajaxReturn($data); exit; } $addid = intval($_POST['addid']);//地址ID if($addid == '') { $data['state'] = false; $data['msg'] = '您未选择地址'; $data['code'] = 3; $this->ajaxReturn($data); exit; } $address = M('address')->where(array('id'=>$addid))->find();//查找地址 if(!$address) { $data['state'] = false; $data['msg'] = '未查找到地址'; $data['code'] = 3; $this->ajaxReturn($data); exit; } $data = array();//开始组合插入订单表orders中的数据 $data['mid'] = $mid; $data['money'] = $money; $data['time'] = date('Y-m-d H:i:s',time()); $data['name'] = $address['name']; $data['address'] = $address['address']; $data['tel'] = $address['tel']; $data['zip'] = $address['zip']; $data['state'] = 0; $data['remark'] = htmlspecialchars($_POST['remark']); $ret = M('orders')->add($data); if($ret) { //查找产品详情并插入产品详情表 $oid = intval($ret);//订单id foreach ($order as $key=>$val ) { $goods = M('goods')->where(array('id'=>$val['gid']))->find(); $detail = array();//清空订单详情预制数据 $detail['oid'] = $oid;//订单id $detail['title'] = $goods['title'];//商品名 $detail['img'] = $goods['img'];//图片 $detail['price'] = $goods['price'];//单价 $detail['num'] = $val['num'];//数量 $detail['gid'] = $goods['id'];//产品id M('ordergoods')->add($detail); } //删除已下单的购物车 M('cart')->where(" `id` in ($orderid) ")->delete(); unset($_SESSION['orderid']);//去除SESSION中保存的待下单购物车id号 $data['state'] = true; $data['msg'] = '下单成功'; $data['code'] = 0; $data['oid'] = $oid; $this->ajaxReturn($data); exit; }else { $data['state'] = false; $data['msg'] = '系统繁忙,请稍后重试'; $data['code'] = 4; $this->ajaxReturn($data); exit; } }
7.展示所有订单
//展示所有的订单 public function myorder() { $mid = $_SESSION['member']['id'];// mid //开始分页 import("ORG.Util.Page"); $count = M('orders')->where(array('mid'=>$mid))->count(); if($count){ $page = new \Page($count, $this->config('page_default')); $order = M('orders')->where(array('mid'=>$mid))->order(' `id` desc ')->limit($page->firstRow.','.$page->listRows)->select(); foreach ($order as $key=>$val ) { $order["$key"]["detail"] = M('ordergoods')->where(array('oid'=>$val['id']))->select(); } $this->assign("order",$order); // dump($order); $this->assign('page',$page->show()); } $this->display(); }
8.删除订单
/** * 删除订单 * @param id 订单id * @param mid 用户id * @state 状态 0:未支付,1:已付款,2:已发货,3:已确认收货 * @return data{ state: true/false, msg:'', code:''} */ public function delOrder() { $id = intval($_POST['id']); $data = array(); if(!$id) { $data['state'] = false; $data['msg'] = "未接收到订单号"; $data['code'] = 1; $this->ajaxReturn($data); exit; } $mid = $_SESSION['member']['id'];//用户id if(!$mid) { $data['state'] = false; $data['msg'] = "未登录"; $data['code'] = 2; $this->ajaxReturn($data); exit; } $order = M('orders')->where(array('id'=>$id, 'mid'=>$mid))->find(); if(!$order) { $data['state'] = false; $data['msg'] = "未找到相关订单"; $data['code'] = 3; $this->ajaxReturn($data); exit; } if(($order['state'] != 0) && ($order['state']!= 3)) { $data['state'] = false; $data['msg'] = "当前订单状态不允许删除"; $data['code'] = 4; $this->ajaxReturn($data); exit; } $ret = M('orders')->where(array('id'=>$id, 'mid'=>$mid))->delete(); //删除订单详情表中相关记录 M('ordergoods')->where(array('oid'=>$order['id']))->delete(); $data['state'] = true; $data['msg'] = "删除成功"; $data['code'] = 0; $this->ajaxReturn($data); exit; }
- 上一篇:Thinkphp收藏设计
- 下一篇:ThinkPhp购物车设计