where("user_id", $user['user_id'])->where("store_id", $sid)->find(); session('user', $user0); //覆盖session 中的 user $this->user = $user0; $this->user_id = $user0['user_id']; $this->assign('user', $user0); //存储用户信息 } else { $usrid = Cookie::get('user_id'); if ($usrid) { $user0 = M('users')->where("user_id", $usrid)->where("store_id", $sid)->find(); session('user', $user0); //覆盖session 中的 user $this->user = $user0; $this->user_id = $user0['user_id']; $this->assign('user', $user0); //存储用户信息 } } $nologin = ['index','lbdetails', 'lbquan', 'libaoorder']; if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) { $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId()))); exit; } if (!in_array(ACTION_NAME, $nologin)) { if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) { $this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId()))); exit; } else if(ACTION_NAME!='vipcard') //已绑定会员 { $storeconfig=M('store_config')->where("store_id",getMobileStoId())->field('switch_list')->find(); $rank_switch=json_decode($storeconfig['switch_list'],true)['rank_switch']; if($rank_switch==1 && empty($this->user['card_field']))//要求商城会员必须开通等级卡(商家开启等级卡) { $this->redirect(U('mobile/User/vipcard', array('stoid' => getMobileStoId()))); exit; } } } $order_status_coment = array( 'WAITPAY' => '待付款 ', //订单查询状态 待支付 'WAITSEND' => '待发货', //订单查询状态 待发货 'WAITRECEIVE' => '待收货', //订单查询状态 待收货 'WAITCCOMMENT' => '待评价', //订单查询状态 待评价 ); $this->assign('order_status_coment', $order_status_coment); } public function index() { $p = I('get.p/d', 1); $pagenum = 10; $where = array('store_id' => getMobileStoId(), 'isdel' => 0, 'startime' => array('elt', time()), ' endtime' => array('egt', time())); $count = M('libao_form')->alias('a')->where($where)->count(1); //总用户量 $Page = $pager = new AjaxPage($count, $pagenum); $show = $Page->show(); $list = M('libao_form') ->where($where) ->order('sort_order asc,id desc') ->limit($Page->firstRow . ',' . $Page->listRows) ->select(); if ($list) { foreach ($list as $k => $vo) { $list[$k]['curnum'] = $vo['num'] - $vo['salenum']; } $this->assign('list', $list); } if ($count > $p * $pagenum) { $this->assign('mshow', 1); } else { $this->assign('mshow', 0); } if (I('is_ajax')) { return $this->fetch('ajax_index', getMobileStoId()); } return $this->fetch('', getMobileStoId()); } //礼包礼品详情 public function lbdetails() { $id=I('id/d'); $myid=I('mid'); $storeid=getMobileStoId(); $data=M('libao_formvip')->where(array('user_id'=>$this->user_id,'id'=>$myid,'fbillstate'=>1))->select(); if($data[0]) { $id=$data[0]['lbid']; $this->assign('mid',$myid); $this->assign('isget',$data[0]['isget']); $this->assign('expdate',$data[0]['expdate']); } if($id) { $list=M('libao_form')->where(array('store_id'=>$storeid,'id'=>$id,'lbtype'=>1))->find(); if ($list) { $isstart = 1; if ($list['startime'] > time()) { $isstart = 0; } if ($list['endtime'] < time()) { $isstart = 2; } $list1 = M('libao_list')->where('lbid=' . $id)->select(); if ($myid) { unset($list1); $list1 = M('libao_listvip')->where('lbvipid=' . $myid)->select(); foreach ($list1 as $k => $voo) { $list1[$k]['alsonum'] = $voo['goods_num'] - $voo['usenum'];//剩余次数 } } $this->assign('isstart', $isstart); $this->assign('list', $list); $this->assign('list1', $list1); } } return $this->fetch('', getMobileStoId()); } //我的礼包 public function mylibao() { $p = I('get.p/d', 1); $pagenum = 10; $where =array('a.user_id'=>$this->user_id,'a.fbillstate'=>1); $count = M('libao_formvip')->alias('a')->where($where)->count(1); //总用户量 $Page = $pager = new AjaxPage($count, $pagenum); $show = $Page->show(); $list=M('libao_formvip') ->alias('a') ->join('libao_form b','a.lbid=b.id','left') ->where($where) ->field(' b.*,a.id as lbvipid,a.expdate') ->order('a.id desc') ->limit($Page->firstRow . ',' . $Page->listRows) ->select(); if ($list) { foreach ($list as $k => $vo) { $list[$k]['curnum']=$vo['num']-$vo['salenum']; } $this->assign('list', $list); } if ($count > $p * $pagenum) { $this->assign('mshow', 1); } else { $this->assign('mshow', 0); } if (I('is_ajax')) { return $this->fetch('ajax_mylibao', getMobileStoId()); } return $this->fetch('', getMobileStoId()); } //礼包礼券详情 public function lbquan() { $id=I('id/d'); $myid=I('mid/d'); $storeid=getMobileStoId(); $data=M('libao_formvip')->where(array('user_id'=>$this->user_id,'id'=>$myid))->select(); if($data[0]) { $id=$data[0]['lbid']; $this->assign('mid',$myid); $this->assign('expdate',$data[0]['expdate']); } if($id) { $list=M('libao_form')->where(array('store_id'=>$storeid,'id'=>$id,'lbtype'=>2))->select(); mlog("list:".json_encode($list),"lbquan/".$storeid); if ($list[0]) { $isstart = 1; if ($list[0]['startime'] > time()) { $isstart = 0; } if ($list[0]['endtime'] < time()) { $isstart = 2; } $list1 = db::name('libao_list') ->alias('a') ->join('coupon b', 'a.cpid=b.id', 'left') ->where('a.lbid=' . $id) ->field('a.goods_num,b.*') ->order('a.id desc') ->select(); $this->assign('isstart', $isstart); mlog("isstart:".$isstart,"lbquan/".$storeid); $this->assign('list', $list[0]); $this->assign('list1', $list1); } } return $this->fetch('', getMobileStoId()); } //生成礼包订单 public function libaoorder() { $lbid=I('lbid/s');//礼包id $paytype=I('paytype');//支付方式 $userid=$this->user_id; if (empty($userid)) { $res=array( 'code'=>-1, 'msg'=>'请先登录' ); return json($res); } $storeid=getMobileStoId(); //验证是否有此礼包 $libao=M('libao_form')->where('store_id='.$storeid.' and startime<='.time().' and endtime>='.time().' and id='.$lbid)->select(); if(empty($libao[0])) { $res=array( 'code'=>-1, 'msg'=>'找不到礼包或礼包已过期' ); return json($res); } if($libao[0]['num'] && $libao[0]['num']<=$libao[0]['salenum'])//礼包已售完 { $res=array( 'code'=>-1, 'msg'=>'礼包已售罄' ); return json($res); } if($libao[0]['limitnum'])//有限制购买 { $user_lb=M('libao_formvip')->where(array('store_id'=>$storeid,'user_id'=>$userid,'lbid'=>$lbid,'fbillstate'=>1))->select(); if(count($user_lb)>=$libao[0]['limitnum']) { $res=array( 'code'=>-1, 'msg'=>'已达每个会员购买限制' ); return json($res); } } if($libao[0]['usertype']) //限制新会员购买 { $orderlist=M('order')->where(array('user_id'=>$userid,'order_status'=>4))->count(); if($orderlist) { $res=array( 'code'=>-1, 'msg'=>'该礼包只允许新会员购买' ); return json($res); } } if($paytype==1)//积分兑换 { $integ=0; if($this->pm_erpid) { $tk = tpCache("shop_info", getMobileStoId()); $userd = M("users")->where('user_id', $userid)->find(); //$map['Id'] = array('=', $userd['erpvipid']); //$rs = getApiData(' wxd.vip.vipinfo.list.get', $tk['api_token'], null, $map); //$d = json_decode($rs, true); if($userd['erpvipid']){ $d=get_erpvipinfo($userd['erpvipid'],$this->pm_erpid); if($d) $integ = $d['Integral']; } }else{ $integ=$this->user['pay_points']; } if ($libao[0]['lbintegral'] > $integ) { $res=array( 'code'=>-1, 'msg'=>'您的积分不足' ); return json($res); } else { //启动事务 db::startTrans(); try { $lbnumber='lb'. date("YmdHis") . rand(1000, 9999); //扣积分 $sc=new \app\mobile\controller\Integral(); $res=$sc->updateoffintegral($userid,'-'.$libao[0]['lbintegral'],'积分兑换礼包',$lbnumber); if($res==1) { $data['user_id']=$userid; $data['store_id']=$storeid; $data['lbid']=$lbid; $data['expdate']=strtotime('+'.$libao[0]['expday'].' day',time()); $data['number']=$lbnumber; $data['paytype']=1; $data['fbillstate']=1; $data['add_time']=time(); $uselbid=M('libao_formvip')->add($data); $list=M('libao_list')->where(array('store_id'=>$storeid,'lbid'=>$lbid))->select(); if($list) { for ($i=0;$iadd($data); } } else { unset($data); $data['user_id'] = $userid; $data['store_id'] = $storeid; $data['lbvipid'] = $uselbid; $data['goods_num'] = $list[$i]['goods_num']; $data['goods_name'] = $libao[0]['lbtype'] == 1 ? $list[$i]['goods_name'] : $list[$i]['cpid']; $data['goods_sn'] = $list[$i]['goods_sn']; $data['lb_state'] = $list[$i]['state']; M('libao_listvip')->add($data); } } } DB::execute("update wxd_libao_form set salenum=salenum+1 where store_id=".$storeid." and id=".$lbid.""); $res=array( 'code'=>1, 'msg'=>'成功' ); } else { $res=array( 'code'=>-1, 'msg'=>'积分扣减失败' ); } Db::commit(); }catch (\Exception $e) { // 回滚事务 db::rollback(); $res=array( 'code'=>-1, 'msg'=>'订单生成失败,请重试' ); // M('libao_form')->where('id='.$result)->delete(); } return json($res); } } else if($paytype==2)//微信购买 { $res=array( 'code'=>1, 'msg'=>'允许购买礼包' ); return json($res); } } //会员礼包礼券详情 public function lbshow() { $myid=I('mid/d'); $storeid=getMobileStoId(); if($myid) { $data=M('libao_formvip')->where(array('user_id'=>$this->user_id,'id'=>$myid))->select(); if($data[0]) { $id=$data[0]['lbid']; $this->assign('mid',$myid); $this->assign('isget',$data[0]['isget']); } if($id) { $list=M('libao_form')->where(array('store_id'=>$storeid,'id'=>$id,'lbtype'=>2))->select(); $list1=db::name('libao_list') ->alias('a') ->join('coupon b','a.cpid=b.id','left') ->where('a.lbid='.$id) ->field('a.goods_num,b.*') ->order('a.id desc') ->select(); $this->assign('list',$list[0]); $this->assign('list1',$list1); } } return $this->fetch('', getMobileStoId()); } //获取礼包礼券 public function getlibaoquan() { $lbvipid=I('lvd'); $code=-1; $msg='找不到礼包或礼包已过期'; $list=M('libao_formvip')->where('id='.$lbvipid)->find(); $isget=$list['isget']; if($isget){ $res=array('code'=>$code,'msg'=>"您已经领取过了", ); return json($res); } if($list) { $userid=$list['user_id']; $list=M('libao_form')->where(array('store_id'=>getMobileStoId(),'id'=>$list['lbid'],'lbtype'=>2))->find(); if($list) { $cplist=Db::query('SELECT a.id as lid,a.goods_num,a.no,b.* from wxd_libao_listvip a LEFT JOIN wxd_coupon b on a.goods_name=b.id where a.lbvipid='.$lbvipid); if($cplist) { $tk = tpCache("shop_info",getMobileStoId()); $userd = M("users")->where('user_id', $userid)->find(); $is_success=1;//默认全部成功线下优惠券 for($i=0;$ipm_erpid) { for ($k = 0; $k < $cplist[$i]['goods_num']; $k++) { //优惠券到帐户 if ($cplist[$i]['no']) { $CashRepNo=$cplist[$i]['no']; } else { $CashRepNo = "9006" . date("ymdHis") . get_total_millisecond(); $updatelistvip = M('libao_listvip')->where('id=' . $cplist[$i]["lid"])->save(array('no' => $CashRepNo)); } unset($data); $data = array( 'CashRepNo' => $CashRepNo,//券号 'VIPId' => $userd["erpvipid"],//会员id 'Sum' => $cplist[$i]["money"],//金额 'SendMan' => '礼包发放',//发放类型 'Operator' => '微信商城',//来源 'BuySum' => $cplist[$i]["condition"],//满多少才能使用 'Remark' => '购买礼包得优惠券【消费满' . $cplist[$i]["condition"] . '元可用】', //'BeginDate' => date('Y-m-d H:i:s', $cplist[$i]["use_start_time"]), //'ValidDate' => date('Y-m-d H:i:s', $cplist[$i]["use_end_time"]), ); if ($cplist[$i]['useobjecttype']) { $data['UseObjectType']=$cplist[$i]['useobjecttype']; $data['UseObjectID']=$cplist[$i]['useobjectid']; $data['UseObjectNo']=$cplist[$i]['useobjectno']; $data['UseObjectName']=$cplist[$i]['useobjectname']; } $coupon=$cplist[$i]; //如果有设置有效期 if($coupon['endtype']==1){ $data['BeginDate'] = date('Y-m-d');//使用开始日期 if($coupon['days']>0){ $ed = strtotime("+" . $coupon['days'] . " day"); $data['ValidDate'] = date('Y-m-d', $ed);//截止期限 } }else { if($coupon["use_start_time"] != "") { $data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期 } if ($coupon["use_end_time"] != "") { $data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//截止期限 } } $erp=tpCache("shop_info.ERPId",getMobileStoId()); $add_rs = getApiData_java_p("/api/erp/vip/cash/insert", $erp, $data,null, null, null, "POST"); if (empty($add_rs)) { $is_success=0; } } //全部成功的话, 再对应修改1 if (empty($is_success)) { $res=array( 'code'=>-1, 'msg'=>'领取失败,请重新点击一键领取优惠券!', ); return json($res); } M('libao_formvip')->where('id=' . $lbvipid)->save(array('isget' => 1)); }else{//纯线上功能 for ($k = 0; $k < $cplist[$i]['goods_num']; $k++) { $coupon=$cplist[$i]; //优惠券到帐户 $CashRepNo = "9006" . date("ymdHis") . get_total_millisecond(); //插入线上优惠券领取表 $coupon_list['cid'] = 0; $coupon_list['type'] = 2; $coupon_list['uid'] = $this->user['user_id']; $coupon_list['send_time'] = time(); $coupon_list['code'] = $CashRepNo; $coupon_list['store_id'] = getMobileStoId(); if($coupon['endtype']==1){ $coupon_list['begintime']=time(); if($coupon['days']>0) { $coupon_list['validtime'] = strtotime("+" . $coupon['days'] . " day"); } else $coupon_list['validtime']=0; }else { if($coupon["use_start_time"] != "") $coupon_list['begintime'] = $coupon["use_start_time"]; if($coupon["use_end_time"] != "") $coupon_list['validtime'] = $coupon["use_end_time"]; } $coupon_list['sum'] = $cplist[$i]["money"]; $coupon_list['buysum'] = $cplist[$i]["condition"];//满多少才能使用 $coupon_list['remark'] = '购买礼包得优惠券【消费满' . $cplist[$i]["condition"] . '元可用】'; $add_couponinfo=add_coupon($coupon_list,$this->user,$this->user['store_id']); M('libao_listvip')->where('id=' . $cplist[$i]["lid"])->save(array('no' => $CashRepNo)); M('libao_formvip')->where('id=' . $lbvipid)->save(array('isget' => 1)); } } } else{ unset($data); $coupon=$cplist[$i]; $CashRepNo="9006" . date("ymdHis") . get_total_millisecond(); $data=array( 'store_id'=>getMobileStoId(), 'user_id'=>$userid, 'no'=>$CashRepNo, 'billdate'=>time(), 'fmid'=>$cplist[$i]['id'], 'condition'=>$coupon['condition'], 'exparea'=>$coupon['exparea'], ); if($coupon['endtype']==1){ $data['use_start_time']=time(); if($coupon['days']==0){ $data['use_end_time']=0; }else { $data['use_end_time'] = strtotime("+" . $coupon['days'] . " day"); } }else { if($coupon["use_start_time"] != "") $data['use_start_time'] = $coupon["use_start_time"]; if($coupon["use_end_time"] != "") $data['use_end_time'] = $coupon["use_end_time"]; } $result1=M('user_feemail')->add($data); if($result1) { M('libao_listvip')->where('id='.$cplist[$i]["lid"])->save(array('no'=>$CashRepNo)); M('libao_formvip')->where('id='.$lbvipid)->save(array('isget'=>1)); } } } $code=1; } } } $res=array( 'code'=>$code, 'msg'=>$msg, ); return json($res); } //获取核销码信息 public function getlibaohxm() { $id=I('id'); $type=I('type'); $userid=$this->user_id; if($type==1) { $list=M('libao_formvip')->where(' user_id='.$userid.' and id='.$id.' and isget=0 and fbillstate=1')->find(); $no='90072'. date("YmdHis") . rand(1000, 9999); //新核销码(全部商品核销) } else{ $list=M('libao_listvip')->where(' user_id='.$userid.' and id='.$id.' and goods_num>usenum')->find(); $no='90071'. date("YmdHis") . rand(1000, 9999); //新核销码(单个商品/服务核销) } if($list) { if($type==1) { $result=M('libao_listvip')->where('lbvipid='.$id)->save(array('freebh'=>$no)); }else{ $result=M('libao_listvip')->where('id='.$id)->save(array('freebh'=>$no)); } if($result) { $res=array( 'code'=>1, 'msg'=>$no, ); } else{ $res=array( 'code'=>-1, 'msg'=>'请重试', ); } } else{ $res=array( 'code'=>-1, 'msg'=>'当前商品已核销完' ); } return json($res); } }