PublicRpcService.php 206 KB

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