PublicRpcService.php 181 KB

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