PublicRpcService.php 191 KB

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