PublicRpcService.php 164 KB

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