Thinkphp收藏设计
jianfly.com 2019-01-31 2262次浏览
//注:依赖于会员设计
1.数据库设计
CREATE TABLE IF NOT EXISTS `qf_fav` ( `id` int(11) NOT NULL AUTO_INCREMENT, `gid` int(11) DEFAULT NULL COMMENT '商品id', `mid` int(11) DEFAULT NULL COMMENT '用户id', `time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '收藏时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
2.展示收藏夹
/** * 展示收藏夹 */ public function index() { $this->checkLogin();//检测是否登录 $mid = $_SESSION['member']['id']; $fav = M('fav')->where(array('mid'))->order(' `id` desc ')->select(); foreach ($fav as $key=>$val) { $fav["$key"]["pro"] = M('goods')->where(array('id'=>$val['gid']))->find(); } $this->assign('fav',$fav);//收藏夹数据 $this->display(); }
3.加入收藏
/** * ajax加入收藏 * @param id 商品id * @param num 数量 * @return json($data) {state: true/false , msg: 'string' , code: ''} */ public function addFav() { $id = intval($_POST['id']); $time = date("Y-m-d H:i:s",time()); $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; } //验证是否已经收藏 $fav = M('fav')->where(array('gid'=>$id,'mid'=>$_SESSION['member']['id']))->find(); if($fav) { $data['state'] = false; $data['msg'] = '商品已经收藏过了'; $data['code'] = 4; $this->ajaxReturn($data); exit; } //加入收藏 if(!$this->doAddFav( $id , $_SESSION['member']['id'] , $time ) ) { $data['state'] = false; $data['msg'] = '系统繁忙,请稍后重试'; $data['code'] = 5; $this->ajaxReturn($data); exit; }else{ $data['state'] = true; $data['msg'] = '加入成功'; $data['code'] = 0; $this->ajaxReturn($data); exit; } } /** * 处理加入收藏 * @param $gid 商品id * @param $mid 用户id * @param $time 时间 * @return true/false */ private function doAddFav( $gid , $mid , $time ) { //获取值 $gid = intval($gid); $mid = intval($mid); $time = $time?$time:date("Y-m-d H:i:s",time()); if( (!$gid) || (!$mid) ) { return false;//商品id或用户id没传过来 exit; } $member = M('member')->where(array('id'=>$mid))->find(); if(!$member) { return false; exit;//用户不存在 } //查看收藏夹是否已有 $fav = M('fav')->where(array('gid'=>$gid,'mid'=>$mid))->find(); if($fav)//存在 { return false; }else{//不存在 $data['gid'] = $gid; $data['mid'] = $mid; $data['time'] = $time; $ret = M("fav")->add($data);//新增 return $ret;//返回保存状态 } } //收藏 function pop_fav( id ) { $.ajax({ type: 'POST', url: "/Fav/addFav", data: {id:id }, dataType: 'json', async: false, success:function(data){ if( data['code'] == 2){ alert(data.msg); //跳转到登陆页面 window.location.href = "/User/login"; } if( data['state'] == true){ alert("收藏成功"); } //location.reload(); console.log(data); }, false:function(){ alert("收藏失败"); } }) }
4.删除收藏
/** * ajax删除收藏 * @param id 收藏夹id * @return json($data) {state: true/false , msg: 'string' , code: ''} */ public function delFav( ) { //获取值 $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; } //验证信息是否存在 $fav = M('fav')->where(array('id'=>$id,'mid'=>$_SESSION['member']['id']))->find();//同时验证是否属于当前用户 if(!$fav) { $data['state'] = false; $data['msg'] = '信息不存在'; $data['code'] = 3; $this->ajaxReturn($data); exit; } //开始删除收藏夹 $state = M('fav')->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); } //取消收藏 function del_fav( id ) { $.ajax({ type: 'POST', url: "/Fav/delFav", data: {id:id }, dataType: 'json', async: false, success:function(data){ console.log(data); $(this).parents("li").hide(); alert("取消收藏成功"); location.reload(); }, false:function(){ } }) }
- 上一篇:Thinkphp支付宝支付
- 下一篇:Thinkphp订单设计