StoreController.php 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\CancelOrderEvent;
  4. use App\Events\EnoughFive;
  5. use App\Events\FinishAccount;
  6. use App\Events\AuthRefundTip;
  7. use App\Events\HelpOrderEvent;
  8. use App\Events\NewOrder;
  9. use App\Events\ReceivingEvent;
  10. use App\Events\RetailOutboundEvent;
  11. use App\Events\RestoreOutboundEvent;
  12. use App\Events\TipSendingEvent;
  13. use App\Events\TipPickUpEvent;
  14. use App\Http\Controllers\MiniSubController as Mini;
  15. use App\Models\Admin;
  16. use App\Models\Amount;
  17. use App\Models\BindRecomLog;
  18. use App\Models\OrderExpressW;
  19. use App\Models\AgentNo;
  20. use App\Models\ShowNumW;
  21. use App\Models\Verify;
  22. use App\Models\Warea;
  23. use App\Models\Select;
  24. use App\Models\OrderCancelW;
  25. use App\Models\OrderCodeW;
  26. use App\Models\OrderDetail;
  27. use App\Models\OrderRefund;
  28. use App\Models\OrderRefundW;
  29. use App\Models\VipLog;
  30. use App\Models\VipLogW;
  31. use App\Models\SettingW;
  32. use Carbon\Carbon;
  33. use Overtrue\EasySms\EasySms;
  34. use App\Handlers\RefundAmount;
  35. use App\Handlers\ExpressHandler;
  36. use App\Handlers\TencentHandler;
  37. use App\Handlers\UploadImg;
  38. use App\Models\Comment;
  39. use App\Models\Order;
  40. use App\Models\StoreGoods;
  41. use App\Models\InventoryW;
  42. use App\Models\IntegralW;
  43. use App\Models\See;
  44. use App\Models\Scan;
  45. use App\Models\User;
  46. use App\Models\Store;
  47. use App\Models\UserW;
  48. use App\Models\UserZ;
  49. use App\Models\OrderW;
  50. use App\Models\OrderShareW;
  51. use App\Models\TestUserW;
  52. use App\Models\AddressW;
  53. use App\Models\ZbsBasic;
  54. use App\Models\ZbsEnroll;
  55. use App\Models\OrderDetailW;
  56. use App\Models\StoreGoodsSku;
  57. use Illuminate\Http\Request;
  58. use Intervention\Image\Facades\Image;
  59. use EasyWeChat\Factory;
  60. use GuzzleHttp\Client;
  61. use Illuminate\Support\Facades\Auth;
  62. use DB;
  63. use Storage;
  64. use Cache;
  65. use Log;
  66. use Validator;
  67. use TencentCloud\Common\Credential;
  68. use TencentCloud\Common\Profile\ClientProfile;
  69. use TencentCloud\Common\Profile\HttpProfile;
  70. use TencentCloud\Common\Exception\TencentCloudSDKException;
  71. use TencentCloud\Ocr\V20181119\OcrClient;
  72. use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
  73. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  74. use Qiniu\Auth as Auths;
  75. use Qiniu\Storage\UploadManager;
  76. use Qiniu\Storage\BucketManager;
  77. use App\Handlers\CompressImageHandler;
  78. class StoreController extends Controller{
  79. //获取店铺信息
  80. public function getStoreInfo()
  81. {
  82. $user_id=Auth::user()->id;
  83. $user=User::where('id',$user_id)->first();
  84. $data['is_delete'] = 0;
  85. if(!empty($user->deleted_at)){
  86. //获取账号余额
  87. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id', $user_id)->value('sub_mchid');
  88. $mini=new Mini();
  89. $result=$mini->getAccount($sub_mchid);
  90. Log::info('------------------------------------------------------------------------------');
  91. Log::info($result);
  92. if($result['pending_amount']+$result['available_amount']==0){
  93. Log::info($user->mobile.'///'.$user->deleted_at.'///'.$result['pending_amount'].'///'.$result['available_amount']);
  94. return $this->error('450005','该商户账号已被删除');
  95. }
  96. $data['is_delete'] = 1;
  97. $store=Store::withTrashed()->where('is_failure',0)->where('is_failure',0)->where('user_id',Auth::user()->id)->first();
  98. }else{
  99. //正式版
  100. if($user->status==1){
  101. return $this->error('450001','该用户已被禁用');
  102. }
  103. if($user->cert_status <> 3){
  104. if($user->cert_status <> 6){
  105. return $this->error('450001','该账号未完成账号审核');
  106. }
  107. }
  108. if($user->service_status <> 0){
  109. return $this->error('450001','该账号未完成经销商审核');
  110. }
  111. if($user->in_group!=1){
  112. return $this->error('450001','该用户未完成政委审核');
  113. }
  114. if($user->freeze_status==1){
  115. return $this->error('450001','该用户已被冻结');
  116. }
  117. // if($user->status <> 0 || $user->cert_status <> 6 || $user->service_status <> 0){// || !empty($user->deleted_at)
  118. // return $this->error('450002','你无权访问该页面');
  119. // }
  120. $store=Store::where('user_id',Auth::user()->id)->first();
  121. $store_del=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->first();
  122. if(empty($store) && !empty($store_del)){
  123. return $this->error('450001','店铺已被删除');
  124. }
  125. if(empty($store) && empty($store_del)){
  126. Store::create([
  127. 'img'=>$user->headimgurl,
  128. 'name'=>$user->nickname,
  129. 'user_id'=>$user->id,
  130. 'phone'=>$user->mobile
  131. ]);
  132. $store=Store::where('user_id',Auth::user()->id)->first();
  133. }
  134. if($store->status==1){
  135. return $this->error('450004','店铺已禁用');
  136. }
  137. }
  138. //测试版
  139. // $test_user=TestUserW::pluck('phone')->toArray();
  140. // if(!in_array($user->mobile,$test_user)){
  141. // return $this->error('450002','你无权访问该页面');
  142. // }
  143. // ->select('id','name','phone','img','star','province','city','area','address','qr_code',
  144. // 'is_test','status','is_apply','applyment_state','is_business','is_dianjin','is_adverting')
  145. $arr_test=Store::getTest();
  146. if($user->headimgurl != $store->img){
  147. Log::info($store->img);
  148. $store->img=$user->headimgurl;
  149. }
  150. if($user->mobile != $store->phone){
  151. $store->phone=$user->mobile;
  152. }
  153. if($user->nickname != $store->name){
  154. $store->name=$user->nickname;
  155. }
  156. if($user->warea_id != $store->warea_id){
  157. $store->warea_id=$user->warea_id;
  158. $store->warea_name=Warea::where('id',$user->warea_id)->value('name');
  159. }
  160. if($user->level != $store->level){
  161. $store->level=$user->level;
  162. }
  163. $store->save();
  164. // $star=Comment::where('store_id',$store->id)->average('star');
  165. // $store->star=floor($star);
  166. if(in_array($store->id,$arr_test)){
  167. $store->is_test=1;
  168. }else{
  169. $store->is_test=0;
  170. }
  171. //测试跳转小程序
  172. if(in_array($user->mobile,['15138929587','15238617326','13030314383','18838980968'])){
  173. $user->mini_zbs=1;
  174. }else{
  175. $user->mini_zbs=0;
  176. }
  177. $no_pay=OrderW::where('store_id',$store->id)->where('is_pay',0)->where('apply_cancel',[0,1,3])->count();
  178. $no_refund=OrderRefundW::where('store_id',$store->id)->where('status',0)->count();
  179. $no_cancel=OrderCancelW::where('store_id',$store->id)->where('status',1)->count();
  180. $see_num=See::where('store_id',$store->id)->whereDate('created_at',date('Y-m-d'))->count();
  181. $order_num=OrderW::where('store_id',$store->id)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1])
  182. ->where('is_pay',1)->whereDate('created_at',date('Y-m-d'))->count();
  183. $order_money=OrderW::where('store_id',$store->id)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1])
  184. ->where('is_pay',1)->whereDate('created_at',date('Y-m-d'))->sum('account');
  185. $wait_send_num=OrderW::where('store_id',$store->id)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1,2])
  186. ->where('is_pay',1)->where('status',1)->count();
  187. $show_num=ShowNumW::where('obj_id',$store->id)->where('obj_type',1)->where('type',1)->first();
  188. $data['show_num1']=0;
  189. if($show_num){
  190. if($show_num->num < 3){
  191. $data['show_num1']=1;
  192. $show_num->num += 1;
  193. $show_num->save();
  194. }
  195. }else{
  196. ShowNumW::create([
  197. 'obj_id'=>$store->id,
  198. 'obj_type'=>1,
  199. 'type'=>1,
  200. 'name'=>'首页操作视频',
  201. 'num'=>1,
  202. ]);
  203. }
  204. $order_money=round($order_money);
  205. // $data['user']=$user;
  206. $data['store']=$store;
  207. $data['wait_send_num']=$wait_send_num?$wait_send_num:0;
  208. $data['order_num']=$order_num?$order_num:0;
  209. $data['see_num']=$see_num?$see_num:0;
  210. $data['order_money']=$order_money?$order_money:'0';
  211. $data['no_pay']=$no_pay?$no_pay:'0';
  212. $data['no_auth']=$no_refund+$no_cancel;
  213. $home_operation_video=SettingW::where('key','home.operation.video')->value('value');
  214. $data['home_operation_video']=$home_operation_video;
  215. return $this->success($data);
  216. }
  217. //微店首页争霸赛元宝弹窗
  218. public function getZbsRank()
  219. {
  220. $start_time=ZbsBasic::where('keys','round_start_time')->value('value');
  221. $end_time=ZbsBasic::where('keys','round_end_time')->value('value');
  222. $season=ZbsBasic::where('keys','season')->value('value');
  223. $store = Store::where('user_id',Auth::user()->id)->first();
  224. // $user = UserZ::where('phone', $store->phone)->first();
  225. $user = UserZ::where('dwbs_id', $store->user_id)->first();
  226. if(empty($user)){
  227. Log::info('争霸赛不存在该用户'.$store->phone);
  228. $zbs['type']=2;
  229. $zbs['message']='争霸赛不存在该用户';
  230. return $this->success($zbs);
  231. }
  232. $now=Carbon::now()->timestamp;
  233. if($now>$start_time && $now<$end_time) { //在争霸赛期间
  234. // $uids = ZbsEnroll::where('season',$season)->where('is_refund',0)->where('status',0)->pluck('uid')->toArray();
  235. $uids = \Illuminate\Support\Facades\Cache::remember('uids',Carbon::now()->addHours(12),function()use($season){
  236. return ZbsEnroll::where('season',$season)->where('is_refund',0)->where('status',0)->pluck('uid')->toArray();
  237. });
  238. if(!in_array($user->id,$uids)){
  239. $zbs['type']=2;
  240. $zbs['message']='没有报名当前这届争霸赛';
  241. return $this->success($zbs);
  242. }
  243. $start_time=date("Y-m-d H:i:s",$start_time);
  244. $end_time=date("Y-m-d H:i:s",$end_time);
  245. $total = OrderW::where('store_id',$store->id)->whereBetween('created_at', [$start_time,$end_time])
  246. ->where('is_help',0)->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1,2])
  247. ->where('zbs_yb',0)->sum('total');
  248. if($total<=0){
  249. $zbs['type']=2;
  250. $zbs['message']='没有积分变化';
  251. return $this->success($zbs);
  252. }
  253. $score = $total * 10;
  254. $integrals = UserZ::where('integral', '>=', $user->integral)
  255. ->whereIn('id',$uids)
  256. ->orderByDesc('integral','id')->pluck('integral','id')->toArray();
  257. arsort($integrals);
  258. $keys0=array_keys($integrals);
  259. $rank0 = array_search($user->id, $keys0);
  260. $inte = $user->integral + $score;
  261. $integrals[$user->id] = $inte;
  262. arsort($integrals);
  263. $keys1=array_keys($integrals);
  264. $rank = array_search($user->id, $keys1);
  265. $ranking = $rank0 - $rank;
  266. if($ranking<=0){
  267. $ranking=0;
  268. }
  269. $zbs['type']=1;
  270. $zbs['message']='在争霸赛期间';
  271. $zbs['score'] = $score;
  272. $zbs['ranking']=$ranking;
  273. // $zbs['integrals'] = $integrals;
  274. OrderW::where('store_id',$store->id)->whereBetween('created_at', [$start_time,$end_time])
  275. ->where('is_help',0)->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1,2])
  276. ->where('zbs_yb',0)->update(['zbs_yb'=>1]);
  277. return $this->success($zbs);
  278. }else{
  279. //不在争霸赛期间
  280. $zbs['type']=2;
  281. $zbs['message']='不在争霸赛期间';
  282. return $this->success($zbs);
  283. }
  284. }
  285. public function getStoreData(){
  286. $user_id=Auth::user()->id;
  287. $user=User::where('id',$user_id)->first();
  288. $store=Store::where('user_id',Auth::user()->id)->first();
  289. if($user->headimgurl != $store->img){
  290. Log::info($store->img);
  291. $store->img=$user->headimgurl;
  292. $store->save();
  293. }
  294. if($user->mobile != $store->phone){
  295. $store->phone=$user->mobile;
  296. $store->save();
  297. }
  298. if($user->nickname != $store->name){
  299. $store->name=$user->nickname;
  300. $store->save();
  301. }
  302. // $star=Comment::where('store_id',$store->id)->average('star');
  303. // $store->star=floor($star);
  304. $no_pay=OrderW::where('store_id',$store->id)->where('is_pay',0)->where('apply_cancel',[0,1,3])->count();
  305. $no_refund=OrderRefundW::where('store_id',$store->id)->where('status',0)->count();
  306. $no_cancel=OrderCancelW::where('store_id',$store->id)->where('status',1)->count();
  307. $see_num=See::where('store_id',$store->id)->whereDate('created_at',date('Y-m-d'))->count();
  308. $order_num=OrderW::where('store_id',$store->id)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1])
  309. ->where('is_pay',1)->whereDate('created_at',date('Y-m-d'))->count();
  310. $order_money=OrderW::where('store_id',$store->id)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1])
  311. ->where('is_pay',1)->whereDate('created_at',date('Y-m-d'))->sum('account');
  312. $wait_send_num=OrderW::where('store_id',$store->id)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1,2])
  313. ->where('is_pay',1)->where('status',1)->count();
  314. $order_money=round($order_money);
  315. $data['user']=$user;
  316. $data['store']=$store;
  317. $data['wait_send_num']=$wait_send_num?$wait_send_num:0;
  318. $data['order_num']=$order_num?$order_num:0;
  319. $data['see_num']=$see_num?$see_num:0;
  320. $data['order_money']=$order_money?$order_money:'0';
  321. $data['no_pay']=$no_pay?$no_pay:'0';
  322. $data['no_auth']=$no_refund+$no_cancel;
  323. return $this->success($data);
  324. }
  325. //获取商户个人信息
  326. public function getAgentInfo(){
  327. $store = Store::where('user_id',Auth::user()->id)->first();
  328. if(empty($store->idCard)){
  329. return $this->error('450001','店铺未认证');
  330. }
  331. $cre_num=$store->idCard;
  332. $year=substr($cre_num,6,4);
  333. if($year>1920 && $year<2020) {
  334. $year .= '-01-01';
  335. $age = Carbon::parse($year)->diffInYears();
  336. }else{
  337. $age = 0;
  338. }
  339. $num=substr($cre_num,16,1);
  340. if($num%2==0){
  341. $sex = '女';
  342. }else{
  343. $sex = '男';
  344. }
  345. $name=$store->username;
  346. $phone=$store->phone;
  347. switch(Auth::user()->level){
  348. case 1:
  349. $level='销售员';
  350. break;
  351. case 2:
  352. $level='经销商';
  353. break;
  354. default:
  355. $level='批发商';
  356. }
  357. $data=['username'=>$name,'sex'=>$sex,'age'=>$age,'phone'=>$phone,'level'=>$level,'cre_num'=>$cre_num];
  358. return $this->success($data);
  359. }
  360. //同意门店协议
  361. // public function setAgree()
  362. // {
  363. // $store=Store::where('user_id',Auth::user()->id)->first();
  364. // $store->is_agree=1;
  365. // $res=$store->save();
  366. // if($res){
  367. // return $this->success([]);
  368. // }else{
  369. // return $this->error();
  370. // }
  371. // }
  372. //修改店铺信息
  373. public function editStoreInfo(Request $request)
  374. {
  375. $input=$request->all();
  376. $rules=[
  377. 'name'=>'required|max:32'
  378. ];
  379. $message=[
  380. 'name.required'=>'用户名称不能为空.',
  381. 'name.max'=>'用户名称不能大于32个字符.'
  382. ];
  383. $validator = Validator::make($input,$rules,$message);
  384. if($validator->fails()){
  385. return $this->error('400013',$validator->errors()->first());
  386. }
  387. $store_id=$input['store_id'];
  388. $name=$input['name'];
  389. $res=Store::where('id',$store_id)->update(['name'=>$name]);
  390. if($res){
  391. return $this->success([]);
  392. }else{
  393. return $this->error();
  394. }
  395. }
  396. //修改手机号
  397. public function editSms(Request $request)
  398. {
  399. $rules=[
  400. 'phone'=>'required|regex:/^1[3456789][0-9]{9}$/',
  401. 'phone_code'=>'required|size:6',
  402. ];
  403. $message=[
  404. 'phone.required'=>'手机号不能为空.',
  405. 'phone.regex'=>'手机号格式有误.',
  406. 'phone_code.required'=>'验证码不能为空.',
  407. 'phone_code.size'=>'验证码为6位字符串.',
  408. ];
  409. $validator = Validator::make($request->input(),$rules,$message);
  410. if($validator->fails()){
  411. return $this->error('400013',$validator->errors()->first());
  412. }
  413. $phone=$request->input('phone');
  414. if($phone == Store::where('user_id',Auth::user()->id)->value('phone')){
  415. return $this->error('450001','新手机号不能与旧手机号相同');
  416. }
  417. if(Store::where('phone',$phone)->first()){
  418. return $this->error('450001','新手机号已绑定店铺');
  419. }
  420. $store_id=$request->input('store_id');
  421. $phone_code=$request->input('phone_code');
  422. $code=Cache::get('sms'.$phone);
  423. if($code==$phone_code){
  424. Cache::put('sms'.$phone,$code,-1);
  425. $res=Store::where('id',$store_id)->update(['phone'=>$phone]);
  426. if($res){
  427. return $this->success([]);
  428. }
  429. }else{
  430. return $this->error('450001','验证码有误');
  431. }
  432. return $this->error();
  433. }
  434. //校验手机验证码
  435. public function checkSms(Request $request)
  436. {
  437. $rules=[
  438. 'phone'=>'required|regex:/^1[3456789][0-9]{9}$/',
  439. 'phone_code'=>'required|size:6',
  440. ];
  441. $message=[
  442. 'phone.required'=>'手机号不能为空.',
  443. 'phone.regex'=>'手机号格式有误.',
  444. 'phone_code.required'=>'验证码不能为空.',
  445. 'phone_code.size'=>'验证码为6位字符串.',
  446. ];
  447. $validator = Validator::make($request->input(),$rules,$message);
  448. if($validator->fails()){
  449. return $this->error('400013',$validator->errors()->first());
  450. }
  451. $phone=$request->input('phone');
  452. $phone_code=$request->input('phone_code');
  453. $code=Cache::get('sms'.$phone);
  454. if($code==$phone_code){
  455. Cache::put('sms'.$phone,$code,-1);
  456. return $this->success([]);
  457. }else{
  458. return $this->error('450001','验证码有误');
  459. }
  460. }
  461. //发送验证码
  462. public function sendSms(Request $request){
  463. $rules=[
  464. 'phone'=>'required|regex:/^1[3456789][0-9]{9}$/'
  465. ];
  466. $message=[
  467. 'phone.required'=>'手机号不能为空.',
  468. 'phone.regex'=>'手机号格式有误.'
  469. ];
  470. $validator = Validator::make($request->input(),$rules,$message);
  471. if($validator->fails()){
  472. return $this->error('400013',$validator->errors()->first());
  473. }
  474. $phone=$request->input('phone');
  475. $config=config('easysms');
  476. $easySms = new EasySms($config);
  477. $code = mt_rand(100000,999999);
  478. $minute=5;
  479. if($request->input('type')=='card'){
  480. $content="【大卫博士】您正在绑定身份信息,{$code}为您的验证码,请于{$minute}分钟内填写。如非本人操作,请忽略本短信。 ";
  481. }else{
  482. $content="【大卫博士】您正在换绑手机号,{$code}为您的更换验证码,请于{$minute}分钟内填写。如非本人操作,请忽略本短信。";
  483. }
  484. Cache::put('sms'.$phone,$code,$minute*60);
  485. try{
  486. $easySms->send($phone, ['content' =>$content ]);
  487. } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
  488. $message = $exception->getException('qcloud')->getMessage();
  489. return $this->error(40013, $message == null ? '短信发送异常' : $message);
  490. }
  491. return $this->success([]);
  492. }
  493. //分享店铺信息
  494. public function shareStore()
  495. {
  496. $store=Store::where('user_id',Auth::user()->id)->first();
  497. $store->user=Auth::user();
  498. return $this->success($store);
  499. }
  500. //修改店铺图片
  501. public function uploadStoreImg(Request $request)
  502. {
  503. $path_url = 'public/store';
  504. $path = $request->file('img')->store($path_url);
  505. if($path){
  506. $store_id=$request->input('store_id');
  507. $url = env('APP_URL').Storage::url($path);
  508. $res=Store::where('id',$store_id)->update(['img'=>$url]);
  509. if ($res) {
  510. return $this->success($url);
  511. }
  512. }
  513. return $this->error();
  514. }
  515. //上传证件照片
  516. public function uploadCertImg(Request $request)
  517. {
  518. $paths='cert/'.date("Ymd").'/';
  519. $path_url = $paths.date("His").mt_rand(10000,99999).'.jpg';
  520. if(!is_dir($paths)){
  521. mkdir($paths,0755,true);
  522. }
  523. Log::info($path_url);
  524. $type=$request->input('type');//1人像面 2国徽面
  525. $file = $request->file('img');
  526. Log::info($file);
  527. $img=Image::make($file);
  528. Log::info(json_encode($img));
  529. // \Illuminate\Support\Facades\Log::error(1);
  530. if($img->filesize() > 1024*1024){
  531. $img->resize(800, null, function ($constraint) {
  532. $constraint->aspectRatio();
  533. })->save($path_url);
  534. }else{
  535. $img->save($path_url);
  536. }
  537. $path='/'.$img->basePath();
  538. if($path){
  539. $url = env('APP_URL').$path;
  540. Log::info('%%%%%%-----%%%%%%'.$url.'--'.$path.'---'.env('APP_URL'));
  541. }else{
  542. return $this->error('450001','证件上传失败');
  543. }
  544. try{
  545. // $certInfo=json_decode($this->getCertInfo($url),true);
  546. $tenCent = new TencentHandler();
  547. $certInfo=json_decode($tenCent->getCertInfo($url),true);
  548. }catch(\Exception $e){
  549. return $this->error('450001',$e->getMessage());
  550. }
  551. if(empty($certInfo)){
  552. return $this->error('450001','上传图片有误');
  553. }
  554. $AdvancedInfo=json_decode($certInfo['AdvancedInfo'],true);
  555. Log::info(Auth::user()->nickname.'身份证图片质量指数:'.$AdvancedInfo['Quality']);
  556. if($AdvancedInfo['Quality']<=55){
  557. return $this->error('450001','身份证图片模糊');
  558. }
  559. foreach($tenCent->AdvancedInfo() as $key=>$val){
  560. if(in_array($key,$AdvancedInfo['WarnInfos']))
  561. return $this->error('450001',$val);
  562. }
  563. if($type==1){
  564. if(empty($certInfo['Name'])){
  565. return $this->error('450001','请上传人像面图片');
  566. }else{
  567. Store::where('user_id',Auth::user()->id)->update(['cardImg1'=>$url]);
  568. }
  569. }elseif($type==2){
  570. if(empty($certInfo['Authority'])){
  571. return $this->error('450001','请上传国徽面图片');
  572. }else{
  573. Store::where('user_id',Auth::user()->id)->update(['cardImg2'=>$url]);
  574. }
  575. }else{
  576. return $this->error('450001','参数有误');
  577. }
  578. $certInfo['url']=$path;
  579. return $this->success($certInfo);
  580. }
  581. //上传证件照片--》七牛云
  582. public function uploadCertImgQiniuyun(Request $request)
  583. {
  584. $type=$request->input('type');//1人像面 2国徽面
  585. if (!$request->hasFile('img')) {
  586. return $this->error('450001','请上传文件');
  587. }
  588. //设置文件后缀白名单
  589. $allowExt = ["jpg","png","jpeg"];
  590. //获取文件
  591. $file = $request->file('img');
  592. // 获取七牛云配置信息
  593. $config = [
  594. 'driver' => 'qiniu',
  595. 'domain' => 'http://image.chuliu.cc', //七牛域名
  596. 'access_key' => 'cIbo9JAqHBgO6uw97gT5fJOlEHiTiGrpB84unVEn', //AccessKey
  597. 'secret_key' => '4f0nTNNrvzOsFc6Rz0GC5DntiJRnk2GcfkyJFNQp', //SecretKey
  598. 'bucket' => 'daweiboshi-app', //Bucket名字
  599. 'dirname'=> 'daweiboshi-app'
  600. ];
  601. // 构建鉴权对象
  602. $auth = new Auths($config['access_key'], $config['secret_key']);
  603. // 生成上传 Token
  604. $token = $auth->uploadToken($config['bucket']);
  605. //获取文件的绝对路径,但是获取到的在本地不能打开
  606. $filePath = $file->getRealPath();
  607. //获取文件的扩展名
  608. $ext = $file->getClientOriginalExtension();
  609. //获取上传文件的名称
  610. $ext_name = $file->getClientOriginalName();
  611. Log::info('上传文件的名称->'.$ext_name);
  612. //判断是否是Excel
  613. if (empty($ext) || !in_array(strtolower($ext), $allowExt)) {
  614. return $this->error('450001','不允许的文件类型');
  615. }
  616. // 新文件名
  617. $filename =date('His') . uniqid(mt_rand(10000,99999)) .'.'. $ext;
  618. $dirname=$config['dirname'];
  619. // 初始化 UploadManager 对象并进行文件的上传。
  620. $uploadMgr = new UploadManager();
  621. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  622. $pathname= $dirname.'/'.date('Ymd').'/'.$filename;
  623. try{
  624. $uploadMgr->putFile($token, $pathname, $filePath);
  625. }catch(\Exception $e){
  626. return $this->error('450001',$e->getMessage(),$e->getMessage());
  627. }
  628. // 获取文件完整路径
  629. $url = $config['domain'].'/'.$pathname;
  630. Log::info($url);
  631. try{
  632. $tenCent = new TencentHandler();
  633. $certInfo=json_decode($tenCent->getCertInfo($url.'?imageView2/0/w/600/q/100'),true);
  634. }catch(\Exception $e){
  635. Log::info('图片识别错误111111');
  636. Log::info($e->getMessage());
  637. return $this->error('450001',$e->getMessage(),$e->getMessage());
  638. }
  639. if(empty($certInfo)){
  640. return $this->error('450001','上传图片有误','上传图片有误');
  641. }
  642. $AdvancedInfo=json_decode($certInfo['AdvancedInfo'],true);
  643. Log::info(Auth::user()->nickname.'身份证图片质量指数:'.$AdvancedInfo['Quality']);
  644. if($AdvancedInfo['Quality']<=55){
  645. return $this->error('450001','身份证图片模糊','身份证图片模糊');
  646. }
  647. foreach($tenCent->AdvancedInfo() as $key=>$val){
  648. if(in_array($key,$AdvancedInfo['WarnInfos'])){
  649. Log::info('111111错误111111--'.$val);
  650. return $this->error('450001',$val,$val);
  651. }
  652. }
  653. if($type==1){
  654. if(empty($certInfo['Name'])){
  655. return $this->error('450001','请上传人像面图片','请上传人像面图片');
  656. }else{
  657. Store::where('user_id',Auth::user()->id)->update(['cardImg1'=>$url]);
  658. }
  659. }elseif($type==2){
  660. if(empty($certInfo['Authority'])){
  661. return $this->error('450001','请上传国徽面图片','请上传国徽面图片');
  662. }else{
  663. Store::where('user_id',Auth::user()->id)->update(['cardImg2'=>$url]);
  664. }
  665. }else{
  666. return $this->error('450001','参数有误','参数有误');
  667. }
  668. $certInfo['url']='/'.$pathname;
  669. return $this->success($certInfo);
  670. }
  671. //上传证件照片--》压缩 七牛云
  672. public function uploadCertImgQny(Request $request)
  673. {
  674. $type= $request->input('type'); //1人像面 2国徽面
  675. $file= $request->file('img');
  676. if (!$request->hasFile('img')) {
  677. return $this->error('450001','请上传文件');
  678. }
  679. //设置文件后缀白名单
  680. $allowExt = ["jpg","png","jpeg"];
  681. //获取文件后缀名
  682. $ext = $file->getClientOriginalExtension();
  683. //判断文件格式是否正确
  684. if (empty($ext) || !in_array(strtolower($ext), $allowExt)) {
  685. return $this->error('450001','不允许上传的文件类型');
  686. }
  687. // 获取七牛云配置信息
  688. $config = [
  689. 'driver' => 'qiniu',
  690. 'domain' => 'http://image.chuliu.cc', //七牛域名
  691. 'access_key' => 'cIbo9JAqHBgO6uw97gT5fJOlEHiTiGrpB84unVEn', //AccessKey
  692. 'secret_key' => '4f0nTNNrvzOsFc6Rz0GC5DntiJRnk2GcfkyJFNQp', //SecretKey
  693. 'bucket' => 'daweiboshi-app', //Bucket名字
  694. 'dirname'=> 'daweiboshi-app'
  695. ];
  696. // 构建鉴权对象
  697. $auth = new Auths($config['access_key'], $config['secret_key']);
  698. // 生成上传 Token
  699. $token = $auth->uploadToken($config['bucket']);
  700. // 初始化 UploadManager 对象并进行文件的上传。
  701. $uploadMgr = new UploadManager();
  702. // 文件存储路径key
  703. $pathname= $config['dirname'].'/'.date('Ymd').'/'.date('His') . uniqid(mt_rand(10000,99999)) .'.'. $ext;
  704. Log::info('文件大小:'.$_FILES['img']['size']);
  705. if($_FILES['img']['size'] > 1024*1024){
  706. $handler= new CompressImageHandler();
  707. $contents= $handler->Upload();
  708. if(!$contents){
  709. return $this->error('450001','图片压缩失败');
  710. }
  711. // 调用 UploadManager 的 put 方法进行文件的上传。
  712. try{
  713. $uploadMgr->put($token, $pathname, $contents);
  714. }catch(\Exception $e){
  715. return $this->error('450001',$e->getMessage(),$e->getMessage());
  716. }
  717. }else{
  718. //获取文件的绝对路径,但是获取到的在本地不能打开
  719. $filePath = $file->getRealPath();
  720. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  721. try{
  722. $uploadMgr->putFile($token, $pathname, $filePath);
  723. }catch(\Exception $e){
  724. return $this->error('450001',$e->getMessage(),$e->getMessage());
  725. }
  726. }
  727. // 获取文件完整路径
  728. $url = $config['domain'].'/'.$pathname;
  729. Log::info($url);
  730. try{
  731. $tenCent = new TencentHandler();
  732. $certInfo=json_decode($tenCent->getCertInfo($url),true);
  733. }catch(\Exception $e){
  734. Log::info('图片识别错误111');
  735. Log::info($e->getMessage());
  736. return $this->error('450001',$e->getMessage(),$e->getMessage());
  737. }
  738. if(empty($certInfo)){
  739. return $this->error('450001','上传图片有误','上传图片有误');
  740. }
  741. $AdvancedInfo=json_decode($certInfo['AdvancedInfo'],true);
  742. Log::info(Auth::user()->nickname.'身份证图片质量指数:'.$AdvancedInfo['Quality']);
  743. if($AdvancedInfo['Quality']<=55){
  744. return $this->error('450001','身份证图片模糊','身份证图片模糊');
  745. }
  746. foreach($tenCent->AdvancedInfo() as $key=>$val){
  747. if(in_array($key,$AdvancedInfo['WarnInfos'])){
  748. Log::info('111错误111--'.$val);
  749. return $this->error('450001',$val,$val);
  750. }
  751. }
  752. if($type==1){
  753. if(empty($certInfo['Name'])){
  754. return $this->error('450001','请上传人像面图片','请上传人像面图片');
  755. }else{
  756. Store::where('user_id',Auth::user()->id)->update(['cardImg1'=>$url]);
  757. }
  758. }elseif($type==2){
  759. if(empty($certInfo['Authority'])){
  760. return $this->error('450001','请上传国徽面图片','请上传国徽面图片');
  761. }else{
  762. Store::where('user_id',Auth::user()->id)->update(['cardImg2'=>$url]);
  763. }
  764. }else{
  765. return $this->error('450001','参数有误','参数有误');
  766. }
  767. $certInfo['url']='/'.$pathname;
  768. return $this->success($certInfo);
  769. }
  770. //上传活体人脸核身视频
  771. public function uploadFaceImg(Request $request){
  772. $path_url = 'public/cert/video/'.date('ymd');
  773. $path = $request->file('video')->store($path_url);
  774. if($path){
  775. $url = env('APP_URL').Storage::url($path);
  776. }else{
  777. return $this->error('450001','视频上传失败');
  778. }
  779. try{
  780. $tenCent = new TencentHandler();
  781. $certInfo=json_decode($tenCent->checkFaceId($idCard,$name,$url),true);
  782. }catch(\Exception $e){
  783. return $this->error('450001',$e->getMessage());
  784. }
  785. if(empty($certInfo) || $certInfo['Sim'] < 70){
  786. return $this->error('450001','认证失败');
  787. }
  788. if($certInfo['Result']=='Success' && $certInfo['Sim'] >= 70){
  789. return $this->success([]);
  790. }
  791. }
  792. //获取证件信息
  793. // public function getCertInfo($img)
  794. // {
  795. // $cred = new Credential(env('TENCENT_SECRETID'), env('TENCENT_SECRETKEY'));
  796. // $httpProfile = new HttpProfile();
  797. // $httpProfile->setEndpoint("ocr.tencentcloudapi.com");
  798. //
  799. // $clientProfile = new ClientProfile();
  800. // $clientProfile->setHttpProfile($httpProfile);
  801. // $client = new OcrClient($cred, "ap-beijing", $clientProfile);
  802. // $req = new IDCardOCRRequest();
  803. // $params = [
  804. // 'ImageUrl'=>$img,
  805. // 'Config' => json_encode(["CopyWarn"=>true,"BorderCheckWarn"=>true,"ReshootWarn"=>true,"DetectPsWarn"=>true,"TempIdWarn"=>true,"InvalidDateWarn"=>true,"Quality"=>true,"ReflectWarn"=>true])
  806. // ];//"CropIdCard"=>true,"CropPortrait"=>true,
  807. // $req->fromJsonString(json_encode($params));
  808. // $resp = $client->IDCardOCR($req);
  809. // return $resp->toJsonString();
  810. // }
  811. //活体人脸核身
  812. // public function checkFaceId($idCard,$name,$base)
  813. // {
  814. // $cred = new Credential(env('TENCENT_SECRETID'), env('TENCENT_SECRETKEY'));
  815. // $httpProfile = new HttpProfile();
  816. // $httpProfile->setEndpoint("faceid.tencentcloudapi.com");
  817. //
  818. // $clientProfile = new ClientProfile();
  819. // $clientProfile->setHttpProfile($httpProfile);
  820. // $client = new OcrClient($cred, "ap-beijing", $clientProfile);
  821. // $req = new IDCardOCRRequest();
  822. // $params = [
  823. // 'IdCard'=>$idCard,
  824. // 'Name'=>$name,
  825. // 'VideoBase64'=>$base,
  826. // 'LivenessType'=>'SILENT',//LIP为数字模式,ACTION为动作模式,SILENT为静默模式
  827. // ];
  828. // $req->fromJsonString(json_encode($params));
  829. // $resp = $client->IDCardOCR($req);
  830. // return $resp->toJsonString();
  831. // }
  832. //门店认证
  833. // public function certStore(Request $request)
  834. // {
  835. // $input=$request->all();
  836. // $rules=[
  837. // 'name'=>'required|max:32',
  838. // 'idCard'=>'required|size:18',
  839. // 'phone'=>'required|regex:/^1[3456789][0-9]{9}$/',
  840. // 'phone_code'=>'required|size:6',
  841. // ];
  842. // $message=[
  843. // 'name.required'=>'用户名称不能为空',
  844. // 'name.max'=>'用户名称不能大于32个字符',
  845. // 'idCard.required'=>'证件号不能为空',
  846. // 'idCard.size'=>'证件号为18个字符',
  847. // 'phone.required'=>'手机号不能为空.',
  848. // 'phone.regex'=>'手机号格式有误.',
  849. // 'phone_code.required'=>'验证码不能为空.',
  850. // 'phone_code.size'=>'验证码为6位字符串.',
  851. // ];
  852. // $validator = Validator::make($input,$rules,$message);
  853. // if($validator->fails()){
  854. // return $this->error('400013',$validator->errors()->first());
  855. // }
  856. // $store_id=$input['store_id'];
  857. // $idCard=$input['idCard'];
  858. // $name=$input['name'];
  859. // $phone=$input['phone'];
  860. // $phone_code=$input['phone_code'];
  861. // $card_name=$input['card_name'];
  862. // $card_no=$input['card_no'];
  863. // $code=Cache::get('sms'.$phone);
  864. // if($code!=$phone_code){
  865. // return $this->error('450001','验证码有误');
  866. // }
  867. // Cache::put('sms'.$phone,$code,-1);
  868. // if($name != $card_name || $idCard!=$card_no){
  869. // return $this->error('450001','证件信息与输入个人信息不一致');
  870. // }
  871. // if(Store::where('idCard',$card_no)->first()){
  872. // return $this->error('450001','证件信息已被绑定');
  873. // }
  874. // $store=Store::where('id',$store_id)->first();
  875. // if($store){
  876. // $store->idCard=$idCard;
  877. // $store->username=$name;
  878. // $store->is_cert=1;
  879. // }else{
  880. // return $this->error('450001','店铺信息有误');
  881. // }
  882. // try{
  883. // DB::Transaction(function()use($store,$idCard){
  884. // $user=User::where('id',$store->user_id)->first();
  885. // if($user){
  886. // $user->cre_num=$idCard;
  887. // $user->save();
  888. // }
  889. //
  890. // $store->save();
  891. //
  892. // },5);
  893. // return $this->success([]);
  894. // }catch(\Exception $e){
  895. // return $this->error($e->getMessage());
  896. // }
  897. // }
  898. //添加新用户
  899. public function addUser(Request $request){
  900. $area_code=$request->input('code','+86');
  901. $regex=Verify::where('code',$area_code)->value('preg');
  902. $rules=[
  903. 'phone'=>['required','regex:'.$regex]
  904. ];
  905. $message=[
  906. 'phone.required'=>'手机号不能为空.',
  907. 'phone.regex'=>'手机号格式有误.'
  908. ];
  909. $validator = Validator::make($request->input(),$rules,$message);
  910. if($validator->fails()){
  911. return $this->error('400013',$validator->errors()->first());
  912. }
  913. $agent_id = Auth::user()->id;
  914. $phone=$request->input('phone');
  915. $nickname=$request->input('nickname');
  916. $is_vip=$request->input('is_vip');
  917. $store = Store::where('user_id',$agent_id)->first();
  918. $agent=User::where('mobile',$phone)
  919. ->where(function($query){
  920. $query->where('cert_status',3)
  921. ->orWhere('cert_status',6);
  922. })
  923. ->where('service_status',0)->whereNull('deleted_at')->first();
  924. if($agent){
  925. return $this->error('450001','该手机号已注册,不能使用');
  926. }
  927. $user=UserW::where('phone',$phone)->first();
  928. try{
  929. DB::transaction(function()use(&$user,$phone,$nickname,$area_code,$store,$is_vip,$request){
  930. if(empty($user)){
  931. $user=UserW::create([
  932. 'area_code'=>$area_code,
  933. 'phone'=>$phone,
  934. 'name'=>$nickname?:$phone,
  935. 'avatar' => 'http://api.app.cliu.cc/public/uploads/logo.jpg',
  936. 'nickname'=>$nickname?:$phone
  937. ]);
  938. }
  939. $agent_id=Auth::user()->id;
  940. $store_id=Store::where('user_id',$agent_id)->value('id');
  941. if($request->input('province') && $request->input('city') && $request->input('area') && $request->input('address')){
  942. AddressW::create([
  943. 'name'=>$nickname?:$phone,
  944. 'area_code'=>$area_code,
  945. 'phone'=>$phone,
  946. 'user_id'=>$user->id,
  947. 'store_id'=>$store_id,
  948. 'province'=>$request->input('province'),
  949. 'city'=>$request->input('city'),
  950. 'area'=>$request->input('area'),
  951. 'address'=>$request->input('address'),
  952. 'is_default'=>$request->input('is_default',0),
  953. 'remark'=>$request->input('remark',null),
  954. 'type'=>2,
  955. ]);
  956. }
  957. if($is_vip==1){
  958. $user->is_vip=1;
  959. VipLogW::firstORCreate(['user_id'=>$user->id,'store_id'=>$store->id],['order_no'=>null,'type'=>1,'remark'=>'商户手动添加vip']);
  960. }else{
  961. $user->is_vip=0;
  962. }
  963. },5);
  964. return $this->success($user);
  965. }catch(\Exception $e){
  966. return $this->error($e->getMessage());
  967. }
  968. }
  969. //设置vip
  970. public function setVip(Request $request){
  971. $agent_id=Auth::user()->id;
  972. $user_id=$request->input('user_id');
  973. $store_id=Store::where('user_id',$agent_id)->value('id');
  974. VipLogW::firstORCreate(['user_id'=>$user_id,'store_id'=>$store_id],['order_no'=>null,'type'=>1,'remark'=>'商户手动设置vip']);
  975. return $this->success([]);
  976. }
  977. //取消vip
  978. public function cancelVip(Request $request){
  979. $agent_id=Auth::user()->id;
  980. $user_id=$request->input('user_id');
  981. $store_id=Store::where('user_id',$agent_id)->value('id');
  982. $vip=VipLogW::where('store_id',$store_id)->where('user_id',$user_id)->first();
  983. if(empty($vip)){
  984. return $this->error('450001','该客户不是vip');
  985. }
  986. $orders=OrderW::where('store_id',$store_id)->where('user_id',$user_id)->get();
  987. if(count($orders)>0){
  988. return $this->error('450001','该客户已下单,无法取消vip');
  989. }
  990. $vip=VipLogW::where('store_id',$store_id)->where('user_id',$user_id)->first();
  991. $vip->remark=!empty($vip->remark)?$vip->remark.'->商户删除':'商户删除';
  992. $vip->save();
  993. $vip->delete();
  994. return $this->success([]);
  995. }
  996. //获取用户数据信息
  997. public function getUserDataInfo()
  998. {
  999. $store_id = Store::where('user_id',Auth::user()->id)->value('id');
  1000. //客户总数
  1001. $user_num = OrderW::where('store_id', $store_id)
  1002. ->where('is_pay',1)->groupBy('user_id')
  1003. ->whereIn('apply_cancel',[0,1,3])
  1004. ->whereIn('is_refund',[0,1,2])
  1005. ->get()->count();
  1006. //订单数量
  1007. $order_num = OrderW::where('store_id', $store_id)
  1008. ->whereIn('apply_cancel',[0,1,3])
  1009. ->whereIn('is_refund',[0,1,2])
  1010. ->where('is_pay',1)->count();
  1011. //总交易额
  1012. $sale_total = OrderW::where('store_id', $store_id)->where('is_pay',1)->sum('account');
  1013. $sale_total = round($sale_total);
  1014. //扫码用户
  1015. $scan_user = Scan::with('user:id,nickname,avatar')->where('store_id', $store_id)
  1016. ->orderByDesc('created_at')->limit(5)->get();
  1017. $scan_num = Scan::has('user')->where('store_id', $store_id)->count();
  1018. $scan_7_num = Scan::has('user')->where('store_id', $store_id)->whereBetween('created_at', [date("Y-m-d H:i", strtotime('-7 days')), date("Y-m-d H:i")])->count();
  1019. //已购客户
  1020. // $buy_num=UserW::whereHas('order', function ($query)use($store_id) {
  1021. // $query->where('store_id', $store_id)->where('is_pay', 1);
  1022. // })->get()->count();
  1023. // $buy_user = OrderW::with('user:id,nickname,avatar,phone')
  1024. // ->where('store_id',$store_id)->where('is_pay',1)
  1025. // ->orderByDesc('created_at')->limit(5)->get();
  1026. // $user_ids=UserW::where('store_id',$store_id)->pluck('id');
  1027. $userInfo=OrderW::with('user:id,nickname,avatar,phone')->has('user')
  1028. ->where('store_id',$store_id)->where('is_pay',1)
  1029. ->select('user_id',DB::raw('sum(total) as total'),DB::raw('sum(account) as amount'))
  1030. ->groupBy('user_id')->orderByDesc('amount')
  1031. ->orderByDesc('total')->orderBy('user_id')
  1032. ->limit(5)->get();
  1033. $buy_7_num = OrderW::where('store_id', $store_id)
  1034. ->whereBetween('created_at', [date("Y-m-d H:i", strtotime('-7 days')), date("Y-m-d H:i")])
  1035. ->where('is_pay',1)
  1036. ->count();
  1037. $data['user_num'] = $user_num ? $user_num : 0;
  1038. $data['sale_total'] = $sale_total ? $sale_total : 0;
  1039. $data['scan_user'] = $scan_user ? $scan_user : [];
  1040. $data['scan_num'] = $scan_num ? $scan_num : 0;
  1041. $data['scan_7_num'] = $scan_7_num ? $scan_7_num : 0;
  1042. // $data['buy_user'] = $buy_user ? $buy_user : [];
  1043. // $data['buy_num'] = $buy_num ? $buy_num : 0;
  1044. $data['order_num'] = $order_num ? $order_num : 0;
  1045. $data['buy_7_num'] = $buy_7_num ? $buy_7_num : 0;
  1046. $data['user_info'] = $userInfo;
  1047. return $this->success($data);
  1048. }
  1049. //扫码用户查看更多
  1050. public function getMoreScan(Request $request)
  1051. {
  1052. $input = $request->input();
  1053. $page_index = $input['page_index'];
  1054. $page_size = $input['page_size'];
  1055. $num = $page_size * ($page_index - 1);
  1056. $store_id = Store::where('user_id',Auth::user()->id)->value('id');
  1057. $data=Scan::with('user:id,nickname,avatar')
  1058. ->has('user')
  1059. ->where('store_id', $store_id);
  1060. $count=$data->count();
  1061. $list=$data->orderByDesc('created_at')->skip($num)->limit($page_size)->get();
  1062. return $this->success_list($list,'成功',$count);
  1063. }
  1064. //已购客户查看更多
  1065. public function getMoreBuy(Request $request)
  1066. {
  1067. $input=$request->input();
  1068. $page_index=$input['page_index'];
  1069. $page_size=$input['page_size'];
  1070. $num=$page_size*($page_index-1);
  1071. $store_id = Store::where('user_id',Auth::user()->id)->value('id');
  1072. // $data=UserW::withCount(['order'=>function($query){
  1073. // $query->select(DB::raw("sum(account) as money"));
  1074. // }])
  1075. // ->where('store_id', $store_id)->having('order_count','>',0);
  1076. $data=OrderW::with('user:id,nickname,avatar,phone')
  1077. ->where('store_id',$store_id)->where('is_pay',1);
  1078. $count=$data->get()->count();
  1079. $list=$data->orderByDesc('created_at')->skip($num)->limit($page_size)
  1080. ->get();
  1081. return $this->success_list($list,'成功',$count);
  1082. }
  1083. //获取更多用户信息
  1084. public function getMoreUserInfo(Request $request){
  1085. $input=$request->input();
  1086. $page_index=$input['page_index'];
  1087. $page_size=$input['page_size'];
  1088. $num=$page_size*($page_index-1);
  1089. $store_id = Store::where('user_id',Auth::user()->id)->value('id');
  1090. $data=OrderW::with('user:id,nickname,avatar,phone')->has('user')
  1091. ->where('store_id',$store_id)->where('is_pay',1)
  1092. ->select('user_id',DB::raw('sum(total) as total'),DB::raw('sum(account) as amount'))
  1093. ->groupBy('user_id');
  1094. $count=$data->get()->count();
  1095. $list=$data->orderByDesc('amount')->orderByDesc('total')->orderBy('user_id')
  1096. ->skip($num)->limit($page_size)->get();
  1097. return $this->success_list($list,'成功',$count);
  1098. }
  1099. //获取用户所有订单
  1100. public function getUserAllOrder(Request $request){
  1101. $user_id=$request->input('user_id');
  1102. $status=$request->input('status');
  1103. $order_no=$request->input('order_no');
  1104. $page_size=$request->input('page_size');
  1105. $page_index=$request->input('page_index');
  1106. $num=$page_size*($page_index-1);
  1107. $where=[];
  1108. if($status!='all'){
  1109. $where['status']=$status;
  1110. }
  1111. $where['user_id']=$user_id;
  1112. $data=OrderW::with(['user:id,nickname,phone','order_cancel'=>function($query){
  1113. $query->where('using',1);}])
  1114. ->where($where)
  1115. ->whereIn('apply_cancel',[0,1,3])
  1116. ->orderByDesc('id');
  1117. if($order_no){
  1118. $data->where('order_no','like','%'.$order_no.'%');
  1119. }
  1120. $count=$data->count();
  1121. $orders=$data->skip($num)->take($page_size)->get();
  1122. foreach($orders as $key=>$val){
  1123. $goods_ids=OrderDetailW::where('order_no',$val->order_no)->groupBy('goods_id')->pluck('goods_id');
  1124. $goods=[];
  1125. foreach($goods_ids as $k=>$v){
  1126. $goods[$k]=StoreGoods::where('id',$v)->first();
  1127. $goods[$k]->sku=OrderDetailW::where('order_no',$val->order_no)->where('goods_id',$v)->get();
  1128. }
  1129. $orders[$key]->goods=$goods;
  1130. }
  1131. return $this->success_list($orders,'成功',$count);
  1132. }
  1133. //获取代为下单用户信息
  1134. public function getUserInfo(Request $request)
  1135. {
  1136. $input=$request->all();
  1137. $phone=$input['phone'];
  1138. $user=UserW::where('phone',$phone)->first();
  1139. if(empty($user)){
  1140. return $this->error('450001','用户手机号有误');
  1141. }
  1142. if($user->status==1){
  1143. return $this->error('450001','用户已被禁用');
  1144. }
  1145. return $this->success($user);
  1146. }
  1147. //获取用户所有收货地址
  1148. public function getAllAddress(Request $request)
  1149. {
  1150. $input=$request->All();
  1151. $agent_id=Auth::user()->id;
  1152. $store_id=Store::where('user_id',$agent_id)->value('id');
  1153. $address=AddressW::where('user_id',$input['id'])->whereIn('type',[1,2])->orderByDesc('is_default')->orderByDesc('created_at')->get();
  1154. // $address=AddressW::where('user_id',$input['id'])->where('store_id',$store_id)->where('type',2)->orderByDesc('is_default')->orderByDesc('created_at')->get();
  1155. return $this->success($address);
  1156. }
  1157. //获取收货地址
  1158. public function getAddress(Request $request)
  1159. {
  1160. $input=$request->All();
  1161. $agent_id=Auth::user()->id;
  1162. $store_id=Store::where('user_id',$agent_id)->value('id');
  1163. $data['address']=AddressW::where('user_id',$input['id'])->whereIn('type',[1,2])->orderByDesc('is_default')->orderByDesc('created_at')->first();
  1164. // $data['address']=AddressW::where('user_id',$input['id'])->where('store_id',$store_id)->where('type',2)->orderByDesc('is_default')->orderByDesc('created_at')->first();
  1165. $data['store']=Store::with('address_store')->where('user_id',Auth::user()->id)->first();
  1166. return $this->success($data);
  1167. }
  1168. //添加收货地址
  1169. public function addAddress(Request $request)
  1170. {
  1171. $input=$request->all();
  1172. $regex=Verify::where('code',$request->input('areaCode','+86'))->value('preg');
  1173. $rules=[
  1174. 'name'=>'required|max:32',
  1175. 'phone'=>['required','regex:'.$regex],
  1176. 'province'=>'required',
  1177. 'city'=>'required',
  1178. 'area'=>'required',
  1179. 'address'=>'required|max:255',
  1180. ];
  1181. $message=[
  1182. 'name.required'=>'收货人不能为空.',
  1183. 'name.max'=>'收货人姓名不能大于32个字符.',
  1184. 'phone.required'=>'手机号不能为空.',
  1185. 'phone.regex'=>'手机号格式有误.',
  1186. 'province.required'=>'省不能为空.',
  1187. 'city.required'=>'市不能为空.',
  1188. 'area.required'=>'区/县不能为空.',
  1189. 'address.required'=>'详细地址不能为空.',
  1190. 'address.max'=>'详细地址不能大于255个字符.'
  1191. ];
  1192. $validator = Validator::make($input,$rules,$message);
  1193. if($validator->fails()){
  1194. return $this->error('400013',$validator->errors()->first());
  1195. }
  1196. $count=AddressW::where('user_id',$input['user_id'])->count();
  1197. // if($count>=10){
  1198. // return $this->error('450001','用户地址数量已达上限');
  1199. // }
  1200. if($input['is_default']==1){
  1201. AddressW::where('user_id',$input['user_id'])->update(['is_default'=>0]);
  1202. }
  1203. $agent_id=Auth::user()->id;
  1204. $store_id=Store::where('user_id',$agent_id)->value('id');
  1205. $res=AddressW::create([
  1206. 'name'=>$input['name'],
  1207. 'area_code'=>$request->input('areaCode','+86'),
  1208. 'phone'=>$input['phone'],
  1209. 'user_id'=>$input['user_id'],
  1210. 'store_id'=>$store_id,
  1211. 'province'=>$input['province'],
  1212. 'city'=>$input['city'],
  1213. 'area'=>$input['area'],
  1214. 'address'=>$input['address'],
  1215. 'is_default'=>$input['is_default'],
  1216. 'remark'=>$input['remark'],
  1217. 'type'=>2,
  1218. ]);
  1219. if($res){
  1220. return $this->success([]);
  1221. }else{
  1222. return $this->error();
  1223. }
  1224. }
  1225. //修改收货地址
  1226. public function updateAddress(Request $request)
  1227. {
  1228. $input=$request->all();
  1229. $regex=Verify::where('code',$request->input('areaCode','+86'))->value('preg');
  1230. $rules=[
  1231. 'name'=>'required|max:32',
  1232. 'phone'=>['required','regex:'.$regex],
  1233. 'province'=>'required',
  1234. 'city'=>'required',
  1235. 'area'=>'required',
  1236. 'address'=>'required|max:255',
  1237. ];
  1238. $message=[
  1239. 'name.required'=>'收货人不能为空.',
  1240. 'name.max'=>'收货人姓名不能大于32个字符.',
  1241. 'phone.required'=>'手机号不能为空.',
  1242. 'phone.regex'=>'手机号格式有误.',
  1243. 'province.required'=>'省不能为空.',
  1244. 'city.required'=>'市不能为空.',
  1245. 'area.required'=>'区/县不能为空.',
  1246. 'address.required'=>'详细地址不能为空.',
  1247. 'address.max'=>'详细地址不能大于255个字符.'
  1248. ];
  1249. $validator = Validator::make($input,$rules,$message);
  1250. if($validator->fails()){
  1251. return $this->error('400013',$validator->errors()->first());
  1252. }
  1253. $address_id=$input['address_id'];
  1254. $address=AddressW::where('id',$address_id)->first();
  1255. if($input['is_default']==1){
  1256. AddressW::where('user_id',$address->user_id)->update(['is_default'=>0]);
  1257. }
  1258. $res=AddressW::where('id',$address_id)->update([
  1259. 'name'=>$input['name'],
  1260. 'area_code'=>$request->input('areaCode','+86'),
  1261. 'phone'=>$input['phone'],
  1262. 'province'=>$input['province'],
  1263. 'city'=>$input['city'],
  1264. 'area'=>$input['area'],
  1265. 'address'=>$input['address'],
  1266. 'is_default'=>$input['is_default'],
  1267. 'remark'=>$input['remark'],
  1268. ]);
  1269. if($res){
  1270. return $this->success([]);
  1271. }else{
  1272. return $this->error();
  1273. }
  1274. }
  1275. //删除收货地址
  1276. public function destroyAddress(Request $request)
  1277. {
  1278. $res=AddressW::where('id',$request->input('id'))->delete();
  1279. if($res){
  1280. return $this->success([]);
  1281. }else{
  1282. return $this->error();
  1283. }
  1284. }
  1285. //设为默认地址
  1286. public function setDefaultAddress(Request $request)
  1287. {
  1288. $address=AddressW::where('id',$request->input('id'))->first();
  1289. try{
  1290. DB::connection('mysql_w')->transaction(function()use($address){
  1291. if($address->is_default == 1) {
  1292. $address->is_default = 0;
  1293. $address->save();
  1294. }else{
  1295. $address->is_default = 1;
  1296. $address->save();
  1297. AddressW::where('user_id',$address->user_id)->whereNotIn('id',[$address->id])->update(['is_default'=>0]);
  1298. }
  1299. },5);
  1300. return $this->success([]);
  1301. }catch(\Exception $e){
  1302. return $this->error($e->getMessage());
  1303. }
  1304. }
  1305. //判断订单价格
  1306. protected function getOrderPrice($user_id,$store_id,$num){
  1307. $vipLog=VipLogW::where('user_id',$user_id)->where('store_id',$store_id)->first();
  1308. if($vipLog){
  1309. return 'vip_ed';//vip价格
  1310. }else{
  1311. if($num>=2){
  1312. return 'vip_ing';//vip价格
  1313. }else{
  1314. return 'normal';//正常价
  1315. }
  1316. }
  1317. }
  1318. //记录下单时商户关系
  1319. protected function saveAgentNoLog($user_id,$agent_id,$order_no){
  1320. $one=$agent_id;
  1321. $one_agent=User::where('id',$one)->first();
  1322. $data=[];
  1323. $data[0]['order_no'] = $order_no;
  1324. $data[0]['user_id'] = $user_id;
  1325. // $data[0]['agent_id'] = $one;
  1326. $data[0]['level'] = $one_agent->level;
  1327. $data[0]['nickname'] = $one_agent->nickname;
  1328. $data[0]['phone'] = $one_agent->mobile;
  1329. $data[0]['created_at'] = date("Y-m-d H:i:s");
  1330. $data[0]['updated_at'] = date("Y-m-d H:i:s");
  1331. if($one_agent->level==2 || $one_agent->level==1){
  1332. $two=User::where('id',$one)->value('agent_id');
  1333. $two_agent=User::where('id',$two)->first();
  1334. $data[1]['order_no'] = $order_no;
  1335. $data[1]['user_id'] = $user_id;
  1336. // $data[1]['agent_id'] = $two;
  1337. $data[1]['level'] = $two_agent->level;
  1338. $data[1]['nickname'] = $two_agent->nickname;
  1339. $data[1]['phone'] = $two_agent->mobile;
  1340. $data[1]['created_at'] = date("Y-m-d H:i:s");
  1341. $data[1]['updated_at'] = date("Y-m-d H:i:s");
  1342. if($two_agent->level==2){
  1343. $three=User::where('id',$two)->value('agent_id');
  1344. $three_agent=User::where('id',$three)->first();
  1345. $data[2]['order_no'] = $order_no;
  1346. $data[2]['user_id'] = $user_id;
  1347. // $data[2]['agent_id'] = $three;
  1348. $data[2]['level'] = $three_agent->level;
  1349. $data[2]['nickname'] = $three_agent->nickname;
  1350. $data[2]['phone'] = $three_agent->mobile;
  1351. $data[2]['created_at'] = date("Y-m-d H:i:s");
  1352. $data[2]['updated_at'] = date("Y-m-d H:i:s");
  1353. }
  1354. }
  1355. return $data;
  1356. }
  1357. public function CheckOrder(Request $request){
  1358. $input=$request->all();
  1359. $sku=$input['sku'];
  1360. $user=UserW::find($input['user_id']);
  1361. $store=Store::where('user_id',Auth::user()->id)->first();
  1362. $time=Carbon::now()->addMonths(-2);
  1363. $re=OrderW::where('user_id',$user->id)->where('is_pay',1)->where('apply_cancel',0)->where('store_id',$store->id)->where('created_at','>=',$time)->pluck('id');
  1364. if (count($re)==0){
  1365. return $this->success([]);
  1366. }
  1367. foreach ($sku as $k=>$v){
  1368. $res=OrderDetailW::whereIn('order_id',$re)->where('sku_id',$v[0])->count();
  1369. if ($res>0){
  1370. return $this->error(50001,[],'您在2个月内已经购买过相同款式尺码的商品,确定要再次购买吗?');
  1371. }
  1372. }
  1373. return $this->success([]);
  1374. }
  1375. //代下单或分享提交订单
  1376. public function submitOrderInfo(Request $request)
  1377. {
  1378. $input=$request->all();
  1379. Log::error($input);
  1380. $sku=$input['sku'];
  1381. $user=UserW::find($input['user_id']);
  1382. $agent=User::find(Auth::user()->id);
  1383. $store=Store::where('user_id',Auth::user()->id)->first();
  1384. if(empty($store)){
  1385. return $this->error('450001','店铺不存在或者已被删除');
  1386. }
  1387. if($store->status==1){
  1388. return $this->error('450001','店铺已被禁用');
  1389. }
  1390. //判断用户是否存在介绍人
  1391. if($user){
  1392. if($user->recomer_id && $user->recomer_store_id==$store->id){
  1393. $recomer_id=$user->recomer_id;
  1394. $recomer_status=0;
  1395. }else{
  1396. $recomer_id=null;
  1397. $recomer_status=0;
  1398. }
  1399. }else{
  1400. return $this->error('450001','客户信息有误');
  1401. }
  1402. $store_id=$store->id;
  1403. $remark=$input['remark'];
  1404. $order_no=$this->order_no('dx');
  1405. $account=$vip_account=$cost_account=$num=0;
  1406. $data=$snapshot=[];
  1407. foreach($sku as $key=>$val){
  1408. if($val[1] > 0){
  1409. $num += $val[1];
  1410. }
  1411. }
  1412. $vip=$this->getOrderPrice($user->id,$store_id,$num);
  1413. $arr_test=Store::getTest();
  1414. foreach($sku as $key=>$val){
  1415. $sku_info=[];
  1416. $goodSku=StoreGoodsSku::with(['goods'])->where('id',$val[0])->first();
  1417. if(!array_key_exists($goodSku->goods_id,$snapshot)){
  1418. $snapshot[$goodSku->goods_id]['name']=$goodSku->goods->name;
  1419. $snapshot[$goodSku->goods_id]['main_attr']=$goodSku->goods->main_attr;
  1420. $snapshot[$goodSku->goods_id]['img']=$goodSku->goods->img;
  1421. $snapshot[$goodSku->goods_id]['price']=$goodSku->goods->price;
  1422. $snapshot[$goodSku->goods_id]['sku']=[];
  1423. }
  1424. if(in_array($store_id,$arr_test)){
  1425. $storeInventory=InventoryW::where(['store_id'=>$store_id,'goods_id'=>$goodSku->goods_id,'sku_id'=>$val[0]])->lockForUpdate()->first();
  1426. $storeInve_num=!empty($storeInventory)?$storeInventory->num:0;
  1427. if($val[1] > $storeInve_num){
  1428. return $this->error('450001','【'.$goodSku->goods->main_attr.'-'.$goodSku->type.'-'.$goodSku->size.'】库存不足');
  1429. }
  1430. }
  1431. if(empty($goodSku)){
  1432. return $this->error('450001','商品规格不存在,刷新页面重新下单');
  1433. }
  1434. if($agent->level==1){
  1435. $cost_account += $goodSku->goods->super_price * $val[1];
  1436. $data[$key]['cost_price']=$goodSku->goods->super_price;
  1437. $data[$key]['cost_account']=$goodSku->goods->super_price * $val[1];
  1438. }
  1439. if($agent->level==2){
  1440. $cost_account += $goodSku->goods->top_price * $val[1];
  1441. $data[$key]['cost_price']=$goodSku->goods->top_price;
  1442. $data[$key]['cost_account']=$goodSku->goods->top_price * $val[1];
  1443. }
  1444. if($agent->level==3){
  1445. $cost_account += $goodSku->goods->crown_price * $val[1];
  1446. $data[$key]['cost_price']=$goodSku->goods->crown_price;
  1447. $data[$key]['cost_account']=$goodSku->goods->crown_price * $val[1];
  1448. }
  1449. if($vip=='vip_ed' || $vip=='vip_ing'){
  1450. $data[$key]['price']=$goodSku->goods->vip_price;
  1451. $sku_info['price']=$goodSku->goods->vip_price;
  1452. $data[$key]['account']=$goodSku->goods->vip_price * $val[1];
  1453. $account += $goodSku->goods->vip_price * $val[1];
  1454. }else{
  1455. $data[$key]['price']=$goodSku->goods->price;
  1456. $sku_info['price']=$goodSku->goods->price;
  1457. $data[$key]['account']=$goodSku->goods->price * $val[1];
  1458. $account += $goodSku->goods->price * $val[1];
  1459. }
  1460. $data[$key]['origin_price']=$goodSku->goods->price;
  1461. $data[$key]['order_no']=$order_no;
  1462. $data[$key]['goods_id']=$goodSku->goods_id;
  1463. $data[$key]['store_id']=$store_id;
  1464. $data[$key]['user_id']=$user->id;
  1465. $data[$key]['sku_id']=$goodSku->id;
  1466. $data[$key]['num']=$val[1];
  1467. $data[$key]['size']=$goodSku->size;
  1468. $data[$key]['type']=$goodSku->type;
  1469. $data[$key]['style']=1;
  1470. $data[$key]['created_at']=date("Y-m-d H:i:s");
  1471. $data[$key]['updated_at']=date("Y-m-d H:i:s");
  1472. $sku_info['origin_price']=$goodSku->goods->price;
  1473. $sku_info['sku_id']=$goodSku->id;
  1474. $sku_info['num']=$val[1];
  1475. $sku_info['size']=$goodSku->size;
  1476. $sku_info['type']=$goodSku->type;
  1477. array_push($snapshot[$goodSku->goods_id]['sku'],$sku_info);
  1478. }
  1479. $order_data=[
  1480. 'order_no'=>$order_no,
  1481. 'store_id'=>$store_id,
  1482. 'user_id'=>$user->id,
  1483. 'remark'=>$remark,
  1484. 'is_help'=>1,
  1485. 'op_id'=>Auth::user()->id,
  1486. 'op_name'=>Auth::user()->nickname,
  1487. // 'status'=>0,
  1488. // 'is_pay'=>0,
  1489. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  1490. 'status'=>1,
  1491. 'is_pay'=>1,//服务商产品权限冻结后下单即支付
  1492. 'pay_money'=>$account,
  1493. 'pay_type'=>3,//免支付
  1494. 'pay_at'=>date("Y-m-d H:i:s"),
  1495. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  1496. 'is_tip_send'=>0,
  1497. 'is_comment'=>0,
  1498. 'total'=>$num,
  1499. 'account'=>$account,
  1500. 'cost_account'=>$cost_account,
  1501. 'snapshot'=>json_encode($snapshot),
  1502. 'recomer_id'=>$recomer_id,
  1503. 'recomer_status'=>$recomer_status,
  1504. 'address_id'=>$request->input('address_id',null),
  1505. ];
  1506. $agent_id=Store::where('id',$store_id)->value('user_id');
  1507. $agent_data=$this->saveAgentNoLog($user->id,$agent_id,$order_no);
  1508. DB::connection('mysql_w')->beginTransaction();
  1509. try{
  1510. $result=OrderW::create($order_data); //生成订单
  1511. // Log::info($result);
  1512. foreach($data as $k=>$v){
  1513. $data[$k]['order_id']=$result->id;
  1514. }
  1515. OrderDetailW::insert($data); //生成订单商品详情
  1516. AgentNo::insert( $agent_data); //账号关系
  1517. DB::connection('mysql_w')->commit();
  1518. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  1519. //代下单通知客户
  1520. $order=$result;
  1521. $user=UserW::where('id',$order->user_id)->first();
  1522. $agent_id=Store::where('id',$order->store_id)->value('user_id');
  1523. $agent = User::where('id',$agent_id)->first();
  1524. if($user->openId){
  1525. $data['openid']=$user->openId;
  1526. $data['data']=[
  1527. 'first' =>'商户【'.$agent->nickname .'】已帮您下单成功,请及时查看订单',
  1528. 'keyword1' => $order->order_no,
  1529. // 'keyword2' => number_format($order->account, 2),
  1530. 'keyword2' => '请联系线下收款',
  1531. 'keyword3' => $order->op_name,
  1532. 'keyword4' => $agent->mobile,
  1533. 'remark' => '点击链接进入系统,查看详情',
  1534. ];
  1535. event(new HelpOrderEvent($data));
  1536. }
  1537. //代下单通知
  1538. //新订单通知
  1539. // $store=Store::where('id',$order->store_id)->first();
  1540. // $user=User::where('id',$store->user_id)->first();
  1541. if($agent->openid){
  1542. $data['openid']=$agent->openid;
  1543. $data['data']=[
  1544. 'first' => '您有新的客户订单,请及时处理',
  1545. 'keyword1' => $user->nickname,
  1546. // 'keyword2' => number_format($order->account, 2),
  1547. 'keyword2' => '请联系线下收款',
  1548. 'keyword3' => $order->order_no,
  1549. 'keyword4' => $order->created_at
  1550. ];
  1551. event(new NewOrder($data));
  1552. }
  1553. //用户vip变更
  1554. $vipLog=VipLogW::where('user_id',$order->user_id)
  1555. ->where('store_id',$order->store_id)->first();
  1556. if(empty($vipLog) && $order->total>=2){
  1557. VipLogW::create([
  1558. 'user_id'=>$order->user_id,
  1559. 'store_id'=>$store->id,
  1560. 'order_no'=>$order->order_no,
  1561. 'type'=>2,//购买两套,添加vip
  1562. 'remark'=>'购买自动添加vip'
  1563. ]);
  1564. OrderW::where('order_no',$order->order_no)->update([
  1565. 'vip'=>1
  1566. ]);
  1567. }
  1568. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  1569. return $this->success($order_no);
  1570. }catch(\Exception $e){
  1571. DB::connection('mysql_w')->rollback();
  1572. return $this->error('450001','提交失败',$e->getMessage());
  1573. }
  1574. }
  1575. //修改订单备注信息
  1576. public function editOrderRemark(Request $request){
  1577. $order_no=$request->input('order_no');
  1578. $remark=$request->input('remark');
  1579. $res=OrderW::where('order_no',$order_no)->update([
  1580. 'remark'=>$remark
  1581. ]);
  1582. if($res){
  1583. return $this->success([]);
  1584. }else{
  1585. return $this->error();
  1586. }
  1587. }
  1588. //分享订单
  1589. public function shareOrderInfo(Request $request)
  1590. {
  1591. $input=$request->all();
  1592. $store_id=Store::where('user_id',Auth::user()->id)->value('id');
  1593. $sku=$input['sku'];
  1594. // $user=UserW::find($input['user_id']);
  1595. $self_receive=$input['self_receive'];//不自提0 自提1
  1596. if($self_receive==1){
  1597. $address_id=Store::where('id',$store_id)->value('address_id');
  1598. }else{
  1599. $address_id=$input['address_id'];
  1600. }
  1601. $remark=$input['remark'];
  1602. $order_no=$this->order_no('fx');
  1603. $account=$vip_account=$num=0;
  1604. $data=[];
  1605. foreach($sku as $key=>$val){
  1606. $goodSku=StoreGoodsSku::with(['goods'])->where('id',$val[0])->first();
  1607. if(empty($goodSku)){
  1608. return $this->error('450001','商品规格不存在,请重新分享');
  1609. }
  1610. $account += $goodSku->goods->price * $val[1];
  1611. $num += $val[1];
  1612. $data[$key]['order_no']=$order_no;
  1613. $data[$key]['goods_id']=$goodSku->goods_id;
  1614. $data[$key]['sku_id']=$goodSku->id;
  1615. $data[$key]['num']=$val[1];
  1616. $data[$key]['price']=$goodSku->goods->price;
  1617. $data[$key]['account']=$goodSku->goods->price * $val[1];
  1618. $data[$key]['size']=$goodSku->size;
  1619. $data[$key]['type']=$goodSku->type;
  1620. $data[$key]['created_at']=date("Y-m-d H:i:s");
  1621. $data[$key]['updated_at']=date("Y-m-d H:i:s");
  1622. }
  1623. $order_data=[
  1624. 'order_no'=>$order_no,
  1625. 'store_id'=>$store_id,
  1626. 'user_id'=>$input['user_id'],
  1627. 'address_id'=>$address_id,
  1628. 'self_receive'=>$self_receive,
  1629. 'remark'=>$remark,
  1630. 'op_id'=>Auth::user()->id,
  1631. 'op_name'=>Auth::user()->nickname,
  1632. 'total'=>$num,
  1633. 'account'=>$account,
  1634. ];
  1635. DB::connection('mysql_w')->beginTransaction();
  1636. try{
  1637. OrderDetailW::insert($data);
  1638. OrderShareW::create($order_data);
  1639. DB::connection('mysql_w')->commit();
  1640. return $this->success($order_no);
  1641. }catch(\Exception $e){
  1642. DB::connection('mysql_w')->rollback();
  1643. return $this->error('450001','提交失败',$e->getMessage());
  1644. }
  1645. }
  1646. //生成订单号
  1647. public function order_no($type='dx')
  1648. {
  1649. if($type=='fx'){
  1650. $prefix = 'FX';
  1651. }else{
  1652. $prefix = 'DX';
  1653. }
  1654. $timestamp=date('ymdHis') . substr(microtime(), 2, 6);
  1655. $rand=mt_rand(10000, 99999);
  1656. $order_no=$prefix . $timestamp . $rand;
  1657. if(OrderW::where('order_no',$order_no)->first()){
  1658. $this->order_no($type);
  1659. }else{
  1660. return $order_no;
  1661. }
  1662. }
  1663. //审核订单取消
  1664. public function authOrderCancel(Request $request){
  1665. $id=$request->input('id');
  1666. $type=$request->input('type');//2通过//3驳回
  1667. $remark=$request->input('remark');
  1668. DB::connection('mysql')->beginTransaction();
  1669. DB::connection('mysql_w')->beginTransaction();
  1670. try{
  1671. $cancel=OrderCancelW::where('id',$id)->first();
  1672. $cancel->status=$type;
  1673. $cancel->remark=$remark;
  1674. $cancel->save();
  1675. $order=OrderW::where('id',$cancel->order_id)->first();
  1676. $order->apply_cancel= $type;
  1677. $order->cancel_at=date("Y-m-d H:i:s");
  1678. $order->save();
  1679. $first='取消订单被驳回';
  1680. if($type==2){//同意取消
  1681. //退款
  1682. // $refund=new RefundAmount();
  1683. // $res=$refund->refundAmount($cancel->order_id);
  1684. // if($res[0]=='450001'){
  1685. // return $this->error('450001',$res[1]);
  1686. // }
  1687. // if($res[0]=='450000'){
  1688. // return $this->error('450001','取消订单退款失败',$res[1]);
  1689. // }
  1690. // // // // // // // // // // // // // // // // // // // // // //
  1691. if($order->pay_type!=3){
  1692. //退款
  1693. $refund=new RefundAmount();
  1694. $res=$refund->refundAmount($cancel->order_id);
  1695. if($res[0]=='450001'){
  1696. return $this->error('450001',$res[1]);
  1697. }
  1698. if($res[0]=='450000'){
  1699. return $this->error('450001','取消订单退款失败',$res[1]);
  1700. }
  1701. }
  1702. // // // // // // // // // // // // // // // // // // // // // //
  1703. //判断是否剥夺vip
  1704. if($order->vip==1){
  1705. $orders=OrderW::where('user_id',$order->user_id)
  1706. ->where('store_id',$order->store_id)
  1707. ->where('is_pay',1)
  1708. ->whereIn('apply_cancel',[0,1,3])
  1709. ->whereIn('is_refund',[0,1])
  1710. ->where('total','>=',2)
  1711. ->where('created_at','>',$order->created_at)
  1712. ->orderBy('created_at')->get();
  1713. if(count($orders)>=1){
  1714. OrderW::where('id',$orders[0]->id)->update([
  1715. 'vip'=>1
  1716. ]);
  1717. }else{
  1718. VipLogW::where('user_id',$order->user_id)->where('store_id',$order->store_id)->delete();
  1719. }
  1720. }
  1721. //判断恢复库存
  1722. $store=Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->first();
  1723. $level=User::where('id',$store->user_id)->value('level');
  1724. if($level==3){
  1725. event(new RestoreOutboundEvent($order->order_no));
  1726. }
  1727. $first='取消订单审核成功';
  1728. }
  1729. //取消订单通知
  1730. $user=UserW::where('id',$order->user_id)->first();
  1731. if($user->openId){
  1732. $data['openid']=$user->openId;
  1733. $data['data']=[
  1734. 'first' => '您的订单:'.$order->order_no.$first,
  1735. 'keyword1' => $user->nickname,
  1736. 'keyword2' => $order->order_no,
  1737. 'keyword3' => '已取消',
  1738. 'keyword4' => $order->cancel_at
  1739. ];
  1740. event(new CancelOrderEvent($data));
  1741. }
  1742. DB::connection('mysql')->commit();
  1743. DB::connection('mysql_w')->commit();
  1744. return $this->success([]);
  1745. }catch(\Exception $e){
  1746. DB::connection('mysql')->rollback();
  1747. DB::connection('mysql_w')->rollback();
  1748. return $this->error('450001','失败',$e->getMessage());
  1749. }
  1750. }
  1751. //商户取消订单
  1752. public function cancelOrder(Request $request){
  1753. $order_id=$request->input('order_id');
  1754. $order=OrderW::where('id',$order_id)->first();
  1755. // if($order->is_help==0){//用户自己下单
  1756. // return $this->error('450001','用户自己下单商户不能取消');
  1757. // }
  1758. if($order->apply_cancel==1){
  1759. return $this->error('450001','该订单正在审核中,不能再次取消');
  1760. }
  1761. if($order->apply_cancel==2){
  1762. return $this->error('450001','该订单已取消,不能再次取消');
  1763. }
  1764. DB::connection('mysql')->beginTransaction();
  1765. DB::connection('mysql_w')->beginTransaction();
  1766. try {
  1767. //退款
  1768. if($order->is_pay==1){
  1769. // $refund=new RefundAmount();
  1770. // $res=$refund->refundAmount($order->id);
  1771. // if($res[0]=='450001'){
  1772. // return $this->error('450001',$res[1]);
  1773. // }
  1774. // if($res[0]=='450000'){
  1775. // return $this->error('450001','退款失败',$res[1]);
  1776. // }
  1777. // // // // // // // // // // // // // // // // // // // // // //
  1778. if($order->pay_type!=3){
  1779. //退款
  1780. $refund=new RefundAmount();
  1781. $res=$refund->refundAmount($order->id);
  1782. if($res[0]=='450001'){
  1783. return $this->error('450001',$res[1]);
  1784. }
  1785. if($res[0]=='450000'){
  1786. return $this->error('450001','退款失败',$res[1]);
  1787. }
  1788. }
  1789. // // // // // // // // // // // // // // // // // // // // // //
  1790. }
  1791. //判断是否剥夺vip
  1792. if($order->vip==1){
  1793. $orders=OrderW::where('user_id',$order->user_id)
  1794. ->where('store_id',$order->store_id)
  1795. ->where('is_pay',1)
  1796. ->whereIn('apply_cancel',[0,1,3])
  1797. ->whereIn('is_refund',[0,1])
  1798. ->where('total','>=',2)
  1799. ->where('created_at','>',$order->created_at)
  1800. ->orderBy('created_at')->get();
  1801. if(count($orders)>=1){
  1802. OrderW::where('id',$orders[0]->id)->update([
  1803. 'vip'=>1
  1804. ]);
  1805. }else{
  1806. VipLogW::where('user_id',$order->user_id)->where('store_id',$order->store_id)->delete();
  1807. }
  1808. }
  1809. //判断恢复库存
  1810. $store=Store::withTrashed()->where('is_failure',0)->where('id',$order->store_id)->first();
  1811. $level=User::where('id',$store->user_id)->value('level');
  1812. if($level==3){
  1813. event(new RestoreOutboundEvent($order->order_no));
  1814. }
  1815. //取消订单
  1816. OrderW::where('id', $order_id)->update([
  1817. 'apply_cancel'=>2,
  1818. 'cancel_at'=>date("Y-m-d H:i:s")
  1819. ]);
  1820. OrderCancelW::where('user_id',$order->user_id)
  1821. ->where('store_id',$order->store_id)
  1822. ->where('order_id',$order->id)->update(['using'=>0]);
  1823. OrderCancelW::create([
  1824. 'user_id' =>$order->user_id,
  1825. 'store_id'=>$order->store_id,
  1826. 'order_id'=>$order->id,
  1827. 'status' =>2,
  1828. 'using' =>1,
  1829. 'type' =>2, //商户取消订单
  1830. 'reason' =>'商户取消订单'
  1831. ]);
  1832. //取消订单通知
  1833. $user=UserW::where('id',$order->user_id)->first();
  1834. if($user->openId){
  1835. $data['openid']=$user->openId;
  1836. $data['data']=[
  1837. 'first' => '您的订单:'.$order->order_no.'已被商户成功取消',
  1838. 'keyword1' => $user->nickname,
  1839. 'keyword2' => $order->order_no,
  1840. 'keyword3' => '已取消',
  1841. 'keyword4' => date("Y-m-d H:i:s")
  1842. ];
  1843. event(new CancelOrderEvent($data));
  1844. }
  1845. DB::connection('mysql')->commit();
  1846. DB::connection('mysql_w')->commit();
  1847. return $this->success([]);
  1848. }catch(\Exception $e){
  1849. DB::connection('mysql')->rollback();
  1850. DB::connection('mysql_w')->rollback();
  1851. return $this->error('450001','失败',$e->getMessage());
  1852. }
  1853. }
  1854. //获取订单状态数量
  1855. public function getOrderStatusNum(Request $request){
  1856. $store=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->first();
  1857. $user_id=$request->input('user_id');
  1858. if($user_id){
  1859. $where['user_id']=$user_id;
  1860. }
  1861. $where['store_id']=$store->id;
  1862. $data=OrderW::where($where)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1,2]);
  1863. $data1=clone $data;
  1864. $data2=clone $data;
  1865. $data3=clone $data;
  1866. $num['noPay']=$data->where('status',0)->count();
  1867. $num['noSend']=$data1->where('status',1)->count();
  1868. $num['noReceive']=$data2->where('status',2)->count();
  1869. $num['Finish']=$data3->where('status',3)->count();
  1870. //判断扫码发货还是立即发货,功能暂时未用到
  1871. // if(in_array($store->phone,['15138929587','18838980968', '13030314383'])){//, '13030314383',
  1872. // $num['is_code']=1;
  1873. // }else{
  1874. // $num['is_code']=0;
  1875. // }
  1876. return $this->success($num);
  1877. }
  1878. //获取订单列表
  1879. public function getOrderList(Request $request)
  1880. {
  1881. $input=$request->all();
  1882. $status=$input['status'];
  1883. $page_index=$input['page_index'];
  1884. $page_size=$input['page_size'];
  1885. $order_no=$input['order_no'];
  1886. $num=$page_size*($page_index-1);
  1887. $where=$wheres=[];
  1888. if($status!='all'){
  1889. $where['status']=$status;
  1890. }
  1891. if($order_no){
  1892. $wheres=function($query)use($order_no){
  1893. $query->where('order_no','like','%'.$order_no.'%');
  1894. };
  1895. }
  1896. $store=Store::where('user_id',Auth::user()->id)->first();
  1897. $where['store_id']=$store->id;
  1898. $data=OrderW::with(['user:id,nickname,phone','order_cancel'=>function($query){
  1899. $query->where('using',1);}])->where($where)->where($wheres)->whereIn('apply_cancel',[0,1,3])
  1900. ->whereIn('is_refund',[0,1,2])
  1901. ->orderByDesc('updated_at')->orderByDesc('id');
  1902. $count=$data->count();
  1903. $orders=$data->skip($num)->take($page_size)->get();
  1904. foreach($orders as $key=>$val){
  1905. if($val->status==3 && $val->is_refund > 0){
  1906. $order_refund = OrderRefundW::where('order_no',$val->order_no)->get();
  1907. if($order_refund){
  1908. $orders[$key]->refund_goods=$order_refund;
  1909. }
  1910. }
  1911. $goods_ids=OrderDetailW::where('order_no',$val->order_no)->groupBy('goods_id')->pluck('goods_id');
  1912. $goods=[];
  1913. foreach($goods_ids as $k=>$v){
  1914. $goods[$k]=StoreGoods::where('id',$v)->select('id','name','img','main_attr')->first();
  1915. $goods[$k]->imgs=str_replace('http://','https://',$goods[$k]->img);
  1916. $goods[$k]->sku=OrderDetailW::where('order_no',$val->order_no)
  1917. ->where('goods_id',$v)->select('price','num')->get();
  1918. }
  1919. $goods_infos=OrderDetailW::with('goods:id,img,name,main_attr')->where('order_no',$val->order_no)
  1920. ->groupBy('goods_id')->select(DB::raw('price','sum(num) as num'))->first();
  1921. $orders[$key]->goods=$goods;
  1922. }
  1923. return $this->success_list($orders,'成功',$count);
  1924. }
  1925. //获取店铺所有介绍人
  1926. public function getAllRecomer(Request $request){
  1927. $input=$request->all();
  1928. $user_id=Auth::user()->id;
  1929. $store_id=Store::where('user_id',$user_id)->value('id');
  1930. $user_ids=OrderW::where('store_id',$store_id)->groupBy('recomer_id')->pluck('recomer_id');
  1931. $user=UserW::whereIn('id',$user_ids);
  1932. $search_name=$input['search_name'];
  1933. if(!empty($search_name)){
  1934. $user->where(function($query)use($search_name){
  1935. $query->where('nickname','like','%'.$search_name.'%')
  1936. ->orWhere('phone','like','%'.$search_name.'%');
  1937. });
  1938. }
  1939. $list=$user->get();
  1940. foreach($list as $key=>$val){
  1941. $uu=User::where('mobile',$val->phone)->first();
  1942. if($uu){
  1943. unset($list[$key]);
  1944. }
  1945. }
  1946. return $this->success($list);
  1947. }
  1948. //绑定介绍人
  1949. public function bindRecomer(Request $request){
  1950. Log::info('打印接收数据');
  1951. Log::info($request->input());
  1952. $user_id=$request->input('id'); //介绍人id
  1953. $order_id=$request->input('order_id'); //订单id
  1954. $order=OrderW::where('id',$order_id)->first();
  1955. if($user_id==$order->user_id){
  1956. return $this->error('450001','介绍人与客户信息相同,不能绑定');
  1957. }
  1958. if(UserW::where('recomer_id',$order->user_id)->exists()){
  1959. return $this->error('450001','该客户已是介绍人,不能绑定');
  1960. }
  1961. $userW=UserW::where('id',$order->user_id)->first();
  1962. $user=User::where('mobile',$userW->phone)->first();
  1963. if($user){
  1964. return $this->error('450001','该客户已存在系统账号,不能绑定');
  1965. }
  1966. $recomW=UserW::where('id',$user_id)->first();//介绍人信息
  1967. if($recomW->recomer_id){
  1968. return $this->error('450001','该介绍人已被其他客户介绍,不能绑定');
  1969. }
  1970. $recomDWBS=User::where('mobile',$recomW->phone)->first();
  1971. if($recomDWBS){
  1972. return $this->error('450001','该介绍人已存在系统账号,不能绑定');
  1973. }
  1974. $store_id=$order->store_id;
  1975. DB::beginTransaction();
  1976. try{
  1977. OrderW::where('user_id',$order->user_id)
  1978. ->where('store_id',$store_id)
  1979. ->update([
  1980. 'recomer_id'=>$user_id,
  1981. 'recomer_status'=>0
  1982. ]);
  1983. UserW::where('id',$order->user_id)->update([
  1984. 'recomer_store_id'=>$store_id,
  1985. 'recomer_id'=>$user_id,
  1986. ]);
  1987. BindRecomLog::create([
  1988. 'user_id'=>$order->user_id,
  1989. 'store_id'=>$store_id,
  1990. 'recomer_id'=>$user_id,
  1991. 'remark'=>'初次绑定',
  1992. ]);
  1993. DB::commit();
  1994. return $this->success([]);
  1995. }catch(\Exception $e){
  1996. DB::rollBack();
  1997. return $this->error('450001','',$e->getMessage());
  1998. }
  1999. }
  2000. //更换绑定介绍人
  2001. public function editBindRecomer(Request $request){
  2002. $user_id=$request->input('id'); //介绍人id
  2003. $order_id=$request->input('order_id'); //订单id
  2004. $remark=$request->input('remark'); //换绑备注
  2005. $order=OrderW::where('id',$order_id)->first();
  2006. if($user_id==$order->user_id){
  2007. return $this->error('450001','介绍人与客户信息相同,不能换绑');
  2008. }
  2009. if(UserW::where('recomer_id',$order->user_id)->exists()){
  2010. return $this->error('450001','该客户已是介绍人,不能换绑');
  2011. }
  2012. $userW=UserW::where('id',$order->user_id)->first();
  2013. if($userW->recomer_agent_id){
  2014. return $this->error('450001','该客户介绍人已存在系统账号,不能换绑');
  2015. }
  2016. if($user_id==$userW->recomer_id){
  2017. return $this->error('450001','介绍人信息没有变更,不能换绑');
  2018. }
  2019. $user=User::where('mobile',$userW->phone)->first();
  2020. if($user){
  2021. return $this->error('450001','该客户已存在系统账号,不能换绑');
  2022. }
  2023. $recomW=UserW::where('id',$user_id)->first();//介绍人信息
  2024. if($recomW->recomer_id){
  2025. return $this->error('450001','该介绍人已被其他客户介绍,不能换绑');
  2026. }
  2027. $recomDWBS=User::where('mobile',$recomW->phone)->first();
  2028. if($recomDWBS){
  2029. return $this->error('450001','该介绍人已存在系统账号,不能换绑');
  2030. }
  2031. $store_id=$order->store_id;
  2032. DB::beginTransaction();
  2033. try{
  2034. OrderW::where('user_id',$order->user_id)
  2035. ->where('store_id',$store_id)
  2036. ->where(function($query){
  2037. $query->whereIn('recomer_status',[0,1])->orWhereNull('recomer_status');
  2038. })
  2039. ->update([
  2040. 'recomer_id'=>$user_id,
  2041. 'recomer_status'=>0
  2042. ]);
  2043. UserW::where('id',$order->user_id)->update([
  2044. 'recomer_store_id'=>$store_id,
  2045. 'recomer_id'=>$user_id,
  2046. ]);
  2047. BindRecomLog::create([
  2048. 'user_id'=>$order->user_id,
  2049. 'store_id'=>$store_id,
  2050. 'recomer_id'=>$user_id,
  2051. 'remark'=>$remark,
  2052. ]);
  2053. DB::commit();
  2054. return $this->success([]);
  2055. }catch(\Exception $e){
  2056. DB::rollBack();
  2057. return $this->error('450001','',$e->getMessage());
  2058. }
  2059. }
  2060. //获取订单列表
  2061. public function getOrderList1(Request $request)
  2062. {
  2063. $input=$request->all();
  2064. $status=$request->input('status','all');
  2065. $page_index=$input['page_index'];
  2066. $page_size=$input['page_size'];
  2067. $order_no=$input['order_no'];
  2068. $num=$page_size*($page_index-1);
  2069. $where=$wheres=[];
  2070. if($status!='all'){
  2071. $where['status']=$status;
  2072. }
  2073. if($order_no){
  2074. if(strlen($order_no)<=3 && is_numeric($order_no)){
  2075. return $this->error('450001','请最少输入4个字搜索');
  2076. }
  2077. $user_ids=UserW::where(function($query)use($order_no){
  2078. $query->where('phone','like','%'.$order_no.'%')->orwhere('nickname','like','%'.$order_no.'%');
  2079. })->pluck('id');
  2080. $wheres=function($query)use($order_no,$user_ids){
  2081. $query->where('order_no','like','%'.$order_no.'%')->orWhereIn('user_id',$user_ids);
  2082. };
  2083. }
  2084. $store=Store::where('user_id',Auth::user()->id)->first();
  2085. $where['store_id']=$store->id;
  2086. $data=OrderW::with(['user:id,nickname,phone','order_cancel'=>function($query){
  2087. $query->where('using',1);}])->where($where)->where($wheres)->whereIn('apply_cancel',[0,1,3])
  2088. ->whereIn('is_refund',[0,1,2])
  2089. ->select('id','order_no','user_id','store_id','status','apply_cancel','is_refund','is_help','self_receive','total','account','is_pay','updated_at','created_at','snapshot')
  2090. ->orderByDesc('updated_at')->orderByDesc('id');
  2091. $count=$data->count();
  2092. $orders=$data->skip($num)->take($page_size)->get();
  2093. foreach($orders as $key=>$val){
  2094. if($val->status==3 && $val->is_refund > 0){
  2095. $order_refund = OrderRefundW::where('order_no',$val->order_no)->get();
  2096. if($order_refund){
  2097. $orders[$key]->refund_goods=$order_refund;
  2098. }
  2099. }
  2100. $orders[$key]->goods=json_decode($val->snapshot,true);
  2101. }
  2102. return $this->success_list($orders,'成功',$count);
  2103. }
  2104. //获取订单列表
  2105. public function getOrderLists(Request $request)
  2106. {
  2107. $input=$request->all();
  2108. $status=$input['status'];
  2109. $page_index=$input['page_index'];
  2110. $page_size=$input['page_size'];
  2111. $order_no=$input['order_no'];
  2112. $num=$page_size*($page_index-1);
  2113. $where=$wheres=[];
  2114. if($status!='all'){
  2115. $where['status']=$status;
  2116. }
  2117. if($order_no){
  2118. $wheres=function($query)use($order_no){
  2119. $query->where('order_no','like','%'.$order_no.'%');
  2120. };
  2121. }
  2122. $store=Store::where('user_id',Auth::user()->id)->first();
  2123. $where['store_id']=$store->id;
  2124. $data=OrderW::with(['user:id,nickname,phone','order_cancel'=>function($query){
  2125. $query->where('using',1);}])->where($where)->where($wheres)->whereIn('apply_cancel',[0,1,3])
  2126. ->whereIn('is_refund',[0,1,2])
  2127. ->orderByDesc('updated_at')->orderByDesc('id');
  2128. $count=$data->count();
  2129. $orders=$data->skip($num)->take($page_size)->get();
  2130. foreach($orders as $key=>$val){
  2131. if($val->status==3 && $val->is_refund > 0){
  2132. $order_refund = OrderRefundW::where('order_no',$val->order_no)->get();
  2133. if($order_refund){
  2134. $orders[$key]->refund_goods=$order_refund;
  2135. }
  2136. }
  2137. $goods=OrderDetailW::with('goods:id,img,name,main_attr')->where('order_id',$val->id)
  2138. ->groupBy('goods_id')->select('goods_id','price',DB::raw('sum(num) as num'))->get();
  2139. foreach($goods as $k=>$v){
  2140. $goods[$k]->goods->imgs=str_replace('http://','https://',$v->goods->img);
  2141. }
  2142. $orders[$key]->goods=$goods;
  2143. }
  2144. return $this->success_list($orders,'成功',$count);
  2145. }
  2146. //获取退货订单详情
  2147. public function getRefundOrderDetail(Request $request){
  2148. $order_no=$request->input('order_no');
  2149. $order_refund=OrderRefundW::where('order_no',$order_no)->get();
  2150. foreach($order_refund as $key => $val){
  2151. $goods_ids = OrderDetailW::where('order_no', $val->refund_no)->groupBy('goods_id')->pluck('goods_id');
  2152. $goods = [];
  2153. foreach ($goods_ids as $k => $v) {
  2154. $goods[$k] = StoreGoods::where('id', $v)->first();
  2155. $goods[$k]->sku = OrderDetailW::where('order_no', $val->refund_no)->where('goods_id', $v)->get();
  2156. }
  2157. $order_refund[$key]->goods=$goods;
  2158. }
  2159. return $this->success($order_refund);
  2160. }
  2161. //获取id订单信息
  2162. public function getOrderInfo(Request $request)
  2163. {
  2164. $input=$request->all();
  2165. $id=$input['id'];
  2166. $order=OrderW::withTrashed()->with(['user:id,nickname,phone','address'])->where('id',$id)->first();
  2167. $goods_ids=OrderDetailW::where('order_no',$order->order_no)->groupBy('goods_id')->pluck('goods_id');
  2168. $goods=[];
  2169. foreach($goods_ids as $key=>$val){
  2170. $goods[$key]=StoreGoods::where('id',$val)->first();
  2171. $goods[$key]->sku=OrderDetailW::where('order_no',$order->order_no)->where('goods_id',$val)->get();
  2172. }
  2173. $order->goods=$goods;
  2174. return $this->success($order);
  2175. }
  2176. //根据订单号查询订单
  2177. public function getOrderNoInfo(Request $request)
  2178. {
  2179. $input=$request->all();
  2180. $order_no=$input['order_no'];
  2181. $order=OrderW::withTrashed()->with(['order_cancel','user:id,nickname,phone,avatar','address','recomer:id,nickname,phone,avatar','store:id,name'])->where('order_no',$order_no)->first();
  2182. $agent_id=Auth::user()->id;
  2183. $store_id=Store::where('user_id',$agent_id)->value('id');
  2184. if($store_id!=$order->store_id){
  2185. return $this->error('450401','没有权限查看该订单');
  2186. }
  2187. if(empty($order)){
  2188. return $this->error('450001','订单号有误');
  2189. }
  2190. $goods_ids=OrderDetailW::where('order_no',$order->order_no)->groupBy('goods_id')->pluck('goods_id');
  2191. $goods=[];
  2192. foreach($goods_ids as $key=>$val){
  2193. $goods[$key]=StoreGoods::where('id',$val)->first();
  2194. $goods[$key]->imgs=str_replace('http://','https://',$goods[$key]->img);
  2195. $goods[$key]->sku=OrderDetailW::where('order_no',$order->order_no)->where('goods_id',$val)->get();
  2196. }
  2197. $order->goods=$goods;
  2198. $order->code=OrderCodeW::where('order_id',$order->id)->get();
  2199. //测试跳转详情显示元宝页面
  2200. if(in_array($order->store_id,[ 99, 102, 105])){//,101,102,105
  2201. $order->is_code=1;
  2202. }else{
  2203. $order->is_code=0;
  2204. }
  2205. return $this->success($order);
  2206. }
  2207. //回复评价
  2208. public function addComment(Request $request){
  2209. $input=$request->all();
  2210. $rules=[
  2211. 'content'=>'required|max:255',
  2212. ];
  2213. $message=[
  2214. 'content.required'=>'评价内容不能为空.',
  2215. 'content.max'=>'评价内容不能大于255个字符.'
  2216. ];
  2217. $validator = Validator::make($input,$rules,$message);
  2218. if($validator->fails()){
  2219. return $this->error('400013',$validator->errors()->first());
  2220. }
  2221. $store_id=Store::where('user_id',Auth::user()->id)->value('id');
  2222. $res=Comment::create([
  2223. 'content'=>$input['content'],
  2224. 'comment_id'=>$input['comment_id'],
  2225. 'store_id'=>$store_id,
  2226. 'is_reply'=>2,
  2227. 'is_see'=>0
  2228. ]);
  2229. if($res){
  2230. return $this->success([]);
  2231. }else{
  2232. return $this->error();
  2233. }
  2234. }
  2235. //查看所有评价
  2236. public function getComment(Request $request){
  2237. $input=$request->all();
  2238. $page_index=$input['page_index'];
  2239. $page_size=$input['page_size'];
  2240. $num=$page_size*($page_index-1);
  2241. $store_id=Store::where('user_id',Auth::user()->id)->value('id');
  2242. $count= Comment::where('store_id',$store_id)->where('comment_id',0)->count();
  2243. $list= Comment::with(['comment','store:id,name,img','user:id,avatar,nickname,phone'])
  2244. ->where('store_id',$store_id)->where('comment_id',0)
  2245. ->orderByDesc('id')
  2246. ->skip($num)->take($page_size)->get();
  2247. return $this->success_list($list,'',$count);
  2248. }
  2249. //用户自提
  2250. public function oneSelfTake(Request $request){
  2251. $order_no=$request->input('order_no');
  2252. $order=OrderW::with('address')->where('order_no',$order_no)->first();
  2253. $order->status=2; //已备货,可自提
  2254. $order->express_time = date("Y-m-d H:i:s"); //备货完成时间
  2255. $res=$order->save();
  2256. if($res){
  2257. /* 已备货通知 */
  2258. $user=UserW::where('id',$order->user_id)->first();
  2259. if($user->openId){
  2260. $address=null;
  2261. if($order->address){
  2262. $address=$order->address->province.$order->address->city.$order->address->area.$order->address->address;
  2263. }
  2264. $mes['openid']=$user->openId;
  2265. $mes['data']=[
  2266. 'first' => '您的订单已完成备货,请前往提货',
  2267. 'keyword1' => $order_no,
  2268. 'keyword2' => $order->created_at,
  2269. 'keyword3' => '已备货,可自提',
  2270. 'keyword4' => $address,
  2271. 'remark' => '点击链接进入系统,查看详情',
  2272. ];
  2273. event(new TipPickUpEvent($mes));
  2274. }
  2275. return $this->success([]);
  2276. }else{
  2277. return $this->error();
  2278. }
  2279. }
  2280. //获取所有快递公司信息
  2281. public function getExpressCode(){
  2282. return $this->success(ExpressHandler::getExpressCode());
  2283. }
  2284. //识别扫码信息
  2285. public function scanCodeInfo(Request $request)
  2286. {
  2287. $code=$request->input('code');
  2288. if(!strpos($code,'://')){
  2289. $info=Select::with('select_good')->select('id','good_select_id','security_code','code','is_lowest')->where('code',$code)->first();
  2290. if(empty($info)){
  2291. return $this->error('450001','未识别到二维码信息');
  2292. }
  2293. if($info->is_lowest==0){
  2294. return $this->error('450001','请扫描单个包装盒二维码');
  2295. }
  2296. if(empty($info->good_select_id)){
  2297. return $this->error('450001','该二维码没有绑定商品信息');
  2298. }
  2299. }else{
  2300. $file=file_get_contents($code);
  2301. Log::info('0000------------------0000--'.$code);
  2302. preg_match('/<span>产品名称:<\/span> 大卫博士健康内裤((.*?))/',$file,$result);
  2303. if(!in_array($result[1],['精装版','简约版','老人版','纯棉老人版','高腰版','青春版'])){
  2304. return $this->error('450001','该二维码没有绑定商品信息');
  2305. }
  2306. switch($result[1]){
  2307. case "精装版":
  2308. $goods_id=1;
  2309. break;
  2310. case "简约版":
  2311. $goods_id=2;
  2312. break;
  2313. case "老人版":
  2314. case "高腰版":
  2315. $goods_id=3;
  2316. break;
  2317. case "纯棉老人版":
  2318. $goods_id=4;
  2319. break;
  2320. case "青春版":
  2321. $goods_id=25;
  2322. break;
  2323. default:
  2324. $goods_id=null;
  2325. }
  2326. $info['good_select_id']=$goods_id;
  2327. $info['security_code']=substr($code,-12);;
  2328. $info['code']=$code;
  2329. }
  2330. $code=substr($code,strrpos($code,'?bid='));
  2331. $order_code = OrderCodeW::where('code',$code)->first();
  2332. if($order_code){
  2333. return $this->error('450001','该二维码已发货');
  2334. }
  2335. return $this->success($info);
  2336. }
  2337. //提交发货扫码信息
  2338. public function submitScanCodeInfo(Request $request)
  2339. {
  2340. $order_no=$request->input('order_no');
  2341. $code=$request->input('code');
  2342. $order=OrderW::where('order_no',$order_no)->first();
  2343. Log::info($code);
  2344. Log::info($order_no);
  2345. DB::beginTransaction();
  2346. try{
  2347. foreach($code as $val){
  2348. if(strpos($val,'://')){
  2349. $order_code = OrderCodeW::where('code',substr($val,strrpos($val,'?bid=')))->first();
  2350. }else{
  2351. $order_code = OrderCodeW::where('code',$val)->first();
  2352. }
  2353. if($order_code){
  2354. if($order_code->order_id != $order->id){
  2355. throw new Exception("二维码:".$order_code->security_code." 已发货");
  2356. }
  2357. }else{
  2358. if(!strpos($val,'://')){
  2359. $info=Select::with('select_good')->select('id','good_select_id','security_code','code','is_lowest')->where('code',$val)->first();
  2360. OrderCodeW::create([
  2361. 'store_id' => $order->store_id,
  2362. 'user_id' => $order->user_id,
  2363. 'order_id' => $order->id,
  2364. 'code' => $val,
  2365. 'security_code' => $info->security_code,
  2366. 'good_select_id' => $info->good_select_id,
  2367. 'good_name' => $info->select_good->good_name,
  2368. ]);
  2369. }else{
  2370. $file=file_get_contents($val);
  2371. preg_match('/<span>产品名称:<\/span> 大卫博士健康内裤((.*?))/',$file,$result);
  2372. if(!in_array($result[1],['精装版','简约版','老人版','纯棉老人版','高腰版','青春版'])){
  2373. throw new Exception("二维码没有绑定商品信息");
  2374. }
  2375. $security_code=substr($val,-12);
  2376. switch($result[1]){
  2377. case "精装版":
  2378. $goods_id=1;
  2379. break;
  2380. case "简约版":
  2381. $goods_id=2;
  2382. break;
  2383. case "老人版":
  2384. case "高腰版":
  2385. $goods_id=3;
  2386. break;
  2387. case "纯棉老人版":
  2388. $goods_id=4;
  2389. break;
  2390. case "青春版":
  2391. $goods_id=25;
  2392. break;
  2393. default:
  2394. $goods_id=null;
  2395. }
  2396. OrderCodeW::create([
  2397. 'store_id' => $order->store_id,
  2398. 'user_id' => $order->user_id,
  2399. 'order_id' => $order->id,
  2400. 'code' => substr($val,strrpos($val,'?bid=')),
  2401. 'security_code' => $security_code,
  2402. 'good_select_id' => $goods_id,
  2403. 'good_name' => '大卫博士'.$result[1],
  2404. ]);
  2405. }
  2406. }
  2407. }
  2408. DB::commit();
  2409. return $this->success([]);
  2410. }catch(\Exception $e){
  2411. DB::rollBack();
  2412. Log::info($e->getMessage());
  2413. return $this->error($e->getMessage());
  2414. }
  2415. }
  2416. // 删除发货码
  2417. public function deleteScanCode(Request $request)
  2418. {
  2419. $ids=$request->input('ids');
  2420. $res=OrderCodeW::whereIn('id',$ids)->delete();
  2421. if($res){
  2422. return $this->success([]);
  2423. }else{
  2424. return $this->error();
  2425. }
  2426. }
  2427. //获取客户所有订单
  2428. public function getNoSendOrder(Request $request){
  2429. $order_no=$request->input('order_no');
  2430. $order=OrderW::where('order_no',$order_no)->first();
  2431. $no_send_orders=OrderW::with('user:id,nickname,phone,avatar')
  2432. ->where('store_id',$order->store_id)->where('user_id',$order->user_id)
  2433. ->where('status',1)->whereIn('apply_cancel',[0,3])->get();
  2434. foreach($no_send_orders as $key=>$val){
  2435. $goods_ids=OrderDetailW::where('order_no',$val->order_no)->groupBy('goods_id')->pluck('goods_id');
  2436. $goods=[];
  2437. foreach($goods_ids as $k=>$v){
  2438. $goods[$k]=StoreGoods::where('id',$v)->first();
  2439. $goods[$k]->sku=OrderDetailW::where('order_no',$val->order_no)->where('goods_id',$v)->get();
  2440. }
  2441. $no_send_orders[$key]->goods=$goods;
  2442. }
  2443. return $this->success($no_send_orders);
  2444. }
  2445. //上传订单快递号
  2446. public function addOrderExpressNo(Request $request){
  2447. $input=$request->all();
  2448. $rules=[
  2449. 'express_no' =>'required',
  2450. 'express_company'=>'required',
  2451. 'express_time' =>'required'
  2452. // 'address_id' =>'required'
  2453. ];
  2454. $message=[
  2455. 'express_no.required' =>'发货单号不能为空.',
  2456. 'express_company.required'=>'物流公司不能为空.',
  2457. 'express_time.required' =>'发货时间不能为空.',
  2458. // 'address_id.required' =>'收货地址不能为空.',
  2459. ];
  2460. $validator = Validator::make($input,$rules,$message);
  2461. if($validator->fails()){
  2462. return $this->error('400013',$validator->errors()->first());
  2463. }
  2464. $status=OrderW::where('order_no',$input['order_no'])->value('status');
  2465. if($status > 1){
  2466. return $this->error('450001','该订单已发货');
  2467. }
  2468. $prefix=mb_substr($input['express_no'],0,2);
  2469. if(strtoupper($prefix)=='DD' || strtoupper($prefix)=='DX'){
  2470. return $this->error('450001','物流单号不正确,你填的是订单号,请重新填写');
  2471. }
  2472. $order_nos=explode(',',$input['order_no']);
  2473. // $address_id=$input['address_id'];
  2474. $express_company=$input['express_company'];
  2475. $express_code=$input['express_code'];
  2476. $express_phone=$input['express_phone'];
  2477. $express_time=$input['express_time'];
  2478. $express_no=str_replace(' ','', $input['express_no']);
  2479. $data=[
  2480. // 'address_id'=>$address_id,
  2481. 'self_receive'=>0,
  2482. 'express_no'=>$express_no,
  2483. 'express_company'=>$express_company,
  2484. 'express_code'=>$express_code,
  2485. 'express_state'=>0,
  2486. 'express_phone'=>$express_phone,
  2487. 'express_time'=>$express_time,
  2488. 'status'=>2
  2489. ];
  2490. try{
  2491. DB::connection('mysql_w')->transaction(function()use($order_nos,$data){
  2492. OrderW::whereIn('order_no',$order_nos)->update($data);
  2493. },5);
  2494. $order=OrderW::with('address')->where('order_no',$order_nos[0])->first();
  2495. $user=UserW::where('id',$order->user_id)->first();
  2496. //已发货短信通知
  2497. $phone=$user->phone;
  2498. $config=config('easysms');
  2499. $easySms = new EasySms($config);
  2500. $express=$express_company.$express_no;
  2501. $content="【大卫博士】您购买的大卫博士健康内裤已发货,快递单号是{$express},请注意查收。";
  2502. try{
  2503. $easySms->send($phone, ['content' =>$content ]);
  2504. Log::info('物流发货短信正常:'.$phone);
  2505. } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
  2506. $message = $exception->getException('qcloud')->getMessage();
  2507. Log::info('物流发货短信异常:'.json_encode($message));
  2508. }
  2509. //判断是否需要通知满5套
  2510. // event(new EnoughFive($order->id));
  2511. //已发货公众号通知
  2512. if($user->openId){
  2513. Log::info($user->openId);
  2514. if($order->address){
  2515. $address=$order->address->name.' '.$order->address->phone.' '.$order->address->province.$order->address->city.$order->address->area.$order->address->address;
  2516. }else{
  2517. $address='无收件信息';
  2518. }
  2519. $mes['openid']=$user->openId;
  2520. $mes['data']=[
  2521. 'first' => '您的订单已发货,请注意查收',
  2522. 'keyword1' => $input['order_no'],
  2523. 'keyword2' => $express_time,
  2524. 'keyword3' => $express_company,
  2525. 'keyword4' => $express_no,
  2526. 'keyword5' => $address,
  2527. ];
  2528. Log::info(json_encode($mes));
  2529. event(new TipSendingEvent($mes));
  2530. }
  2531. return $this->success([]);
  2532. }catch(\Exception $e){
  2533. return $this->error('450001',$e->getMessage());
  2534. }
  2535. // OrderExpressW::create([
  2536. // 'order_no'=>$input['order_no'],
  2537. // 'express_no'=>str_replace(' ','', $input['express_no']),
  2538. // 'express_company'=>$input['express_company'],
  2539. // 'express_code'=>$input['express_code'],
  2540. // 'express_state'=>0,
  2541. // 'express_phone'=>$input['express_phone'],
  2542. // 'express_receive_time'=>null,
  2543. // ]);
  2544. }
  2545. //查询订单物流信息
  2546. public function getOrderExpress(Request $request){
  2547. $order_no=$request->input('order_no');
  2548. $order=OrderW::with('address')->where('order_no',$order_no)->first();
  2549. if(empty($order)){
  2550. return $this->error('450001','订单信息有误');
  2551. }
  2552. if($order->express_state==3){ //已签收
  2553. $express=json_decode($order->express_info,true);
  2554. }else{ //未签收
  2555. $CustomerName = null;
  2556. if($order->express_code =='SF'){
  2557. $CustomerName = substr($order->express_phone,-4);
  2558. }
  2559. $express=new ExpressHandler();
  2560. $express=$express->search($order->express_no, $order->express_code, $order->order_no, $CustomerName);
  2561. $express=json_decode(json_encode($express),'true');
  2562. if(isset($express['Traces']) && !empty($express['Traces'])){
  2563. foreach ($express['Traces'] as $key=>$val){
  2564. $express['Traces'][$key]['AcceptTime']=date("Y-m-d H:i:s",substr($val['AcceptTime'],0,strlen($val['AcceptTime'])-3));
  2565. }
  2566. OrderW::where('order_no',$order_no)->update([
  2567. 'express_info'=>json_encode($express),
  2568. 'express_receive_time'=>array_pop($express['Traces'])['AcceptTime'],
  2569. 'express_state'=>$express['State']
  2570. ]);
  2571. }
  2572. }
  2573. $data['express']=$express;
  2574. $data['express_state']=$order->express_state;
  2575. $data['express_code']=$order->express_code;
  2576. $data['express_company']=$order->express_company;
  2577. $data['express_no']=$order->express_no;
  2578. $data['express_time']=$order->express_time;
  2579. $data['express_phone']=$order->express_phone;
  2580. return $this->success($data);
  2581. }
  2582. //修改店铺地址
  2583. public function editStoreAddress(Request $request){
  2584. $user_id=Auth::user()->id;
  2585. // $user=User::where('id',$user_id)->first();
  2586. // $store_id=Store::where('user_id',$user_id)->value('id');
  2587. $res=Store::where('user_id',$user_id)
  2588. ->update([
  2589. 'province'=>$request->input('province'),
  2590. 'city'=>$request->input('city'),
  2591. 'area'=>$request->input('area'),
  2592. 'address'=>$request->input('address'),
  2593. ]);
  2594. // $address=AddressW::where('store_id',$store_id)->first();
  2595. // if($address){
  2596. // $address->province=$request->input('province');
  2597. // $address->city=$request->input('city');
  2598. // $address->area=$request->input('area');
  2599. // $address->address=$request->input('address');
  2600. // $res=$address->save();
  2601. // }else{
  2602. // $res=AddressW::create([
  2603. // 'store_id'=>$store_id,
  2604. // 'name'=>$user->nickname,
  2605. // 'phone'=>$user->mobile,
  2606. // 'province'=>$request->input('province'),
  2607. // 'city'=>$request->input('city'),
  2608. // 'area'=>$request->input('area'),
  2609. // 'address'=>$request->input('address'),
  2610. // 'is_default'=>0
  2611. // ]);
  2612. // }
  2613. if($res){
  2614. return $this->success([]);
  2615. }else{
  2616. return $this->error();
  2617. }
  2618. }
  2619. //获取微信位置授权
  2620. public function getSignPackage(){
  2621. $app=app('wechat.official_account');
  2622. $url='http://api.app.cliu.cc/api/gzh';
  2623. $app->jssdk->setUrl($url);
  2624. $data=$app->jssdk->buildConfig(
  2625. array('onMenuShareAppMessage','onMenuShareTimeline',
  2626. 'openLocation','getLocation','chooseImage',
  2627. 'uploadImage', 'downloadImage','scanQRCode',
  2628. 'updateAppMessageShareData', 'updateTimelineShareData','hideMenuItems'),
  2629. $debug = false, $beta = false, $json = true,
  2630. $openTagList = ['wx-open-launch-app']);
  2631. return $this->success($data);
  2632. }
  2633. //根据经纬度获取地理位置信息
  2634. public function getAddressInfo(Request $request){
  2635. $location=$request->input('location');
  2636. $key='b019688375d389c1e55150985e90f816';
  2637. $client=new Client();
  2638. $url='https://restapi.amap.com/v3/geocode/regeo?key='.$key.'&location='.$location;
  2639. $result=$client->request('GET',$url);
  2640. $res = json_decode($result->getBody()->getContents());
  2641. return $this->success($res);
  2642. }
  2643. //提交退货订单
  2644. public function submitRefundOrder(Request $request)
  2645. {
  2646. $sku = $request->input('sku');
  2647. $reason = $request->input('reason');
  2648. $reason_all = $request->input('reason_all');
  2649. $img = $request->input('img');
  2650. $origin_order_no=$request->input('origin_order_no');
  2651. $origin_order=OrderW::where('order_no',$origin_order_no)->first();
  2652. if($origin_order->is_refund==1){
  2653. return $this->error('450001','该订单存在正在审核中的退货订单');
  2654. }
  2655. if($origin_order->is_refund==3){
  2656. return $this->error('450001','该订单已全部退货');
  2657. }
  2658. $refund_no=OrderW::order_no('th');
  2659. $account = $total = 0;
  2660. $list = $sku_ids = [];
  2661. foreach($sku as $key=>$val){
  2662. if($val[1] > 0){
  2663. $order_detail=OrderDetailW::with(['goods'])->where('id',$val[0])->first();
  2664. if($order_detail->num < $val[1]){
  2665. $num = $order_detail->num;
  2666. }else{
  2667. $num = $val[1];
  2668. }
  2669. $list[$key]['num']=$num;
  2670. $list[$key]['price']=$order_detail->price;
  2671. $list[$key]['account']=$order_detail->price * $num;
  2672. $list[$key]['order_no']=$refund_no;
  2673. $list[$key]['goods_id']=$order_detail->goods_id;
  2674. $list[$key]['sku_id']=$order_detail->sku_id;
  2675. $list[$key]['size']=$order_detail->size;
  2676. $list[$key]['type']=$order_detail->type;
  2677. $list[$key]['style']=2;
  2678. $list[$key]['created_at']=date("Y-m-d H:i:s");
  2679. $list[$key]['updated_at']=date("Y-m-d H:i:s");
  2680. $total += $num;
  2681. $account += $order_detail->price * $num;
  2682. }
  2683. }
  2684. $data=[
  2685. 'refund_no'=>$refund_no,
  2686. 'order_no'=>$origin_order_no,
  2687. 'store_id'=>$origin_order->store_id,
  2688. 'user_id'=>$origin_order->user_id,
  2689. 'reason'=>$reason,
  2690. 'reason_all'=>$reason_all,
  2691. 'total'=>$total,
  2692. 'account'=>$account,
  2693. 'img'=>json_encode($img),
  2694. 'status'=>0,
  2695. 'op_name'=> Auth::user()->nickname,
  2696. 'op_type'=>2,
  2697. 'remark'=>null,
  2698. ];
  2699. try{
  2700. DB::connection('mysql_w')->transaction(function ()use($list,$data,$origin_order_no,$account){
  2701. OrderDetailW::insert($list);
  2702. OrderRefundW::create($data);
  2703. OrderW::where('order_no',$origin_order_no)->update(['is_refund'=>1]);
  2704. },5);
  2705. //申请退货提醒
  2706. $user=UserW::where('id',$origin_order->user_id)->first();
  2707. if($user->openId){
  2708. $data['openid']=$user->openId;
  2709. $data['data']=[
  2710. 'first' => '商户提交了新的退单信息',
  2711. 'keyword1' => $origin_order->order_no,
  2712. 'keyword2' => '商户退货,退款金额:'.number_format($account,2)
  2713. ];
  2714. event(new AuthRefundTip($data));
  2715. }
  2716. return $this->success($refund_no);
  2717. }catch(\Exception $e){
  2718. return $this->error('450001',$e->getMessage());
  2719. }
  2720. }
  2721. //获取退款订单原始信息
  2722. public function getOriginOrderInfo(Request $request)
  2723. {
  2724. $order_no=$request->input('order_no');
  2725. $data=OrderW::with(['address','order_cancel'=>function($query){$query->where('using',1);},'store:id,name,img,phone']);
  2726. $order=$data->where('order_no',$order_no)->first()->toArray();
  2727. if(empty($order)){
  2728. return $this->error('450001','订单不存在');
  2729. }
  2730. $goods_ids=OrderDetailW::where('order_no',$order['order_no'])->groupBy('goods_id')->pluck('goods_id');
  2731. $goods=[];
  2732. foreach($goods_ids as $key=>$val){
  2733. $goods[$val]=StoreGoods::select('id','name','img','main_attr')->where('id',$val)->first()->toArray();
  2734. $order_details=OrderDetailW::where('order_no',$order['order_no'])->where('goods_id',$val)->get()->toArray();
  2735. if(count($order_details)>0){
  2736. $sku=[];
  2737. foreach($order_details as $k=>$v){
  2738. $sku[$v['sku_id']]=$v;
  2739. }
  2740. $goods[$val]['sku']=$sku;
  2741. $goods[$val]['price']=$order_details[0]['price'];
  2742. }
  2743. }
  2744. $order['goods']=$goods;
  2745. $refund_nos=OrderRefundW::where('order_no',$order_no)->whereIn('status',[0,1,3])->pluck('refund_no');
  2746. $refund_goods_ids=OrderDetailW::whereIn('order_no',$refund_nos)->groupBy('goods_id')->pluck('goods_id');
  2747. $refunds=[];
  2748. foreach($refund_goods_ids as $key=>$val){
  2749. $refunds[$val]=StoreGoods::select('id','name','img','main_attr')->where('id',$val)->first()->toArray();
  2750. $refunds_details=OrderDetailW::select('goods_id','sku_id','type','size', DB::raw('sum(num) as num'))
  2751. ->whereIn('order_no',$refund_nos)->where('goods_id',$val)->groupBy('sku_id')->get()->toArray();
  2752. if(count($refunds_details)>0){
  2753. $sku=[];
  2754. foreach($refunds_details as $k=>$v){
  2755. $sku[$v['sku_id']]=$v;
  2756. }
  2757. $refunds[$val]['sku']=$sku;
  2758. }
  2759. }
  2760. $order['refunds']=$refunds;
  2761. $products=[];
  2762. foreach($goods as $key=>$val){
  2763. $products[$key]= $goods[$key];
  2764. $sku=[];
  2765. foreach($val['sku'] as $k=>$v){
  2766. if(empty($refunds[$key]['sku'][$k])){
  2767. unset($products[$key]['sku']);
  2768. $sku[$k]= $goods[$key]['sku'][$k];
  2769. }else{
  2770. if($goods[$key]['sku'][$k]['num'] > $refunds[$key]['sku'][$k]['num']){
  2771. unset($products[$key]['sku']);
  2772. $sku[$k] = $goods[$key]['sku'][$k];
  2773. $sku[$k]['num']=$goods[$key]['sku'][$k]['num'] - $refunds[$key]['sku'][$k]['num'];
  2774. }else{
  2775. unset($products[$key]['sku']);
  2776. }
  2777. }
  2778. }
  2779. $products[$key]['sku']= $sku;
  2780. if(empty($products[$key]['sku'])){
  2781. unset($products[$key]);
  2782. }
  2783. }
  2784. $order['products']=$products;
  2785. return $this->success($order);
  2786. }
  2787. //上传退货图片
  2788. public function uploadRefundOrderImg(Request $request){
  2789. $path_url = 'public/refund';
  2790. $path = $request->file('img')->store($path_url);
  2791. if($path){
  2792. $url = env('APP_URL'). Storage::url($path);
  2793. return $this->success($url);
  2794. }
  2795. return $this->error();
  2796. }
  2797. //取消退货审核
  2798. public function cancelRefundOrder(Request $request){
  2799. $refund_id=$request->input('refund_id');
  2800. $refund=OrderRefundW::where('id',$refund_id)->first();
  2801. if(empty($refund)){
  2802. return $this->error('450001','退货单不存在');
  2803. }
  2804. try{
  2805. DB::connection('mysql_w')->transaction(function()use($refund){
  2806. OrderRefundW::where('id',$refund->id)->delete();
  2807. $auth_count=OrderRefundW::where('order_no',$refund->order_no)->where('status',0)->count();
  2808. if($auth_count>=1){
  2809. OrderW::where('order_no',$refund->order_no)->update([
  2810. 'is_refund'=>1
  2811. ]);
  2812. }else{
  2813. $count=OrderRefundW::where('order_no',$refund->order_no)->where('status',1)->count();
  2814. if($count>=1){
  2815. OrderW::where('order_no',$refund->order_no)->update([
  2816. 'is_refund'=>2
  2817. ]);
  2818. }else{
  2819. OrderW::where('order_no',$refund->order_no)->update([
  2820. 'is_refund'=>0
  2821. ]);
  2822. }
  2823. }
  2824. },5);
  2825. return $this->success([]);
  2826. }catch(\Exception $e){
  2827. return $this->error('450001',$e->getMessage());
  2828. }
  2829. }
  2830. //上传签名图片
  2831. public function uploadQianMingImg(Request $request){
  2832. $base64_img=$request->input('img');
  2833. Log::error('签名');
  2834. // preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64_img,$res);
  2835. // $base64_img=base64_decode(str_replace($res[1],'', $base64_img));
  2836. // 获取七牛云配置信息
  2837. $config = [
  2838. 'driver' => 'qiniu',
  2839. 'domain' => 'http://image.chuliu.cc', //七牛域名
  2840. 'access_key' => 'cIbo9JAqHBgO6uw97gT5fJOlEHiTiGrpB84unVEn', //AccessKey
  2841. 'secret_key' => '4f0nTNNrvzOsFc6Rz0GC5DntiJRnk2GcfkyJFNQp', //SecretKey
  2842. 'bucket' => 'daweiboshi-app', //Bucket名字
  2843. 'dirname'=> 'daweiboshi-weidian'
  2844. ];
  2845. $auth = new Auths($config['access_key'], $config['secret_key']);
  2846. // 生成上传 Token
  2847. $token = $auth->uploadToken($config['bucket']);
  2848. $filename=$this->randomKeys(18).'.jpg';
  2849. $dirname=$config['dirname'];
  2850. // 初始化 UploadManager 对象并进行文件的上传。
  2851. $uploadMgr = new UploadManager();
  2852. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  2853. $pathname= $dirname.'/qianming/'.date('Ymd').'/'.$filename;
  2854. try{
  2855. $uploadMgr->putFile($token, $pathname, $base64_img);
  2856. }catch(\Exception $e){
  2857. return $this->error('450001',$e->getMessage(),$e->getMessage());
  2858. }
  2859. // 获取文件完整路径
  2860. $url = $config['domain'].'/'.$pathname;
  2861. if ($url){
  2862. return $this->success($url);
  2863. }
  2864. return $this->error();
  2865. }
  2866. function randomKeys($length){
  2867. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
  2868. $key='';
  2869. for($i=0;$i<$length;$i++)
  2870. {
  2871. $key .= $pattern{mt_rand(0,strlen($pattern)-1)};//生成php随机数
  2872. }
  2873. return $key;
  2874. }
  2875. //商户确认收货
  2876. public function agentSureReceiveOrder(Request $request){
  2877. $order_no=$request->input('order_no');
  2878. $order=OrderW::where('order_no',$order_no)->first();
  2879. if($order->status==3){
  2880. return $this->error('450001','订单已完成');
  2881. }
  2882. try{
  2883. DB::connection('mysql_w')->transaction(function()use($order_no,$order,$request){
  2884. $order->status=3;
  2885. $order->order_status=2;
  2886. $order->self_receive=0;//收货
  2887. $order->express_receive_time=date("Y-m-d H:i:s");//订单签收时间
  2888. $order->save();
  2889. },5);
  2890. // event(new FinishAccount($order_no));
  2891. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  2892. if($order->pay_type != 3){
  2893. event(new FinishAccount($order_no));
  2894. }else{
  2895. OrderW::where('order_no',$order->order_no)->update(['is_finish'=>1,'finish_no'=>$order->order_no]);
  2896. }
  2897. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  2898. $user=UserW::where('id',$order->user_id)->first();
  2899. //已签收短信通知
  2900. $phone=$user->phone;
  2901. $config=config('easysms');
  2902. $easySms = new EasySms($config);
  2903. $content="【大卫博士】您购买的大卫博士健康内裤被商户标记为确认签收。若已收到货物,请忽略此短信;若没有收到,请及时联系商户查询。 ";
  2904. // $content="【大卫博士】您购买的大卫博士健康内裤被代理标记为确认签收。若已收到货物,请忽略此短信;若没有收到,请及时联系代理商查询。 ";
  2905. try{
  2906. $easySms->send($phone, ['content' =>$content ]);
  2907. } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
  2908. $message = $exception->getException('qcloud')->getMessage();
  2909. Log::info('确认收货短信异常:'.json_encode($message));
  2910. }
  2911. return $this->success([]);
  2912. }catch(\Exception $e){
  2913. Log::info('签收失败2---'.json_encode($e->getMessage()));
  2914. return $this->error('450001',$e->getMessage());
  2915. }
  2916. }
  2917. //商户端确认收货,客户当面签收
  2918. public function sureReceiveOrder(Request $request){
  2919. $signature=$request->input('signature');
  2920. $order_nos=explode(',',$request->input('order_no'));
  2921. $ors=OrderW::whereIn('order_no',$order_nos)->where('status',3)->get();
  2922. Log::info(';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;');
  2923. Log::info($ors);
  2924. if(count($ors)>0){
  2925. return $this->error('450001','存在已完成订单');
  2926. }
  2927. Log::info(json_encode($request->input()));
  2928. try{
  2929. $order=OrderW::where('order_no',$order_nos[0])->first();
  2930. $user=UserW::where('id',$order->user_id)->first();
  2931. DB::connection('mysql_w')
  2932. ->transaction(function()use($order_nos,$order,$user,$signature,$request){
  2933. $info['status']=3;
  2934. $info['order_status']=0;
  2935. $info['self_receive']=1;//送货上门
  2936. $info['express_receive_time']=date("Y-m-d H:i:s");//订单签收时间
  2937. $info['signature']=$signature;
  2938. //以下单所填地址为准
  2939. // if(empty($order->address_id)){
  2940. // $province=$request->input('province');
  2941. // if(is_array($province) && !empty($province)){
  2942. // $province=implode('',$province);
  2943. // }
  2944. // if(is_array($province) && empty($province)){
  2945. // $province='';
  2946. // }
  2947. // $city=$request->input('city');
  2948. // if(is_array($city) && !empty($city)){
  2949. // $city=implode('',$city);
  2950. // }
  2951. // if(is_array($city) && empty($city)){
  2952. // $city='';
  2953. // }
  2954. // $area=$request->input('area');
  2955. // if(is_array($area) && !empty($area)){
  2956. // $area=implode('',$area);
  2957. // }
  2958. // if(is_array($area) && empty($area)){
  2959. // $area='';
  2960. // }
  2961. // Log::info('dddd-------dddd'.$province.'---'.$city.'---'.$area);
  2962. //
  2963. // $address=AddressW::where('user_id',$order->user_id)
  2964. // ->where('province',$province)->where('city',$city)
  2965. // ->where('area',$area)->first();
  2966. // if($address){
  2967. // $info['address_id']=$address->id;
  2968. // }else{
  2969. // $res=AddressW::create([
  2970. // 'name'=>$user->nickname,
  2971. // 'phone'=>$user->phone,
  2972. // 'user_id'=>$order->user_id,
  2973. // 'store_id'=>$order->store_id,
  2974. // 'province'=>$province,
  2975. // 'city'=>$city,
  2976. // 'area'=>$area,
  2977. // 'type'=>3,
  2978. // 'is_default'=>0,
  2979. // 'remark'=>'自动定位',
  2980. // ]);
  2981. // $info['address_id']=$res->id;
  2982. // }
  2983. // }
  2984. OrderW::whereIn('order_no',$order_nos)->update($info);
  2985. },5);
  2986. foreach($order_nos as $val){
  2987. // event(new FinishAccount($val));
  2988. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  2989. $oro=OrderW::where('order_no',$val)->first();
  2990. if($oro->pay_type != 3){
  2991. event(new FinishAccount($val));
  2992. }else{
  2993. OrderW::where('order_no',$val)->update(['is_finish'=>1,'finish_no'=>$val]);
  2994. }
  2995. // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
  2996. }
  2997. //已签收短信通知
  2998. $phone=$user->phone;
  2999. $config=config('easysms');
  3000. $easySms = new EasySms($config);
  3001. $content="【大卫博士】您购买的大卫博士健康内裤已由你本人确认签收,谢谢。";
  3002. try{
  3003. $easySms->send($phone, ['content' =>$content ]);
  3004. Log::info('签收短信正常:'.$phone);
  3005. } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
  3006. $message = $exception->getException('qcloud')->getMessage();
  3007. Log::info('签收短信异常:'.json_encode($message));
  3008. }
  3009. //判断是否需要通知满5套
  3010. // event(new EnoughFive($order->id));
  3011. //签收公众号提醒
  3012. if($user->openId){
  3013. $data['openid']=$user->openId;
  3014. $data['data']=[
  3015. 'first' => '您的订单已被当面签收,如果没有请联系商户',
  3016. 'keyword1' => $request->input('order_no'),
  3017. 'keyword2' => date('Y-m-d H:i:s')
  3018. ];
  3019. event(new ReceivingEvent($data));
  3020. }
  3021. return $this->success([]);
  3022. }catch(\Exception $e){
  3023. Log::info('签收失败1---'.$e->getMessage());
  3024. return $this->error('450001',$e->getMessage());
  3025. }
  3026. }
  3027. //获取客户信息
  3028. public function getUser(Request $request){
  3029. $user_id=$request->input('user_id');
  3030. $user=UserW::where('id',$user_id)->first();
  3031. $store_id=Store::where('user_id',Auth::user()->id)->value('id');
  3032. $vip_log=VipLogW::where('user_id',$user->id)->where('store_id',$store_id)->first();
  3033. if($vip_log){
  3034. $user->is_vip=1;
  3035. }else{
  3036. $user->is_vip=0;
  3037. }
  3038. $orders=OrderW::where('store_id',$store_id)->where('user_id',$user_id)->get();
  3039. if(count($orders)==0){//判断客户在店铺有没有单子
  3040. $user->has_order=0;
  3041. }else{
  3042. $user->has_order=1;
  3043. }
  3044. if($user){
  3045. return $this->success($user);
  3046. }else{
  3047. return $this->error();
  3048. }
  3049. }
  3050. //修改客户信息
  3051. public function editUser(Request $request){
  3052. $rules=[
  3053. 'name'=>'required|max:32',
  3054. 'nickname'=>'required|max:32',
  3055. 'remark'=>'max:255',
  3056. 'phone'=>'required|regex:/^1[3456789][0-9]{9}$/'
  3057. ];
  3058. $message=[
  3059. 'phone.required'=>'手机号不能为空.',
  3060. 'phone.regex'=>'手机号格式有误.',
  3061. 'name.required'=>'客户姓名不能为空.',
  3062. 'name.max'=>'客户姓名最多32个字符.',
  3063. 'nickname.required'=>'客户昵称不能为空.',
  3064. 'nickname.max'=>'客户昵称最多32个字符.',
  3065. 'remark.max'=>'备注最多255个字符.',
  3066. ];
  3067. $validator = Validator::make($request->input(),$rules,$message);
  3068. if($validator->fails()){
  3069. return $this->error('400013',$validator->errors()->first());
  3070. }
  3071. $user_id=$request->input('user_id');
  3072. $res=UserW::where('id',$user_id)->update([
  3073. 'name'=>$request->input('name'),
  3074. 'phone'=>$request->input('phone'),
  3075. 'nickname'=>$request->input('nickname'),
  3076. 'sex'=>$request->input('sex'),
  3077. 'remark'=>$request->input('remark'),
  3078. ]);
  3079. if($res){
  3080. return $this->success([]);
  3081. }else{
  3082. return $this->error();
  3083. }
  3084. }
  3085. //获取所有快递公司信息
  3086. public function getExpressCompany(Request $request){
  3087. $express=new ExpressHandler();
  3088. $expressCode=$request->input('expressCode');
  3089. $res=$express->getExpressCompany($expressCode);
  3090. return $this->success($res);
  3091. }
  3092. //咨询客服,获取政委信息
  3093. public function getServiceInfo()
  3094. {
  3095. $user_id=Auth::user()->id;
  3096. $user=User::where('id',$user_id)->first();
  3097. $admin_id=Warea::where('id',$user->warea_id)->value('admin_id');
  3098. $admin=Admin::where('id',$admin_id)->first();
  3099. return $this->success($admin);
  3100. }
  3101. //通过手机号获取证件图片
  3102. public function getStoreCard(Request $request){
  3103. // DB::connection()->enableQueryLog(); // 开启QueryLog
  3104. $data=Store::query();
  3105. $data->with('useraa:id,nickname,mobile,level,agent_id,crown_id,warea_id,deleted_at');
  3106. $data->whereHas('useraa',function($query){
  3107. $query->where('status',0)->where('cert_status',6)->whereNull('deleted_at')->where('service_status',0);
  3108. });
  3109. // $data->orderByDesc('user_id')->limit('1');
  3110. return $data->count();
  3111. // return $data->get();
  3112. // \App\User::find(1);
  3113. // return DB::getQueryLog();
  3114. // $dwbs=UserZ::select(DB::raw('count(*) as count'),'dwbs_id')->groupBy('dwbs_id')->having('count','>',1)->pluck('dwbs_id');
  3115. //// return $dwbs;
  3116. // foreach($dwbs as $key=>$val){
  3117. // if(!empty($val)){
  3118. // $users=UserZ::where('dwbs_id',$val)->get();
  3119. // foreach($users as $k=>$v){
  3120. // $enroll=ZbsEnroll::query()->where('uid',$v->id)->where('season',31)->where('status',0)->first();
  3121. // if($enroll){
  3122. // $store_id=Store::where('user_id',$val)->value('id');
  3123. // $list[$val.'--'.$store_id] = IntegralW::where('store_id',$store_id)->where('created_at','>','2021-09-13 00:00:00')->where('integral','>',0)->get();
  3124. // }
  3125. // }
  3126. // }
  3127. // }
  3128. //
  3129. // return $this->success($list);
  3130. //
  3131. // foreach($list as $key=>$val){
  3132. // if(count($val)>0){
  3133. // foreach($val as $k=>$v){
  3134. // DB::transaction(function()use($v){
  3135. // IntegralW::query()->where('id',$v->id)->update([
  3136. // 'integral'=>0,
  3137. // 'integral_double'=>0,
  3138. // 'is_zbs'=>1
  3139. // ]);
  3140. // $store = Store::where('id',$v->store_id)->first();
  3141. // $store->integral-=$v->integral;
  3142. // $store->cycle_inte-=$v->integral;
  3143. // $store->save();
  3144. //
  3145. // OrderW::where('order_no',$v->order_no)->update([
  3146. // 'is_zbs'=>1
  3147. // ]);
  3148. // },5);
  3149. // }
  3150. // }
  3151. // }
  3152. // return $this->success($list);
  3153. // $phone=$request->input('phone');
  3154. // $data=Store::where('phone',$phone)->select('name','phone','cardImg1','cardImg2')->first();
  3155. // $list['name']=$data->name;
  3156. // $list['phone']=$data->phone;
  3157. // $list['cardImg1']=env('APP_URL').$data->cardImg1;
  3158. // $list['cardImg2']=env('APP_URL').$data->cardImg2;
  3159. // return $this->success($list);
  3160. }
  3161. // public function changeInfo(Request $request){
  3162. // $type=$request->input('type');
  3163. // $n=$request->input('n');
  3164. //
  3165. // $ids=Amount::select('id','order_no','transaction_id',DB::raw('count(id) as c'))
  3166. // ->groupBy('user_id','store_id','order_no','transaction_id','type','status','status_code','money','remark')
  3167. // ->having('c','>',$n)
  3168. // ->pluck('id');
  3169. // if($type == 1){
  3170. // return $ids;
  3171. // }
  3172. // if($type==2){
  3173. // $a=Amount::query()->whereIn('id',$ids)->update(['deleted_at'=>date('Y-m-d H:i:s')]);
  3174. // return $a;
  3175. // }
  3176. // }
  3177. }