| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746 |
- <?php
- namespace App\JsonRpc;
- // use ___PHPSTORM_HELPERS\object;
- use _PHPStan_62c6a0a8b\OndraM\CiDetector\Env;
- use App\Model\BlackWord;
- use App\Model\Department;
- use App\Model\District;
- use App\Model\LetterOfComplaint;
- use App\Model\LetterType;
- use App\Model\LevelUser;
- use App\Model\UserLevel;
- use App\Tools\Result;
- use Hyperf\DbConnection\Db;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\RpcServer\Annotation\RpcService;
- use App\Service\MinioService;
- use Hyperf\Redis\Redis;
- use Overtrue\ChineseCalendar\Calendar;
- use App\Model\TemplateClass;
- use App\Model\Template;
- use App\Model\WebsiteTemplate;
- use App\Model\WebsiteTemplateInfo;
- use App\Model\Sector;
- use App\Model\Component;
- use App\Model\Link;
- use App\Model\FooterCategory;
- use App\Model\Size;
- use function Hyperf\Support\retry;
- use Hyperf\Paginator\Paginator;
- use App\Model\User;
- use App\Model\Website;
- use App\Constants\ErrorCode;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- use App\Model\SectorPlace;
- use App\Model\ComponentType;
- use App\Model\Article;
- use App\Model\WebsiteCategory;
- use App\Model\AdPlace;
- use App\Model\Company;
- use App\Model\WebsiteImg;
- use App\Model\SectorComponent;
- use App\Model\ComponentImg;
- use App\Model\TemplateRule;
- use App\Model\SectorType;
- use App\Model\Level;
- use App\Model\StyleCode;
- use think\controller\Rest;
- use App\Model\Aichat;
- use App\Model\AichatSession;
- use App\Model\TemplateDraftbox;
- #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class PublicRpcService implements PublicRpcServiceInterface
- {
- #[Inject]
- protected Redis $redis;
- /**
- * @param array $data
- * @return array
- */
- public function getDistrictList(array $data): array
- {
- $where = [];
- if (isset($data['keyWord'])) {
- $where = [
- ['name', 'like', '%' . $data['keyWord'] . '%']
- ];
- }
- $result = [];
- if (isset($data['pageSize'])) {
- $rep = District::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("code", "asc")->get();
- $count = District::where($where)->count();
- $result = [
- 'rows' => $rep,
- 'count' => $count
- ];
- } else {
- $result = District::where($data)->orderBy("code", "asc")->get();
- }
- return $result ? Result::success($result) : Result::error("没有查到数据");
- }
- /**
- * @param array $data
- * @return array
- */
- public function getUserLevelList(array $data): array
- {
- $where = [];
- if (isset($data['keyWord'])) {
- $where = [
- ['name', 'like', '%' . $data['keyWord'] . '%'],
- ];
- }
- $result = [];
- if (isset($data['pageSize'])) {
- $rep = UserLevel::where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("id", "asc")->get();
- $count = UserLevel::where($where)->count();
- $result = [
- 'rows' => $rep,
- 'count' => $count,
- ];
- } else {
- $result = UserLevel::orderBy("id", "asc")->get();
- }
- return $result ? Result::success($result) : Result::error("没有查到数据");
- }
- /**
- * 添加用户等级
- * @param array $data
- * @return array
- */
- public function addUserLevel(array $data): array
- {
- LevelUser::insertGetId($data);
- return Result::success([]);
- }
- /**
- * 更新等级
- * @param array $data
- * @return array
- */
- public function updateUserLevel(array $data): array
- {
- $result = LevelUser::where(['id' => $data['id']])->update($data);
- if ($result) {
- return Result::success($result);
- }
- return Result::error("更新失败");
- }
- /**
- * 删除等级
- * @param array $data
- * @return array
- */
- public function delUserLevel(array $data): array
- {
- $result = LevelUser::where(['id' => $data['id']])->delete();
- if ($result) {
- return Result::success($result);
- }
- return Result::error("删除失败");
- }
- /**
- * 查询投诉举报信息
- * @param array $data
- * @return array
- */
- public function getLetterOfComplaint(array $data = []): array
- {
- var_dump("====");
- $where = [];
- if (isset($data['user_id']) && !empty($data['user_id'])) {
- array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
- }
- if (isset($data['nature']) && !empty($data['nature'])) {
- array_push($where, ['letter_of_complaint.nature', '=', $data['nature']]);
- }
- if (isset($data['nature_level0']) && !empty($data['nature_level0'])) {
- array_push($where, ['letter_of_complaint.nature_level0', '=', $data['nature_level0']]);
- }
- if (isset($data['status']) && !empty($data['status'])) {
- array_push($where, ['letter_of_complaint.status', '=', $data['status']]);
- }
- $result = [];
- if (isset($data['pageSize'])) {
- $rep = LetterOfComplaint::where($where)
- ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
- ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
- ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
- ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
- ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
- ->select(
- "letter_of_complaint.*",
- "type_a.type_name as nature_name",
- "type_b.type_name as nature_name1",
- "type_c.type_name as nature_name0",
- "type_d.type_name as status_name",
- "type_e.type_name as nature_name3"
- )
- ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("letter_of_complaint.id", "desc")->get();
- $count = LetterOfComplaint::where($where)->count();
- if ($rep) {
- foreach ($rep as $val) {
- if ($val['judgment']) {
- $val['judgment'] = json_decode($val['judgment']);
- }
- if ($val['audio_and_video']) {
- $val['audio_and_video'] = json_decode($val['audio_and_video']);
- }
- if ($val['contract']) {
- $val['contract'] = json_decode($val['contract']);
- }
- if ($val['qualifications']) {
- $val['qualifications'] = json_decode($val['qualifications']);
- }
- }
- }
- $result = [
- 'rows' => $rep,
- 'count' => $count,
- ];
- } else {
- $result = LetterOfComplaint::where($where)
- ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
- ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
- ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
- ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
- ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
- ->select(
- "letter_of_complaint.*",
- "type_a.type_name as nature_name",
- "type_b.type_name as nature_name1",
- "type_c.type_name as nature_name0",
- "type_d.type_name as status_name",
- "type_e.type_name as nature_name3"
- )
- ->orderBy("letter_of_complaint.id", "desc")->get();
- }
- return $result ? Result::success($result) : Result::error("没有查到数据");
- }
- /**
- * 添加投诉举报信息
- * @param array $data
- * @return array
- */
- public function addLetterOfComplaint(array $data): array
- {
- $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
- $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
- $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
- $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
- unset($data['id']);
- $result = LetterOfComplaint::insertGetId($data);
- if (empty($result)) {
- return Result::error("创建失败", 0);
- } else {
- return Result::success(["id" => $result]);
- }
- }
- /**
- * 用户端更新投诉举报
- * @param array $data
- * @return array
- */
- public function userUpLetterOfComplaint(array $data): array
- {
- $data['judgment'] = $data['judgment'] ? json_encode($data['judgment']) : '';
- $data['audio_and_video'] = $data['audio_and_video'] ? json_encode($data['audio_and_video']) : '';
- $data['contract'] = $data['contract'] ? json_encode($data['contract']) : '';
- $data['qualifications'] = $data['qualifications'] ? json_encode($data['qualifications']) : '';
- $result = LetterOfComplaint::where(['id' => $data['id']])->update($data);
- if (empty($result)) {
- return Result::error("创建失败", 0);
- } else {
- return Result::success(["id" => $result]);
- }
- }
- /**
- * 管理后台更新投诉举报信息
- * @param array $data
- * @return array
- */
- public function upLetterOfComplaint(array $data): array
- {
- var_dump("admin:", $data);
- $where = [
- 'id' => $data['id'],
- ];
- $filtered_array = array_filter($data, function ($value) {
- return $value !== "" && $value !== null && $value !== false && !is_array($value) || !empty($value);
- });
- $filtered_array['judgment'] = isset($filtered_array['judgment']) ? json_encode($filtered_array['judgment']) : '';
- $filtered_array['audio_and_video'] = isset($filtered_array['audio_and_video']) ? json_encode($filtered_array['audio_and_video']) : '';
- $filtered_array['contract'] = isset($filtered_array['contract']) ? json_encode($filtered_array['contract']) : '';
- $filtered_array['qualifications'] = isset($filtered_array['qualifications']) ? json_encode($filtered_array['qualifications']) : '';
- unset($filtered_array['nature_name']);
- unset($filtered_array['type_name']);
- unset($filtered_array['nature_level_name']);
- unset($filtered_array['status_name']);
- unset($filtered_array['is_admin']);
- unset($filtered_array['type_level_name']);
- $result = LetterOfComplaint::where($where)->update($filtered_array);
- if ($result) {
- return Result::success($result);
- }
- return Result::error("更新失败", 0);
- }
- /**
- * 查询投诉举报记录
- * @param array $data
- * @return array
- */
- public function getLetterOfComplaintInfo(array $data): array
- {
- $where = [
- 'letter_of_complaint.id' => $data['id'],
- ];
- if (isset($data['user_id']) && !empty($data['user_id'])) {
- array_push($where, ['letter_of_complaint.user_id', '=', $data['user_id']]);
- }
- $result = LetterOfComplaint::where($where)
- ->leftJoin("letter_type as type_a", "letter_of_complaint.nature", "type_a.id")
- ->leftJoin("letter_type as type_c", "letter_of_complaint.nature_level0", "type_c.id")
- ->leftJoin("letter_type as type_b", "letter_of_complaint.nature_level1", "type_b.id")
- ->leftJoin("letter_type as type_e", "letter_of_complaint.nature_level3", "type_e.id")
- ->leftJoin("letter_type as type_d", "letter_of_complaint.status", "type_d.id")
- ->select(
- "letter_of_complaint.*",
- "type_a.type_name as nature_name",
- "type_b.type_name as nature_name1",
- "type_c.type_name as nature_name0",
- "type_d.type_name as status_name",
- "type_e.type_name as nature_name3"
- )
- ->first();
- return Result::success($result);
- }
- /**
- * 删除投诉举报信息
- * @param array $data
- * @return array
- */
- public function delLetterOfComplaint(array $data): array
- {
- $result = LetterOfComplaint::when($data, function ($query) use ($data) {
- if (isset($data['id']) && !empty($data['id'])) {
- $query->where(['id' => $data['id']]);
- }
- if (isset($data['user_id']) && !empty($data['user_id'])) {
- $query->where(['user_id' => $data['user_id']]);
- }
- })->delete();
- if (empty($result)) {
- return Result::error("删除失败", 0);
- } else {
- return Result::success();
- }
- }
- /**
- * 获取举报信息类型
- * @param array $data
- * @return array
- */
- public function getLetterType(array $data): array
- {
- $where = [];
- if (isset($data['type'])) {
- array_push($where, ['type', '=', $data['type']]);
- }
- if (isset($data['pid']) && $data['pid'] > 0) {
- array_push($where, ['pid', '=', $data['pid']]);
- }
- $result = LetterType::where($where)->orderBy('sort', 'asc')->get();
- return $result ? Result::success($result) : Result::error("没有查到数据");
- }
- /**
- * 更新举报类型
- * @param array $data
- * @return array
- */
- public function upLetterType(array $data): array
- {
- return [];
- }
- /**
- * 添加举报类型
- * @param array $data
- * @return array
- */
- public function addLetterType(array $data): array
- {
- $result = LetterType::insertGetId($data);
- if (empty($result)) {
- return Result::error("创建失败", 0);
- } else {
- return Result::success(["id" => $result]);
- }
- }
- /**
- * 删除举报类型
- * @param array $data
- * @return array
- */
- public function delLetterType(array $data): array
- {
- $result = LetterType::where('id', $data['id'])->delete();
- if (empty($result)) {
- return Result::error("删除失败", 0);
- } else {
- return Result::success();
- }
- }
- /**
- * 检测是否已经被接案
- * @param array $data
- * @return array
- */
- public function checkMeasure(array $data): array
- {
- $where = [
- 'id' => $data['id'],
- ];
- $letterOfComplaintInfo = LetterOfComplaint::where($where)->first();
- var_dump("查询数据:", $letterOfComplaintInfo['admin_id'], $data['user_id']);
- //操作人和当前登陆用户id 相等说明是当前人接收的案件
- if (($letterOfComplaintInfo['admin_id'] == $data['user_id']) || empty($letterOfComplaintInfo['admin_id'])) {
- return Result::success();
- } else {
- return Result::error("您不能处理其他人已经接过的案件", 0);
- }
- }
- /**
- * 后台获取职能部门
- * @param array $data
- * @return array
- */
- public function getZhinengbumenList(array $data): array
- {
- // 获取分页参数,默认每页 10 条记录
- $page = isset($data['page']) ? (int) $data['page'] : 1;
- $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
- // 查询数据并分页
- $query = Department::query();
- // 可以在这里添加更多的查询条件
- if (isset($data['search'])) {
- $query->where('name', 'like', '%' . $data['search'] . '%');
- }
- // 执行分页查询
- $result = $query->paginate($perPage, ['*'], 'page', $page);
- // 返回分页结果
- return Result::success([
- 'count' => $result->total(),
- 'current_page' => $result->currentPage(),
- 'last_page' => $result->lastPage(),
- 'pagesize' => $result->perPage(),
- 'rows' => $result->items(),
- ]);
- }
- /**
- * 添加获取职能部门
- * @param array $data
- * @return array
- */
- public function addZhinengbumen(array $data): array
- {
- $result = Department::insertGetId($data);
- if (empty($result)) {
- return Result::error("创建失败", 0);
- } else {
- return Result::success(["id" => $result]);
- }
- }
- public function delZhinengbumen(array $data): array
- {
- $result = Department::where('id', $data['id'])->delete();
- if (empty($result)) {
- return Result::error("删除失败", 0);
- } else {
- return Result::success();
- }
- }
- public function getZhinengbumen(array $data): array
- {
- $result = Department::where('id', $data['id'])
- ->orderBy('updated_at', 'desc')
- ->first();
- return Result::success($result);
- }
- public function getPidZhinengbumen(array $data): array
- {
- if (empty($data['pid'])) {
- $data['pid'] = 0;
- }
- $result = Department::where('pid', $data['pid'])->get();
- return Result::success($result);
- }
- public function modZhinengbumen(array $data): array
- {
- $result = Department::where('id', $data['id'])->update($data);
- if (empty($result)) {
- return Result::error("修改失败", 0);
- } else {
- return Result::success();
- }
- }
- /**
- * 查询职能列表
- * @param array $data
- * @return array
- */
- public function getDepartment(array $data): array
- {
- $where = [];
- if (isset($data['pid'])) {
- $where = [
- 'pid' => $data['pid'] ?? 0
- ];
- }
- $result = Department::when(!empty($where), function ($query) use ($where) {
- $query->where($where);
- })->orderBy("sort", "desc")->get();
- if (empty($result)) {
- return Result::error("查询失败", 0);
- } else {
- return Result::success($result);
- }
- }
- /**
- * 获取所有的buckets
- * @param array $data
- * @return array
- */
- public function getBuckets(array $data): array
- {
- $result = new MinioService();
- // 调用服务层的方法获取存储桶列表
- $bucketsResponse = $result->listBuckets();
- // 直接返回服务层生成的响应
- return Result::success($bucketsResponse['data']);
- }
- /**
- * 上传文件
- * @param array $data
- * @return array
- */
- public function uploadFile(array $data): array
- {
- $result = new MinioService();
- $rep = $result->uploadFile($data);
- if ($rep['code'] == 200) {
- return Result::success($rep['data']);
- } else {
- return Result::error("上传失败!");
- }
- }
- /**
- * 黑名单管理
- * @param array $data
- * @return array
- */
- public function getBlackWordList(array $data): array
- {
- $result = BlackWord::when($data, function ($query) use ($data) {
- if (isset($data['name']) && $data['name']) {
- $query->where('black_word.name', 'like', '%' . $data['name'] . '%');
- }
- })->orderBy('black_word.id', 'desc')
- ->paginate(
- intval($data['pageSize']),
- [
- 'black_word.*',
- ],
- 'page',
- intval($data['page'])
- );
- $count = $result->total();
- $returnData = [
- 'rows' => $result->items(),
- 'count' => $count
- ];
- return Result::success($returnData);
- }
- /**
- * 添加黑名单
- * @param array $data
- * @return array
- */
- public function addBlackWord(array $data): array
- {
- Db::beginTransaction();
- try {
- $info = BlackWord::where(['name' => $data['name']])->first();
- if ($info) {
- Db::rollBack();
- return Result::error("该黑名单已存在", 0);
- }
- $data['type'] = 10;
- $result = BlackWord::insertGetId($data);
- $redisKey = 'black_word';
- $this->redis->sAdd($redisKey, $data['name']);
- Db::commit();
- return Result::success(["id" => $result]);
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error("创建失败" . $e->getMessage(), 0);
- }
- }
- /**
- * 删除黑名单
- * @param array $data
- * @return array
- */
- public function delBlackWord(array $data): array
- {
- Db::beginTransaction();
- try {
- BlackWord::where(['name' => $data['name']])->delete();
- $redisKey = 'black_word';
- $this->redis->sRem($redisKey, $data['name']);
- Db::commit();
- return Result::success([]);
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error("删除失败" . $e->getMessage(), 0);
- }
- }
- /**
- * 修改违禁词
- * @param array $data
- * @return array
- */
- public function upBlackWord(array $data): array
- {
- Db::beginTransaction();
- try {
- $checkInfo = BlackWord::where(['name' => $data['name']])->first();
- if ($checkInfo) {
- Db::rollBack();
- return Result::error("该违禁词已经存在", 0);
- }
- $blackWordInfo = BlackWord::where(['id' => $data['id']])->first();
- if ($blackWordInfo) {
- //先删除redis
- $blackWordInfo = $blackWordInfo->toArray();
- $redisKey = 'black_word';
- $this->redis->sRem($redisKey, $blackWordInfo['name']);
- $this->redis->sAdd($redisKey, $data['name']);
- BlackWord::where(['id' => $data['id']])->update(['name' => $data['name']]);
- Db::commit();
- return Result::success([]);
- } else {
- Db::rollBack();
- return Result::error("系统错误", 0);
- }
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error("修改失败" . $e->getMessage(), 0);
- }
- }
- /**
- * 获取风格
- * @return void
- */
- public function getTemplateClassList(array $data): array
- {
- $where = [];
- if (isset($data['name']) && $data['name']) {
- array_push($where, ['template_class.name', 'like', '%' . $data['name'] . '%']);
- }
- if (isset($data['keyword']) && $data['keyword']) {
- array_push($where, ['template_class.keyword', 'like', '%' . $data['keyword'] . '%']);
- }
- $template = TemplateClass::when($where, function ($query) use ($where) {
- $query->where($where);
- });
- $count = $template->count();
- // $countQuery = clone $template;
- $row = $template
- ->leftJoin('template', function ($join) {
- $join->on('template_class.class_id', '=', 'template.template_class_id');
- })
- ->select('template_class.*', DB::raw('COUNT(template.template_class_id) as template_count'))
- ->groupBy('template_class.id')
- ->orderBy('template_class.id', 'desc')
- ->offset(($data['page'] - 1) * $data['pageSize'])
- ->limit($data['pageSize'])
- ->get();
- $result = [
- 'rows' => $row,
- 'count' => $count,
- ];
- if ($row->isEmpty()) {
- return Result::error("暂无风格", 0);
- } else {
- return Result::success($result);
- }
- }
- /**
- * 添加风格
- * @param
- * @return void
- */
- public function addTemplateClass(array $data): array
- {
- $data['keyword'] = json_encode($data['keyword']);
- $template_class = TemplateClass::where('name', $data['name'])
- ->orWhere('class_id', $data['class_id'])
- ->first();
- if ($template_class) {
- return Result::error("风格名称或者风格编号已存在,不可添加!", 0);
- }
- $result = TemplateClass::insertGetId($data);
- if (empty($result)) {
- return Result::error("创建风格失败", 0);
- } else {
- return Result::success(["id" => $result]);
- }
- }
- /**
- * 更新风格
- * @param array $data
- * @return array
- */
- public function upTemplateClass(array $data): array
- {
- $where = [
- 'id' => $data['id'],
- ];
- $template_class = TemplateClass::where($where)->first();
- if (empty($template_class)) {
- return Result::error("未查询到风格", 0);
- }
- if ($template_class->type == 1) {
- return Result::error("默认风格不能修改", 0);
- }
- $template = TemplateClass::where('id', '!=', $data['id'])
- // ->where(['name' => $data['name']])
- // ->orWhere(['class_id' => $data['class_id']])
- ->where(function ($query) use ($data) {
- $query->where('name', $data['name'])
- ->orWhere('class_id', $data['class_id']);
- })
- ->first();
- if ($template) {
- return Result::error("风格名称或者风格编号已存在,不可编辑!", 0);
- }
- $updateData = [
- 'name' => $data['name'],
- 'keyword' => json_encode($data['keyword']),
- 'class_id' => $data['class_id'],
- ];
- $result = TemplateClass::where($where)->update($updateData);
- if (empty($result)) {
- return Result::error("更新失败", 0);
- } else {
- return Result::success($result);
- }
- }
- /**
- * 删除风格
- * @param array $data
- * @return array
- */
- public function delTemplateClass(array $data): array
- {
- $where = [
- 'id' => $data['id'],
- ];
- $template_class = TemplateClass::where($where)->first();
- if (empty($template_class)) {
- return Result::error("未查询到风格", 0);
- }
- if ($template_class->type == 1) {
- return Result::error("默认风格不能删除", 0);
- }
- $template = Template::where('template_class_id', $template_class['class_id'])->get();
- if (!empty($template->all())) {
- try {
- // 默认风格 1
- Db::beginTransaction();
- $template = Template::where('template_class_id', $template['class_id'])->update(['template_class_id' => 1]);
- if (empty($template)) {
- Db::rollBack();
- return Result::error("删除失败", 0);
- }
- $result = TemplateClass::where($where)->delete();
- } catch (\Exception $e) {
- return Result::error("删除失败" . $e->getMessage(), 0);
- }
- } else {
- $result = TemplateClass::where('id', $data['id'])->delete();
- }
- if (empty($result)) {
- return Result::error("删除失败", 0);
- }
- return Result::success($result);
- }
- /**
- * 根据风格名称-获取获取所有风格
- * @param array $data
- * @return array
- */
- public function getTemplateClass(array $data): array
- {
- $where = [];
- if (!empty($data['template_name'])) {
- $where['name'] = $data['template_name'];
- }
- $result = TemplateClass::where($where)->get();
- if (empty($result)) {
- return Result::error("暂无风格", 0);
- }
- return Result::success($result);
- }
- /**
- * 获取皮肤列表
- * @param array $data
- * @return array
- */
- public function getTemplateList(array $data): array
- {
- $where = [];
- if (!empty($data['template_class_id'])) {
- $where['template_class_id'] = $data['template_class_id'];
- }
- if (!empty($data['template_name'])) {
- array_push($where, ['template_name', 'like', '%' . $data['template_name'] . '%']);
- }
- if (!empty($data['template_keyword'])) {
- array_push($where, ['template_keyword', 'like', '%' . $data['template_keyword'] . '%']);
- }
- $result = Template::where($where)
- ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
- ->select('template.*', 'template_class.name as template_class_name')
- ->orderBy('template.template_id', 'desc')
- ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
- if (empty($result)) {
- return Result::error("暂无皮肤", 0);
- }
- return Result::success($result);
- }
- public function getTemplateInfo(array $data): array
- {
- $result = Template::where('template.id', $data['id'])
- ->leftJoin('template_class', 'template.template_class_id', 'template_class.class_id')
- ->select('template.*', 'template_class.name as template_class_name')
- ->first();
- if (empty($result)) {
- return Result::error("暂无皮肤", 0);
- }
- return Result::success($result);
- }
- /**
- * 添加皮肤
- * @param array $data
- * @return array
- */
- public function addTemplate(array $data): array
- {
- var_dump($data);
- unset($data['user_id']);
- $template = Template::where('template_name', $data['template_name'])
- ->orWhere('template_id', $data['template_id'])
- ->first();
- if ($template) {
- return Result::error("皮肤名称或者皮肤编号已存在,不可添加!", 0);
- }
- $data['template_keyword'] = array_map('trim', $data['template_keyword']);
- $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
- $page_type = json_decode($data['page_type'], true);
- $data['page_type'] = json_encode(array_values(array_unique($page_type)));
- $result = Template::insertGetId($data);
- if (empty($result)) {
- return Result::error("创建失败", 0);
- } else {
- return Result::success($result);
- }
- }
- /**
- * 删除皮肤
- * @param array $data
- * @return array
- */
- public function delTemplate(array $data): array
- {
- $rector = Sector::where('template_id', $data['id'])->first();
- if (!empty($rector)) {
- return Result::error("此皮肤已被绑定通栏,不可删除", 0);
- }
- $template = Template::where('id', $data['id'])->first();
- if (empty($template)) {
- return Result::error("此皮肤不存在!", 0);
- }
- $web_template = WebsiteTemplateInfo::where('template_id', $template['template_id'])->first();
- if (!empty($web_template)) {
- return Result::error("此皮肤已被绑定网站,不可删除", 0);
- }
- // if ($template->type == 1) {
- // return Result::error("默认皮肤不能删除", 0);
- // }
- $component_img = ComponentImg::where('template_id', $template['template_id'])->get();
- if (!empty($component_img) && $component_img->count() > 0) {
- return Result::error('此皮肤已绑定组件预览图,不可删除!' . $component_img);
- }
- $result = Template::where('id', $data['id'])->delete();
- var_dump($result, '-------------------delete');
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error("删除失败", 0);
- }
- }
- /**
- * 更新皮肤
- * @param array $data
- * @return array
- */
- public function updateTemplate(array $data): array
- {
- unset($data['user_id']);
- $template = Template::where('id', $data['id'])
- ->first();
- if (empty($template)) {
- return Result::error("此皮肤不存在!", 0);
- }
- $template = Template::where('id', '!=', $data['id'])
- ->where(function ($query) use ($data) {
- $query->where('template_name', $data['template_name'])
- ->orWhere('template_id', $data['template_id']);
- })
- ->first();
- if ($template) {
- return Result::error("皮肤名称或者皮肤编号已存在,不可编辑!", 0);
- }
- $data['template_keyword'] = array_map('trim', $data['template_keyword']);
- $data['template_keyword'] = json_encode($data['template_keyword']) ?? '';
- $page_type = json_decode($data['page_type'], true);
- $data['page_type'] = json_encode(array_values(array_unique($page_type)));
- $result = Template::where('id', $data['id'])->update($data);
- var_dump($result, '-------------------update');
- if (!$result) {
- return Result::error("更新失败", 0);
- } else {
- return Result::success('更新成功');
- }
- }
- /**
- * 获取所有尺寸
- * @param array $data
- * @return array
- */
- public function getAllSize(array $data): array
- {
- if (isset($data['id']) && !empty($data['id'])) {
- $result = Size::where('id', $data['id'])->get()->all();
- } else {
- $result = Size::get()->all();
- }
- if (empty($result)) {
- return Result::error("暂无尺寸", 0);
- }
- return Result::success($result);
- }
- /**
- * 根据皮肤名称-获取所有皮肤
- * @param array $data
- * @return array
- */
- public function getAllTemplate(array $data): array
- {
- $where = [];
- if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
- $where[] = ['template_class_id', $data['template_class_id']];
- }
- if (isset($data['template_name']) && !empty($data['template_name'])) {
- $where[] = ['template_name', 'like', '%' . $data['template_name'] . '%'];
- }
- $result = Template::where($where)->get()->all();
- if (empty($result)) {
- return Result::error("暂无皮肤", 0);
- }
- return Result::success($result);
- }
- /**
- * 获取通栏列表
- * @param array $data
- * @return array
- */
- public function getSectorList(array $data): array
- {
- $where = [];
- if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
- $where['template.template_class_id'] = $data['template_class_id'];
- }
- if (isset($data['template_id']) && !empty($data['template_id'])) {
- array_push($where, ['template.template_id', $data['template_id']]);
- }
- if (isset($data['sector_name']) && !empty($data['sector_name'])) {
- array_push($where, ['sector.sector_name', 'like', '%' . $data['sector_name'] . '%']);
- }
- if (isset($data['height']) && !empty($data['height'])) {
- array_push($where, ['sector.pic_height', 'like', '%' . $data['height'] . '%']);
- }
- if (isset($data['page_type']) && !empty($data['page_type'])) {
- array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
- }
- $result = Sector::leftJoin('template', 'template.template_id', '=', 'sector.template_id')
- ->leftJoin('template_class', 'template_class.class_id', '=', 'template.template_class_id')
- ->leftJoin('sector_place', 'sector_place.sector_type', '=', 'sector.place_type')
- ->where($where)
- ->where(function ($query) {
- $query->where('sector_place.type_id', 1)
- ->orWhereNull('sector_place.type_id');
- })
- ->select(
- 'sector.*',
- 'template_class.name as class_name',
- 'template_class.class_id',
- 'template.template_name',
- 'sector_place.name as place_name',
- 'sector_place.sector_img as place_img'
- )
- ->orderBy('sector.id', 'desc')
- ->paginate($data['page_size'], ['*'], 'page', $data['page']);
- if (empty($result)) {
- return Result::error('暂无通栏!');
- }
- return Result::success($result);
- }
- /**
- * 获取通栏详情
- * @param array $data
- * @return array
- */
- public function getSectorInfo(array $data): array
- {
- // $where = [];
- $where[] = ['sector.id', '=', $data['id']];
- $result = Sector::where($where)
- ->leftJoin('template', 'template.template_id', '=', 'sector.template_id')
- ->select('sector.*', 'template.template_name', 'template.template_id')
- ->orderBy('sector.id', 'desc')
- ->first();
- $result['component'] = SectorComponent::where('sector_component.sector_id', $data['id'])
- ->leftJoin('component', 'component.component_type', '=', 'sector_component.component_id')
- ->select('component.component_type', 'sector_component.sort_id', 'component.component_name')
- ->get()->all();
- // $sorts = array_values(array_unique(array_column($components, 'sort_id')));
- // $result['component'] = $components;
- $result['place'] = SectorPlace::where('sector_place.sector_type', $result['place_type'])
- ->where('sector_place.type_id', 1)
- ->select('sector_place.sector_img', 'sector_place.map')
- ->first();
- if (empty($result)) {
- return Result::error('此通栏不存在!');
- }
- return Result::success($result);
- }
- /**
- * 获取经纬度信息
- * @return void
- */
- public function getIpInfo(array $data): array
- {
- if (empty($data['ip'])) {
- // 在RPC服务中,依赖$_SERVER是不可靠的,强制要求调用方传入IP
- return Result::error('IP地址不能为空!');
- }
- $client_ip = $data['ip'];
- try {
- // 直接从容器获取 Guzzle 客户端实例
- /** @var \GuzzleHttp\Client $client */
- $client = \Hyperf\Context\ApplicationContext::getContainer()->get(Client::class);
- // 使用新的 GeoJS API 地址
- $api_url = "https://get.geojs.io/v1/ip/geo/{$client_ip}.json";
- // 将 timeout 作为请求选项传递
- $response = $client->get($api_url, ['timeout' => 5]);
- if ($response->getStatusCode() !== 200) {
- return Result::error('IP查询接口请求失败,状态码:' . $response->getStatusCode());
- }
- $ip_info = json_decode($response->getBody()->getContents(), true);
- // GeoJS 的成功响应不包含 'status' 字段,直接检查关键数据是否存在
- if ($ip_info && isset($ip_info['latitude']) && isset($ip_info['longitude'])) {
- // GeoJS 返回的经纬度是字符串,转换为浮点数
- $ip_info['latitude'] = floatval($ip_info['latitude']);
- $ip_info['longitude'] = floatval($ip_info['longitude']);
- return Result::success($ip_info);
- }
- // 如果返回的数据不符合预期,提供一个通用错误
- $errorMessage = $ip_info['message'] ?? '获取IP地理位置信息失败';
- return Result::error($errorMessage);
- } catch (GuzzleException $e) {
- // 这个异常会给出详细的网络错误信息,如超时、无法解析域名等
- return Result::error('请求IP查询接口异常: ' . $e->getMessage());
- } catch (\Throwable $e) {
- return Result::error('获取IP信息时发生未知错误: ' . $e->getMessage());
- }
- }
- /**
- * 获取天气
- * @param array $data
- * @return array
- */
- public function getWeatherInfo(array $data): array
- {
- $month = $data['month'] ?? date('m');
- $day = $data['day'] ?? date('d');
- // 使用缓存键
- $cacheKey = "tsbb_data_weather_{$month}_{$day}";
- // 尝试从缓存获取数据
- $container = \Hyperf\Context\ApplicationContext::getContainer();
- $cache = $container->get(\Psr\SimpleCache\CacheInterface::class);
- if ($cachedData = $cache->get($cacheKey)) {
- return Result::success(unserialize($cachedData));
- }
- $location = $data['latitude'] . ":" . $data['longitude'];
- $api_url = "https://api.seniverse.com/v3/weather/now.json?key=" . \Hyperf\Support\env('WEATHER_KEY') . "&location=" . $location . "&language=zh-Hans&unit=c";
- $ch = curl_init($api_url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- // 解析 JSON 响应
- $WeatherInfo = json_decode($response, true);
- // 缓存结果,设置1小时过期
- $cache->set($cacheKey, serialize($WeatherInfo), 3600);
- if ($WeatherInfo) {
- return Result::success($WeatherInfo);
- } else {
- return Result::error("获取天气失败", 0);
- }
- }
- /**
- * 获取农历信息
- * @return void
- */
- public function getCalendar(array $data): array
- {
- $calendar = new Calendar();
- $result = $calendar->solar($data['year'], $data['month'], $data['day'], $data['hour']); // 阳历
- return Result::success($result);
- }
- /**
- * 添加通栏
- * @param array $data
- * @return array
- */
- public function addSector(array $data): array
- {
- unset($data['user_id']);
- // 对传入的 page_type 数组进行去重、转换为整数并重新索引
- $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
- $data['page_type'] = json_encode($data['page_type']);
- $template = Template::where('template_id', $data['template_id'])
- ->first();
- if (empty($template)) {
- return Result::error('皮肤不存在!');
- }
- $sector_query = Sector::where('template_id', $data['template_id']);
- if (!empty($sector_query->where('sector_id', $data['sector_id'])->first())) {
- return Result::error('通栏编号已存在!');
- }
- if(!empty($sector_query->where('sector_type', $data['place_type'])->where('component_code',$data['component_code'])->first())){
- return Result::error('通栏位置已存在!');
- }
- $sector_place = SectorPlace::where('sector_type', $data['place_type'])->get();
- if (empty($sector_place)) {
- return Result::error('位置不存在!');
- }
- $sort = json_decode($data['component_code'], true);
- $sector = 0;
- // $flattened = [];
- // $sector = [];
- $component = Component::whereIn('component_type', $sort)
- ->leftJoin('component_type','component.type_id','component_type.id')
- ->select('component.*','component_type.cat_num')
- ->get()->all();
-
- // return Result::success($data);
- if (empty($component)) {
- return Result::error('组件不存在!');
- }
- $data['cat_num'] = array_sum(array_column($component, 'cat_num'));
- $sector_component = [];
- foreach ($sort as $key => $subArray) {
- foreach ($subArray as $k => $item) {
- // // $component[] = $item;
- $sector_component[] = [
- 'sector_id' => $data['sector_id'],
- 'component_id' => $subArray[$k],
- 'sort_id' => $key + 1,
- 'place_id' => $data['place_type'],
- // 'sectorid' => $data['id'],
- ];
- // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
- // // $componentTypes[] = $item;
- // // $components[] = [
- // // 'sort_id' => $key + 1,
- // // 'component_type' => $item,
- // // ];
- // // $place_ids[] = $data['place_type'];
- }
- }
- // $component_num = count($component);
- // $count = count(array_unique($component));
- // 使用array_map批量替换sector_id
- // return Result::success($sector_component);
- if (count($sort) != $data['component_num']) {
- return Result::error('组件关联错误!');
- }
- // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
- $sector_code = [
- $data['sector_id'] => [
- 'sectorName' => $data['sector_name'],
- 'sectorHeight' => $data['pic_height'],
- 'sectorImg' => $data['sector_img'],
- ]
- ];
- // return Result::success($sector_code);
- $data['sector_code'] = json_encode($sector_code);
- Db::beginTransaction();
- try {
- $sector = Sector::insertGetId($data);
- if (empty($sector)) {
- Db::rollBack();
- return Result::error('添加失败');
- }
- $sector_com = array_map(function ($item) use ($sector) {
- // 替换当前记录的sector_id为变量值(保留其他字段不变)
- $item['sectorid'] = $sector;
- return $item;
- }, $sector_component);
- // Db::rollBack();
- // return Result::success($sector_com);
- $com_sector = SectorComponent::insert($sector_com);
- if (empty($com_sector)) {
- Db::rollBack();
- return Result::error('通栏关联组件失败!');
- }
- // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
- // 查询数据库中已存在的component_type(只保留存在的记录)
- // $existingTypes = Component::whereIn('component_type', $componentTypes)
- // ->pluck('component_type') // 提取存在的component_type
- // ->toArray();
- // if(empty($existingTypes)){
- // Db::rollBack();
- // return Result::error('组件不存在!');
- // }
- // // var_dump($existingTypes);
- // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增)
- // $updateData = array_filter($components, function ($item) use ($existingTypes) {
- // return in_array($item['component_type'], $existingTypes);
- // });
- // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增)
- // if (!empty($updateData)) {
- // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']);
- // }
- // if(empty($component_sort)){
- // Db::rollBack();
- // return Result::error('组件位置关联失败!');
- // }
- Db::commit();
- return Result::success($sector);
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- }
- /**
- * 删除通栏
- * @param array $data
- * @return array
- */
- public function delSector(array $data): array
- {
- $sector = Sector::where('id', $data['id'])->first();
- if (empty($sector)) {
- return Result::error('通栏不存在!');
- }
- Db::beginTransaction();
- try {
- $component_id = SectorComponent::where('sectorid', $data['id'])->delete();
- if (empty($component_id)) {
- Db::rollBack();
- return Result::error('解除相关组件关联关系失败!');
- }
- $result = Sector::where('id', $data['id'])->delete();
- if (empty($result)) {
- Db::rollBack();
- return Result::error('通栏删除失败!');
- }
- Db::commit();
- return Result::success('通栏删除成功!');
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- }
- /**
- * 修改通栏
- * @param array $data
- * @return array
- */
- public function updateSector(array $data): array
- {
- unset($data['user_id']);
- $sector = Sector::where('id', $data['id'])->first();
- if (empty($sector)) {
- return Result::error('通栏不存在!');
- }
- if ($sector['sector_id'] != $data['sector_id']) {
- $sector_id = Sector::where('sector_id', $data['sector_id'])->where('template_id', $data['template_id'])->first();
- if (!empty($sector_id)) {
- return Result::error('通栏编号已存在!');
- }
- }
- $sector_code = [
- $data['sector_id'] => [
- 'sectorName' => $data['sector_name'],
- 'sectorHeight' => $data['pic_height'],
- 'sectorImg' => $data['sector_img'],
- ]
- ];
- // return Result::success($sector_code);
- $data['sector_code'] = json_encode($sector_code);
- // 对传入的 page_type 数组进行去重、转换为整数并重新索引
- $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
- $data['page_type'] = json_encode($data['page_type']);
- $template = Template::where('template_id', $data['template_id'])
- ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
- ->first();
- if (empty($template)) {
- return Result::error('皮肤不存在!');
- }
- // 将嵌套二维数组展平为唯一值的一维数组
- $sort = json_decode($data['component_code'], true);
- $component_id = array_values(array_unique(array_merge(...$sort)));
- $component = Component::whereIn('component_type', $component_id)
- ->leftJoin('component_type','component.type_id','component_type.id')
- ->select('component.*','component_type.cat_num')
- ->get()->all();
- // return Result::success($data);
- if (empty($component)) {
- return Result::error('组件不存在!');
- }
- $data['cat_num'] = array_sum(array_column($component, 'cat_num'));
- $sector_component = [];
- foreach ($sort as $key => $subArray) {
- foreach ($subArray as $k => $item) {
- // // $component[] = $item;
- $sector_component[] = [
- 'sector_id' => $data['sector_id'],
- 'component_id' => $subArray[$k],
- 'sort_id' => $key + 1,
- 'place_id' => $data['place_type'],
- 'sectorid' => $data['id'],
- ];
- // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
- // // $componentTypes[] = $item;
- // // $components[] = [
- // // 'sort_id' => $key + 1,
- // // 'component_type' => $item,
- // // ];
- // // $place_ids[] = $data['place_type'];
- }
- }
- if (count($sort) != $data['component_num']) {
- return Result::error('组件关联错误!');
- }
- $data['sector_code'] = json_encode($sector_code);
- Db::beginTransaction();
- try {
- $com_sector = SectorComponent::where('sectorid', $data['id'])->pluck('component_id');
- if (empty($com_sector)) {
- Db::rollBack();
- return Result::error('通栏解除组件关联失败!');
- }
- $del_SectorComponent = SectorComponent::where('sectorid', $data['id'])->delete();
- if (empty($del_SectorComponent)) {
- Db::rollBack();
- return Result::error('通栏解除组件关联失败!1');
- }
- $sector = Sector::where('id', $data['id'])->update($data);
- if (empty($sector)) {
- Db::rollBack();
- return Result::error('修改失败');
- }
- $sector_com = array_map(function ($item) use ($data) {
- // 替换当前记录的sector_id为变量值(保留其他字段不变)
- $item['sectorid'] = $data['id'];
- return $item;
- }, $sector_component);
- // var_dump($sector_com);
- $com_sector = SectorComponent::insert($sector_com);
- if (empty($com_sector)) {
- Db::rollBack();
- return Result::error('通栏关联组件失败!');
- }
- // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
- // $up_component = Component::whereIn('component_id',$sector_component['component_type'])
- // ->update(['status' => 2,]);
- // if(empty($up_component)){
- // Db::rollBack();
- // return Result::error('通栏关联组件失败!');
- // }
- // 2. 查询数据库中已存在的component_type(只保留存在的记录)
- // $existingTypes = Component::whereIn('component_type', $componentTypes)
- // ->pluck('component_type') // 提取存在的component_type
- // ->toArray();
- // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增)
- // $updateData = array_filter($components, function ($item) use ($existingTypes) {
- // return in_array($item['component_type'], $existingTypes);
- // });
- // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增)
- // if (!empty($updateData)) {
- // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']);
- // }
- // if(empty($component_sort)){
- // Db::rollBack();
- // return Result::error('组件位置关联失败!');
- // }
- Db::commit();
- return Result::success($sector);
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- }
- public function getComponentList(array $data): array
- {
- $where = [];
- if (isset($data['component_name']) && !empty($data['component_name'])) {
- array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']);
- }
- if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
- array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
- array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
- } else if (isset($data['width']) && !empty($data['width'])) {
- array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
- } else if (isset($data['height']) && !empty($data['height'])) {
- array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
- } else {
- }
- if (isset($data['type_id']) && !empty($data['type_id'])) {
- $where['component.type_id'] = $data['type_id'];
- }
- $result = Component::where($where)
- ->leftJoin('component_type', 'component_type.id', '=', 'component.type_id')
- ->leftJoin('component_img', 'component.component_type', '=', 'component_img.component_id')
- ->select(
- 'component.*',
- 'component_type.com_typename',
- DB::raw('COUNT(component_img.id) as img_count')
- )
- ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量
- ->orderBy('component.updated_at', 'desc')
- ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
- if (empty($result)) {
- return Result::error('暂无数据');
- }
- return Result::success($result);
- }
- public function getComponentInfo(array $data): array
- {
- $where = ['component.id' => $data['id']];
- $result = Component::where($where)
- ->leftJoin('component_type', 'component_type.id', '=', 'component.type_id')
- ->select('component.*', 'component_type.com_typename')
- ->get();
- if (empty($result)) {
- return Result::error('暂无数据');
- }
- return Result::success($result);
- }
- /*
- 新增组件
- */
- public function addComponent(array $data): array
- {
- unset($data['user_id']);
- $component_id = Component::where('component_type', $data['component_type'])->first();
- if (!empty($component_id)) {
- return Result::error('组件编号已存在!');
- }
- $component_type = ComponentType::where('id', $data['type_id'])->first();
- if (empty($component_type)) {
- return Result::error('组件分类不存在!');
- }
- // '组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;
- // 8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;14:广告资讯混合类
- $data['type_id'] = intval($data['type_id']);
- $add_arr = [
- // 'template_id' => intval($data['template_id']),
- 'component_type' => intval($data['component_type']),
- 'component_name' => $data['component_name'],
- 'component_img' => $data['component_img'],
- 'component_width' => $data['component_width'],
- 'component_height' => $data['component_height'],
- 'type_id' => intval($data['type_id']),
- 'component_keyword' => $data['component_keyword'],
- ];
- $component_head_code = [
- 'component_id' => $add_arr['component_type'],
- 'component_type' => $add_arr['type_id'],
- 'component_name' => $add_arr['component_name'],
- 'component_style' => 1,
- ];
- $add_arr['component_code'] = json_encode($component_head_code, true);
- $type = $data['type_id'];
- switch ($type) {
- case 19: //19:选项卡-资讯-推荐图类组件;
- $type = 3;
- break;
- case 20: //20:选项卡-资讯-最新类组件;
- $type = 4;
- break;
- case 21: //21:选项卡-资讯-推荐类组件;
- $type = 5;
- break;
- case 22: //22:选项卡-资讯-热点类组件;
- $type = 6;
- break;
- case 23: //23:选项卡-资讯-栏目类组件;
- $type = 7;
- break;
- case 24: //24:选项卡-资讯-广告混合类组件;
- $type = 14;
- break;
- }
- switch ($type) {
- case 1: //1:资讯-头条组件;
- case 2: //2:资讯-轮播组件;
- case 3: //3:资讯-推荐图类组件;
- case 4: //4:资讯-最新类组件;
- case 5: //5:资讯-推荐类组件;
- case 6: //6:资讯-热点类组件;
- // $component_data['componentData']['name'] = '';
- $add_arr['level'] = $data['type_id'];
- $add_arr['img_num'] = intval($data['img_num']);
- $add_arr['text_num'] = intval($data['text_num']);
- $component_data['componentData'] = [
- // 'category_id' => '',
- 'level' => $add_arr['level'],
- 'name' => '',
- // 'category_arr' => [],
- 'imgSize' => $data['img_num'] ?? '',
- 'textSize' => $data['text_num'] ?? '',
- 'child' => [
- 'id' => '',
- 'imgSize' => '',
- 'textSize' => ''
- ]
- ];
- if ($data['type_id'] == 4) {
- $component_data['componentData']['name'] = '最新资讯';
- }
- if ($data['type_id'] == 5 || $data['type_id'] == 6) {
- $component_data['componentData']['name'] = '热点精选';
- }
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 7: //7:资讯-栏目类组件;
- case 15: //15:滚动图文类;
- $add_arr['img_num'] = intval($data['img_num']);
- $add_arr['text_num'] = intval($data['text_num']);
- $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
- $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
- $component_data['componentData'] = [
- 'category_id' => '',
- 'level' => '',
- 'name' => '',
- 'category_arr' => [],
- 'imgSize' => $add_arr['img_num'] ?? '',
- 'textSize' => $add_arr['text_num'] ?? '',
- 'child' => [
- 'id' => '',
- 'imgSize' => $add_arr['child_imgnum'] ?? '',
- 'textSize' => $add_arr['child_textnum'] ?? '',
- ]
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 8: //8:列表类组件;
- $add_arr['pageSize'] = intval($data['pageSize']);
- $component_data['componentData'] = [
- 'category_id' => '',
- 'pageType' => [
- 'page' => 1,
- 'pageSize' => $add_arr['pageSize'] ?? '',
- ]
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 9: //9:详情类组件;
- $component_data['componentData'] = [
- 'article_id' => '',
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 10: //10:二级导航类组件;
- $add_arr['cate_place'] = 1;
- $add_arr['num'] = intval($data['num']);
- $component_data['componentData'] = [
- 'pid' => '',
- 'placeid' => 1,
- 'num' => $add_arr['num'],
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 11: //11:广告类;
- $add_arr['ad_width'] = intval($data['ad_width']);
- $add_arr['ad_height'] = intval($data['ad_height']);
- $add_arr['component_width'] = intval($data['ad_width']);
- $add_arr['component_height'] = intval($data['ad_height']);
- $add_arr['ad_type'] = intval($data['ad_type']);
- // $add_arr['ad_img'] = $data['ad_img'];
- $ad = [
- 'width' => $add_arr['ad_width'],
- 'height' => $add_arr['ad_height'],
- 'name' => '',
- 'price' => '',
- 'introduce' => '',
- 'website_id' => '',
- // 'thumb' => $add_arr['ad_img'],
- 'typeid' => $add_arr['ad_type'],
- 'ad_tag' => '',
- ];
- $add_arr['ad'] = json_encode($ad, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- // $add_arr['adimg_info'] = $data['img_info'];
- // $ad_imginfo = json_decode($data['img_info'],true);
- break;
- // case 13: //13:底部导航类;
- case 17: //17:单页列表类;
- case 18: //18:单页详情类;
- $component_data['componentData'] = [
- 'fcat_id' => '',
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 14: //14:栏目资讯-广告混合类;
- $add_arr['ad_width'] = intval($data['ad_width']);
- $add_arr['ad_height'] = intval($data['ad_height']);
- $add_arr['component_width'] = intval($data['component_width']);
- $add_arr['component_height'] = intval($data['component_height']);
- $add_arr['ad_type'] = intval($data['ad_type']);
- $add_arr['ad_img'] = $data['ad_img'];
- $add_arr['img_num'] = intval($data['img_num']);
- $add_arr['text_num'] = intval($data['text_num']);
- $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
- $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
- $component_data['componentData'] = [
- 'category_id' => '',
- 'level' => '',
- 'name' => '',
- 'category_arr' => [],
- 'imgSize' => $data['img_num'] ?? '',
- 'textSize' => $data['text_num'] ?? '',
- 'child' => [
- 'id' => '',
- 'imgSize' => $data['child_imgnum'] ?? '',
- 'textSize' => $data['child_textnum'] ?? '',
- ]
- ];
- $ad = [
- 'width' => $add_arr['ad_width'],
- 'height' => $add_arr['ad_height'],
- 'name' => '',
- 'price' => '',
- 'introduce' => '',
- 'website_id' => '',
- 'thumb' => $add_arr['ad_img'],
- 'typeid' => $add_arr['ad_type'],
- 'ad_tag' => '',
- ];
- $add_arr['ad'] = json_encode($ad, true);
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- // $add_arr['adimg_info'] = $data['img_info'];
- // $ad_imginfo = json_decode($data['img_info'],true);
- break;
- default:
- break;
- }
- // if----带广告的特殊处理(广告位默认图上传静态资源的处理---已废弃)
- // if ($data['type_id'] == 11 || $data['type_id'] == 14) {
- // Db::beginTransaction();
- // try {
- // // $website_img = WebsiteImg::insertGetId($ad_imginfo);
- // // var_dump($website_img);
- // // $add_arr['ad_imgid'] = $website_img;
- // // if(empty($website_img)){
- // // Db::rollBack();
- // // return Result::error('广告默认图上传失败!');
- // // }
- // $result = Component::insertGetId($add_arr);
- // if (empty($result)) {
- // Db::rollBack();
- // return Result::error('添加失败!');
- // }
- // Db::commit();
- // } catch (\Exception $e) {
- // Db::rollback();
- // return Result::error('添加失败!');
- // }
- // }
- // else {
- // return Result::success($add_arr);
- $result = Component::insertGetId($add_arr);
- if (empty($result)) {
- return Result::error('添加组件失败!');
- }
- // }
- return Result::success($result);
- }
- public function delComponent(array $data): array
- {
- $component = Component::where('id', $data['id'])->first();
- if (empty($component)) {
- return Result::error('组件不存在!');
- }
- $component_id = SectorComponent::where('component_id', $component['component_type'])->first();
- if (!empty($component_id)) {
- return Result::error('请先解除相关组件关联关系!');
- }
- Db::beginTransaction();
- try {
- // if($component['type_id'] == 11 || $component['type_id'] == 14){
- // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
- // if(!empty($ad_img)){
- // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
- // if(empty($ad_img)){
- // Db::rollBack();
- // return Result::error('删除广告默认图失败!');
- // }
- // }
- // }
- $component_img = ComponentImg::where('component_id', $component['component_type'])->first();
- if (!empty($component_img)) {
- $del_img = ComponentImg::where('component_id', $component['component_type'])->delete();
- if (empty($del_img)) {
- Db::rollBack();
- return Result::error('删除组件预览图失败!');
- }
- }
- $result = Component::where('id', $data['id'])->delete();
- if (empty($result)) {
- Db::rollBack();
- return Result::error('删除失败!');
- }
- Db::commit();
- return Result::success($result);
- } catch (\Exception $e) {
- Db::rollback();
- return Result::error('删除失败!');
- }
- }
- public function updateComponent(array $data): array
- {
- unset($data['user_id']);
- $id = $data['id'];
- unset($data['id']);
- $component_id = Component::where('id', '!=', $id)->where('component_type', $data['component_type'])->first();
- if (!empty($component_id) || $component_id != null) {
- return Result::error('组件编号已存在!');
- }
- $component_type = ComponentType::where('id', $data['type_id'])->first();
- if (empty($component_type)) {
- return Result::error('组件分类不存在!');
- }
- $component = Component::where('id', $id)->first();
- if (empty($component)) {
- return Result::error('组件不存在!');
- }
- // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;
- // 7:资讯-栏目类组件;8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;
- $data['type_id'] = intval($data['type_id']);
- $add_arr = [
- // 'template_id' => intval($data['template_id']),
- 'component_type' => intval($data['component_type']),
- 'component_name' => $data['component_name'],
- 'component_img' => $data['component_img'],
- 'component_width' => $data['component_width'],
- 'component_height' => $data['component_height'],
- 'type_id' => intval($data['type_id']),
- 'component_keyword' => $data['component_keyword'],
- ];
- $component_head_code = [
- 'component_id' => $add_arr['component_type'],
- 'component_type' => $add_arr['type_id'],
- 'component_name' => $add_arr['component_name'],
- 'component_style' => 1,
- ];
- $add_arr['component_code'] = json_encode($component_head_code, true);
- $type = $data['type_id'];
- switch ($type) {
- case 19: //19:选项卡-资讯-推荐图类组件;
- $type = 3;
- break;
- case 20: //20:选项卡-资讯-最新类组件;
- $type = 4;
- break;
- case 21: //21:选项卡-资讯-推荐类组件;
- $type = 5;
- break;
- case 22: //22:选项卡-资讯-热点类组件;
- $type = 6;
- break;
- case 23: //23:选项卡-资讯-栏目类组件;
- $type = 7;
- break;
- case 24: //24:选项卡-资讯-广告混合类组件;
- $type = 14;
- break;
- }
- switch ($type) {
- case 1: //1:资讯-头条组件;
- case 2: //2:资讯-轮播组件;
- case 3: //3:资讯-推荐图类组件;
- case 4: //4:资讯-最新类组件;
- case 5: //5:资讯-推荐类组件;
- case 6: //6:资讯-热点类组件;
- // $component_data['componentData']['name'] = '';
- $add_arr['level'] = $data['type_id'];
- $add_arr['img_num'] = intval($data['img_num']);
- $add_arr['text_num'] = intval($data['text_num']);
- $component_data['componentData'] = [
- // 'category_id' => '',
- 'level' => $add_arr['level'],
- // 'category_arr' => [],
- 'name' => '',
- 'imgSize' => $data['img_num'] ?? '',
- 'textSize' => $data['text_num'] ?? '',
- 'child' => [
- 'id' => '',
- 'imgSize' => '',
- 'textSize' => ''
- ]
- ];
- if ($data['type_id'] == 4) {
- $component_data['componentData']['name'] = '最新资讯';
- }
- if ($data['type_id'] == 5 || $data['type_id'] == 6) {
- $component_data['componentData']['name'] = '热点精选';
- }
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 7: //7:资讯-栏目类组件;
- case 15: //15:滚动图文类;
- $add_arr['img_num'] = intval($data['img_num']);
- $add_arr['text_num'] = intval($data['text_num']);
- $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
- $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
- $component_data['componentData'] = [
- 'category_id' => '',
- 'level' => '',
- 'name' => '',
- 'category_arr' => [],
- 'imgSize' => $add_arr['img_num'] ?? '',
- 'textSize' => $add_arr['text_num'] ?? '',
- 'child' => [
- 'id' => '',
- 'imgSize' => $add_arr['child_imgnum'] ?? '',
- 'textSize' => $add_arr['child_textnum'] ?? '',
- ]
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 8: //8:列表类组件;
- $add_arr['pageSize'] = intval($data['pageSize']);
- $component_data['componentData'] = [
- 'category_id' => '',
- 'pageType' => [
- 'page' => 1,
- 'pageSize' => $add_arr['pageSize'] ?? '',
- ]
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 9: //9:详情类组件;
- $component_data['componentData'] = [
- 'article_id' => '',
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 10: //10:二级导航类组件;
- $add_arr['cate_place'] = 1;
- $add_arr['num'] = intval($data['num']);
- $component_data['componentData'] = [
- 'pid' => '',
- 'placeid' => 1,
- 'num' => $add_arr['num'],
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 11: //11:广告类;
- $add_arr['ad_width'] = intval($data['ad_width']);
- $add_arr['ad_height'] = intval($data['ad_height']);
- $add_arr['component_width'] = intval($data['ad_width']);
- $add_arr['component_height'] = intval($data['ad_height']);
- $add_arr['ad_type'] = intval($data['ad_type']);
- // $add_arr['ad_img'] = $data['ad_img'];
- $ad = [
- 'width' => $add_arr['ad_width'],
- 'height' => $add_arr['ad_height'],
- 'name' => '',
- 'price' => '',
- 'introduce' => '',
- 'website_id' => '',
- 'thumb' => $add_arr['ad_img'],
- // 'typeid' => $add_arr['ad_type'],
- 'ad_tag' => '',
- ];
- $add_arr['ad'] = json_encode($ad, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- // $add_arr['adimg_info'] = $data['img_info'];
- // $ad_imginfo = json_decode($data['img_info'],true);
- break;
- // case 13: //13:底部导航类;
- case 17: //17:单页列表类;
- case 18: //18:单页详情类;
- $component_data['componentData'] = [
- 'fcat_id' => '',
- ];
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- break;
- case 14:
- $add_arr['ad_width'] = intval($data['ad_width']);
- $add_arr['ad_height'] = intval($data['ad_height']);
- $add_arr['component_width'] = intval($data['component_width']);
- $add_arr['component_height'] = intval($data['component_height']);
- $add_arr['ad_type'] = intval($data['ad_type']);
- $add_arr['ad_img'] = $data['ad_img'];
- $add_arr['img_num'] = intval($data['img_num']);
- $add_arr['text_num'] = intval($data['text_num']);
- $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
- $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
- $component_data['componentData'] = [
- 'category_id' => '',
- 'level' => '',
- 'name' => '',
- 'category_arr' => [],
- 'imgSize' => $data['img_num'] ?? '',
- 'textSize' => $data['text_num'] ?? '',
- 'child' => [
- 'id' => '',
- 'imgSize' => $data['child_imgnum'] ?? '',
- 'textSize' => $data['child_textnum'] ?? '',
- ]
- ];
- $ad = [
- 'width' => $add_arr['ad_width'],
- 'height' => $add_arr['ad_height'],
- 'name' => '',
- 'price' => '',
- 'introduce' => '',
- 'website_id' => '',
- 'thumb' => $add_arr['ad_img'],
- 'typeid' => $add_arr['ad_type'],
- 'ad_tag' => '',
- ];
- $add_arr['ad'] = json_encode($ad, true);
- $add_arr['component_data'] = json_encode($component_data, true);
- $add_arr['component_column'] = $component_type['com_code'] ?? '';
- // $add_arr['adimg_info'] = $data['img_info'];
- // $ad_imginfo = json_decode($data['img_info'],true);
- break;
- default:
- break;
- }
- // var_dump("ad_imginfo:",$ad_imginfo);
- $cll_column = [
- 'img_num' => null,
- 'text_num' => null,
- 'child_imgnum' => null,
- 'child_textnum' => null,
- 'category_id' => null,
- 'child_id' => null,
- 'num' => null,
- 'level' => null,
- 'fcatid' => null,
- 'article_id' => null,
- 'cate_place' => null,
- 'pageSize' => null,
- // 'ad_img' => null,
- 'ad_width' => null,
- 'ad_height' => null,
- 'ad' => '',
- 'ad_type' => null,
- // 'ad_imgid' => null,
- 'adimg_info' => '[]',
- ];
- Db::beginTransaction();
- try {
- // return Result::success($add_arr);
- // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] != 11 && $data['type_id'] != 14)){
- // // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
- // if(empty($website_img)){
- // Db::rollBack();
- // return Result::error('广告默认图删除失败!');
- // }
- // var_dump("删除",$website_img);
- // }
- // if(($data['type_id'] == 11 || $data['type_id'] == 14) && ($component['type_id'] != 11 && $component['type_id'] != 14)){
- // $website_img = WebsiteImg::insertGetId($ad_imginfo);
- // $add_arr['ad_imgid'] = $website_img;
- // if(empty($website_img)){
- // Db::rollBack();
- // return Result::error('广告默认图上传失败!');
- // }
- // var_dump("上传",$website_img);
- // }
- // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] == 11 || $data['type_id'] == 14)){
- // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
- // if(empty($website_img)){
- // $website_img = WebsiteImg::insertGetId($ad_imginfo);
- // $add_arr['ad_imgid'] = $website_img;
- // }else{
- // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->update($ad_imginfo);
- // $add_arr['ad_imgid'] = $component['ad_imgid'];
- // }
- // if(empty($website_img)){
- // Db::rollBack();
- // return Result::error('广告默认图修改失败!');
- // }
- // }
- $add_arr = array_merge($cll_column, $add_arr);
- $result = Component::where('id', $id)->update($add_arr);
- if (empty($result)) {
- Db::rollBack();
- return Result::error('修改失败!');
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error('修改失败!');
- }
- return Result::success($result);
- }
- public function getWebsiteTemplateInfo(array $data)
- {
- $where = [];
- if (isset($data['id'])) {
- $where[] = ['id', '=', $data['id']];
- }
- $result = WebsiteTemplateInfo::where($where)
- ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
- ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
- ->select('website_template_info.*', 'website.website_name')
- ->get();
- if ($result) {
- return Result::success($result);
- } else {
- return Result::error('暂无数据');
- }
- }
- /**
- * 自助建站----1.获取页面类型回显
- * @param array $data
- * @return array
- */
- public function getWebPageType(array $data): array
- {
- // 1:图片 2:文字 3:底部
- $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
- if (empty($friend_link->toArray())) {
- return Result::error('暂无友情链接数据!');
- }
- $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
- if (empty($footer_category->toArray())) {
- return Result::error('暂无底部导航数据!');
- }
- $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
- if (empty($page_type)) {
- $page_type = [];
- } else {
- $page_type = json_decode($page_type, true);
- }
- // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
- if (isset($page_type) && in_array(5, $page_type)) {
- $page_type = 1;
- } else {
- $page_type = 0;
- }
- $result = [
- 'friend_link' => $friend_link,
- 'footer_category' => $footer_category,
- 'page_type' => $page_type,
- ];
- return Result::success($result);
- }
- /**
- * 自助建站----2.添加页面类型
- * @param array $data
- * @return array
- */
- public function addWebPageType(array $data): array
- {
- // return Result::success($data);
- // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
- if ($data['is_search'] == 1) {
- $page_type = json_encode([1, 2, 3, 4, 5, 6, 7]);
- } else {
- $page_type = json_encode([1, 2, 3, 4, 6, 7]);
- }
- // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
- $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
- $website_template = WebsiteTemplate::where('website_id', $data['website_id'])->first();
- Db::beginTransaction();
- try {
- if (empty($website_template_info)) {
- $result['template_info'] = WebsiteTemplateInfo::insertGetId([
- 'website_id' => $data['website_id'],
- 'page_type' => $page_type,
- 'user_id' => $data['user_id'],
- 'status' => 0,
- 'action_id' => 1,
- ]);
- } else {
- if ($website_template_info['status'] == 2) {
- Db::rollBack();
- return Result::error('网站已应用,不可再次修改!');
- }
- $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type, 'action_id' => 1, 'user_id' => $data['user_id']]);
- }
- if (!$result['template_info']) {
- Db::rollBack();
- return Result::error('网站所选页面添加失败');
- }
- if (empty($website_template)) {
- $result['template'] = WebsiteTemplate::insertGetId([
- 'website_id' => $data['website_id'],
- 'page_type' => $page_type,
- 'user_id' => $data['user_id'],
- ]);
- } else {
- $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])->update([
- 'page_type' => $page_type,
- ]);
- }
- if (!$result['template']) {
- Db::rollBack();
- return Result::error('添加网站预制模版信息失败!');
- }
- Db::commit();
- return Result::success($result);
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error('添加失败');
- }
- }
- /**
- * 自助建站---流程---3.选择皮肤列表
- * @param array $data
- * @return array
- */
- public function getWebsiteTemplateList(array $data): array
- {
- $where = [];
- if (isset($data['template_class_id']) && $data['template_class_id']) {
- $where[] = ['template_class_id', '=', $data['template_class_id']];
- }
- // 0:未构建 1:未应用 2:已应用 status
- $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
- if (empty($templste_info)) {
- return Result::error('请先填写网站基础信息!');
- }
- if ($templste_info['status'] == 2) {
- return Result::error('网站已应用,不可再次修改!');
- }
- if (isset($data['keyword']) && !empty($data['keyword'])) {
- if (is_array($data['keyword'])) {
- // 拼接多个模糊查询条件
- $query = Template::query();
- foreach ($data['keyword'] as $kw) {
- $escapedKw = addcslashes($kw, '%_'); // 转义通配符
- $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
- }
- } else {
- $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
- $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
- }
- } else {
- $query = Template::where($where);
- }
- $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
- // return Result::success($template_id);
- if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
- $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
- } else {
- $template_page = [];
- }
- if (!empty($template_id['template_id'])) {
- $result['template_id'] = $templste_info['template_id'];
- }
- // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
- $template_page_str = json_encode($template_page);
- $result['template'] = $query->where($where)
- ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
- ->orderBy('template.template_id', 'desc')
- ->paginate($data['page_size'], ['*'], 'page', $data['page']);
- if (empty($result['template'])) {
- $result['template'] = Template::orderBy('template.template_id', 'desc')
- ->paginate($data['page_size'], ['*'], 'page', $data['page']);
- }
- $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
- if ($template) {
- $result['template_id'] = $template;
- } else {
- $result['template_id'] = 0;
- }
- return Result::success($result);
- }
- /**
- * 自助建站---流程---4.选择皮肤
- * @param array $data
- * @return array
- */
- public function chooseWebsiteTemplate(array $data): array
- {
- $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
- if (empty($website_template_info)) {
- return Result::error('请先填写网站基础信息!');
- }
- // 0:未构建 1:未应用 2:已应用 status
- if ($website_template_info['status'] == 2) {
- return Result::error('网站已应用,不可再次修改!');
- }
- $template = Template::where('template_id', $data['template_id'])->first();
- if (empty($template)) {
- return Result::error('未查询到皮肤!');
- }
- // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
- // 分别更新 WebsiteTemplateInfo 和 WebsiteTemplate 表
- Db::beginTransaction();
- try {
- $reuslt['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])
- ->update(['template_id' => $data['template_id'], 'action_id' => 2, 'user_id' => $data['user_id']]);
- if (empty($reuslt['template_info'])) {
- Db::rollBack();
- return Result::error('选择皮肤失败!');
- }
- $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])
- ->update(['template_id' => $data['template_id'], 'user_id' => $data['user_id']]);
- if (empty($result['template'])) {
- Db::rollBack();
- return Result::error('网站模版选择皮肤失败!');
- }
- Db::commit();
- return Result::success($result);
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error('选择皮肤失败!');
- }
- }
- /**
- * 自助建站---流程---5.保存模版
- * @param array $data
- * @return array
- */
- public function saveWebsiteTemplate(array $data): array
- {
- // return Result::success($data);
- $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
- if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
- return Result::error('请先填写网站基础信息!');
- }
- // 0:未构建 1:未应用 2:已应用 status
- if ($website_template_info['status'] == 2) {
- return Result::error('网站已应用,不可再次保存!');
- }
- if ($website_template_info['action_id'] != 2) {
- return Result::error('请完成之前的步骤!');
- }
- $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
- // var_dump($data['template_data']);
- try {
- Db::beginTransaction();
- if (empty($template_info)) {
- Db::rollback();
- return Result::error('该网站不存在已保存的模版!');
- } else {
- $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
- [
- 'template_data' => $data['template_data'],
- 'user_id' => $data['user_id'],
- 'canvas_data' => $data['canvas_data'],
- ]
- );
- }
- if (empty($template)) {
- Db::rollback();
- return Result::error('保存失败!');
- } else {
- $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
- if (empty($tempalte_info)) {
- Db::rollback();
- return Result::error('网站搭建状态保存失败!');
- }
- Db::commit();
- }
- } catch (\Exception $e) {
- return Result::error('模版数据格式错误!');
- }
- return Result::success($template);
- }
- /**
- * 自助建站---流程---6.返显保存的模版
- * @param array $data
- * @return array
- */
- public function getWebsiteTemplate(array $data): array
- {
- $web = Website::where('id', $data['website_id'])->first();
- if (empty($web)) {
- return Result::error('未查询到网站信息!');
- }
- $template = WebsiteTemplate::where('website_id', $data['website_id'])
- ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
- if (empty($template)) {
- return Result::error('未查询到模版!');
- }
- $template['page_type'] = json_decode($template['page_type'], true);
- return Result::success($template);
- }
- /**
- * 通栏版式管理-获取通栏版式列表
- */
- public function getSectorPlaceList(array $data): array
- {
- $where['sector_place.type_id'] = $data['type_id'];
- if (isset($data['component_num']) && !empty($data['component_num'])) {
- $where['sector_place.component_num'] = $data['component_num'];
- }
- if (isset($data['sector_type']) && !empty($data['sector_type'])) {
- $where['sector_place.sector_type'] = $data['sector_type'];
- }
- // if(isset($data['component_type']) && !empty($data['component_type'])){
- // $where['sector_place.component_type'] = $data['component_type'];
- // }
- if(isset($data['name']) && !empty($data['name'])){
- array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
- }
- if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
- array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
- array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
- } else if( isset($data['width']) && !empty($data['width'])){
- array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
- }else if( isset($data['height']) && !empty($data['height'])){
- array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
- }else{
- $size_id = [];
- }
- $sector_place = SectorPlace::where($where)
- ->select('sector_place.*');
- $result['count'] = $sector_place->count();
- $result['row'] = $sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
- if($data['type_id'] == 2){
- $result['map_json'] = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->pluck('map');
- }
- if (empty($result['count'])) {
- return Result::error("暂无相关版式数据!");
- }
- return Result::success($result);
- }
- /**
- * 通栏版式管理-添加通栏版式
- */
- public function addSectorPlace(array $data): array
- {
- unset($data['user_id']);
- // 1:通栏;2:组件;
- if ($data['type_id'] == 1) {
- $sector_place = SectorPlace::where('sector_type', $data['sector_type'])->first();
- if (!empty($sector_place)) {
- return Result::error('通栏版式类别编号已存在!');
- }
- $data['sort_id'] = 0;
- } else {
- $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id', 1)->first();
- if (empty($sector_type)) {
- return Result::error('通栏版式类别不存在!');
- }
- $component = SectorPlace::where('type_id', 2)->where('sector_type', $data['sector_type'])->get()->all();
- if(!empty($component)){
- if(count($component) >= $sector_type['component_num']){
- return Result::error('通栏版式组件数量已达上限!');
- }
- 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'))){
- return Result::error('组件版式位置已存在!');
- }
- if(in_array($data['component_type'], array_column($component, 'component_type'))){
- return Result::error('组件版式编号已存在!');
- }
- $data['component_num'] = 0;
- if(in_array($data['type'],[23,24])){
- $data['cat_num'] = 2;
- }
- }
- }
- Db::beginTransaction();
- try{
- $result = SectorPlace::insertGetId($data);
- if (empty($result)) {
- Db::rollBack();
- return Result::error('添加失败!');
- }
- if($data['type_id'] == 2 && count($component)+1 == $sector_type['component_num']){
- $map = $this->changeSectorPlaceJson($sector_type['sector_type']);
- $sector_place_map = SectorPlace::where('sector_type', $sector_type['sector_type'])
- ->where('type_id', 1)->update(['map' => json_encode($map['map_json']),'cat_num'=>$map['cat_num']]);
- if($sector_place_map === false){
- Db::rollBack();
- return Result::error('通栏版式画布数据更新失败!');
- }
- }
- Db::commit();
- return Result::success($result);
- }catch(\Exception $e){
- Db::rollBack();
- return Result::error($e->getMessage());
- }
-
- }
- /**
- * 通栏版式管理-版式画布
- * @param array $data
- * @return array
- */
- public function changeSectorPlaceJson($sector_type)
- {
- $component = SectorPlace::where('type_id', 2)->where('sector_type', $sector_type)->get()->all();
- // $component[count($component)] = $data;
- $sector_type_info = SectorPlace::where('sector_type', $sector_type)->where('type_id', 1)->first();
- if (empty($sector_type_info)) {
- return Result::error('通栏版式类别不存在!');
- }
- $allcomponent_type = array_column($component, 'type');
- if($sector_type_info['column_num'] > 1 && in_array(13,$allcomponent_type)){
- $aboutArtcle_catkey = array_search(13,$allcomponent_type);
- // 确保取到的是数字类型的 width,避免与模型对象相乘
- $width = (int) ($component[$aboutArtcle_catkey]['width'] ?? 1);
- $sector_type_info['width'] = $sector_type_info['width'] + $width;
- }
- $row_num = 1;
- $cat_num = 0;
- $row_width = [];
- $map = [];
- // 按 column_num 分组并取每组最小 width
- $col_width = [];
- foreach ($component as $item) {
- $col = $item['column_num']-1;
- if (!isset($grouped[$col]) || $item['width'] < $grouped[$col]['width']) {
- $column_width[$col] = $item['width'];
- // $column_width[$col][$item['line_num']-1] = $item['line_num'];
- }
- }
- // $grouped 即为分组后各组 width 最小的记录
- // return Result::success($column_width);
- if(count($component) == $sector_type_info['component_num']){
- foreach($component as $key => $val){
- if(in_array($val['type'],[23,24])){
- $cat_num = $cat_num + 2;
- }
- if(in_array($val['type'],[7,14])){
- $cat_num = $cat_num + 1;
- }
- if(!isset($map[$val['column_num'] - 1]['col_data']['row_num'])){
- $row_num = 1;
- $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num;
- }else{
- $map[$val['column_num'] - 1]['col_data']['row_num'] = $row_num +1;
- }
- // 原代码使用 $sector_type['width'] 有误,应该使用 $sector_type_info
- // $width = number_format((($val['width'] / $sector_type_info['width']) * 100 - 1.5), 2);
- $height = number_format((($val['height'] / $sector_type_info['height']) * 100 - 2.5), 2);
- $col_width = $column_width[$val['column_num'] - 1];
-
- $width = number_format((($val['width'] / $col_width) * 100 - 1.5), 2);
- $col_width = number_format((($col_width / $sector_type_info['width']) * 100 - 1.5), 2);
- $row_key = intval($val['line_num'] - 1);
- $map[$val['column_num'] - 1]['col_data']['row_data'][$row_key] = [
- 'row_width' => $width.'%',
- 'row_height' => $height.'%',
- 'component_sort' => $val['sort_id'],
- ];
- $map[$val['column_num'] - 1]['col_data']['col_width'] = $col_width.'%';
-
- }
- }
- // 补全缺失的行,确保row_data是连续的索引数组
- if (!empty($map)) {
- // 为每一列补全缺失的行
- for ($col_index = 0; $col_index < count($map); $col_index++) {
- if (isset($map[$col_index]['col_data']['row_data'])) {
- $row_data = &$map[$col_index]['col_data']['row_data'];
-
- // 检查当前行数据是否是连续的索引数组
- $rows = array_keys($row_data);
- if (empty($rows)) {
- continue;
- }
-
- // 检查是否存在断连
- $min_row = min($rows);
- $max_row = max($rows);
- $is_continuous = (count($rows) === $max_row - $min_row + 1);
-
- // 只有当行号不连续时才进行补全
- if (!$is_continuous || $min_row > 0) {
- $temp_row_data = [];
-
- // 遍历从0到最大行号的所有行
- for ($row_index = 0; $row_index <= $max_row; $row_index++) {
- if (isset($row_data[$row_index])) {
- // 保留原有行数据
- $temp_row_data[$row_index] = $row_data[$row_index];
- } else {
- // 补全新行数据
- $temp_row_data[$row_index] = [
- 'row_width' => '1%', // 固定为1%
- 'row_height' => '30.83%', // 默认高度
- 'component_sort' => null // 补全的行没有component_sort
- ];
-
- // 如果不是第一列,尝试从上一列获取高度
- if ($col_index > 0 && isset($map[$col_index - 1]['col_data']['row_data'][$row_index])) {
- $temp_row_data[$row_index]['row_height'] = $map[$col_index - 1]['col_data']['row_data'][$row_index]['row_height'];
- }
- }
- }
-
- // 更新为连续的索引数组
- $map[$col_index]['col_data']['row_data'] = array_values($temp_row_data);
- // 更新row_num
- $map[$col_index]['col_data']['row_num'] = count($temp_row_data);
- } elseif (!is_array(reset($row_data))) {
- // 如果是关联数组但键是连续的,转换为索引数组
- ksort($row_data);
- $map[$col_index]['col_data']['row_data'] = array_values($row_data);
- }
- }
- }
- }
-
- $map_data = [
- 'map_json'=>$map ?? [],
- 'component'=>count($component),
- 'row_num'=>$row_num ?? 0,
- 'cat_num'=>$cat_num ?? 0,
- 'component_info'=>$component,
- ];
- return $map_data;
- }
- /**
- * 通栏版式管理-修改通栏版式
- */
- public function upSectorPlace($data): array
- {
- unset($data['user_id']);
- // return Result::success($data);
- // $id = $data['id'];
- // 1:通栏;2:组件;
- if ($data['type_id'] == 1) {
- $sector_type = SectorPlace::where('id', $data['id'])
- ->where('type_id',1)
- ->first();
- if (empty($sector_type)) {
- return Result::error('通栏版式不存在!');
- }
- unset($data['sort_id']);
- unset($data['component_type']);
- unset($data['type']);
- // return Result::success($map);
- $sector_place_id = SectorPlace::where('id', '!=', $data['id'])->where('sector_type', $data['sector_type'])->where('type_id', 1)->first();
- if (!empty($sector_place_id)) {
- return Result::error('通栏版式编号已存在!');
- }
- $map = $this->changeSectorPlaceJson($sector_type['sector_type']) ?? [];
- if($data['component_num'] == $map['component']){
- $data['cat_num'] = $map['cat_num'];
- $data['map'] = json_encode($map['map_json']);
- }else{
- $data['cat_num'] = $map['cat_num'];
- $data['map'] = '';
- }
- if($data['sector_type'] != $sector_type['sector_type'] && $map['component'] != 0){
- $com_place = SectorPlace::where('sector_type', $sector_type['sector_type'])
- ->where('type_id', 2)
- ->update(['sector_type'=>$data['sector_type']]);
- if(empty($com_place)){
- return Result::error('组件版式编号同步更新失败!');
- }
- }
- $map = $this->changeSectorPlaceJson($data['sector_type']) ?? [];
- $data['cat_num'] = $map['cat_num'] ?? 0;
- $map = json_encode($map['map_json'] ?? []);
- $result = SectorPlace::where('id', $data['id'])->update($data);
- } else {
- $component_type = SectorPlace::where('id', $data['id'])->where('type_id', 2)->first();
- if (empty($component_type)) {
- return Result::error('组件版式不存在!');
- }
- unset($data['component_num']);
- unset($data['map']);
- $component = SectorPlace::where('sector_type', $data['sector_type'])
- ->where(function($query)use($data){
- $query->where('component_type', $data['component_type'])
- ->Orwhere('sort_id', $data['sort_id']);
- })
- ->where('id', '!=', $data['id'])
- ->where('type_id', 2)
- ->get()->all();
- if (!empty($component)) {
- return Result::error('组件版式编号或位置已存在!');
- }
- $result['component'] = SectorPlace::where('id', $data['id'])->update($data);
- $map = $this->changeSectorPlaceJson($data['sector_type']) ?? [];
- $data['cat_num'] = $map['cat_num'] ?? 0;
- $map = json_encode($map['map_json'] ?? []);
- $result['map'] = SectorPlace::where('sector_type', $data['sector_type'])
- ->where('type_id', 1)
- ->update(['map'=>$map,'cat_num'=>$data['cat_num']]);
- }
-
- if (empty($result)) {
- return Result::error('修改失败!');
- }
- // $update_result['sector_place'] = SectorPlace::where('id',$data['id'])->first();
- // $update_result['map'] = $map;
- return Result::success([
- 'result'=>$result,
- ]);
- }
- /**
- * 通栏版式管理-删除通栏版式
- */
- public function delSectorPlace(array $data): array
- {
- $sector_place = SectorPlace::where('id', $data['id'])->first();
- if (empty($sector_place)) {
- return Result::error('通栏版式不存在!');
- }
- $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
- if (!empty($sector)) {
- return Result::error('通栏版式分类已应用,暂不可删除!');
- }
- Db::beginTransaction();
- try{
- if($sector_place['type_id'] == 1){
-
- $where = ['sector_type'=>$sector_place['sector_type']];
- }else{
- $update_result = SectorPlace::where('sector_type', $sector_place['sector_type'])
- ->where('type_id', 1)
- ->whereNotNull('map')
- ->update(['map' => null]);
- if ($update_result === false) {
- Db::rollBack();
- return Result::error('通栏版式画布数据清空失败!');
- }
- $where = ['id'=>$data['id']];
- }
- $result = SectorPlace::where($where)->delete();
- if (empty($result)) {
- Db::rollBack();
- return Result::error('删除失败!');
- }
- Db::commit();
- return Result::success('删除成功!');
- }catch(\Exception $e){
- Db::rollBack();
- return Result::error($e->getMessage());
- }
-
-
- }
- /**
- * 自助建站-通栏版式管理-获取通栏版式详情
- */
- public function getSectorPlaceInfo(array $data): array
- {
- $result = SectorPlace::where('sector_place.id',$data['id'])
- ->leftJoin('size','sector_place.size_id','=','size.id')
- ->select('sector_place.*','size.width','size.height')
- ->first();
- if(empty($result)){
- return Result::error('通栏版式不存在!');
- }
- return Result::success($result);
- }
- /**
- * 自助建站-通栏版式管理-获取通栏版式列表
- */
- public function getSectorPlaceSort(array $data): array
- {
- $sector = SectorPlace::where('sector_type', $data['sector_type'])->first();
- if (empty($sector)) {
- return Result::error('通栏版式不存在!');
- }
- $component = SectorPlace::where('type_id', 2)->where('sector_type', $sector['sector_type'])->pluck('sort_id')->toArray();
- if (count($component) < $sector['component_num']) {
- // 根据 $sector['component_num'] 的值生成对应元素数量的数组
- if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
- $num = intval($sector['component_num']);
- if ($num > 0 && $num <= 10) {
- // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
- $sector_arr = range(1, $num);
- } else {
- $sector_arr = [];
- }
- } else {
- $sector_arr = [];
- }
- // 获取 $sector_arr 中不在 $component 里的元素
- $sort = array_diff($sector_arr, $component);
- // return Result::success($sort);
- }
- if (!empty($sort)) {
- $sort = array_values(array_map(function ($value) {
- return ['id' => $value];
- }, $sort));
- } else {
- return Result::error("组件版式数量已达上限!");
- }
- return Result::success($sort);
- }
- /**
- * 获取所有通栏版式
- */
- public function getAllSectorPlace(array $data): array
- {
- $where['type_id'] = $data['type_id'];
- // if($data['type_id'] == 1){
- // $where['status'] = 1;
- // }
- $component_num = null;
- if (isset($data['sector_type']) && !empty($data['sector_type'])) {
- $where['sector_type'] = $data['sector_type'];
- }
- if (isset($data['component_num']) && !empty($data['component_num'])) {
- $where['component_num'] = $data['component_num'];
- }
- $sector_place = SectorPlace::where($where)
- ->when($data['type_id'] == 2, function ($query) use ($data) {
- $query->orderBy('sort_id', 'asc');
- })
- ->when(isset($data['component_num']) && !empty($data['component_num']), function ($query) use ($data) {
- // 子查询:统计每个 sector_type 下 type_id=2 的子级数量
- $sub = SectorPlace::selectRaw('sector_type, COUNT(*) as child_count')
- ->where('type_id', 2)
- ->groupBy('sector_type');
- // 关联子查询,并保证子级数量等于传入的 component_num
- $query->leftJoinSub($sub, 'component_place', function ($join) {
- $join->on('sector_place.sector_type', '=', 'component_place.sector_type');
- })->whereRaw('component_place.child_count = ?', [$data['component_num']]);
- })
- ->get()->all();
- if($data['type_id'] == 2){
- foreach($sector_place as $key => $value){
- $sector_place[$key]['component'] = Component::where('type_id',$value['type'])
- ->where('component_width',$value['width'])
- ->where('component_height',$value['height'])
- ->get()->all();
- }
- }
- if(empty($sector_place)){
- return Result::error('通栏版式不存在!');
- }
- return Result::success($sector_place);
- }
- /**
- * 组件管理-获取组件类型
- */
- public function getComponentType(array $data): array
- {
- $where = [];
- if (isset($data['id']) && !empty($data['id'])) {
- $where['id'] = $data['id'];
- }
- $component_type = ComponentType::where($where)->get()->all();
- if (empty($component_type)) {
- return Result::error('组件类型不存在!');
- }
- if (count($component_type) == 1) {
- $all_code = json_decode($component_type[0]['com_code'] ?? '', true);
- $component_type[0]['all_code'] = $all_code['listType'] ?? [];
- $result = $component_type[0];
- } else {
- $result = $component_type;
- }
- if(empty($component_type)){
- return Result::error('组件类型不存在!');
- }
- return Result::success($component_type);
- }
- /**
- * 组件管理-获取所有组件样式
- */
- public function getAllComponent(array $data): array
- {
- $where = [];
- if ((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))) {
- $sector_id['sector.sector_id'] = $data['sector_id'];
- $sector['sector_component.sort_id'] = $data['sort_id'];
- $sectors = Sector::where($sector_id)
- ->where('sector.template_id', $data['template_id'])
- ->leftJoin('sector_component', 'sector.id', 'sector_component.sectorid')
- ->where($sector)
- ->select('sector.template_id', 'sector_component.component_id')
- ->get()->all();
- // return Result::success($sectors);
- $component_id = array_column($sectors, 'component_id');
- $template = ['template_id' => $sectors[0]['template_id']];
- $component = ComponentImg::whereIn('component_id', $component_id)
- ->where($template)
- ->select('img_id', 'img_name', 'img_url', 'template_id')
- ->orderBy('component_img.img_id', 'asc')
- ->get()->all();
- } else {
- if (isset($data['type_id']) && !empty($data['type_id'])) {
- $where['component.type_id'] = $data['type_id'];
- }
- if (isset($data['width']) && !empty($data['width'])) {
- $where['component.component_width'] = $data['width'];
- }
- if (isset($data['height']) && !empty($data['height'])) {
- $where['component.component_height'] = $data['height'];
- }
- $component = Component::where($where)
- ->leftJoin('component_img', 'component.component_id', '=', 'component_img.component_id')
- ->select('component.*', 'component_img.img_id', 'component_img.img_url', 'component_img.img_name')
- // ->orderBy('component_img.template_id')
- ->orderBy('component_img.img_id', 'asc')
- ->get()->all();
- }
- if(empty($component)){
- return Result::error('组件不存在!');
- }
- return Result::success($component);
- }
- /**
- * 自助建站-流程管理-获取所有通栏
- */
- public function getAllSector(array $data): array
- {
- $where = [];
- if (isset($data['template_id']) && !empty($data['template_id'])) {
- $where['sector.template_id'] = $data['template_id'];
- }
- if (isset($data['page_type']) && !empty($data['page_type'])) {
- array_push($where, ['sector.page_type', 'like', '%' . $data['page_type'] . '%']);
- }
- $sector = Sector::where($where)
- ->leftJoin('sector_component', 'sector.id', '=', 'sector_component.sectorid')
- ->leftJoin('component', 'sector_component.component_id', '=', 'component.component_type')
- ->when(isset($data['template_id']), function ($query) use ($data) {
- // 当 type_id 等于 11 时,关联查询 component_img 表并添加 img_url 字段
- return $query->leftJoin('component_img', function ($join) use ($data) {
- $join->on('component.component_type', '=', 'component_img.component_id')
- ->where('component_img.template_id', $data['template_id'])
- ->where('component.type_id', 11);
- });
- })
- ->select(
- 'sector.sector_name',
- 'sector.sector_id',
- 'component.*',
- 'component_img.img_url',
- 'sector.pic_height',
- 'sector.sector_img',
- 'sector_component.sort_id'
- )
- ->orderBy('sector_component.sort_id')
- ->orderBy('sector_component.sector_id')
- ->get()->all();
- if(empty($sector)){
- return Result::error('通栏不存在!');
- }
- $com_num = array_count_values(array_column($sector, 'sector_id'));
- // 使用 array_keys 和 array_diff 函数,不使用循环将值不为 1 的字段键提取到 $sectors_key 中
- $sectors_key = array_keys(array_diff($com_num, [1]));
- // return Result::success($sector);
- $num = 0;
- $array_key = 0;
- foreach ($sector as $key => $value) {
- $sector_id = $value['sector_id'];
- $sectors[$key] = $value['sector_id'];
- // if(isset($comlist_key) && !empty($comlist_key)){
- // $sector_arr_key[$key] = array_keys($comlist_key);
- // }
- if (in_array($sector_id, $sectors_key) && in_array($sector_id, $sectors)) {
- $array_key = array_search($sector_id, $sectors);
- if (isset($sectors) && !in_array($sector_id, $sectors)) {
- $array_key++;
- }
- $num++;
- $array_key++;
- } else {
- $array_key++;
- }
- // var_dump($array_key);
- // var_dump(in_array($sector_id,$sectors_key));
- $comlist_key[$array_key][$sector_id]['sectorName'] = $value['sector_name'];
- $comlist_key[$array_key][$sector_id]['sectorHeight'] = $value['pic_height'];
- $comlist_key[$array_key][$sector_id]['sectorImg'] = $value['sector_img'];
- $componentList['component_style'] = 1;
- $componentList['sort'] = $value['sort_id'];
- // 1:资讯;2:广告;3:静态;
- $component_data = empty($value['component_data']) ? null : (json_decode($value['component_data']) ?? []);
- // 检查 $value['component_column'] 是否为非空字符串,避免传递 null 给 json_decode
- $listType = empty($value['component_column']) ? null : (json_decode($value['component_column']) ?? []);;
- // $page[$key][$setor_id]['componentList'] =
- // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;8:列表类组件;9:详情类组件;
- // 10:二级导航栏类组件;11:广告类;12:静态资源类;13:单页导航类;14:广告资讯混合类;15:滚动图文;16:搜索框类;17:单页列表类;18:单页详情类;
- if (in_array($value['type_id'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 17, 18])) {
- $type = 1;
- $componentList['component_type'] = $type;
- if (in_array($value['type_id'], [7, 15]) && $data['page_type'] == 1) {
- $component_data->componentData->name = '请选择导航..';
- } else if (in_array($value['type_id'], [7, 15]) && $data['page_type'] == 2) {
- $component_data->componentData->name = '自动生成';
- } else if ($data['page_type'] == 3) {
- if ($value['type_id'] == 4) {
- $component_data->componentData->name = '最新资讯';
- }
- if ($value['type_id'] == 6) {
- $component_data->componentData->name = '热点精选';
- }
- } else {
- unset($component_data->componentData->name);
- }
- $componentData = $component_data->componentData;
- $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
- // $page_listType = is_object($listType) ? (array)$listType->listType ?? [] : $listType['listType'] ?? [];
- $componentList['componentData']['listType'] = $listType->listType;
- } else if (in_array($value['type_id'], [10, 12, 13, 16])) {
- $type = 3;
- $componentList['component_type'] = $type;
- $componentList['componentData'] = (object)[];
- } else if ($value['type_id'] == 11) {
- $type = 2;
- $componentList['component_type'] = $type;
- $componentList['componentData'] = (object)[];
- $ad = json_decode($value['ad']) ?? [];
- if (is_object($ad)) {
- $ad->thumb = $value['img_url'];
- } else {
- $ad['thumb'] = $value['img_url'];
- }
- $comlist_key[$array_key][$sector_id]['ad'] = is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
- // 在 $page[$key][$setor_id] 中增加一个键值为 ad 的数组
- // $page[$key][$setor_id]['ad'] = is_object($ad) ? (array)$ad->ad ?? [] : $ad['ad'] ?? [];
- } else if ($value['type_id'] == 14) {
- $type = 1;
- $componentList['component_type'] = $type;
- if ($data['page_type'] == 1) {
- $component_data->componentData->name = '请选择导航..';
- }
- $componentData = $component_data->componentData;
- $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
- $componentList['componentData']['listType'] = $listType->listType;
- $ad = json_decode($value['ad']) ?? [];
- $comlist_key[$array_key][$sector_id]['ad'] = is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
- } else {
- }
- if (in_array($sector_id, $sectors_key)) {
- $comlist_key[$array_key][$sector_id]['componentList'][$num - 1] = $componentList;
- $comlist_key[$array_key][$sector_id]['componentList'] = array_values($comlist_key[$array_key][$sector_id]['componentList']);
- } else {
- $comlist_key[$array_key][$sector_id]['componentList'] = $componentList;
- }
- $keys[$key] = $key;
- }
- // return Result::success($sector_arr_key);
- // $page[$keys] = array_column($page,$sectors_key[0]);
- $comlist_key = array_values($comlist_key);
- // $sector_page = json_encode($comlist_key);
- if (empty($comlist_key)) {
- return Result::error('通栏不存在!');
- }
- return Result::success($comlist_key);
- // return Result::success(gettype($listType));
- }
- /**
- * 自助建站-组件管理-获取组件预览图列表
- */
- public function getComponentImgList(array $data): array
- {
- $where['component_img.component_id'] = $data['component_id'];
- if (isset($data['template_id']) && !empty($data['template_id'])) {
- $where['component_img.template_id'] = $data['template_id'];
- }
- $component_img = ComponentImg::where($where)
- ->leftJoin('template', 'component_img.template_id', '=', 'template.template_id')
- ->leftJoin('component', 'component_img.component_id', '=', 'component.component_type')
- ->select('component_img.*', 'template.template_name', 'component.component_name')
- ->orderBy('component_img.template_id')
- ->orderBy('component_img.img_id')
- ->paginate($data['page_size'], ['*'], 'page', $data['page']);
- if($component_img->isEmpty()){
- return Result::error('组件预览图不存在!');
- }
- $component_img = [
- 'data' => $component_img->items(),
- 'total' => $component_img->total(),
- ];
- return Result::success($component_img);
- }
- /**
- * 自助建站-组件管理-添加组件预览图
- */
- public function addComponentImg(array $data): array
- {
- unset($data['user_id']);
- $component = Component::where('component_type', $data['component_id'])->first();
- if (empty($component)) {
- return Result::error('组件不存在!');
- }
- if ($component['type_id'] == 11) {
- $img = ComponentImg::where('component_id', $data['component_id'])->where('template_id', $data['template_id'])->get()->all();
- if (!empty($img)) {
- return Result::error('该组件已存在此皮肤的预览图!');
- }
- }
- $component = ComponentImg::where('template_id', $data['template_id'])->where('component_id', $data['component_id']);
- if (!empty($component->where('img_id', $data['img_id'])->first())) {
- return Result::error('组件预览图编号已存在!');
- }
- if(!empty($component->where('img_name', $data['img_name'])->first())){
- return Result::error('组件预览图名称已存在!');
- }
- $component_img = ComponentImg::insertGetId($data);
- if (empty($component_img)) {
- return Result::error('添加失败!');
- }
- return Result::success($component_img);
- }
- /**
- * 自助建站-组件管理-删除组件预览图
- */
- public function delComponentImg(array $data): array
- {
- $component_img = ComponentImg::where('id', $data['id'])->delete();
- if (empty($component_img)) {
- return Result::error('删除失败!');
- }
- return Result::success($component_img);
- }
- /**
- * 自助建站-组件管理-更新组件预览图
- */
- public function updateComponentImg(array $data): array
- {
- $component = Component::where('component_type', $data['component_id'])->first();
- if (empty($component)) {
- return Result::error('组件不存在!');
- }
- if ($component['type_id'] == 11) {
- $img = ComponentImg::where('id', '!=', $data['id'])->where('component_id', $data['component_id'])->where('template_id', $data['template_id'])->get()->all();
- if (!empty($img)) {
- return Result::error('该组件已存在此皮肤的预览图!');
- }
- }
- $component = ComponentImg::where('id', '!=', $data['id'])->where('component_id', $data['component_id'])->where('template_id', $data['template_id']);
- if (!empty($component->where('img_id', $data['img_id'])->first())) {
- return Result::error('组件预览图编号已存在!');
- }
- if(!empty($component->where('img_name', $data['img_name'])->first())){
- return Result::error('组件预览图名称已存在!');
- }
- // return Result::success($component);
- unset($data['user_id']);
- // unset($data['updated_at']);
- $id = $data['id'];
- unset($data['id']);
- // return Result::success($data);
- $component_img = ComponentImg::where('id', $id)->update($data);
- if (empty($component_img)) {
- return Result::error('更新失败!');
- }
- return Result::success($component_img);
- }
- /**
- * 自助建站-获取通栏类型
- */
- public function getSectorType(array $data): array
- {
- $sector = SectorType::get()->all();
- if (empty($sector)) {
- return Result::error('通栏类型不存在!');
- }
- return Result::success($sector);
- }
- /**
- * 自助建站-随机获取模板
- */
- public function randomWebTemplate(array $data): array
- {
- $web = Website::where('id', $data['website_id'])->first();
- if (empty($web)) {
- return Result::error('此网站不存在!');
- }
- $template = Template::where('template_id', $data['template_id'])->first();
- if (empty($template)) {
- return Result::error('此皮肤不存在!');
- }
- $rule = TemplateRule::first();
- if (isset($data['page']) && !empty($data['page'])) {
- switch ($data['page']) {
- case 1:
- $index_rule = json_decode($rule['index_rule']);
- break;
- case 2:
- $index_rule = json_decode($rule['class_rule']);
- break;
- case 3:
- $index_rule = json_decode($rule['list_rule']);
- break;
- case 4:
- $index_rule = json_decode($rule['info_rule']);
- break;
- case 5:
- $index_rule = json_decode($rule['search_rule']);
- break;
- case 6:
- $index_rule = json_decode($rule['footerlist_rule']);
- break;
- case 7:
- $index_rule = json_decode($rule['footerinfo_rule']);
- break;
- default:
- break;
- }
- }
- $query = WebsiteCategory::where('website_id', $data['website_id']);
- $cate_num = $query->where('is_show', 1)->count();
- if ($cate_num < 5) {
- return Result::error('此网站栏目过少,无法进行随机模板!');
- }
- // return Result::success($data);
- // $tempalte['index'] = $this->randomWeb($data,$index_rule);
- if(isset($data['page']) && !empty($data['page'])){
- $tempalte['index'] = $this->randomPage($data,$cate_num,$index_rule);
- }else{
- $data['page'] = 1;
- $ruleIndex = json_decode($rule->index_rule);
- $tempalte['index'] = $this->randomPage($data, $cate_num, $ruleIndex);
- $data['page'] = 2;
- $ruleClass = json_decode($rule->class_rule);
- $class_cate_num = $query->where('pid','!=',0)->groupBy('pid')->selectRaw('pid, count(*) as count')->get()->pluck('count','pid');
- $cate_num = max($class_cate_num);
- $tempalte['class'] = $this->randomPage($data, $cate_num, $ruleClass);
- $data['page'] = 3;
- $ruleList = json_decode($rule->list_rule);
- $tempalte['list'] = $this->randomPage($data, $cate_num, $ruleList);
- $data['page'] = 4;
- $ruleInfo = json_decode($rule->info_rule);
- $tempalte['info'] = $this->randomPage($data, $cate_num, $ruleInfo);
- $data['page'] = 5;
- $ruleSearch = json_decode($rule->search_rule);
- $tempalte['search'] = $this->randomPage($data, $cate_num, $ruleSearch);
- $data['page'] = 6;
- $ruleFooterlist = json_decode($rule->footerlist_rule);
- $tempalte['footerlist'] = $this->randomPage($data, $cate_num, $ruleFooterlist);
- $data['page'] = 7;
- $ruleFooterinfo = json_decode($rule->footerinfo_rule);
- $tempalte['footerinfo'] = $this->randomPage($data,$cate_num,$ruleFooterinfo);
- }
- // $index = $this->randomPage($data);
- return Result::success($tempalte);
- }
- public function randomPage($data, $cate_num, $rule)
- {
- // return $rule;
- $page = $data['page'];
- $sectors = Sector::where('sector.template_id', $data['template_id'])
- ->where('sector.page_type', 'like', '%' . $page . '%')
- ->leftJoin('sector_place', function ($join) {
- $join->on('sector.place_type', '=', 'sector_place.sector_type')
- ->where('sector_place.type_id', 1);
- })
- ->whereNotNull('sector_place.sector_type')
- ->select(
- 'sector.sector_name',
- 'sector.sector_id',
- 'sector.pic_height',
- 'sector.sector_img',
- 'sector.sector_type',
- 'sector.sector_code',
- 'sector.id',
- 'sector.sector_width',
- 'sector.component_num',
- 'sector.cat_num',
- )
- ->orderBy('id')
- ->get()->all();
- if (empty($sectors)) {
- return Result::error('通栏不存在!');
- }
- // return $sectors;
- // 获取每个类别的通栏数量
- // $type_num = array_count_values(array_column($sectors, 'sector_type'));
- // 此页面通栏的所有类别
- // $all_sector_types = array_keys($type_num);
- //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏
- $all_sector_type = array_column($sectors, 'sector_type');
- $grouped = array_reduce(
- array_keys($sectors),
- function ($carry, $idx) use ($sectors) {
- $carry[$sectors[$idx]['sector_type']][] = $idx;
- return $carry;
- },
- []
- );
- // 对 all_sector_type 去重后,每个类型随机挑一个 sector 索引
- $unipue_sector_keys = array_combine(
- array_keys($grouped),
- array_map(
- function ($type) use ($grouped) {
- // 随机取一个索引
- return $grouped[$type][array_rand($grouped[$type])];
- },
- array_keys($grouped)
- )
- );
- // 1. 必需通栏:同一类型只随机保留一条
- $rule_must_type = $rule->must_type;
- $sector_num = $rule->max_num;
- $unique_sector_type = array_flip($unipue_sector_keys);
- $must_type = array_unique(array_intersect($unique_sector_type,$rule_must_type));
- // 先求交集,去重后统计
- $is_must = count($must_type);
- if ($is_must != count(array_unique($must_type))) {
- return '此页面缺少必要通栏!';
- }
- // 先按类型把 sectors 分组,key 为 sector_type,value 为对应 sector 数组
-
- // return [
- // 'unipue_sector_keys'=>$unipue_sector_keys,
- // 'grouped'=>$grouped,
- // ];
- // 最终保留的 must_type 数组(去重且随机保留一个)
- // return [
- // 'must_key'=>$must_key,'must_type'=>$must_type,
- // 'must_sector_keys'=>$must_sector_keys
- // ];
- // $must_sector_keys = array_intersect_key($unipue_sector_keys,$must_type);
- $must_sector = array_values(array_intersect_key($sectors,$must_type));
- $must_count = count($must_sector);
- // return $sector_comnum;
- // $must_sector = array_column($must_sector,'sector_type');
- // 目前按照首页必需通栏的数量进行处理,若是存在必需通栏的数量大于查询到的通栏数量,则返回必需通栏的数量
- if ($must_count > $sector_num) {
- return $must_sector;
- }
- // 必需通栏中的栏目数量
- $must_cat_num = array_sum(array_column($must_sector,'cat_num'));
- if($must_cat_num>$cate_num){
- return '栏目数量过少,无法完成必需通栏随机!';
- }
- // var_dump(['must_cat_num'=>$must_cat_num,'cate_num'=>$cate_num]);
- // 2.非必须通栏的随机处理
- $not_must_type = array_unique(array_diff($unique_sector_type, $must_type));
- $total = count($not_must_type);
- $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
- $and_type = $rule->and_type ?? [];
- // return $not_must_type;
- if ($randomCount === 0) {
- $random_sector = [];
- $rand_count = 0;
- } else {
- // 随机获取指定数量的键名(保留原键)
- $randomKeys = array_rand($not_must_type, $randomCount);
- // 处理单元素情况(array_rand返回字符串,需转为数组)
- if (!is_array($randomKeys)) {
- $randomKeys = [$randomKeys];
- }
- // 通栏关联性规则暂时无法实现---------------即若是随机了头条通栏必须要有轮播图通栏(皆为非必须通栏)
- // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
- // 判断 $oneDimensionalArray 是否为一维数组
- // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) {
- // 判断是否存在有关联通栏的交集
- // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) {
- // // var_dump("11111",$and_type);
- // // var_dump("22222",$randomKeys);
- // // 将一维数组中的值都放到随机数组中
- // $randomKeys = array_merge($randomKeys, $and_type);
- // }
- // var_dump(count(array_intersect($and_type, $randomKeys)));
- // }
- // -----------------------------
- // 根据随机键名提取元素(保留原键值关系)
- // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
- // var_dump("randomKeys",$randomKeys);
- $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
- $rand_count = count($random_sector);
- }
- $random_cat_num = array_sum(array_column($random_sector,'cat_num'));
- $random_cat_num += $must_cat_num;
- if($random_cat_num>$cate_num){
- return '栏目数量过少,无法完成随机!';
- }
- // var_dump("randomKeys",$randomKeys);
- // var_dump("random_sector",$random_sector);
- // var_dump("rand_count", $rand_count);
- // var_dump("random_cat_num", $random_cat_num);
- // return $random_sector;
- // var_dump("must_sector",$must_sector);
- // var_dump("must_count",$must_count);
- // return ['random_sector'=>$random_sector,'random_cat_num'=>$random_cat_num,'cate_num'=>$cate_num];
- // 3.计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
- $repeat_num = $sector_num - $rand_count - $must_count;
- // 组合起来通栏
- $sector_zuhe1 = array_merge($must_sector, $random_sector);
- $sector_zuhe1_type = array_column($sector_zuhe1, 'sector_type');
- $sector_ids = array_column($sector_zuhe1, 'id');
- if ($repeat_num > 0) {
- $rule_repeat = (array)$rule->type_max;
- $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1, 'sector_type'));
- // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
- foreach ($rule_repeat as $key => $value) {
- if (in_array($key, array_keys($sector_zuhe1_type))) {
- $rule_repeat[$key] = $value - 1;
- }
- }
- // var_dump("repeat_num", $repeat_num);
- // 剩余通栏进行重复处理
- $rule_repeat = (array)$rule->type_max;
- // 取到可以重复的通栏类别
- $rule_repaet_type = array_keys($rule_repeat);
- $repeat_type = array_intersect($rule_repaet_type, $unique_sector_type);
- // 获取可以随机重复的通栏
- $repaet_key = 0;
- $num = 0;
- $type = [];
- // return $rule_repaet_type;
- // 防止无限循环,设置最大迭代次数
- $max_iterations = 10;
- $iteration_count = 0;
- // 剩余的栏目数量
- $sector_cate_comnum = $cate_num - $random_cat_num;
- if($sector_cate_comnum < 0){
- return '栏目数量过少,无法完成随机!';
- }
- // var_dump("sector_comnum", $sector_cate_comnum);
- $com_num = 0;
- // return [
- // 'repeat_type'=>$repeat_type,
- // 'repeat_num'=>$repeat_num,
- // 'sector_comnum'=>$sector_cate_comnum,
- // 'rule_repeat'=>$rule_repeat,
- // 'unique_sector_type'=>$unique_sector_type,
- // 'all_sector_type'=>$all_sector_type,
- // ];
- $repaet_cat_num = 0;
- $sector_key = 0;
- $repeat_onenum = [];
- $repeat_type_sectornum = array_count_values( $all_sector_type);
- $repeat_type_sectorkey = array_keys($repeat_type_sectornum);
- // 随机重复通栏-----
- while ($repaet_key <= $repeat_num-1 && $iteration_count < $max_iterations) {
- if ($sector_cate_comnum - $repaet_cat_num > 0) {
- foreach ($repeat_type as $key => $value) {
- if(!isset($repeat_onenum[$value])){
- $repeat_onenum[$value] = 0;
- }
- $sector_key = $repaet_key;
- // 确保 $value 是数组且不为空,再使用 $value[0] 或 array_rand
- if(in_array($value,$all_sector_type) && in_array($value,$repeat_type_sectorkey) ){
- // $sector_key = $key+$num;
- // 可重复通栏,每一个可重复种类的通栏随机取1
- if($repeat_type_sectornum[$value] > 1){
- // 找出 all_sector_type 中值为 1 的键
- $keys_equal_one = array_keys($all_sector_type,$value);
- // 随机取一个键
- $random_key = $keys_equal_one[array_rand($keys_equal_one)];
- $repeat_sector_key[$sector_key] = $random_key;
- }else{
- $repeat_sector_key[$sector_key] = array_keys($all_sector_type,$value)[0];
- }
- $sector_cat_key = $repeat_sector_key[$sector_key];
- $repaet_cat_num += $sectors[$sector_cat_key]['cat_num'];
- // var_dump(['--------------------' => $sector_cate_comnum - $repaet_cat_num <= 0,
- // '************************' => $repaet_cat_num]);
- if($sector_cate_comnum - $repaet_cat_num <= 0){
- unset($repeat_sector_key[$sector_key]);
- // $repaet_key--;
- // $sector_key = $sector_key-$repaet_key;
- $repaet_cat_num = $repaet_cat_num-$sectors[$sector_cat_key]['cat_num'];
- // var_dump(['33333333333333333' => $sector_key]);
- continue;
- }else{
- $repeat_onenum[$value]++;
-
- $repaet_key++;
- }
- // var_dump(['22222222222222222' => $sector_key]);
-
- }else{
- break;
- }
- // if (is_array($value) && count($value) > 0) {
- // if (in_array($value, $repeat_type_sectorkey) && in_array($value, $repeat_type_sectorkey) &&
- // isset($rule_repeat[$value]) && $repeat_onenum[$value] <= $rule_repeat[$value]) {
- // $repeat_onenum[$value] = $key;
- // }
-
- // }
- }
- } else {
- break;
- }
- $num++;
- $iteration_count++;
- }
- // $value = 1;
- // return [
- // 'repeat_num'=>$repeat_num,
- // 'sector_cate_comnum'=>$sector_cate_comnum,
- // 'repaet_cat_num'=>$repaet_cat_num,
- // 'repeat_onenum'=>$repeat_onenum,
- // 'repeat_sector_key'=>$repeat_sector_key,
- // // 'sector_cate_comnum'=>$sector_cate_comnum,
- // 'sector_cat_key'=>$sector_cat_key,
- // // 'all_sector_type'=>$all_sector_type,
- // // 'sectors'=>$sectors,
- // ];
- // var_dump("num", $num);
- // var_dump("repeat_sector_key", $repeat_sector_key);
- // 随机的重复通栏键值转换成真正的通栏
- $repeat_sector = [];
- if (isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)) {
- foreach ($repeat_sector_key as $key => $value) {
- if (isset($sectors[$value])) {
- $repeat_sector[$key] = $sectors[$value];
- }
- }
- }
- $repeat_count = count($repeat_sector);
- var_dump("repeat_count", $repeat_count);
- // return [
- // 'sector_comnum'=>$sector_comnum,
- // 'com_num'=>$com_num,
- // 'repeat_count'=>$repeat_count,
- // 'repeat_sector'=>$repeat_sector,
- // ];
- // // var_dump("repeat_sector", $repeat_sector);
- // return $repeat_sector;
-
- $sector = array_merge($repeat_sector,$sector_zuhe1);
- }else{
- $sector = $sector_zuhe1;
- }
- // var_dump("sector_ids",$sector_ids);
- //
- // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
- // var_dump("sector",$sector);
- // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
- $sector_specal_sort = array_column($sector,'sector_type');
- if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
-
- $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
- $pic_key = array_search(7,$sector_specal_sort) ?? null;
- $cat_key = array_search(5,$sector_specal_sort) ?? null;
- $toutiao = $sector[$toutiao_key];
- $pic = $sector[$pic_key];
- $cat = $sector[$cat_key];
- $sort_1 = $sector[0];
- $sort_2 = $sector[1];
- $sort_3 = $sector[2];
-
- // return $sector_specal_sort;
- // return [
- // 'sector_ids' => array_column($sector_zuhe1,'id'),
- // // 'repeat_sector' => $repeat_sector,
- // 'sector_id' => array_column($sector,'id'),
- // 'array_intersect' => count(array_intersect($sector,$must_sector)),
- // 'old' => [ $toutiao_key,$pic_key,$cat_key],
- // 'new' => [ $sort_1,$sort_2,$sort_3]
- // ];
- if($data['page'] == 1 || $data['page'] == '1'){
- if($toutiao_key){
- $sector[0] = $toutiao;
- $sector[$toutiao_key] = $sort_1;
- $sector[1] = $pic;
- $sector[$pic_key] = $sort_2;
- }
- if($cat_key){
- $sector[0] = $pic;
- $sector[$pic_key] = $sort_1;
- }
- }
- if ($data['page'] == '2' || $data['page'] == 2 || in_array(5, $sector_specal_sort)) {
- $sector[0] = $cat;
- $sector[$cat_key] = $sort_1;
- if ($toutiao_key && $pic_key) {
- $sector[1] = $toutiao;
- $sector[$toutiao_key] = $sort_2;
- $sector[2] = $pic;
- $sector[$pic_key] = $sort_3;
- }
- if ($pic_key) {
- $sector[1] = $pic;
- $sector[$pic_key] = $sort_2;
- }
- }
- }
- $sector_id = array_column($sector,'id');
-
- // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){
- // return '此页面缺少必要通栏!';
- // }
- // return Result::success([
- // 'sector_id'=>$sector_id,
- // 'sector'=>$sector,
- // 'cate_num'=>$cate_num,
- // ]);
- // $data['page'] = $page;
- $component[] = $this->randomComponent($sector_id, $data, $sector);
- var_dump("sector_id", $sector_id);
- return [
- // 'sector_id'=>$sector_id,
- // 'sectors'=>$sectors,
- // 'sector_comnum'=>$sector_comnum,
- // 'com_num'=>$com_num,
- // 'cate_num'=>$cate_num,
- // '$sector_zuhe1_comnum'=>$sector_zuhe1_comnum+$com_num,
- 'component' => $component,
- // 'sector'=>$sector,
- ];
- }
- public function randomComponent($sector_id, $data, $sector)
- {
- $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
- ->leftJoin('component', 'component.component_type', 'sector_component.component_id')
- ->leftJoin('component_img', 'component_img.component_id', 'component.component_type')
- // ->leftJoin('sector', 'sector.id', 'sector_component.sectorid')
- ->where('component_img.template_id', '=', $data['template_id'])
- ->select(
- 'component.*',
- 'component_img.img_name',
- 'component_img.img_id',
- 'component_img.img_url',
- 'sector_component.sort_id',
- 'sector_component.sectorid',
- // 'sector.sector_type',
- )
- ->orderBy('sector_component.sort_id')
- ->orderBy('component.component_type')
- ->get();
- // 按sectorid和sort_id分组数据
- $groupedResults = [];
- $sort_ids = [];
- $sector_ids = [];
- $component_list = [];
- $component_ids = [];
- $img_ids = [];
- $sort_key = 0;
- // return $rawResults;
- // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式)
- // $sort_component = array_column($rawResults,'sort_id');
- foreach ($rawResults as $key => $item) {
- $sectorId = $item->sectorid;
- $sortId = $item->sort_id;
- $sector_type = $item->sector_type;
- $componentId = $item->component_type;
- $component_data = $item->component_data;
- $component_column = $item->component_column;
- $component_type = $item->type_id;
- $component_ad = $item->ad;
- $imgId = $item->img_id;
- if (!isset($groupedResults[$sectorId])) {
- $groupedResults[$sectorId] = [];
- }
- $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
- $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
- $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
- $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
- // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
- if (!isset($componentData['componentData']) || !is_array($componentData['componentData'])) {
- $componentData['componentData'] = [];
- }
- $componentData['componentData'] += $component_listtype;
- $component_code = $componentData;
- $component_list[$key] = $component_code;
- // $component_list[$key]['listType'] = $component_listtype;
- // }
- if(in_array($component_type,[19,20,21,22,23,24])){
- $sortId = $sortId*2-1;
- $sort_id = intval($sortId-1);
- }else{
- $sort_id = intval($sortId - 1);
- }
- // if(!isset($groupedResults[$sectorId][$sort_id])) {
- // // $groupedResults[$sectorId][$sortId] = [];
- // $com_key = 0;
- // $groupedResults[$sectorId][$sort_id][$com_key] = [
- // 'component_type' => $item->component_type,
- // 'type_id' => $component_type,
- // 'sort' => $sortId,
- // 'width' => $item->component_width,
- // 'height' => $item->component_height,
- // 'images' => [
- // 'img_name' => $item->img_name,
- // 'img_id' => $item->img_id,
- // 'img_url' => $item->img_url,
- // 'component_type' => $component_type,
- // ]
- // ];
- // // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
- // }else{
- // if((isset($component_ids) && in_array($componentId,$component_ids))){
- // if(isset($img_arr)){
- // $img_arr++;
- // }else{
- // $img_arr = 0;
- // if(isset($com_key)){
- // $com_key++;
- // }
- // }
- // // $com_id_key = array_search($componentId,$component_ids);
- // $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [
- // 'img_name' => $item->img_name,
- // 'img_id' => $item->img_id,
- // 'img_url' => $item->img_url,
- // 'component_type' => $component_type,
- // ];
- // }else{
- // $img_arr = 0;
- // $com_key = $com_key ?? 0;
- // $groupedResults[$sectorId][$sort_id][$com_key] = [
- // 'component_type' => $item->component_type,
- // 'type_id' => $component_type,
- // 'sort' => $sortId,
- // 'width' => $item->component_width,
- // 'height' => $item->component_height,
- // 'images' =>
- // [
- // $img_arr =>
- // [
- // 'img_name' => $item->img_name,
- // 'img_id' => $item->img_id,
- // 'img_url' => $item->img_url,
- // 'component_type' => $component_type,
- // ]
- // ]
- // ];
- // }
- // }
- // $component = $groupedResults[$sectorId][$sort_id];
- // if(count($component) > 1){
- // $com_key = 0;
- // }
- // if(!in_array($sectorId,$sector_ids)){
- // $sort_key = 0;
- // $sector_ids[$sectorId][$sort_key] = $sectorId;
- // }else{
- // $sort_key++;
- // $sector_ids[$sectorId][$sort_key] = $sectorId;
- // }
- if (!isset($com_key) || !in_array($sectorId, $sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])) {
- $com_key = 0;
- // if(!in_array($componentId,$component_ids)){
- // $img_key = 0;
- // }
- $img_key = 0;
- } else if (in_array($sortId, $sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId, $component_ids)) {
- $com_key++;
- }
- if (in_array($componentId, $component_ids) && !in_array($imgId, $img_ids[$componentId])) {
- $img_key++;
- } else {
- $img_key = 0;
- }
- if (!in_array($componentId, $component_ids)) {
- $groupedResults[$sectorId][$sort_id][$com_key] = [
- 'component_type' => $item->component_type,
- 'type_id' => $component_type,
- 'sort' => $sortId,
- 'width' => $item->component_width,
- 'height' => $item->component_height,
- ];
- if(in_array($component_type,[19,20,21,22,23,24])){
- $groupedResults[$sectorId][$sort_id+1][$com_key] = [
- 'component_type' => $item->component_type,
- 'type_id' => $component_type,
- 'sort' => $sortId+1,
- 'width' => $item->component_width,
- 'height' => $item->component_height,
- ];
- }
- }
- $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_key] = [
- 'img_name' => $item->img_name,
- 'img_id' => $imgId,
- 'img_url' => $item->img_url,
- 'component_type' => $componentId,
- ];
- if(in_array($component_type,[19,20,21,22,23,24])){
- $groupedResults[$sectorId][$sort_id+1][$com_key]['images'][$img_key] = [
- 'img_name' => $item->img_name,
- 'img_id' => $imgId,
- 'img_url' => $item->img_url,
- 'component_type' => $componentId,
- ];
- }
- // $groupedResults[$sectorId]['sector_type'] = $sector_type;
- // $component_type[$key] = $groupedResults[$sectorId][$sort_id]['component_type'];
- // $sort_com = $groupedResults[$sectorId][$sort_id]['component_type'];
- // if(count($sort_com) == 1){
- // $com_key = 0;
- // }
- // // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
- // // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
- if (is_scalar($sectorId) && is_scalar($sort_id)) {
- if (!in_array($component_type, [10, 12, 16])) {
- if ($component_type == 11 || $component_type == 14 || $component_type == 24) {
- $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad;
- $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id'];
- }
- if ($component_type != 11) {
- $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
- if(in_array($component_type,[19,20,21,22,23,24])){
- $groupedResults[(int)$sectorId][(int)$sort_id+1][$com_key]['componentData'] = $component_code['componentData'];
- }
- }
- } else {
- $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = [];
- if(in_array($component_type,[19,20,21,22,23,24])){
- $groupedResults[(int)$sectorId][(int)$sort_id+1][$com_key]['componentData'] = [];
- }
- }
- } else {
- // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
- trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
- }
- // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
- $component_ids[$key] = $componentId;
- if (!isset($sort_ids[$sectorId])) {
- $sort_ids[$sectorId] = [];
- }
- if (!in_array($sortId, $sort_ids[$sectorId])) {
- $sort_ids[$sectorId][] = $sortId;
- }
- if (!isset($img_ids[$componentId])) {
- $img_ids[$componentId] = [];
- }
- if (!in_array($imgId, $img_ids[$componentId])) {
- $img_ids[$componentId][] = $imgId;
- }
- $sector_ids[$key] = $sectorId;
- $sort_ids[$sectorId][$sort_id] = $sort_id;
- // $img_ids[$componentId] = $imgId;
- }
- // return $sort_ids;
- $result = [];
- // return $groupedResults;
- $y_num = 0;
- foreach ($sector as $key => $value) {
- // array_push($value['sort_id'],$key);
- // $sector_id_ = $value['sector_id'];
- $sectorid = $value['id'];
- $template_data[$key] = [
- 'sectorName' => $value['sector_id'],
- 'sort' => $key + 1,
- ];
- $date = date('mdHisu'); // 获取时分秒微秒,转换为纯数字格式
- // $date = 11010630021;
- if (substr($date, 0, 1) == 0) {
- $date = substr($date, 0, 10); // 截取前9位
- } else {
- $date = substr($date, 0, 9); // 截取前9位
- }
- $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
- $y_num = $y_num + $value['pic_height'];
- $sector_width = $value['sector_width'] / 100;
- $canvas_data[$key] = [
- 'i' => $random_num,
- 'x' => 0,
- 'y' => $y_num,
- 'w' => $sector_width,
- 'h' => $value['pic_height'],
- 'type' => $value['sector_id'],
- 'content' => [
- 'sectorName' => $value['sector_id'],
- ]
- ];
- // $sector_key[$key] = array_keys($value );
- $sector_component = [];
- if (isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])) {
- $componet_key = 0;
- foreach ($groupedResults[$sectorid] as $sortId => $components) {
- // 通栏某一位置的随机组件
- if (count($components) > 1) {
- $componentIds = count($components);
- $selectedComponentId = rand(0, $componentIds - 1);
- } else {
- $selectedComponentId = 0;
- }
- // var_dump($components[$selectedComponentId]);
- // $Component = $components[$selectedComponentId];
- $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0;
- $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1;
- $Component['type_id'] = $components[$selectedComponentId]['type_id'] ?? 0;
- // 通栏某一位置的随机组件样式图
- // 检查 $Component['images'] 是否存在且为数组
- if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) {
- $images = count($components[$selectedComponentId]['images']);
- $selectedImage = 0;
- if ($images > 1) {
- $selectedImage = rand(0, $images - 1);
- }
- if (isset($components[$selectedComponentId]['images'][$selectedImage])) {
- $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id'];
- }
- }
- $image_info[$key] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage] : $components[$selectedComponentId]['images'][$selectedImage];
- // var_dump($images);
- $Component['componentData'] = $components[$selectedComponentId]['componentData'] ?? [];
- if ($data['page'] == 1 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'], [7, 14, 15]))) {
- $Component['componentData']['name'] = '请选择栏目';
- }
- if ($data['page'] == 2 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'], [7, 14, 15]))) {
- $Component['componentData']['name'] = '自动生成';
- }
- if (isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 11) {
- $ad = $components[$selectedComponentId]['ad'];
- // $ad['thumb'] = $image_info[$key]['img_url'];
- $template_data[$key]['ad'] = $ad;
- $canvas_adinfo[$key] = $ad;
- $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
- // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合
- // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
- $Component['componentData'] = [];
- }
- if (isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 14) {
- $ad = $components[$selectedComponentId]['ad'];
- $ad['thumb'] = $image_info[$key]['img_url'];
- $template_data[$key]['ad'] = $ad;
- $canvas_adinfo[$key] = $ad;
- $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
- // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
- // // $Component['componentData'] = [];
- }
- // var_dump('---------------------------',$components[$selectedComponentId]);
- $sector_component[$componet_key] = $Component;
- $componet_key++;
- }
- // var_dump("image",$image);
- }
- $template_data[$key]['componentList'] = $sector_component;
- $canvas_data[$key]['content']['componentList'] = $sector_component;
- // $canvas_data[$key]['ad'] = $key;
- $canvas_data[$key]['dataSort'] = $key;
- $canvas_data[$key]['moved'] = false;
- // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
- }
- // return $canvas_data;
- return [
- 'template_data' => $template_data,
- 'canvas_data' => $canvas_data,
- ];
- }
- // public function randomWeb($data,$index_rule)
- // {
- // // 在使用 with 方法关联查询其他表时限制查询字段,不需要修改 Sector 的 model 文件,
- // // 可以直接在查询时通过 select 方法指定要查询的字段。以下是修改后的代码:
- // $component_img = Sector::select('sector.id', 'sector.template_id', 'sector.page_type','sector.sector_id','sector.component_num'
- // ,'sector.sector_name','sector.sector_type')
- // ->where('sector.template_id', $data['template_id'])
- // ->where('sector.page_type', 'like', '%' . $data['page'] . '%')
- // ->with([
- // 'sectorComponents' => function ($query) use ($data) {
- // $query->select('sectorid', 'sort_id', 'component_id')
- // ->with([
- // 'component' => function ($q) use ($data) {
- // $q->select('id', 'component_type', 'component_data', 'component_code', 'component_column', 'ad','type_id')
- // ->with([
- // 'componentImgs' => function ($subQ) use ($data) {
- // $subQ
- // ->where('template_id', $data['template_id'])
- // // ->select('id', 'img_id', 'img_name', 'img_url', 'template_id')
- // ;
- // }
- // ]);
- // }
- // ]);
- // }
- // ])
- // ->get()
- // ->all();
- // // 通栏类别:1:资讯类:2:广告类;3:混合类;4:搜索框类;5:导航类;6:头条类;
- // // 7:轮播图类;8:静态资源类;9:单页(列表)类;10:单页(详情)类;11:列表类;12:详情类;13:栏目轮播图类;',
- // // {"max_num": 10, "min_num": 6, "and_type": [6, 7],
- // // "type_max": {"1": 20, "2": 4, "3": 3}, "must_type": [1, 2]}
- // $rule = is_array($index_rule) ? $index_rule : (array)$index_rule;
- // // return gettype($rule);
- // $must_num = count($rule['must_type']);
- // $sector_type = array_column($component_img,'sector_type');
- // $notmust_sectortype = count(array_diff(array_unique(array_column($component_img,'sector_type')),$rule['must_type']));
- // $notmust_num = random_int(0,$notmust_sectortype);
- // $min_num = $notmust_sectortype+$must_num;
- // $max_num = $rule['max_num'];
- // $index_num = random_int($min_num, $max_num);
- // $repeat_num = $index_num - $must_num - $notmust_num;
- // $category_num = WebsiteCategory::where('website_id',$data['website_id'])
- // ->where('is_show',1)
- // ->get()
- // ->all();
- // $num = 0;
- // $sector_ids = [];
- // $sector_sortids = [];
- // foreach($components as $key => $value){
- // if(!in_array($value['sectorid'],$sector_ids)){
- // $num = 0;
- // }
- // if($value['type_id'] == 7 && !in_array($value['sort_id'],$sector_sortids[$value['sectorid']])){
- // $num++;
- // }
- // $sector_typenum[$value['sectorid']]['num'] = $num;
- // $sector_typenum[$value['sectorid']][$value['sort_id']] = $num;
- // $sector_ids[$key] = $value['sectorid'];
- // $sector_sortids[$value['sectorid']][$key] = $value['sort_id'];
- // // }
- // foreach($sector_type as $key => $value){
- // if($value == 1){
- // $sector_type[$key] = 1;
- // }
- // }
- // return [
- // 'index_num' => $index_num,
- // 'min_num' => $min_num,
- // '$sector_type' => $sector_type,
- // 'notmust_sectortype' => $notmust_sectortype,
- // 'must_num' => $must_num,
- // 'notmust_num' => $notmust_num,
- // 'repeat_num' => $repeat_num,
- // // '$sector_typenum' => $sector_typenum,
- // ];
- // return $component_img;
- // }
- //level
- public function addLevel(array $data)
- {
- var_dump($data, 'request data');
- $level = new Level();
- $level->fill($data);
- $level->save();
- return Result::success($level->toArray());
- }
- public function updateLevel(array $data)
- {
- $level = Level::find($data['id']);
- $level->fill($data);
- $level->save();
- return Result::success($level->toArray());
- }
- public function deleteLevel(array $data)
- {
- $level = Level::find($data['id']);
- $level->delete();
- return Result::success();
- }
- public function getLevelInfo(array $data)
- {
- $level = Level::find($data['id']);
- return Result::success($level);
- }
- public function getLevelList(array $data)
- {
- $where = [];
- if (isset($data['name'])) {
- $where[] = ['name', 'like', '%' . $data['name'] . '%'];
- }
- $level = Level::where($where)
- ->orderBy('updated_at', 'desc')
- ->paginate(($data['page_size'] ?? 10) * ($data['page'] ?? 1), ['*'], 'page', $data['page'] ?? 1);
- return Result::success([
- 'data' => $level->items(),
- 'total' => $level->total(),
- ]);
- }
- public function getLevel(array $data)
- {
- $where = [];
- if (isset($data['pid'])) {
- $where = [
- 'pid' => $data['pid'] ?? 0
- ];
- }
- $result = level::when(!empty($where), function ($query) use ($where) {
- $query->where($where);
- })->orderBy("sort", "desc")->get();
- var_dump($result);
- if (empty($result)) {
- return Result::error("查询失败", 0);
- } else {
- return Result::success($result);
- }
- }
- /**
- * 自助建站-添加样式代码
- */
- public function addStyleCode(array $data): array
- {
- unset($data['status']);
- $style_code = StyleCode::insertGetId($data);
- if (empty($style_code)) {
- return Result::error('添加失败!');
- }
- return Result::success($style_code);
- }
- /**
- * 自助建站-获取样式代码
- */
- public function getStyleCode(array $data): array
- {
- unset($data['status']);
- $style_code = StyleCode::get()->all();
- if (empty($style_code)) {
- return Result::error('样式代码不存在!');
- }
- return Result::success($style_code);
- }
- /**
- * 自助建站-获取所有通栏、组件、组件样式
- */
- public function getAllSectorComponentStyle(array $data): array
- {
- $sector_component_style = Sector::where('sector.template_id',$data['template_id'])
- ->leftJoin('sector_component','sector.id','sector_component.sectorid')
- ->leftJoin('component', 'component.component_type', 'sector_component.component_id')
- ->leftJoin('component_img', 'component_img.component_id', 'component.component_type')
- // ->leftJoin('sector', 'sector.id', 'sector_component.sectorid')
- ->where('component_img.template_id', '=', $data['template_id'])
- ->select(
- 'sector.sector_id',
- 'sector.sector_keyword',
- 'sector.component_num',
- 'sector.sector_width',
- 'sector.sector_height',
- 'sector.pic_height',
- 'component.component_type',
- 'component.component_keyword',
- 'component.component_data',
- 'component.component_column',
- 'component.ad',
- 'component.type_id',
- 'component_img.img_name',
- 'component_img.img_id',
- 'component_img.img_url',
- 'sector_component.sort_id',
- 'sector_component.sectorid',
- )
- ->orderBy('sector_component.sort_id')
- ->orderBy('component.component_type')
- ->get()->all();
- $groupedResults = [];
- $sort_ids = [];
- $sector_ids = [];
- $component_list = [];
- $component_ids = [];
- $img_ids = [];
- // return Result::success($sector_component_style);
- foreach ($sector_component_style as $key => $item) {
- $sectorId = $item->sectorid;
- $sortId = $item->sort_id;
- $sector_type = $item->sector_type;
- $component_num = $item->component_num;
- $componentId = $item->component_type;
- $component_data = $item->component_data;
- $component_column = $item->component_column;
- $component_keyword = $item->component_keyword;
- $component_type = $item->type_id;
- $component_ad = $item->ad;
- $imgId = $item->img_id;
- $sector_height = $item->sector_height;
- $pic_height = $item->pic_height;
- $sectorName = $item->sector_id;
- if (!isset($groupedResults[$sectorId])) {
- $groupedResults[$sectorId] = [];
- }
- $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
- $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
- $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
- $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
- // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
- if (!isset($componentData['componentData']) || !is_array($componentData['componentData'])) {
- $componentData['componentData'] = [];
- }
- $componentData['componentData'] += $component_listtype;
- $component_code = $componentData;
- $component_list[$key] = $component_code;
- // $component_list[$key]['listType'] = $component_listtype;
- // }
- if(!in_array($sectorId,$sector_ids)){
- $groupedResults[$sectorId] = [
- 'sector_id' => $sectorName,
- 'sector_keyword' => $item->sector_keyword,
- 'component_num' => $component_num,
- 'sector_width' => $item->sector_width,
- 'sector_height' => $sector_height,
- 'pic_height' => $pic_height,
- ];
- }
- $sort_id = intval($sortId - 1);
- if (!isset($com_key) || !in_array($sectorId, $sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])) {
- $com_key = 0;
- $img_key = 0;
- } else if (in_array($sortId, $sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId, $component_ids)) {
- $com_key++;
- }
- if (in_array($componentId, $component_ids) && !in_array($imgId, $img_ids[$componentId])) {
- $img_key++;
- } else {
- $img_key = 0;
- }
- if (!in_array($componentId, $component_ids)) {
- $groupedResults[$sectorId]['component_list'][$sort_id][$com_key] = [
- 'component_type' => $item->component_type,
- 'type_id' => $component_type,
- 'sort' => $sortId,
- ];
- }
- if($component_type == 11){
- $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [
- 'img_name' => $item->img_name,
- 'img_id' => $imgId,
- 'img_url' => $item->img_url,
- ];
- }else{
- $groupedResults[$sectorId]['component_list'][$sort_id][$com_key]['component_style_data'][$img_key] = [
- 'img_name' => $item->img_name,
- 'img_id' => $imgId,
- ];
- }
- $com_key = 0;
- // }
- // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
- // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
- // 19:选项卡-推荐图类;20:选项卡-最新资讯类;21:选项卡-推荐资讯类;22:选项卡-热点资讯类;23:选项卡-栏目资讯类;24:选项卡-栏目广告混合类;
- if (is_scalar($sectorId) && is_scalar($sort_id)) {
- if (!in_array($component_type, [10, 12, 16])) {
- if ($component_type == 11 || $component_type == 14 || $component_type == 24) {
- $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad'] = $component_ad;
- $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id'];
- }
- if ($component_type != 11) {
- $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
- }
- } else {
- $groupedResults[(int)$sectorId]['component_list'][(int)$sort_id][$com_key]['componentData'] = [];
- }
- } else {
- // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
- trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
- }
- // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
- $component_ids[$key] = $componentId;
- if (!isset($sort_ids[$sectorId])) {
- $sort_ids[$sectorId] = [];
- }
- if (!in_array($sortId, $sort_ids[$sectorId])) {
- $sort_ids[$sectorId][] = $sortId;
- }
- if (!isset($img_ids[$componentId])) {
- $img_ids[$componentId] = [];
- }
- if (!in_array($imgId, $img_ids[$componentId])) {
- $img_ids[$componentId][] = $imgId;
- }
- $sector_ids[$key] = $sectorId;
- $sort_ids[$sectorId][$sort_id] = $sort_id;
- // $img_ids[$componentId] = $imgId;
- }
- // return Result::success($groupedResults);
- if (empty($groupedResults)) {
- return Result::error('通栏组件样式不存在!');
- }
- $groupedResults = array_values($groupedResults);
- return Result::success($groupedResults);
- }
- /**
- * 自助建站-ai会话-获取会话列表
- */
- public function getAiSessionList(array $data): array
- {
- $where['user_id'] = $data['user_id'];
- if(isset($data['website_id']) && !empty($data['website_id'])){
- $where['website_id'] = $data['website_id'];
- }
- if(isset($data['template_id']) && !empty($data['template_id'])){
- $where['template_id'] = $data['template_id'];
- }
- $result = AichatSession::when(!empty($where),function($query)use($where){
- $query->where($where);
- })->get()->all();
- if(empty($result)){
- return Result::error('会话不存在!');
- }
- return Result::success($result);
- }
- /**
- * 自助建站-ai会话-创建会话
- */
- public function addAiSession(array $data): array
- {
- $website = Website::where('id',$data['website_id'])->first();
- if(empty($website)){
- return Result::error('网站不存在!');
- }
- // $template = Template::where('template_id',$data['template_id'])->first();
- // if(empty($template)){
- // return Result::error('皮肤不存在!');
- // }
- $date = date('Hisms');
- // $date = 1631191019
- $rand_num = rand(1000,9999);
- $data['session_id'] = $date.$rand_num;
- // $data['session_id'] = 16320610069512
- $ai_sessions = AichatSession::where('user_id',$data['user_id'])->pluck('session_id')->all();
- if(!empty($data['session_id']) && in_array($data['session_id'],$ai_sessions)){
- $rand_num = rand(1000,9999);
- $data['session_id'] = $date.$rand_num;
- }
- $result = AichatSession::insert($data);
- if(empty($result)){
- return Result::error('创建会话失败!');
- }
- return Result::success($data['session_id']);
- }
- /**
- * 自助建站-ai会话-修改会话
- */
- public function upAiSession(array $data): array
- {
- $where['user_id'] = $data['user_id'];
- $where['session_id'] = $data['session_id'];
- // return Result::success($where);
- $aichat_session = AichatSession::where($where)->first();
- if(empty($aichat_session)){
- return Result::error('会话不存在!');
- }
- unset($data['session_id']);
- // 会话状态:1-开启,0-关闭
- // if(isset($data['is_active']) && $aichat_session['is_active'] == $data['is_active']){
- // return Result::success('会话状态已为'.$data['is_active'].'!');
- // }
- // if(isset($data['is_active']) && ($data['is_active'] == 0 || $data['is_active'] == '0')){
- // $data['end_time'] = date('Y-m-d H:i:s');
- // }
- $result = AichatSession::where($where)->update($data);
- if(empty($result)){
- return Result::error('修改会话失败!');
- }
- return Result::success($result);
- }
- /**
- * 自助建站-ai会话-删除会话
- */
- public function delAiSession(array $data): array
- {
- $where['user_id'] = $data['user_id'];
- $where['session_id'] = $data['session_id'];
- // 删除会话
- Db::beginTransaction();
- try{
- $aichat_session = AichatSession::where($where)->get()->all();
- if(!empty($aichat_session)){
- $result['ai_chat_session'] = AichatSession::where($where)->delete();
- if(empty($result['ai_chat_session'])){
- Db::rollBack();
- return Result::error('删除会话消息失败!');
- }
- }
- // 删除会话消息
- $aichat = Aichat::where($where)->get()->all();
- if(!empty($aichat)){
- $result['ai_chat_message'] = Aichat::where($where)->delete();
- if(empty($result['ai_chat_message'])){
- Db::rollBack();
- return Result::error('删除会话消息失败!');
- }
- }
- // 删除草稿模板
- $template_draftbox = TemplateDraftbox::where($where)->get()->all();
- if(!empty($template_draftbox)){
- $result['template_draftbox'] = TemplateDraftbox::where($where)->delete();
- if(empty($result['template_draftbox'])){
- Db::rollBack();
- return Result::error('删除草稿模板失败!');
- }
- }
- Db::commit();
- return Result::success($result);
- }catch(\Exception $e){
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- }
- /**
- * 自助建站-ai会话-发送消息
- */
- public function sendAiMessage(array $data): array
- {
- $where['user_id'] = $data['user_id'];
- $where['session_id'] = $data['session_id'];
- // $where['is_active'] = 1;
- $aichat_session = AichatSession::where($where)->first();
- if(empty($aichat_session)){
- return Result::error('会话不存在!');
- }
- Db::beginTransaction();
- try{
- $data['message_type'] = empty($data['message_type']) ? 'text' : $data['message_type'];
- $result['aichat'] = Aichat::insertGetId($data);
- if(empty($result)){
- Db::rollBack();
- return Result::error('发送消息失败!');
- }
- $result['aichat_session'] = AichatSession::where($where)->update(['end_time'=>date('Y-m-d H:i:s')]);
- if(empty($result['aichat_session'])){
- Db::rollBack();
- return Result::error('更新会话截止时间失败!');
- }
- Db::commit();
- return Result::success($result);
- }catch(\Exception $e){
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- }
- /**
- * 自助建站-ai会话-获取消息列表
- */
- public function getAiMessageList(array $data): array
- {
- // $where['user_id'] = $data['user_id'];
- $where['session_id'] = $data['session_id'];
- // $where['is_active'] = 1;
- $session = AichatSession::where($where)->first();
- if(empty($session)){
- return Result::error('会话不存在!');
- }
- $aichat = Aichat::where('user_id',$data['user_id'])
- ->where('session_id',$data['session_id'])
- ->orderBy('send_time','desc')
- ->get()->all();
- if(empty($aichat)){
- return Result::error('消息不存在!');
- }
- return Result::success($aichat);
- }
- /**
- * 自助建站-ai会话-添加模板草稿
- */
- public function addTemplateDraftbox(array $data): array
- {
- $where['session_id'] = $data['session_id'];
- // $where['user_id'] = $data['user_id'];
- // 后续若是用来对应会话记录,则此项需要修改
- $session = AichatSession::where($where)->first();
- if(empty($session)){
- return Result::error('会话不存在!');
- }
- $data['website_id'] = $session['website_id'];
- // $data['template_id'] = $session['template_id'];
- $template_draftbox = TemplateDraftbox::where($where)->first();
- if(!empty($template_draftbox)){
- $result = TemplateDraftbox::where($where)->update(['template_data'=>$data['template_data'],'canvas_data'=>$data['canvas_data'],'template_id'=>$data['template_id']]);
- }else{
- $result = TemplateDraftbox::insert($data);
- }
- if(empty($result)){
- return Result::error('添加草稿模板失败!');
- }
- return Result::success($result);
- }
- /**
- * 自助建站-ai会话-修改模板草稿
- */
- public function upTemplateDraftbox(array $data): array
- {
- $where['session_id'] = $data['session_id'];
- $where['user_id'] = $data['user_id'];
- $session = AichatSession::where($where)->first();
- if(empty($session)){
- return Result::error('会话不存在!');
- }
- if(isset($data['chat_id']) && !empty($data['chat_id'])){
- $chat = Aichat::where($where)->where('chat_id',$data['chat_id'])->first();
- if(empty($chat)){
- return Result::error('消息不存在!');
- }
- }
- // 后续若是用来对应会话记录,则此项需要修改
- $template_draftbox = TemplateDraftbox::where($where)->first();
- if(empty($template_draftbox)){
- return Result::error('草稿模板不存在!');
- }
- unset($data['session_id']);
- $result = TemplateDraftbox::where($where)->update($data);
- if(empty($result)){
- return Result::error('修改草稿模板失败!');
- }
- return Result::success($result);
- }
- /**
- * 自助建站-ai会话-获取模板草稿列表
- */
- public function getTemplateDraftboxList(array $data): array
- {
- $where['session_id'] = $data['session_id'];
- // $where['user_id'] = $data['user_id'];
- if(isset($data['website_id']) && !empty($data['website_id'])){
- $where['website_id'] = $data['website_id'];
- }
- if(isset($data['template_id']) && !empty($data['template_id'])){
- $where['template_id'] = $data['template_id'];
- }
- if(isset($data['status'])){
- $where['status'] = $data['status'];
- }
- if(isset($data['chat_id']) && !empty($data['chat_id'])){
- $where['chat_id'] = $data['chat_id'];
- }
- $template_draftbox = TemplateDraftbox::where($where)->first();
- if(empty($template_draftbox)){
- return Result::error('草稿模板不存在!');
- }
- return Result::success($template_draftbox);
- }
- /**
- * 自助建站-验证关键词填写重复问题
- */
- public function checkRepeatKeyword(array $data): array
- {
- if(empty($data['keyword'])){
- return Result::error('关键词不能为空!');
- }
- $keyword = json_decode($data['keyword'],true);
- $keyword = array_map('trim', $keyword);
- // $keyword_list = array_column($keyword,'keyword');
- if(count($keyword) != count(array_unique($keyword))){
- return Result::error('关键词不能重复!');
- }
- // return Result::success(json_encode($keyword,JSON_UNESCAPED_UNICODE));
- $keyword = json_encode($keyword, JSON_UNESCAPED_UNICODE);
-
- if($data['type'] == 'style'){
- $template_class_keyword = TemplateClass::pluck('keyword')->toArray();
- $keyword = preg_replace('/,/', ', ', $keyword);
- if(in_array($keyword,$template_class_keyword)){
- return Result::error('此风格关键词已存在!');
- }
- }
- if($data['type'] == 'skin'){
- $template_keyword = Template::pluck('template_keyword')->toArray();
- $keyword = preg_replace('/,/', ', ', $keyword);
- if(in_array($keyword,$template_keyword)){
- return Result::error('此皮肤关键词已存在!');
- }
- return Result::success(['关键词'=>$keyword,'type'=>$template_keyword]);
- }
- if($data['type'] == 'sector'){
- $sector_keyword = Sector::where('template_id',$data['template_id'])->pluck('sector_keyword')->toArray();
- if(in_array($keyword,$sector_keyword)){
- return Result::error('此通栏关键词已存在!');
- }
- }
- if($data['type'] == 'component'){
- $component_keyword = Component::pluck('component_keyword')->toArray();
- if(in_array($keyword,$component_keyword)){
- return Result::error('此组件关键词已存在!');
- }
- }
-
-
- return Result::success(['验证通过']);
- }
- /**
- * 自助建站-验证网站模板相关类型
- * 版本1----在构建网站首页之后使用
- */
- public function checkWebTemplate(array $data): array
- {
- // 网站id (是否存在该网站)
- // (若是只传这个字段会判断是否关联导航池;)
- // (再判断是否有三种类型的友情链接)
- // (头部信息是否填写)
- // (两种类型的底部导航)
- $website = Website::where('id',$data['website_id'])->first();
- if(empty($website)){
- return Result::error('网站不存在!');
- }
- $template_data = json_decode($data['template_data'],true);
- if(empty($template_data)){
- return Result::error('模板数据不能为空!');
- }
- return Result::success($template_data);
- if(!isset($template_data['website_id']) || empty($template_data['website_id'])){
- return Result::error('网站ID不能为空!');
- }
- if(!isset($template_data['template_id']) || empty($template_data['template_id'])){
- return Result::error('模板ID不能为空!');
- }
- return Result::success($template_data);
- }
- public function getAITemplate(array $data): array
- {
- $result['template'] = Template::select('id','template_id','template_name','template_keyword','page_type',
- 'template_class_id','template_img','updated_at')
- ->get()->all();
- if (empty($result)) {
- return Result::error("暂无皮肤", 0);
- }
- // $num = 1;
- // 确保 $result 是数组且包含数据
- if (!is_array($result) || empty($result)) {
- return Result::error('模板数据异常');
- }
- // 初始化最新更新时间
- $latestUpdate = null;
- foreach ($result['template'] as $key => $value) {
- $templateImg = json_decode($value['template_img'], true);
- $result['template'][$key]['template_img'] = $templateImg;
- // 更新最新时间:取最大的 updated_at
- if ($latestUpdate === null || $value['updated_at'] > $latestUpdate) {
- $latestUpdate = $value['updated_at'];
- }
- // unset($result[$key]['updated_at']);
- }
- // 将最新时间放入返回结果,若无数据则返回当前时间
- $result['update_time'] = date('Y-m-d H:i:s', strtotime($latestUpdate));
- return Result::success($result);
- }
- /**
- * 自助建站-ai会话-删除聊天记录
- */
- public function delAiChatRecord(array $data): array
- {
- $session_id = $data['session_id'];
- $user_id = $data['user_id'];
- $chat = AiChat::where('session_id',$session_id)->where('user_id',$user_id)->orderBy('send_time','desc')->first();
- if(empty($chat)){
- return Result::error('此用户的聊天记录不存在!');
- }
- $del_chat = AiChat::where('chat_id',$chat['chat_id'])->where('user_id',$user_id)->delete();
- if(empty($del_chat)){
- return Result::error('删除失败!');
- }
- return Result::success('删除成功'.$chat['chat_id']);
- }
- /**
- * 自助建站-ai会话-给模板添加导航
- */
- public function addTemplateCates(array $data): array
- {
- $website_template = TemplateDraftbox::where('session_id',$data['session_id'])->first();
- if(empty($website_template)){
- return Result::error('此网站模板不存在!');
- }
- // return Result::success($website_template);
- $template_data = json_decode($website_template['template_data'],true);
- $template_index = $template_data['template']['index'];
- if(empty($template_index)){
- return Result::error('模板数据不能为空!');
- }
- $canvas_data = json_decode($website_template['canvas_data'],true);
- $canvas_index = $canvas_data['template']['index'];
- if(empty($canvas_index)){
- return Result::error('画布数据不能为空!');
- }
- $cate_column = [
- 'placeid' => 1,
- 'pid' => 0,
- 'num' => 23,
- 'website_id' => $website_template['website_id'],
- 'is_show' => 1,
- ];
- $websiteServer = new \App\JsonRpc\WebsiteService();
- $all_cate_data = $websiteServer->getWebsiteModelCategory($cate_column);
- $all_cate = $all_cate_data['data'] ?? [];
- // return Result::success($all_cate);
- if (count($all_cate) == 0) {
- return Result::error('栏目不存在!');
- }
- // 将 Collection 转为数组
- $all_cate = $all_cate->toArray();
- $del_catid = 0;
- foreach ($all_cate as $k => $v) {
- $cat_arr_id = json_decode($v['category_arr_id'],true) ?? [];
- if (!empty($v['web_url']) || $v['type'] != 1 || $v['pid'] == $del_catid) {
- $del_catid = $v['category_id'] ?? '';
- unset($all_cate[$k]);
- }
- }
- $all_cate = array_values($all_cate);
- // return Result::success($all_cate);
- $componentList = array_column($template_index,'componentList');
- $componentData = [];
- $cat_key = 0;
- foreach ($componentList as $key => $item) {
- $componentList[$key] = $item ?? [];
- if(count($item) == 1 && isset($item[0]['componentData']['level']) && (empty($v['componentData']['level']) || $v['componentData']['level'] == "")){
- // if(isset($item[0]) && !empty($item[0])){
- $componentList[$key][0]['componentData']['name'] = $all_cate[$cat_key]['alias'] ?? '';
- $componentList[$key][0]['componentData']['category_arr'] = json_decode($all_cate[$cat_key]['category_arr_id'],true) ?? '';
- $componentList[$key][0]['componentData']['category_id'] = $all_cate[$cat_key]['category_id'] ?? '';
- $cat_key++;
- $template_index[$key]['componentList'] = $componentList[$key] ?? [];
- $canvas_index[$key]['content']['componentList'] = $componentList[$key] ?? [];
- // }
- }else{
- foreach ($item as $k => $v) {
- $componentList[$key][$k] = $v ?? [];
- if(isset($v['componentData']['level']) && (empty($v['componentData']['level']) || $v['componentData']['level'] == "")){
- $componentList[$key][$k]['componentData']['name'] = $all_cate[$cat_key]['alias'] ?? '';
- $componentList[$key][$k]['componentData']['category_arr'] = json_decode($all_cate[$cat_key]['category_arr_id'],true) ?? '';
- $componentList[$key][$k]['componentData']['category_id'] = $all_cate[$cat_key]['category_id'] ?? '';
- $cat_key++;
- $template_index[$key]['componentList'][$k] = $componentList[$key][$k] ?? [];
- $canvas_index[$key]['content']['componentList'][$k] = $componentList[$key][$k] ?? [];
- }
- }
- }
- }
- // 从 componentList[0] 中取出 componentData 字段
- $template_data['template']['index'] = $template_index;
- $canvas_data['template']['index'] = $canvas_index;
- // $componentData = $componentList[0] ?? [];
- $result = TemplateDraftbox::where('session_id',$data['session_id'])->update([
- 'template_data'=>json_encode($template_data,JSON_UNESCAPED_UNICODE),
- 'canvas_data'=>json_encode($canvas_data,JSON_UNESCAPED_UNICODE),
- ]);
- if(empty($result)){
- return Result::error('更新失败!');
- }
- return Result::success($result);
- }
- }
|