PublicRpcService.php 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794
  1. <?php
  2. namespace App\JsonRpc;
  3. // use ___PHPSTORM_HELPERS\object;
  4. use _PHPStan_62c6a0a8b\OndraM\CiDetector\Env;
  5. use App\Model\BlackWord;
  6. use App\Model\Department;
  7. use App\Model\District;
  8. use App\Model\LetterOfComplaint;
  9. use App\Model\LetterType;
  10. use App\Model\LevelUser;
  11. use App\Model\UserLevel;
  12. use App\Tools\Result;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\Di\Annotation\Inject;
  15. use Hyperf\RpcServer\Annotation\RpcService;
  16. use App\Service\MinioService;
  17. use Hyperf\Redis\Redis;
  18. use Overtrue\ChineseCalendar\Calendar;
  19. use App\Model\TemplateClass;
  20. use App\Model\Template;
  21. use App\Model\WebsiteTemplate;
  22. use App\Model\WebsiteTemplateInfo;
  23. use App\Model\Sector;
  24. use App\Model\Component;
  25. use App\Model\Link;
  26. use App\Model\FooterCategory;
  27. use App\Model\Size;
  28. use function Hyperf\Support\retry;
  29. use Hyperf\Paginator\Paginator;
  30. use App\Model\User;
  31. use App\Model\Website;
  32. use App\Constants\ErrorCode;
  33. use GuzzleHttp\Client;
  34. use GuzzleHttp\Exception\GuzzleException;
  35. use App\Model\SectorPlace;
  36. use App\Model\ComponentType;
  37. use App\Model\Article;
  38. use App\Model\WebsiteCategory;
  39. use App\Model\AdPlace;
  40. use App\Model\Company;
  41. use App\Model\WebsiteImg;
  42. use App\Model\SectorComponent;
  43. use App\Model\ComponentImg;
  44. use App\Model\TemplateRule;
  45. use App\Model\SectorType;
  46. use App\Model\Level;
  47. use App\Model\StyleCode;
  48. use think\controller\Rest;
  49. use App\Model\Aichat;
  50. use App\Model\AichatSession;
  51. use App\Model\TemplateDraftbox;
  52. use App\JsonRpc\WebsiteServer;
  53. #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  54. class PublicRpcService implements PublicRpcServiceInterface
  55. {
  56. #[Inject]
  57. protected Redis $redis;
  58. /**
  59. * @param array $data
  60. * @return array
  61. */
  62. public function getDistrictList(array $data): array
  63. {
  64. $where = [];
  65. if (isset($data['keyWord'])) {
  66. $where = [
  67. ['name', 'like', '%' . $data['keyWord'] . '%']
  68. ];
  69. }
  70. $result = [];
  71. if (isset($data['pageSize'])) {
  72. $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
  73. $count = District::where($where)->count();
  74. $result = [
  75. 'rows' => $rep,
  76. 'count' => $count
  77. ];
  78. } else {
  79. $result = District::where($data)->orderBy("code", "asc")->get();
  80. }
  81. return $result ? Result::success($result) : Result::error("没有查到数据");
  82. }
  83. /**
  84. * @param array $data
  85. * @return array
  86. */
  87. public function getUserLevelList(array $data): array
  88. {
  89. $where = [];
  90. if (isset($data['keyWord'])) {
  91. $where = [
  92. ['name', 'like', '%' . $data['keyWord'] . '%'],
  93. ];
  94. }
  95. $result = [];
  96. if (isset($data['pageSize'])) {
  97. $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
  98. $count = UserLevel::where($where)->count();
  99. $result = [
  100. 'rows' => $rep,
  101. 'count' => $count,
  102. ];
  103. } else {
  104. $result = UserLevel::orderBy("id", "asc")->get();
  105. }
  106. return $result ? Result::success($result) : Result::error("没有查到数据");
  107. }
  108. /**
  109. * 添加用户等级
  110. * @param array $data
  111. * @return array
  112. */
  113. public function addUserLevel(array $data): array
  114. {
  115. LevelUser::insertGetId($data);
  116. return Result::success([]);
  117. }
  118. /**
  119. * 更新等级
  120. * @param array $data
  121. * @return array
  122. */
  123. public function updateUserLevel(array $data): array
  124. {
  125. $result = LevelUser::where(['id' => $data['id']])->update($data);
  126. if ($result) {
  127. return Result::success($result);
  128. }
  129. return Result::error("更新失败");
  130. }
  131. /**
  132. * 删除等级
  133. * @param array $data
  134. * @return array
  135. */
  136. public function delUserLevel(array $data): array
  137. {
  138. $result = LevelUser::where(['id' => $data['id']])->delete();
  139. if ($result) {
  140. return Result::success($result);
  141. }
  142. return Result::error("删除失败");
  143. }
  144. /**
  145. * 查询投诉举报信息
  146. * @param array $data
  147. * @return array
  148. */
  149. public function getLetterOfComplaint(array $data = []): array
  150. {
  151. var_dump("====");
  152. $where = [];
  153. if (isset($data['user_id']) && !empty($data['user_id'])) {
  154. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  155. }
  156. if (isset($data['nature']) && !empty($data['nature'])) {
  157. array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
  158. }
  159. if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
  160. array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
  161. }
  162. if (isset($data['status']) && !empty($data['status'])) {
  163. array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
  164. }
  165. $result = [];
  166. if (isset($data['pageSize'])) {
  167. $rep = LetterOfComplaint::where($where)
  168. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  169. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  170. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  171. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  172. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  173. ->select(
  174. "letter_of_complaint.*",
  175. "type_a.type_name as nature_name",
  176. "type_b.type_name as nature_name1",
  177. "type_c.type_name as nature_name0",
  178. "type_d.type_name as status_name",
  179. "type_e.type_name as nature_name3"
  180. )
  181. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
  182. $count = LetterOfComplaint::where($where)->count();
  183. if ($rep) {
  184. foreach ($rep as $val) {
  185. if ($val['judgment']) {
  186. $val['judgment'] = json_decode($val['judgment']);
  187. }
  188. if ($val['audio_and_video']) {
  189. $val['audio_and_video'] = json_decode($val['audio_and_video']);
  190. }
  191. if ($val['contract']) {
  192. $val['contract'] = json_decode($val['contract']);
  193. }
  194. if ($val['qualifications']) {
  195. $val['qualifications'] = json_decode($val['qualifications']);
  196. }
  197. }
  198. }
  199. $result = [
  200. 'rows' => $rep,
  201. 'count' => $count,
  202. ];
  203. } else {
  204. $result = LetterOfComplaint::where($where)
  205. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  206. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  207. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  208. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  209. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  210. ->select(
  211. "letter_of_complaint.*",
  212. "type_a.type_name as nature_name",
  213. "type_b.type_name as nature_name1",
  214. "type_c.type_name as nature_name0",
  215. "type_d.type_name as status_name",
  216. "type_e.type_name as nature_name3"
  217. )
  218. ->orderBy("letter_of_complaint.id", "desc")->get();
  219. }
  220. return $result ? Result::success($result) : Result::error("没有查到数据");
  221. }
  222. /**
  223. * 添加投诉举报信息
  224. * @param array $data
  225. * @return array
  226. */
  227. public function addLetterOfComplaint(array $data): array
  228. {
  229. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  230. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  231. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  232. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  233. unset($data['id']);
  234. $result = LetterOfComplaint::insertGetId($data);
  235. if (empty($result)) {
  236. return Result::error("创建失败", 0);
  237. } else {
  238. return Result::success(["id" => $result]);
  239. }
  240. }
  241. /**
  242. * 用户端更新投诉举报
  243. * @param array $data
  244. * @return array
  245. */
  246. public function userUpLetterOfComplaint(array $data): array
  247. {
  248. $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
  249. $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
  250. $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
  251. $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
  252. $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
  253. if (empty($result)) {
  254. return Result::error("创建失败", 0);
  255. } else {
  256. return Result::success(["id" => $result]);
  257. }
  258. }
  259. /**
  260. * 管理后台更新投诉举报信息
  261. * @param array $data
  262. * @return array
  263. */
  264. public function upLetterOfComplaint(array $data): array
  265. {
  266. var_dump("admin:", $data);
  267. $where = [
  268. 'id' => $data['id'],
  269. ];
  270. $filtered_array = array_filter($data, function ($value) {
  271. return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
  272. });
  273. $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
  274. $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
  275. $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
  276. $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
  277. unset($filtered_array['nature_name']);
  278. unset($filtered_array['type_name']);
  279. unset($filtered_array['nature_level_name']);
  280. unset($filtered_array['status_name']);
  281. unset($filtered_array['is_admin']);
  282. unset($filtered_array['type_level_name']);
  283. $result = LetterOfComplaint::where($where)->update($filtered_array);
  284. if ($result) {
  285. return Result::success($result);
  286. }
  287. return Result::error("更新失败", 0);
  288. }
  289. /**
  290. * 查询投诉举报记录
  291. * @param array $data
  292. * @return array
  293. */
  294. public function getLetterOfComplaintInfo(array $data): array
  295. {
  296. $where = [
  297. 'letter_of_complaint.id' => $data['id'],
  298. ];
  299. if (isset($data['user_id']) && !empty($data['user_id'])) {
  300. array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
  301. }
  302. $result = LetterOfComplaint::where($where)
  303. ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
  304. ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
  305. ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
  306. ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
  307. ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
  308. ->select(
  309. "letter_of_complaint.*",
  310. "type_a.type_name as nature_name",
  311. "type_b.type_name as nature_name1",
  312. "type_c.type_name as nature_name0",
  313. "type_d.type_name as status_name",
  314. "type_e.type_name as nature_name3"
  315. )
  316. ->first();
  317. return Result::success($result);
  318. }
  319. /**
  320. * 删除投诉举报信息
  321. * @param array $data
  322. * @return array
  323. */
  324. public function delLetterOfComplaint(array $data): array
  325. {
  326. $result = LetterOfComplaint::when($data, function ($query) use ($data) {
  327. if (isset($data['id']) && !empty($data['id'])) {
  328. $query->where(['id' => $data['id']]);
  329. }
  330. if (isset($data['user_id']) && !empty($data['user_id'])) {
  331. $query->where(['user_id' => $data['user_id']]);
  332. }
  333. })->delete();
  334. if (empty($result)) {
  335. return Result::error("删除失败", 0);
  336. } else {
  337. return Result::success();
  338. }
  339. }
  340. /**
  341. * 获取举报信息类型
  342. * @param array $data
  343. * @return array
  344. */
  345. public function getLetterType(array $data): array
  346. {
  347. $where = [];
  348. if (isset($data['type'])) {
  349. array_push($where, ['type', '=', $data['type']]);
  350. }
  351. if (isset($data['pid']) && $data['pid'] > 0) {
  352. array_push($where, ['pid', '=', $data['pid']]);
  353. }
  354. $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
  355. return $result ? Result::success($result) : Result::error("没有查到数据");
  356. }
  357. /**
  358. * 更新举报类型
  359. * @param array $data
  360. * @return array
  361. */
  362. public function upLetterType(array $data): array
  363. {
  364. return [];
  365. }
  366. /**
  367. * 添加举报类型
  368. * @param array $data
  369. * @return array
  370. */
  371. public function addLetterType(array $data): array
  372. {
  373. $result = LetterType::insertGetId($data);
  374. if (empty($result)) {
  375. return Result::error("创建失败", 0);
  376. } else {
  377. return Result::success(["id" => $result]);
  378. }
  379. }
  380. /**
  381. * 删除举报类型
  382. * @param array $data
  383. * @return array
  384. */
  385. public function delLetterType(array $data): array
  386. {
  387. $result = LetterType::where('id', $data['id'])->delete();
  388. if (empty($result)) {
  389. return Result::error("删除失败", 0);
  390. } else {
  391. return Result::success();
  392. }
  393. }
  394. /**
  395. * 检测是否已经被接案
  396. * @param array $data
  397. * @return array
  398. */
  399. public function checkMeasure(array $data): array
  400. {
  401. $where = [
  402. 'id' => $data['id'],
  403. ];
  404. $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
  405. var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
  406. //操作人和当前登陆用户id 相等说明是当前人接收的案件
  407. if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
  408. return Result::success();
  409. } else {
  410. return Result::error("您不能处理其他人已经接过的案件", 0);
  411. }
  412. }
  413. /**
  414. * 后台获取职能部门
  415. * @param array $data
  416. * @return array
  417. */
  418. public function getZhinengbumenList(array $data): array
  419. {
  420. // 获取分页参数,默认每页 10 条记录
  421. $page = isset($data['page']) ? (int) $data['page'] : 1;
  422. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  423. // 查询数据并分页
  424. $query = Department::query();
  425. // 可以在这里添加更多的查询条件
  426. if (isset($data['search'])) {
  427. $query->where('name', 'like', '%' . $data['search'] . '%');
  428. }
  429. // 执行分页查询
  430. $result = $query->paginate($perPage, ['*'], 'page', $page);
  431. // 返回分页结果
  432. return Result::success([
  433. 'count' => $result->total(),
  434. 'current_page' => $result->currentPage(),
  435. 'last_page' => $result->lastPage(),
  436. 'pagesize' => $result->perPage(),
  437. 'rows' => $result->items(),
  438. ]);
  439. }
  440. /**
  441. * 添加获取职能部门
  442. * @param array $data
  443. * @return array
  444. */
  445. public function addZhinengbumen(array $data): array
  446. {
  447. $result = Department::insertGetId($data);
  448. if (empty($result)) {
  449. return Result::error("创建失败", 0);
  450. } else {
  451. return Result::success(["id" => $result]);
  452. }
  453. }
  454. public function delZhinengbumen(array $data): array
  455. {
  456. $result = Department::where('id', $data['id'])->delete();
  457. if (empty($result)) {
  458. return Result::error("删除失败", 0);
  459. } else {
  460. return Result::success();
  461. }
  462. }
  463. public function getZhinengbumen(array $data): array
  464. {
  465. $result = Department::where('id', $data['id'])
  466. ->orderBy('department', 'desc')
  467. ->first();
  468. return Result::success($result);
  469. }
  470. public function getPidZhinengbumen(array $data): array
  471. {
  472. if (empty($data['pid'])) {
  473. $data['pid'] = 0;
  474. }
  475. $result = Department::where('pid', $data['pid'])->get();
  476. return Result::success($result);
  477. }
  478. public function modZhinengbumen(array $data): array
  479. {
  480. $result = Department::where('id', $data['id'])->update($data);
  481. if (empty($result)) {
  482. return Result::error("修改失败", 0);
  483. } else {
  484. return Result::success();
  485. }
  486. }
  487. /**
  488. * 查询职能列表
  489. * @param array $data
  490. * @return array
  491. */
  492. public function getDepartment(array $data): array
  493. {
  494. $where = [];
  495. if (isset($data['pid'])) {
  496. $where = [
  497. 'pid' => $data['pid'] ?? 0
  498. ];
  499. }
  500. $result = Department::when(!empty($where), function ($query) use ($where) {
  501. $query->where($where);
  502. })->orderBy("sort", "desc")->get();
  503. if (empty($result)) {
  504. return Result::error("查询失败", 0);
  505. } else {
  506. return Result::success($result);
  507. }
  508. }
  509. /**
  510. * 获取所有的buckets
  511. * @param array $data
  512. * @return array
  513. */
  514. public function getBuckets(array $data): array
  515. {
  516. $result = new MinioService();
  517. // 调用服务层的方法获取存储桶列表
  518. $bucketsResponse = $result->listBuckets();
  519. // 直接返回服务层生成的响应
  520. return Result::success($bucketsResponse['data']);
  521. }
  522. /**
  523. * 上传文件
  524. * @param array $data
  525. * @return array
  526. */
  527. public function uploadFile(array $data): array
  528. {
  529. $result = new MinioService();
  530. $rep = $result->uploadFile($data);
  531. if ($rep['code'] == 200) {
  532. return Result::success($rep['data']);
  533. } else {
  534. return Result::error("上传失败!");
  535. }
  536. }
  537. /**
  538. * 黑名单管理
  539. * @param array $data
  540. * @return array
  541. */
  542. public function getBlackWordList(array $data): array
  543. {
  544. $result = BlackWord::when($data, function ($query) use ($data) {
  545. if (isset($data['name']) && $data['name']) {
  546. $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
  547. }
  548. })->orderBy('black_word.id', 'desc')
  549. ->paginate(
  550. intval($data['pageSize']),
  551. [
  552. 'black_word.*',
  553. ],
  554. 'page',
  555. intval($data['page'])
  556. );
  557. $count = $result->total();
  558. $returnData = [
  559. 'rows' => $result->items(),
  560. 'count' => $count
  561. ];
  562. return Result::success($returnData);
  563. }
  564. /**
  565. * 添加黑名单
  566. * @param array $data
  567. * @return array
  568. */
  569. public function addBlackWord(array $data): array
  570. {
  571. Db::beginTransaction();
  572. try {
  573. $info = BlackWord::where(['name' => $data['name']])->first();
  574. if ($info) {
  575. Db::rollBack();
  576. return Result::error("该黑名单已存在", 0);
  577. }
  578. $data['type'] = 10;
  579. $result = BlackWord::insertGetId($data);
  580. $redisKey = 'black_word';
  581. $this->redis->sAdd($redisKey, $data['name']);
  582. Db::commit();
  583. return Result::success(["id" => $result]);
  584. } catch (\Exception $e) {
  585. Db::rollBack();
  586. return Result::error("创建失败" . $e->getMessage(), 0);
  587. }
  588. }
  589. /**
  590. * 删除黑名单
  591. * @param array $data
  592. * @return array
  593. */
  594. public function delBlackWord(array $data): array
  595. {
  596. Db::beginTransaction();
  597. try {
  598. BlackWord::where(['name' => $data['name']])->delete();
  599. $redisKey = 'black_word';
  600. $this->redis->sRem($redisKey, $data['name']);
  601. Db::commit();
  602. return Result::success([]);
  603. } catch (\Exception $e) {
  604. Db::rollBack();
  605. return Result::error("删除失败" . $e->getMessage(), 0);
  606. }
  607. }
  608. /**
  609. * 修改违禁词
  610. * @param array $data
  611. * @return array
  612. */
  613. public function upBlackWord(array $data): array
  614. {
  615. Db::beginTransaction();
  616. try {
  617. $checkInfo = BlackWord::where(['name' => $data['name']])->first();
  618. if ($checkInfo) {
  619. Db::rollBack();
  620. return Result::error("该违禁词已经存在", 0);
  621. }
  622. $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
  623. if ($blackWordInfo) {
  624. //先删除redis
  625. $blackWordInfo = $blackWordInfo->toArray();
  626. $redisKey = 'black_word';
  627. $this->redis->sRem($redisKey, $blackWordInfo['name']);
  628. $this->redis->sAdd($redisKey, $data['name']);
  629. BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
  630. Db::commit();
  631. return Result::success([]);
  632. } else {
  633. Db::rollBack();
  634. return Result::error("系统错误", 0);
  635. }
  636. } catch (\Exception $e) {
  637. Db::rollBack();
  638. return Result::error("修改失败" . $e->getMessage(), 0);
  639. }
  640. }
  641. /**
  642. * 获取风格
  643. * @return void
  644. */
  645. public function getTemplateClassList(array $data): array
  646. {
  647. $where = [];
  648. if (isset($data['name']) && $data['name']) {
  649. array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
  650. }
  651. if (isset($data['keyword']) && $data['keyword']) {
  652. array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
  653. }
  654. $template = TemplateClass::when($where, function ($query) use ($where) {
  655. $query->where($where);
  656. });
  657. $count = $template->count();
  658. // $countQuery = clone $template;
  659. $row = $template
  660. ->leftJoin('template', function ($join) {
  661. $join->on('template_class.class_id', '=', 'template.template_class_id');
  662. })
  663. ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
  664. ->groupBy('template_class.id')
  665. ->orderBy('template_class.id', 'desc')
  666. ->offset(($data['page'] - 1) * $data['pageSize'])
  667. ->limit($data['pageSize'])
  668. ->get();
  669. $result = [
  670. 'rows' => $row,
  671. 'count' => $count,
  672. ];
  673. if ($row->isEmpty()) {
  674. return Result::error("暂无风格", 0);
  675. } else {
  676. return Result::success($result);
  677. }
  678. }
  679. /**
  680. * 添加风格
  681. * @param
  682. * @return void
  683. */
  684. public function addTemplateClass(array $data): array
  685. {
  686. $data['keyword'] = json_encode($data['keyword']);
  687. $template_class = TemplateClass::where('name', $data['name'])
  688. ->orWhere('class_id', $data['class_id'])
  689. ->first();
  690. if ($template_class) {
  691. return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
  692. }
  693. $result = TemplateClass::insertGetId($data);
  694. if (empty($result)) {
  695. return Result::error("创建风格失败", 0);
  696. } else {
  697. return Result::success(["id" => $result]);
  698. }
  699. }
  700. /**
  701. * 更新风格
  702. * @param array $data
  703. * @return array
  704. */
  705. public function upTemplateClass(array $data): array
  706. {
  707. $where = [
  708. 'id' => $data['id'],
  709. ];
  710. $template_class = TemplateClass::where($where)->first();
  711. if (empty($template_class)) {
  712. return Result::error("未查询到风格", 0);
  713. }
  714. if ($template_class->type == 1) {
  715. return Result::error("默认风格不能修改", 0);
  716. }
  717. $template = TemplateClass::where('id', '!=', $data['id'])
  718. // ->where(['name' => $data['name']])
  719. // ->orWhere(['class_id' => $data['class_id']])
  720. ->where(function ($query) use ($data) {
  721. $query->where('name', $data['name'])
  722. ->orWhere('class_id', $data['class_id']);
  723. })
  724. ->first();
  725. if ($template) {
  726. return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
  727. }
  728. if(is_string($data['keyword'])){
  729. $keyword = json_decode($data['keyword'],true) ?? $data['keyword'];
  730. }
  731. if(is_array($data['keyword'])){
  732. $keyword = json_encode($data['keyword']);
  733. }
  734. $updateData = [
  735. 'name' => $data['name'],
  736. 'keyword' => $keyword,
  737. 'class_id' => $data['class_id'],
  738. ];
  739. $result = TemplateClass::where($where)->update($updateData);
  740. if (empty($result)) {
  741. return Result::error("更新失败", 0);
  742. } else {
  743. return Result::success($result);
  744. }
  745. }
  746. /**
  747. * 删除风格
  748. * @param array $data
  749. * @return array
  750. */
  751. public function delTemplateClass(array $data): array
  752. {
  753. $where = [
  754. 'id' => $data['id'],
  755. ];
  756. $template_class = TemplateClass::where($where)->first();
  757. if (empty($template_class)) {
  758. return Result::error("未查询到风格", 0);
  759. }
  760. if ($template_class->type == 1) {
  761. return Result::error("默认风格不能删除", 0);
  762. }
  763. $template = Template::where('template_class_id', $template_class['class_id'])->get();
  764. if (!empty($template->all())) {
  765. try {
  766. // 默认风格 1
  767. Db::beginTransaction();
  768. $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
  769. if (empty($template)) {
  770. Db::rollBack();
  771. return Result::error("删除失败", 0);
  772. }
  773. $result = TemplateClass::where($where)->delete();
  774. } catch (\Exception $e) {
  775. return Result::error("删除失败" . $e->getMessage(), 0);
  776. }
  777. } else {
  778. $result = TemplateClass::where('id', $data['id'])->delete();
  779. }
  780. if (empty($result)) {
  781. return Result::error("删除失败", 0);
  782. }
  783. return Result::success($result);
  784. }
  785. /**
  786. * 根据风格名称-获取获取所有风格
  787. * @param array $data
  788. * @return array
  789. */
  790. public function getTemplateClass(array $data): array
  791. {
  792. $where = [];
  793. if (!empty($data['template_name'])) {
  794. $where['name'] = $data['template_name'];
  795. }
  796. $result = TemplateClass::where($where)->get();
  797. if (empty($result)) {
  798. return Result::error("暂无风格", 0);
  799. }
  800. return Result::success($result);
  801. }
  802. /**
  803. * 获取皮肤列表
  804. * @param array $data
  805. * @return array
  806. */
  807. public function getTemplateList(array $data): array
  808. {
  809. $where = [];
  810. if (!empty($data['template_class_id'])) {
  811. $where['template_class_id'] = $data['template_class_id'];
  812. }
  813. if (!empty($data['template_name'])) {
  814. array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
  815. }
  816. if (!empty($data['template_keyword'])) {
  817. array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
  818. }
  819. $result = Template::where($where)
  820. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  821. ->select('template.*', 'template_class.name as template_class_name')
  822. ->orderBy('template.template_id', 'desc')
  823. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  824. if (empty($result)) {
  825. return Result::error("暂无皮肤", 0);
  826. }
  827. return Result::success($result);
  828. }
  829. public function getTemplateInfo(array $data): array
  830. {
  831. $result = Template::where('template.id', $data['id'])
  832. ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
  833. ->select('template.*', 'template_class.name as template_class_name')
  834. ->first();
  835. if (empty($result)) {
  836. return Result::error("暂无皮肤", 0);
  837. }
  838. return Result::success($result);
  839. }
  840. /**
  841. * 添加皮肤
  842. * @param array $data
  843. * @return array
  844. */
  845. public function addTemplate(array $data): array
  846. {
  847. var_dump($data);
  848. unset($data['user_id']);
  849. $template = Template::where('template_name', $data['template_name'])
  850. ->orWhere('template_id', $data['template_id'])
  851. ->first();
  852. if ($template) {
  853. return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
  854. }
  855. $data['template_keyword'] = array_map('trim', $data['template_keyword']);
  856. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  857. $page_type = json_decode($data['page_type'], true);
  858. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  859. $result = Template::insertGetId($data);
  860. if (empty($result)) {
  861. return Result::error("创建失败", 0);
  862. } else {
  863. return Result::success($result);
  864. }
  865. }
  866. /**
  867. * 删除皮肤
  868. * @param array $data
  869. * @return array
  870. */
  871. public function delTemplate(array $data): array
  872. {
  873. $rector = Sector::where('template_id', $data['id'])->first();
  874. if (!empty($rector)) {
  875. return Result::error("此皮肤已被绑定通栏,不可删除", 0);
  876. }
  877. $template = Template::where('id', $data['id'])->first();
  878. if(empty($template)){
  879. return Result::error("此皮肤不存在!", 0);
  880. }
  881. $web_template = WebsiteTemplateInfo::where('template_id', $template['template_id'])->first();
  882. if (!empty($web_template)) {
  883. return Result::error("此皮肤已被绑定网站,不可删除", 0);
  884. }
  885. // if ($template->type == 1) {
  886. // return Result::error("默认皮肤不能删除", 0);
  887. // }
  888. $component_img = ComponentImg::where('template_id', $template['template_id'])->get();
  889. if (!empty($component_img) && $component_img->count() > 0) {
  890. return Result::error('此皮肤已绑定组件预览图,不可删除!' . $component_img);
  891. }
  892. $result = Template::where('id', $data['id'])->delete();
  893. var_dump($result, '-------------------delete');
  894. if ($result) {
  895. return Result::success($result);
  896. } else {
  897. return Result::error("删除失败", 0);
  898. }
  899. }
  900. /**
  901. * 更新皮肤
  902. * @param array $data
  903. * @return array
  904. */
  905. public function updateTemplate(array $data): array
  906. {
  907. unset($data['user_id']);
  908. $template = Template::where('id', $data['id'])
  909. ->first();
  910. if (empty($template)) {
  911. return Result::error("此皮肤不存在!", 0);
  912. }
  913. $template = Template::where('id', '!=', $data['id'])
  914. ->where(function ($query) use ($data) {
  915. $query->where('template_name', $data['template_name'])
  916. ->orWhere('template_id', $data['template_id']);
  917. })
  918. ->first();
  919. if ($template) {
  920. return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
  921. }
  922. $data['template_keyword'] = array_map('trim', $data['template_keyword']);
  923. $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
  924. $page_type = json_decode($data['page_type'], true);
  925. $data['page_type'] = json_encode(array_values(array_unique($page_type)));
  926. $result = Template::where('id', $data['id'])->update($data);
  927. var_dump($result, '-------------------update');
  928. if (!$result) {
  929. return Result::error("更新失败", 0);
  930. } else {
  931. return Result::success('更新成功');
  932. }
  933. }
  934. /**
  935. * 获取所有尺寸
  936. * @param array $data
  937. * @return array
  938. */
  939. public function getAllSize(array $data): array
  940. {
  941. if(isset($data['id']) && !empty($data['id'])){
  942. $result = Size::where('id', $data['id'])->get()->all();
  943. }else{
  944. $result = Size::get()->all();
  945. }
  946. if(empty($result)){
  947. return Result::error("暂无尺寸", 0);
  948. }
  949. return Result::success($result);
  950. }
  951. /**
  952. * 根据皮肤名称-获取所有皮肤
  953. * @param array $data
  954. * @return array
  955. */
  956. public function getAllTemplate(array $data): array
  957. {
  958. $where = [];
  959. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  960. $where[] = ['template_class_id', $data['template_class_id']];
  961. }
  962. if (isset($data['template_name']) && !empty($data['template_name'])) {
  963. $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
  964. }
  965. $result = Template::where($where)->get()->all();
  966. if (empty($result)) {
  967. return Result::error("暂无皮肤", 0);
  968. }
  969. return Result::success($result);
  970. }
  971. /**
  972. * 获取通栏列表
  973. * @param array $data
  974. * @return array
  975. */
  976. public function getSectorList(array $data): array
  977. {
  978. $where = [];
  979. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  980. $where['template.template_class_id'] = $data['template_class_id'];
  981. }
  982. if (isset($data['template_id']) && !empty($data['template_id'])) {
  983. array_push($where, ['template.template_id', $data['template_id']]);
  984. }
  985. if (isset($data['sector_name']) && !empty($data['sector_name'])) {
  986. array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
  987. }
  988. if (isset($data['height']) && !empty($data['height'])) {
  989. array_push($where, ['sector.pic_height', 'like', '%' . $data['height'] . '%']);
  990. }
  991. if (isset($data['page_type']) && !empty($data['page_type'])) {
  992. array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
  993. }
  994. $result = Sector::leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  995. ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
  996. ->leftJoin('sector_place', 'sector_place.sector_type', '=', 'sector.place_type')
  997. ->where($where)
  998. ->where(function ($query) {
  999. $query->where('sector_place.type_id', 1)
  1000. ->orWhereNull('sector_place.type_id');
  1001. })
  1002. ->select(
  1003. 'sector.*',
  1004. 'template_class.name as class_name',
  1005. 'template_class.class_id',
  1006. 'template.template_name',
  1007. 'sector_place.name as place_name',
  1008. 'sector_place.sector_img as place_img'
  1009. )
  1010. ->orderBy('sector.id', 'desc')
  1011. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  1012. if(empty($result)){
  1013. return Result::error('暂无通栏!');
  1014. }
  1015. return Result::success($result);
  1016. }
  1017. /**
  1018. * 获取通栏详情
  1019. * @param array $data
  1020. * @return array
  1021. */
  1022. public function getSectorInfo(array $data): array
  1023. {
  1024. // $where = [];
  1025. $where[] = ['sector.id', '=', $data['id']];
  1026. $result = Sector::where($where)
  1027. ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
  1028. ->select('sector.*', 'template.template_name', 'template.template_id')
  1029. ->orderBy('sector.id', 'desc')
  1030. ->first();
  1031. $result['component'] = SectorComponent::where('sector_component.sector_id', $data['id'])
  1032. ->leftJoin('component', 'component.component_type', '=', 'sector_component.component_id')
  1033. ->select('component.component_type', 'sector_component.sort_id', 'component.component_name')
  1034. ->get()->all();
  1035. // $sorts = array_values(array_unique(array_column($components, 'sort_id')));
  1036. // $result['component'] = $components;
  1037. $result['place'] = SectorPlace::where('sector_place.sector_type', $result['place_type'])
  1038. ->where('sector_place.type_id', 1)
  1039. ->select('sector_place.sector_img', 'sector_place.map')
  1040. ->first();
  1041. if (empty($result)) {
  1042. return Result::error('此通栏不存在!');
  1043. }
  1044. return Result::success($result);
  1045. }
  1046. /**
  1047. * 获取经纬度信息
  1048. * @return void
  1049. */
  1050. public function getIpInfo(array $data): array
  1051. {
  1052. if (empty($data['ip'])) {
  1053. // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
  1054. return Result::error('IP地址不能为空!');
  1055. }
  1056. $client_ip = $data['ip'];
  1057. try {
  1058. // 直接从容器获取 Guzzle 客户端实例
  1059. /** @var \GuzzleHttp\Client $client */
  1060. $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
  1061. // 使用新的 GeoJS API 地址
  1062. $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
  1063. // 将 timeout 作为请求选项传递
  1064. $response = $client->get($api_url, ['timeout' => 5]);
  1065. if ($response->getStatusCode() !== 200) {
  1066. return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
  1067. }
  1068. $ip_info = json_decode($response->getBody()->getContents(), true);
  1069. // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
  1070. if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
  1071. // GeoJS 返回的经纬度是字符串,转换为浮点数
  1072. $ip_info['latitude'] = floatval($ip_info['latitude']);
  1073. $ip_info['longitude'] = floatval($ip_info['longitude']);
  1074. return Result::success($ip_info);
  1075. }
  1076. // 如果返回的数据不符合预期,提供一个通用错误
  1077. $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
  1078. return Result::error($errorMessage);
  1079. } catch (GuzzleException $e) {
  1080. // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
  1081. return Result::error('请求IP查询接口异常: ' . $e->getMessage());
  1082. } catch (\Throwable $e) {
  1083. return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
  1084. }
  1085. }
  1086. /**
  1087. * 获取天气
  1088. * @param array $data
  1089. * @return array
  1090. */
  1091. public function getWeatherInfo(array $data): array
  1092. {
  1093. $month = $data['month'] ?? date('m');
  1094. $day = $data['day'] ?? date('d');
  1095. // 使用缓存键
  1096. $cacheKey = "tsbb_data_weather_{$month}_{$day}";
  1097. // 尝试从缓存获取数据
  1098. $container = \Hyperf\Context\ApplicationContext::getContainer();
  1099. $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
  1100. if ($cachedData = $cache->get($cacheKey)) {
  1101. return Result::success(unserialize($cachedData));
  1102. }
  1103. $location = $data['latitude'] . ":" . $data['longitude'];
  1104. $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
  1105. $ch = curl_init($api_url);
  1106. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1107. $response = curl_exec($ch);
  1108. curl_close($ch);
  1109. // 解析 JSON 响应
  1110. $WeatherInfo = json_decode($response, true);
  1111. // 缓存结果,设置1小时过期
  1112. $cache->set($cacheKey, serialize($WeatherInfo), 3600);
  1113. if ($WeatherInfo) {
  1114. return Result::success($WeatherInfo);
  1115. } else {
  1116. return Result::error("获取天气失败", 0);
  1117. }
  1118. }
  1119. /**
  1120. * 获取农历信息
  1121. * @return void
  1122. */
  1123. public function getCalendar(array $data): array
  1124. {
  1125. $calendar = new Calendar();
  1126. $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
  1127. return Result::success($result);
  1128. }
  1129. /**
  1130. * 添加通栏
  1131. * @param array $data
  1132. * @return array
  1133. */
  1134. public function addSector(array $data): array
  1135. {
  1136. unset($data['user_id']);
  1137. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1138. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1139. $data['page_type'] = json_encode($data['page_type']);
  1140. $template = Template::where('template_id', $data['template_id'])
  1141. ->first();
  1142. if (empty($template)) {
  1143. return Result::error('皮肤不存在!');
  1144. }
  1145. $sector_query = Sector::where('template_id', $data['template_id']);
  1146. if (!empty($sector_query->where('sector_id', $data['sector_id'])->first())) {
  1147. return Result::error('通栏编号已存在!');
  1148. }
  1149. if(!empty($sector_query->where('sector_type', $data['place_type'])->where('component_code',$data['component_code'])->first())){
  1150. return Result::error('通栏位置已存在!');
  1151. }
  1152. $sector_place = SectorPlace::where('sector_type', $data['place_type'])->get();
  1153. if (empty($sector_place)) {
  1154. return Result::error('位置不存在!');
  1155. }
  1156. $sort = json_decode($data['component_code'], true);
  1157. $sector = 0;
  1158. // $flattened = [];
  1159. // $sector = [];
  1160. $component = Component::whereIn('component_type', $sort)
  1161. ->leftJoin('component_type','component.type_id','component_type.id')
  1162. ->select('component.*','component_type.cat_num')
  1163. ->get()->all();
  1164. // return Result::success($data);
  1165. if (empty($component)) {
  1166. return Result::error('组件不存在!');
  1167. }
  1168. $data['cat_num'] = array_sum(array_column($component, 'cat_num'));
  1169. $sector_component = [];
  1170. foreach ($sort as $key => $subArray) {
  1171. foreach ($subArray as $k => $item) {
  1172. // // $component[] = $item;
  1173. $sector_component[] = [
  1174. 'sector_id' => $data['sector_id'],
  1175. 'component_id' => $subArray[$k],
  1176. 'sort_id' => $key + 1,
  1177. 'place_id' => $data['place_type'],
  1178. // 'sectorid' => $data['id'],
  1179. ];
  1180. // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1181. // // $componentTypes[] = $item;
  1182. // // $components[] = [
  1183. // // 'sort_id' => $key + 1,
  1184. // // 'component_type' => $item,
  1185. // // ];
  1186. // // $place_ids[] = $data['place_type'];
  1187. }
  1188. }
  1189. // $component_num = count($component);
  1190. // $count = count(array_unique($component));
  1191. // 使用array_map批量替换sector_id
  1192. // return Result::success($sector_component);
  1193. if (count($sort) != $data['component_num']) {
  1194. return Result::error('组件关联错误!');
  1195. }
  1196. // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
  1197. $sector_code = [
  1198. $data['sector_id'] => [
  1199. 'sectorName' => $data['sector_name'],
  1200. 'sectorHeight' => $data['pic_height'],
  1201. 'sectorImg' => $data['sector_img'],
  1202. ]
  1203. ];
  1204. // return Result::success($sector_code);
  1205. $data['sector_code'] = json_encode($sector_code);
  1206. Db::beginTransaction();
  1207. try {
  1208. $sector = Sector::insertGetId($data);
  1209. if (empty($sector)) {
  1210. Db::rollBack();
  1211. return Result::error('添加失败');
  1212. }
  1213. $sector_com = array_map(function ($item) use ($sector) {
  1214. // 替换当前记录的sector_id为变量值(保留其他字段不变)
  1215. $item['sectorid'] = $sector;
  1216. return $item;
  1217. }, $sector_component);
  1218. // Db::rollBack();
  1219. // return Result::success($sector_com);
  1220. $com_sector = SectorComponent::insert($sector_com);
  1221. if (empty($com_sector)) {
  1222. Db::rollBack();
  1223. return Result::error('通栏关联组件失败!');
  1224. }
  1225. // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1226. // 查询数据库中已存在的component_type(只保留存在的记录)
  1227. // $existingTypes = Component::whereIn('component_type', $componentTypes)
  1228. // ->pluck('component_type') // 提取存在的component_type
  1229. // ->toArray();
  1230. // if(empty($existingTypes)){
  1231. // Db::rollBack();
  1232. // return Result::error('组件不存在!');
  1233. // }
  1234. // // var_dump($existingTypes);
  1235. // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增)
  1236. // $updateData = array_filter($components, function ($item) use ($existingTypes) {
  1237. // return in_array($item['component_type'], $existingTypes);
  1238. // });
  1239. // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增)
  1240. // if (!empty($updateData)) {
  1241. // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']);
  1242. // }
  1243. // if(empty($component_sort)){
  1244. // Db::rollBack();
  1245. // return Result::error('组件位置关联失败!');
  1246. // }
  1247. Db::commit();
  1248. return Result::success($sector);
  1249. } catch (\Exception $e) {
  1250. Db::rollBack();
  1251. return Result::error($e->getMessage());
  1252. }
  1253. }
  1254. /**
  1255. * 删除通栏
  1256. * @param array $data
  1257. * @return array
  1258. */
  1259. public function delSector(array $data): array
  1260. {
  1261. $sector = Sector::where('id', $data['id'])->first();
  1262. if (empty($sector)) {
  1263. return Result::error('通栏不存在!');
  1264. }
  1265. Db::beginTransaction();
  1266. try {
  1267. $component_id = SectorComponent::where('sectorid', $data['id'])->delete();
  1268. if (empty($component_id)) {
  1269. Db::rollBack();
  1270. return Result::error('解除相关组件关联关系失败!');
  1271. }
  1272. $result = Sector::where('id', $data['id'])->delete();
  1273. if (empty($result)) {
  1274. Db::rollBack();
  1275. return Result::error('通栏删除失败!');
  1276. }
  1277. Db::commit();
  1278. return Result::success('通栏删除成功!');
  1279. } catch (\Exception $e) {
  1280. Db::rollBack();
  1281. return Result::error($e->getMessage());
  1282. }
  1283. }
  1284. /**
  1285. * 修改通栏
  1286. * @param array $data
  1287. * @return array
  1288. */
  1289. public function updateSector(array $data): array
  1290. {
  1291. unset($data['user_id']);
  1292. $sector = Sector::where('id', $data['id'])->first();
  1293. if (empty($sector)) {
  1294. return Result::error('通栏不存在!');
  1295. }
  1296. if ($sector['sector_id'] != $data['sector_id']) {
  1297. $sector_id = Sector::where('sector_id', $data['sector_id'])->where('template_id', $data['template_id'])->first();
  1298. if (!empty($sector_id)) {
  1299. return Result::error('通栏编号已存在!');
  1300. }
  1301. }
  1302. $sector_code = [
  1303. $data['sector_id'] => [
  1304. 'sectorName' => $data['sector_name'],
  1305. 'sectorHeight' => $data['pic_height'],
  1306. 'sectorImg' => $data['sector_img'],
  1307. ]
  1308. ];
  1309. // return Result::success($sector_code);
  1310. $data['sector_code'] = json_encode($sector_code);
  1311. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1312. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1313. $data['page_type'] = json_encode($data['page_type']);
  1314. $template = Template::where('template_id', $data['template_id'])
  1315. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1316. ->first();
  1317. if (empty($template)) {
  1318. return Result::error('皮肤不存在!');
  1319. }
  1320. // 将嵌套二维数组展平为唯一值的一维数组
  1321. $sort = json_decode($data['component_code'], true);
  1322. $component_id = array_values(array_unique(array_merge(...$sort)));
  1323. $component = Component::whereIn('component_type', $component_id)
  1324. ->leftJoin('component_type','component.type_id','component_type.id')
  1325. ->select('component.*','component_type.cat_num')
  1326. ->get()->all();
  1327. // return Result::success($data);
  1328. if (empty($component)) {
  1329. return Result::error('组件不存在!');
  1330. }
  1331. $data['cat_num'] = array_sum(array_column($component, 'cat_num'));
  1332. $sector_component = [];
  1333. foreach ($sort as $key => $subArray) {
  1334. foreach ($subArray as $k => $item) {
  1335. // // $component[] = $item;
  1336. $sector_component[] = [
  1337. 'sector_id' => $data['sector_id'],
  1338. 'component_id' => $subArray[$k],
  1339. 'sort_id' => $key + 1,
  1340. 'place_id' => $data['place_type'],
  1341. 'sectorid' => $data['id'],
  1342. ];
  1343. // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1344. // // $componentTypes[] = $item;
  1345. // // $components[] = [
  1346. // // 'sort_id' => $key + 1,
  1347. // // 'component_type' => $item,
  1348. // // ];
  1349. // // $place_ids[] = $data['place_type'];
  1350. }
  1351. }
  1352. if (count($sort) != $data['component_num']) {
  1353. return Result::error('组件关联错误!');
  1354. }
  1355. $data['sector_code'] = json_encode($sector_code);
  1356. Db::beginTransaction();
  1357. try {
  1358. $com_sector = SectorComponent::where('sectorid', $data['id'])->pluck('component_id');
  1359. if (empty($com_sector)) {
  1360. Db::rollBack();
  1361. return Result::error('通栏解除组件关联失败!');
  1362. }
  1363. $del_SectorComponent = SectorComponent::where('sectorid', $data['id'])->delete();
  1364. if (empty($del_SectorComponent)) {
  1365. Db::rollBack();
  1366. return Result::error('通栏解除组件关联失败!1');
  1367. }
  1368. $sector = Sector::where('id', $data['id'])->update($data);
  1369. if (empty($sector)) {
  1370. Db::rollBack();
  1371. return Result::error('修改失败');
  1372. }
  1373. $sector_com = array_map(function ($item) use ($data) {
  1374. // 替换当前记录的sector_id为变量值(保留其他字段不变)
  1375. $item['sectorid'] = $data['id'];
  1376. return $item;
  1377. }, $sector_component);
  1378. // var_dump($sector_com);
  1379. $com_sector = SectorComponent::insert($sector_com);
  1380. if (empty($com_sector)) {
  1381. Db::rollBack();
  1382. return Result::error('通栏关联组件失败!');
  1383. }
  1384. // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1385. // $up_component = Component::whereIn('component_id',$sector_component['component_type'])
  1386. // ->update(['status' => 2,]);
  1387. // if(empty($up_component)){
  1388. // Db::rollBack();
  1389. // return Result::error('通栏关联组件失败!');
  1390. // }
  1391. // 2. 查询数据库中已存在的component_type(只保留存在的记录)
  1392. // $existingTypes = Component::whereIn('component_type', $componentTypes)
  1393. // ->pluck('component_type') // 提取存在的component_type
  1394. // ->toArray();
  1395. // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增)
  1396. // $updateData = array_filter($components, function ($item) use ($existingTypes) {
  1397. // return in_array($item['component_type'], $existingTypes);
  1398. // });
  1399. // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增)
  1400. // if (!empty($updateData)) {
  1401. // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']);
  1402. // }
  1403. // if(empty($component_sort)){
  1404. // Db::rollBack();
  1405. // return Result::error('组件位置关联失败!');
  1406. // }
  1407. Db::commit();
  1408. return Result::success($sector);
  1409. } catch (\Exception $e) {
  1410. Db::rollBack();
  1411. return Result::error($e->getMessage());
  1412. }
  1413. }
  1414. public function getComponentList(array $data): array
  1415. {
  1416. $where = [];
  1417. if (isset($data['component_name']) && !empty($data['component_name'])) {
  1418. array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']);
  1419. }
  1420. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  1421. array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
  1422. array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
  1423. } else if (isset($data['width']) && !empty($data['width'])) {
  1424. array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
  1425. } else if (isset($data['height']) && !empty($data['height'])) {
  1426. array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
  1427. } else {
  1428. }
  1429. if(isset($data['type_id']) && !empty($data['type_id'])){
  1430. $where['component.type_id'] = $data['type_id'];
  1431. }
  1432. $result = Component::where($where)
  1433. ->leftJoin('component_type', 'component_type.id', '=', 'component.type_id')
  1434. ->leftJoin('component_img', 'component.component_type', '=', 'component_img.component_id')
  1435. ->select(
  1436. 'component.*',
  1437. 'component_type.com_typename',
  1438. DB::raw('COUNT(component_img.id) as img_count')
  1439. )
  1440. ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量
  1441. ->orderBy('component.updated_at', 'desc')
  1442. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1443. if(empty($result)){
  1444. return Result::error('暂无数据');
  1445. }
  1446. return Result::success($result);
  1447. }
  1448. public function getComponentInfo(array $data): array
  1449. {
  1450. $where = ['component.id'=>$data['id']];
  1451. $result = Component::where($where)
  1452. ->leftJoin('component_type','component_type.id','=','component.type_id')
  1453. ->select( 'component.*','component_type.com_typename')
  1454. ->get();
  1455. if(empty($result)){
  1456. return Result::error('暂无数据');
  1457. }
  1458. return Result::success($result);
  1459. }
  1460. /*
  1461. 新增组件
  1462. */
  1463. public function addComponent(array $data): array
  1464. {
  1465. unset($data['user_id']);
  1466. $component_id = Component::where('component_type',$data['component_type'])->first();
  1467. if(!empty($component_id)){
  1468. return Result::error('组件编号已存在!');
  1469. }
  1470. $component_type = ComponentType::where('id',$data['type_id'])->first();
  1471. if(empty($component_type)){
  1472. return Result::error('组件分类不存在!');
  1473. }
  1474. // '组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;
  1475. // 8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;14:广告资讯混合类
  1476. $data['type_id'] = intval($data['type_id']);
  1477. $add_arr = [
  1478. // 'template_id' => intval($data['template_id']),
  1479. 'component_type' => intval($data['component_type']),
  1480. 'component_name' => $data['component_name'],
  1481. 'component_img' => $data['component_img'],
  1482. 'component_width' => $data['component_width'],
  1483. 'component_height' => $data['component_height'],
  1484. 'type_id' => intval($data['type_id']),
  1485. 'component_keyword' => $data['component_keyword'],
  1486. ];
  1487. $component_head_code = [
  1488. 'component_id' => $add_arr['component_type'],
  1489. 'component_type' => $add_arr['type_id'],
  1490. 'component_name' => $add_arr['component_name'],
  1491. 'component_style' => 1,
  1492. ];
  1493. $add_arr['component_code'] = json_encode($component_head_code, true);
  1494. $type = $data['type_id'];
  1495. switch ($type) {
  1496. case 19: //19:选项卡-资讯-推荐图类组件;
  1497. $type = 3;
  1498. break;
  1499. case 20: //20:选项卡-资讯-最新类组件;
  1500. $type = 4;
  1501. break;
  1502. case 21: //21:选项卡-资讯-推荐类组件;
  1503. $type = 5;
  1504. break;
  1505. case 22: //22:选项卡-资讯-热点类组件;
  1506. $type = 6;
  1507. break;
  1508. case 23: //23:选项卡-资讯-栏目类组件;
  1509. $type = 7;
  1510. break;
  1511. case 24: //24:选项卡-资讯-广告混合类组件;
  1512. $type = 14;
  1513. break;
  1514. }
  1515. switch ($type) {
  1516. case 1: //1:资讯-头条组件;
  1517. case 2: //2:资讯-轮播组件;
  1518. case 3: //3:资讯-推荐图类组件;
  1519. case 4: //4:资讯-最新类组件;
  1520. case 5: //5:资讯-推荐类组件;
  1521. case 6: //6:资讯-热点类组件;
  1522. $add_arr['level'] = $data['type_id'];
  1523. $add_arr['img_num'] = intval($data['img_num']);
  1524. $add_arr['text_num'] = intval($data['text_num']);
  1525. $component_data['componentData'] = [
  1526. // 'category_id' => '',
  1527. 'level' => $add_arr['level'],
  1528. 'name' => '',
  1529. // 'category_arr' => [],
  1530. 'imgSize' => $data['img_num'] ?? '',
  1531. 'textSize' => $data['text_num'] ?? '',
  1532. 'child' => [
  1533. 'id' => '',
  1534. 'imgSize' => '',
  1535. 'textSize' => ''
  1536. ]
  1537. ];
  1538. if ($data['type_id'] == 4) {
  1539. $component_data['componentData']['name'] = '最新资讯';
  1540. }
  1541. if ($data['type_id'] == 5 || $data['type_id'] == 6) {
  1542. $component_data['componentData']['name'] = '热点精选';
  1543. }
  1544. $add_arr['component_data'] = json_encode($component_data, true);
  1545. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1546. break;
  1547. case 7: //7:资讯-栏目类组件;
  1548. case 15: //15:滚动图文类;
  1549. $add_arr['img_num'] = intval($data['img_num']);
  1550. $add_arr['text_num'] = intval($data['text_num']);
  1551. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1552. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1553. $component_data['componentData'] = [
  1554. 'category_id' => '',
  1555. 'level' => '',
  1556. 'name' => '',
  1557. 'category_arr' => [],
  1558. 'imgSize' => $add_arr['img_num'] ?? '',
  1559. 'textSize' => $add_arr['text_num'] ?? '',
  1560. 'child' => [
  1561. 'id' => '',
  1562. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1563. 'textSize' => $add_arr['child_textnum'] ?? '',
  1564. ]
  1565. ];
  1566. $add_arr['component_data'] = json_encode($component_data,true);
  1567. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1568. break;
  1569. case 8: //8:列表类组件;
  1570. $add_arr['pageSize'] = intval($data['pageSize']);
  1571. $component_data['componentData'] = [
  1572. 'category_id' => '',
  1573. 'pageType' => [
  1574. 'page' => 1,
  1575. 'pageSize' => $add_arr['pageSize'] ?? '',
  1576. ]
  1577. ];
  1578. $add_arr['component_data'] = json_encode($component_data,true);
  1579. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1580. break;
  1581. case 9: //9:详情类组件;
  1582. $component_data['componentData'] = [
  1583. 'article_id' => '',
  1584. ];
  1585. $add_arr['component_data'] = json_encode($component_data,true);
  1586. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1587. break;
  1588. case 10: //10:二级导航类组件;
  1589. $add_arr['cate_place'] = 1;
  1590. $add_arr['num'] = intval($data['num']);
  1591. $component_data['componentData'] = [
  1592. 'pid' => '',
  1593. 'placeid' => 1,
  1594. 'num' => $add_arr['num'],
  1595. ];
  1596. $add_arr['component_data'] = json_encode($component_data,true);
  1597. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1598. break;
  1599. case 11: //11:广告类;
  1600. $add_arr['ad_width'] = intval($data['ad_width']);
  1601. $add_arr['ad_height'] = intval($data['ad_height']);
  1602. $add_arr['component_width'] = intval($data['ad_width']);
  1603. $add_arr['component_height'] = intval($data['ad_height']);
  1604. $add_arr['ad_type'] = intval($data['ad_type']);
  1605. // $add_arr['ad_img'] = $data['ad_img'];
  1606. $ad = [
  1607. 'width' => $add_arr['ad_width'],
  1608. 'height' => $add_arr['ad_height'],
  1609. 'name' => '',
  1610. 'price' => '',
  1611. 'introduce' => '',
  1612. 'website_id' => '',
  1613. // 'thumb' => $add_arr['ad_img'],
  1614. 'typeid' => $add_arr['ad_type'],
  1615. 'ad_tag' => '',
  1616. ];
  1617. $add_arr['ad'] = json_encode($ad,true);
  1618. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1619. // $add_arr['adimg_info'] = $data['img_info'];
  1620. // $ad_imginfo = json_decode($data['img_info'],true);
  1621. break;
  1622. // case 13: //13:底部导航类;
  1623. case 17: //17:单页列表类;
  1624. case 18: //18:单页详情类;
  1625. $component_data['componentData'] = [
  1626. 'fcat_id' => '',
  1627. ];
  1628. $add_arr['component_data'] = json_encode($component_data,true);
  1629. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1630. break;
  1631. case 14: //14:栏目资讯-广告混合类;
  1632. $add_arr['ad_width'] = intval($data['ad_width']);
  1633. $add_arr['ad_height'] = intval($data['ad_height']);
  1634. $add_arr['component_width'] = intval($data['component_width']);
  1635. $add_arr['component_height'] = intval($data['component_height']);
  1636. $add_arr['ad_type'] = intval($data['ad_type']);
  1637. $add_arr['ad_img'] = $data['ad_img'];
  1638. $add_arr['img_num'] = intval($data['img_num']);
  1639. $add_arr['text_num'] = intval($data['text_num']);
  1640. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1641. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1642. $component_data['componentData'] = [
  1643. 'category_id' => '',
  1644. 'level' => '',
  1645. 'name' => '',
  1646. 'category_arr' => [],
  1647. 'imgSize' => $data['img_num'] ?? '',
  1648. 'textSize' => $data['text_num'] ?? '',
  1649. 'child' => [
  1650. 'id' => '',
  1651. 'imgSize' => $data['child_imgnum'] ?? '',
  1652. 'textSize' => $data['child_textnum'] ?? '',
  1653. ]
  1654. ];
  1655. $ad = [
  1656. 'width' => $add_arr['ad_width'],
  1657. 'height' => $add_arr['ad_height'],
  1658. 'name' => '',
  1659. 'price' => '',
  1660. 'introduce' => '',
  1661. 'website_id' => '',
  1662. 'thumb' => $add_arr['ad_img'],
  1663. 'typeid' => $add_arr['ad_type'],
  1664. 'ad_tag' => '',
  1665. ];
  1666. $add_arr['ad'] = json_encode($ad,true);
  1667. $add_arr['component_data'] = json_encode($component_data,true);
  1668. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1669. // $add_arr['adimg_info'] = $data['img_info'];
  1670. // $ad_imginfo = json_decode($data['img_info'],true);
  1671. break;
  1672. default:
  1673. break;
  1674. }
  1675. if($data['type_id'] == 24){
  1676. unset($add_arr['ad']);
  1677. $add_arr['ad'][0] = $ad;
  1678. $add_arr['ad'][1] = $ad;
  1679. $add_arr['ad'] = json_encode($add_arr['ad'],true);
  1680. }
  1681. // if----带广告的特殊处理(广告位默认图上传静态资源的处理---已废弃)
  1682. // if ($data['type_id'] == 11 || $data['type_id'] == 14) {
  1683. // Db::beginTransaction();
  1684. // try {
  1685. // // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1686. // // var_dump($website_img);
  1687. // // $add_arr['ad_imgid'] = $website_img;
  1688. // // if(empty($website_img)){
  1689. // // Db::rollBack();
  1690. // // return Result::error('广告默认图上传失败!');
  1691. // // }
  1692. // $result = Component::insertGetId($add_arr);
  1693. // if (empty($result)) {
  1694. // Db::rollBack();
  1695. // return Result::error('添加失败!');
  1696. // }
  1697. // Db::commit();
  1698. // } catch (\Exception $e) {
  1699. // Db::rollback();
  1700. // return Result::error('添加失败!');
  1701. // }
  1702. // }
  1703. // else {
  1704. // return Result::success($add_arr);
  1705. $result = Component::insertGetId($add_arr);
  1706. if (empty($result)) {
  1707. return Result::error('添加组件失败!');
  1708. }
  1709. // }
  1710. return Result::success($result);
  1711. }
  1712. public function delComponent(array $data): array
  1713. {
  1714. $component = Component::where('id', $data['id'])->first();
  1715. if(empty($component)){
  1716. return Result::error('组件不存在!');
  1717. }
  1718. $component_id = SectorComponent::where('component_id', $component['component_type'])->first();
  1719. if (!empty($component_id)) {
  1720. return Result::error('请先解除相关组件关联关系!');
  1721. }
  1722. Db::beginTransaction();
  1723. try{
  1724. // if($component['type_id'] == 11 || $component['type_id'] == 14){
  1725. // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
  1726. // if(!empty($ad_img)){
  1727. // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
  1728. // if(empty($ad_img)){
  1729. // Db::rollBack();
  1730. // return Result::error('删除广告默认图失败!');
  1731. // }
  1732. // }
  1733. // }
  1734. $component_img = ComponentImg::where('component_id',$component['component_type'])->first();
  1735. if(!empty($component_img)){
  1736. $del_img = ComponentImg::where('component_id',$component['component_type'])->delete();
  1737. if(empty($del_img)){
  1738. Db::rollBack();
  1739. return Result::error('删除组件预览图失败!');
  1740. }
  1741. }
  1742. $result = Component::where('id',$data['id'])->delete();
  1743. if(empty($result)){
  1744. Db::rollBack();
  1745. return Result::error('删除失败!');
  1746. }
  1747. Db::commit();
  1748. return Result::success($result);
  1749. }catch(\Exception $e){
  1750. Db::rollback();
  1751. return Result::error('删除失败!');
  1752. }
  1753. }
  1754. public function updateComponent(array $data): array
  1755. {
  1756. unset($data['user_id']);
  1757. $id = $data['id'];
  1758. unset($data['id']);
  1759. $component_id = Component::where('id','!=',$id)->where('component_type',$data['component_type'])->first();
  1760. if(!empty($component_id) || $component_id != null){
  1761. return Result::error('组件编号已存在!');
  1762. }
  1763. $component_type = ComponentType::where('id',$data['type_id'])->first();
  1764. if(empty($component_type)){
  1765. return Result::error('组件分类不存在!');
  1766. }
  1767. $component = Component::where('id',$id)->first();
  1768. if(empty($component)){
  1769. return Result::error('组件不存在!');
  1770. }
  1771. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;
  1772. // 7:资讯-栏目类组件;8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;
  1773. $data['type_id'] = intval($data['type_id']);
  1774. $add_arr = [
  1775. // 'template_id' => intval($data['template_id']),
  1776. 'component_type' => intval($data['component_type']),
  1777. 'component_name' => $data['component_name'],
  1778. 'component_img' => $data['component_img'],
  1779. 'component_width' => $data['component_width'],
  1780. 'component_height' => $data['component_height'],
  1781. 'type_id' => intval($data['type_id']),
  1782. 'component_keyword' => $data['component_keyword'],
  1783. ];
  1784. $component_head_code = [
  1785. 'component_id' => $add_arr['component_type'],
  1786. 'component_type' => $add_arr['type_id'],
  1787. 'component_name' => $add_arr['component_name'],
  1788. 'component_style' => 1,
  1789. ];
  1790. $add_arr['component_code'] = json_encode($component_head_code, true);
  1791. $type = $data['type_id'];
  1792. switch ($type) {
  1793. case 19: //19:选项卡-资讯-推荐图类组件;
  1794. $type = 3;
  1795. break;
  1796. case 20: //20:选项卡-资讯-最新类组件;
  1797. $type = 4;
  1798. break;
  1799. case 21: //21:选项卡-资讯-推荐类组件;
  1800. $type = 5;
  1801. break;
  1802. case 22: //22:选项卡-资讯-热点类组件;
  1803. $type = 6;
  1804. break;
  1805. case 23: //23:选项卡-资讯-栏目类组件;
  1806. $type = 7;
  1807. break;
  1808. case 24: //24:选项卡-资讯-广告混合类组件;
  1809. $type = 14;
  1810. break;
  1811. }
  1812. switch ($type) {
  1813. case 1: //1:资讯-头条组件;
  1814. case 2: //2:资讯-轮播组件;
  1815. case 3: //3:资讯-推荐图类组件;
  1816. case 4: //4:资讯-最新类组件;
  1817. case 5: //5:资讯-推荐类组件;
  1818. case 6: //6:资讯-热点类组件;
  1819. // $component_data['componentData']['name'] = '';
  1820. $add_arr['level'] = $data['type_id'];
  1821. $add_arr['img_num'] = intval($data['img_num']);
  1822. $add_arr['text_num'] = intval($data['text_num']);
  1823. $component_data['componentData'] = [
  1824. // 'category_id' => '',
  1825. 'level' => $add_arr['level'],
  1826. // 'category_arr' => [],
  1827. 'name' => '',
  1828. 'imgSize' => $data['img_num'] ?? '',
  1829. 'textSize' => $data['text_num'] ?? '',
  1830. 'child' => [
  1831. 'id' => '',
  1832. 'imgSize' => '',
  1833. 'textSize' => ''
  1834. ]
  1835. ];
  1836. if ($data['type_id'] == 4) {
  1837. $component_data['componentData']['name'] = '最新资讯';
  1838. }
  1839. if ($data['type_id'] == 5 || $data['type_id'] == 6) {
  1840. $component_data['componentData']['name'] = '热点精选';
  1841. }
  1842. $add_arr['component_data'] = json_encode($component_data, true);
  1843. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1844. break;
  1845. case 7: //7:资讯-栏目类组件;
  1846. case 15: //15:滚动图文类;
  1847. $add_arr['img_num'] = intval($data['img_num']);
  1848. $add_arr['text_num'] = intval($data['text_num']);
  1849. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1850. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1851. $component_data['componentData'] = [
  1852. 'category_id' => '',
  1853. 'level' => '',
  1854. 'name' => '',
  1855. 'category_arr' => [],
  1856. 'imgSize' => $add_arr['img_num'] ?? '',
  1857. 'textSize' => $add_arr['text_num'] ?? '',
  1858. 'child' => [
  1859. 'id' => '',
  1860. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1861. 'textSize' => $add_arr['child_textnum'] ?? '',
  1862. ]
  1863. ];
  1864. $add_arr['component_data'] = json_encode($component_data,true);
  1865. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1866. break;
  1867. case 8: //8:列表类组件;
  1868. $add_arr['pageSize'] = intval($data['pageSize']);
  1869. $component_data['componentData'] = [
  1870. 'category_id' => '',
  1871. 'pageType' => [
  1872. 'page' => 1,
  1873. 'pageSize' => $add_arr['pageSize'] ?? '',
  1874. ]
  1875. ];
  1876. $add_arr['component_data'] = json_encode($component_data,true);
  1877. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1878. break;
  1879. case 9: //9:详情类组件;
  1880. $component_data['componentData'] = [
  1881. 'article_id' => '',
  1882. ];
  1883. $add_arr['component_data'] = json_encode($component_data,true);
  1884. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1885. break;
  1886. case 10: //10:二级导航类组件;
  1887. $add_arr['cate_place'] = 1;
  1888. $add_arr['num'] = intval($data['num']);
  1889. $component_data['componentData'] = [
  1890. 'pid' => '',
  1891. 'placeid' => 1,
  1892. 'num' => $add_arr['num'],
  1893. ];
  1894. $add_arr['component_data'] = json_encode($component_data,true);
  1895. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1896. break;
  1897. case 11: //11:广告类;
  1898. $add_arr['ad_width'] = intval($data['ad_width']);
  1899. $add_arr['ad_height'] = intval($data['ad_height']);
  1900. $add_arr['component_width'] = intval($data['ad_width']);
  1901. $add_arr['component_height'] = intval($data['ad_height']);
  1902. $add_arr['ad_type'] = intval($data['ad_type']);
  1903. // $add_arr['ad_img'] = $data['ad_img'];
  1904. $ad = [
  1905. 'width' => $add_arr['ad_width'],
  1906. 'height' => $add_arr['ad_height'],
  1907. 'name' => '',
  1908. 'price' => '',
  1909. 'introduce' => '',
  1910. 'website_id' => '',
  1911. 'thumb' => $add_arr['ad_img'],
  1912. // 'typeid' => $add_arr['ad_type'],
  1913. 'ad_tag' => '',
  1914. ];
  1915. $add_arr['ad'] = json_encode($ad, true);
  1916. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1917. // $add_arr['adimg_info'] = $data['img_info'];
  1918. // $ad_imginfo = json_decode($data['img_info'],true);
  1919. break;
  1920. // case 13: //13:底部导航类;
  1921. case 17: //17:单页列表类;
  1922. case 18: //18:单页详情类;
  1923. $component_data['componentData'] = [
  1924. 'fcat_id' => '',
  1925. ];
  1926. $add_arr['component_data'] = json_encode($component_data,true);
  1927. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1928. break;
  1929. case 14:
  1930. $add_arr['ad_width'] = intval($data['ad_width']);
  1931. $add_arr['ad_height'] = intval($data['ad_height']);
  1932. $add_arr['component_width'] = intval($data['component_width']);
  1933. $add_arr['component_height'] = intval($data['component_height']);
  1934. $add_arr['ad_type'] = intval($data['ad_type']);
  1935. $add_arr['ad_img'] = $data['ad_img'];
  1936. $add_arr['img_num'] = intval($data['img_num']);
  1937. $add_arr['text_num'] = intval($data['text_num']);
  1938. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1939. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1940. $component_data['componentData'] = [
  1941. 'category_id' => '',
  1942. 'level' => '',
  1943. 'name' => '',
  1944. 'category_arr' => [],
  1945. 'imgSize' => $data['img_num'] ?? '',
  1946. 'textSize' => $data['text_num'] ?? '',
  1947. 'child' => [
  1948. 'id' => '',
  1949. 'imgSize' => $data['child_imgnum'] ?? '',
  1950. 'textSize' => $data['child_textnum'] ?? '',
  1951. ]
  1952. ];
  1953. $ad = [
  1954. 'width' => $add_arr['ad_width'],
  1955. 'height' => $add_arr['ad_height'],
  1956. 'name' => '',
  1957. 'price' => '',
  1958. 'introduce' => '',
  1959. 'website_id' => '',
  1960. 'thumb' => $add_arr['ad_img'],
  1961. 'typeid' => $add_arr['ad_type'],
  1962. 'ad_tag' => '',
  1963. ];
  1964. $add_arr['ad'] = json_encode($ad,true);
  1965. $add_arr['component_data'] = json_encode($component_data,true);
  1966. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1967. // $add_arr['adimg_info'] = $data['img_info'];
  1968. // $ad_imginfo = json_decode($data['img_info'],true);
  1969. break;
  1970. default:
  1971. break;
  1972. }
  1973. // var_dump("ad_imginfo:",$ad_imginfo);
  1974. $cll_column = [
  1975. 'img_num' => null,
  1976. 'text_num' => null,
  1977. 'child_imgnum' => null,
  1978. 'child_textnum' => null,
  1979. 'category_id' => null,
  1980. 'child_id' => null,
  1981. 'num' => null,
  1982. 'level' => null,
  1983. 'fcatid' => null,
  1984. 'article_id' => null,
  1985. 'cate_place' => null,
  1986. 'pageSize' => null,
  1987. // 'ad_img' => null,
  1988. 'ad_width' => null,
  1989. 'ad_height' => null,
  1990. 'ad' => '',
  1991. 'ad_type' => null,
  1992. // 'ad_imgid' => null,
  1993. 'adimg_info' => '[]',
  1994. ];
  1995. if($data['type_id'] == 24){
  1996. unset($add_arr['ad']);
  1997. $add_arr['ad'][0] = $ad;
  1998. $add_arr['ad'][1] = $ad;
  1999. $add_arr['ad'] = json_encode($add_arr['ad'],true);
  2000. }
  2001. Db::beginTransaction();
  2002. try{
  2003. // return Result::success($add_arr);
  2004. // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] != 11 && $data['type_id'] != 14)){
  2005. // // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
  2006. // if(empty($website_img)){
  2007. // Db::rollBack();
  2008. // return Result::error('广告默认图删除失败!');
  2009. // }
  2010. // var_dump("删除",$website_img);
  2011. // }
  2012. // if(($data['type_id'] == 11 || $data['type_id'] == 14) && ($component['type_id'] != 11 && $component['type_id'] != 14)){
  2013. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  2014. // $add_arr['ad_imgid'] = $website_img;
  2015. // if(empty($website_img)){
  2016. // Db::rollBack();
  2017. // return Result::error('广告默认图上传失败!');
  2018. // }
  2019. // var_dump("上传",$website_img);
  2020. // }
  2021. // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] == 11 || $data['type_id'] == 14)){
  2022. // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
  2023. // if(empty($website_img)){
  2024. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  2025. // $add_arr['ad_imgid'] = $website_img;
  2026. // }else{
  2027. // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->update($ad_imginfo);
  2028. // $add_arr['ad_imgid'] = $component['ad_imgid'];
  2029. // }
  2030. // if(empty($website_img)){
  2031. // Db::rollBack();
  2032. // return Result::error('广告默认图修改失败!');
  2033. // }
  2034. // }
  2035. $add_arr = array_merge($cll_column,$add_arr);
  2036. $result = Component::where('id',$id)->update($add_arr);
  2037. if(empty($result)){
  2038. Db::rollBack();
  2039. return Result::error('修改失败!');
  2040. }
  2041. Db::commit();
  2042. }catch(\Exception $e){
  2043. Db::rollBack();
  2044. return Result::error('修改失败!');
  2045. }
  2046. return Result::success($result);
  2047. }
  2048. public function getWebsiteTemplateInfo(array $data)
  2049. {
  2050. $where = [];
  2051. if (isset($data['id'])) {
  2052. $where[] = ['id', '=', $data['id']];
  2053. }
  2054. $result = WebsiteTemplateInfo::where($where)
  2055. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  2056. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  2057. ->select('website_template_info.*', 'website.website_name')
  2058. ->get();
  2059. if ($result) {
  2060. return Result::success($result);
  2061. } else {
  2062. return Result::error('暂无数据');
  2063. }
  2064. }
  2065. /**
  2066. * 自助建站----1.获取页面类型回显
  2067. * @param array $data
  2068. * @return array
  2069. */
  2070. public function getWebPageType(array $data): array
  2071. {
  2072. // 1:图片 2:文字 3:底部
  2073. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  2074. if (empty($friend_link->toArray())) {
  2075. return Result::error('暂无友情链接数据!');
  2076. }
  2077. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  2078. if (empty($footer_category->toArray())) {
  2079. return Result::error('暂无底部导航数据!');
  2080. }
  2081. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  2082. if (empty($page_type)) {
  2083. $page_type = [];
  2084. } else {
  2085. $page_type = json_decode($page_type, true);
  2086. }
  2087. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  2088. if (isset($page_type) && in_array(5, $page_type)) {
  2089. $page_type = 1;
  2090. } else {
  2091. $page_type = 0;
  2092. }
  2093. $result = [
  2094. 'friend_link' => $friend_link,
  2095. 'footer_category' => $footer_category,
  2096. 'page_type' => $page_type,
  2097. ];
  2098. return Result::success($result);
  2099. }
  2100. /**
  2101. * 自助建站----2.添加页面类型
  2102. * @param array $data
  2103. * @return array
  2104. */
  2105. public function addWebPageType(array $data): array
  2106. {
  2107. // return Result::success($data);
  2108. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  2109. if ($data['is_search'] == 1) {
  2110. $page_type = json_encode([1,2,3,4,5,6,7]);
  2111. } else {
  2112. $page_type = json_encode([1,2,3,4,6,7]);
  2113. }
  2114. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  2115. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2116. $website_template = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  2117. Db::beginTransaction();
  2118. try{
  2119. if (empty($website_template_info)) {
  2120. $result['template_info'] = WebsiteTemplateInfo::insertGetId([
  2121. 'website_id' => $data['website_id'],
  2122. 'page_type' => $page_type,
  2123. 'user_id' => $data['user_id'],
  2124. 'status' => 0,
  2125. 'action_id' => 1,
  2126. ]);
  2127. } else {
  2128. if ($website_template_info['status'] == 2) {
  2129. Db::rollBack();
  2130. return Result::error('网站已应用,不可再次修改!');
  2131. }
  2132. $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type, 'action_id' => 1, 'user_id' => $data['user_id']]);
  2133. }
  2134. if (!$result['template_info']) {
  2135. Db::rollBack();
  2136. return Result::error('网站所选页面添加失败');
  2137. }
  2138. if(empty($website_template)){
  2139. $result['template'] = WebsiteTemplate::insertGetId([
  2140. 'website_id' => $data['website_id'],
  2141. 'page_type' => $page_type,
  2142. 'user_id' => $data['user_id'],
  2143. ]);
  2144. }else{
  2145. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])->update([
  2146. 'page_type' => $page_type,
  2147. ]);
  2148. }
  2149. if(!$result['template']){
  2150. Db::rollBack();
  2151. return Result::error('添加网站预制模版信息失败!');
  2152. }
  2153. Db::commit();
  2154. return Result::success($result);
  2155. }catch(\Exception $e){
  2156. Db::rollBack();
  2157. return Result::error('添加失败');
  2158. }
  2159. }
  2160. /**
  2161. * 自助建站---流程---3.选择皮肤列表
  2162. * @param array $data
  2163. * @return array
  2164. */
  2165. public function getWebsiteTemplateList(array $data): array
  2166. {
  2167. $where = [];
  2168. if (isset($data['template_class_id']) && $data['template_class_id']) {
  2169. $where[] = ['template_class_id', '=', $data['template_class_id']];
  2170. }
  2171. // 0:未构建 1:未应用 2:已应用 status
  2172. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2173. if (empty($templste_info)) {
  2174. return Result::error('请先填写网站基础信息!');
  2175. }
  2176. if ($templste_info['status'] == 2) {
  2177. return Result::error('网站已应用,不可再次修改!');
  2178. }
  2179. if (isset($data['keyword']) && !empty($data['keyword'])) {
  2180. if (is_array($data['keyword'])) {
  2181. // 拼接多个模糊查询条件
  2182. $query = Template::query();
  2183. foreach ($data['keyword'] as $kw) {
  2184. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  2185. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  2186. }
  2187. } else {
  2188. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  2189. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  2190. }
  2191. } else {
  2192. $query = Template::where($where);
  2193. }
  2194. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  2195. // return Result::success($template_id);
  2196. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  2197. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  2198. } else {
  2199. $template_page = [];
  2200. }
  2201. if (!empty($template_id['template_id'])) {
  2202. $result['template_id'] = $templste_info['template_id'];
  2203. }
  2204. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  2205. $template_page_str = json_encode($template_page);
  2206. $result['template'] = $query->where($where)
  2207. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  2208. ->orderBy('template.template_id', 'desc')
  2209. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2210. if (empty($result['template'])) {
  2211. $result['template'] = Template::orderBy('template.template_id', 'desc')
  2212. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2213. }
  2214. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  2215. if ($template) {
  2216. $result['template_id'] = $template;
  2217. } else {
  2218. $result['template_id'] = 0;
  2219. }
  2220. return Result::success($result);
  2221. }
  2222. /**
  2223. * 自助建站---流程---4.选择皮肤
  2224. * @param array $data
  2225. * @return array
  2226. */
  2227. public function chooseWebsiteTemplate(array $data): array
  2228. {
  2229. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2230. if (empty($website_template_info)) {
  2231. return Result::error('请先填写网站基础信息!');
  2232. }
  2233. // 0:未构建 1:未应用 2:已应用 status
  2234. if ($website_template_info['status'] == 2) {
  2235. return Result::error('网站已应用,不可再次修改!');
  2236. }
  2237. $template = Template::where('template_id', $data['template_id'])->first();
  2238. if (empty($template)) {
  2239. return Result::error('未查询到皮肤!');
  2240. }
  2241. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  2242. // 分别更新 WebsiteTemplateInfo 和 WebsiteTemplate 表
  2243. Db::beginTransaction();
  2244. try {
  2245. if($data['template_id'] != $website_template_info['template_id']){
  2246. $info_data = [
  2247. 'template_id' => $data['template_id'],
  2248. 'status' => 0,
  2249. 'action_id' => 2,
  2250. 'user_id'=> $data['user_id']
  2251. ];
  2252. $template = [
  2253. 'template_id' => $data['template_id'],
  2254. 'user_id'=> $data['user_id'],
  2255. 'template_data' => null,
  2256. 'canvas_data' => null
  2257. ];
  2258. }else{
  2259. $info_data = [
  2260. 'template_id' => $data['template_id'],
  2261. 'user_id'=> $data['user_id'],
  2262. 'action_id' => 2,
  2263. ];
  2264. $template = [
  2265. 'template_id' => $data['template_id'],
  2266. 'user_id'=> $data['user_id']
  2267. ];
  2268. }
  2269. $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])
  2270. ->update($info_data);
  2271. if(empty($result['template_info'])){
  2272. Db::rollBack();
  2273. return Result::error('选择皮肤失败!');
  2274. }
  2275. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])
  2276. ->update($template);
  2277. if(empty($result['template'])){
  2278. Db::rollBack();
  2279. return Result::error('网站模版选择皮肤失败!');
  2280. }
  2281. Db::commit();
  2282. return Result::success($result);
  2283. } catch (\Exception $e) {
  2284. Db::rollBack();
  2285. return Result::error('选择皮肤失败!');
  2286. }
  2287. }
  2288. /**
  2289. * 自助建站---流程---5.保存模版
  2290. * @param array $data
  2291. * @return array
  2292. */
  2293. public function saveWebsiteTemplate(array $data): array
  2294. {
  2295. // return Result::success($data);
  2296. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2297. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  2298. return Result::error('请先填写网站基础信息!');
  2299. }
  2300. // 0:未构建 1:未应用 2:已应用 status
  2301. if ($website_template_info['status'] == 2) {
  2302. return Result::error('网站已应用,不可再次保存!');
  2303. }
  2304. if ($website_template_info['action_id'] != 2) {
  2305. return Result::error('请完成之前的步骤!');
  2306. }
  2307. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  2308. // var_dump($data['template_data']);
  2309. try {
  2310. Db::beginTransaction();
  2311. if (empty($template_info)) {
  2312. Db::rollback();
  2313. return Result::error('该网站不存在已保存的模版!');
  2314. } else {
  2315. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  2316. [
  2317. 'template_data' => $data['template_data'],
  2318. 'user_id' => $data['user_id'],
  2319. 'canvas_data' => $data['canvas_data'],
  2320. ]
  2321. );
  2322. }
  2323. if (empty($template)) {
  2324. Db::rollback();
  2325. return Result::error('保存失败!');
  2326. } else {
  2327. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  2328. if (empty($tempalte_info)) {
  2329. Db::rollback();
  2330. return Result::error('网站搭建状态保存失败!');
  2331. }
  2332. Db::commit();
  2333. }
  2334. } catch (\Exception $e) {
  2335. return Result::error('模版数据格式错误!');
  2336. }
  2337. return Result::success($template);
  2338. }
  2339. /**
  2340. * 自助建站---流程---6.返显保存的模版
  2341. * @param array $data
  2342. * @return array
  2343. */
  2344. public function getWebsiteTemplate(array $data): array
  2345. {
  2346. $web = Website::where('id', $data['website_id'])->first();
  2347. if (empty($web)) {
  2348. return Result::error('未查询到网站信息!');
  2349. }
  2350. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  2351. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  2352. if (empty($template)) {
  2353. return Result::error('未查询到模版!');
  2354. }
  2355. $template['page_type'] = json_decode($template['page_type'],true);
  2356. return Result::success($template);
  2357. }
  2358. /**
  2359. * 通栏版式管理-获取通栏版式列表
  2360. */
  2361. public function getSectorPlaceList(array $data): array
  2362. {
  2363. $where['sector_place.type_id'] = $data['type_id'];
  2364. if(isset($data['component_num']) && !empty($data['component_num'])){
  2365. $where['sector_place.component_num'] = $data['component_num'];
  2366. }
  2367. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2368. $where['sector_place.sector_type'] = $data['sector_type'];
  2369. }
  2370. // if(isset($data['component_type']) && !empty($data['component_type'])){
  2371. // $where['sector_place.component_type'] = $data['component_type'];
  2372. // }
  2373. if(isset($data['name']) && !empty($data['name'])){
  2374. array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
  2375. }
  2376. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  2377. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2378. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2379. } else if( isset($data['width']) && !empty($data['width'])){
  2380. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2381. }else if( isset($data['height']) && !empty($data['height'])){
  2382. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2383. }else{
  2384. $size_id = [];
  2385. }
  2386. $sector_place = SectorPlace::where($where)
  2387. ->select('sector_place.*');
  2388. $result['count'] = $sector_place->count();
  2389. $result['row'] = $sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2390. if($data['type_id'] == 2){
  2391. $result['map_json'] = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->pluck('map');
  2392. }
  2393. if (empty($result['count'])) {
  2394. return Result::error("暂无相关版式数据!");
  2395. }
  2396. return Result::success($result);
  2397. }
  2398. /**
  2399. * 通栏版式管理-添加通栏版式
  2400. */
  2401. public function addSectorPlace(array $data): array
  2402. {
  2403. unset($data['user_id']);
  2404. // 1:通栏;2:组件;
  2405. if ($data['type_id'] == 1) {
  2406. $sector_place = SectorPlace::where('sector_type', $data['sector_type'])->first();
  2407. if (!empty($sector_place)) {
  2408. return Result::error('通栏版式类别编号已存在!');
  2409. }
  2410. $data['sort_id'] = 0;
  2411. } else {
  2412. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->first();
  2413. if (empty($sector_type)) {
  2414. return Result::error('通栏版式类别不存在!');
  2415. }
  2416. $component = SectorPlace::where('type_id', 2)->where('sector_type', $data['sector_type'])->get()->all();
  2417. if(!empty($component)){
  2418. if(count($component) >= $sector_type['component_num']){
  2419. return Result::error('通栏版式组件数量已达上限!');
  2420. }
  2421. if(in_array($data['sort_id'], array_column($component, 'sort_id')) && in_array($data['line_num'], array_column($component, 'line_num')) && in_array($data['column_num'], array_column($component, 'column_num'))){
  2422. return Result::error('组件版式位置已存在!');
  2423. }
  2424. if(in_array($data['component_type'], array_column($component, 'component_type'))){
  2425. return Result::error('组件版式编号已存在!');
  2426. }
  2427. $data['component_num'] = 0;
  2428. }
  2429. if(in_array($data['type'],[23,24])){
  2430. $data['cat_num'] = 2;
  2431. }else if(in_array($data['type'],[7,14])){
  2432. $data['cat_num'] = 1;
  2433. }else{
  2434. $data['cat_num'] = 0;
  2435. }
  2436. }
  2437. Db::beginTransaction();
  2438. try{
  2439. $result = SectorPlace::insertGetId($data);
  2440. if (empty($result)) {
  2441. Db::rollBack();
  2442. return Result::error('添加失败!');
  2443. }
  2444. if($data['type_id'] == 2 && count($component)+1 == $sector_type['component_num']){
  2445. $map = $this->changeSectorPlaceJson($sector_type['sector_type']);
  2446. $sector_place_map = SectorPlace::where('sector_type', $sector_type['sector_type'])
  2447. ->where('type_id', 1)->update(['map' => json_encode($map['map_json']),'cat_num'=>$map['cat_num']]);
  2448. if($sector_place_map === false){
  2449. Db::rollBack();
  2450. return Result::error('通栏版式画布数据更新失败!');
  2451. }
  2452. }
  2453. Db::commit();
  2454. return Result::success($result);
  2455. }catch(\Exception $e){
  2456. Db::rollBack();
  2457. return Result::error($e->getMessage());
  2458. }
  2459. }
  2460. /**
  2461. * 通栏版式管理-版式画布
  2462. * @param array $data
  2463. * @return array
  2464. */
  2465. public function changeSectorPlaceJson($sector_type)
  2466. {
  2467. $component = SectorPlace::where('type_id', 2)->where('sector_type', $sector_type)->get()->all();
  2468. // $component[count($component)] = $data;
  2469. $sector_type_info = SectorPlace::where('sector_type', $sector_type)->where('type_id', 1)->first();
  2470. if (empty($sector_type_info)) {
  2471. return Result::error('通栏版式类别不存在!');
  2472. }
  2473. $row_num = 1;
  2474. $cat_num = 0;
  2475. $row_width = [];
  2476. $map = [];
  2477. if(count($component) == $sector_type_info['component_num']){
  2478. foreach($component as $key => $val){
  2479. if(in_array($val['type'],[23,24])){
  2480. $cat_num = $cat_num + 2;
  2481. }
  2482. if(in_array($val['type'],[7,14])){
  2483. $cat_num = $cat_num + 1;
  2484. }
  2485. if(!isset($map[$val['column_num'] - 1]['col_data']['row_num'])){
  2486. $row_num = 1;
  2487. $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num;
  2488. }else{
  2489. $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num +1;
  2490. }
  2491. // 原代码使用 $sector_type['width'] 有误,应该使用 $sector_type_info
  2492. $width = number_format((($val['width'] / $sector_type_info['width']) * 100 - 1.5), 2);
  2493. $height = number_format((($val['height'] / $sector_type_info['height']) * 100 - 2.5), 2);
  2494. $col_width = $width;
  2495. if(!isset($column_num_width[$val['column_num']])){
  2496. $column_num_width[$val['column_num']] = $val['width'];
  2497. var_dump(array_keys($column_num_width));
  2498. }else if($column_num_width[$val['column_num']] != $val['width']){
  2499. $col_width = min($val['width'],$column_num_width[$val['column_num']]);
  2500. $col_width = number_format((($col_width / $sector_type_info['width']) * 100 - 1.5), 2);
  2501. $width = number_format((($val['width'] / $column_num_width[$val['column_num']]) * 100 - 2.5), 2);
  2502. }
  2503. $row_key = intval($val['line_num'] - 1);
  2504. $map[$val['column_num'] - 1]['col_data']['row_data'][$row_key] = [
  2505. 'row_width' => $width.'%',
  2506. 'row_height' => $height.'%',
  2507. 'component_sort' => $val['sort_id'],
  2508. ];
  2509. $map[$val['column_num'] - 1]['col_data']['col_width'] = $col_width.'%';
  2510. }
  2511. }
  2512. $map_data = [
  2513. 'map_json'=>$map ?? [],
  2514. 'component'=>count($component),
  2515. 'row_num'=>$row_num ?? 0,
  2516. 'cat_num'=>$cat_num ?? 0,
  2517. 'component_info'=>$component,
  2518. ];
  2519. return $map_data;
  2520. }
  2521. /**
  2522. * 通栏版式管理-修改通栏版式
  2523. */
  2524. public function upSectorPlace($data): array
  2525. {
  2526. unset($data['user_id']);
  2527. // return Result::success($data);
  2528. // $id = $data['id'];
  2529. // 1:通栏;2:组件;
  2530. if ($data['type_id'] == 1) {
  2531. $sector_type = SectorPlace::where('id', $data['id'])
  2532. ->where('type_id',1)
  2533. ->first();
  2534. if (empty($sector_type)) {
  2535. return Result::error('通栏版式不存在!');
  2536. }
  2537. unset($data['sort_id']);
  2538. unset($data['component_type']);
  2539. unset($data['type']);
  2540. // return Result::success($map);
  2541. $sector_place_id = SectorPlace::where('id', '!=', $data['id'])->where('sector_type', $data['sector_type'])->where('type_id', 1)->first();
  2542. if (!empty($sector_place_id)) {
  2543. return Result::error('通栏版式编号已存在!');
  2544. }
  2545. $map = $this->changeSectorPlaceJson($sector_type['sector_type']) ?? [];
  2546. if($data['component_num'] == $map['component']){
  2547. $data['cat_num'] = $map['cat_num'];
  2548. $data['map'] = json_encode($map['map_json']);
  2549. }else{
  2550. $data['cat_num'] = $map['cat_num'];
  2551. $data['map'] = '';
  2552. }
  2553. if($data['sector_type'] != $sector_type['sector_type'] && $map['component'] != 0){
  2554. $com_place = SectorPlace::where('sector_type', $sector_type['sector_type'])
  2555. ->where('type_id', 2)
  2556. ->update(['sector_type'=>$data['sector_type']]);
  2557. if(empty($com_place)){
  2558. return Result::error('组件版式编号同步更新失败!');
  2559. }
  2560. }
  2561. $map = $this->changeSectorPlaceJson($data['sector_type']) ?? [];
  2562. $data['cat_num'] = $map['cat_num'] ?? 0;
  2563. $map = json_encode($map['map_json'] ?? []);
  2564. $result = SectorPlace::where('id', $data['id'])->update($data);
  2565. } else {
  2566. $component_type = SectorPlace::where('id', $data['id'])->where('type_id', 2)->first();
  2567. if (empty($component_type)) {
  2568. return Result::error('组件版式不存在!');
  2569. }
  2570. unset($data['component_num']);
  2571. unset($data['map']);
  2572. $component = SectorPlace::where('sector_type', $data['sector_type'])
  2573. ->where(function($query)use($data){
  2574. $query->where('component_type', $data['component_type'])
  2575. ->Orwhere('sort_id', $data['sort_id']);
  2576. })
  2577. ->where('id', '!=', $data['id'])
  2578. ->where('type_id', 2)
  2579. ->get()->all();
  2580. if (!empty($component)) {
  2581. return Result::error('组件版式编号或位置已存在!');
  2582. }
  2583. if(in_array($data['type'],[23,24])){
  2584. $data['cat_num'] = 2;
  2585. }else if(in_array($data['type'],[7,14])){
  2586. $data['cat_num'] = 1;
  2587. }else{
  2588. $data['cat_num'] = 0;
  2589. }
  2590. $result['component'] = SectorPlace::where('id', $data['id'])->update($data);
  2591. $map = $this->changeSectorPlaceJson($data['sector_type']) ?? [];
  2592. $data['cat_num'] = $map['cat_num'] ?? 0;
  2593. $map = json_encode($map['map_json'] ?? []);
  2594. $result['map'] = SectorPlace::where('sector_type', $data['sector_type'])
  2595. ->where('type_id', 1)
  2596. ->update(['map'=>$map,'cat_num'=>$data['cat_num']]);
  2597. }
  2598. if (empty($result)) {
  2599. return Result::error('修改失败!');
  2600. }
  2601. // $update_result['sector_place'] = SectorPlace::where('id',$data['id'])->first();
  2602. // $update_result['map'] = $map;
  2603. return Result::success([
  2604. 'result'=>$result,
  2605. ]);
  2606. }
  2607. /**
  2608. * 通栏版式管理-删除通栏版式
  2609. */
  2610. public function delSectorPlace(array $data): array
  2611. {
  2612. $sector_place = SectorPlace::where('id', $data['id'])->first();
  2613. if (empty($sector_place)) {
  2614. return Result::error('通栏版式不存在!');
  2615. }
  2616. $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
  2617. if (!empty($sector)) {
  2618. return Result::error('通栏版式分类已应用,暂不可删除!');
  2619. }
  2620. Db::beginTransaction();
  2621. try{
  2622. if($sector_place['type_id'] == 1){
  2623. $where = ['sector_type'=>$sector_place['sector_type']];
  2624. }else{
  2625. $update_result = SectorPlace::where('sector_type', $sector_place['sector_type'])
  2626. ->where('type_id', 1)
  2627. ->whereNotNull('map')
  2628. ->update(['map' => null]);
  2629. if ($update_result === false) {
  2630. Db::rollBack();
  2631. return Result::error('通栏版式画布数据清空失败!');
  2632. }
  2633. $where = ['id'=>$data['id']];
  2634. }
  2635. $result = SectorPlace::where($where)->delete();
  2636. if (empty($result)) {
  2637. Db::rollBack();
  2638. return Result::error('删除失败!');
  2639. }
  2640. Db::commit();
  2641. return Result::success('删除成功!');
  2642. }catch(\Exception $e){
  2643. Db::rollBack();
  2644. return Result::error($e->getMessage());
  2645. }
  2646. }
  2647. /**
  2648. * 自助建站-通栏版式管理-获取通栏版式详情
  2649. */
  2650. public function getSectorPlaceInfo(array $data): array
  2651. {
  2652. $result = SectorPlace::where('sector_place.id',$data['id'])
  2653. ->leftJoin('size','sector_place.size_id','=','size.id')
  2654. ->select('sector_place.*','size.width','size.height')
  2655. ->first();
  2656. if(empty($result)){
  2657. return Result::error('通栏版式不存在!');
  2658. }
  2659. return Result::success($result);
  2660. }
  2661. /**
  2662. * 自助建站-通栏版式管理-获取通栏版式列表
  2663. */
  2664. public function getSectorPlaceSort(array $data): array
  2665. {
  2666. $sector = SectorPlace::where('sector_type',$data['sector_type'])->first();
  2667. if(empty($sector)){
  2668. return Result::error('通栏版式不存在!');
  2669. }
  2670. $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray();
  2671. if(count($component) < $sector['component_num']){
  2672. // 根据 $sector['component_num'] 的值生成对应元素数量的数组
  2673. if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
  2674. $num = intval($sector['component_num']);
  2675. if ($num > 0 && $num <= 10) {
  2676. // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
  2677. $sector_arr = range(1, $num);
  2678. } else {
  2679. $sector_arr = [];
  2680. }
  2681. } else {
  2682. $sector_arr = [];
  2683. }
  2684. // 获取 $sector_arr 中不在 $component 里的元素
  2685. $sort = array_diff($sector_arr, $component);
  2686. // return Result::success($sort);
  2687. }
  2688. if(!empty($sort)){
  2689. $sort = array_values(array_map(function($value) {
  2690. return ['id' => $value];
  2691. }, $sort));
  2692. }else{
  2693. return Result::error("组件版式数量已达上限!");
  2694. }
  2695. return Result::success($sort);
  2696. }
  2697. /**
  2698. * 获取所有通栏版式
  2699. */
  2700. public function getAllSectorPlace(array $data): array
  2701. {
  2702. $where['type_id'] = $data['type_id'];
  2703. // if($data['type_id'] == 1){
  2704. // $where['status'] = 1;
  2705. // }
  2706. $component_num = null;
  2707. if (isset($data['sector_type']) && !empty($data['sector_type'])) {
  2708. $where['sector_type'] = $data['sector_type'];
  2709. }
  2710. if(isset($data['component_num']) && !empty($data['component_num'])){
  2711. $where['component_num'] = $data['component_num'];
  2712. }
  2713. $sector_place = SectorPlace::where($where)
  2714. ->when($data['type_id'] == 2, function ($query) use ($data) {
  2715. $query->orderBy('sort_id', 'asc');
  2716. })
  2717. ->when(isset($data['component_num']) && !empty($data['component_num']), function ($query) use ($data) {
  2718. // 子查询:统计每个 sector_type 下 type_id=2 的子级数量
  2719. $sub = SectorPlace::selectRaw('sector_type, COUNT(*) as child_count')
  2720. ->where('type_id', 2)
  2721. ->groupBy('sector_type');
  2722. // 关联子查询,并保证子级数量等于传入的 component_num
  2723. $query->leftJoinSub($sub, 'component_place', function ($join) {
  2724. $join->on('sector_place.sector_type', '=', 'component_place.sector_type');
  2725. })->whereRaw('component_place.child_count = ?', [$data['component_num']]);
  2726. })
  2727. ->get()->all();
  2728. if($data['type_id'] == 2){
  2729. foreach($sector_place as $key => $value){
  2730. $sector_place[$key]['component'] = Component::where('type_id',$value['type'])
  2731. ->where('component_width',$value['width'])
  2732. ->where('component_height',$value['height'])
  2733. ->get()->all();
  2734. }
  2735. }
  2736. if(empty($sector_place)){
  2737. return Result::error('通栏版式不存在!');
  2738. }
  2739. return Result::success($sector_place);
  2740. }
  2741. /**
  2742. * 组件管理-获取组件类型
  2743. */
  2744. public function getComponentType(array $data): array
  2745. {
  2746. $where = [];
  2747. if(isset($data['id']) && !empty($data['id'])){
  2748. $where['id'] = $data['id'];
  2749. }
  2750. $component_type = ComponentType::where($where)->get()->all();
  2751. if(empty($component_type)){
  2752. return Result::error('组件类型不存在!');
  2753. }
  2754. if(count($component_type) == 1){
  2755. $all_code = json_decode($component_type[0]['com_code'] ?? '',true);
  2756. $component_type[0]['all_code'] = $all_code['listType'] ?? [];
  2757. $result = $component_type[0];
  2758. }else{
  2759. $result = $component_type;
  2760. }
  2761. if(empty($component_type)){
  2762. return Result::error('组件类型不存在!');
  2763. }
  2764. return Result::success($component_type);
  2765. }
  2766. /**
  2767. * 组件管理-获取所有组件
  2768. */
  2769. public function getAllComponent(array $data): array
  2770. {
  2771. $where = [];
  2772. if ((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))) {
  2773. $sector_id['sector.sector_id'] = $data['sector_id'];
  2774. $sector['sector_component.sort_id'] = $data['sort_id'];
  2775. $sectors = Sector::where($sector_id)
  2776. ->where('sector.template_id', $data['template_id'])
  2777. ->leftJoin('sector_component', 'sector.id', 'sector_component.sectorid')
  2778. ->where($sector)
  2779. ->select('sector.template_id', 'sector_component.component_id')
  2780. ->get()->all();
  2781. // return Result::success($sectors);
  2782. $component_id = array_column($sectors, 'component_id');
  2783. $template = ['template_id' => $sectors[0]['template_id']];
  2784. $component = ComponentImg::whereIn('component_id', $component_id)
  2785. ->where($template)
  2786. ->select('img_id', 'img_name', 'img_url', 'template_id')
  2787. ->orderBy('component_img.img_id', 'asc')
  2788. ->get()->all();
  2789. } else {
  2790. if (isset($data['type_id']) && !empty($data['type_id'])) {
  2791. $where['component.type_id'] = $data['type_id'];
  2792. }
  2793. if(isset($data['width']) && !empty($data['width'])){
  2794. $where['component.component_width'] = $data['width'];
  2795. }
  2796. if(isset($data['height']) && !empty($data['height'])){
  2797. $where['component.component_height'] = $data['height'];
  2798. }
  2799. $component = Component::where($where)
  2800. ->leftJoin('component_img', 'component.component_id', '=', 'component_img.component_id')
  2801. ->select('component.*', 'component_img.img_id', 'component_img.img_url', 'component_img.img_name')
  2802. // ->orderBy('component_img.template_id')
  2803. ->orderBy('component_img.img_id', 'asc')
  2804. ->get()->all();
  2805. }
  2806. if(empty($component)){
  2807. return Result::error('组件不存在!');
  2808. }
  2809. return Result::success($component);
  2810. }
  2811. /**
  2812. * 自助建站-流程管理-获取所有通栏
  2813. */
  2814. public function getAllSector(array $data): array
  2815. {
  2816. $where = [];
  2817. if (isset($data['template_id']) && !empty($data['template_id'])) {
  2818. $where['sector.template_id'] = $data['template_id'];
  2819. }
  2820. if (isset($data['page_type']) && !empty($data['page_type'])) {
  2821. array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
  2822. }
  2823. $sector = Sector::where($where)
  2824. ->leftJoin('sector_component', 'sector.id', '=', 'sector_component.sectorid')
  2825. ->leftJoin('component', 'sector_component.component_id', '=', 'component.component_type')
  2826. ->when(isset($data['template_id']), function ($query) use ($data) {
  2827. // 当 type_id 等于 11 时,关联查询 component_img 表并添加 img_url 字段
  2828. return $query->leftJoin('component_img', function ($join) use ($data) {
  2829. $join->on('component.component_type', '=', 'component_img.component_id')
  2830. ->where('component_img.template_id', $data['template_id'])
  2831. ->where('component.type_id', 11);
  2832. });
  2833. })
  2834. ->select(
  2835. 'sector.sector_name',
  2836. 'sector.sector_id',
  2837. 'component.*',
  2838. 'component_img.img_url',
  2839. 'sector.pic_height',
  2840. 'sector.sector_img',
  2841. 'sector_component.sort_id'
  2842. )
  2843. ->orderBy('sector_component.sort_id')
  2844. ->orderBy('sector_component.sector_id')
  2845. ->get()->all();
  2846. if(empty($sector)){
  2847. return Result::error('通栏不存在!');
  2848. }
  2849. $com_num = array_count_values(array_column($sector, 'sector_id'));
  2850. // 使用 array_keys 和 array_diff 函数,不使用循环将值不为 1 的字段键提取到 $sectors_key 中
  2851. $sectors_key = array_keys(array_diff($com_num, [1]));
  2852. // return Result::success($sector);
  2853. $num = 0;
  2854. $array_key = 0;
  2855. foreach ($sector as $key => $value) {
  2856. $sector_id = $value['sector_id'];
  2857. $sectors[$key] = $value['sector_id'];
  2858. // if(isset($comlist_key) && !empty($comlist_key)){
  2859. // $sector_arr_key[$key] = array_keys($comlist_key);
  2860. // }
  2861. if (in_array($sector_id, $sectors_key) && in_array($sector_id, $sectors)) {
  2862. $array_key = array_search($sector_id, $sectors);
  2863. if (isset($sectors) && !in_array($sector_id, $sectors)) {
  2864. $array_key++;
  2865. }
  2866. $num++;
  2867. $array_key++;
  2868. } else {
  2869. $array_key++;
  2870. }
  2871. // var_dump($array_key);
  2872. // var_dump(in_array($sector_id,$sectors_key));
  2873. $comlist_key[$array_key][$sector_id]['sectorName'] = $value['sector_name'];
  2874. $comlist_key[$array_key][$sector_id]['sectorHeight'] = $value['pic_height'];
  2875. $comlist_key[$array_key][$sector_id]['sectorImg'] = $value['sector_img'];
  2876. $componentList['component_style'] = 1;
  2877. $componentList['sort'] = $value['sort_id'];
  2878. // 1:资讯;2:广告;3:静态;
  2879. $component_data = empty($value['component_data']) ? null : (json_decode($value['component_data']) ?? []);
  2880. // 检查 $value['component_column'] 是否为非空字符串,避免传递 null 给 json_decode
  2881. $listType = empty($value['component_column']) ? null : (json_decode($value['component_column']) ?? []);;
  2882. // $page[$key][$setor_id]['componentList'] =
  2883. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;8:列表类组件;9:详情类组件;
  2884. // 10:二级导航栏类组件;11:广告类;12:静态资源类;13:单页导航类;14:广告资讯混合类;15:滚动图文;16:搜索框类;17:单页列表类;18:单页详情类;
  2885. if (in_array($value['type_id'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 17, 18])) {
  2886. $type = 1;
  2887. $componentList['component_type'] = $type;
  2888. if (in_array($value['type_id'], [7, 15]) && $data['page_type'] == 1) {
  2889. $component_data->componentData->name = '请选择导航..';
  2890. } else if (in_array($value['type_id'], [7, 15]) && $data['page_type'] == 2) {
  2891. $component_data->componentData->name = '自动生成';
  2892. } else if ($data['page_type'] == 3) {
  2893. if ($value['type_id'] == 4) {
  2894. $component_data->componentData->name = '最新资讯';
  2895. }
  2896. if ($value['type_id'] == 6) {
  2897. $component_data->componentData->name = '热点精选';
  2898. }
  2899. } else {
  2900. unset($component_data->componentData->name);
  2901. }
  2902. $componentData = $component_data->componentData;
  2903. $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
  2904. // $page_listType = is_object($listType) ? (array)$listType->listType ?? [] : $listType['listType'] ?? [];
  2905. $componentList['componentData']['listType'] = $listType->listType;
  2906. } else if (in_array($value['type_id'], [10, 12, 13, 16])) {
  2907. $type = 3;
  2908. $componentList['component_type'] = $type;
  2909. $componentList['componentData'] = (object)[];
  2910. } else if ($value['type_id'] == 11) {
  2911. $type = 2;
  2912. $componentList['component_type'] = $type;
  2913. $componentList['componentData'] = (object)[];
  2914. $ad = json_decode($value['ad']) ?? [];
  2915. if (is_object($ad)) {
  2916. $ad->thumb = $value['img_url'];
  2917. } else {
  2918. $ad['thumb'] = $value['img_url'];
  2919. }
  2920. $comlist_key[$array_key][$sector_id]['ad'] = is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
  2921. // 在 $page[$key][$setor_id] 中增加一个键值为 ad 的数组
  2922. // $page[$key][$setor_id]['ad'] = is_object($ad) ? (array)$ad->ad ?? [] : $ad['ad'] ?? [];
  2923. } else if ($value['type_id'] == 14) {
  2924. $type = 1;
  2925. $componentList['component_type'] = $type;
  2926. if ($data['page_type'] == 1) {
  2927. $component_data->componentData->name = '请选择导航..';
  2928. }
  2929. $componentData = $component_data->componentData;
  2930. $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
  2931. $componentList['componentData']['listType'] = $listType->listType;
  2932. $ad = json_decode($value['ad']) ?? [];
  2933. $comlist_key[$array_key][$sector_id]['ad'] = is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
  2934. } else {
  2935. }
  2936. if (in_array($sector_id, $sectors_key)) {
  2937. $comlist_key[$array_key][$sector_id]['componentList'][$num - 1] = $componentList;
  2938. $comlist_key[$array_key][$sector_id]['componentList'] = array_values($comlist_key[$array_key][$sector_id]['componentList']);
  2939. } else {
  2940. $comlist_key[$array_key][$sector_id]['componentList'] = $componentList;
  2941. }
  2942. $keys[$key] = $key;
  2943. }
  2944. // return Result::success($sector_arr_key);
  2945. // $page[$keys] = array_column($page,$sectors_key[0]);
  2946. $comlist_key = array_values($comlist_key);
  2947. // $sector_page = json_encode($comlist_key);
  2948. if (empty($comlist_key)) {
  2949. return Result::error('通栏不存在!');
  2950. }
  2951. return Result::success($comlist_key);
  2952. // return Result::success(gettype($listType));
  2953. }
  2954. /**
  2955. * 自助建站-组件管理-获取组件预览图列表
  2956. */
  2957. public function getComponentImgList(array $data): array
  2958. {
  2959. $where['component_img.component_id'] = $data['component_id'];
  2960. if(isset($data['template_id']) && !empty($data['template_id'])){
  2961. $where['component_img.template_id'] = $data['template_id'];
  2962. }
  2963. $component_img = ComponentImg::where($where)
  2964. ->leftJoin('template', 'component_img.template_id', '=', 'template.template_id')
  2965. ->leftJoin('component', 'component_img.component_id', '=', 'component.component_type')
  2966. ->select('component_img.*', 'template.template_name', 'component.component_name')
  2967. ->orderBy('component_img.template_id')
  2968. ->orderBy('component_img.img_id')
  2969. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2970. if($component_img->isEmpty()){
  2971. return Result::error('组件预览图不存在!');
  2972. }
  2973. $component_img = [
  2974. 'data' => $component_img->items(),
  2975. 'total' => $component_img->total(),
  2976. ];
  2977. return Result::success($component_img);
  2978. }
  2979. /**
  2980. * 自助建站-组件管理-添加组件预览图
  2981. */
  2982. public function addComponentImg(array $data): array
  2983. {
  2984. unset($data['user_id']);
  2985. $component = Component::where('component_type',$data['component_id'])->first();
  2986. if(empty($component)){
  2987. return Result::error('组件不存在!');
  2988. }
  2989. if($component['type_id'] == 11){
  2990. $img = ComponentImg::where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2991. if(!empty($img)){
  2992. return Result::error('该组件已存在此皮肤的预览图!');
  2993. }
  2994. }
  2995. $component = ComponentImg::where('template_id', $data['template_id'])->where('component_id', $data['component_id']);
  2996. if (!empty($component->where('img_id', $data['img_id'])->first())) {
  2997. return Result::error('组件预览图编号已存在!');
  2998. }
  2999. if(!empty($component->where('img_name', $data['img_name'])->first())){
  3000. return Result::error('组件预览图名称已存在!');
  3001. }
  3002. $component_img = ComponentImg::insertGetId($data);
  3003. if(empty($component_img)){
  3004. return Result::error('添加失败!');
  3005. }
  3006. return Result::success($component_img);
  3007. }
  3008. /**
  3009. * 自助建站-组件管理-删除组件预览图
  3010. */
  3011. public function delComponentImg(array $data): array
  3012. {
  3013. $component_img = ComponentImg::where('id', $data['id'])->delete();
  3014. if (empty($component_img)) {
  3015. return Result::error('删除失败!');
  3016. }
  3017. return Result::success($component_img);
  3018. }
  3019. /**
  3020. * 自助建站-组件管理-更新组件预览图
  3021. */
  3022. public function updateComponentImg(array $data): array
  3023. {
  3024. $component = Component::where('component_type',$data['component_id'])->first();
  3025. if(empty($component)){
  3026. return Result::error('组件不存在!');
  3027. }
  3028. if($component['type_id'] == 11){
  3029. $img = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  3030. if(!empty($img)){
  3031. return Result::error('该组件已存在此皮肤的预览图!');
  3032. }
  3033. }
  3034. $component = ComponentImg::where('id', '!=', $data['id'])->where('component_id', $data['component_id'])->where('template_id', $data['template_id']);
  3035. if (!empty($component->where('img_id', $data['img_id'])->first())) {
  3036. return Result::error('组件预览图编号已存在!');
  3037. }
  3038. if(!empty($component->where('img_name', $data['img_name'])->first())){
  3039. return Result::error('组件预览图名称已存在!');
  3040. }
  3041. // return Result::success($component);
  3042. unset($data['user_id']);
  3043. // unset($data['updated_at']);
  3044. $id = $data['id'];
  3045. unset($data['id']);
  3046. // return Result::success($data);
  3047. $component_img = ComponentImg::where('id',$id)->update($data);
  3048. if(empty($component_img)){
  3049. return Result::error('更新失败!');
  3050. }
  3051. return Result::success($component_img);
  3052. }
  3053. /**
  3054. * 自助建站-获取通栏类型
  3055. */
  3056. public function getSectorType(array $data): array
  3057. {
  3058. $sector = SectorType::get()->all();
  3059. if(empty($sector)){
  3060. return Result::error('通栏类型不存在!');
  3061. }
  3062. return Result::success($sector);
  3063. }
  3064. /**
  3065. * 自助建站-随机获取模板
  3066. */
  3067. public function randomWebTemplate(array $data): array
  3068. {
  3069. $web = Website::where('id',$data['website_id'])->first();
  3070. if(empty($web)){
  3071. return Result::error('此网站不存在!');
  3072. }
  3073. $template = Template:: where('template_id',$data['template_id'])->first();
  3074. if(empty($template)){
  3075. return Result::error('此皮肤不存在!');
  3076. }
  3077. $rule = TemplateRule::first();
  3078. if(isset($data['page']) && !empty($data['page'])){
  3079. switch ($data['page']) {
  3080. case 1:
  3081. $rule = json_decode($rule['index_rule']);
  3082. break;
  3083. case 2:
  3084. $rule = json_decode($rule['class_rule']);
  3085. break;
  3086. case 3:
  3087. $rule = json_decode($rule['list_rule']);
  3088. break;
  3089. case 4:
  3090. $rule = json_decode($rule['info_rule']);
  3091. break;
  3092. case 5:
  3093. $rule = json_decode($rule['search_rule']);
  3094. break;
  3095. case 6:
  3096. $rule = json_decode($rule['footerlist_rule']);
  3097. break;
  3098. case 7:
  3099. $rule = json_decode($rule['footerinfo_rule']);
  3100. break;
  3101. default:
  3102. break;
  3103. }
  3104. }
  3105. $query = WebsiteCategory::where('website_id', $data['website_id']);
  3106. $cate_num = $query->where('is_show', 1)->count();
  3107. if ($cate_num < 5) {
  3108. return'此网站栏目过少,无法进行随机模板!';
  3109. }
  3110. $cate_num = min($cate_num, 24);
  3111. // return Result::success($data);
  3112. $tempalte['index'] = $this->randomWeb($data,$rule);
  3113. return Result::success($tempalte);
  3114. if(isset($data['page']) && !empty($data['page'])){
  3115. $tempalte['index'] = $this->randomPage($data,$cate_num,$rule);
  3116. }else{
  3117. $data['page'] = 1;
  3118. $ruleIndex = json_decode($rule->index_rule);
  3119. $tempalte['index'] = $this->randomPage($data, $cate_num, $ruleIndex);
  3120. $data['page'] = 2;
  3121. $ruleClass = json_decode($rule->class_rule);
  3122. // $class_cate_num = $query->where('pid','!=',0)->groupBy('pid')->selectRaw('pid, count(*) as count')->get()->pluck('count','pid');
  3123. // $cate_num = max($class_cate_num);
  3124. $tempalte['class'] = $this->randomPage($data, $cate_num, $ruleClass);
  3125. $data['page'] = 3;
  3126. $ruleList = json_decode($rule->list_rule);
  3127. $tempalte['list'] = $this->randomPage($data,$cate_num,$ruleList);
  3128. $data['page'] = 4;
  3129. $ruleInfo = json_decode($rule->info_rule);
  3130. $tempalte['info'] = $this->randomPage($data,$cate_num,$ruleInfo);
  3131. $data['page'] = 5;
  3132. $ruleSearch = json_decode($rule->search_rule);
  3133. $tempalte['search'] = $this->randomPage($data,$cate_num,$ruleSearch);
  3134. $data['page'] = 6;
  3135. $ruleFooterlist = json_decode($rule->footerlist_rule);
  3136. $tempalte['footerlist'] = $this->randomPage($data,$cate_num,$ruleFooterlist);
  3137. $data['page'] = 7;
  3138. $ruleFooterinfo = json_decode($rule->footerinfo_rule);
  3139. $tempalte['footerinfo'] = $this->randomPage($data,$cate_num,$ruleFooterinfo);
  3140. }
  3141. // $index = $this->randomPage($data);
  3142. return Result::success($tempalte);
  3143. }
  3144. public function randomPage($data, $cate_num, $rule)
  3145. {
  3146. // return $rule;
  3147. $page = $data['page'];
  3148. $sectors = Sector::where('sector.template_id', $data['template_id'])
  3149. ->where('sector.page_type', 'like', '%' . $page . '%')
  3150. ->leftJoin('sector_place', function ($join) {
  3151. $join->on('sector.place_type', '=', 'sector_place.sector_type')
  3152. ->where('sector_place.type_id', 1);
  3153. })
  3154. ->whereNotNull('sector_place.sector_type')
  3155. ->select(
  3156. 'sector.sector_name',
  3157. 'sector.sector_id',
  3158. 'sector.pic_height',
  3159. 'sector.sector_img',
  3160. 'sector.sector_type',
  3161. 'sector.sector_code',
  3162. 'sector.id',
  3163. 'sector.sector_width',
  3164. 'sector.component_num',
  3165. 'sector.cat_num',
  3166. )
  3167. ->orderBy('id')
  3168. ->get()->all();
  3169. if (empty($sectors)) {
  3170. return Result::error('通栏不存在!');
  3171. }
  3172. // return $sectors;
  3173. // 获取每个类别的通栏数量
  3174. // $type_num = array_count_values(array_column($sectors, 'sector_type'));
  3175. // 此页面通栏的所有类别
  3176. // $all_sector_types = array_keys($type_num);
  3177. //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏
  3178. $all_sector_type = array_column($sectors, 'sector_type');
  3179. $grouped = array_reduce(
  3180. array_keys($sectors),
  3181. function ($carry, $idx) use ($sectors) {
  3182. $carry[$sectors[$idx]['sector_type']][] = $idx;
  3183. return $carry;
  3184. },
  3185. );
  3186. // 对 all_sector_type 去重后,每个类型随机挑一个 sector 索引
  3187. $unipue_sector_keys = array_combine(
  3188. array_keys($grouped),
  3189. array_map(
  3190. function ($type) use ($grouped) {
  3191. // 随机取一个索引
  3192. return $grouped[$type][array_rand($grouped[$type])];
  3193. },
  3194. array_keys($grouped)
  3195. )
  3196. );
  3197. // 1. 必需通栏:同一类型只随机保留一条
  3198. $rule_must_type = $rule->must_type;
  3199. $sector_num = $rule->max_num;
  3200. $unique_sector_type = array_flip($unipue_sector_keys);
  3201. $must_type = array_unique(array_intersect($unique_sector_type,$rule_must_type));
  3202. // 先求交集,去重后统计
  3203. $is_must = count($must_type);
  3204. if ($is_must != count(array_unique($must_type))) {
  3205. return '此页面缺少必要通栏!';
  3206. }
  3207. // 先按类型把 sectors 分组,key 为 sector_type,value 为对应 sector 数组
  3208. // return [
  3209. // 'unipue_sector_keys'=>$unipue_sector_keys,
  3210. // 'grouped'=>$grouped,
  3211. // ];
  3212. // 最终保留的 must_type 数组(去重且随机保留一个)
  3213. // return [
  3214. // 'must_key'=>$must_key,'must_type'=>$must_type,
  3215. // 'must_sector_keys'=>$must_sector_keys
  3216. // ];
  3217. // $must_sector_keys = array_intersect_key($unipue_sector_keys,$must_type);
  3218. $must_sector = array_values(array_intersect_key($sectors,$must_type));
  3219. $must_count = count($must_sector);
  3220. // return $sector_comnum;
  3221. // $must_sector = array_column($must_sector,'sector_type');
  3222. // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量
  3223. if ($must_count > $sector_num) {
  3224. return $must_sector;
  3225. }
  3226. // 必需通栏中的栏目数量
  3227. if($page == 1){
  3228. $must_cat_num = array_sum(array_column($must_sector,'cat_num'));
  3229. if($must_cat_num>$cate_num){
  3230. return '栏目数量过少,无法完成必需通栏随机!';
  3231. }
  3232. }
  3233. // var_dump(['must_cat_num'=>$must_cat_num,'cate_num'=>$cate_num]);
  3234. // 2.非必须通栏的随机处理
  3235. $not_must_type = array_unique(array_diff($unique_sector_type, $must_type));
  3236. $total = count($not_must_type);
  3237. $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
  3238. $and_type = $rule->and_type ?? [];
  3239. // return $not_must_type;
  3240. if ($randomCount === 0) {
  3241. $random_sector = [];
  3242. $rand_count = 0;
  3243. } else {
  3244. // 随机获取指定数量的键名(保留原键)
  3245. $randomKeys = array_rand($not_must_type, $randomCount);
  3246. // 处理单元素情况(array_rand返回字符串,需转为数组)
  3247. if (!is_array($randomKeys)) {
  3248. $randomKeys = [$randomKeys];
  3249. }
  3250. // 通栏关联性规则暂时无法实现---------------即若是随机了头条通栏必须要有轮播图通栏(皆为非必须通栏)
  3251. // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
  3252. // 判断 $oneDimensionalArray 是否为一维数组
  3253. // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) {
  3254. // 判断是否存在有关联通栏的交集
  3255. // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) {
  3256. // // var_dump("11111",$and_type);
  3257. // // var_dump("22222",$randomKeys);
  3258. // // 将一维数组中的值都放到随机数组中
  3259. // $randomKeys = array_merge($randomKeys, $and_type);
  3260. // }
  3261. // var_dump(count(array_intersect($and_type, $randomKeys)));
  3262. // }
  3263. // -----------------------------
  3264. // 根据随机键名提取元素(保留原键值关系)
  3265. // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
  3266. // var_dump("randomKeys",$randomKeys);
  3267. $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
  3268. $rand_count = count($random_sector);
  3269. }
  3270. if($page == 1){
  3271. $random_cat_num = array_sum(array_column($random_sector,'cat_num'));
  3272. $random_cat_num += $must_cat_num;
  3273. if($random_cat_num>$cate_num){
  3274. return '栏目数量过少,无法完成随机!';
  3275. }
  3276. }
  3277. // var_dump("randomKeys",$randomKeys);
  3278. // var_dump("random_sector",$random_sector);
  3279. // var_dump("rand_count", $rand_count);
  3280. // var_dump("random_cat_num", $random_cat_num);
  3281. // return $random_sector;
  3282. // var_dump("must_sector",$must_sector);
  3283. // var_dump("sector_num",$sector_num);
  3284. // var_dump("must_count",$must_count);
  3285. // return ['random_sector'=>$random_sector,'random_cat_num'=>$random_cat_num,'cate_num'=>$cate_num];
  3286. // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
  3287. $repeat_num = $sector_num - $rand_count - $must_count;
  3288. // 组合起来通栏
  3289. $sector_zuhe1 = array_merge($must_sector, $random_sector);
  3290. $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type');
  3291. $sector_ids = array_column($sector_zuhe1, 'id');
  3292. if($repeat_num > 0){
  3293. $rule_repeat = (array)$rule->type_max;
  3294. $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
  3295. // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
  3296. foreach ($rule_repeat as $key => $value) {
  3297. if (in_array($key, array_keys($sector_zuhe1_type))) {
  3298. $rule_repeat[$key] = $value - 1;
  3299. }
  3300. }
  3301. // var_dump("repeat_num", $repeat_num);
  3302. // 剩余通栏进行重复处理
  3303. $rule_repeat = (array)$rule->type_max;
  3304. // 取到可以重复的通栏类别
  3305. $rule_repaet_type = array_keys($rule_repeat);
  3306. $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type);
  3307. // 获取可以随机重复的通栏
  3308. $repaet_key = 0;
  3309. $type = [];
  3310. // return $rule_repaet_type;
  3311. // 防止无限循环,设置最大迭代次数
  3312. $max_iterations = 10;
  3313. $iteration_count = 0;
  3314. // 剩余的栏目数量
  3315. if($page == 1){
  3316. $sector_cate_comnum = $cate_num - $random_cat_num;
  3317. if($sector_cate_comnum < 0){
  3318. return '栏目数量过少,无法完成随机!';
  3319. }
  3320. }
  3321. // return [
  3322. // // 'repeat_type'=>$repeat_type,
  3323. // // 'repeat_num'=>$repeat_num,
  3324. // 'must_count'=>$must_count,
  3325. // 'rand_count'=>$rand_count,
  3326. // 'repeat_num'=>$repeat_num,
  3327. // 'sector_num'=>$sector_num,
  3328. // 'cate_num'=>$cate_num,
  3329. // 'sector_comnum'=>$sector_cate_comnum,
  3330. // 'random_cat_num'=>$random_cat_num,
  3331. // // 'rule_repeat'=>$rule_repeat,
  3332. // // 'unique_sector_type'=>$unique_sector_type,
  3333. // // 'all_sector_type'=>$all_sector_type,
  3334. // // 'sector_zuhe1' =>$sector_zuhe1
  3335. // ];
  3336. // -----新添加的代码-----
  3337. $re_sector = array_intersect($all_sector_type,$repeat_type);
  3338. $re_sector_key = array_keys($re_sector);
  3339. $every_repeattype_num = array_count_values($re_sector);
  3340. // $re_sector_key = array_keys($re_sector,1);
  3341. $repaet_key = 0;
  3342. $re_type_keys = [];
  3343. $one_sector_key = array_search(1,$re_sector);
  3344. $two_sector_key = array_search(2,$re_sector);
  3345. $three_sector_key = array_search(3,$re_sector);
  3346. $all_cat_num = array_column($sectors,'cat_num');
  3347. $repeat_sector_key = [];
  3348. // $rule_repeat['1'] = 1;
  3349. // return $rule_repeat;
  3350. // $three_sector_key = array_search(3,$re_sector);
  3351. // $min_one_sector = max(array_column('cat_num',$all_sector));
  3352. // 随机重复通栏-----
  3353. // $rule_repeat['1'] = 1;
  3354. // // 找出 all_sector_type 中值为 1 的键
  3355. while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) {
  3356. // 剩余通栏数量>0 若不是首页则继续;若是首页则需要判断剩余栏目数量是否>3
  3357. if ($repeat_num-$repaet_key > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) {
  3358. foreach($repeat_type as $key => $value){
  3359. $type_key = strval($value);
  3360. if(!isset($re_type_keys[$value])){
  3361. $re_type_keys[$value] = [];
  3362. }
  3363. // 剩余的通栏数量>0 并且 剩余的此类通栏数量>0
  3364. if($repeat_num-$repaet_key > 0 && $rule_repeat[$type_key]- count($re_type_keys[$value]) > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))){
  3365. // var_dump("0000",array_keys($re_sector,$value));
  3366. $one_sector_type = array_keys($re_sector,$value);
  3367. // 若是此类通栏的数量>1 则随机取一个对应的键值
  3368. if(count($one_sector_type) > 1){
  3369. // 从 $one_sector_type 数组中随机取一个键名,并取其对应值
  3370. $cat_key = $one_sector_type[array_rand($one_sector_type)];
  3371. }else{
  3372. $cat_key = $one_sector_type[0];
  3373. }
  3374. $repeat_sector_key[$repaet_key] = $cat_key;
  3375. $repaet_key++;
  3376. // var_dump("1115----------cat_key",$cat_key);
  3377. if($page == 1){
  3378. $sector_cate_comnum -= $all_cat_num[$cat_key];
  3379. }
  3380. if(!in_array($repaet_key,$re_type_keys[$value])){
  3381. $re_type_keys[$value][] = $repaet_key;
  3382. // var_dump("1117----------re_type_keys",$re_type_keys);
  3383. }
  3384. }else{
  3385. break;
  3386. }
  3387. }
  3388. } else {
  3389. break;
  3390. }
  3391. $iteration_count++;
  3392. }
  3393. // var_dump("num", $num);
  3394. // var_dump("repeat_sector_key", $repeat_sector_key);
  3395. // 若是首页的通栏没有到10个,栏目还有剩余1~3个,则继续随机(已知当剩余栏目为4还会随机)
  3396. if($page == 1 && count($repeat_sector_key) < $repeat_num && $sector_cate_comnum > 0){
  3397. if($sector_cate_comnum == 1){
  3398. array_push($repeat_sector_key,$one_sector_key);
  3399. }else if($sector_cate_comnum == 2){
  3400. array_push($repeat_sector_key,$two_sector_key);
  3401. }else{
  3402. array_push($repeat_sector_key,$three_sector_key);
  3403. }
  3404. }
  3405. // 随机的重复通栏键值转换成真正的通栏
  3406. $repeat_sector = [];
  3407. if (isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)) {
  3408. foreach ($repeat_sector_key as $key => $value) {
  3409. if (isset($sectors[$value])) {
  3410. $repeat_sector[$key] = $sectors[$value];
  3411. }else{
  3412. continue;
  3413. }
  3414. }
  3415. }
  3416. $repeat_count = count($repeat_sector);
  3417. // var_dump("repeat_count", $repeat_count);
  3418. // return [
  3419. // 'not_repeat_num'=>$rand_count + $must_count,
  3420. // // 'repeat_count'=>$repeat_count,
  3421. // 'repeat_num'=>$repeat_num,
  3422. // 'repeat_sector'=>$repeat_sector,
  3423. // 'repeat_type'=>$repeat_type,
  3424. // '$repaet_key'=>$repaet_key,
  3425. // 'all_sector_type'=>$all_sector_type,
  3426. // 're_sector'=>$re_sector,
  3427. // 're_sector_key'=>$re_sector_key,
  3428. // 'resdfddd'=>$every_repeattype_num,
  3429. // 'repeat_sector_key'=>$repeat_sector_key,
  3430. // // 'ddd'=>$ddd,
  3431. // // 'sector_num'=>$sector_num,
  3432. // // 'sector_zuhe1'=>count($sector_zuhe1),
  3433. // // // 'repeat_sector'=>$repeat_sector,
  3434. // // 'cate_num'=>$cate_num,
  3435. // // 'random_cat_num'=>$random_cat_num,
  3436. // 'rule_repeat' => $rule_repeat
  3437. // ];
  3438. // // var_dump("repeat_sector", $repeat_sector);
  3439. // return $repeat_sector;
  3440. $sector = array_merge($repeat_sector,$sector_zuhe1);
  3441. }else{
  3442. $sector = $sector_zuhe1;
  3443. }
  3444. // var_dump("sector_ids",$sector_ids);
  3445. //
  3446. // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
  3447. // var_dump("sector",$sector);
  3448. // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
  3449. $sector_specal_sort = array_column($sector,'sector_type');
  3450. if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
  3451. $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
  3452. $pic_key = array_search(7,$sector_specal_sort) ?? null;
  3453. $cat_key = array_search(5,$sector_specal_sort) ?? null;
  3454. $toutiao = $sector[$toutiao_key];
  3455. $pic = $sector[$pic_key];
  3456. $cat = $sector[$cat_key];
  3457. $sort_1 = $sector[0];
  3458. $sort_2 = $sector[1];
  3459. $sort_3 = $sector[2];
  3460. // return $sector_specal_sort;
  3461. // return [
  3462. // 'sector_ids' => array_column($sector_zuhe1,'id'),
  3463. // // 'repeat_sector' => $repeat_sector,
  3464. // 'sector_id' => array_column($sector,'id'),
  3465. // 'array_intersect' => count(array_intersect($sector,$must_sector)),
  3466. // 'old' => [ $toutiao_key,$pic_key,$cat_key],
  3467. // 'new' => [ $sort_1,$sort_2,$sort_3]
  3468. // ];
  3469. if($data['page'] == 1 || $data['page'] == '1'){
  3470. if($toutiao_key && $pic_key){
  3471. $sector[0] = $toutiao;
  3472. $sector[$toutiao_key] = $sort_1;
  3473. $sector[1] = $pic;
  3474. $sector[$pic_key] = $sort_2;
  3475. }else if($pic_key){
  3476. $sector[0] = $pic;
  3477. $sector[$pic_key] = $sort_1;
  3478. }else{
  3479. $sector[0] = $toutiao;
  3480. $sector[$toutiao_key] = $sort_1;
  3481. }
  3482. }
  3483. if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) {
  3484. $sector[0] = $cat;
  3485. $sector[$cat_key] = $sort_1;
  3486. if ($toutiao_key && $pic_key) {
  3487. $sector[1] = $toutiao;
  3488. $sector[$toutiao_key] = $sort_2;
  3489. $sector[2] = $pic;
  3490. $sector[$pic_key] = $sort_3;
  3491. }else if ($pic_key) {
  3492. $sector[1] = $pic;
  3493. $sector[$pic_key] = $sort_2;
  3494. }else if($toutiao_key){
  3495. $sector[1] = $toutiao;
  3496. $sector[$toutiao_key] = $sort_2;
  3497. }
  3498. }
  3499. }
  3500. $sector_id = array_column($sector,'id');
  3501. // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){
  3502. // return '此页面缺少必要通栏!';
  3503. // }
  3504. // return Result::success([
  3505. // // 'sector_id'=>$sector_id,
  3506. // 'sector'=>$sector,
  3507. // // 'cate_num'=>$cate_num,
  3508. // ]);
  3509. // $data['page'] = $page;
  3510. $component[] = $this->randomComponent($sector_id,$data,$sector);
  3511. var_dump("sector_id",$sector_id);
  3512. return [
  3513. // 'sector'=>$sector,
  3514. // 'must_sector'=>$must_sector,
  3515. // 'sector_count'=>count($sector),
  3516. // 'sectors'=>$sectors,
  3517. // 'sector_comnum'=>$sector_comnum,
  3518. // 'com_num'=>$com_num,
  3519. // 'cate_num'=>$cate_num,
  3520. // '$sector_zuhe1_comnum'=>$sector_zuhe1_comnum+$com_num,
  3521. 'component'=>$component,
  3522. // 'sector'=>$sector,
  3523. ];
  3524. }
  3525. public function randomComponent($sector_id, $data, $sector)
  3526. {
  3527. $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
  3528. ->leftJoin('component', 'component.component_type', 'sector_component.component_id')
  3529. ->leftJoin('component_img', function ($join) use ($data) {
  3530. $join->on('component_img.component_id', '=', 'component.component_type')
  3531. ->where('component_img.template_id', '=', $data['template_id'] ?? 0);
  3532. })
  3533. ->select(
  3534. 'component.*',
  3535. 'component_img.img_name',
  3536. 'component_img.img_id',
  3537. 'component_img.img_url',
  3538. 'sector_component.sort_id',
  3539. 'sector_component.sectorid'
  3540. )
  3541. ->orderBy('sector_component.sort_id')
  3542. ->orderBy('component.component_type')
  3543. ->get()->all();
  3544. // return $rawResults;
  3545. $img_key = 0;
  3546. $com_key = 0;
  3547. foreach( $rawResults as $key => $value){
  3548. $component_id = $value['component_type'];
  3549. $sort_id = $value['sort_id']-1;
  3550. $img_id = intval($value['img_id']);
  3551. $sector_id = $value['sectorid'];
  3552. $component_data = json_decode($value['component_data'] ?? '[]',true);
  3553. $component_column = json_decode($value['component_column'] ?? '[]',true);
  3554. $component_json['componentData'] = $component_data['componentData'] ?? [];
  3555. // 组件类型:1:头条;2:轮播图;3:推荐图;4:最新资讯;5:推荐资讯;6:热点资讯;7:栏目资讯类;
  3556. // 8:列表类;9:详情类;10:二级导航栏类;11:广告类;12:静态类;13:单页导航类;14:资讯广告混合类;
  3557. // 15:滚动图文类;16:搜索框类;17:单页列表类;18:单页详情类;19-24:选项卡3-7
  3558. if(!in_array($value['type_id'],[10,11,12,16,])){
  3559. $component_json['componentData']['listType'] = $component_column['listType'] ?? '';
  3560. }
  3561. if(!isset($component_img[$component_id])){
  3562. $component_img[$component_id] = [];
  3563. $com_key = 0;
  3564. }else{
  3565. $com_key++;
  3566. }
  3567. if(!isset($component_img[$component_id][$sort_id])){
  3568. $component_img[$component_id][$sort_id] = [];
  3569. $img_key = 0;
  3570. }else{
  3571. $img_key++;
  3572. }
  3573. if(!empty($img_id)){
  3574. $component_img[$component_id][$sort_id][$img_key] =
  3575. [
  3576. 'component_id' => $value['component_type'],
  3577. 'img_id' => $value['img_id'],
  3578. 'component_imgurl' => $value['img_url'],
  3579. ];
  3580. }
  3581. // else{
  3582. // $component_img[$component_id][$sort_id][$img_key] = [];
  3583. // }
  3584. // $component_img = array_values($component_img[$component_id][$sort_id]);
  3585. if(in_array($value['type_id'],[19, 20, 21, 22, 23, 24])){
  3586. $groupedResults[$sector_id][$sort_id] =
  3587. [
  3588. 'component_type' => $component_id,
  3589. 'sort' => $value['sort_id'],
  3590. 'type_id' => $value['type_id'],
  3591. 'componentData' => [
  3592. $component_json['componentData'],
  3593. $component_json['componentData'],
  3594. ],
  3595. 'images' => $component_img[$component_id][$sort_id]
  3596. ];
  3597. }else{
  3598. $groupedResults[$sector_id][$sort_id] =
  3599. [
  3600. 'component_type' => $component_id,
  3601. 'sort' => $value['sort_id'],
  3602. 'type_id' => $value['type_id'],
  3603. 'componentData' => $component_json['componentData'],
  3604. 'images' => $component_img[$component_id][$sort_id]
  3605. ];
  3606. }
  3607. if(in_array($value['type_id'],[11,14,24])){
  3608. $groupedResults[$sector_id][$sort_id]['ad'] = json_decode($value['ad'],JSON_UNESCAPED_UNICODE);
  3609. }
  3610. // [$img_id-1] = $value['img_id'];
  3611. }
  3612. // return ['groupedResults'=>$groupedResults,
  3613. // 'component_data'=>$component_data];
  3614. $y_num = 0;
  3615. foreach ($sector as $key => $value) {
  3616. // array_push($value['sort_id'],$key);
  3617. // $sector_id_ = $value['sector_id'];
  3618. $sectorid = $value['id'];
  3619. $template_data[$key] = [
  3620. 'sectorName' => $value['sector_id'],
  3621. 'sort' => $key + 1,
  3622. ];
  3623. $date = date('mdHisu'); // 获取时分秒微秒,转换为纯数字格式
  3624. // $date = 11010630021;
  3625. if (substr($date, 0, 1) == 0) {
  3626. $date = substr($date, 0, 10); // 截取前9位
  3627. } else {
  3628. $date = substr($date, 0, 9); // 截取前9位
  3629. }
  3630. $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
  3631. $y_num = $y_num + $value['pic_height'];
  3632. $sector_width = $value['sector_width'] / 100;
  3633. $canvas_data[$key] = [
  3634. 'i' => $random_num,
  3635. 'x' => 0,
  3636. 'y' => $y_num,
  3637. 'w' => $sector_width,
  3638. 'h' => $value['pic_height'],
  3639. 'type' => $value['sector_id'],
  3640. 'content' => [
  3641. 'sectorName' => $value['sector_id'],
  3642. ]
  3643. ];
  3644. // $sector_key[$key] = array_keys($value );
  3645. $sector_component = [];
  3646. if (isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])) {
  3647. $componet_key = 0;
  3648. foreach ($groupedResults[$sectorid] as $sortId => $components) {
  3649. // // // // 通栏某一位置的随机组件
  3650. // if (count($components) > 1) {
  3651. // $componentIds = count($components);
  3652. // $selectedComponentId = rand(0, $componentIds - 1);
  3653. // } else {
  3654. // $selectedComponentId = 0;
  3655. // }
  3656. // // var_dump($components[$selectedComponentId]);
  3657. // $Component = $components;
  3658. $Component['component_type'] = $components['component_type'] ?? 0;
  3659. $Component['sort'] = $components['sort'] ?? 1;
  3660. $Component['type_id'] = $components['type_id'] ?? 0;
  3661. // // 通栏某一位置的随机组件样式图
  3662. // // 检查 $Component['images'] 是否存在且为数组
  3663. if (isset($components['images']) && is_array($components['images'])) {
  3664. // var_dump('action1-------------------------------------sortId',$sortId);
  3665. $images = count($components['images']);
  3666. $selectedImage = 0;
  3667. if ($images > 1) {
  3668. $selectedImage = rand(0, $images - 1);
  3669. }
  3670. if (isset($components['images'][$selectedImage])) {
  3671. $Component['component_style'] = is_object($components['images'][$selectedImage]) ? $components['images'][$selectedImage]->img_id : $components['images'][$selectedImage]['img_id'];
  3672. }
  3673. }
  3674. if(isset($components['images'][$selectedImage])){
  3675. $image_info[$key] = is_object($components['images'][$selectedImage]) ? $components['images'][$selectedImage] : $components['images'][$selectedImage];
  3676. }
  3677. // var_dump($images);
  3678. // if(isset($components['type_id']) && in_array($components['type_id'], [19, 20, 21, 22, 23, 24])){
  3679. // // $Component['componentData'][0] = $components['componentData'] ?? [];
  3680. // // $Component['componentData'][1] = $components['componentData'] ?? [];
  3681. // }else{
  3682. $Component['componentData'] = $components['componentData'] ?? [];
  3683. // }
  3684. if ($data['page'] == 1 && (isset($components['type_id']) && in_array($components['type_id'], [7, 14, 15, 23, 24]))) {
  3685. if(in_array($components['type_id'], [23, 24])){
  3686. $Component['componentData'][0]['name'] = '请选择栏目';
  3687. $Component['componentData'][1]['name'] = '请选择栏目';
  3688. }else{
  3689. $Component['componentData']['name'] = '请选择栏目';
  3690. }
  3691. }
  3692. if ($data['page'] == 2 && (isset($components['type_id']) && in_array($components['type_id'], [7, 14, 15, 23, 24]))) {
  3693. if(in_array($components['type_id'], [23, 24])){
  3694. $Component['componentData'][0]['name'] = '自动生成';
  3695. $Component['componentData'][1]['name'] = '自动生成';
  3696. }else{
  3697. $Component['componentData']['name'] = '自动生成';
  3698. }
  3699. }
  3700. if (isset($components['type_id']) && $components['type_id'] == 11) {
  3701. $ad = $components['ad'];
  3702. $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? '';
  3703. $template_data[$key]['ad'] = $ad;
  3704. $canvas_adinfo[$key] = $ad;
  3705. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3706. // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合
  3707. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3708. $Component['componentData'] = [];
  3709. }
  3710. if (isset($components['type_id']) && $components['type_id'] == 14) {
  3711. $ad = $components['ad'];
  3712. $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? '';
  3713. $template_data[$key]['ad'] = $ad;
  3714. $canvas_adinfo[$key] = $ad;
  3715. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3716. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3717. // // $Component['componentData'] = [];
  3718. }
  3719. if (isset($components['type_id']) && $components['type_id'] == 24) {
  3720. $ad = $components['ad'][0];
  3721. // $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? '';
  3722. $template_data[$key]['ad'][0] = $ad;
  3723. $template_data[$key]['ad'][1] = $ad;
  3724. $canvas_adinfo[$key] = $ad;
  3725. $canvas_data[$key]['content']['ad'][0] = $canvas_adinfo[$key];
  3726. $canvas_data[$key]['content']['ad'][1] = $canvas_adinfo[$key];
  3727. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3728. // // $Component['componentData'] = [];
  3729. }
  3730. // // var_dump('---------------------------',$components[$selectedComponentId]);
  3731. $sector_component[$componet_key] = $Component;
  3732. $componet_key++;
  3733. }
  3734. // var_dump("image",$image);
  3735. }
  3736. $template_data[$key]['componentList'] = $sector_component;
  3737. $canvas_data[$key]['content']['componentList'] = $sector_component;
  3738. // $canvas_data[$key]['ad'] = $key;
  3739. $canvas_data[$key]['dataSort'] = $key;
  3740. $canvas_data[$key]['moved'] = false;
  3741. // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
  3742. }
  3743. // return $canvas_data;
  3744. return [
  3745. 'template_data' => $template_data,
  3746. 'canvas_data' => $canvas_data,
  3747. ];
  3748. }
  3749. public function randomWeb($data,$rule)
  3750. {
  3751. $page = $data['page'];
  3752. // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件,
  3753. // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码:
  3754. // 修正:去掉多余的英文分号,保持链式调用完整
  3755. $sectors = Sector::select('sector.id', 'sector.template_id', 'sector.page_type','sector.sector_id','sector.cat_num'
  3756. ,'sector.sector_name','sector.sector_type','sector.sector_width','sector.sector_height','sector.pic_height',
  3757. 'sector.component_num')
  3758. ->where('sector.template_id', $data['template_id'])
  3759. ->where('sector.page_type', 'like', '%' . $data['page'] . '%')
  3760. ->with([
  3761. 'sectorComponents' => function ($query) use ($data) {
  3762. $query->select('sectorid', 'sort_id', 'component_id')
  3763. ->with([
  3764. 'component' => function ($q) use ($data) {
  3765. $q->select('id', 'component_type', 'component_data', 'component_code', 'component_column', 'ad','type_id')
  3766. ->with([
  3767. 'componentImgs' => function ($subQ) use ($data) {
  3768. $subQ->where('template_id', $data['template_id'])
  3769. // ->select('id', 'img_id', 'img_name', 'img_url', 'template_id')
  3770. ;
  3771. }
  3772. ]);
  3773. }
  3774. ]);
  3775. }
  3776. ])
  3777. ->get();
  3778. // 将模型对象转换为数组,确保关系字段正确命名
  3779. $sectors = $sectors->toArray();
  3780. // 提取并返回 component_imgs 内容,添加空安全检查
  3781. // return $sectors[0]['sector_components'][0]['component']['component_imgs'] ?? [];
  3782. // return $sectors;
  3783. $query = WebsiteCategory::where('website_id', $data['website_id']);
  3784. // $cate_num = $query->where('is_show', 1)->count();
  3785. $cat_column = [
  3786. 'website_id' => $data['website_id'],
  3787. 'is_show' => 1,
  3788. 'placeid' => 1,
  3789. 'num' => 23,
  3790. 'pid' => 0,
  3791. ];
  3792. // 临时注入 WebsiteService,后续请改为属性注入
  3793. // 本项目中已存在 WebsiteServer 及其 getWebsiteModelCategory 方法,直接调用
  3794. $websiteServer = new \App\JsonRpc\WebsiteService();
  3795. $all_cate_data = $websiteServer->getWebsiteModelCategory($cat_column);
  3796. $all_cate = $all_cate_data['data'];
  3797. // $all_cate = $query->where('is_show', 1)
  3798. // // ->where('pid', 0)
  3799. // ->orderBy('sort')
  3800. // ->orderBy('updated_at', 'desc')
  3801. // ->get()->toArray();
  3802. $cate_num = count($all_cate);
  3803. // return $all_cate;
  3804. if ($cate_num < 5) {
  3805. return 此网站栏目过少,无法进行随机模板!;
  3806. }
  3807. $cate_num = min($cate_num, 24);
  3808. $all_sector_type = array_column($sectors, 'sector_type');
  3809. // return $all_sector_type;
  3810. $grouped = array_reduce(array_keys($sectors),function ($carry, $idx) use ($sectors) {
  3811. $carry[$sectors[$idx]['sector_type']][] = $idx;
  3812. return $carry;
  3813. },[]);
  3814. $unipue_sector_keys = array_combine(array_keys($grouped),
  3815. array_map(function ($type) use ($grouped) {
  3816. // 随机取一个索引
  3817. return $grouped[$type][array_rand($grouped[$type])];
  3818. },array_keys($grouped))
  3819. );
  3820. // 通栏类别:1:资讯类:2:广告类;3:混合类;4:搜索框类;5:导航类;6:头条类;
  3821. // 7:轮播图类;8:静态资源类;9:单页(列表)类;10:单页(详情)类;11:列表类;12:详情类;13:栏目轮播图类;',
  3822. // {"max_num": 10, "min_num": 6, "and_type": [6, 7],
  3823. // "type_max": {"1": 20, "2": 4, "3": 3}, "must_type": [1, 2]}
  3824. // 1. 必需通栏:同一类型只随机保留一条
  3825. $rule_must_type = $rule->must_type;
  3826. $sector_num = $rule->max_num;
  3827. $unique_sector_type = array_flip($unipue_sector_keys);
  3828. $must_type = array_unique(array_intersect($unique_sector_type,$rule_must_type));
  3829. // 先求交集,去重后统计
  3830. $is_must = count($must_type);
  3831. if ($is_must != count(array_unique($must_type))) {
  3832. return '此页面缺少必要通栏!';
  3833. }
  3834. $must_sector = array_values(array_intersect_key($sectors,$must_type));
  3835. $must_count = count($must_sector);
  3836. // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量
  3837. if ($must_count > $sector_num) {
  3838. return $must_sector;
  3839. }
  3840. // 必需通栏中的栏目数量
  3841. if($page == 1){
  3842. $must_cat_num = array_sum(array_column($must_sector,'cat_num'));
  3843. if($must_cat_num>$cate_num){
  3844. return '栏目数量过少,无法完成必需通栏随机!';
  3845. }
  3846. }
  3847. // 2.非必须通栏的随机处理
  3848. $not_must_type = array_unique(array_diff($unique_sector_type, $must_type));
  3849. $total = count($not_must_type);
  3850. $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
  3851. $and_type = $rule->and_type ?? [];
  3852. // return $not_must_type;
  3853. if ($randomCount === 0) {
  3854. $random_sector = [];
  3855. $rand_count = 0;
  3856. } else {
  3857. // 随机获取指定数量的键名(保留原键)
  3858. $randomKeys = array_rand($not_must_type, $randomCount);
  3859. // 处理单元素情况(array_rand返回字符串,需转为数组)
  3860. if (!is_array($randomKeys)) {
  3861. $randomKeys = [$randomKeys];
  3862. }
  3863. $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
  3864. $rand_count = count($random_sector);
  3865. }
  3866. if($page == 1){
  3867. $random_cat_num = array_sum(array_column($random_sector,'cat_num'));
  3868. $random_cat_num += $must_cat_num;
  3869. if($random_cat_num>$cate_num){
  3870. return '栏目数量过少,无法完成随机!';
  3871. }
  3872. }
  3873. // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
  3874. $repeat_num = $sector_num - $rand_count - $must_count;
  3875. // 组合起来通栏
  3876. $sector_zuhe1 = array_merge($must_sector, $random_sector);
  3877. $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type');
  3878. $sector_ids = array_column($sector_zuhe1, 'id');
  3879. if($repeat_num > 0){
  3880. $rule_repeat = (array)$rule->type_max;
  3881. $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
  3882. // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
  3883. foreach ($rule_repeat as $key => $value) {
  3884. if (in_array($key, array_keys($sector_zuhe1_type))) {
  3885. $rule_repeat[$key] = $value - 1;
  3886. }
  3887. }
  3888. // var_dump("repeat_num", $repeat_num);
  3889. // 剩余通栏进行重复处理
  3890. $rule_repeat = (array)$rule->type_max;
  3891. // 取到可以重复的通栏类别
  3892. $rule_repaet_type = array_keys($rule_repeat);
  3893. $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type);
  3894. // 获取可以随机重复的通栏
  3895. $repaet_key = 0;
  3896. $type = [];
  3897. // return $rule_repaet_type;
  3898. // 防止无限循环,设置最大迭代次数
  3899. $max_iterations = 10;
  3900. $iteration_count = 0;
  3901. // 剩余的栏目数量
  3902. if($page == 1){
  3903. $sector_cate_comnum = $cate_num - $random_cat_num;
  3904. if($sector_cate_comnum < 0){
  3905. return '栏目数量过少,无法完成随机!';
  3906. }
  3907. }
  3908. // -----新添加的代码-----
  3909. $re_sector = array_intersect($all_sector_type,$repeat_type);
  3910. $re_sector_key = array_keys($re_sector);
  3911. $every_repeattype_num = array_count_values($re_sector);
  3912. // $re_sector_key = array_keys($re_sector,1);
  3913. $repaet_key = 0;
  3914. $re_type_keys = [];
  3915. $one_sector_key = array_search(1,$re_sector);
  3916. $two_sector_key = array_search(2,$re_sector);
  3917. $three_sector_key = array_search(3,$re_sector);
  3918. $all_cat_num = array_column($sectors,'cat_num');
  3919. $repeat_sector_key = [];
  3920. // $rule_repeat['1'] = 1;
  3921. // return $rule_repeat;
  3922. // $three_sector_key = array_search(3,$re_sector);
  3923. // $min_one_sector = max(array_column('cat_num',$all_sector));
  3924. // 随机重复通栏-----
  3925. // $rule_repeat['1'] = 1;
  3926. // // 找出 all_sector_type 中值为 1 的键
  3927. while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) {
  3928. // 剩余通栏数量>0 若不是首页则继续;若是首页则需要判断剩余栏目数量是否>3
  3929. if ($repeat_num-$repaet_key > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) {
  3930. foreach($repeat_type as $key => $value){
  3931. $type_key = strval($value);
  3932. if(!isset($re_type_keys[$value])){
  3933. $re_type_keys[$value] = [];
  3934. }
  3935. // 剩余的通栏数量>0 并且 剩余的此类通栏数量>0
  3936. if($repeat_num-$repaet_key > 0 && $rule_repeat[$type_key]- count($re_type_keys[$value]) > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))){
  3937. // var_dump("0000",array_keys($re_sector,$value));
  3938. $one_sector_type = array_keys($re_sector,$value);
  3939. // 若是此类通栏的数量>1 则随机取一个对应的键值
  3940. if(count($one_sector_type) > 1){
  3941. // 从 $one_sector_type 数组中随机取一个键名,并取其对应值
  3942. $cat_key = $one_sector_type[array_rand($one_sector_type)];
  3943. }else{
  3944. $cat_key = $one_sector_type[0];
  3945. }
  3946. $repeat_sector_key[$repaet_key] = $cat_key;
  3947. $repaet_key++;
  3948. // var_dump("1115----------cat_key",$cat_key);
  3949. if($page == 1){
  3950. $sector_cate_comnum -= $all_cat_num[$cat_key];
  3951. }
  3952. if(!in_array($repaet_key,$re_type_keys[$value])){
  3953. $re_type_keys[$value][] = $repaet_key;
  3954. // var_dump("1117----------re_type_keys",$re_type_keys);
  3955. }
  3956. }else{
  3957. break;
  3958. }
  3959. }
  3960. } else {
  3961. break;
  3962. }
  3963. $iteration_count++;
  3964. }
  3965. // 随机的重复通栏键值转换成真正的通栏
  3966. $repeat_sector = [];
  3967. if (isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)) {
  3968. foreach ($repeat_sector_key as $key => $value) {
  3969. if (isset($sectors[$value])) {
  3970. $repeat_sector[$key] = $sectors[$value];
  3971. }else{
  3972. continue;
  3973. }
  3974. }
  3975. }
  3976. $repeat_count = count($repeat_sector);
  3977. $sector = array_merge($repeat_sector,$sector_zuhe1);
  3978. }else{
  3979. $sector = $sector_zuhe1;
  3980. }
  3981. // var_dump("sector_ids",$sector_ids);
  3982. //
  3983. // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
  3984. // var_dump("sector",$sector);
  3985. // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
  3986. $sector_specal_sort = array_column($sector,'sector_type');
  3987. if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
  3988. $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
  3989. $pic_key = array_search(7,$sector_specal_sort) ?? null;
  3990. $cat_key = array_search(5,$sector_specal_sort) ?? null;
  3991. $toutiao = $sector[$toutiao_key];
  3992. $pic = $sector[$pic_key];
  3993. $cat = $sector[$cat_key];
  3994. $sort_1 = $sector[0];
  3995. $sort_2 = $sector[1];
  3996. $sort_3 = $sector[2];
  3997. if($data['page'] == 1 || $data['page'] == '1'){
  3998. if($toutiao_key && $pic_key){
  3999. $sector[0] = $toutiao;
  4000. $sector[$toutiao_key] = $sort_1;
  4001. $sector[1] = $pic;
  4002. $sector[$pic_key] = $sort_2;
  4003. }else if($pic_key){
  4004. $sector[0] = $pic;
  4005. $sector[$pic_key] = $sort_1;
  4006. }else{
  4007. $sector[0] = $toutiao;
  4008. $sector[$toutiao_key] = $sort_1;
  4009. }
  4010. }
  4011. if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) {
  4012. $sector[0] = $cat;
  4013. $sector[$cat_key] = $sort_1;
  4014. if ($toutiao_key && $pic_key) {
  4015. $sector[1] = $toutiao;
  4016. $sector[$toutiao_key] = $sort_2;
  4017. $sector[2] = $pic;
  4018. $sector[$pic_key] = $sort_3;
  4019. }else if ($pic_key) {
  4020. $sector[1] = $pic;
  4021. $sector[$pic_key] = $sort_2;
  4022. }else if($toutiao_key){
  4023. $sector[1] = $toutiao;
  4024. $sector[$toutiao_key] = $sort_2;
  4025. }
  4026. }
  4027. }
  4028. $sector_id = array_column($sector,'id');
  4029. $y_num = 0;
  4030. $cat_key = 0;
  4031. $ad_key = 0;
  4032. // return $sector_id;
  4033. foreach ($sector as $key => $value) {
  4034. // array_push($value['sort_id'],$key);
  4035. // $sector_id_ = $value['sector_id'];
  4036. $sectorid = $value['id'];
  4037. $template_data[$key] = [
  4038. 'sectorName' => $value['sector_id'],
  4039. 'sort' => $key + 1,
  4040. // 'com_num' => $value['component_num'],
  4041. ];
  4042. $date = date('mdHisu'); // 获取时分秒微秒,转换为纯数字格式
  4043. // $date = 11010630021;
  4044. if (substr($date, 0, 1) == 0) {
  4045. $date = substr($date, 0, 10); // 截取前9位
  4046. } else {
  4047. $date = substr($date, 0, 9); // 截取前9位
  4048. }
  4049. $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
  4050. $sector_width = $value['sector_width'] / 100;
  4051. $canvas_data[$key] = [
  4052. 'i' => $random_num,
  4053. 'x' => 0,
  4054. 'y' => $y_num,
  4055. 'w' => $sector_width,
  4056. 'h' => $value['pic_height'],
  4057. 'type' => $value['sector_id'],
  4058. 'content' => [
  4059. 'sectorName' => $value['sector_id'],
  4060. ]
  4061. ];
  4062. $y_num = $y_num + $value['pic_height'];
  4063. // $componentData = [];
  4064. $sector_component = [];
  4065. for ($i = 0; $i < $value['component_num']; $i++) {
  4066. // $component_data = [];
  4067. $componentData = [];
  4068. $component_imgs = $value['sector_components'][$i]['component']['component_imgs'] ?? [];
  4069. $component_type = $value['sector_components'][$i]['component']['type_id'] ?? 0;
  4070. $component_data = json_decode($value['sector_components'][$i]['component']['component_data'] ?? '[]',true) ?? [];
  4071. $list_type = json_decode($value['sector_components'][$i]['component']['component_column'] ?? '[]',true) ?? [];
  4072. if(!empty($component_data) && !empty($list_type)){
  4073. $componentData = $component_data['componentData'];
  4074. if($component_type != 10){
  4075. $componentData['listType'] = $list_type['listType'];
  4076. }
  4077. }
  4078. if(in_array($component_type,[7,14,15,23,24]) && ($page == 1 || $page == 2)){
  4079. if($page == 1){
  4080. if(in_array($component_type,[23,24])){
  4081. $componentData[0]['name'] = $all_cate[$cat_key]['alias'];
  4082. $componentData[0]['category_arr'] = $all_cate[$cat_key]['category_arr_id'];
  4083. $componentData[1]['name'] = $all_cate[$cat_key+1]['alias'];
  4084. $componentData[1]['category_arr'] = $all_cate[$cat_key+1]['category_arr_id'];
  4085. $cat_key += 2;
  4086. }else{
  4087. $componentData['name'] = $all_cate[$cat_key]['alias'];
  4088. $componentData['category_arr'] = $all_cate[$cat_key]['category_arr_id'];
  4089. $cat_key ++;
  4090. }
  4091. }
  4092. if($page == 2){
  4093. $componentData['name'] = '自动生成';
  4094. }
  4095. }
  4096. if(!empty($component_imgs)){
  4097. $component_imgids = array_column($component_imgs,'img_id');
  4098. $component_imgid= array_rand($component_imgids);
  4099. }
  4100. if(in_array($component_type,[11,14,24])){
  4101. $ad_data = json_decode($value['sector_components'][$i]['component']['ad'] ?? '[]',true) ?? [];
  4102. $component_imgkey = array_search($component_imgid,$component_imgids);
  4103. $ad_data['thumb'] = $component_imgs[$component_imgkey]['img_url'];
  4104. if($component_type == 24){
  4105. $ad[0] = $ad_data;
  4106. $ad[1] = $ad_data;
  4107. }else{
  4108. $ad = $ad_data;
  4109. }
  4110. $template_data[$key]['ad'] = $ad;
  4111. $canvas_data[$key]['ad'] = $ad;
  4112. }
  4113. $sector_component[$i] = [
  4114. 'component_type' => $value['sector_components'][$i]['component']['component_type'],
  4115. 'sort' => $value['sector_components'][$i]['sort_id'],
  4116. 'type_id' => $component_type,
  4117. // 'page' => $page,
  4118. 'component_style' => $component_imgids[array_rand($component_imgids)], // 若是上个数组为[1]随机为0,原因:array_rand 在单元素数组时必然返回 0
  4119. 'componentData' => $componentData,
  4120. // 'type_id' => $value['sector_components'][$i]['component']['type_id'],
  4121. ];
  4122. }
  4123. // $sector_key[$key] = array_keys($value );
  4124. // $sector_component = [
  4125. // 'component_type' $value['sector_components'];
  4126. // ];
  4127. $template_data[$key]['componentList'] = $sector_component;
  4128. $canvas_data[$key]['content']['componentList'] = $sector_component;
  4129. // $canvas_data[$key]['ad'] = $key;
  4130. $canvas_data[$key]['dataSort'] = $key;
  4131. $canvas_data[$key]['moved'] = false;
  4132. // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
  4133. }
  4134. // return $canvas_data;
  4135. return [
  4136. 'template_data' => $template_data,
  4137. 'canvas_data' => $canvas_data,
  4138. ];
  4139. return [
  4140. 'all_sector'=> array_column($sectors,'sector_name'),
  4141. 'sector'=> array_column($sector,'sector_name'),
  4142. ];
  4143. }
  4144. /**
  4145. * 自助建站-获取所有通栏、组件、组件样式
  4146. */
  4147. public function getAllSectorComponentStyle(array $data): array
  4148. {
  4149. $sector_component_style = Sector::where('sector.template_id',$data['template_id'])
  4150. ->leftJoin('sector_component','sector.id','sector_component.sectorid')
  4151. ->leftJoin('component', 'component.component_type', 'sector_component.component_id')
  4152. ->leftJoin('component_img', 'component_img.component_id', 'component.component_type')
  4153. // ->leftJoin('sector', 'sector.id', 'sector_component.sectorid')
  4154. ->where('component_img.template_id', '=', $data['template_id'])
  4155. ->select(
  4156. 'sector.sector_id',
  4157. 'sector.sector_keyword',
  4158. 'sector.component_num',
  4159. 'sector.sector_width',
  4160. 'sector.sector_height',
  4161. 'sector.pic_height',
  4162. 'component.component_type',
  4163. 'component.component_keyword',
  4164. 'component.component_data',
  4165. 'component.component_column',
  4166. 'component.ad',
  4167. 'component.type_id',
  4168. 'component_img.img_name',
  4169. 'component_img.img_id',
  4170. 'component_img.img_url',
  4171. 'sector_component.sort_id',
  4172. 'sector_component.sectorid',
  4173. )
  4174. ->orderBy('sector_component.sort_id')
  4175. ->orderBy('component.component_type')
  4176. ->get()->all();
  4177. $groupedResults = [];
  4178. $sort_ids = [];
  4179. $sector_ids = [];
  4180. $component_list = [];
  4181. $component_ids = [];
  4182. $img_ids = [];
  4183. // return Result::success($sector_component_style);
  4184. foreach ($sector_component_style as $key => $item) {
  4185. $sectorId = $item->sectorid;
  4186. $sortId = $item->sort_id;
  4187. $sector_type = $item->sector_type;
  4188. $component_num = $item->component_num;
  4189. $componentId = $item->component_type;
  4190. $component_data = $item->component_data;
  4191. $component_column = $item->component_column;
  4192. $component_keyword = $item->component_keyword;
  4193. $component_type = $item->type_id;
  4194. $component_ad = $item->ad;
  4195. $imgId = $item->img_id;
  4196. $sector_height = $item->sector_height;
  4197. $pic_height = $item->pic_height;
  4198. $sectorName = $item->sector_id;
  4199. if (!isset($groupedResults[$sectorId])) {
  4200. $groupedResults[$sectorId] = [];
  4201. }
  4202. $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
  4203. $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
  4204. $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
  4205. $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
  4206. // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
  4207. if (!isset($componentData['componentData']) || !is_array($componentData['componentData'])) {
  4208. $componentData['componentData'] = [];
  4209. }
  4210. $componentData['componentData'] += $component_listtype;
  4211. $component_code = $componentData;
  4212. $component_list[$key] = $component_code;
  4213. // $component_list[$key]['listType'] = $component_listtype;
  4214. // }
  4215. if(!in_array($sectorId,$sector_ids)){
  4216. $groupedResults[$sectorId] = [
  4217. 'sector_id' => $sectorName,
  4218. 'sector_keyword' => $item->sector_keyword,
  4219. 'component_num' => $component_num,
  4220. 'sector_width' => $item->sector_width,
  4221. 'sector_height' => $sector_height,
  4222. 'pic_height' => $pic_height,
  4223. ];
  4224. }
  4225. $sort_id = intval($sortId - 1);
  4226. if (!isset($com_key) || !in_array($sectorId, $sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])) {
  4227. $com_key = 0;
  4228. $img_key = 0;
  4229. } else if (in_array($sortId, $sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId, $component_ids)) {
  4230. $com_key++;
  4231. }
  4232. if (in_array($componentId, $component_ids) && !in_array($imgId, $img_ids[$componentId])) {
  4233. $img_key++;
  4234. } else {
  4235. $img_key = 0;
  4236. }
  4237. if (!in_array($componentId, $component_ids)) {
  4238. $groupedResults[$sectorId]['component_list'][$sort_id][$com_key] = [
  4239. 'component_type' => $item->component_type,
  4240. 'type_id' => $component_type,
  4241. 'sort' => $sortId,
  4242. ];
  4243. }
  4244. if($component_type == 11){
  4245. $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [
  4246. 'img_name' => $item->img_name,
  4247. 'img_id' => $imgId,
  4248. 'img_url' => $item->img_url,
  4249. ];
  4250. }else{
  4251. $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [
  4252. 'img_name' => $item->img_name,
  4253. 'img_id' => $imgId,
  4254. ];
  4255. }
  4256. $com_key = 0;
  4257. // }
  4258. // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
  4259. // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
  4260. // 19:选项卡-推荐图类;20:选项卡-最新资讯类;21:选项卡-推荐资讯类;22:选项卡-热点资讯类;23:选项卡-栏目资讯类;24:选项卡-栏目广告混合类;
  4261. if (is_scalar($sectorId) && is_scalar($sort_id)) {
  4262. if (!in_array($component_type, [10, 12, 16])) {
  4263. if ($component_type == 11 || $component_type == 14 || $component_type == 24) {
  4264. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad'] = $component_ad;
  4265. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id'];
  4266. }
  4267. if ($component_type != 11) {
  4268. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
  4269. }
  4270. } else {
  4271. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = [];
  4272. }
  4273. } else {
  4274. // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
  4275. trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
  4276. }
  4277. // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
  4278. $component_ids[$key] = $componentId;
  4279. if (!isset($sort_ids[$sectorId])) {
  4280. $sort_ids[$sectorId] = [];
  4281. }
  4282. if (!in_array($sortId, $sort_ids[$sectorId])) {
  4283. $sort_ids[$sectorId][] = $sortId;
  4284. }
  4285. if (!isset($img_ids[$componentId])) {
  4286. $img_ids[$componentId] = [];
  4287. }
  4288. if (!in_array($imgId, $img_ids[$componentId])) {
  4289. $img_ids[$componentId][] = $imgId;
  4290. }
  4291. $sector_ids[$key] = $sectorId;
  4292. $sort_ids[$sectorId][$sort_id] = $sort_id;
  4293. // $img_ids[$componentId] = $imgId;
  4294. }
  4295. // return Result::success($groupedResults);
  4296. if (empty($groupedResults)) {
  4297. return Result::error('通栏组件样式不存在!');
  4298. }
  4299. $groupedResults = array_values($groupedResults);
  4300. return Result::success($groupedResults);
  4301. }
  4302. /**
  4303. * 自助建站-ai会话-获取会话列表
  4304. */
  4305. public function getAiSessionList(array $data): array
  4306. {
  4307. $where['user_id'] = $data['user_id'];
  4308. if(isset($data['website_id']) && !empty($data['website_id'])){
  4309. $where['website_id'] = $data['website_id'];
  4310. }
  4311. if(isset($data['template_id']) && !empty($data['template_id'])){
  4312. $where['template_id'] = $data['template_id'];
  4313. }
  4314. $result = AichatSession::where($where)->get()->all();
  4315. if(empty($result)){
  4316. return Result::error('会话不存在!');
  4317. }
  4318. return Result::success($result);
  4319. }
  4320. /**
  4321. * 自助建站-ai会话-创建会话
  4322. */
  4323. public function addAiSession(array $data): array
  4324. {
  4325. $website = Website::where('id',$data['website_id'])->first();
  4326. if(empty($website)){
  4327. return Result::error('网站不存在!');
  4328. }
  4329. $template = Template::where('template_id',$data['template_id'])->first();
  4330. if(empty($template)){
  4331. return Result::error('皮肤不存在!');
  4332. }
  4333. $date = date('Hisms');
  4334. // $date = 1631191019
  4335. $rand_num = rand(1000,9999);
  4336. $data['session_id'] = $date.$rand_num;
  4337. // $data['session_id'] = 16320610069512
  4338. $ai_sessions = AichatSession::where('user_id',$data['user_id'])->pluck('session_id')->all();
  4339. if(count($ai_sessions) >= 5){
  4340. return Result::error('会话数量已达上限!');
  4341. }
  4342. if(!empty($data['session_id']) && in_array($data['session_id'],$ai_sessions)){
  4343. $rand_num = rand(1000,9999);
  4344. $data['session_id'] = $date.$rand_num;
  4345. }
  4346. $result = AichatSession::insert($data);
  4347. if(empty($result)){
  4348. return Result::error('创建会话失败!');
  4349. }
  4350. return Result::success($result);
  4351. }
  4352. /**
  4353. * 自助建站-ai会话-修改会话
  4354. */
  4355. public function upAiSession(array $data): array
  4356. {
  4357. $where['user_id'] = $data['user_id'];
  4358. $where['session_id'] = $data['session_id'];
  4359. // return Result::success($where);
  4360. $aichat_session = AichatSession::where($where)->first();
  4361. if(empty($aichat_session)){
  4362. return Result::error('会话不存在!');
  4363. }
  4364. unset($data['session_id']);
  4365. // 会话状态:1-开启,0-关闭
  4366. if(isset($data['is_active']) && $aichat_session['is_active'] == $data['is_active']){
  4367. return Result::success('会话状态已为'.$data['is_active'].'!');
  4368. }
  4369. if(isset($data['is_active']) && ($data['is_active'] == 0 || $data['is_active'] == '0')){
  4370. $data['end_time'] = date('Y-m-d H:i:s');
  4371. }
  4372. $result = AichatSession::where($where)->update($data);
  4373. if(empty($result)){
  4374. return Result::error('修改会话失败!');
  4375. }
  4376. return Result::success($result);
  4377. }
  4378. /**
  4379. * 自助建站-ai会话-删除会话
  4380. */
  4381. public function delAiSession(array $data): array
  4382. {
  4383. $where['user_id'] = $data['user_id'];
  4384. $where['session_id'] = $data['session_id'];
  4385. // 删除会话
  4386. Db::beginTransaction();
  4387. try{
  4388. $result['ai_chat_session'] = AichatSession::where($where)->delete();
  4389. if(empty($result['ai_chat_session'])){
  4390. Db::rollBack();
  4391. return Result::error('删除会话消息失败!');
  4392. }
  4393. // 删除会话消息
  4394. $aichat = Aichat::where($where)->get()->all();
  4395. if(!empty($aichat)){
  4396. $result['ai_chat_message'] = Aichat::where($where)->delete();
  4397. if(empty($result['ai_chat_message'])){
  4398. Db::rollBack();
  4399. return Result::error('删除会话消息失败!');
  4400. }
  4401. }
  4402. // 删除草稿模板
  4403. $template_draftbox = TemplateDraftbox::where($where)->get()->all();
  4404. if(!empty($template_draftbox)){
  4405. $result['template_draftbox'] = TemplateDraftbox::where($where)->delete();
  4406. if(empty($result['template_draftbox'])){
  4407. Db::rollBack();
  4408. return Result::error('删除草稿模板失败!');
  4409. }
  4410. }
  4411. Db::commit();
  4412. return Result::success($result);
  4413. }catch(\Exception $e){
  4414. Db::rollBack();
  4415. return Result::error($e->getMessage());
  4416. }
  4417. }
  4418. /**
  4419. * 自助建站-ai会话-发送消息
  4420. */
  4421. public function sendAiMessage(array $data): array
  4422. {
  4423. $where['user_id'] = $data['user_id'];
  4424. $where['session_id'] = $data['session_id'];
  4425. $where['is_active'] = 1;
  4426. $aichat_session = AichatSession::where($where)->first();
  4427. if(empty($aichat_session)){
  4428. return Result::error('会话不存在!');
  4429. }
  4430. $data['message_type'] = empty($data['message_type']) ? 'text' : $data['message_type'];
  4431. $result = Aichat::insertGetId($data);
  4432. if(empty($result)){
  4433. return Result::error('发送消息失败!');
  4434. }
  4435. return Result::success($result);
  4436. }
  4437. /**
  4438. * 自助建站-ai会话-获取消息列表
  4439. */
  4440. public function getAiMessageList(array $data): array
  4441. {
  4442. $where['user_id'] = $data['user_id'];
  4443. $where['session_id'] = $data['session_id'];
  4444. $where['is_active'] = 1;
  4445. $session = AichatSession::where($where)->first();
  4446. if(empty($session)){
  4447. return Result::error('会话不存在!');
  4448. }
  4449. $aichat = Aichat::where('user_id',$data['user_id'])
  4450. ->where('session_id',$data['session_id'])
  4451. ->orderBy('send_time')
  4452. ->get()->all();
  4453. if(empty($aichat)){
  4454. return Result::error('消息不存在!');
  4455. }
  4456. return Result::success($aichat);
  4457. }
  4458. /**
  4459. * 自助建站-ai会话-添加模板草稿
  4460. */
  4461. public function addTemplateDraftbox(array $data): array
  4462. {
  4463. $where['session_id'] = $data['session_id'];
  4464. $where['user_id'] = $data['user_id'];
  4465. // 后续若是用来对应会话记录,则此项需要修改
  4466. $session = AichatSession::where($where)->first();
  4467. if(empty($session)){
  4468. return Result::error('会话不存在!');
  4469. }
  4470. $data['website_id'] = $session['website_id'];
  4471. $data['template_id'] = $session['template_id'];
  4472. $template_draftbox = TemplateDraftbox::where($where)->first();
  4473. if(!empty($template_draftbox)){
  4474. return Result::error('草稿模板已存在!');
  4475. }
  4476. $result = TemplateDraftbox::insert($data);
  4477. if(empty($result)){
  4478. return Result::error('添加草稿模板失败!');
  4479. }
  4480. return Result::success($result);
  4481. }
  4482. /**
  4483. * 自助建站-ai会话-修改模板草稿
  4484. */
  4485. public function upTemplateDraftbox(array $data): array
  4486. {
  4487. $where['session_id'] = $data['session_id'];
  4488. $where['user_id'] = $data['user_id'];
  4489. $session = AichatSession::where($where)->first();
  4490. if(empty($session)){
  4491. return Result::error('会话不存在!');
  4492. }
  4493. if(isset($data['chat_id']) && !empty($data['chat_id'])){
  4494. $chat = Aichat::where($where)->where('chat_id',$data['chat_id'])->first();
  4495. if(empty($chat)){
  4496. return Result::error('消息不存在!');
  4497. }
  4498. }
  4499. // 后续若是用来对应会话记录,则此项需要修改
  4500. $template_draftbox = TemplateDraftbox::where($where)->first();
  4501. if(empty($template_draftbox)){
  4502. return Result::error('草稿模板不存在!');
  4503. }
  4504. unset($data['session_id']);
  4505. $result = TemplateDraftbox::where($where)->update($data);
  4506. if(empty($result)){
  4507. return Result::error('修改草稿模板失败!');
  4508. }
  4509. return Result::success($result);
  4510. }
  4511. /**
  4512. * 自助建站-ai会话-获取模板草稿列表
  4513. */
  4514. public function getTemplateDraftboxList(array $data): array
  4515. {
  4516. $where['session_id'] = $data['session_id'];
  4517. $where['user_id'] = $data['user_id'];
  4518. if(isset($data['website_id']) && !empty($data['website_id'])){
  4519. $where['website_id'] = $data['website_id'];
  4520. }
  4521. if(isset($data['template_id']) && !empty($data['template_id'])){
  4522. $where['template_id'] = $data['template_id'];
  4523. }
  4524. if(isset($data['status'])){
  4525. $where['status'] = $data['status'];
  4526. }
  4527. if(isset($data['chat_id']) && !empty($data['chat_id'])){
  4528. $where['chat_id'] = $data['chat_id'];
  4529. }
  4530. $template_draftbox = TemplateDraftbox::where($where)->get()->all();
  4531. if(empty($template_draftbox)){
  4532. return Result::error('草稿模板不存在!');
  4533. }
  4534. return Result::success($template_draftbox);
  4535. }
  4536. /**
  4537. * 自助建站-验证关键词填写重复问题
  4538. */
  4539. public function checkRepeatKeyword(array $data): array
  4540. {
  4541. if(empty($data['keyword']) || $data['keyword'] == '' || $data['keyword'] == '[]'){
  4542. return Result::success('关键词为空');
  4543. }
  4544. $arr_keyword = json_decode(stripslashes($data['keyword']), true) ?: [];
  4545. if(is_array($arr_keyword) && count($arr_keyword) > 0){
  4546. $keyword = array_map('trim', $arr_keyword);
  4547. if(count($keyword) != count(array_unique($keyword))){
  4548. return Result::error('关键词不能重复!');
  4549. }
  4550. }
  4551. $where['id'] = $data['id'] ?? null;
  4552. if($data['type'] == 'style'){
  4553. $type = '风格';
  4554. $all_keywords = TemplateClass::when(!empty($where),function($query)use($where){
  4555. $query->where('id','!=',$where['id']);
  4556. })->pluck('keyword')->toArray();
  4557. }
  4558. if($data['type'] == 'skin'){
  4559. $type = '皮肤';
  4560. $all_keywords = Template::when(!empty($where),function($query)use($where){
  4561. $query->where('id','!=',$where['id']);
  4562. })->pluck('template_keyword')->toArray();
  4563. }
  4564. if($data['type'] == 'sector'){
  4565. $type = '通栏';
  4566. $all_keywords = Sector::when(!empty($where),function($query)use($where){
  4567. $query->where('id','!=',$where['id']);
  4568. })->where('template_id',$data['template_id'])->pluck('sector_keyword')->toArray();
  4569. }
  4570. if($data['type'] == 'component'){
  4571. $type = '组件';
  4572. $all_keywords = Component::when(!empty($where),function($query)use($where){
  4573. $query->where('id','!=',$where['id']);
  4574. })->pluck('component_keyword')->toArray();
  4575. }
  4576. foreach($all_keywords as $key => $value){
  4577. $value_keyword = json_decode($value ?? '[]',true);
  4578. if(is_array($value_keyword) && count($value_keyword) > 0){
  4579. $repeat_keywords = array_intersect($arr_keyword,$value_keyword);
  4580. if(count($repeat_keywords) == count($keyword) && count($repeat_keywords) == count($value_keyword)){
  4581. return Result::error('此'.$type.'关键词已存在!');
  4582. }
  4583. }
  4584. }
  4585. return Result::success(['验证通过']);
  4586. }
  4587. /**
  4588. * 自助建站-验证网站模板相关类型
  4589. * 版本1----在构建网站首页之后使用
  4590. */
  4591. public function checkWebTemplate(array $data): array
  4592. {
  4593. // 网站id (是否存在该网站)
  4594. // (若是只传这个字段会判断是否关联导航池;)
  4595. // (再判断是否有三种类型的友情链接)
  4596. // (头部信息是否填写)
  4597. // (两种类型的底部导航)
  4598. $website = Website::where('id',$data['website_id'])->first();
  4599. if(empty($website)){
  4600. return Result::error('网站不存在!');
  4601. }
  4602. $template_data = json_decode($data['template_data'],true);
  4603. if(empty($template_data)){
  4604. return Result::error('模板数据不能为空!');
  4605. }
  4606. return Result::success($template_data);
  4607. if(!isset($template_data['website_id']) || empty($template_data['website_id'])){
  4608. return Result::error('网站ID不能为空!');
  4609. }
  4610. if(!isset($template_data['template_id']) || empty($template_data['template_id'])){
  4611. return Result::error('模板ID不能为空!');
  4612. }
  4613. return Result::success($template_data);
  4614. }
  4615. public function getAITemplate(array $data): array
  4616. {
  4617. $result['template'] = Template::select('id','template_id','template_name','template_keyword','page_type',
  4618. 'template_class_id','template_img','updated_at')
  4619. ->get()->all();
  4620. if (empty($result)) {
  4621. return Result::error("暂无皮肤", 0);
  4622. }
  4623. // $num = 1;
  4624. // 确保 $result 是数组且包含数据
  4625. if (!is_array($result) || empty($result)) {
  4626. return Result::error('模板数据异常');
  4627. }
  4628. // 初始化最新更新时间
  4629. $latestUpdate = null;
  4630. foreach ($result['template'] as $key => $value) {
  4631. $templateImg = json_decode($value['template_img'], true);
  4632. $result['template'][$key]['template_img'] = $templateImg;
  4633. // 更新最新时间:取最大的 updated_at
  4634. if ($latestUpdate === null || $value['updated_at'] > $latestUpdate) {
  4635. $latestUpdate = $value['updated_at'];
  4636. }
  4637. // unset($result[$key]['updated_at']);
  4638. }
  4639. // 将最新时间放入返回结果,若无数据则返回当前时间
  4640. $result['update_time'] = date('Y-m-d H:i:s', strtotime($latestUpdate));
  4641. return Result::success($result);
  4642. }
  4643. }