PublicRpcService.php 161 KB

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