PublicRpcService.php 192 KB

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