PublicRpcService.php 193 KB

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