PublicRpcService.php 190 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417
  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. $keyword = json_decode($data['keyword'],true) ?? $data['keyword'];
  728. if(!is_array($keyword)){
  729. $keyword = explode('|', $data['keyword']);
  730. $keyword = array_map('trim', $keyword);
  731. }
  732. $keyword = json_encode($keyword);
  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. $reuslt['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])
  2245. ->update(['template_id' => $data['template_id'],'action_id' => 2,'user_id'=> $data['user_id']]);
  2246. if(empty($reuslt['template_info'])){
  2247. Db::rollBack();
  2248. return Result::error('选择皮肤失败!');
  2249. }
  2250. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])
  2251. ->update(['template_id' => $data['template_id'],'user_id'=> $data['user_id']]);
  2252. if(empty($result['template'])){
  2253. Db::rollBack();
  2254. return Result::error('网站模版选择皮肤失败!');
  2255. }
  2256. Db::commit();
  2257. return Result::success($result);
  2258. } catch (\Exception $e) {
  2259. Db::rollBack();
  2260. return Result::error('选择皮肤失败!');
  2261. }
  2262. }
  2263. /**
  2264. * 自助建站---流程---5.保存模版
  2265. * @param array $data
  2266. * @return array
  2267. */
  2268. public function saveWebsiteTemplate(array $data): array
  2269. {
  2270. // return Result::success($data);
  2271. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2272. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  2273. return Result::error('请先填写网站基础信息!');
  2274. }
  2275. // 0:未构建 1:未应用 2:已应用 status
  2276. if ($website_template_info['status'] == 2) {
  2277. return Result::error('网站已应用,不可再次保存!');
  2278. }
  2279. if ($website_template_info['action_id'] != 2) {
  2280. return Result::error('请完成之前的步骤!');
  2281. }
  2282. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  2283. // var_dump($data['template_data']);
  2284. try {
  2285. Db::beginTransaction();
  2286. if (empty($template_info)) {
  2287. Db::rollback();
  2288. return Result::error('该网站不存在已保存的模版!');
  2289. } else {
  2290. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  2291. [
  2292. 'template_data' => $data['template_data'],
  2293. 'user_id' => $data['user_id'],
  2294. 'canvas_data' => $data['canvas_data'],
  2295. ]
  2296. );
  2297. }
  2298. if (empty($template)) {
  2299. Db::rollback();
  2300. return Result::error('保存失败!');
  2301. } else {
  2302. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  2303. if (empty($tempalte_info)) {
  2304. Db::rollback();
  2305. return Result::error('网站搭建状态保存失败!');
  2306. }
  2307. Db::commit();
  2308. }
  2309. } catch (\Exception $e) {
  2310. return Result::error('模版数据格式错误!');
  2311. }
  2312. return Result::success($template);
  2313. }
  2314. /**
  2315. * 自助建站---流程---6.返显保存的模版
  2316. * @param array $data
  2317. * @return array
  2318. */
  2319. public function getWebsiteTemplate(array $data): array
  2320. {
  2321. $web = Website::where('id', $data['website_id'])->first();
  2322. if (empty($web)) {
  2323. return Result::error('未查询到网站信息!');
  2324. }
  2325. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  2326. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  2327. if (empty($template)) {
  2328. return Result::error('未查询到模版!');
  2329. }
  2330. $template['page_type'] = json_decode($template['page_type'],true);
  2331. return Result::success($template);
  2332. }
  2333. /**
  2334. * 通栏版式管理-获取通栏版式列表
  2335. */
  2336. public function getSectorPlaceList(array $data): array
  2337. {
  2338. $where['sector_place.type_id'] = $data['type_id'];
  2339. if(isset($data['component_num']) && !empty($data['component_num'])){
  2340. $where['sector_place.component_num'] = $data['component_num'];
  2341. }
  2342. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2343. $where['sector_place.sector_type'] = $data['sector_type'];
  2344. }
  2345. // if(isset($data['component_type']) && !empty($data['component_type'])){
  2346. // $where['sector_place.component_type'] = $data['component_type'];
  2347. // }
  2348. if(isset($data['name']) && !empty($data['name'])){
  2349. array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
  2350. }
  2351. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  2352. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2353. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2354. } else if( isset($data['width']) && !empty($data['width'])){
  2355. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2356. }else if( isset($data['height']) && !empty($data['height'])){
  2357. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2358. }else{
  2359. $size_id = [];
  2360. }
  2361. $sector_place = SectorPlace::where($where)
  2362. ->select('sector_place.*');
  2363. $result['count'] = $sector_place->count();
  2364. $result['row'] = $sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2365. if($data['type_id'] == 2){
  2366. $result['map_json'] = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->pluck('map');
  2367. }
  2368. if (empty($result['count'])) {
  2369. return Result::error("暂无相关版式数据!");
  2370. }
  2371. return Result::success($result);
  2372. }
  2373. /**
  2374. * 通栏版式管理-添加通栏版式
  2375. */
  2376. public function addSectorPlace(array $data): array
  2377. {
  2378. unset($data['user_id']);
  2379. // 1:通栏;2:组件;
  2380. if ($data['type_id'] == 1) {
  2381. $sector_place = SectorPlace::where('sector_type', $data['sector_type'])->first();
  2382. if (!empty($sector_place)) {
  2383. return Result::error('通栏版式类别编号已存在!');
  2384. }
  2385. $data['sort_id'] = 0;
  2386. } else {
  2387. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->first();
  2388. if (empty($sector_type)) {
  2389. return Result::error('通栏版式类别不存在!');
  2390. }
  2391. $component = SectorPlace::where('type_id', 2)->where('sector_type', $data['sector_type'])->get()->all();
  2392. if(!empty($component)){
  2393. if(count($component) >= $sector_type['component_num']){
  2394. return Result::error('通栏版式组件数量已达上限!');
  2395. }
  2396. 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'))){
  2397. return Result::error('组件版式位置已存在!');
  2398. }
  2399. if(in_array($data['component_type'], array_column($component, 'component_type'))){
  2400. return Result::error('组件版式编号已存在!');
  2401. }
  2402. $data['component_num'] = 0;
  2403. }
  2404. if(in_array($data['type'],[23,24])){
  2405. $data['cat_num'] = 2;
  2406. }else if(in_array($data['type'],[7,14])){
  2407. $data['cat_num'] = 1;
  2408. }else{
  2409. $data['cat_num'] = 0;
  2410. }
  2411. }
  2412. Db::beginTransaction();
  2413. try{
  2414. $result = SectorPlace::insertGetId($data);
  2415. if (empty($result)) {
  2416. Db::rollBack();
  2417. return Result::error('添加失败!');
  2418. }
  2419. if($data['type_id'] == 2 && count($component)+1 == $sector_type['component_num']){
  2420. $map = $this->changeSectorPlaceJson($sector_type['sector_type']);
  2421. $sector_place_map = SectorPlace::where('sector_type', $sector_type['sector_type'])
  2422. ->where('type_id', 1)->update(['map' => json_encode($map['map_json']),'cat_num'=>$map['cat_num']]);
  2423. if($sector_place_map === false){
  2424. Db::rollBack();
  2425. return Result::error('通栏版式画布数据更新失败!');
  2426. }
  2427. }
  2428. Db::commit();
  2429. return Result::success($result);
  2430. }catch(\Exception $e){
  2431. Db::rollBack();
  2432. return Result::error($e->getMessage());
  2433. }
  2434. }
  2435. /**
  2436. * 通栏版式管理-版式画布
  2437. * @param array $data
  2438. * @return array
  2439. */
  2440. public function changeSectorPlaceJson($sector_type)
  2441. {
  2442. $component = SectorPlace::where('type_id', 2)->where('sector_type', $sector_type)->get()->all();
  2443. // $component[count($component)] = $data;
  2444. $sector_type_info = SectorPlace::where('sector_type', $sector_type)->where('type_id', 1)->first();
  2445. if (empty($sector_type_info)) {
  2446. return Result::error('通栏版式类别不存在!');
  2447. }
  2448. $row_num = 1;
  2449. $cat_num = 0;
  2450. $row_width = [];
  2451. $map = [];
  2452. if(count($component) == $sector_type_info['component_num']){
  2453. foreach($component as $key => $val){
  2454. if(in_array($val['type'],[23,24])){
  2455. $cat_num = $cat_num + 2;
  2456. }
  2457. if(in_array($val['type'],[7,14])){
  2458. $cat_num = $cat_num + 1;
  2459. }
  2460. if(!isset($map[$val['column_num'] - 1]['col_data']['row_num'])){
  2461. $row_num = 1;
  2462. $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num;
  2463. }else{
  2464. $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num +1;
  2465. }
  2466. // 原代码使用 $sector_type['width'] 有误,应该使用 $sector_type_info
  2467. $width = number_format((($val['width'] / $sector_type_info['width']) * 100 - 1.5), 2);
  2468. $height = number_format((($val['height'] / $sector_type_info['height']) * 100 - 2.5), 2);
  2469. $row_key = intval($val['line_num'] - 1);
  2470. $map[$val['column_num'] - 1]['col_data']['row_data'][$row_key] = [
  2471. 'row_width' => $width.'%',
  2472. 'row_height' => $height.'%',
  2473. 'component_sort' => $val['sort_id'],
  2474. ];
  2475. $map[$val['column_num'] - 1]['col_data']['col_width'] = $width.'%';
  2476. }
  2477. }
  2478. $map_data = [
  2479. 'map_json'=>$map ?? [],
  2480. 'component'=>count($component),
  2481. 'row_num'=>$row_num ?? 0,
  2482. 'cat_num'=>$cat_num ?? 0,
  2483. 'component_info'=>$component,
  2484. ];
  2485. return $map_data;
  2486. }
  2487. /**
  2488. * 通栏版式管理-修改通栏版式
  2489. */
  2490. public function upSectorPlace($data): array
  2491. {
  2492. unset($data['user_id']);
  2493. // return Result::success($data);
  2494. // $id = $data['id'];
  2495. // 1:通栏;2:组件;
  2496. if ($data['type_id'] == 1) {
  2497. $sector_type = SectorPlace::where('id', $data['id'])
  2498. ->where('type_id',1)
  2499. ->first();
  2500. if (empty($sector_type)) {
  2501. return Result::error('通栏版式不存在!');
  2502. }
  2503. unset($data['sort_id']);
  2504. unset($data['component_type']);
  2505. unset($data['type']);
  2506. // return Result::success($map);
  2507. $sector_place_id = SectorPlace::where('id', '!=', $data['id'])->where('sector_type', $data['sector_type'])->where('type_id', 1)->first();
  2508. if (!empty($sector_place_id)) {
  2509. return Result::error('通栏版式编号已存在!');
  2510. }
  2511. $map = $this->changeSectorPlaceJson($sector_type['sector_type']) ?? [];
  2512. if($data['component_num'] == $map['component']){
  2513. $data['cat_num'] = $map['cat_num'];
  2514. $data['map'] = json_encode($map['map_json']);
  2515. }else{
  2516. $data['cat_num'] = $map['cat_num'];
  2517. $data['map'] = '';
  2518. }
  2519. if($data['sector_type'] != $sector_type['sector_type'] && $map['component'] != 0){
  2520. $com_place = SectorPlace::where('sector_type', $sector_type['sector_type'])
  2521. ->where('type_id', 2)
  2522. ->update(['sector_type'=>$data['sector_type']]);
  2523. if(empty($com_place)){
  2524. return Result::error('组件版式编号同步更新失败!');
  2525. }
  2526. }
  2527. $map = $this->changeSectorPlaceJson($data['sector_type']) ?? [];
  2528. $data['cat_num'] = $map['cat_num'] ?? 0;
  2529. $map = json_encode($map['map_json'] ?? []);
  2530. $result = SectorPlace::where('id', $data['id'])->update($data);
  2531. } else {
  2532. $component_type = SectorPlace::where('id', $data['id'])->where('type_id', 2)->first();
  2533. if (empty($component_type)) {
  2534. return Result::error('组件版式不存在!');
  2535. }
  2536. unset($data['component_num']);
  2537. unset($data['map']);
  2538. $component = SectorPlace::where('sector_type', $data['sector_type'])
  2539. ->where(function($query)use($data){
  2540. $query->where('component_type', $data['component_type'])
  2541. ->Orwhere('sort_id', $data['sort_id']);
  2542. })
  2543. ->where('id', '!=', $data['id'])
  2544. ->where('type_id', 2)
  2545. ->get()->all();
  2546. if (!empty($component)) {
  2547. return Result::error('组件版式编号或位置已存在!');
  2548. }
  2549. if(in_array($data['type'],[23,24])){
  2550. $data['cat_num'] = 2;
  2551. }else if(in_array($data['type'],[7,14])){
  2552. $data['cat_num'] = 1;
  2553. }else{
  2554. $data['cat_num'] = 0;
  2555. }
  2556. $result['component'] = SectorPlace::where('id', $data['id'])->update($data);
  2557. $map = $this->changeSectorPlaceJson($data['sector_type']) ?? [];
  2558. $data['cat_num'] = $map['cat_num'] ?? 0;
  2559. $map = json_encode($map['map_json'] ?? []);
  2560. $result['map'] = SectorPlace::where('sector_type', $data['sector_type'])
  2561. ->where('type_id', 1)
  2562. ->update(['map'=>$map,'cat_num'=>$data['cat_num']]);
  2563. }
  2564. if (empty($result)) {
  2565. return Result::error('修改失败!');
  2566. }
  2567. // $update_result['sector_place'] = SectorPlace::where('id',$data['id'])->first();
  2568. // $update_result['map'] = $map;
  2569. return Result::success([
  2570. 'result'=>$result,
  2571. ]);
  2572. }
  2573. /**
  2574. * 通栏版式管理-删除通栏版式
  2575. */
  2576. public function delSectorPlace(array $data): array
  2577. {
  2578. $sector_place = SectorPlace::where('id', $data['id'])->first();
  2579. if (empty($sector_place)) {
  2580. return Result::error('通栏版式不存在!');
  2581. }
  2582. $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
  2583. if (!empty($sector)) {
  2584. return Result::error('通栏版式分类已应用,暂不可删除!');
  2585. }
  2586. Db::beginTransaction();
  2587. try{
  2588. if($sector_place['type_id'] == 1){
  2589. $where = ['sector_type'=>$sector_place['sector_type']];
  2590. }else{
  2591. $update_result = SectorPlace::where('sector_type', $sector_place['sector_type'])
  2592. ->where('type_id', 1)
  2593. ->whereNotNull('map')
  2594. ->update(['map' => null]);
  2595. if ($update_result === false) {
  2596. Db::rollBack();
  2597. return Result::error('通栏版式画布数据清空失败!');
  2598. }
  2599. $where = ['id'=>$data['id']];
  2600. }
  2601. $result = SectorPlace::where($where)->delete();
  2602. if (empty($result)) {
  2603. Db::rollBack();
  2604. return Result::error('删除失败!');
  2605. }
  2606. Db::commit();
  2607. return Result::success('删除成功!');
  2608. }catch(\Exception $e){
  2609. Db::rollBack();
  2610. return Result::error($e->getMessage());
  2611. }
  2612. }
  2613. /**
  2614. * 自助建站-通栏版式管理-获取通栏版式详情
  2615. */
  2616. public function getSectorPlaceInfo(array $data): array
  2617. {
  2618. $result = SectorPlace::where('sector_place.id',$data['id'])
  2619. ->leftJoin('size','sector_place.size_id','=','size.id')
  2620. ->select('sector_place.*','size.width','size.height')
  2621. ->first();
  2622. if(empty($result)){
  2623. return Result::error('通栏版式不存在!');
  2624. }
  2625. return Result::success($result);
  2626. }
  2627. /**
  2628. * 自助建站-通栏版式管理-获取通栏版式列表
  2629. */
  2630. public function getSectorPlaceSort(array $data): array
  2631. {
  2632. $sector = SectorPlace::where('sector_type',$data['sector_type'])->first();
  2633. if(empty($sector)){
  2634. return Result::error('通栏版式不存在!');
  2635. }
  2636. $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray();
  2637. if(count($component) < $sector['component_num']){
  2638. // 根据 $sector['component_num'] 的值生成对应元素数量的数组
  2639. if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
  2640. $num = intval($sector['component_num']);
  2641. if ($num > 0 && $num <= 10) {
  2642. // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
  2643. $sector_arr = range(1, $num);
  2644. } else {
  2645. $sector_arr = [];
  2646. }
  2647. } else {
  2648. $sector_arr = [];
  2649. }
  2650. // 获取 $sector_arr 中不在 $component 里的元素
  2651. $sort = array_diff($sector_arr, $component);
  2652. // return Result::success($sort);
  2653. }
  2654. if(!empty($sort)){
  2655. $sort = array_values(array_map(function($value) {
  2656. return ['id' => $value];
  2657. }, $sort));
  2658. }else{
  2659. return Result::error("组件版式数量已达上限!");
  2660. }
  2661. return Result::success($sort);
  2662. }
  2663. /**
  2664. * 获取所有通栏版式
  2665. */
  2666. public function getAllSectorPlace(array $data): array
  2667. {
  2668. $where['type_id'] = $data['type_id'];
  2669. // if($data['type_id'] == 1){
  2670. // $where['status'] = 1;
  2671. // }
  2672. $component_num = null;
  2673. if (isset($data['sector_type']) && !empty($data['sector_type'])) {
  2674. $where['sector_type'] = $data['sector_type'];
  2675. }
  2676. if(isset($data['component_num']) && !empty($data['component_num'])){
  2677. $where['component_num'] = $data['component_num'];
  2678. }
  2679. $sector_place = SectorPlace::where($where)
  2680. ->when($data['type_id'] == 2, function ($query) use ($data) {
  2681. $query->orderBy('sort_id', 'asc');
  2682. })
  2683. ->when(isset($data['component_num']) && !empty($data['component_num']), function ($query) use ($data) {
  2684. // 子查询:统计每个 sector_type 下 type_id=2 的子级数量
  2685. $sub = SectorPlace::selectRaw('sector_type, COUNT(*) as child_count')
  2686. ->where('type_id', 2)
  2687. ->groupBy('sector_type');
  2688. // 关联子查询,并保证子级数量等于传入的 component_num
  2689. $query->leftJoinSub($sub, 'component_place', function ($join) {
  2690. $join->on('sector_place.sector_type', '=', 'component_place.sector_type');
  2691. })->whereRaw('component_place.child_count = ?', [$data['component_num']]);
  2692. })
  2693. ->get()->all();
  2694. if($data['type_id'] == 2){
  2695. foreach($sector_place as $key => $value){
  2696. $sector_place[$key]['component'] = Component::where('type_id',$value['type'])
  2697. ->where('component_width',$value['width'])
  2698. ->where('component_height',$value['height'])
  2699. ->get()->all();
  2700. }
  2701. }
  2702. if(empty($sector_place)){
  2703. return Result::error('通栏版式不存在!');
  2704. }
  2705. return Result::success($sector_place);
  2706. }
  2707. /**
  2708. * 组件管理-获取组件类型
  2709. */
  2710. public function getComponentType(array $data): array
  2711. {
  2712. $where = [];
  2713. if(isset($data['id']) && !empty($data['id'])){
  2714. $where['id'] = $data['id'];
  2715. }
  2716. $component_type = ComponentType::where($where)->get()->all();
  2717. if(empty($component_type)){
  2718. return Result::error('组件类型不存在!');
  2719. }
  2720. if(count($component_type) == 1){
  2721. $all_code = json_decode($component_type[0]['com_code'] ?? '',true);
  2722. $component_type[0]['all_code'] = $all_code['listType'] ?? [];
  2723. $result = $component_type[0];
  2724. }else{
  2725. $result = $component_type;
  2726. }
  2727. if(empty($component_type)){
  2728. return Result::error('组件类型不存在!');
  2729. }
  2730. return Result::success($component_type);
  2731. }
  2732. /**
  2733. * 组件管理-获取所有组件
  2734. */
  2735. public function getAllComponent(array $data): array
  2736. {
  2737. $where = [];
  2738. if ((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))) {
  2739. $sector_id['sector.sector_id'] = $data['sector_id'];
  2740. $sector['sector_component.sort_id'] = $data['sort_id'];
  2741. $sectors = Sector::where($sector_id)
  2742. ->where('sector.template_id', $data['template_id'])
  2743. ->leftJoin('sector_component', 'sector.id', 'sector_component.sectorid')
  2744. ->where($sector)
  2745. ->select('sector.template_id', 'sector_component.component_id')
  2746. ->get()->all();
  2747. // return Result::success($sectors);
  2748. $component_id = array_column($sectors, 'component_id');
  2749. $template = ['template_id' => $sectors[0]['template_id']];
  2750. $component = ComponentImg::whereIn('component_id', $component_id)
  2751. ->where($template)
  2752. ->select('img_id', 'img_name', 'img_url', 'template_id')
  2753. ->orderBy('component_img.img_id', 'asc')
  2754. ->get()->all();
  2755. } else {
  2756. if (isset($data['type_id']) && !empty($data['type_id'])) {
  2757. $where['component.type_id'] = $data['type_id'];
  2758. }
  2759. if(isset($data['width']) && !empty($data['width'])){
  2760. $where['component.component_width'] = $data['width'];
  2761. }
  2762. if(isset($data['height']) && !empty($data['height'])){
  2763. $where['component.component_height'] = $data['height'];
  2764. }
  2765. $component = Component::where($where)
  2766. ->leftJoin('component_img', 'component.component_id', '=', 'component_img.component_id')
  2767. ->select('component.*', 'component_img.img_id', 'component_img.img_url', 'component_img.img_name')
  2768. // ->orderBy('component_img.template_id')
  2769. ->orderBy('component_img.img_id', 'asc')
  2770. ->get()->all();
  2771. }
  2772. if(empty($component)){
  2773. return Result::error('组件不存在!');
  2774. }
  2775. return Result::success($component);
  2776. }
  2777. /**
  2778. * 自助建站-流程管理-获取所有通栏
  2779. */
  2780. public function getAllSector(array $data): array
  2781. {
  2782. $where = [];
  2783. if (isset($data['template_id']) && !empty($data['template_id'])) {
  2784. $where['sector.template_id'] = $data['template_id'];
  2785. }
  2786. if (isset($data['page_type']) && !empty($data['page_type'])) {
  2787. array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
  2788. }
  2789. $sector = Sector::where($where)
  2790. ->leftJoin('sector_component', 'sector.id', '=', 'sector_component.sectorid')
  2791. ->leftJoin('component', 'sector_component.component_id', '=', 'component.component_type')
  2792. ->when(isset($data['template_id']), function ($query) use ($data) {
  2793. // 当 type_id 等于 11 时,关联查询 component_img 表并添加 img_url 字段
  2794. return $query->leftJoin('component_img', function ($join) use ($data) {
  2795. $join->on('component.component_type', '=', 'component_img.component_id')
  2796. ->where('component_img.template_id', $data['template_id'])
  2797. ->where('component.type_id', 11);
  2798. });
  2799. })
  2800. ->select(
  2801. 'sector.sector_name',
  2802. 'sector.sector_id',
  2803. 'component.*',
  2804. 'component_img.img_url',
  2805. 'sector.pic_height',
  2806. 'sector.sector_img',
  2807. 'sector_component.sort_id'
  2808. )
  2809. ->orderBy('sector_component.sort_id')
  2810. ->orderBy('sector_component.sector_id')
  2811. ->get()->all();
  2812. if(empty($sector)){
  2813. return Result::error('通栏不存在!');
  2814. }
  2815. $com_num = array_count_values(array_column($sector, 'sector_id'));
  2816. // 使用 array_keys 和 array_diff 函数,不使用循环将值不为 1 的字段键提取到 $sectors_key 中
  2817. $sectors_key = array_keys(array_diff($com_num, [1]));
  2818. // return Result::success($sector);
  2819. $num = 0;
  2820. $array_key = 0;
  2821. foreach ($sector as $key => $value) {
  2822. $sector_id = $value['sector_id'];
  2823. $sectors[$key] = $value['sector_id'];
  2824. // if(isset($comlist_key) && !empty($comlist_key)){
  2825. // $sector_arr_key[$key] = array_keys($comlist_key);
  2826. // }
  2827. if (in_array($sector_id, $sectors_key) && in_array($sector_id, $sectors)) {
  2828. $array_key = array_search($sector_id, $sectors);
  2829. if (isset($sectors) && !in_array($sector_id, $sectors)) {
  2830. $array_key++;
  2831. }
  2832. $num++;
  2833. $array_key++;
  2834. } else {
  2835. $array_key++;
  2836. }
  2837. // var_dump($array_key);
  2838. // var_dump(in_array($sector_id,$sectors_key));
  2839. $comlist_key[$array_key][$sector_id]['sectorName'] = $value['sector_name'];
  2840. $comlist_key[$array_key][$sector_id]['sectorHeight'] = $value['pic_height'];
  2841. $comlist_key[$array_key][$sector_id]['sectorImg'] = $value['sector_img'];
  2842. $componentList['component_style'] = 1;
  2843. $componentList['sort'] = $value['sort_id'];
  2844. // 1:资讯;2:广告;3:静态;
  2845. $component_data = empty($value['component_data']) ? null : (json_decode($value['component_data']) ?? []);
  2846. // 检查 $value['component_column'] 是否为非空字符串,避免传递 null 给 json_decode
  2847. $listType = empty($value['component_column']) ? null : (json_decode($value['component_column']) ?? []);;
  2848. // $page[$key][$setor_id]['componentList'] =
  2849. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;8:列表类组件;9:详情类组件;
  2850. // 10:二级导航栏类组件;11:广告类;12:静态资源类;13:单页导航类;14:广告资讯混合类;15:滚动图文;16:搜索框类;17:单页列表类;18:单页详情类;
  2851. if (in_array($value['type_id'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 17, 18])) {
  2852. $type = 1;
  2853. $componentList['component_type'] = $type;
  2854. if (in_array($value['type_id'], [7, 15]) && $data['page_type'] == 1) {
  2855. $component_data->componentData->name = '请选择导航..';
  2856. } else if (in_array($value['type_id'], [7, 15]) && $data['page_type'] == 2) {
  2857. $component_data->componentData->name = '自动生成';
  2858. } else if ($data['page_type'] == 3) {
  2859. if ($value['type_id'] == 4) {
  2860. $component_data->componentData->name = '最新资讯';
  2861. }
  2862. if ($value['type_id'] == 6) {
  2863. $component_data->componentData->name = '热点精选';
  2864. }
  2865. } else {
  2866. unset($component_data->componentData->name);
  2867. }
  2868. $componentData = $component_data->componentData;
  2869. $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
  2870. // $page_listType = is_object($listType) ? (array)$listType->listType ?? [] : $listType['listType'] ?? [];
  2871. $componentList['componentData']['listType'] = $listType->listType;
  2872. } else if (in_array($value['type_id'], [10, 12, 13, 16])) {
  2873. $type = 3;
  2874. $componentList['component_type'] = $type;
  2875. $componentList['componentData'] = (object)[];
  2876. } else if ($value['type_id'] == 11) {
  2877. $type = 2;
  2878. $componentList['component_type'] = $type;
  2879. $componentList['componentData'] = (object)[];
  2880. $ad = json_decode($value['ad']) ?? [];
  2881. if (is_object($ad)) {
  2882. $ad->thumb = $value['img_url'];
  2883. } else {
  2884. $ad['thumb'] = $value['img_url'];
  2885. }
  2886. $comlist_key[$array_key][$sector_id]['ad'] = is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
  2887. // 在 $page[$key][$setor_id] 中增加一个键值为 ad 的数组
  2888. // $page[$key][$setor_id]['ad'] = is_object($ad) ? (array)$ad->ad ?? [] : $ad['ad'] ?? [];
  2889. } else if ($value['type_id'] == 14) {
  2890. $type = 1;
  2891. $componentList['component_type'] = $type;
  2892. if ($data['page_type'] == 1) {
  2893. $component_data->componentData->name = '请选择导航..';
  2894. }
  2895. $componentData = $component_data->componentData;
  2896. $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
  2897. $componentList['componentData']['listType'] = $listType->listType;
  2898. $ad = json_decode($value['ad']) ?? [];
  2899. $comlist_key[$array_key][$sector_id]['ad'] = is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
  2900. } else {
  2901. }
  2902. if (in_array($sector_id, $sectors_key)) {
  2903. $comlist_key[$array_key][$sector_id]['componentList'][$num - 1] = $componentList;
  2904. $comlist_key[$array_key][$sector_id]['componentList'] = array_values($comlist_key[$array_key][$sector_id]['componentList']);
  2905. } else {
  2906. $comlist_key[$array_key][$sector_id]['componentList'] = $componentList;
  2907. }
  2908. $keys[$key] = $key;
  2909. }
  2910. // return Result::success($sector_arr_key);
  2911. // $page[$keys] = array_column($page,$sectors_key[0]);
  2912. $comlist_key = array_values($comlist_key);
  2913. // $sector_page = json_encode($comlist_key);
  2914. if (empty($comlist_key)) {
  2915. return Result::error('通栏不存在!');
  2916. }
  2917. return Result::success($comlist_key);
  2918. // return Result::success(gettype($listType));
  2919. }
  2920. /**
  2921. * 自助建站-组件管理-获取组件预览图列表
  2922. */
  2923. public function getComponentImgList(array $data): array
  2924. {
  2925. $where['component_img.component_id'] = $data['component_id'];
  2926. if(isset($data['template_id']) && !empty($data['template_id'])){
  2927. $where['component_img.template_id'] = $data['template_id'];
  2928. }
  2929. $component_img = ComponentImg::where($where)
  2930. ->leftJoin('template', 'component_img.template_id', '=', 'template.template_id')
  2931. ->leftJoin('component', 'component_img.component_id', '=', 'component.component_type')
  2932. ->select('component_img.*', 'template.template_name', 'component.component_name')
  2933. ->orderBy('component_img.template_id')
  2934. ->orderBy('component_img.img_id')
  2935. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2936. if($component_img->isEmpty()){
  2937. return Result::error('组件预览图不存在!');
  2938. }
  2939. $component_img = [
  2940. 'data' => $component_img->items(),
  2941. 'total' => $component_img->total(),
  2942. ];
  2943. return Result::success($component_img);
  2944. }
  2945. /**
  2946. * 自助建站-组件管理-添加组件预览图
  2947. */
  2948. public function addComponentImg(array $data): array
  2949. {
  2950. unset($data['user_id']);
  2951. $component = Component::where('component_type',$data['component_id'])->first();
  2952. if(empty($component)){
  2953. return Result::error('组件不存在!');
  2954. }
  2955. if($component['type_id'] == 11){
  2956. $img = ComponentImg::where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2957. if(!empty($img)){
  2958. return Result::error('该组件已存在此皮肤的预览图!');
  2959. }
  2960. }
  2961. $component = ComponentImg::where('template_id', $data['template_id'])->where('component_id', $data['component_id']);
  2962. if (!empty($component->where('img_id', $data['img_id'])->first())) {
  2963. return Result::error('组件预览图编号已存在!');
  2964. }
  2965. if(!empty($component->where('img_name', $data['img_name'])->first())){
  2966. return Result::error('组件预览图名称已存在!');
  2967. }
  2968. $component_img = ComponentImg::insertGetId($data);
  2969. if(empty($component_img)){
  2970. return Result::error('添加失败!');
  2971. }
  2972. return Result::success($component_img);
  2973. }
  2974. /**
  2975. * 自助建站-组件管理-删除组件预览图
  2976. */
  2977. public function delComponentImg(array $data): array
  2978. {
  2979. $component_img = ComponentImg::where('id', $data['id'])->delete();
  2980. if (empty($component_img)) {
  2981. return Result::error('删除失败!');
  2982. }
  2983. return Result::success($component_img);
  2984. }
  2985. /**
  2986. * 自助建站-组件管理-更新组件预览图
  2987. */
  2988. public function updateComponentImg(array $data): array
  2989. {
  2990. $component = Component::where('component_type',$data['component_id'])->first();
  2991. if(empty($component)){
  2992. return Result::error('组件不存在!');
  2993. }
  2994. if($component['type_id'] == 11){
  2995. $img = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2996. if(!empty($img)){
  2997. return Result::error('该组件已存在此皮肤的预览图!');
  2998. }
  2999. }
  3000. $component = ComponentImg::where('id', '!=', $data['id'])->where('component_id', $data['component_id'])->where('template_id', $data['template_id']);
  3001. if (!empty($component->where('img_id', $data['img_id'])->first())) {
  3002. return Result::error('组件预览图编号已存在!');
  3003. }
  3004. if(!empty($component->where('img_name', $data['img_name'])->first())){
  3005. return Result::error('组件预览图名称已存在!');
  3006. }
  3007. // return Result::success($component);
  3008. unset($data['user_id']);
  3009. // unset($data['updated_at']);
  3010. $id = $data['id'];
  3011. unset($data['id']);
  3012. // return Result::success($data);
  3013. $component_img = ComponentImg::where('id',$id)->update($data);
  3014. if(empty($component_img)){
  3015. return Result::error('更新失败!');
  3016. }
  3017. return Result::success($component_img);
  3018. }
  3019. /**
  3020. * 自助建站-获取通栏类型
  3021. */
  3022. public function getSectorType(array $data): array
  3023. {
  3024. $sector = SectorType::get()->all();
  3025. if(empty($sector)){
  3026. return Result::error('通栏类型不存在!');
  3027. }
  3028. return Result::success($sector);
  3029. }
  3030. /**
  3031. * 自助建站-随机获取模板
  3032. */
  3033. public function randomWebTemplate(array $data): array
  3034. {
  3035. $web = Website::where('id',$data['website_id'])->first();
  3036. if(empty($web)){
  3037. return Result::error('此网站不存在!');
  3038. }
  3039. $template = Template:: where('template_id',$data['template_id'])->first();
  3040. if(empty($template)){
  3041. return Result::error('此皮肤不存在!');
  3042. }
  3043. $rule = TemplateRule::first();
  3044. if(isset($data['page']) && !empty($data['page'])){
  3045. switch ($data['page']) {
  3046. case 1:
  3047. $rule = json_decode($rule['index_rule']);
  3048. break;
  3049. case 2:
  3050. $rule = json_decode($rule['class_rule']);
  3051. break;
  3052. case 3:
  3053. $rule = json_decode($rule['list_rule']);
  3054. break;
  3055. case 4:
  3056. $rule = json_decode($rule['info_rule']);
  3057. break;
  3058. case 5:
  3059. $rule = json_decode($rule['search_rule']);
  3060. break;
  3061. case 6:
  3062. $rule = json_decode($rule['footerlist_rule']);
  3063. break;
  3064. case 7:
  3065. $rule = json_decode($rule['footerinfo_rule']);
  3066. break;
  3067. default:
  3068. break;
  3069. }
  3070. }
  3071. $query = WebsiteCategory::where('website_id', $data['website_id']);
  3072. $cate_num = $query->where('is_show', 1)->count();
  3073. if ($cate_num < 5) {
  3074. return Result::error('此网站栏目过少,无法进行随机模板!');
  3075. }
  3076. $cate_num = min($cate_num, 24);
  3077. // return Result::success($data);
  3078. // $tempalte['index'] = $this->randomWeb($data,$index_rule);
  3079. if(isset($data['page']) && !empty($data['page'])){
  3080. $tempalte['index'] = $this->randomPage($data,$cate_num,$rule);
  3081. }else{
  3082. $data['page'] = 1;
  3083. $ruleIndex = json_decode($rule->index_rule);
  3084. $tempalte['index'] = $this->randomPage($data, $cate_num, $ruleIndex);
  3085. $data['page'] = 2;
  3086. $ruleClass = json_decode($rule->class_rule);
  3087. $class_cate_num = $query->where('pid','!=',0)->groupBy('pid')->selectRaw('pid, count(*) as count')->get()->pluck('count','pid');
  3088. $cate_num = max($class_cate_num);
  3089. $tempalte['class'] = $this->randomPage($data, $cate_num, $ruleClass);
  3090. $data['page'] = 3;
  3091. $ruleList = json_decode($rule->list_rule);
  3092. $tempalte['list'] = $this->randomPage($data,$cate_num,$ruleList);
  3093. $data['page'] = 4;
  3094. $ruleInfo = json_decode($rule->info_rule);
  3095. $tempalte['info'] = $this->randomPage($data,$cate_num,$ruleInfo);
  3096. $data['page'] = 5;
  3097. $ruleSearch = json_decode($rule->search_rule);
  3098. $tempalte['search'] = $this->randomPage($data,$cate_num,$ruleSearch);
  3099. $data['page'] = 6;
  3100. $ruleFooterlist = json_decode($rule->footerlist_rule);
  3101. $tempalte['footerlist'] = $this->randomPage($data,$cate_num,$ruleFooterlist);
  3102. $data['page'] = 7;
  3103. $ruleFooterinfo = json_decode($rule->footerinfo_rule);
  3104. $tempalte['footerinfo'] = $this->randomPage($data,$cate_num,$ruleFooterinfo);
  3105. }
  3106. // $index = $this->randomPage($data);
  3107. return Result::success($tempalte);
  3108. }
  3109. public function randomPage($data, $cate_num, $rule)
  3110. {
  3111. // return $rule;
  3112. $page = $data['page'];
  3113. $sectors = Sector::where('sector.template_id', $data['template_id'])
  3114. ->where('sector.page_type', 'like', '%' . $page . '%')
  3115. ->leftJoin('sector_place', function ($join) {
  3116. $join->on('sector.place_type', '=', 'sector_place.sector_type')
  3117. ->where('sector_place.type_id', 1);
  3118. })
  3119. ->whereNotNull('sector_place.sector_type')
  3120. ->select(
  3121. 'sector.sector_name',
  3122. 'sector.sector_id',
  3123. 'sector.pic_height',
  3124. 'sector.sector_img',
  3125. 'sector.sector_type',
  3126. 'sector.sector_code',
  3127. 'sector.id',
  3128. 'sector.sector_width',
  3129. 'sector.component_num',
  3130. 'sector.cat_num',
  3131. )
  3132. ->orderBy('id')
  3133. ->get()->all();
  3134. if (empty($sectors)) {
  3135. return Result::error('通栏不存在!');
  3136. }
  3137. // return $sectors;
  3138. // 获取每个类别的通栏数量
  3139. // $type_num = array_count_values(array_column($sectors, 'sector_type'));
  3140. // 此页面通栏的所有类别
  3141. // $all_sector_types = array_keys($type_num);
  3142. //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏
  3143. $all_sector_type = array_column($sectors, 'sector_type');
  3144. $grouped = array_reduce(
  3145. array_keys($sectors),
  3146. function ($carry, $idx) use ($sectors) {
  3147. $carry[$sectors[$idx]['sector_type']][] = $idx;
  3148. return $carry;
  3149. },
  3150. );
  3151. // 对 all_sector_type 去重后,每个类型随机挑一个 sector 索引
  3152. $unipue_sector_keys = array_combine(
  3153. array_keys($grouped),
  3154. array_map(
  3155. function ($type) use ($grouped) {
  3156. // 随机取一个索引
  3157. return $grouped[$type][array_rand($grouped[$type])];
  3158. },
  3159. array_keys($grouped)
  3160. )
  3161. );
  3162. // 1. 必需通栏:同一类型只随机保留一条
  3163. $rule_must_type = $rule->must_type;
  3164. $sector_num = $rule->max_num;
  3165. $unique_sector_type = array_flip($unipue_sector_keys);
  3166. $must_type = array_unique(array_intersect($unique_sector_type,$rule_must_type));
  3167. // 先求交集,去重后统计
  3168. $is_must = count($must_type);
  3169. if ($is_must != count(array_unique($must_type))) {
  3170. return '此页面缺少必要通栏!';
  3171. }
  3172. // 先按类型把 sectors 分组,key 为 sector_type,value 为对应 sector 数组
  3173. // return [
  3174. // 'unipue_sector_keys'=>$unipue_sector_keys,
  3175. // 'grouped'=>$grouped,
  3176. // ];
  3177. // 最终保留的 must_type 数组(去重且随机保留一个)
  3178. // return [
  3179. // 'must_key'=>$must_key,'must_type'=>$must_type,
  3180. // 'must_sector_keys'=>$must_sector_keys
  3181. // ];
  3182. // $must_sector_keys = array_intersect_key($unipue_sector_keys,$must_type);
  3183. $must_sector = array_values(array_intersect_key($sectors,$must_type));
  3184. $must_count = count($must_sector);
  3185. // return $sector_comnum;
  3186. // $must_sector = array_column($must_sector,'sector_type');
  3187. // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量
  3188. if ($must_count > $sector_num) {
  3189. return $must_sector;
  3190. }
  3191. // 必需通栏中的栏目数量
  3192. if($page == 1){
  3193. $must_cat_num = array_sum(array_column($must_sector,'cat_num'));
  3194. if($must_cat_num>$cate_num){
  3195. return '栏目数量过少,无法完成必需通栏随机!';
  3196. }
  3197. }
  3198. // var_dump(['must_cat_num'=>$must_cat_num,'cate_num'=>$cate_num]);
  3199. // 2.非必须通栏的随机处理
  3200. $not_must_type = array_unique(array_diff($unique_sector_type, $must_type));
  3201. $total = count($not_must_type);
  3202. $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
  3203. $and_type = $rule->and_type ?? [];
  3204. // return $not_must_type;
  3205. if ($randomCount === 0) {
  3206. $random_sector = [];
  3207. $rand_count = 0;
  3208. } else {
  3209. // 随机获取指定数量的键名(保留原键)
  3210. $randomKeys = array_rand($not_must_type, $randomCount);
  3211. // 处理单元素情况(array_rand返回字符串,需转为数组)
  3212. if (!is_array($randomKeys)) {
  3213. $randomKeys = [$randomKeys];
  3214. }
  3215. // 通栏关联性规则暂时无法实现---------------即若是随机了头条通栏必须要有轮播图通栏(皆为非必须通栏)
  3216. // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
  3217. // 判断 $oneDimensionalArray 是否为一维数组
  3218. // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) {
  3219. // 判断是否存在有关联通栏的交集
  3220. // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) {
  3221. // // var_dump("11111",$and_type);
  3222. // // var_dump("22222",$randomKeys);
  3223. // // 将一维数组中的值都放到随机数组中
  3224. // $randomKeys = array_merge($randomKeys, $and_type);
  3225. // }
  3226. // var_dump(count(array_intersect($and_type, $randomKeys)));
  3227. // }
  3228. // -----------------------------
  3229. // 根据随机键名提取元素(保留原键值关系)
  3230. // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
  3231. // var_dump("randomKeys",$randomKeys);
  3232. $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
  3233. $rand_count = count($random_sector);
  3234. }
  3235. if($page == 1){
  3236. $random_cat_num = array_sum(array_column($random_sector,'cat_num'));
  3237. $random_cat_num += $must_cat_num;
  3238. if($random_cat_num>$cate_num){
  3239. return '栏目数量过少,无法完成随机!';
  3240. }
  3241. }
  3242. // var_dump("randomKeys",$randomKeys);
  3243. // var_dump("random_sector",$random_sector);
  3244. // var_dump("rand_count", $rand_count);
  3245. // var_dump("random_cat_num", $random_cat_num);
  3246. // return $random_sector;
  3247. // var_dump("must_sector",$must_sector);
  3248. // var_dump("sector_num",$sector_num);
  3249. // var_dump("must_count",$must_count);
  3250. // return ['random_sector'=>$random_sector,'random_cat_num'=>$random_cat_num,'cate_num'=>$cate_num];
  3251. // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
  3252. $repeat_num = $sector_num - $rand_count - $must_count;
  3253. // 组合起来通栏
  3254. $sector_zuhe1 = array_merge($must_sector, $random_sector);
  3255. $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type');
  3256. $sector_ids = array_column($sector_zuhe1, 'id');
  3257. if($repeat_num > 0){
  3258. $rule_repeat = (array)$rule->type_max;
  3259. $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
  3260. // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
  3261. foreach ($rule_repeat as $key => $value) {
  3262. if (in_array($key, array_keys($sector_zuhe1_type))) {
  3263. $rule_repeat[$key] = $value - 1;
  3264. }
  3265. }
  3266. // var_dump("repeat_num", $repeat_num);
  3267. // 剩余通栏进行重复处理
  3268. $rule_repeat = (array)$rule->type_max;
  3269. // 取到可以重复的通栏类别
  3270. $rule_repaet_type = array_keys($rule_repeat);
  3271. $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type);
  3272. // 获取可以随机重复的通栏
  3273. $repaet_key = 0;
  3274. $type = [];
  3275. // return $rule_repaet_type;
  3276. // 防止无限循环,设置最大迭代次数
  3277. $max_iterations = 10;
  3278. $iteration_count = 0;
  3279. // 剩余的栏目数量
  3280. if($page == 1){
  3281. $sector_cate_comnum = $cate_num - $random_cat_num;
  3282. if($sector_cate_comnum < 0){
  3283. return '栏目数量过少,无法完成随机!';
  3284. }
  3285. }
  3286. // return [
  3287. // // 'repeat_type'=>$repeat_type,
  3288. // // 'repeat_num'=>$repeat_num,
  3289. // 'must_count'=>$must_count,
  3290. // 'rand_count'=>$rand_count,
  3291. // 'repeat_num'=>$repeat_num,
  3292. // 'sector_num'=>$sector_num,
  3293. // 'cate_num'=>$cate_num,
  3294. // 'sector_comnum'=>$sector_cate_comnum,
  3295. // 'random_cat_num'=>$random_cat_num,
  3296. // // 'rule_repeat'=>$rule_repeat,
  3297. // // 'unique_sector_type'=>$unique_sector_type,
  3298. // // 'all_sector_type'=>$all_sector_type,
  3299. // // 'sector_zuhe1' =>$sector_zuhe1
  3300. // ];
  3301. // -----新添加的代码-----
  3302. $re_sector = array_intersect($all_sector_type,$repeat_type);
  3303. $re_sector_key = array_keys($re_sector);
  3304. $every_repeattype_num = array_count_values($re_sector);
  3305. // $re_sector_key = array_keys($re_sector,1);
  3306. $repaet_key = 0;
  3307. $re_type_keys = [];
  3308. $one_sector_key = array_search(1,$re_sector);
  3309. $two_sector_key = array_search(2,$re_sector);
  3310. $three_sector_key = array_search(3,$re_sector);
  3311. $all_cat_num = array_column($sectors,'cat_num');
  3312. $repeat_sector_key = [];
  3313. // $rule_repeat['1'] = 1;
  3314. // return $rule_repeat;
  3315. // $three_sector_key = array_search(3,$re_sector);
  3316. // $min_one_sector = max(array_column('cat_num',$all_sector));
  3317. // 随机重复通栏-----
  3318. // $rule_repeat['1'] = 1;
  3319. // // 找出 all_sector_type 中值为 1 的键
  3320. while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) {
  3321. // 剩余通栏数量>0 若不是首页则继续;若是首页则需要判断剩余栏目数量是否>3
  3322. if ($repeat_num-$repaet_key > 0 && ($page != 1 || (isset($sector_cate_comnum) && $sector_cate_comnum > 3))) {
  3323. foreach($repeat_type as $key => $value){
  3324. $type_key = strval($value);
  3325. if(!isset($re_type_keys[$value])){
  3326. $re_type_keys[$value] = [];
  3327. }
  3328. // 剩余的通栏数量>0 并且 剩余的此类通栏数量>0
  3329. 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))){
  3330. // var_dump("0000",array_keys($re_sector,$value));
  3331. $one_sector_type = array_keys($re_sector,$value);
  3332. // 若是此类通栏的数量>1 则随机取一个对应的键值
  3333. if(count($one_sector_type) > 1){
  3334. // 从 $one_sector_type 数组中随机取一个键名,并取其对应值
  3335. $cat_key = $one_sector_type[array_rand($one_sector_type)];
  3336. }else{
  3337. $cat_key = $one_sector_type[0];
  3338. }
  3339. $repeat_sector_key[$repaet_key] = $cat_key;
  3340. $repaet_key++;
  3341. // var_dump("1115----------cat_key",$cat_key);
  3342. if($page == 1){
  3343. $sector_cate_comnum -= $all_cat_num[$cat_key];
  3344. }
  3345. if(!in_array($repaet_key,$re_type_keys[$value])){
  3346. $re_type_keys[$value][] = $repaet_key;
  3347. // var_dump("1117----------re_type_keys",$re_type_keys);
  3348. }
  3349. }else{
  3350. break;
  3351. }
  3352. }
  3353. } else {
  3354. break;
  3355. }
  3356. $iteration_count++;
  3357. }
  3358. // var_dump("num", $num);
  3359. // var_dump("repeat_sector_key", $repeat_sector_key);
  3360. // 若是首页的通栏没有到10个,栏目还有剩余1~3个,则继续随机(已知当剩余栏目为4还会随机)
  3361. if($page == 1 && count($repeat_sector_key) < $repeat_num && $sector_cate_comnum > 0){
  3362. if($sector_cate_comnum == 1){
  3363. array_push($repeat_sector_key,$one_sector_key);
  3364. }else if($sector_cate_comnum == 2){
  3365. array_push($repeat_sector_key,$two_sector_key);
  3366. }else{
  3367. array_push($repeat_sector_key,$three_sector_key);
  3368. }
  3369. }
  3370. // 随机的重复通栏键值转换成真正的通栏
  3371. $repeat_sector = [];
  3372. if (isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)) {
  3373. foreach ($repeat_sector_key as $key => $value) {
  3374. if (isset($sectors[$value])) {
  3375. $repeat_sector[$key] = $sectors[$value];
  3376. }else{
  3377. continue;
  3378. }
  3379. }
  3380. }
  3381. $repeat_count = count($repeat_sector);
  3382. // var_dump("repeat_count", $repeat_count);
  3383. // return [
  3384. // 'not_repeat_num'=>$rand_count + $must_count,
  3385. // // 'repeat_count'=>$repeat_count,
  3386. // 'repeat_num'=>$repeat_num,
  3387. // 'repeat_sector'=>$repeat_sector,
  3388. // 'repeat_type'=>$repeat_type,
  3389. // '$repaet_key'=>$repaet_key,
  3390. // 'all_sector_type'=>$all_sector_type,
  3391. // 're_sector'=>$re_sector,
  3392. // 're_sector_key'=>$re_sector_key,
  3393. // 'resdfddd'=>$every_repeattype_num,
  3394. // 'repeat_sector_key'=>$repeat_sector_key,
  3395. // // 'ddd'=>$ddd,
  3396. // // 'sector_num'=>$sector_num,
  3397. // // 'sector_zuhe1'=>count($sector_zuhe1),
  3398. // // // 'repeat_sector'=>$repeat_sector,
  3399. // // 'cate_num'=>$cate_num,
  3400. // // 'random_cat_num'=>$random_cat_num,
  3401. // 'rule_repeat' => $rule_repeat
  3402. // ];
  3403. // // var_dump("repeat_sector", $repeat_sector);
  3404. // return $repeat_sector;
  3405. $sector = array_merge($repeat_sector,$sector_zuhe1);
  3406. }else{
  3407. $sector = $sector_zuhe1;
  3408. }
  3409. // var_dump("sector_ids",$sector_ids);
  3410. //
  3411. // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
  3412. // var_dump("sector",$sector);
  3413. // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
  3414. $sector_specal_sort = array_column($sector,'sector_type');
  3415. if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
  3416. $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
  3417. $pic_key = array_search(7,$sector_specal_sort) ?? null;
  3418. $cat_key = array_search(5,$sector_specal_sort) ?? null;
  3419. $toutiao = $sector[$toutiao_key];
  3420. $pic = $sector[$pic_key];
  3421. $cat = $sector[$cat_key];
  3422. $sort_1 = $sector[0];
  3423. $sort_2 = $sector[1];
  3424. $sort_3 = $sector[2];
  3425. // return $sector_specal_sort;
  3426. // return [
  3427. // 'sector_ids' => array_column($sector_zuhe1,'id'),
  3428. // // 'repeat_sector' => $repeat_sector,
  3429. // 'sector_id' => array_column($sector,'id'),
  3430. // 'array_intersect' => count(array_intersect($sector,$must_sector)),
  3431. // 'old' => [ $toutiao_key,$pic_key,$cat_key],
  3432. // 'new' => [ $sort_1,$sort_2,$sort_3]
  3433. // ];
  3434. if($data['page'] == 1 || $data['page'] == '1'){
  3435. if($toutiao_key && $pic_key){
  3436. $sector[0] = $toutiao;
  3437. $sector[$toutiao_key] = $sort_1;
  3438. $sector[1] = $pic;
  3439. $sector[$pic_key] = $sort_2;
  3440. }else if($pic_key){
  3441. $sector[0] = $pic;
  3442. $sector[$pic_key] = $sort_1;
  3443. }else{
  3444. $sector[0] = $toutiao;
  3445. $sector[$toutiao_key] = $sort_1;
  3446. }
  3447. }
  3448. if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) {
  3449. $sector[0] = $cat;
  3450. $sector[$cat_key] = $sort_1;
  3451. if ($toutiao_key && $pic_key) {
  3452. $sector[1] = $toutiao;
  3453. $sector[$toutiao_key] = $sort_2;
  3454. $sector[2] = $pic;
  3455. $sector[$pic_key] = $sort_3;
  3456. }else if ($pic_key) {
  3457. $sector[1] = $pic;
  3458. $sector[$pic_key] = $sort_2;
  3459. }else if($toutiao_key){
  3460. $sector[1] = $toutiao;
  3461. $sector[$toutiao_key] = $sort_2;
  3462. }
  3463. }
  3464. }
  3465. $sector_id = array_column($sector,'id');
  3466. // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){
  3467. // return '此页面缺少必要通栏!';
  3468. // }
  3469. // return Result::success([
  3470. // // 'sector_id'=>$sector_id,
  3471. // 'sector'=>$sector,
  3472. // // 'cate_num'=>$cate_num,
  3473. // ]);
  3474. // $data['page'] = $page;
  3475. $component[] = $this->randomComponent($sector_id,$data,$sector);
  3476. var_dump("sector_id",$sector_id);
  3477. return [
  3478. // 'sector'=>$sector,
  3479. // 'must_sector'=>$must_sector,
  3480. // 'sector_count'=>count($sector),
  3481. // 'sectors'=>$sectors,
  3482. // 'sector_comnum'=>$sector_comnum,
  3483. // 'com_num'=>$com_num,
  3484. // 'cate_num'=>$cate_num,
  3485. // '$sector_zuhe1_comnum'=>$sector_zuhe1_comnum+$com_num,
  3486. 'component'=>$component,
  3487. // 'sector'=>$sector,
  3488. ];
  3489. }
  3490. public function randomComponent($sector_id, $data, $sector)
  3491. {
  3492. $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
  3493. ->leftJoin('component', 'component.component_type', 'sector_component.component_id')
  3494. ->leftJoin('component_img', function ($join) use ($data) {
  3495. $join->on('component_img.component_id', '=', 'component.component_type')
  3496. ->where('component_img.template_id', '=', $data['template_id'] ?? 0);
  3497. })
  3498. ->select(
  3499. 'component.*',
  3500. 'component_img.img_name',
  3501. 'component_img.img_id',
  3502. 'component_img.img_url',
  3503. 'sector_component.sort_id',
  3504. 'sector_component.sectorid'
  3505. )
  3506. ->orderBy('sector_component.sort_id')
  3507. ->orderBy('component.component_type')
  3508. ->get()->all();
  3509. // return $rawResults;
  3510. $img_key = 0;
  3511. $com_key = 0;
  3512. foreach( $rawResults as $key => $value){
  3513. $component_id = $value['component_type'];
  3514. $sort_id = $value['sort_id']-1;
  3515. $img_id = intval($value['img_id']);
  3516. $sector_id = $value['sectorid'];
  3517. $component_data = json_decode($value['component_data'] ?? '[]',true);
  3518. $component_column = json_decode($value['component_column'] ?? '[]',true);
  3519. $component_json['componentData'] = $component_data['componentData'] ?? [];
  3520. // 组件类型:1:头条;2:轮播图;3:推荐图;4:最新资讯;5:推荐资讯;6:热点资讯;7:栏目资讯类;
  3521. // 8:列表类;9:详情类;10:二级导航栏类;11:广告类;12:静态类;13:单页导航类;14:资讯广告混合类;
  3522. // 15:滚动图文类;16:搜索框类;17:单页列表类;18:单页详情类;19-24:选项卡3-7
  3523. if(!in_array($value['type_id'],[10,11,12,16,])){
  3524. $component_json['componentData']['listType'] = $component_column['listType'] ?? '';
  3525. }
  3526. if(!isset($component_img[$component_id])){
  3527. $component_img[$component_id] = [];
  3528. $com_key = 0;
  3529. }else{
  3530. $com_key++;
  3531. }
  3532. if(!isset($component_img[$component_id][$sort_id])){
  3533. $component_img[$component_id][$sort_id] = [];
  3534. $img_key = 0;
  3535. }else{
  3536. $img_key++;
  3537. }
  3538. if(!empty($img_id)){
  3539. $component_img[$component_id][$sort_id][$img_key] =
  3540. [
  3541. 'component_id' => $value['component_type'],
  3542. 'img_id' => $value['img_id'],
  3543. 'component_imgurl' => $value['img_url'],
  3544. ];
  3545. }
  3546. // else{
  3547. // $component_img[$component_id][$sort_id][$img_key] = [];
  3548. // }
  3549. // $component_img = array_values($component_img[$component_id][$sort_id]);
  3550. if(in_array($value['type_id'],[19, 20, 21, 22, 23, 24])){
  3551. $groupedResults[$sector_id][$sort_id] =
  3552. [
  3553. 'component_type' => $component_id,
  3554. 'sort' => $value['sort_id'],
  3555. 'type_id' => $value['type_id'],
  3556. 'componentData' => [
  3557. $component_json['componentData'],
  3558. $component_json['componentData'],
  3559. ],
  3560. 'images' => $component_img[$component_id][$sort_id]
  3561. ];
  3562. }else{
  3563. $groupedResults[$sector_id][$sort_id] =
  3564. [
  3565. 'component_type' => $component_id,
  3566. 'sort' => $value['sort_id'],
  3567. 'type_id' => $value['type_id'],
  3568. 'componentData' => $component_json['componentData'],
  3569. 'images' => $component_img[$component_id][$sort_id]
  3570. ];
  3571. }
  3572. if(in_array($value['type_id'],[11,14,24])){
  3573. $groupedResults[$sector_id][$sort_id]['ad'] = json_decode($value['ad'],JSON_UNESCAPED_UNICODE);
  3574. }
  3575. // [$img_id-1] = $value['img_id'];
  3576. }
  3577. // return ['groupedResults'=>$groupedResults,
  3578. // 'component_data'=>$component_data];
  3579. $y_num = 0;
  3580. foreach ($sector as $key => $value) {
  3581. // array_push($value['sort_id'],$key);
  3582. // $sector_id_ = $value['sector_id'];
  3583. $sectorid = $value['id'];
  3584. $template_data[$key] = [
  3585. 'sectorName' => $value['sector_id'],
  3586. 'sort' => $key + 1,
  3587. ];
  3588. $date = date('mdHisu'); // 获取时分秒微秒,转换为纯数字格式
  3589. // $date = 11010630021;
  3590. if (substr($date, 0, 1) == 0) {
  3591. $date = substr($date, 0, 10); // 截取前9位
  3592. } else {
  3593. $date = substr($date, 0, 9); // 截取前9位
  3594. }
  3595. $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
  3596. $y_num = $y_num + $value['pic_height'];
  3597. $sector_width = $value['sector_width'] / 100;
  3598. $canvas_data[$key] = [
  3599. 'i' => $random_num,
  3600. 'x' => 0,
  3601. 'y' => $y_num,
  3602. 'w' => $sector_width,
  3603. 'h' => $value['pic_height'],
  3604. 'type' => $value['sector_id'],
  3605. 'content' => [
  3606. 'sectorName' => $value['sector_id'],
  3607. ]
  3608. ];
  3609. // $sector_key[$key] = array_keys($value );
  3610. $sector_component = [];
  3611. if (isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])) {
  3612. $componet_key = 0;
  3613. foreach ($groupedResults[$sectorid] as $sortId => $components) {
  3614. // // // // 通栏某一位置的随机组件
  3615. // if (count($components) > 1) {
  3616. // $componentIds = count($components);
  3617. // $selectedComponentId = rand(0, $componentIds - 1);
  3618. // } else {
  3619. // $selectedComponentId = 0;
  3620. // }
  3621. // // var_dump($components[$selectedComponentId]);
  3622. // $Component = $components;
  3623. $Component['component_type'] = $components['component_type'] ?? 0;
  3624. $Component['sort'] = $components['sort'] ?? 1;
  3625. $Component['type_id'] = $components['type_id'] ?? 0;
  3626. // // 通栏某一位置的随机组件样式图
  3627. // // 检查 $Component['images'] 是否存在且为数组
  3628. if (isset($components['images']) && is_array($components['images'])) {
  3629. // var_dump('action1-------------------------------------sortId',$sortId);
  3630. $images = count($components['images']);
  3631. $selectedImage = 0;
  3632. if ($images > 1) {
  3633. $selectedImage = rand(0, $images - 1);
  3634. }
  3635. if (isset($components['images'][$selectedImage])) {
  3636. $Component['component_style'] = is_object($components['images'][$selectedImage]) ? $components['images'][$selectedImage]->img_id : $components['images'][$selectedImage]['img_id'];
  3637. }
  3638. }
  3639. if(isset($components['images'][$selectedImage])){
  3640. $image_info[$key] = is_object($components['images'][$selectedImage]) ? $components['images'][$selectedImage] : $components['images'][$selectedImage];
  3641. }
  3642. // var_dump($images);
  3643. // if(isset($components['type_id']) && in_array($components['type_id'], [19, 20, 21, 22, 23, 24])){
  3644. // // $Component['componentData'][0] = $components['componentData'] ?? [];
  3645. // // $Component['componentData'][1] = $components['componentData'] ?? [];
  3646. // }else{
  3647. $Component['componentData'] = $components['componentData'] ?? [];
  3648. // }
  3649. if ($data['page'] == 1 && (isset($components['type_id']) && in_array($components['type_id'], [7, 14, 15, 23, 24]))) {
  3650. if(in_array($components['type_id'], [23, 24])){
  3651. $Component['componentData'][0]['name'] = '请选择栏目';
  3652. $Component['componentData'][1]['name'] = '请选择栏目';
  3653. }else{
  3654. $Component['componentData']['name'] = '请选择栏目';
  3655. }
  3656. }
  3657. if ($data['page'] == 2 && (isset($components['type_id']) && in_array($components['type_id'], [7, 14, 15, 23, 24]))) {
  3658. if(in_array($components['type_id'], [23, 24])){
  3659. $Component['componentData'][0]['name'] = '自动生成';
  3660. $Component['componentData'][1]['name'] = '自动生成';
  3661. }else{
  3662. $Component['componentData']['name'] = '自动生成';
  3663. }
  3664. }
  3665. if (isset($components['type_id']) && $components['type_id'] == 11) {
  3666. $ad = $components['ad'];
  3667. $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? '';
  3668. $template_data[$key]['ad'] = $ad;
  3669. $canvas_adinfo[$key] = $ad;
  3670. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3671. // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合
  3672. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3673. $Component['componentData'] = [];
  3674. }
  3675. if (isset($components['type_id']) && $components['type_id'] == 14) {
  3676. $ad = $components['ad'];
  3677. $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? '';
  3678. $template_data[$key]['ad'] = $ad;
  3679. $canvas_adinfo[$key] = $ad;
  3680. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3681. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3682. // // $Component['componentData'] = [];
  3683. }
  3684. if (isset($components['type_id']) && $components['type_id'] == 24) {
  3685. $ad = $components['ad'][0];
  3686. // $ad['thumb'] = $image_info[$key]['component_imgurl'] ?? '';
  3687. $template_data[$key]['ad'][0] = $ad;
  3688. $template_data[$key]['ad'][1] = $ad;
  3689. $canvas_adinfo[$key] = $ad;
  3690. $canvas_data[$key]['content']['ad'][0] = $canvas_adinfo[$key];
  3691. $canvas_data[$key]['content']['ad'][1] = $canvas_adinfo[$key];
  3692. // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3693. // // $Component['componentData'] = [];
  3694. }
  3695. // // var_dump('---------------------------',$components[$selectedComponentId]);
  3696. $sector_component[$componet_key] = $Component;
  3697. $componet_key++;
  3698. }
  3699. // var_dump("image",$image);
  3700. }
  3701. $template_data[$key]['componentList'] = $sector_component;
  3702. $canvas_data[$key]['content']['componentList'] = $sector_component;
  3703. // $canvas_data[$key]['ad'] = $key;
  3704. $canvas_data[$key]['dataSort'] = $key;
  3705. $canvas_data[$key]['moved'] = false;
  3706. // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
  3707. }
  3708. // return $canvas_data;
  3709. return [
  3710. 'template_data' => $template_data,
  3711. 'canvas_data' => $canvas_data,
  3712. ];
  3713. }
  3714. // public function randomWeb($data,$index_rule)
  3715. // {
  3716. // // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件,
  3717. // // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码:
  3718. // $component_img = Sector::select('sector.id', 'sector.template_id', 'sector.page_type','sector.sector_id','sector.component_num'
  3719. // ,'sector.sector_name','sector.sector_type')
  3720. // ->where('sector.template_id', $data['template_id'])
  3721. // ->where('sector.page_type', 'like', '%' . $data['page'] . '%')
  3722. // ->with([
  3723. // 'sectorComponents' => function ($query) use ($data) {
  3724. // $query->select('sectorid', 'sort_id', 'component_id')
  3725. // ->with([
  3726. // 'component' => function ($q) use ($data) {
  3727. // $q->select('id', 'component_type', 'component_data', 'component_code', 'component_column', 'ad','type_id')
  3728. // ->with([
  3729. // 'componentImgs' => function ($subQ) use ($data) {
  3730. // $subQ
  3731. // ->where('template_id', $data['template_id'])
  3732. // // ->select('id', 'img_id', 'img_name', 'img_url', 'template_id')
  3733. // ;
  3734. // }
  3735. // ]);
  3736. // }
  3737. // ]);
  3738. // }
  3739. // ])
  3740. // ->get()
  3741. // ->all();
  3742. // // 通栏类别:1:资讯类:2:广告类;3:混合类;4:搜索框类;5:导航类;6:头条类;
  3743. // // 7:轮播图类;8:静态资源类;9:单页(列表)类;10:单页(详情)类;11:列表类;12:详情类;13:栏目轮播图类;',
  3744. // // {"max_num": 10, "min_num": 6, "and_type": [6, 7],
  3745. // // "type_max": {"1": 20, "2": 4, "3": 3}, "must_type": [1, 2]}
  3746. // $rule = is_array($index_rule) ? $index_rule : (array)$index_rule;
  3747. // // return gettype($rule);
  3748. // $must_num = count($rule['must_type']);
  3749. // $sector_type = array_column($component_img,'sector_type');
  3750. // $notmust_sectortype = count(array_diff(array_unique(array_column($component_img,'sector_type')),$rule['must_type']));
  3751. // $notmust_num = random_int(0,$notmust_sectortype);
  3752. // $min_num = $notmust_sectortype+$must_num;
  3753. // $max_num = $rule['max_num'];
  3754. // $index_num = random_int($min_num, $max_num);
  3755. // $repeat_num = $index_num - $must_num - $notmust_num;
  3756. // $category_num = WebsiteCategory::where('website_id',$data['website_id'])
  3757. // ->where('is_show',1)
  3758. // ->get()
  3759. // ->all();
  3760. // $num = 0;
  3761. // $sector_ids = [];
  3762. // $sector_sortids = [];
  3763. // foreach($components as $key => $value){
  3764. // if(!in_array($value['sectorid'],$sector_ids)){
  3765. // $num = 0;
  3766. // }
  3767. // if($value['type_id'] == 7 && !in_array($value['sort_id'],$sector_sortids[$value['sectorid']])){
  3768. // $num++;
  3769. // }
  3770. // $sector_typenum[$value['sectorid']]['num'] = $num;
  3771. // $sector_typenum[$value['sectorid']][$value['sort_id']] = $num;
  3772. // $sector_ids[$key] = $value['sectorid'];
  3773. // $sector_sortids[$value['sectorid']][$key] = $value['sort_id'];
  3774. // // }
  3775. // foreach($sector_type as $key => $value){
  3776. // if($value == 1){
  3777. // $sector_type[$key] = 1;
  3778. // }
  3779. // }
  3780. // return [
  3781. // 'index_num' => $index_num,
  3782. // 'min_num' => $min_num,
  3783. // '$sector_type' => $sector_type,
  3784. // 'notmust_sectortype' => $notmust_sectortype,
  3785. // 'must_num' => $must_num,
  3786. // 'notmust_num' => $notmust_num,
  3787. // 'repeat_num' => $repeat_num,
  3788. // // '$sector_typenum' => $sector_typenum,
  3789. // ];
  3790. // return $component_img;
  3791. // }
  3792. /**
  3793. * 自助建站-获取所有通栏、组件、组件样式
  3794. */
  3795. public function getAllSectorComponentStyle(array $data): array
  3796. {
  3797. $sector_component_style = Sector::where('sector.template_id',$data['template_id'])
  3798. ->leftJoin('sector_component','sector.id','sector_component.sectorid')
  3799. ->leftJoin('component', 'component.component_type', 'sector_component.component_id')
  3800. ->leftJoin('component_img', 'component_img.component_id', 'component.component_type')
  3801. // ->leftJoin('sector', 'sector.id', 'sector_component.sectorid')
  3802. ->where('component_img.template_id', '=', $data['template_id'])
  3803. ->select(
  3804. 'sector.sector_id',
  3805. 'sector.sector_keyword',
  3806. 'sector.component_num',
  3807. 'sector.sector_width',
  3808. 'sector.sector_height',
  3809. 'sector.pic_height',
  3810. 'component.component_type',
  3811. 'component.component_keyword',
  3812. 'component.component_data',
  3813. 'component.component_column',
  3814. 'component.ad',
  3815. 'component.type_id',
  3816. 'component_img.img_name',
  3817. 'component_img.img_id',
  3818. 'component_img.img_url',
  3819. 'sector_component.sort_id',
  3820. 'sector_component.sectorid',
  3821. )
  3822. ->orderBy('sector_component.sort_id')
  3823. ->orderBy('component.component_type')
  3824. ->get()->all();
  3825. $groupedResults = [];
  3826. $sort_ids = [];
  3827. $sector_ids = [];
  3828. $component_list = [];
  3829. $component_ids = [];
  3830. $img_ids = [];
  3831. // return Result::success($sector_component_style);
  3832. foreach ($sector_component_style as $key => $item) {
  3833. $sectorId = $item->sectorid;
  3834. $sortId = $item->sort_id;
  3835. $sector_type = $item->sector_type;
  3836. $component_num = $item->component_num;
  3837. $componentId = $item->component_type;
  3838. $component_data = $item->component_data;
  3839. $component_column = $item->component_column;
  3840. $component_keyword = $item->component_keyword;
  3841. $component_type = $item->type_id;
  3842. $component_ad = $item->ad;
  3843. $imgId = $item->img_id;
  3844. $sector_height = $item->sector_height;
  3845. $pic_height = $item->pic_height;
  3846. $sectorName = $item->sector_id;
  3847. if (!isset($groupedResults[$sectorId])) {
  3848. $groupedResults[$sectorId] = [];
  3849. }
  3850. $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
  3851. $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
  3852. $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
  3853. $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
  3854. // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
  3855. if (!isset($componentData['componentData']) || !is_array($componentData['componentData'])) {
  3856. $componentData['componentData'] = [];
  3857. }
  3858. $componentData['componentData'] += $component_listtype;
  3859. $component_code = $componentData;
  3860. $component_list[$key] = $component_code;
  3861. // $component_list[$key]['listType'] = $component_listtype;
  3862. // }
  3863. if(!in_array($sectorId,$sector_ids)){
  3864. $groupedResults[$sectorId] = [
  3865. 'sector_id' => $sectorName,
  3866. 'sector_keyword' => $item->sector_keyword,
  3867. 'component_num' => $component_num,
  3868. 'sector_width' => $item->sector_width,
  3869. 'sector_height' => $sector_height,
  3870. 'pic_height' => $pic_height,
  3871. ];
  3872. }
  3873. $sort_id = intval($sortId - 1);
  3874. if (!isset($com_key) || !in_array($sectorId, $sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])) {
  3875. $com_key = 0;
  3876. $img_key = 0;
  3877. } else if (in_array($sortId, $sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId, $component_ids)) {
  3878. $com_key++;
  3879. }
  3880. if (in_array($componentId, $component_ids) && !in_array($imgId, $img_ids[$componentId])) {
  3881. $img_key++;
  3882. } else {
  3883. $img_key = 0;
  3884. }
  3885. if (!in_array($componentId, $component_ids)) {
  3886. $groupedResults[$sectorId]['component_list'][$sort_id][$com_key] = [
  3887. 'component_type' => $item->component_type,
  3888. 'type_id' => $component_type,
  3889. 'sort' => $sortId,
  3890. ];
  3891. }
  3892. if($component_type == 11){
  3893. $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [
  3894. 'img_name' => $item->img_name,
  3895. 'img_id' => $imgId,
  3896. 'img_url' => $item->img_url,
  3897. ];
  3898. }else{
  3899. $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [
  3900. 'img_name' => $item->img_name,
  3901. 'img_id' => $imgId,
  3902. ];
  3903. }
  3904. $com_key = 0;
  3905. // }
  3906. // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
  3907. // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
  3908. // 19:选项卡-推荐图类;20:选项卡-最新资讯类;21:选项卡-推荐资讯类;22:选项卡-热点资讯类;23:选项卡-栏目资讯类;24:选项卡-栏目广告混合类;
  3909. if (is_scalar($sectorId) && is_scalar($sort_id)) {
  3910. if (!in_array($component_type, [10, 12, 16])) {
  3911. if ($component_type == 11 || $component_type == 14 || $component_type == 24) {
  3912. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad'] = $component_ad;
  3913. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id'];
  3914. }
  3915. if ($component_type != 11) {
  3916. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
  3917. }
  3918. } else {
  3919. $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = [];
  3920. }
  3921. } else {
  3922. // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
  3923. trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
  3924. }
  3925. // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
  3926. $component_ids[$key] = $componentId;
  3927. if (!isset($sort_ids[$sectorId])) {
  3928. $sort_ids[$sectorId] = [];
  3929. }
  3930. if (!in_array($sortId, $sort_ids[$sectorId])) {
  3931. $sort_ids[$sectorId][] = $sortId;
  3932. }
  3933. if (!isset($img_ids[$componentId])) {
  3934. $img_ids[$componentId] = [];
  3935. }
  3936. if (!in_array($imgId, $img_ids[$componentId])) {
  3937. $img_ids[$componentId][] = $imgId;
  3938. }
  3939. $sector_ids[$key] = $sectorId;
  3940. $sort_ids[$sectorId][$sort_id] = $sort_id;
  3941. // $img_ids[$componentId] = $imgId;
  3942. }
  3943. // return Result::success($groupedResults);
  3944. if (empty($groupedResults)) {
  3945. return Result::error('通栏组件样式不存在!');
  3946. }
  3947. $groupedResults = array_values($groupedResults);
  3948. return Result::success($groupedResults);
  3949. }
  3950. /**
  3951. * 自助建站-ai会话-获取会话列表
  3952. */
  3953. public function getAiSessionList(array $data): array
  3954. {
  3955. $where['user_id'] = $data['user_id'];
  3956. if(isset($data['website_id']) && !empty($data['website_id'])){
  3957. $where['website_id'] = $data['website_id'];
  3958. }
  3959. if(isset($data['template_id']) && !empty($data['template_id'])){
  3960. $where['template_id'] = $data['template_id'];
  3961. }
  3962. $result = AichatSession::where($where)->get()->all();
  3963. if(empty($result)){
  3964. return Result::error('会话不存在!');
  3965. }
  3966. return Result::success($result);
  3967. }
  3968. /**
  3969. * 自助建站-ai会话-创建会话
  3970. */
  3971. public function addAiSession(array $data): array
  3972. {
  3973. $website = Website::where('id',$data['website_id'])->first();
  3974. if(empty($website)){
  3975. return Result::error('网站不存在!');
  3976. }
  3977. $template = Template::where('template_id',$data['template_id'])->first();
  3978. if(empty($template)){
  3979. return Result::error('皮肤不存在!');
  3980. }
  3981. $date = date('Hisms');
  3982. // $date = 1631191019
  3983. $rand_num = rand(1000,9999);
  3984. $data['session_id'] = $date.$rand_num;
  3985. // $data['session_id'] = 16320610069512
  3986. $ai_sessions = AichatSession::where('user_id',$data['user_id'])->pluck('session_id')->all();
  3987. if(!empty($data['session_id']) && in_array($data['session_id'],$ai_sessions)){
  3988. $rand_num = rand(1000,9999);
  3989. $data['session_id'] = $date.$rand_num;
  3990. }
  3991. $result = AichatSession::insert($data);
  3992. if(empty($result)){
  3993. return Result::error('创建会话失败!');
  3994. }
  3995. return Result::success($result);
  3996. }
  3997. /**
  3998. * 自助建站-ai会话-修改会话
  3999. */
  4000. public function upAiSession(array $data): array
  4001. {
  4002. $where['user_id'] = $data['user_id'];
  4003. $where['session_id'] = $data['session_id'];
  4004. // return Result::success($where);
  4005. $aichat_session = AichatSession::where($where)->first();
  4006. if(empty($aichat_session)){
  4007. return Result::error('会话不存在!');
  4008. }
  4009. unset($data['session_id']);
  4010. // 会话状态:1-开启,0-关闭
  4011. if(isset($data['is_active']) && $aichat_session['is_active'] == $data['is_active']){
  4012. return Result::success('会话状态已为'.$data['is_active'].'!');
  4013. }
  4014. if(isset($data['is_active']) && ($data['is_active'] == 0 || $data['is_active'] == '0')){
  4015. $data['end_time'] = date('Y-m-d H:i:s');
  4016. }
  4017. $result = AichatSession::where($where)->update($data);
  4018. if(empty($result)){
  4019. return Result::error('修改会话失败!');
  4020. }
  4021. return Result::success($result);
  4022. }
  4023. /**
  4024. * 自助建站-ai会话-删除会话
  4025. */
  4026. public function delAiSession(array $data): array
  4027. {
  4028. $where['user_id'] = $data['user_id'];
  4029. $where['session_id'] = $data['session_id'];
  4030. // 删除会话
  4031. Db::beginTransaction();
  4032. try{
  4033. $result['ai_chat_session'] = AichatSession::where($where)->delete();
  4034. if(empty($result['ai_chat_session'])){
  4035. Db::rollBack();
  4036. return Result::error('删除会话消息失败!');
  4037. }
  4038. // 删除会话消息
  4039. $aichat = Aichat::where($where)->get()->all();
  4040. if(!empty($aichat)){
  4041. $result['ai_chat_message'] = Aichat::where($where)->delete();
  4042. if(empty($result['ai_chat_message'])){
  4043. Db::rollBack();
  4044. return Result::error('删除会话消息失败!');
  4045. }
  4046. }
  4047. // 删除草稿模板
  4048. $template_draftbox = TemplateDraftbox::where($where)->get()->all();
  4049. if(!empty($template_draftbox)){
  4050. $result['template_draftbox'] = TemplateDraftbox::where($where)->delete();
  4051. if(empty($result['template_draftbox'])){
  4052. Db::rollBack();
  4053. return Result::error('删除草稿模板失败!');
  4054. }
  4055. }
  4056. Db::commit();
  4057. return Result::success($result);
  4058. }catch(\Exception $e){
  4059. Db::rollBack();
  4060. return Result::error($e->getMessage());
  4061. }
  4062. }
  4063. /**
  4064. * 自助建站-ai会话-发送消息
  4065. */
  4066. public function sendAiMessage(array $data): array
  4067. {
  4068. $where['user_id'] = $data['user_id'];
  4069. $where['session_id'] = $data['session_id'];
  4070. $where['is_active'] = 1;
  4071. $aichat_session = AichatSession::where($where)->first();
  4072. if(empty($aichat_session)){
  4073. return Result::error('会话不存在!');
  4074. }
  4075. $data['message_type'] = empty($data['message_type']) ? 'text' : $data['message_type'];
  4076. $result = Aichat::insertGetId($data);
  4077. if(empty($result)){
  4078. return Result::error('发送消息失败!');
  4079. }
  4080. return Result::success($result);
  4081. }
  4082. /**
  4083. * 自助建站-ai会话-获取消息列表
  4084. */
  4085. public function getAiMessageList(array $data): array
  4086. {
  4087. $where['user_id'] = $data['user_id'];
  4088. $where['session_id'] = $data['session_id'];
  4089. $where['is_active'] = 1;
  4090. $session = AichatSession::where($where)->first();
  4091. if(empty($session)){
  4092. return Result::error('会话不存在!');
  4093. }
  4094. $aichat = Aichat::where('user_id',$data['user_id'])
  4095. ->where('session_id',$data['session_id'])
  4096. ->orderBy('send_time','desc')
  4097. ->get()->all();
  4098. if(empty($aichat)){
  4099. return Result::error('消息不存在!');
  4100. }
  4101. return Result::success($aichat);
  4102. }
  4103. /**
  4104. * 自助建站-ai会话-添加模板草稿
  4105. */
  4106. public function addTemplateDraftbox(array $data): array
  4107. {
  4108. $where['session_id'] = $data['session_id'];
  4109. $where['user_id'] = $data['user_id'];
  4110. // 后续若是用来对应会话记录,则此项需要修改
  4111. $session = AichatSession::where($where)->first();
  4112. if(empty($session)){
  4113. return Result::error('会话不存在!');
  4114. }
  4115. $data['website_id'] = $session['website_id'];
  4116. $data['template_id'] = $session['template_id'];
  4117. $template_draftbox = TemplateDraftbox::where($where)->first();
  4118. if(!empty($template_draftbox)){
  4119. return Result::error('草稿模板已存在!');
  4120. }
  4121. $result = TemplateDraftbox::insert($data);
  4122. if(empty($result)){
  4123. return Result::error('添加草稿模板失败!');
  4124. }
  4125. return Result::success($result);
  4126. }
  4127. /**
  4128. * 自助建站-ai会话-修改模板草稿
  4129. */
  4130. public function upTemplateDraftbox(array $data): array
  4131. {
  4132. $where['session_id'] = $data['session_id'];
  4133. $where['user_id'] = $data['user_id'];
  4134. $session = AichatSession::where($where)->first();
  4135. if(empty($session)){
  4136. return Result::error('会话不存在!');
  4137. }
  4138. if(isset($data['chat_id']) && !empty($data['chat_id'])){
  4139. $chat = Aichat::where($where)->where('chat_id',$data['chat_id'])->first();
  4140. if(empty($chat)){
  4141. return Result::error('消息不存在!');
  4142. }
  4143. }
  4144. // 后续若是用来对应会话记录,则此项需要修改
  4145. $template_draftbox = TemplateDraftbox::where($where)->first();
  4146. if(empty($template_draftbox)){
  4147. return Result::error('草稿模板不存在!');
  4148. }
  4149. unset($data['session_id']);
  4150. $result = TemplateDraftbox::where($where)->update($data);
  4151. if(empty($result)){
  4152. return Result::error('修改草稿模板失败!');
  4153. }
  4154. return Result::success($result);
  4155. }
  4156. /**
  4157. * 自助建站-ai会话-获取模板草稿列表
  4158. */
  4159. public function getTemplateDraftboxList(array $data): array
  4160. {
  4161. $where['session_id'] = $data['session_id'];
  4162. $where['user_id'] = $data['user_id'];
  4163. if(isset($data['website_id']) && !empty($data['website_id'])){
  4164. $where['website_id'] = $data['website_id'];
  4165. }
  4166. if(isset($data['template_id']) && !empty($data['template_id'])){
  4167. $where['template_id'] = $data['template_id'];
  4168. }
  4169. if(isset($data['status'])){
  4170. $where['status'] = $data['status'];
  4171. }
  4172. if(isset($data['chat_id']) && !empty($data['chat_id'])){
  4173. $where['chat_id'] = $data['chat_id'];
  4174. }
  4175. $template_draftbox = TemplateDraftbox::where($where)->get()->all();
  4176. if(empty($template_draftbox)){
  4177. return Result::error('草稿模板不存在!');
  4178. }
  4179. return Result::success($template_draftbox);
  4180. }
  4181. /**
  4182. * 自助建站-验证关键词填写重复问题
  4183. */
  4184. public function checkRepeatKeyword(array $data): array
  4185. {
  4186. if(empty($data['keyword']) || $data['keyword'] == '' || $data['keyword'] == '[]'){
  4187. return Result::success('关键词为空');
  4188. }
  4189. $arr_keyword = json_decode(stripslashes($data['keyword']), true) ?: [];
  4190. if(is_array($arr_keyword) && count($arr_keyword) > 0){
  4191. $keyword = array_map('trim', $arr_keyword);
  4192. if(count($keyword) != count(array_unique($keyword))){
  4193. return Result::error('关键词不能重复!');
  4194. }
  4195. }
  4196. $where['id'] = $data['id'] ?? null;
  4197. if($data['type'] == 'style'){
  4198. $type = '风格';
  4199. $all_keywords = TemplateClass::when(!empty($where),function($query)use($where){
  4200. $query->where('id','!=',$where['id']);
  4201. })->pluck('keyword')->toArray();
  4202. }
  4203. if($data['type'] == 'skin'){
  4204. $type = '皮肤';
  4205. $all_keywords = Template::when(!empty($where),function($query)use($where){
  4206. $query->where('id','!=',$where['id']);
  4207. })->pluck('template_keyword')->toArray();
  4208. }
  4209. if($data['type'] == 'sector'){
  4210. $type = '通栏';
  4211. $all_keywords = Sector::when(!empty($where),function($query)use($where){
  4212. $query->where('id','!=',$where['id']);
  4213. })->where('template_id',$data['template_id'])->pluck('sector_keyword')->toArray();
  4214. }
  4215. if($data['type'] == 'component'){
  4216. $type = '组件';
  4217. $all_keywords = Component::when(!empty($where),function($query)use($where){
  4218. $query->where('id','!=',$where['id']);
  4219. })->pluck('component_keyword')->toArray();
  4220. }
  4221. foreach($all_keywords as $key => $value){
  4222. $value_keyword = json_decode($value ?? '[]',true);
  4223. if(is_array($value_keyword) && count($value_keyword) > 0){
  4224. $repeat_keywords = array_intersect($arr_keyword,$value_keyword);
  4225. if(count($repeat_keywords) == count($keyword) && count($repeat_keywords) == count($value_keyword)){
  4226. return Result::error('此'.$type.'关键词已存在!');
  4227. }
  4228. }
  4229. }
  4230. return Result::success(['验证通过']);
  4231. }
  4232. /**
  4233. * 自助建站-验证网站模板相关类型
  4234. * 版本1----在构建网站首页之后使用
  4235. */
  4236. public function checkWebTemplate(array $data): array
  4237. {
  4238. // 网站id (是否存在该网站)
  4239. // (若是只传这个字段会判断是否关联导航池;)
  4240. // (再判断是否有三种类型的友情链接)
  4241. // (头部信息是否填写)
  4242. // (两种类型的底部导航)
  4243. $website = Website::where('id',$data['website_id'])->first();
  4244. if(empty($website)){
  4245. return Result::error('网站不存在!');
  4246. }
  4247. $template_data = json_decode($data['template_data'],true);
  4248. if(empty($template_data)){
  4249. return Result::error('模板数据不能为空!');
  4250. }
  4251. return Result::success($template_data);
  4252. if(!isset($template_data['website_id']) || empty($template_data['website_id'])){
  4253. return Result::error('网站ID不能为空!');
  4254. }
  4255. if(!isset($template_data['template_id']) || empty($template_data['template_id'])){
  4256. return Result::error('模板ID不能为空!');
  4257. }
  4258. return Result::success($template_data);
  4259. }
  4260. public function getAITemplate(array $data): array
  4261. {
  4262. $result['template'] = Template::select('id','template_id','template_name','template_keyword','page_type',
  4263. 'template_class_id','template_img','updated_at')
  4264. ->get()->all();
  4265. if (empty($result)) {
  4266. return Result::error("暂无皮肤", 0);
  4267. }
  4268. // $num = 1;
  4269. // 确保 $result 是数组且包含数据
  4270. if (!is_array($result) || empty($result)) {
  4271. return Result::error('模板数据异常');
  4272. }
  4273. // 初始化最新更新时间
  4274. $latestUpdate = null;
  4275. foreach ($result['template'] as $key => $value) {
  4276. $templateImg = json_decode($value['template_img'], true);
  4277. $result['template'][$key]['template_img'] = $templateImg;
  4278. // 更新最新时间:取最大的 updated_at
  4279. if ($latestUpdate === null || $value['updated_at'] > $latestUpdate) {
  4280. $latestUpdate = $value['updated_at'];
  4281. }
  4282. // unset($result[$key]['updated_at']);
  4283. }
  4284. // 将最新时间放入返回结果,若无数据则返回当前时间
  4285. $result['update_time'] = date('Y-m-d H:i:s', strtotime($latestUpdate));
  4286. return Result::success($result);
  4287. }
  4288. }