123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460 |
- <?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=预定餐
- $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 = config("app.app_host").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 = config("app.app_host").$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 = config("app.app_host").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([],'发送成功');
- }
- }
|