12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462 |
- <?php
- namespace app\h5\controller;
- use app\hander\HelpHander;
- use EasyWeChat\Factory;
- use think\App;
- use think\Controller;
- use think\Db;
- use think\Exception;
- class WxBookDinner extends Controller
- {
- protected $user = [];
- protected $orgId = 0;
- protected $addressId = 0;
- public function __construct()
- {
- parent::__construct();
- $userId = dinner_is_login();
- // $userId = 205;
- if(empty($userId)){
- $this->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=预定餐
- encodecookie('is_spot',$is_spot);
- $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([],'发送成功');
- }
- }
|