redirect(url('WxHome/login')); } $sn = decodecookie('sn'); $orgId = decodecookie('HospitalId'); if(!$sn||!$orgId){ $this->redirect(url('WxHome/login')); } $data = get_qrcode_arr($sn); if(!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address'){ $this->redirect(url('WxHome/index')); } $this->addressId = $data['id']; $orgName = Db::name('org')->where('id',$orgId)->cache(3600)->value('name'); $this->assign('orgName',$orgName); $user = Db::name('wxuser')->where('id',$userId)->where('del',0)->find(); if(!$user){ $this->redirect(url('WxHome/login')); } if($user['curr_org_id'] != $orgId){ Db::name('wxuser')->where('id',$user['id'])->update(['curr_org_id'=>$orgId]); } $user['isdinder'] = decodecookie('isdinder') == 1?1:0; $user['HospitalName'] = $orgName; $this->orgId = $orgId; $this->user = $user; } public function index(){ $is_spot = decodecookie('is_spot'); $is_spot = $is_spot==2?2:1; // 默认1=当日餐 2=预定餐 $dinner_type_id = input('dinner_type_id/d',0); // 判断是否是点餐员 $bDinder = isset($this->user['isdinder'])?$this->user['isdinder']:false; if ($bDinder||$is_spot==1) { if($bDinder){ encodecookie('is_spot',1); } $is_spot = 1; // 显示当天的 $is_today = 1; } else { $is_today = 0; } $cartCount = $this->cartCount(); $this->assign('cartCount',$cartCount); $this->assign('user',$this->user); $this->assign('is_spot',$is_spot); $this->assign('is_today',$is_today); $this->assign('bDinder',$bDinder); $typeList = model('DinnerType')->getList($this->orgId); if(!$dinner_type_id){ if(isset($typeList[0])){ $dinner_type_id=$typeList[0]['id']; } } $this->assign('dinner_type_id',$dinner_type_id); $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find(); $this->assign('content1',$config?$config['content1']:''); $this->assign('content2',$config?$config['content2']:''); $this->assign('typeList',$typeList); $special = Db::name('dinner_config')->where('org_id',$this->orgId)->value('special'); $special = $special==1?$special:0; if($special){ $isSpecial = $this->checkSpecial(); $this->assign('isSpecial',$isSpecial); } $this->assign('special',$special); return $this->fetch(); } // 点餐页 public function tc(){ if(!$this->checkSpecial()){ $this->error("只有特殊人群才可进入"); } $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周 $this->assign('week',$week); $curday = date('Y-m-d'); $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订 // 当天12点之前可以定明天的餐,12点之后可以定后台的餐 $hour = intval(date('H')); if($hour < 12){ $curday = date('Y-m-d'); }else{ $curday = date('Y-m-d'); $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订 } $cur = get_week_days($week); // 获取当前周已经定过餐的日期 $days1 = Db::name('dinner_order_refuse_item') ->alias('a') ->join('dinner_order_refuse c','c.id = a.order_refuse_id') ->join('dinner_order b','c.dinner_order_id = b.id') ->where('b.cate',1) ->where('c.status',4) ->where('a.day','>=',$cur[0]) ->where('a.day','<=',$cur[1]) ->where('b.user_id',$this->user['id']) ->where('b.org_id',$this->orgId) ->group('a.day') ->distinct(true) ->column('a.day'); $days1 = $days1?$days1:[]; $seldays = Db::name('dinner_order_item') ->alias('a') ->join('dinner_order b','a.order_id = b.id') ->where('b.cate',1) ->where('a.is_refuse',0) ->where('a.day','>=',$cur[0]) ->where('a.day','<=',$cur[1]) ->where('b.user_id',$this->user['id']) ->where('b.org_id',$this->orgId) ->group('a.day') ->distinct(true) ->column('a.day'); $seldays = $seldays?$seldays:[]; $seldays = array_unique(array_merge($seldays,$days1)); // 获取当前周已选择的套餐 $groupId = Db::name('dinner_order_item') ->alias('a') ->join('dinner_order b','a.order_id = b.id') ->where('b.cate',1) ->where('a.day','>=',$cur[0]) ->where('a.day','<=',$cur[1]) ->where('b.user_id',$this->user['id']) ->where('b.org_id',$this->orgId) ->value('a.group_id'); if($groupId > 0){ $map[] = ['id','=',$groupId]; } $map[] = ['start','=',$cur[0]]; $map[] = ['del','=',0]; $map[] = ['enable','=',1]; $map[] = ['org_id','=',$this->orgId]; $groups = Db::name('dinner_group') ->where($map) ->field('id,title,start,end') ->select(); $groups = $groups?$groups:[]; $days = []; $sday = $cur[0]; while (true){ if($sday > $cur[1]){ break; } if($curday < $sday){ if(!$seldays || ($seldays && !in_array($sday,$seldays))){ $days[] = [ 'count' => 0, 'money' => 0, 'day' => $sday, 'text' => get_week_txt($sday), ]; } } $sday = date('Y-m-d',strtotime($sday) + 24*60*60); } $typeList = model('DinnerType')->getList($this->orgId); $typeList = $typeList?$typeList:[]; foreach ($groups as $gk=>$gv){ $combination = Db::name('dinner_group_combination') ->where('group_id',$gv['id']) ->select(); $combination = $combination?$combination:[]; foreach ($combination as $ck=>$cv){ $combination[$ck]['items'] = explode(',',$cv['items']); } $groups[$gk]['combination'] = $combination; $dinners = Db::name('dinner_group_item') ->alias('a') ->join('dinner b','b.id = a.dinner_id') ->where('b.enable',1) ->where('b.cate',1) ->where('a.group_id',$gv['id']) ->field('a.id,a.day,a.group_id,a.max,a.required,a.money,b.name,b.content,b.money as oldmoney,b.imgs,b.dinner_type_id,b.type') ->order('b.sort asc,a.id desc') ->select(); $dinners = $dinners?$dinners:[]; foreach ($dinners as $dkk=>$dvv){ if($dvv['money'] == 0 && $dvv['oldmoney'] > 0){ $dinners[$dkk]['money'] = $dvv['oldmoney']; } $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[]; $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:''; $dinners[$dkk]['nums'] = 0; $dinners[$dkk]['cid'] = 0; // 组合id $items = []; foreach ($combination as $ck=>$cv){ if(in_array($dvv['id'],$cv['items'])){ $items = array_merge($items,$cv['items']); $dinners[$dkk]['cid'] = $cv['id']; } } if($items){ $items = array_diff(array_unique($items),[$dvv['id']]); sort($items); } $dinners[$dkk]['combination'] = $items; } $groups[$gk]['dinners'] = $dinners?$dinners:[]; foreach ($days as $dk=>$dv){ $days[$dk]['group'] = $gv['title']; $types = []; foreach ($typeList as $tk=>$tv){ $limit = Db::name('dinner_group_type') ->where('group_id',$gv['id']) ->where('dinner_type_id',$tv['id']) ->where('day',$dv['day']) ->value('limit'); $coms = []; foreach ($combination as $ck=>$cv){ if($cv['dinner_type_id'] == $tv['id'] && $cv['day'] == $dv['day']){ $coms[] = $cv; } } $combination2 = Db::name('dinner_group_combination2') ->where('group_id',$gv['id']) ->where('dinner_type_id',$tv['id']) ->where('day',$dv['day']) ->select(); $combination2 = $combination2?$combination2:[]; $ndinners = []; foreach ($dinners as $dkk=>$dvv){ if($dvv['day'] == $dv['day'] && $dvv['group_id'] == $gv['id'] && $dvv['dinner_type_id'] == $tv['id']){ $dvv['cid'] = 0; $dvv['ctitle'] = ''; foreach ($coms as $cck=>$ccv){ if(in_array($dvv['id'],$ccv['items'])){ $dvv['cid'] = $ccv['id']; $dvv['ctitle'] = $ccv['title']; break; } } $coms2 = []; foreach ($combination2 as $ck=>$cv){ $items = explode(',',$cv['items']); if(in_array($dvv['id'],$items)){ foreach ($items as $ik=>$iv){ if($iv != $dvv['id'] && !in_array($iv,$coms2)){ $coms2[] = $iv; } } } } $dvv['combination2'] = $coms2; $ndinners[] = $dvv; } } $types[] = [ 'id' => $tv['id'], 'name' => $tv['name'], 'group_id' => $gv['id'], 'group_title' => $gv['title'], 'limit' => $limit > 0?$limit:0, 'dinners' => $ndinners, 'combination' => $coms, ]; } $days[$dk]['types'] = $types; } $groups[$gk]['days'] = $days; } // halt($groups); $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find(); $this->assign('standard',$config['standard']?$config['standard']:0); $this->assign('standardmin',$config['min']?$config['min']:0); $this->assign('groups',$groups); $this->assign('days',$days); $this->assign('user',$this->user); return $this->fetch(); } public function tc备份2(){ if(!$this->checkSpecial()){ $this->error("只有特殊人群才可进入"); } $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周 $this->assign('week',$week); $curday = date('Y-m-d'); $cur = get_week_days($week); // 获取当前周已经定过餐的日期 $seldays = Db::name('dinner_order_item') ->alias('a') ->join('dinner_order b','a.order_id = b.id') ->where('b.cate',1) ->where('a.day','>=',$cur[0]) ->where('a.day','<=',$cur[1]) ->where('b.user_id',$this->user['id']) ->where('b.org_id',$this->orgId) ->group('a.day') ->distinct(true) ->column('a.day'); // 获取当前周已选择的套餐 $groupId = Db::name('dinner_order_item') ->alias('a') ->join('dinner_order b','a.order_id = b.id') ->where('b.cate',1) ->where('a.day','>=',$cur[0]) ->where('a.day','<=',$cur[1]) ->where('b.user_id',$this->user['id']) ->where('b.org_id',$this->orgId) ->value('a.group_id'); if($groupId > 0){ $map[] = ['id','=',$groupId]; } $map[] = ['start','=',$cur[0]]; $map[] = ['del','=',0]; $map[] = ['enable','=',1]; $map[] = ['org_id','=',$this->orgId]; $groups = Db::name('dinner_group') ->where($map) ->field('id,title,start,end') ->select(); $groups = $groups?$groups:[]; $days = []; $sday = $cur[0]; while (true){ if($sday > $cur[1]){ break; } if($curday < $sday){ if(!$seldays || ($seldays && !in_array($sday,$seldays))){ $days[] = [ 'count' => 0, 'money' => 0, 'day' => $sday, 'text' => get_week_txt($sday), ]; } } $sday = date('Y-m-d',strtotime($sday) + 24*60*60); } $typeList = model('DinnerType')->getList($this->orgId); $typeList = $typeList?$typeList:[]; foreach ($groups as $gk=>$gv){ foreach ($days as $dk=>$dv){ $days[$dk]['group'] = $gv['title']; $types = []; foreach ($typeList as $tk=>$tv){ $dinners = Db::name('dinner_group_item') ->alias('a') ->join('dinner b','b.id = a.dinner_id') ->where('b.enable',1) ->where('b.cate',1) ->where('b.dinner_type_id',$tv['id']) ->where('a.group_id',$gv['id']) ->where('a.day',$dv['day']) ->field('a.id,a.day,a.group_id,b.name,b.content,b.money,b.imgs,a.required,b.dinner_type_id,b.type') ->order('b.sort asc,a.id desc') ->select(); $dinners = $dinners?$dinners:[]; foreach ($dinners as $dkk=>$dvv){ $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[]; $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:''; $dinners[$dkk]['nums'] = 0; } $limit = Db::name('dinner_group_type') ->where('group_id',$gv['id']) ->where('dinner_type_id',$tv['id']) ->where('day',$dv['day']) ->value('limit'); $types[] = [ 'id' => $tv['id'], 'name' => $tv['name'], 'group_id' => $gv['id'], 'group_title' => $gv['title'], 'limit' => $limit > 0?$limit:0, 'dinners' => $dinners, ]; } $days[$dk]['types'] = $types; } $groups[$gk]['days'] = $days; } $this->assign('groups',$groups); $this->assign('days',$days); $this->assign('user',$this->user); return $this->fetch(); } public function tc备份(){ if(!$this->checkSpecial()){ $this->error("只有特殊人群才可进入"); } $week = input('week/d',0); // -1=全部默认本周 0=本周 1=下周 $this->assign('week',$week); $curday = date('Y-m-d'); $cur = get_week_days($week); $weeks = [ ['week'=>$week,'start'=>$cur[0],'end'=>$cur[1],'selected'=>1], ]; foreach ($weeks as $k=>$v){ $days = []; $sday = $v['start']; while (true){ if($sday > $v['end']){ break; } if($curday < $sday){ $days[] = [ 'count' => 0, 'money' => 0, 'day' => $sday, 'text' => get_week_txt($sday), ]; } $sday = date('Y-m-d',strtotime($sday) + 24*60*60); } foreach ($days as $dk=>$dv){ $groups = Db::name('dinner_group') ->where('org_id',$this->orgId) ->where('del',0) ->where('enable',1) ->where('start',$v['start']) ->select(); $groups = $groups?$groups:[]; $types = []; foreach ($groups as $kk=>$vv){ $typeList = model('DinnerType')->getList($this->orgId); $typeList = $typeList?$typeList:[]; foreach ($typeList as $tk=>$tv){ $dinners = Db::name('dinner_group_item') ->alias('a') ->join('dinner b','b.id = a.dinner_id') ->where('b.enable',1) ->where('b.cate',1) ->where('b.dinner_type_id',$tv['id']) ->where('a.group_id',$vv['id']) ->where('a.day',$dv['day']) ->field('a.id,a.day,a.group_id,b.name,b.content,b.money,b.imgs,a.required,b.dinner_type_id') ->order('b.sort asc,a.id desc') ->select(); $dinners = $dinners?$dinners:[]; foreach ($dinners as $dkk=>$dvv){ $imgs = $dvv['imgs']?explode(',',$dvv['imgs']):[]; $dinners[$dkk]['imgs'] = $imgs?$imgs[0]:''; // $dinners[$dkk]['selected'] = $dvv['required']?1:0; } $limit = Db::name('dinner_group_type') ->where('group_id',$vv['id']) ->where('dinner_type_id',$tv['id']) ->where('day',$dv['day']) ->value('limit'); $types[] = [ 'id' => $tv['id'], 'name' => $tv['name'], 'group_id' => $vv['id'], 'group_title' => $vv['title'], 'group_type' => $vv['type'], 'limit' => $limit > 0?$limit:0, 'dinners' => $dinners, ]; } } $days[$dk]['types'] = $types; } $weeks[$k]['days'] = $days; } $this->assign('weeks',$weeks); $groups = Db::name('dinner_group') ->where('org_id',$this->orgId) ->where('del',0) ->where('enable',1) ->where('start',$cur[0]) ->select(); $groups = $groups?$groups:[]; $this->assign('groups',$groups); $this->assign('user',$this->user); return $this->fetch(); } // 点套餐商品页 public function group(){ } public function cartCount(){ $cartCount = Db::name('dinner_cart') ->where('user_id',$this->user['id']) ->where('org_id',$this->orgId) ->sum('count'); return $cartCount; } public function indexchange(){ $is_spot = input('spot/d',1); $is_spot = $is_spot==1?1:2; // 默认1当日餐 2=预定餐 // 清空购物车 Db::name('dinner_cart')->where('user_id',$this->user['id'])->delete(); encodecookie('is_spot',$is_spot); $this->redirect(url('WxBookDinner/index')); } public function get_type_json(){ $size = input('size'); $page = input('page'); $today = input('today'); $btoday = false; if (!empty($today)&&($today==1)) { $btoday = true; } $DinnerTypeId = input('type'); $ret = $this->show_all_page_by_type($btoday, $DinnerTypeId,$page, $size); if (empty($ret)) { header('Content-Type:application/json; charset=utf-8'); exit(json_encode([])); } else { header('Content-Type:application/json; charset=utf-8'); exit(json_encode($ret)); } } public function show_all_page_by_type($today, $DinnerTypeId, $page, $size){ $query = Db::name('dinner'); $query->where('cate', 0); if ($DinnerTypeId>0) { $query->where('dinner_type_id', $DinnerTypeId); } if($today){ $query->where('today_enable', 1); // 限定时间 $currtime = date('H:i'); $query->whereRaw(Db::raw('(today_start_time<="'.$currtime.'" and today_end_time>="'.$currtime.'") or today_start_time= null or today_end_time=null')); }else{ $query->where('enable', 1); // 限定时间 $currtime = date('H:i'); $query->whereRaw(Db::raw('(start_time<="'.$currtime.'" and end_time>="'.$currtime.'") or start_time= null or end_time=null')); } $query->where('org_id', $this->orgId); $query->order('sort ASC,id desc'); $query->page($page,$size); $ret = $query->select(); if (empty($ret)) { return array(); } $isSpecial = $this->checkSpecial(); foreach ($ret as $key => $value) { $img = $value['imgs']?explode(',',$value['imgs']):[]; $ret[$key]['img'] = $img?$img[0]:''; if($isSpecial){ $money = $this->getDiscountMoney($value); }else{ $money = $value['money']; } $ret[$key]['money'] = sprintf("%.2f", $money); } return $ret; } public function getDiscountMoney($value){ $money = $value['money']; if($value['free'] == 1){ $money = 0; }else{ $money = $money*$value['discount']; } return round($money,2); } public function addCart(){ $id = input('id/d',0); if($id <= 0){ HelpHander::error('参数错误'); } $map[] = ['dinner_id','=',$id]; $map[] = ['org_id','=',$this->orgId]; $map[] = ['user_id','=',$this->user['id']]; $num = input('num/d',1); $info = Db::name('dinner_cart')->where($map)->find(); if(!empty($info)){ Db::name('dinner_cart')->where($map)->setInc('count',$num); $count = $num+$info['count']; }else{ $a = [ 'dinner_id'=>$id, 'org_id'=>$this->orgId, 'user_id'=>$this->user['id'], 'count'=>1, 'create_time'=>getTime(), ]; Db::name('dinner_cart')->insertGetId($a); $count = 1; } HelpHander::success(['count'=>$count],'操作成功'); } public function updateCart(){ $id = input('id/d',0); if($id <= 0){ HelpHander::error('参数错误'); } $map[] = ['id','=',$id]; $map[] = ['org_id','=',$this->orgId]; $map[] = ['user_id','=',$this->user['id']]; $num = input('num/d',1); Db::name('dinner_cart')->where($map)->update([ 'count'=>$num ]); Db::name('dinner_cart')->where($map)->where('count',0)->delete(); HelpHander::success(['count'=>$this->cartCount()],'操作成功'); } public function detail(){ $id = input('id/d',0); $res = Db::name('dinner')->where('id',$id)->find(); if(!$res){ return $this->fetch('h5/msg_error',['msg'=>'记录不存在']); } $res['imgs'] = $res['imgs']?explode(',',$res['imgs']):[]; $isSpecial = $this->checkSpecial(); if($isSpecial){ $res['money'] = $this->getDiscountMoney($res); } $this->assign('info',$res); $this->assign('t',time()); return $this->fetch(); } public function cart(){ // 需要检查是否还在售卖时间范围内,只取范围内的数据 $cartCount = $this->cartCount(); $map[] = ['org_id','=',$this->orgId]; $map[] = ['user_id','=',$this->user['id']]; $info = Db::name('dinner_cart')->where($map)->select(); $a = []; foreach ($info as $k=>$v){ $data = Db::name('dinner')->where('id',$v['dinner_id'])->find(); $imgs = $data['imgs']?explode(',',$data['imgs']):[]; $data['img'] = $imgs?$imgs[0]:''; $data['count'] = $v['count']; $data['cart_id'] = $v['id']; $a[] = $data; } $this->assign('cartCount',$cartCount); $this->assign('data',$a); return $this->fetch(); } public function submitorder(){ $bDinder = $this->user['isdinder']; if (request()->isGet()) { if(!decodecookie('is_spot')){ return $this->fetch('h5/msg_error',['msg'=>'参数错误']); } $orderids = input('orders'); if (empty($orderids)) { return $this->fetch('h5/msg_error',['msg'=>'ids不能为空']); } $ids = explode(',', $orderids); //创建订单 $cart =Db::name('dinner_cart') ->where('id','in',$ids) ->where('org_id',$this->orgId) ->where('user_id',$this->user['id']) ->column('dinner_id'); if(empty($cart)){ return $this->fetch('h5/msg_error',['msg'=>'购物车信息不存在']); } $all_data = Db::name('dinner') ->where('id','in',$cart) ->select(); if(count($all_data) != count($ids)){ return $this->fetch('h5/msg_error',['msg'=>'参数错误']); } $is_spot = decodecookie('is_spot'); $is_spot = $is_spot==1?1:0; $price_count =0; $curTime = date('Y-m-d H:i:s'); $isSpecial = $this->checkSpecial(); foreach ($all_data as $key => $value) { $catNum = Db::name('dinner_cart') ->where('dinner_id',$value['id']) ->where('org_id',$this->orgId) ->where('user_id',$this->user['id']) ->value('count'); if($is_spot == 1){ if($value['today_enable'] != 1){ return $this->fetch('h5/msg_error',['msg'=>$value['name'].'已下架']); } if($curTime < date('Y-m-d').' '.$value['today_start_time']||$curTime > date('Y-m-d').' '.$value['today_end_time']){ return $this->fetch('h5/msg_error',['msg'=>$value['name'].'不再售卖时间内']); } } if($is_spot == 0){ if($value['enable'] != 1){ return $this->fetch('h5/msg_error',['msg'=>$value['name'].'已下架']); } if($curTime < date('Y-m-d').' '.$value['start_time']||$curTime > date('Y-m-d').' '.$value['end_time']){ return $this->fetch('h5/msg_error',['msg'=>$value['name'].'不再售卖时间内']); } } $money = $value['money']; if($isSpecial == 1){ $money = $this->getDiscountMoney($value); } $all_data[$key]['money'] = $money; $all_data[$key]['count'] = $catNum; $price_count += $money*$catNum; } $addressInfo = Db::name('dinner_address') ->where('id',$this->addressId) ->find(); if(!$addressInfo){ return $this->fetch('h5/msg_error',['msg'=>"地点不正确,请重新扫码"]); } $address = $addressInfo?$addressInfo['title']:''; $is_pay = true; $ret = Db::name('dinner_order') ->where('org_id',$this->orgId) ->where('user_id',$this->user['id']) ->order('id desc') ->find(); if($ret){ $this->assign('name',$ret['name']); $this->assign('phone',$ret['mobile']); }else{ $this->assign('name',''); $this->assign('phone',''); } $this->assign('list',$all_data); $this->assign('count',$price_count); $this->assign('address',$address); $this->assign('bDinder',$bDinder); $this->assign('is_pay',$is_pay); $this->assign('HospitalName',$this->user['HospitalName']); $this->assign('ids',$orderids); //显示当前医院 return $this->fetch(); } else { if(!decodecookie('is_spot')){ HelpHander::error('参数错误'); } $orderids = input('orders'); if (empty($orderids)) { HelpHander::error('ids不能为空'); } if (empty($this->user)) { HelpHander::error('未登录请先登录'); } $ids = explode(',', $orderids); //创建订单 $cart =Db::name('dinner_cart') ->where('id','in',$ids) ->where('org_id',$this->orgId) ->where('user_id',$this->user['id']) ->column('dinner_id'); if(empty($cart)){ HelpHander::error('购物车信息不存在'); } $all_data = Db::name('dinner') ->where('id','in',$cart) ->select(); if(count($all_data) != count($ids)){ HelpHander::error('参数错误'); } $is_spot = decodecookie('is_spot'); $is_spot = $is_spot==1?1:0; $is_take = input('is_take'); if($is_take==1){ $typeId = array_unique(array_column($all_data,'dinner_type_id')); if($typeId >1){ HelpHander::error('自取订单只能下单同一分类菜品'); } } $price_count = 0; $money_count = 0; $curTime = date('Y-m-d H:i:s'); $isSpecial = $this->checkSpecial(); foreach ($all_data as $key => $value) { $catNum = Db::name('dinner_cart') ->where('dinner_id',$value['id']) ->where('org_id',$this->orgId) ->where('user_id',$this->user['id']) ->value('count'); if($is_spot == 1){ if($value['today_enable'] != 1){ HelpHander::error($value['name'].'已下架'); } if($curTime < date('Y-m-d').' '.$value['today_start_time']||$curTime > date('Y-m-d').' '.$value['today_end_time']){ HelpHander::error($value['name'].'不再售卖时间内'); } } if($is_spot == 0){ if($value['enable'] != 1){ HelpHander::error($value['name'].'已下架'); } if($curTime < date('Y-m-d').' '.$value['start_time']||$curTime > date('Y-m-d').' '.$value['end_time']){ HelpHander::error($value['name'].'不再售卖时间内'); } } $money1 = $value['money']; if($isSpecial == 1){ $money1 = $this->getDiscountMoney($value); } $all_data[$key]['money'] = $money1; $all_data[$key]['original_price'] = $value['money']; $all_data[$key]['count'] = $catNum; $price_count += $money1*$catNum; $money_count += $value['money']*$catNum; } $money = $money_count - $price_count; $name =input('name'); $mobile = input('mobile'); $address = input('address'); $order_type = input('order_type'); $remark = trim(input('remark')); if($remark && mb_strlen($remark) > 15){ HelpHander::error('床号及备注最多15字'); } // log_message('error','值是:'.$ORDER_TYPE); // 如果没有的话,表示的就是自己订餐 // 1-微信;2-食堂卡;3-现金;4-未付款(不需要付款) if (empty($order_type)) { $order_type = 1; $is_replace = 0; } // 4-未付款 elseif ($order_type==4) { $is_replace = 0; } // else { $is_replace = 1; } $bDinder = $this->user['isdinder']; if($bDinder){ $is_spot = 1; // 点餐员只能点当日餐 } if (empty($name)) { HelpHander::error('姓名不能为空'); } if (empty($mobile)) { HelpHander::error('手机号不能为空'); } if(!check_mobile($mobile)){ HelpHander::error('手机号格式错误'); } if (empty($address)) { HelpHander::error('地址不能为空'); } if (empty($remark)) { HelpHander::error('床号及备注不能为空'); } Db::startTrans(); if ($order_type==1) { $state = 0; } else { $state = 1; } if($price_count == 0){ $state = 1; } try{ $pp = array( 'price' => $price_count, 'state' => $state, 'org_id'=>$this->orgId, 'sn' => get_unique_id(), 'create_time' => date('Y-m-d H:i:s'), 'ymd'=>date('Ymd'), 'y'=>date('Y'), 'ym'=>date('Ym'), 'w'=>date('Y').date('W'), 'pay_time' => date('Y-m-d H:i:s'), 'user_id' => $this->user['id'], 'order_type'=>$order_type, 'name'=>$name, 'mobile'=>$mobile, 'address'=>$address, 'is_replace'=>$is_replace, 'is_spot'=>$is_spot, 'remark'=>$remark, 'is_take'=>$is_take, 'cate' => 0, 'money' => $money, ); $orderId = Db::name('dinner_order') ->insertGetId($pp); if(!$orderId){ \exception('订单添加失败'); } $item = []; foreach ($all_data as $kk=>$vv){ $item[] = [ 'dinner_id'=>$vv['id'], 'order_id'=>$orderId, 'price'=>$vv['money']*$vv['count'], 'num'=>$vv['count'], 'dinner_type_id'=>$vv['dinner_type_id'], 'original_price'=>$vv['original_price'], 'money'=>$vv['money'], ]; } $r = Db::name('dinner_order_item')->insertAll($item); if(!$r){ \exception('操作失败'); } $rr = Db::name('dinner_cart') ->where('id','in',$ids) ->delete(); if(!$rr){ \exception('操作失败'); } Db::commit(); if ($state==0) { HelpHander::success(['url'=>url('pay').'?id='.$orderId],'操作成功' ); } else { HelpHander::success(['url'=>url('payresult').'?id='.$orderId], '操作成功'); } }catch (Exception $e){ Db::rollback(); HelpHander::error($e->getMessage()); } } } public function submitorder2(){ $user = $this->user; if (request()->isGet()) { $ret = Db::name('dinner_apply') ->where('org_id',$this->orgId) ->where('user_id',$user['id']) ->where('del',0) ->where('status',1) ->find(); if(!$ret){ return $this->fetch('h5/msg_error',['msg'=>'只有特殊人群才可进入']); } $this->assign('name',$ret['name']); $this->assign('phone',$ret['phone']); $addressInfo = Db::name('dinner_address') ->where('id',$this->addressId) ->find(); if(!$addressInfo){ return $this->fetch('h5/msg_error',['msg'=>"地点不正确,请重新扫码"]); } $address = $addressInfo?$addressInfo['title']:''; $this->assign('HospitalName',$this->user['HospitalName']); $this->assign('address',$address); return $this->fetch(); } else { if (empty($this->user)) { $this->error('未登录请先登录'); } $is_take = 0; $name =input('name','','trim'); $mobile = input('mobile','','trim'); $address = input('address','','trim'); $order_type = 4; $remark = trim(input('remark')); if($remark && mb_strlen($remark) > 15){ $this->error('床号及备注最多15字'); } if (empty($name)) { $this->error('姓名不能为空'); } if (empty($mobile)) { $this->error('手机号不能为空'); } if(!check_mobile($mobile)){ $this->error('手机号格式错误'); } if (empty($address)) { $this->error('地址不能为空'); } if (empty($remark)) { $this->error('床号及备注不能为空'); } $config = Db::name('dinner_config')->where('org_id',$this->orgId)->find(); $special = $config&&$config['special']==1?1:0; if(!$special){ $this->error('未开启套餐预定模式'); } $money = 0; $img = ''; $goods = input('goods'); if(!$goods){ $this->error('参数错误'); } $goods = json_decode($goods,true); if(!$goods){ $this->error('参数错误'); } $days = []; $type = 0; $all_data = []; $groups = []; foreach ($goods as $k=>$v){ if($v['nums'] <= 0){ $this->error('参数错误'); } $dinner = Db::name('dinner_group_item') ->alias('a') ->join('dinner_group b','a.group_id = b.id') ->join('dinner c','a.dinner_id = c.id') ->where('a.id',$v['id']) ->where('b.enable',1) ->where('b.del',0) ->where('c.enable',1) ->where('c.cate',1) ->field('a.group_id,a.day,a.money,c.id,c.money as oldmoney,c.dinner_type_id,c.type,a.max,c.name') ->find(); if(!$dinner){ $this->error('选择的部分商品不存在或已下架'); } if($dinner['max'] > 0 && $v['nums'] > $dinner['max']){ $this->error($dinner['name'].'最多选择'.$dinner['max'].'个'); } if($dinner['money'] == 0 && $dinner['oldmoney'] > 0){ $dinner['money'] == $dinner['oldmoney']; } unset($dinner['oldmoney']); // 当天12点之前可以定明天的餐,12点之后可以定后台的餐 $hour = intval(date('H')); if($hour < 12){ $curday = date('Y-m-d'); if($dinner['day'] <= $curday){ $this->error('12点之前只能提前一天预订'); } }else{ $curday = date('Y-m-d'); $curday = date('Y-m-d',strtotime($curday) + 24*60*60); // 提前2天订 if($dinner['day'] <= $curday){ $this->error('12点之后只能后天的餐'); } } $groups[] = $dinner['group_id']; $dinner['nums'] = $v['nums']; $all_data[] = $dinner; $days[] = $dinner['day']; if($dinner['type'] == 1){ $type = 1; } $money = $money + $dinner['money']*$v['nums']; } if($config['standard'] > 0){ foreach ($days as $day){ $dmoney = 0; foreach ($all_data as $k=>$v){ if($v['day'] == $day){ $dmoney += $v['money']*$v['nums']; } } if($dmoney > $config['standard']){ $this->error($day.'超过餐标标准'); } } } $groups = array_unique($groups); if(count($groups) > 1){ $this->error('一周只能点一种套餐'); } // 检查本周是否有其他订单 $week = get_week_days(0); // 本周 $week1 = get_week_days(1); // 下周 $day = $days[0]; $start = ""; $end = ""; if($day >= $week[0] && $day <= $week[1]){ $start = $week[0]; $end = $week[1]; } if($day >= $week1[0] && $day <= $week1[1]){ $start = $week1[0]; $end = $week1[1]; } if(!$start){ $this->error('只能预订本周或下周的餐'); } $ret = Db::name('dinner_order_item') ->alias('a') ->join('dinner_order b','a.order_id = b.id') ->where('b.cate',1) ->where('b.user_id',$this->user['id']) ->where('a.group_id','<>',$groups[0]) ->where('a.day','>=',$start) ->where('a.day','<=',$end) ->find(); if($ret){ $this->error('一周只能点一种套餐'); } // 验证这些天是否已有订单 foreach ($days as $k=>$v){ $days1 = Db::name('dinner_order_refuse_item') ->alias('a') ->join('dinner_order_refuse c','c.id = a.order_refuse_id') ->join('dinner_order b','c.dinner_order_id = b.id') ->where('b.cate',1) ->where('c.status',4) ->where('a.day',$v) ->where('b.user_id',$this->user['id']) ->field('a.*') ->find(); if($days1){ $this->error($v.'已下过订单,不能下单'); } $ret = Db::name('dinner_order_item') ->alias('a') ->join('dinner_order b','a.order_id = b.id') ->where('b.user_id',$this->user['id']) ->where('b.cate',1) ->where('a.is_refuse',0) ->where('a.day',$v) ->find(); if($ret){ $this->error($v.'已下过订单,不能下单'); } } if($type == 1){ // 必须上传医嘱证明 if(!$_FILES['files']['tmp_name']){ $this->error('未上传医嘱证明'); } try{ $updir = config('upload_dir'); $file = request()->file('files'); $fconfig = config('admin_upload_img'); $finfo = $file->validate($fconfig) ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files'); if($finfo){ $img = '/'.$updir.'/files/' . $finfo->getSaveName(); $img = request()->domain(true).str_replace('\\', '/', $img); }else{ // 上传失败获取错误信息 \exception($file->getError()); } }catch (\Exception $e){ $this->error($e->getMessage()); } } Db::startTrans(); try{ $pp = array( 'price' => 0, 'state' => 1, 'org_id' => $this->orgId, 'sn' => get_unique_id(), 'create_time' => date('Y-m-d H:i:s'), 'ymd'=>date('Ymd'), 'y'=>date('Y'), 'ym'=>date('Ym'), 'w'=>date('Y').date('W'), 'pay_time' => date('Y-m-d H:i:s'), 'user_id' => $this->user['id'], 'order_type'=>$order_type, 'name'=>$name, 'mobile'=>$mobile, 'address'=>$address, 'remark'=>$remark, 'is_take'=>$is_take, 'cate' => 1, 'money' => $money, 'img' => $img ); $orderId = Db::name('dinner_order')->insertGetId($pp); if(!$orderId){ \exception('订单添加失败'); } $item = []; foreach ($all_data as $kk=>$vv){ $item[] = [ 'dinner_id'=>$vv['id'], 'order_id'=>$orderId, 'price'=>$vv['money']*$vv['nums'], 'original_price' => $vv['money'], 'num'=>$vv['nums'], 'dinner_type_id'=>$vv['dinner_type_id'], 'group_id'=>$vv['group_id'], 'day' => $vv['day'], 'money'=>$vv['money'], ]; } $r = Db::name('dinner_order_item')->insertAll($item); if(!$r){ \exception('操作失败'); } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error($e->getMessage()); } $this->redirect(url('payresult').'?id='.$orderId); } } public function pay() { $id = input('id'); if (empty($id)) { return $this->fetch('h5/msg_error',['msg'=>'ID不能为空']); } $_order = Db::name('dinner_order') ->where('id',$id) ->find(); if (empty($_order)) { return $this->fetch('h5/msg_error',['msg'=>'不存此订单']); } if($_order['state'] != 0){ return $this->fetch('h5/msg_error',['msg'=>'此订单不能支付']); } if($_order['state'] == 0){ $tt = time() - strtotime($_order['create_time']); if($tt >= 30*60){ // 半小时内可以支付 return $this->fetch('h5/msg_error',['msg'=>'此订单已过期不能支付']); } } $notify_url = getSite().url('Notify/payDinner',['org_id'=>$_order['org_id']]); $org = Db::name('org')->where('id',$_order['org_id'])->find(); $body = $org['name'].'_'.$_order['org_id']; $config =get_pay_wechat($_order['org_id']); $app = \EasyWeChat\Factory::payment($config); $result = $app->order->unify([ 'body' => $body, 'out_trade_no' => $_order['sn'], 'total_fee' => $_order['price']*100, 'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型 'openid' => $this->user['openid'], ]); trace($notify_url); if($result['return_code'] != 'SUCCESS' || $result['result_code'] != 'SUCCESS'){ $msg = isset($result['return_msg'])?$result['return_msg']:'调起失败支付'; return $this->fetch('h5/msg_error',['msg'=>$msg]); } $jssdk = $app->jssdk; $jsApiParameters = $jssdk->bridgeConfig($result['prepay_id']); $params = array( 'orderid'=>$id, 'localorder' => $_order, 'openid' => $this->user['openid'], // 'input' => $input, 'config' => $config, // 'order' => $order, 'jsApiParameters' => $jsApiParameters, // 'editAddress' => $editAddress, ); $params['jsApiParameters'] = json_encode($params['jsApiParameters']); // var_dump($params);exit(); $this->assign('params',$params); return $this->fetch(); } public function payresult() { $msg = ''; $detail= ''; $type=0; $id = input('id'); if (empty($id)) { $msg='ID不能为空'; } else { $_order = Db::name('dinner_order') ->where('id',$id) ->find(); if (empty($_order)) { $msg='不存在此订单'; } else { if ($_order['state']==1) { $msg='订购成功!'; $type=1; // 减库存 // $this->load->model('DinnercartModel'); // $this->DinnercartModel->dec_diner($_order); } else { $msg='支付可能失败'; $detail= '没有查询到订单成功的状态,可能系统缓慢原因导致,稍后在我的订单里查看'; } } } $this->assign('msg',$msg); $this->assign('type',$type); $this->assign('detail',$detail); return $this->fetch(); } public function checkSpecial(){ $isSpecial = false; $user = $this->user; $ret = Db::name('dinner_apply') ->where('org_id',$this->orgId) ->where('user_id',$user['id']) ->where('del',0) ->where('status',1) ->find(); if($ret){ $isSpecial = true; } return $isSpecial; } public function order() { $DinnerCount = $this->cartCount(); //用户个人信息 $user = $this->user; $org = Db::name('org')->where('id',$this->orgId)->find(); $user['HospitalName'] = $org['name']; $this->assign('orgName',$org['name']); $DinnerCart = []; $this->assign('cart',$DinnerCart); $this->assign('cartCount',$DinnerCount); $this->assign('user',$user); $special = Db::name('dinner_config')->where('org_id',$this->orgId)->value('special'); $special = $special==1?$special:0; if($special){ $isSpecial = 0; //未申请 $apply = Db::name('dinner_apply') ->where('org_id',$this->orgId) ->where('user_id',$user['id']) ->where('del',0) ->where('status','in',[0,1]) ->find(); if($apply && $apply['status'] == 1){ $isSpecial = 2; // 通过审核 } if($apply && $apply['status'] == 0){ $isSpecial = 1; // 审核中 } $this->assign('isSpecial',$isSpecial); } $this->assign('special',$special); return $this->fetch(); } public function userapply(){ if(request()->isPost()){ $user = $this->user; $ret = Db::name('dinner_apply') ->where('org_id',$this->orgId) ->where('user_id',$user['id']) ->where('del',0) ->find(); if($ret && $ret['status'] == 0){ $this->error('信息审核中,勿重复提交'); } if($ret && $ret['status'] == 1){ $this->error('你已是特殊人群,勿重复提交'); } $updir = config('upload_dir'); $imgs = []; if(!$_FILES['files']['tmp_name'][0]){ $this->error('未上传证明图片'); } try{ $files = request()->file('files'); foreach($files as $file){ // 移动到框架应用根目录/uploads/ 目录下 $info = $file->validate(config('app.admin_upload_img'))->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files'); if($info){ $img = '/'.$updir.'/files/' . $info->getSaveName(); $img = str_replace('\\', '/', $img); $path = request()->domain(true).$img; $imgs[] = $path; }else{ \exception($file->getError()); } } }catch (\Exception $e){ $this->error($e->getMessage()); } $data = [ 'name' => input('name','','trim'), 'phone' => input('TEL','','trim'), 'imgs' => $imgs?implode(',',$imgs):'', 'org_id' => $this->orgId, 'user_id' => $this->user['id'], 'status' => 0, 'create_time' => date('Y-m-d H:i:s'), 'remark' => input('remark','','trim') ]; if(!$data['name']){ $this->error('请输入姓名'); } if(!$data['phone']){ $this->error('请输入手机号'); } if(!check_mobile($data['phone'])){ $this->error('手机号格式错误'); } $ret = Db::name('dinner_apply')->insert($data); if(!$ret){ $this->error('操作失败'); }else { $this->redirect(url('order')); } }else{ return $this->fetch(); } } public function get_orders_json() { $user = $this->user; $size = input('size'); $page = input('page'); $ret=$this->show_all_orders($user['id'], $page, $size); if (empty($ret)) { header('Content-Type:application/json; charset=utf-8'); exit(json_encode([])); } else { header('Content-Type:application/json; charset=utf-8'); exit(json_encode($ret)); } } public function show_all_orders($wxuserid, $page, $size) { $map[] = ['user_id','=',$wxuserid]; $ret = Db::name('dinner_order') ->where($map) ->page($page,$size) ->order('id','desc')->select(); foreach ($ret as $key => $value) { $ret[$key]['Todo'] = null; $ret[$key]['pay'] = 0; $ret[$key]['refund'] = 0; if($value['state'] == 0){ $tt = time() - strtotime($value['create_time']); if($tt < 30*60){ // 半小时内可以支付 $ret[$key]['pay'] = 1; } } if($value['state']== 1||$value['state'] == 3){ if(in_array($value['is_ok'],[0,2])){ $day = date('Y-m-d'); $cday = date('Y-m-d',strtotime($value['create_time'])); if($day == $cday){ // 仅当天可退款 $ret[$key]['refund'] = 1; } } } } return $ret; } public function order_detail(){ $id = input('id'); $info = Db::name('dinner_order') ->where('id',$id) ->find(); if(!$info){ $this->error('订单不存在'); } if($info['cate'] == 0){ $all = Db::name('dinner_order_item') ->alias('a') ->join('dinner b','a.dinner_id=b.id') ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.content,b.imgs') ->where('a.order_id',$info['id']) ->where('a.is_refuse',0) ->select(); foreach ($all as $k=>$v){ $im = $v['imgs']?explode(',',$v['imgs']):[]; $all[$k]['img'] = $im?$im[0]:''; } $refuse = Db::name('dinner_order_refuse_item') ->alias('a') ->join('dinner_order_refuse c','a.order_refuse_id=c.id') ->join('dinner b','a.dinner_id=b.id') ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.money,b.content,b.imgs') ->where('c.dinner_order_id',$info['id']) ->select(); foreach ($refuse as $k=>$v){ $im = $v['imgs']?explode(',',$v['imgs']):[]; $refuse[$k]['img'] = $im?$im[0]:''; } $this->assign('item',$all); $this->assign('refuse_item',$refuse); }else{ // 套餐 $all = Db::name('dinner_order_item') ->alias('a') ->join('dinner b','a.dinner_id=b.id') ->join('dinner_group c','a.group_id=c.id') ->join('dinner_type d','a.dinner_type_id=d.id') ->field('a.*,b.name as dinner_name,c.title as group_title,d.name as type_name,b.type') ->where('a.order_id',$info['id']) ->where('a.is_refuse',0) ->order('a.id asc') ->select(); $all = $all?$all:[]; $types = []; $tids = []; foreach ($all as $k=>$v){ $tid = $v['dinner_type_id'].'|'.$v['group_id'].'|'.$v['day']; if(!in_array($tid,$tids)){ $tids[] = $tid; } } $curday = date('Y-m-d'); // $curday = date('Y-m-d',strtotime($curday) + 24*60*60); foreach ($tids as $tk=>$tv){ $dinners = []; $type = 0; $dinner_type_id = 0; $dinner_type = ''; $group = ''; $group_id = 0; $day = ''; foreach ($all as $k=>$v){ $tid = $v['dinner_type_id'].'|'.$v['group_id'].'|'.$v['day']; if($tid == $tv){ $dinner_type = $v['type_name']; $dinner_type_id = $v['dinner_type_id']; $group = $v['group_title']; $group_id = $v['group_id']; $day = $v['day']; $dinners[] = $v['dinner_name'].'*'.$v['num']; if($v['type'] == 1){ $type = 1; } } } $options = 1; if($info['state'] != 1){ $options = 0; } if($day <= $curday){ $options = 0; } $types[] = [ 'dinner_type_id' => $dinner_type_id, 'dinner_type' => $dinner_type, 'day' => $day, 'options' => $options, 'group' => $group, 'group_id' => $group_id, 'type' => $type, 'dinners' => implode(',',$dinners), 'sign' => think_encrypt($info['id'].'|'.$dinner_type_id.'|'.$day.'|'.$group_id) ]; } $info['types'] = $types; } $info['orgName'] = Db::name('org') ->where('id',$info['org_id']) ->value('name'); $this->assign('info',$info); return $this->fetch(); } public function canceldinner(){ $sign = think_decrypt(input('sign','','trim')); if(!$sign){ $this->error('参数错误'); } $signs = explode('|',$sign); if(count($signs) != 4){ $this->error('参数错误'); } $orderId = $signs[0]; $dinnerTypeId = $signs[1]; $day = $signs[2]; $groupId = $signs[3]; $cur = date('Y-m-d'); if($day <= $cur){ $this->error('只能取消明天或之后的餐'); } $order = Db::name('dinner_order')->where('id',$orderId)->find(); if(!$order){ $this->error("订单不存在"); } if($order['state'] != 1){ $this->error('订单该状态无法操作'); } if($order['user_id'] != $this->user['id']){ $this->error('无权限操作'); } // 取出 $ids = Db::name('dinner_order_item') ->where('is_refuse',0) ->where('dinner_type_id',$dinnerTypeId) ->where('group_id',$groupId) ->where('day',$day) ->where('order_id',$orderId) ->column('id'); if(!$ids){ $this->error("参数错误或已取消"); } Db::startTrans(); try{ // 查询订单下是否还有有效餐品 $ids2 = Db::name('dinner_order_item') ->where('is_refuse',0) ->where('id','not in',$ids) ->where('order_id',$orderId) ->column('id'); if(!$ids2){ // 没有有效的餐品,订单改完已退款状态 $ret = Db::name('dinner_order')->where('id',$orderId)->update(['state'=>5,'update_time'=>date('Y-m-d H:i;s')]); if(!$ret){ \exception('操作失败'); } } $ret = Db::name('dinner_order_item')->where('id','in',$ids)->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i;s')]); if(!$ret){ \exception('操作失败'); } Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error('操作失败'); } $this->success('操作成功'); } public function changedinner(){ $initsign = input('sign','','trim'); $sign = think_decrypt($initsign); if(!$sign){ if(request()->isPost()){ $this->error('参数错误'); }else{ return $this->fetch('h5/msg_error',['msg'=>'参数错误']); } } $signs = explode('|',$sign); if(count($signs) != 4){ if(request()->isPost()){ $this->error('参数错误'); }else{ return $this->fetch('h5/msg_error',['msg'=>'参数错误']); } } $orderId = $signs[0]; $dinnerTypeId = $signs[1]; $day = $signs[2]; $groupId = $signs[3]; if(request()->isPost()){ $dinnerId = input('dinner_id/d',0); if($dinnerId <= 0){ $this->error('请选择要替换的流食或半流食餐品'); } $info = Db::name('dinner_group_item') ->alias('a') ->join('dinner b','a.dinner_id = b.id') ->where('a.id',$dinnerId) ->where('b.dinner_type_id',$dinnerTypeId) ->where('b.type',1) ->where('b.enable',1) ->field('a.*,b.money as oldmoney') ->find(); if(!$info){ $this->error('选择的流食或半流食餐品不存在或已下架'); } if($info['money'] == 9 && $info['old_money'] > 0){ $info['money'] = $info['old_money']; } $order = Db::name('dinner_order')->where('id',$orderId)->find(); if(!$order){ $this->error('订单不存在'); } if($order['state'] != 1){ $this->error('订单该状态无法操作'); } if($order['user_id'] != $this->user['id']){ $this->error('无权限操作'); } // 取出要退款的餐品 $ids = Db::name('dinner_order_item') ->where('is_refuse',0) ->where('dinner_type_id',$dinnerTypeId) ->where('group_id',$groupId) ->where('day',$day) ->where('order_id',$orderId) ->column('id'); if(!$ids){ $this->error("参数错误或已取消"); } $tmoney = Db::name('dinner_order_item')->where('id','in',$ids)->sum('price'); $money = $order['money'] - $tmoney + $info['money']; $img = $order['img']; if(!$img){ if(!$_FILES['files']['tmp_name']){ $this->error('未上传医嘱证明'); } try{ $updir = config('upload_dir'); $file = request()->file('files'); $config = config('admin_upload_img'); $finfo = $file->validate($config) ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $updir . DIRECTORY_SEPARATOR . 'files'); if($finfo){ $img = '/'.$updir.'/files/' . $finfo->getSaveName(); $img = request()->domain(true).str_replace('\\', '/', $img); }else{ // 上传失败获取错误信息 \exception($file->getError()); } }catch (\Exception $e){ $this->error($e->getMessage()); } } Db::startTrans(); try{ $ret = Db::name('dinner_order')->where('id',$orderId)->update(['money'=>$money,'img'=>$img,'type'=>1,'update_time'=>date('Y-m-d H:i;s')]); if(!$ret){ \exception('操作失败'); } $ret = Db::name('dinner_order_item')->where('id','in',$ids)->update(['is_refuse'=>1,'refuse_time'=>date('Y-m-d H:i;s')]); if(!$ret){ \exception('操作失败'); } // 添加流食商品 $ret = Db::name('dinner_order_item')->insert([ 'dinner_id' => $info['dinner_id'], 'order_id' => $orderId, 'price' => $info['money'], 'original_price' => $info['money'], 'money' => $info['money'], 'num' => 1, 'dinner_type_id' => $dinnerTypeId, 'group_id' => $groupId, 'day' => $day ]); if(!$ret){ \exception('操作失败'); } Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error('操作失败'); } $this->redirect(url('order_detail',['id'=>$orderId])); }else{ $order = Db::name('dinner_order')->where('id',$orderId)->find(); if(!$order){ return $this->fetch('h5/msg_error',['msg'=>'订单不存在']); } $this->assign('order',$order); // 获取流食商品 $dinners = Db::name('dinner_group_item') ->alias('a') ->join('dinner b','a.dinner_id = b.id') ->where('a.day',$day) ->where('a.group_id',$groupId) ->where('b.dinner_type_id',$dinnerTypeId) ->where('b.type',1) ->field('a.id,b.name,b.money as oldmoney,a.money') ->select(); $dinners = $dinners?$dinners:[]; foreach ($dinners as $k=>$v){ if($v['money'] == 0 && $v['oldmoney'] > 0){ $dinners[$k]['money'] = $v['oldmoney']; } } $this->assign('dinners',$dinners); $group = Db::name('dinner_group')->where('id',$groupId)->value('title'); $dinnertype = Db::name('dinner_type')->where('id',$dinnerTypeId)->value('name'); $ids = Db::name('dinner_order_item') ->where('is_refuse',0) ->where('dinner_type_id',$dinnerTypeId) ->where('group_id',$groupId) ->where('day',$day) ->where('order_id',$orderId) ->column('dinner_id'); $names = []; if($ids){ $names = Db::name('dinner')->where('id','in',$ids)->column('name'); } $this->assign('group',$group?$group:''); $this->assign('dinnertype',$dinnertype?$dinnertype:''); $this->assign('day',$day); $this->assign('dinnernames',$names?implode(',',$names):''); $this->assign('orderId',$orderId); $this->assign('sign',$initsign); return $this->fetch(); } } public function refunddinner(){ $initsign = input('sign','','trim'); $sign = think_decrypt($initsign); if(!$sign){ if(request()->isPost()){ $this->error('参数错误'); }else{ return $this->fetch('h5/msg_error',['msg'=>'参数错误']); } } $signs = explode('|',$sign); if(count($signs) != 4){ if(request()->isPost()){ $this->error('参数错误'); }else{ return $this->fetch('h5/msg_error',['msg'=>'参数错误']); } } $curday = date('Y-m-d'); // $curday = date('Y-m-d',strtotime($curday) + 24*60*60); $orderId = $signs[0]; $dinnerTypeId = $signs[1]; $day = $signs[2]; $groupId = $signs[3]; if(request()->isPost()){ $day = input('day',0); if(!$day){ $this->error('请选择要退餐的日期'); } if($day <= $curday){ $this->error('已超过退餐时间,不能退餐'); } $data = Db::name('dinner_order') ->where('id',$orderId) ->find(); if (!in_array($data['state'],[1])) { $this->error('订单号不是已支付状态,不能执行退款'); } if ($data['is_replace'] !=0) { $this->error('不是您自己订的不能退款,退款请联系工作人员'); } if (!in_array($data['is_ok'],[0,2])) { $this->error('当前订单已标记完成'); } $items = Db::name('dinner_order_item')->where('order_id',$orderId)->where('day',$day)->select(); $ids = []; foreach ($items as $k=>$v){ if($v['is_refuse'] == 1){ $this->error('已有商品退餐,勿重复退餐'); } $ids[] = $v['id']; } Db::startTrans(); try{ $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array( 'dinner_order_id'=>$orderId ,'refuse_price'=>0 ,'refuse_y'=>date('Y') ,'refuse_ymd'=>date('Ymd') ,'refuse_ym'=>date('Ym') ,'created_user_id'=>$this->user['id'] ,'create_time'=>date('Y-m-d H:i:s') ,'refuse_w'=>date('Y').date('W') ,'order_type'=>$data['order_type'] ,'is_replace'=>$data['is_replace'] ,'org_id'=>$this->orgId ,'status'=>4 ,'refuse_remark'=>'' )); if(!$refuse_id){ \exception('退餐单添加失败'); } $refuse_item = []; foreach ($items as $k=>$v){ $refuse_item[] = [ 'dinner_id'=>$v['dinner_id'], 'order_refuse_id'=>$refuse_id, 'price'=>$v['price'], 'num'=>$v['num'], 'dinner_type_id'=>$v['dinner_type_id'], 'day' => $v['day'], 'group_id' => $v['group_id'] ]; } $rr = Db::name('dinner_order_refuse_item') ->insertAll($refuse_item); if(!$rr){ \exception('操作失败2'); } $rr = Db::name('dinner_order_item') ->where('id','in',$ids) ->update([ 'is_refuse'=>1 ]); if(!$rr){ \exception('操作失败1'); } // $res = Db::name('dinner_order') // ->where('id',$orderId) // ->update([ // 'state'=>4, // 'update_time'=>getTime() // ]); // // if(!$res){ // \exception('订单更新失败'); // } Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error('操作失败'.$e->getMessage()); } $this->redirect(url('order_detail',['id'=>$orderId])); }else{ $order = Db::name('dinner_order')->where('id',$orderId)->find(); if(!$order){ return $this->fetch('h5/msg_error',['msg'=>'订单不存在']); } $this->assign('order',$order); $days = Db::name('dinner_order_item') ->where('is_refuse',0) ->where('dinner_type_id',$dinnerTypeId) ->where('group_id',$groupId) ->where('order_id',$orderId) ->where('day','>',$curday) ->group('day') ->column('day'); $this->assign('day',$day); $this->assign('days',$days); // $group = Db::name('dinner_group')->where('id',$groupId)->value('title'); // $dinnertype = Db::name('dinner_type')->where('id',$dinnerTypeId)->value('name'); // // $ids = Db::name('dinner_order_item') // ->where('is_refuse',0) // ->where('dinner_type_id',$dinnerTypeId) // ->where('group_id',$groupId) // ->where('day',$day) // ->where('order_id',$orderId) // ->column('dinner_id'); // $names = []; // if($ids){ // $names = Db::name('dinner')->where('id','in',$ids)->column('name'); // } // $this->assign('group',$group?$group:''); // $this->assign('dinnertype',$dinnertype?$dinnertype:''); // $this->assign('day',$day); // $this->assign('dinnernames',$names?implode(',',$names):''); $this->assign('orderId',$orderId); $this->assign('sign',$initsign); return $this->fetch(); } } public function refuse(){ $orderid = input('id'); if (empty($orderid)) { return $this->fetch('h5/msg_error',['msg'=>'订单号不能为空']); } $data = Db::name('dinner_order') ->where('id',$orderid) ->find(); if (!in_array($data['state'],[1])) { return $this->fetch('h5/msg_error',['msg'=>'订单号不是已支付状态,不能执行退款']); } if ($data['is_replace'] !=0) { return $this->fetch('h5/msg_error',['msg'=>'不是您自己订的不能退款,退款请联系工作人员']); } if (!in_array($data['is_ok'],[0,2])) { return $this->fetch('h5/msg_error',['msg'=>'当前订单已标记完成']); } if($data['state'] == 1 ||$data['state'] == 3){ $day = date('Y-m-d'); $cday = date('Y-m-d',strtotime($data['create_time'])); if($day != $cday){ // 仅当天可退款 return $this->fetch('h5/msg_error',['msg'=>'订单仅当天能退款']); } } $all = Db::name('dinner_order_item') ->alias('a') ->join('dinner b','a.dinner_id=b.id') ->field('a.*,b.name as dinner_name,b.id as dinner_id,b.money,b.content,b.imgs') ->where('a.order_id',$orderid) ->where('a.is_refuse',0) ->select(); foreach ($all as $k=>$v){ $im = explode(',',$v['imgs']); $all[$k]['img'] = $im[0]; } $data['cartInfo'] = $all; if (request()->isGet()) { $this->assign('data',$data); $this->assign('id',$orderid); return $this->fetch(); } else { $d = request()->post(); if (empty($d)) { HelpHander::error('没有选择退单的商品'); } $post_d = array(); foreach ($d as $key => $value) { $arr = explode("_", $key); $post_d[] = $arr[1]; } $refuse_price = 0; $items = Db::name('dinner_order_item') ->where('id','in',$post_d) ->select(); foreach ($items as $k=>$v){ if($v['is_refuse']!=0){ HelpHander::error('当前选择物品有已退单物品,请刷新,重新选择'); } $refuse_price+=$v['price']; } try{ Db::startTrans(); if($data['is_spot']==0){//预定餐 $config3 = Db::name('dinner_config') ->where('org_id',$data['org_id']) ->find(); $zd = 0;//0自动退款1审核退款 if(empty($config3) || empty($config3['content3'])){ $zd =1; }else{ $cur = date('Y-m-d H:i'); $cc = date('Y-m-d H:i',strtotime(date('Y-m-d').' '.$config3['content3'])); if($cur >$cc){ $zd = 1; }else{ $zd = 0; } } if($zd == 0){ $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array( 'dinner_order_id'=>$orderid ,'refuse_price'=>$refuse_price ,'refuse_y'=>date('Y') ,'refuse_ymd'=>date('Ymd') ,'refuse_ym'=>date('Ym') ,'created_user_id'=>$this->user['id'] ,'create_time'=>date('Y-m-d H:i:s') ,'refuse_w'=>date('Y').date('W') ,'order_type'=>$data['order_type'] ,'is_replace'=>$data['is_replace'] ,'org_id'=>$this->orgId ,'status'=>5 ,'refuse_remark'=>'自动退款' )); if(!$refuse_id){ \exception('退款单添加失败'); } $refuse_item = []; foreach ($items as $k=>$v){ $refuse_item[] = [ 'dinner_id'=>$v['dinner_id'], 'order_refuse_id'=>$refuse_id, 'price'=>$v['price'], 'num'=>$v['num'], 'dinner_type_id'=>$v['dinner_type_id'], ]; } $rr = Db::name('dinner_order_refuse_item') ->insertAll($refuse_item); if(!$rr){ \exception('操作失败'); } $rr = Db::name('dinner_order_item') ->where('id','in',$post_d) ->update([ 'is_refuse'=>1 ]); if(!$rr){ \exception('操作失败'); } $checkAll = Db::name('dinner_order_item') ->where('order_id',$orderid) ->where('is_refuse',0) ->find(); if($checkAll){//部分退款 $res = Db::name('dinner_order') ->where('id',$orderid) ->update([ 'state'=>7, 'update_time'=>getTime() ]); }else{//全部退款 $res = Db::name('dinner_order') ->where('id',$orderid) ->update([ 'state'=>5, 'update_time'=>getTime() ]); } if(!$res){ \exception('订单更新失败'); } $config = get_pay_wechat($data['org_id']); $app = \EasyWeChat\Factory::payment($config); $ret = $app->refund->byOutTradeNumber($data['sn'], 'T' . $data['sn'], $data['price'] * 100, $refuse_price * 100); if ($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS') { trace(json_encode($ret)); \exception('微信退订失败:'.$ret['return_msg']); } }else{ $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array( 'dinner_order_id'=>$orderid ,'refuse_price'=>$refuse_price ,'refuse_y'=>date('Y') ,'refuse_ymd'=>date('Ymd') ,'refuse_ym'=>date('Ym') ,'created_user_id'=>$this->user['id'] ,'create_time'=>date('Y-m-d H:i:s') ,'refuse_w'=>date('Y').date('W') ,'order_type'=>$data['order_type'] ,'is_replace'=>$data['is_replace'] ,'org_id'=>$this->orgId ,'status'=>4 )); if(!$refuse_id){ \exception('退款单添加失败'); } $refuse_item = []; foreach ($items as $k=>$v){ $refuse_item[] = [ 'dinner_id'=>$v['dinner_id'], 'order_refuse_id'=>$refuse_id, 'price'=>$v['price'], 'num'=>$v['num'], 'dinner_type_id'=>$v['dinner_type_id'], ]; } $rr = Db::name('dinner_order_refuse_item') ->insertAll($refuse_item); if(!$rr){ \exception('操作失败'); } $rr = Db::name('dinner_order_item') ->where('id','in',$post_d) ->update([ 'is_refuse'=>1 ]); if(!$rr){ \exception('操作失败'); } $res = Db::name('dinner_order') ->where('id',$orderid) ->update([ 'state'=>4, 'update_time'=>getTime() ]); if(!$res){ \exception('订单更新失败'); } } }else{ $refuse_id = Db::name('dinner_order_refuse')->insertGetId(array( 'dinner_order_id'=>$orderid ,'refuse_price'=>$refuse_price ,'refuse_y'=>date('Y') ,'refuse_ymd'=>date('Ymd') ,'refuse_ym'=>date('Ym') ,'created_user_id'=>$this->user['id'] ,'create_time'=>date('Y-m-d H:i:s') ,'refuse_w'=>date('Y').date('W') ,'order_type'=>$data['order_type'] ,'is_replace'=>$data['is_replace'] ,'org_id'=>$this->orgId ,'status'=>4 )); if(!$refuse_id){ \exception('退款单添加失败'); } $refuse_item = []; foreach ($items as $k=>$v){ $refuse_item[] = [ 'dinner_id'=>$v['dinner_id'], 'order_refuse_id'=>$refuse_id, 'price'=>$v['price'], 'num'=>$v['num'], 'dinner_type_id'=>$v['dinner_type_id'], ]; } $rr = Db::name('dinner_order_refuse_item') ->insertAll($refuse_item); if(!$rr){ \exception('操作失败'); } $rr = Db::name('dinner_order_item') ->where('id','in',$post_d) ->update([ 'is_refuse'=>1 ]); if(!$rr){ \exception('操作失败'); } $res = Db::name('dinner_order') ->where('id',$orderid) ->update([ 'state'=>4, 'update_time'=>getTime() ]); if(!$res){ \exception('订单更新失败'); } } Db::commit(); HelpHander::success([],'操作成功'); }catch (Exception $e){ Db::rollback(); trace($e->getMessage()); HelpHander::error($e->getMessage()); } } } public function loginuser(){ if (request()->isGet()) { return $this->fetch(); } else { $tel = input('TEL'); $code = input('VCODE'); if(!$tel){ HelpHander::error('手机不能为空'); } $info = Db::name('dinner_user') ->where('org_id',$this->orgId) ->where('mobile',$tel) ->where('del',0) ->where('enable',1) ->find(); if(!$info){ HelpHander::error('点餐员不存在'); } if(!$code){ HelpHander::error('验证码不能为空'); } if(!verify_sms($tel,$code)){ HelpHander::error('验证码信息错误'); } $user = $this->user; $user['isdinder'] = 1; encodecookie('isdinder',1); // 清空购物车 Db::name('dinner_cart')->where('user_id')->delete(); HelpHander::success([],'操作成功'); } } public function sms(){ $phone = input('tel'); if(empty($phone)){ HelpHander::error('手机号不能为空'); } $res = send_verify_sms($phone); if(!$res){ HelpHander::error('发送失败'); } HelpHander::success([],'发送成功'); } }