PublicRpcService.php 214 KB

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