PublicRpcService.php 147 KB

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