PublicRpcService.php 163 KB

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