PublicRpcService.php 190 KB

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