PublicRpcService.php 183 KB

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