PublicRpcService.php 226 KB

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