PublicRpcService.php 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546
  1. <?php
  2. namespace App\JsonRpc;
  3. use _PHPStan_62c6a0a8b\OndraM\CiDetector\Env;
  4. use App\Model\BlackWord;
  5. use App\Model\Department;
  6. use App\Model\District;
  7. use App\Model\LetterOfComplaint;
  8. use App\Model\LetterType;
  9. use App\Model\LevelUser;
  10. use App\Model\UserLevel;
  11. use App\Tools\Result;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\Di\Annotation\Inject;
  14. use Hyperf\RpcServer\Annotation\RpcService;
  15. use App\Service\MinioService;
  16. use Hyperf\Redis\Redis;
  17. use Overtrue\ChineseCalendar\Calendar;
  18. use App\Model\TemplateClass;
  19. use App\Model\Template;
  20. use App\Model\WebsiteTemplate;
  21. use App\Model\WebsiteTemplateInfo;
  22. use App\Model\Sector;
  23. use App\Model\Component;
  24. use App\Model\Link;
  25. use App\Model\FooterCategory;
  26. use App\Model\Size;
  27. use function Hyperf\Support\retry;
  28. use Hyperf\Paginator\Paginator;
  29. use App\Model\User;
  30. use App\Model\Website;
  31. use App\Constants\ErrorCode;
  32. use GuzzleHttp\Client;
  33. use GuzzleHttp\Exception\GuzzleException;
  34. use App\Model\SectorPlace;
  35. use App\Model\ComponentType;
  36. use App\Model\Article;
  37. use App\Model\WebsiteCategory;
  38. use App\Model\AdPlace;
  39. use App\Model\Company;
  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.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) && $component_img->count() > 0){
  875. return Result::error('此皮肤已绑定组件预览图,不可删除!'.$component_img);
  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'])->where('template_id',$data['template_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. $sector = 0;
  1150. // $flattened = [];
  1151. // $sector = [];
  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' => $subArray[$k],
  1159. 'sort_id' => $key + 1,
  1160. 'place_id' => $data['place_type'],
  1161. // 'sectorid' => $data['id'],
  1162. ];
  1163. // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1164. // // $componentTypes[] = $item;
  1165. // // $components[] = [
  1166. // // 'sort_id' => $key + 1,
  1167. // // 'component_type' => $item,
  1168. // // ];
  1169. // // $place_ids[] = $data['place_type'];
  1170. }
  1171. }
  1172. // $component_num = count($component);
  1173. // $count = count(array_unique($component));
  1174. // 使用array_map批量替换sector_id
  1175. // return Result::success($sector_component);
  1176. if(count($sort) != $data['component_num']){
  1177. return Result::error('组件关联错误!');
  1178. }
  1179. // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
  1180. $sector_code = [
  1181. $data['sector_id'] => [
  1182. 'sectorName' => $data['sector_name'],
  1183. 'sectorHeight' => $data['pic_height'],
  1184. 'sectorImg' => $data['sector_img'],
  1185. ]
  1186. ];
  1187. // return Result::success($sector_code);
  1188. $data['sector_code'] = json_encode($sector_code);
  1189. Db::beginTransaction();
  1190. try {
  1191. $sector = Sector::insertGetId($data);
  1192. if (empty($sector)) {
  1193. Db::rollBack();
  1194. return Result::error('添加失败');
  1195. }
  1196. $sector_com = array_map(function ($item) use ($sector) {
  1197. // 替换当前记录的sector_id为变量值(保留其他字段不变)
  1198. $item['sectorid'] = $sector;
  1199. return $item;
  1200. }, $sector_component);
  1201. // Db::rollBack();
  1202. // return Result::success($sector_com);
  1203. $com_sector = SectorComponent::insert($sector_com);
  1204. if(empty($com_sector)){
  1205. Db::rollBack();
  1206. return Result::error('通栏关联组件失败!');
  1207. }
  1208. // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1209. // 查询数据库中已存在的component_type(只保留存在的记录)
  1210. // $existingTypes = Component::whereIn('component_type', $componentTypes)
  1211. // ->pluck('component_type') // 提取存在的component_type
  1212. // ->toArray();
  1213. // if(empty($existingTypes)){
  1214. // Db::rollBack();
  1215. // return Result::error('组件不存在!');
  1216. // }
  1217. // // var_dump($existingTypes);
  1218. // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增)
  1219. // $updateData = array_filter($components, function ($item) use ($existingTypes) {
  1220. // return in_array($item['component_type'], $existingTypes);
  1221. // });
  1222. // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增)
  1223. // if (!empty($updateData)) {
  1224. // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']);
  1225. // }
  1226. // if(empty($component_sort)){
  1227. // Db::rollBack();
  1228. // return Result::error('组件位置关联失败!');
  1229. // }
  1230. Db::commit();
  1231. return Result::success($sector);
  1232. } catch (\Exception $e) {
  1233. Db::rollBack();
  1234. return Result::error($e->getMessage());
  1235. }
  1236. }
  1237. /**
  1238. * 删除通栏
  1239. * @param array $data
  1240. * @return array
  1241. */
  1242. public function delSector(array $data): array
  1243. {
  1244. $sector = Sector::where('id', $data['id'])->first();
  1245. if (empty($sector)) {
  1246. return Result::error('通栏不存在!');
  1247. }
  1248. Db::beginTransaction();
  1249. try {
  1250. $component_id = SectorComponent::where('sectorid', $data['id'])->delete();
  1251. if (empty($component_id)) {
  1252. Db::rollBack();
  1253. return Result::error('解除相关组件关联关系失败!');
  1254. }
  1255. $result = Sector::where('id', $data['id'])->delete();
  1256. if (empty($result)) {
  1257. Db::rollBack();
  1258. return Result::error('通栏删除失败!');
  1259. }
  1260. Db::commit();
  1261. return Result::success('通栏删除成功!');
  1262. } catch (\Exception $e) {
  1263. Db::rollBack();
  1264. return Result::error($e->getMessage());
  1265. }
  1266. }
  1267. /**
  1268. * 修改通栏
  1269. * @param array $data
  1270. * @return array
  1271. */
  1272. public function updateSector(array $data): array
  1273. {
  1274. unset($data['user_id']);
  1275. $sector = Sector::where('id', $data['id'])->first();
  1276. if (empty($sector)) {
  1277. return Result::error('通栏不存在!');
  1278. }
  1279. if($sector['sector_id'] != $data['sector_id']){
  1280. $sector_id = Sector::where('sector_id',$data['sector_id'])->where('template_id',$data['template_id'])->first();
  1281. if(!empty($sector_id)){
  1282. return Result::error('通栏编号已存在!');
  1283. }
  1284. }
  1285. $sector_code = [
  1286. $data['sector_id'] => [
  1287. 'sectorName' => $data['sector_name'],
  1288. 'sectorHeight' => $data['pic_height'],
  1289. 'sectorImg' => $data['sector_img'],
  1290. ]
  1291. ];
  1292. // return Result::success($sector_code);
  1293. $data['sector_code'] = json_encode($sector_code);
  1294. // 对传入的 page_type 数组进行去重、转换为整数并重新索引
  1295. $data['page_type'] = array_values(array_unique(array_map('intval', $data['page_type'])));
  1296. $data['page_type'] = json_encode($data['page_type']);
  1297. $template = Template::where('template_id', $data['template_id'])
  1298. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$data['page_type']])
  1299. ->first();
  1300. if (empty($template)) {
  1301. return Result::error('皮肤不存在!');
  1302. }
  1303. $sort = json_decode($data['component_code'],true);
  1304. $sector_component = [];
  1305. foreach ($sort as $key => $subArray) {
  1306. foreach ($subArray as $k => $item) {
  1307. // // $component[] = $item;
  1308. $sector_component[] = [
  1309. 'sector_id' => $data['sector_id'],
  1310. 'component_id' => $subArray[$k],
  1311. 'sort_id' => $key + 1,
  1312. 'place_id' => $data['place_type'],
  1313. 'sectorid' => $data['id'],
  1314. ];
  1315. // // // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1316. // // $componentTypes[] = $item;
  1317. // // $components[] = [
  1318. // // 'sort_id' => $key + 1,
  1319. // // 'component_type' => $item,
  1320. // // ];
  1321. // // $place_ids[] = $data['place_type'];
  1322. }
  1323. }
  1324. if(count($sort) != $data['component_num']){
  1325. return Result::error('组件关联错误!');
  1326. }
  1327. $data['sector_code'] = json_encode($sector_code);
  1328. Db::beginTransaction();
  1329. try {
  1330. $com_sector = SectorComponent::where('sectorid', $data['id'])->pluck('component_id');
  1331. if(empty($com_sector)){
  1332. Db::rollBack();
  1333. return Result::error('通栏解除组件关联失败!');
  1334. }
  1335. $del_SectorComponent = SectorComponent::where('sectorid', $data['id'])->delete();
  1336. if(empty($del_SectorComponent)){
  1337. Db::rollBack();
  1338. return Result::error('通栏解除组件关联失败!1');
  1339. }
  1340. $sector = Sector::where('id', $data['id'])->update($data);
  1341. if (empty($sector)) {
  1342. Db::rollBack();
  1343. return Result::error('修改失败');
  1344. }
  1345. $sector_com = array_map(function ($item) use ($data) {
  1346. // 替换当前记录的sector_id为变量值(保留其他字段不变)
  1347. $item['sectorid'] = $data['id'];
  1348. return $item;
  1349. }, $sector_component);
  1350. // var_dump($sector_com);
  1351. $com_sector = SectorComponent::insert($sector_com);
  1352. if(empty($com_sector)){
  1353. Db::rollBack();
  1354. return Result::error('通栏关联组件失败!');
  1355. }
  1356. // ---------同步通栏的sort_id到组件之中,目前已废弃,涉及到批量修改------
  1357. // $up_component = Component::whereIn('component_id',$sector_component['component_type'])
  1358. // ->update(['status' => 2,]);
  1359. // if(empty($up_component)){
  1360. // Db::rollBack();
  1361. // return Result::error('通栏关联组件失败!');
  1362. // }
  1363. // 2. 查询数据库中已存在的component_type(只保留存在的记录)
  1364. // $existingTypes = Component::whereIn('component_type', $componentTypes)
  1365. // ->pluck('component_type') // 提取存在的component_type
  1366. // ->toArray();
  1367. // // 3. 过滤$components数组,只保留数据库中已存在的记录(核心:避免新增)
  1368. // $updateData = array_filter($components, function ($item) use ($existingTypes) {
  1369. // return in_array($item['component_type'], $existingTypes);
  1370. // });
  1371. // // 4. 执行upsert(此时$updateData中只有已存在的记录,不会新增)
  1372. // if (!empty($updateData)) {
  1373. // $component_sort = Component::upsert($updateData,['component_type'],['sort_id']);
  1374. // }
  1375. // if(empty($component_sort)){
  1376. // Db::rollBack();
  1377. // return Result::error('组件位置关联失败!');
  1378. // }
  1379. Db::commit();
  1380. return Result::success($sector);
  1381. } catch (\Exception $e) {
  1382. Db::rollBack();
  1383. return Result::error($e->getMessage());
  1384. }
  1385. }
  1386. public function getComponentList(array $data): array
  1387. {
  1388. $where = [];
  1389. if (isset($data['component_name']) && !empty($data['component_name'])) {
  1390. array_push($where, ['component.component_name', 'like', '%' . $data['component_name'] . '%']);
  1391. }
  1392. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])) {
  1393. array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
  1394. array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
  1395. } else if (isset($data['width']) && !empty($data['width'])) {
  1396. array_push($where, ['component.component_width', 'like', '%' . $data['width'] . '%']);
  1397. } else if (isset($data['height']) && !empty($data['height'])) {
  1398. array_push($where, ['component.component_height', 'like', '%' . $data['height'] . '%']);
  1399. } else {
  1400. }
  1401. if(isset($data['type_id']) && !empty($data['type_id'])){
  1402. $where['component.type_id'] = $data['type_id'];
  1403. }
  1404. $result = Component::where($where)
  1405. ->leftJoin('component_type', 'component_type.id', '=', 'component.type_id')
  1406. ->leftJoin('component_img', 'component.component_type', '=', 'component_img.component_id')
  1407. ->select(
  1408. 'component.*',
  1409. 'component_type.com_typename',
  1410. DB::raw('COUNT(component_img.id) as img_count')
  1411. )
  1412. ->groupBy('component.id') // 按组件ID分组,确保统计每个组件的图片数量
  1413. ->orderBy('component.updated_at', 'desc')
  1414. ->paginate($data['page_size'], ['*'], 'mypage_name', $data['page']);
  1415. if(empty($result)){
  1416. return Result::error('暂无数据');
  1417. }
  1418. return Result::success($result);
  1419. }
  1420. public function getComponentInfo(array $data): array
  1421. {
  1422. $where = ['component.id'=>$data['id']];
  1423. $result = Component::where($where)
  1424. ->leftJoin('component_type','component_type.id','=','component.type_id')
  1425. ->select( 'component.*','component_type.com_typename')
  1426. ->get();
  1427. if(empty($result)){
  1428. return Result::error('暂无数据');
  1429. }
  1430. return Result::success($result);
  1431. }
  1432. /*
  1433. 新增组件
  1434. */
  1435. public function addComponent(array $data): array
  1436. {
  1437. unset($data['user_id']);
  1438. $component_id = Component::where('component_type',$data['component_type'])->first();
  1439. if(!empty($component_id)){
  1440. return Result::error('组件编号已存在!');
  1441. }
  1442. $component_type = ComponentType::where('id',$data['type_id'])->first();
  1443. if(empty($component_type)){
  1444. return Result::error('组件分类不存在!');
  1445. }
  1446. // '组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;
  1447. // 8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;14:广告资讯混合类
  1448. $data['type_id'] = intval($data['type_id']);
  1449. $add_arr = [
  1450. // 'template_id' => intval($data['template_id']),
  1451. 'component_type' => intval($data['component_type']),
  1452. 'component_name' => $data['component_name'],
  1453. 'component_img' => $data['component_img'],
  1454. 'component_width' => $data['component_width'],
  1455. 'component_height' => $data['component_height'],
  1456. 'type_id' => intval($data['type_id']),
  1457. 'component_keyword' => $data['component_keyword'],
  1458. ];
  1459. $component_head_code = [
  1460. 'component_id' => $add_arr['component_type'],
  1461. 'component_type' => $add_arr['type_id'],
  1462. 'component_name' => $add_arr['component_name'],
  1463. 'component_style' => 1,
  1464. ];
  1465. $add_arr['component_code'] = json_encode($component_head_code,true);
  1466. switch($data['type_id']){
  1467. case 1: //1:资讯-头条组件;
  1468. case 2: //2:资讯-轮播组件;
  1469. case 3: //3:资讯-推荐图类组件;
  1470. case 4: //4:资讯-最新类组件;
  1471. case 5: //5:资讯-推荐类组件;
  1472. case 6: //6:资讯-热点类组件;
  1473. // $component_data['componentData']['name'] = '';
  1474. $add_arr['level'] = $data['type_id'];
  1475. $add_arr['img_num'] = intval($data['img_num']);
  1476. $add_arr['text_num'] = intval($data['text_num']);
  1477. $component_data['componentData'] = [
  1478. // 'category_id' => '',
  1479. 'level' => $add_arr['level'],
  1480. 'name' => '',
  1481. // 'category_arr' => [],
  1482. 'imgSize' => $data['img_num'] ?? '',
  1483. 'textSize' => $data['text_num'] ?? '',
  1484. 'child' => [
  1485. 'id' => '',
  1486. 'imgSize' => '',
  1487. 'textSize' => ''
  1488. ]
  1489. ];
  1490. if($data['type_id'] == 4){
  1491. $component_data['componentData']['name'] = '最新资讯';
  1492. }
  1493. if($data['type_id'] == 5 || $data['type_id'] == 6){
  1494. $component_data['componentData']['name'] = '热点精选';
  1495. }
  1496. $add_arr['component_data'] = json_encode($component_data, true);
  1497. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1498. break;
  1499. case 7: //7:资讯-栏目类组件;
  1500. case 15: //15:滚动图文类;
  1501. $add_arr['img_num'] = intval($data['img_num']);
  1502. $add_arr['text_num'] = intval($data['text_num']);
  1503. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1504. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1505. $component_data['componentData'] = [
  1506. 'category_id' => '',
  1507. 'level' => '',
  1508. 'name' => '',
  1509. 'category_arr' => [],
  1510. 'imgSize' => $add_arr['img_num'] ?? '',
  1511. 'textSize' => $add_arr['text_num'] ?? '',
  1512. 'child' => [
  1513. 'id' => '',
  1514. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1515. 'textSize' => $add_arr['child_textnum'] ?? '',
  1516. ]
  1517. ];
  1518. $add_arr['component_data'] = json_encode($component_data,true);
  1519. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1520. break;
  1521. case 8: //8:列表类组件;
  1522. $add_arr['pageSize'] = intval($data['pageSize']);
  1523. $component_data['componentData'] = [
  1524. 'category_id' => '',
  1525. 'pageType' => [
  1526. 'page' => 1,
  1527. 'pageSize' => $add_arr['pageSize'] ?? '',
  1528. ]
  1529. ];
  1530. $add_arr['component_data'] = json_encode($component_data,true);
  1531. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1532. break;
  1533. case 9: //9:详情类组件;
  1534. $component_data['componentData'] = [
  1535. 'article_id' => '',
  1536. ];
  1537. $add_arr['component_data'] = json_encode($component_data,true);
  1538. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1539. break;
  1540. case 10: //10:二级导航类组件;
  1541. $add_arr['cate_place'] = 1;
  1542. $add_arr['num'] = intval($data['num']);
  1543. $component_data['componentData'] = [
  1544. 'pid' => '',
  1545. 'placeid' => 1,
  1546. 'num' => $add_arr['num'],
  1547. ];
  1548. $add_arr['component_data'] = json_encode($component_data,true);
  1549. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1550. break;
  1551. case 11: //11:广告类;
  1552. $add_arr['ad_width'] = intval($data['ad_width']);
  1553. $add_arr['ad_height'] = intval($data['ad_height']);
  1554. $add_arr['component_width'] = intval($data['ad_width']);
  1555. $add_arr['component_height'] = intval($data['ad_height']);
  1556. $add_arr['ad_type'] = intval($data['ad_type']);
  1557. // $add_arr['ad_img'] = $data['ad_img'];
  1558. $ad = [
  1559. 'width' => $add_arr['ad_width'],
  1560. 'height' => $add_arr['ad_height'],
  1561. 'name' => '',
  1562. 'price' => '',
  1563. 'introduce' => '',
  1564. 'website_id' => '',
  1565. // 'thumb' => $add_arr['ad_img'],
  1566. 'typeid' => $add_arr['ad_type'],
  1567. 'ad_tag' => '',
  1568. ];
  1569. $add_arr['ad'] = json_encode($ad,true);
  1570. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1571. // $add_arr['adimg_info'] = $data['img_info'];
  1572. // $ad_imginfo = json_decode($data['img_info'],true);
  1573. break;
  1574. // case 13: //13:底部导航类;
  1575. case 17: //17:单页列表类;
  1576. case 18: //18:单页详情类;
  1577. $component_data['componentData'] = [
  1578. 'fcat_id' => '',
  1579. ];
  1580. $add_arr['component_data'] = json_encode($component_data,true);
  1581. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1582. break;
  1583. case 14:
  1584. $add_arr['ad_width'] = intval($data['ad_width']);
  1585. $add_arr['ad_height'] = intval($data['ad_height']);
  1586. $add_arr['component_width'] = intval($data['component_width']);
  1587. $add_arr['component_height'] = intval($data['component_height']);
  1588. $add_arr['ad_type'] = intval($data['ad_type']);
  1589. $add_arr['ad_img'] = $data['ad_img'];
  1590. $add_arr['img_num'] = intval($data['img_num']);
  1591. $add_arr['text_num'] = intval($data['text_num']);
  1592. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1593. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1594. $component_data['componentData'] = [
  1595. 'category_id' => '',
  1596. 'level' => '',
  1597. 'name' => '',
  1598. 'category_arr' => [],
  1599. 'imgSize' => $data['img_num'] ?? '',
  1600. 'textSize' => $data['text_num'] ?? '',
  1601. 'child' => [
  1602. 'id' => '',
  1603. 'imgSize' => $data['child_imgnum'] ?? '',
  1604. 'textSize' => $data['child_textnum'] ?? '',
  1605. ]
  1606. ];
  1607. $ad = [
  1608. 'width' => $add_arr['ad_width'],
  1609. 'height' => $add_arr['ad_height'],
  1610. 'name' => '',
  1611. 'price' => '',
  1612. 'introduce' => '',
  1613. 'website_id' => '',
  1614. 'thumb' => $add_arr['ad_img'],
  1615. 'typeid' => $add_arr['ad_type'],
  1616. 'ad_tag' => '',
  1617. ];
  1618. $add_arr['ad'] = json_encode($ad,true);
  1619. $add_arr['component_data'] = json_encode($component_data,true);
  1620. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1621. // $add_arr['adimg_info'] = $data['img_info'];
  1622. // $ad_imginfo = json_decode($data['img_info'],true);
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. if($data['type_id'] == 11 || $data['type_id'] == 14){
  1628. Db::beginTransaction();
  1629. try{
  1630. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1631. // var_dump($website_img);
  1632. // $add_arr['ad_imgid'] = $website_img;
  1633. // if(empty($website_img)){
  1634. // Db::rollBack();
  1635. // return Result::error('广告默认图上传失败!');
  1636. // }
  1637. $result = Component::insertGetId($add_arr);
  1638. if(empty($result)){
  1639. Db::rollBack();
  1640. return Result::error('添加失败!');
  1641. }
  1642. Db::commit();
  1643. }catch(\Exception $e){
  1644. Db::rollback();
  1645. return Result::error('添加失败!');
  1646. }
  1647. }else{
  1648. $result = Component::insertGetId($add_arr);
  1649. if(empty($result)){
  1650. return Result::error('添加组件失败!');
  1651. }
  1652. }
  1653. return Result::success($result);
  1654. }
  1655. public function delComponent(array $data): array
  1656. {
  1657. $component = Component::where('id', $data['id'])->first();
  1658. if(empty($component)){
  1659. return Result::error('组件不存在!');
  1660. }
  1661. $component_id = SectorComponent::where('component_id', $component['component_type'])->first();
  1662. if (!empty($component_id)) {
  1663. return Result::error('请先解除相关组件关联关系!');
  1664. }
  1665. Db::beginTransaction();
  1666. try{
  1667. // if($component['type_id'] == 11 || $component['type_id'] == 14){
  1668. // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
  1669. // if(!empty($ad_img)){
  1670. // $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
  1671. // if(empty($ad_img)){
  1672. // Db::rollBack();
  1673. // return Result::error('删除广告默认图失败!');
  1674. // }
  1675. // }
  1676. // }
  1677. $component_img = ComponentImg::where('component_id',$component['component_type'])->first();
  1678. if(!empty($component_img)){
  1679. $del_img = ComponentImg::where('component_id',$component['component_type'])->delete();
  1680. if(empty($del_img)){
  1681. Db::rollBack();
  1682. return Result::error('删除组件预览图失败!');
  1683. }
  1684. }
  1685. $result = Component::where('id',$data['id'])->delete();
  1686. if(empty($result)){
  1687. Db::rollBack();
  1688. return Result::error('删除失败!');
  1689. }
  1690. Db::commit();
  1691. return Result::success($result);
  1692. }catch(\Exception $e){
  1693. Db::rollback();
  1694. return Result::error('删除失败!');
  1695. }
  1696. }
  1697. public function updateComponent(array $data): array
  1698. {
  1699. unset($data['user_id']);
  1700. $id = $data['id'];
  1701. unset($data['id']);
  1702. $component_id = Component::where('id','!=',$id)->where('component_type',$data['component_type'])->first();
  1703. if(!empty($component_id) || $component_id != null){
  1704. return Result::error('组件编号已存在!');
  1705. }
  1706. $component_type = ComponentType::where('id',$data['type_id'])->first();
  1707. if(empty($component_type)){
  1708. return Result::error('组件分类不存在!');
  1709. }
  1710. $component = Component::where('id',$id)->first();
  1711. if(empty($component)){
  1712. return Result::error('组件不存在!');
  1713. }
  1714. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;
  1715. // 7:资讯-栏目类组件;8:列表类组件;9:详情类组件;10:二级导航栏类组件;11:广告类;12:静态资源类;13:底部导航类;
  1716. $data['type_id'] = intval($data['type_id']);
  1717. $add_arr = [
  1718. // 'template_id' => intval($data['template_id']),
  1719. 'component_type' => intval($data['component_type']),
  1720. 'component_name' => $data['component_name'],
  1721. 'component_img' => $data['component_img'],
  1722. 'component_width' => $data['component_width'],
  1723. 'component_height' => $data['component_height'],
  1724. 'type_id' => intval($data['type_id']),
  1725. 'component_keyword' => $data['component_keyword'],
  1726. ];
  1727. $component_head_code = [
  1728. 'component_id' => $add_arr['component_type'],
  1729. 'component_type' => $add_arr['type_id'],
  1730. 'component_name' => $add_arr['component_name'],
  1731. 'component_style' => 1,
  1732. ];
  1733. $add_arr['component_code'] = json_encode($component_head_code,true);
  1734. switch($data['type_id']){
  1735. case 1: //1:资讯-头条组件;
  1736. case 2: //2:资讯-轮播组件;
  1737. case 3: //3:资讯-推荐图类组件;
  1738. case 4: //4:资讯-最新类组件;
  1739. case 5: //5:资讯-推荐类组件;
  1740. case 6: //6:资讯-热点类组件;
  1741. // $component_data['componentData']['name'] = '';
  1742. $add_arr['level'] = $data['type_id'];
  1743. $add_arr['img_num'] = intval($data['img_num']);
  1744. $add_arr['text_num'] = intval($data['text_num']);
  1745. $component_data['componentData'] = [
  1746. // 'category_id' => '',
  1747. 'level' => $add_arr['level'],
  1748. // 'category_arr' => [],
  1749. 'name' => '',
  1750. 'imgSize' => $data['img_num'] ?? '',
  1751. 'textSize' => $data['text_num'] ?? '',
  1752. 'child' => [
  1753. 'id' => '',
  1754. 'imgSize' => '',
  1755. 'textSize' => ''
  1756. ]
  1757. ];
  1758. if($data['type_id'] == 4){
  1759. $component_data['componentData']['name'] = '最新资讯';
  1760. }
  1761. if($data['type_id'] == 5 || $data['type_id'] == 6){
  1762. $component_data['componentData']['name'] = '热点精选';
  1763. }
  1764. $add_arr['component_data'] = json_encode($component_data, true);
  1765. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1766. break;
  1767. case 7: //7:资讯-栏目类组件;
  1768. case 15: //15:滚动图文类;
  1769. $add_arr['img_num'] = intval($data['img_num']);
  1770. $add_arr['text_num'] = intval($data['text_num']);
  1771. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1772. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1773. $component_data['componentData'] = [
  1774. 'category_id' => '',
  1775. 'level' => '',
  1776. 'name' => '',
  1777. 'category_arr' => [],
  1778. 'imgSize' => $add_arr['img_num'] ?? '',
  1779. 'textSize' => $add_arr['text_num'] ?? '',
  1780. 'child' => [
  1781. 'id' => '',
  1782. 'imgSize' => $add_arr['child_imgnum'] ?? '',
  1783. 'textSize' => $add_arr['child_textnum'] ?? '',
  1784. ]
  1785. ];
  1786. $add_arr['component_data'] = json_encode($component_data,true);
  1787. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1788. break;
  1789. case 8: //8:列表类组件;
  1790. $add_arr['pageSize'] = intval($data['pageSize']);
  1791. $component_data['componentData'] = [
  1792. 'category_id' => '',
  1793. 'pageType' => [
  1794. 'page' => 1,
  1795. 'pageSize' => $add_arr['pageSize'] ?? '',
  1796. ]
  1797. ];
  1798. $add_arr['component_data'] = json_encode($component_data,true);
  1799. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1800. break;
  1801. case 9: //9:详情类组件;
  1802. $component_data['componentData'] = [
  1803. 'article_id' => '',
  1804. ];
  1805. $add_arr['component_data'] = json_encode($component_data,true);
  1806. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1807. break;
  1808. case 10: //10:二级导航类组件;
  1809. $add_arr['cate_place'] = 1;
  1810. $add_arr['num'] = intval($data['num']);
  1811. $component_data['componentData'] = [
  1812. 'pid' => '',
  1813. 'placeid' => 1,
  1814. 'num' => $add_arr['num'],
  1815. ];
  1816. $add_arr['component_data'] = json_encode($component_data,true);
  1817. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1818. break;
  1819. case 11: //11:广告类;
  1820. $add_arr['ad_width'] = intval($data['ad_width']);
  1821. $add_arr['ad_height'] = intval($data['ad_height']);
  1822. $add_arr['component_width'] = intval($data['ad_width']);
  1823. $add_arr['component_height'] = intval($data['ad_height']);
  1824. $add_arr['ad_type'] = intval($data['ad_type']);
  1825. // $add_arr['ad_img'] = $data['ad_img'];
  1826. $ad = [
  1827. 'width' => $add_arr['ad_width'],
  1828. 'height' => $add_arr['ad_height'],
  1829. 'name' => '',
  1830. 'price' => '',
  1831. 'introduce' => '',
  1832. 'website_id' => '',
  1833. 'thumb' => $add_arr['ad_img'],
  1834. // 'typeid' => $add_arr['ad_type'],
  1835. 'ad_tag' => '',
  1836. ];
  1837. $add_arr['ad'] = json_encode($ad,true);
  1838. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1839. // $add_arr['adimg_info'] = $data['img_info'];
  1840. // $ad_imginfo = json_decode($data['img_info'],true);
  1841. break;
  1842. // case 13: //13:底部导航类;
  1843. case 17: //17:单页列表类;
  1844. case 18: //18:单页详情类;
  1845. $component_data['componentData'] = [
  1846. 'fcat_id' => '',
  1847. ];
  1848. $add_arr['component_data'] = json_encode($component_data,true);
  1849. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1850. break;
  1851. case 14:
  1852. $add_arr['ad_width'] = intval($data['ad_width']);
  1853. $add_arr['ad_height'] = intval($data['ad_height']);
  1854. $add_arr['component_width'] = intval($data['component_width']);
  1855. $add_arr['component_height'] = intval($data['component_height']);
  1856. $add_arr['ad_type'] = intval($data['ad_type']);
  1857. $add_arr['ad_img'] = $data['ad_img'];
  1858. $add_arr['img_num'] = intval($data['img_num']);
  1859. $add_arr['text_num'] = intval($data['text_num']);
  1860. $add_arr['child_imgnum'] = intval($data['child_imgnum'] ?? null);
  1861. $add_arr['child_textnum'] = intval($data['child_textnum'] ?? null);
  1862. $component_data['componentData'] = [
  1863. 'category_id' => '',
  1864. 'level' => '',
  1865. 'name' => '',
  1866. 'category_arr' => [],
  1867. 'imgSize' => $data['img_num'] ?? '',
  1868. 'textSize' => $data['text_num'] ?? '',
  1869. 'child' => [
  1870. 'id' => '',
  1871. 'imgSize' => $data['child_imgnum'] ?? '',
  1872. 'textSize' => $data['child_textnum'] ?? '',
  1873. ]
  1874. ];
  1875. $ad = [
  1876. 'width' => $add_arr['ad_width'],
  1877. 'height' => $add_arr['ad_height'],
  1878. 'name' => '',
  1879. 'price' => '',
  1880. 'introduce' => '',
  1881. 'website_id' => '',
  1882. 'thumb' => $add_arr['ad_img'],
  1883. 'typeid' => $add_arr['ad_type'],
  1884. 'ad_tag' => '',
  1885. ];
  1886. $add_arr['ad'] = json_encode($ad,true);
  1887. $add_arr['component_data'] = json_encode($component_data,true);
  1888. $add_arr['component_column'] = $component_type['com_code'] ?? '';
  1889. // $add_arr['adimg_info'] = $data['img_info'];
  1890. // $ad_imginfo = json_decode($data['img_info'],true);
  1891. break;
  1892. default:
  1893. break;
  1894. }
  1895. // var_dump("ad_imginfo:",$ad_imginfo);
  1896. $cll_column = [
  1897. 'img_num' => null,
  1898. 'text_num' => null,
  1899. 'child_imgnum' => null,
  1900. 'child_textnum' => null,
  1901. 'category_id' => null,
  1902. 'child_id' => null,
  1903. 'num' => null,
  1904. 'level' => null,
  1905. 'fcatid' => null,
  1906. 'article_id' => null,
  1907. 'cate_place' => null,
  1908. 'pageSize' => null,
  1909. // 'ad_img' => null,
  1910. 'ad_width' => null,
  1911. 'ad_height' => null,
  1912. 'ad' => '',
  1913. 'ad_type' => null,
  1914. // 'ad_imgid' => null,
  1915. 'adimg_info' => '[]',
  1916. ];
  1917. Db::beginTransaction();
  1918. try{
  1919. // return Result::success($add_arr);
  1920. // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] != 11 && $data['type_id'] != 14)){
  1921. // // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
  1922. // if(empty($website_img)){
  1923. // Db::rollBack();
  1924. // return Result::error('广告默认图删除失败!');
  1925. // }
  1926. // var_dump("删除",$website_img);
  1927. // }
  1928. // if(($data['type_id'] == 11 || $data['type_id'] == 14) && ($component['type_id'] != 11 && $component['type_id'] != 14)){
  1929. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1930. // $add_arr['ad_imgid'] = $website_img;
  1931. // if(empty($website_img)){
  1932. // Db::rollBack();
  1933. // return Result::error('广告默认图上传失败!');
  1934. // }
  1935. // var_dump("上传",$website_img);
  1936. // }
  1937. // if(($component['type_id'] == 11 || $component['type_id'] == 14) && ($data['type_id'] == 11 || $data['type_id'] == 14)){
  1938. // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
  1939. // if(empty($website_img)){
  1940. // $website_img = WebsiteImg::insertGetId($ad_imginfo);
  1941. // $add_arr['ad_imgid'] = $website_img;
  1942. // }else{
  1943. // $website_img = WebsiteImg::where('id',$component['ad_imgid'])->update($ad_imginfo);
  1944. // $add_arr['ad_imgid'] = $component['ad_imgid'];
  1945. // }
  1946. // if(empty($website_img)){
  1947. // Db::rollBack();
  1948. // return Result::error('广告默认图修改失败!');
  1949. // }
  1950. // }
  1951. $add_arr = array_merge($cll_column,$add_arr);
  1952. $result = Component::where('id',$id)->update($add_arr);
  1953. if(empty($result)){
  1954. Db::rollBack();
  1955. return Result::error('修改失败!');
  1956. }
  1957. Db::commit();
  1958. }catch(\Exception $e){
  1959. Db::rollBack();
  1960. return Result::error('修改失败!');
  1961. }
  1962. return Result::success($result);
  1963. }
  1964. public function getWebsiteTemplateInfo(array $data)
  1965. {
  1966. $where = [];
  1967. if (isset($data['id'])) {
  1968. $where[] = ['id', '=', $data['id']];
  1969. }
  1970. $result = WebsiteTemplateInfo::where($where)
  1971. ->leftJoin('website', 'website_template_info.website_id', '=', 'website.id')
  1972. ->leftJoin('website_template', 'website_template_info.template_id', '=', 'website_template.id')
  1973. ->select('website_template_info.*', 'website.website_name')
  1974. ->get();
  1975. if ($result) {
  1976. return Result::success($result);
  1977. } else {
  1978. return Result::error('暂无数据');
  1979. }
  1980. }
  1981. /**
  1982. * 自助建站----1.获取页面类型回显
  1983. * @param array $data
  1984. * @return array
  1985. */
  1986. public function getWebPageType(array $data): array
  1987. {
  1988. // 1:图片 2:文字 3:底部
  1989. $friend_link = Link::where('website_id', $data['website_id'])->where('type', 3)->get();
  1990. if (empty($friend_link->toArray())) {
  1991. return Result::error('暂无友情链接数据!');
  1992. }
  1993. $footer_category = FooterCategory::where('website_id', $data['website_id'])->get();
  1994. if (empty($footer_category->toArray())) {
  1995. return Result::error('暂无底部导航数据!');
  1996. }
  1997. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('page_type');
  1998. if (empty($page_type)) {
  1999. $page_type = [];
  2000. } else {
  2001. $page_type = json_decode($page_type, true);
  2002. }
  2003. // '网站页面类型(存数组)1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  2004. if (isset($page_type) && in_array(5, $page_type)) {
  2005. $page_type = 1;
  2006. } else {
  2007. $page_type = 0;
  2008. }
  2009. $result = [
  2010. 'friend_link' => $friend_link,
  2011. 'footer_category' => $footer_category,
  2012. 'page_type' => $page_type,
  2013. ];
  2014. return Result::success($result);
  2015. }
  2016. /**
  2017. * 自助建站----2.添加页面类型
  2018. * @param array $data
  2019. * @return array
  2020. */
  2021. public function addWebPageType(array $data): array
  2022. {
  2023. // return Result::success($data);
  2024. // 1:首页 2:频道页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页'
  2025. if ($data['is_search'] == 1) {
  2026. $page_type = json_encode([1,2,3,4,5,6,7]);
  2027. } else {
  2028. $page_type = json_encode([1,2,3,4,6,7]);
  2029. }
  2030. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  2031. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2032. $website_template = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  2033. Db::beginTransaction();
  2034. try{
  2035. if (empty($website_template_info)) {
  2036. $result['template_info'] = WebsiteTemplateInfo::create([
  2037. 'website_id' => $data['website_id'],
  2038. 'page_type' => $page_type,
  2039. 'user_id' => $data['user_id']
  2040. ])->id;
  2041. } else {
  2042. if ($website_template_info['status'] == 2) {
  2043. Db::rollBack();
  2044. return Result::error('网站已应用,不可再次修改!');
  2045. }
  2046. $result['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['page_type' => $page_type, 'action_id' => 1, 'user_id' => $data['user_id']]);
  2047. }
  2048. if (!$result['template_info']) {
  2049. Db::rollBack();
  2050. return Result::error('网站所选页面添加失败');
  2051. }
  2052. if(empty($website_template)){
  2053. $result['template'] = WebsiteTemplate::insertGetId([
  2054. 'website_id' => $data['website_id'],
  2055. 'page_type' => $page_type,
  2056. 'user_id' => $data['user_id'],
  2057. ]);
  2058. }else{
  2059. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])->update([
  2060. 'page_type' => $page_type,
  2061. ]);
  2062. }
  2063. if(!$result['template']){
  2064. Db::rollBack();
  2065. return Result::error('添加网站预制模版信息失败!');
  2066. }
  2067. Db::commit();
  2068. return Result::success($result);
  2069. }catch(\Exception $e){
  2070. Db::rollBack();
  2071. return Result::error('添加失败');
  2072. }
  2073. }
  2074. /**
  2075. * 自助建站---流程---3.选择皮肤列表
  2076. * @param array $data
  2077. * @return array
  2078. */
  2079. public function getWebsiteTemplateList(array $data): array
  2080. {
  2081. $where = [];
  2082. if (isset($data['template_class_id']) && $data['template_class_id']) {
  2083. $where[] = ['template_class_id', '=', $data['template_class_id']];
  2084. }
  2085. // 0:未构建 1:未应用 2:已应用 status
  2086. $templste_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2087. if (empty($templste_info)) {
  2088. return Result::error('请先填写网站基础信息!');
  2089. }
  2090. if ($templste_info['status'] == 2) {
  2091. return Result::error('网站已应用,不可再次修改!');
  2092. }
  2093. if (isset($data['keyword']) && !empty($data['keyword'])) {
  2094. if (is_array($data['keyword'])) {
  2095. // 拼接多个模糊查询条件
  2096. $query = Template::query();
  2097. foreach ($data['keyword'] as $kw) {
  2098. $escapedKw = addcslashes($kw, '%_'); // 转义通配符
  2099. $query->WhereRaw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?", ["%$escapedKw%"]);
  2100. }
  2101. } else {
  2102. $escapedKeyword = addcslashes($data['keyword'], '%_'); // 转义通配符
  2103. $where[] = [DB::raw("JSON_EXTRACT(template_keyword, '$[*]') LIKE ?"), "%$escapedKeyword%"];
  2104. }
  2105. } else {
  2106. $query = Template::where($where);
  2107. }
  2108. $template_id = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first(['template_id', 'page_type']);
  2109. // return Result::success($template_id);
  2110. if (!empty($template_id['page_type']) && $templste_info['page_type'] != '[]' && is_string($templste_info['page_type'])) {
  2111. $template_page = is_string($templste_info['page_type']) ? json_decode($templste_info['page_type'], true) : [];
  2112. } else {
  2113. $template_page = [];
  2114. }
  2115. if (!empty($template_id['template_id'])) {
  2116. $result['template_id'] = $templste_info['template_id'];
  2117. }
  2118. // 确保 $template_page 为有效数据,避免 SQL 注入风险,这里使用参数绑定
  2119. $template_page_str = json_encode($template_page);
  2120. $result['template'] = $query->where($where)
  2121. ->whereRaw("JSON_CONTAINS(template.page_type, ?)", [$template_page_str])
  2122. ->orderBy('template.template_id','desc')
  2123. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2124. if (empty($result['template'])) {
  2125. $result['template'] = Template::orderBy('template.template_id','desc')
  2126. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2127. }
  2128. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->value('template_id');
  2129. if ($template) {
  2130. $result['template_id'] = $template;
  2131. } else {
  2132. $result['template_id'] = 0;
  2133. }
  2134. return Result::success($result);
  2135. }
  2136. /**
  2137. * 自助建站---流程---4.选择皮肤
  2138. * @param array $data
  2139. * @return array
  2140. */
  2141. public function chooseWebsiteTemplate(array $data): array
  2142. {
  2143. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2144. if (empty($website_template_info)) {
  2145. return Result::error('请先填写网站基础信息!');
  2146. }
  2147. // 0:未构建 1:未应用 2:已应用 status
  2148. if ($website_template_info['status'] == 2) {
  2149. return Result::error('网站已应用,不可再次修改!');
  2150. }
  2151. $template = Template::where('template_id', $data['template_id'])->first();
  2152. if (empty($template)) {
  2153. return Result::error('未查询到皮肤!');
  2154. }
  2155. // 0:未构建;1:已填写基础信息;2:已选择模板; action_id
  2156. // 分别更新 WebsiteTemplateInfo 和 WebsiteTemplate 表
  2157. Db::beginTransaction();
  2158. try {
  2159. $reuslt['template_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])
  2160. ->update(['template_id' => $data['template_id'],'action_id' => 2,'user_id'=> $data['user_id']]);
  2161. if(empty($reuslt['template_info'])){
  2162. Db::rollBack();
  2163. return Result::error('选择皮肤失败!');
  2164. }
  2165. $result['template'] = WebsiteTemplate::where('website_id', $data['website_id'])
  2166. ->update(['template_id' => $data['template_id'],'user_id'=> $data['user_id']]);
  2167. if(empty($result['template'])){
  2168. Db::rollBack();
  2169. return Result::error('网站模版选择皮肤失败!');
  2170. }
  2171. Db::commit();
  2172. return Result::success($result);
  2173. } catch (\Exception $e) {
  2174. Db::rollBack();
  2175. return Result::error('选择皮肤失败!');
  2176. }
  2177. }
  2178. /**
  2179. * 自助建站---流程---5.保存模版
  2180. * @param array $data
  2181. * @return array
  2182. */
  2183. public function saveWebsiteTemplate(array $data): array
  2184. {
  2185. // return Result::success($data);
  2186. $website_template_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2187. if (empty($website_template_info) || empty($website_template_info->template_id) || empty($website_template_info->page_type)) {
  2188. return Result::error('请先填写网站基础信息!');
  2189. }
  2190. // 0:未构建 1:未应用 2:已应用 status
  2191. if ($website_template_info['status'] == 2) {
  2192. return Result::error('网站已应用,不可再次保存!');
  2193. }
  2194. if ($website_template_info['action_id'] != 2) {
  2195. return Result::error('请完成之前的步骤!');
  2196. }
  2197. $template_info = WebsiteTemplate::where('website_id', $data['website_id'])->first();
  2198. // var_dump($data['template_data']);
  2199. try {
  2200. Db::beginTransaction();
  2201. if (empty($template_info)) {
  2202. Db::rollback();
  2203. return Result::error('该网站不存在已保存的模版!');
  2204. } else {
  2205. $template = WebsiteTemplate::where('website_id', $data['website_id'])->update(
  2206. [
  2207. 'template_data' => $data['template_data'],
  2208. 'user_id' => $data['user_id'],
  2209. 'canvas_data' => $data['canvas_data'],
  2210. ]
  2211. );
  2212. }
  2213. if (empty($template)) {
  2214. Db::rollback();
  2215. return Result::error('保存失败!');
  2216. } else {
  2217. $tempalte_info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['status' => 1]);
  2218. if (empty($tempalte_info)) {
  2219. Db::rollback();
  2220. return Result::error('网站搭建状态保存失败!');
  2221. }
  2222. Db::commit();
  2223. }
  2224. } catch (\Exception $e) {
  2225. return Result::error('模版数据格式错误!');
  2226. }
  2227. return Result::success($template);
  2228. }
  2229. /**
  2230. * 自助建站---流程---6.返显保存的模版
  2231. * @param array $data
  2232. * @return array
  2233. */
  2234. public function getWebsiteTemplate(array $data): array
  2235. {
  2236. $web = Website::where('id', $data['website_id'])->first();
  2237. if (empty($web)) {
  2238. return Result::error('未查询到网站信息!');
  2239. }
  2240. $template = WebsiteTemplate::where('website_id', $data['website_id'])
  2241. ->first(['website_id', 'template_id', 'page_type', 'template_data', 'canvas_data']);
  2242. if (empty($template)) {
  2243. return Result::error('未查询到模版!');
  2244. }
  2245. $template['page_type'] = json_decode($template['page_type'],true);
  2246. return Result::success($template);
  2247. }
  2248. /**
  2249. * 通栏版式管理-获取通栏版式列表
  2250. */
  2251. public function getSectorPlaceList(array $data): array
  2252. {
  2253. $where['sector_place.type_id'] = $data['type_id'];
  2254. if(isset($data['component_num']) && !empty($data['component_num'])){
  2255. $where['sector_place.component_num'] = $data['component_num'];
  2256. }
  2257. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2258. $where['sector_place.sector_type'] = $data['sector_type'];
  2259. }
  2260. // if(isset($data['component_type']) && !empty($data['component_type'])){
  2261. // $where['sector_place.component_type'] = $data['component_type'];
  2262. // }
  2263. if(isset($data['name']) && !empty($data['name'])){
  2264. array_push($where,['sector_place.name', 'like', '%'.$data['name'].'%']);
  2265. }
  2266. if (isset($data['width']) && !empty($data['width']) && isset($data['height']) && !empty($data['height'])){
  2267. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2268. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2269. } else if( isset($data['width']) && !empty($data['width'])){
  2270. array_push($where,['sector_place.width','like','%'.$data['width'].'%']);
  2271. }else if( isset($data['height']) && !empty($data['height'])){
  2272. array_push($where,['sector_place.height','like','%'.$data['height'].'%']);
  2273. }else{
  2274. $size_id = [];
  2275. }
  2276. $sector_place = SectorPlace::where($where)
  2277. // ->when(!empty($size_id), function ($query) use ($size_id) {
  2278. // $query->whereIn('sector_place.size_id', $size_id);
  2279. // })
  2280. // ->leftJoin('size','sector_place.size_id','=','size.id')
  2281. ->select('sector_place.*');
  2282. $result['count'] = $sector_place->count();
  2283. $result['row'] =$sector_place->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2284. if(empty($result['count'])){
  2285. return Result::error("暂无相关版式数据!");
  2286. }
  2287. return Result::success($result);
  2288. }
  2289. /**
  2290. * 通栏版式管理-添加通栏版式
  2291. */
  2292. public function addSectorPlace(array $data): array
  2293. {
  2294. // 1:通栏;2:组件;
  2295. if($data['type_id'] == 1){
  2296. $sector_place = SectorPlace::where('sector_type', $data['type'])->first();
  2297. if(!empty($sector_place)){
  2298. return Result::error('通栏版式类别编号已存在!');
  2299. }
  2300. $data = [
  2301. 'name' => $data['name'],
  2302. 'component_num' => $data['component_num'],
  2303. 'size_id' => $data['size_id'],
  2304. 'sort_id' => 0,
  2305. 'sector_img' => $data['sector_img'],
  2306. 'sector_type' => $data['type'],
  2307. 'type_id' => $data['type_id'],
  2308. 'status' => 0,
  2309. ];
  2310. }else{
  2311. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  2312. if(empty($sector_type)){
  2313. return Result::error('通栏版式类别不存在!');
  2314. }
  2315. // $com_query = SectorPlace::where('type_id',2);
  2316. // ->query(function($query) use ($data){
  2317. // $query->where('component_type', $data['type'])
  2318. // ->Subquery(function($subQuery) use ($data) {
  2319. // $subQuery->where('sector_type', $data['sector_type'])
  2320. // ->where('sort_id', $data['sort']);
  2321. // });
  2322. // }) //若是组件版式编号或者组件版式位置重复用此语句筛选
  2323. // ->where('sector_type',$data['sector_type']) //若是组件版式编号在同一个通栏版式下不重复就行,则启用此语句
  2324. $component = SectorPlace::where('type_id',2)->where('component_type',$data['type'])->first();
  2325. if(!empty($component)){
  2326. return Result::error('组件版式类别编号已存在!');
  2327. }
  2328. // $com_query = clone $com_query;
  2329. $com_num = SectorPlace::where('type_id',2)->where('sector_type',$data['sector_type'])->count('sort_id');
  2330. // return Result::success($com_num);
  2331. if($sector_type['component_num'] = $com_num){
  2332. $sector = SectorPlace::where('sector_type',$data['sector_type'])->where('type_id',1)->update(['status'=>1]);
  2333. if(empty($sector)){
  2334. return Result::error('通栏版式状态修改失败!');
  2335. }
  2336. }
  2337. $data = [
  2338. 'name' => $data['name'],
  2339. 'component_num' => 0,
  2340. 'size_id' => $data['size_id'],
  2341. 'sort_id' => $data['sort_id'],
  2342. 'sector_img' => $data['sector_img'],
  2343. 'component_type' => $data['type'],
  2344. 'type_id' => $data['type_id'],
  2345. 'sector_type' => $data['sector_type'],
  2346. ];
  2347. }
  2348. $result = SectorPlace::insertGetId($data);
  2349. if(empty($result)){
  2350. return Result::error('添加失败!');
  2351. }
  2352. return Result::success('添加成功!',$result);
  2353. }
  2354. /**
  2355. * 通栏版式管理-修改通栏版式
  2356. */
  2357. public function upSectorPlace(array $data): array
  2358. {
  2359. $id = $data['id'];
  2360. // 1:通栏;2:组件;
  2361. if($data['type_id'] == 1){
  2362. $sector_place = SectorPlace::where('id','!=',$id)->where('sector_type', $data['type'])->where('type_id',1)->first();
  2363. if(!empty($sector_place)){
  2364. return Result::error('通栏版式类别编号已存在!');
  2365. }
  2366. $data = [
  2367. 'name' => $data['name'],
  2368. 'component_num' => $data['component_num'],
  2369. 'size_id' => $data['size_id'],
  2370. 'sort_id' => 0,
  2371. 'sector_img' => $data['sector_img'],
  2372. 'sector_type' => $data['type'],
  2373. 'type_id' => $data['type_id'],
  2374. ];
  2375. }else{
  2376. $sector_type = SectorPlace::where('sector_type', $data['sector_type'])->where('type_id',1)->first();
  2377. if(empty($sector_type)){
  2378. return Result::error('通栏版式类别不存在!');
  2379. }
  2380. $component = SectorPlace::where('id','!=',$id)
  2381. ->where('component_type', $data['type'])
  2382. ->where('type_id',2)
  2383. ->first();
  2384. if(!empty($component)){
  2385. return Result::error('组件版式类别编号已存在!');
  2386. }
  2387. $data = [
  2388. 'name' => $data['name'],
  2389. 'component_num' => 0,
  2390. 'size_id' => $data['size_id'],
  2391. 'sort_id' => $data['sort_id'],
  2392. 'sector_img' => $data['sector_img'],
  2393. 'component_type' => $data['type'],
  2394. 'type_id' => $data['type_id'],
  2395. 'sector_type' => $data['sector_type'],
  2396. ];
  2397. }
  2398. $result = SectorPlace::where('id',$id)->update($data);
  2399. if(empty($result)){
  2400. return Result::error('修改失败!');
  2401. }
  2402. return Result::success('修改成功!',$result);
  2403. }
  2404. /**
  2405. * 通栏版式管理-删除通栏版式
  2406. */
  2407. public function delSectorPlace(array $data): array
  2408. {
  2409. $sector_place = SectorPlace::where('id',$data['id'])->first();
  2410. if(empty($sector_place)){
  2411. return Result::error('通栏版式不存在!');
  2412. }
  2413. if($sector_place['type_id'] == 1){
  2414. $sector = Sector::where('place_type', $sector_place['sector_type'])->first();
  2415. $component = SectorPlace::where('sector_type', $sector_place['sector_type'])
  2416. ->where('type_id',2)
  2417. ->first();
  2418. if(!empty($sector) || !empty($component)){
  2419. return Result::error('通栏版式分类已应用,暂不可删除!');
  2420. }
  2421. }else{
  2422. $sector = Component::where('place_type', $sector_place['component_type'])->first();
  2423. if(!empty($sector)){
  2424. return Result::error('组件版式分类已应用,暂不可删除!');
  2425. }
  2426. $component_num = SectorPlace::where('sector_type', $sector_place['sector_type'])
  2427. ->where('type_id',1)->update(['status'=> 0]);
  2428. if(empty($component_num)){
  2429. return Result::error('通栏版式状态修改失败!');
  2430. }
  2431. }
  2432. $result = SectorPlace::where('id',$data['id'])->delete();
  2433. if(empty($result)){
  2434. return Result::error('删除失败!');
  2435. }
  2436. return Result::success('删除成功!');
  2437. }
  2438. /**
  2439. * 自助建站-通栏版式管理-获取通栏版式详情
  2440. */
  2441. public function getSectorPlaceInfo(array $data): array
  2442. {
  2443. $result = SectorPlace::where('sector_place.id',$data['id'])
  2444. ->leftJoin('size','sector_place.size_id','=','size.id')
  2445. ->select('sector_place.*','size.width','size.height')
  2446. ->first();
  2447. if(empty($result)){
  2448. return Result::error('通栏版式不存在!');
  2449. }
  2450. return Result::success($result);
  2451. }
  2452. /**
  2453. * 自助建站-通栏版式管理-获取通栏版式列表
  2454. */
  2455. public function getSectorPlaceSort(array $data): array
  2456. {
  2457. $sector = SectorPlace::where('sector_type',$data['sector_type'])->first();
  2458. if(empty($sector)){
  2459. return Result::error('通栏版式不存在!');
  2460. }
  2461. $component = SectorPlace::where('type_id',2)->where('sector_type',$sector['sector_type'])->pluck('sort_id')->toArray();
  2462. if(count($component) < $sector['component_num']){
  2463. // 根据 $sector['component_num'] 的值生成对应元素数量的数组
  2464. if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
  2465. $num = intval($sector['component_num']);
  2466. if ($num > 0 && $num <= 10) {
  2467. // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
  2468. $sector_arr = range(1, $num);
  2469. } else {
  2470. $sector_arr = [];
  2471. }
  2472. } else {
  2473. $sector_arr = [];
  2474. }
  2475. // 获取 $sector_arr 中不在 $component 里的元素
  2476. $sort = array_diff($sector_arr, $component);
  2477. // return Result::success($sort);
  2478. }
  2479. if(!empty($sort)){
  2480. $sort = array_values(array_map(function($value) {
  2481. return ['id' => $value];
  2482. }, $sort));
  2483. }else{
  2484. return Result::error("组件版式数量已达上限!");
  2485. }
  2486. return Result::success($sort);
  2487. }
  2488. /**
  2489. * 获取所有通栏版式
  2490. */
  2491. public function getAllSectorPlace(array $data): array
  2492. {
  2493. $where['type_id'] = $data['type_id'];
  2494. // if($data['type_id'] == 1){
  2495. // $where['status'] = 1;
  2496. // }
  2497. if(isset($data['sector_type']) && !empty($data['sector_type'])){
  2498. $where['sector_type'] = $data['sector_type'];
  2499. }
  2500. if(isset($data['component_num']) && !empty($data['component_num'])){
  2501. $where['component_num'] = $data['component_num'];
  2502. }
  2503. $sector_place = SectorPlace::where($where)
  2504. ->when($data['type_id'] == 2, function($query) use($data){
  2505. $query->orderBy('sort_id','asc');
  2506. })
  2507. ->get()->all();
  2508. if($data['type_id'] == 2){
  2509. foreach($sector_place as $key => $value){
  2510. $sector_place[$key]['component'] = Component::where('type_id',$value['type'])
  2511. ->where('component_width',$value['width'])
  2512. ->where('component_height',$value['height'])
  2513. ->get()->all();
  2514. }
  2515. }
  2516. if(empty($sector_place)){
  2517. return Result::error('通栏版式不存在!');
  2518. }
  2519. return Result::success($sector_place);
  2520. }
  2521. /**
  2522. * 组件管理-获取组件类型
  2523. */
  2524. public function getComponentType(array $data): array
  2525. {
  2526. $where = [];
  2527. if(isset($data['id']) && !empty($data['id'])){
  2528. $where['id'] = $data['id'];
  2529. }
  2530. $component_type = ComponentType::where($where)->get()->all();
  2531. if(empty($component_type)){
  2532. return Result::error('组件类型不存在!');
  2533. }
  2534. if(count($component_type) == 1){
  2535. $all_code = json_decode($component_type[0]['com_code'] ?? '',true);
  2536. $component_type[0]['all_code'] = $all_code['listType'] ?? [];
  2537. $result = $component_type[0];
  2538. }else{
  2539. $result = $component_type;
  2540. }
  2541. if(empty($component_type)){
  2542. return Result::error('组件类型不存在!');
  2543. }
  2544. return Result::success($component_type);
  2545. }
  2546. /**
  2547. * 组件管理-获取所有组件样式
  2548. */
  2549. public function getAllComponent(array $data): array
  2550. {
  2551. $where = [];
  2552. if((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))){
  2553. $sector_id['sector.sector_id'] = $data['sector_id'];
  2554. $sector['sector_component.sort_id'] = $data['sort_id'];
  2555. $sectors = Sector::where($sector_id)
  2556. ->where('sector.template_id',$data['template_id'])
  2557. ->leftJoin('sector_component','sector.id','sector_component.sectorid')
  2558. ->where($sector)
  2559. ->select('sector.template_id','sector_component.component_id')
  2560. ->get()->all();
  2561. // return Result::success($sectors);
  2562. $component_id = array_column($sectors,'component_id');
  2563. $template = ['template_id' => $sectors[0]['template_id']];
  2564. $component = ComponentImg::whereIn('component_id',$component_id)
  2565. ->where($template)
  2566. ->select('img_id','img_name','img_url','template_id')
  2567. ->orderBy('component_img.img_id','asc')
  2568. ->get()->all();
  2569. }else{
  2570. if(isset($data['type_id']) && !empty($data['type_id'])){
  2571. $where['component.type_id'] = $data['type_id'];
  2572. }
  2573. if(isset($data['width']) && !empty($data['width'])){
  2574. $where['component.component_width'] = $data['width'];
  2575. }
  2576. if(isset($data['height']) && !empty($data['height'])){
  2577. $where['component.component_height'] = $data['height'];
  2578. }
  2579. $component = Component::where($where)
  2580. ->leftJoin('component_img','component.component_id','=','component_img.component_id')
  2581. ->select('component.*','component_img.img_id','component_img.img_url','component_img.img_name')
  2582. // ->orderBy('component_img.template_id')
  2583. ->orderBy('component_img.img_id','asc')
  2584. ->get()->all();
  2585. }
  2586. if(empty($component)){
  2587. return Result::error('组件不存在!');
  2588. }
  2589. return Result::success($component);
  2590. }
  2591. /**
  2592. * 自助建站-流程管理-获取所有通栏
  2593. */
  2594. public function getAllSector(array $data): array
  2595. {
  2596. $where = [];
  2597. if(isset($data['template_id']) && !empty($data['template_id'])){
  2598. $where['sector.template_id'] = $data['template_id'];
  2599. }
  2600. if(isset($data['page_type']) && !empty($data['page_type'])){
  2601. array_push($where,['sector.page_type', 'like', '%'.$data['page_type'].'%']);
  2602. }
  2603. $sector = Sector::where($where)
  2604. ->leftJoin('sector_component', 'sector.id', '=', 'sector_component.sectorid')
  2605. ->leftJoin('component', 'sector_component.component_id', '=', 'component.component_type')
  2606. ->when(isset($data['template_id']), function ($query) use ($data) {
  2607. // 当 type_id 等于 11 时,关联查询 component_img 表并添加 img_url 字段
  2608. return $query->leftJoin('component_img', function ($join) use ($data) {
  2609. $join->on('component.component_type', '=', 'component_img.component_id')
  2610. ->where('component_img.template_id', $data['template_id'])
  2611. ->where('component.type_id', 11);
  2612. });
  2613. })
  2614. ->select('sector.sector_name', 'sector.sector_id', 'component.*', 'component_img.img_url'
  2615. ,'sector.pic_height','sector.sector_img','sector_component.sort_id')
  2616. ->orderBy('sector_component.sort_id')
  2617. ->orderBy('sector_component.sector_id')
  2618. ->get()->all();
  2619. if(empty($sector)){
  2620. return Result::error('通栏不存在!');
  2621. }
  2622. $com_num = array_count_values(array_column($sector,'sector_id'));
  2623. // 使用 array_keys 和 array_diff 函数,不使用循环将值不为 1 的字段键提取到 $sectors_key 中
  2624. $sectors_key = array_keys(array_diff($com_num, [1]));
  2625. // return Result::success($sector);
  2626. $num = 0;
  2627. $array_key = 0;
  2628. foreach($sector as $key => $value){
  2629. $sector_id = $value['sector_id'];
  2630. $sectors[$key] = $value['sector_id'];
  2631. // if(isset($comlist_key) && !empty($comlist_key)){
  2632. // $sector_arr_key[$key] = array_keys($comlist_key);
  2633. // }
  2634. if(in_array($sector_id,$sectors_key) && in_array($sector_id,$sectors)){
  2635. $array_key = array_search($sector_id,$sectors);
  2636. if(isset($sectors) && !in_array($sector_id,$sectors)){
  2637. $array_key++;
  2638. }
  2639. $num++;
  2640. $array_key++;
  2641. }else{
  2642. $array_key++;
  2643. }
  2644. // var_dump($array_key);
  2645. // var_dump(in_array($sector_id,$sectors_key));
  2646. $comlist_key[$array_key][$sector_id]['sectorName'] = $value['sector_name'];
  2647. $comlist_key[$array_key][$sector_id]['sectorHeight'] = $value['pic_height'];
  2648. $comlist_key[$array_key][$sector_id]['sectorImg'] = $value['sector_img'];
  2649. $componentList['component_style'] = 1;
  2650. $componentList['sort'] = $value['sort_id'];
  2651. // 1:资讯;2:广告;3:静态;
  2652. $component_data = empty($value['component_data']) ? null : (json_decode($value['component_data']) ?? []);
  2653. // 检查 $value['component_column'] 是否为非空字符串,避免传递 null 给 json_decode
  2654. $listType = empty($value['component_column']) ? null : (json_decode($value['component_column']) ?? []);;
  2655. // $page[$key][$setor_id]['componentList'] =
  2656. // 组件分类:1:资讯-头条组件;2:资讯-轮播组件;3:资讯-推荐图类组件;4:资讯-最新类组件;5:资讯-推荐类;6:资讯-热点类组件;7:资讯-栏目类组件;8:列表类组件;9:详情类组件;
  2657. // 10:二级导航栏类组件;11:广告类;12:静态资源类;13:单页导航类;14:广告资讯混合类;15:滚动图文;16:搜索框类;17:单页列表类;18:单页详情类;
  2658. if(in_array($value['type_id'],[1,2,3,4,5,6,7,8,9,15,17,18])){
  2659. $type = 1;
  2660. $componentList['component_type'] = $type;
  2661. if(in_array($value['type_id'],[7,15]) && $data['page_type'] == 1){
  2662. $component_data->componentData->name = '请选择导航..';
  2663. }else if(in_array($value['type_id'],[7,15]) && $data['page_type'] == 2){
  2664. $component_data->componentData->name = '自动生成';
  2665. }else if($data['page_type'] == 3){
  2666. if($value['type_id'] == 4){
  2667. $component_data->componentData->name = '最新资讯';
  2668. }
  2669. if($value['type_id'] == 6){
  2670. $component_data->componentData->name = '热点精选';
  2671. }
  2672. }else{
  2673. unset($component_data->componentData->name);
  2674. }
  2675. $componentData = $component_data->componentData;
  2676. $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
  2677. // $page_listType = is_object($listType) ? (array)$listType->listType ?? [] : $listType['listType'] ?? [];
  2678. $componentList['componentData']['listType'] = $listType->listType;
  2679. }else if(in_array($value['type_id'],[10,12,13,16])){
  2680. $type = 3;
  2681. $componentList['component_type'] = $type;
  2682. $componentList['componentData']=(object)[];
  2683. }else if($value['type_id']== 11){
  2684. $type = 2;
  2685. $componentList['component_type'] = $type;
  2686. $componentList['componentData']=(object)[];
  2687. $ad = json_decode($value['ad']) ?? [];
  2688. if(is_object($ad)){
  2689. $ad->thumb = $value['img_url'];
  2690. }else{
  2691. $ad['thumb'] = $value['img_url'];
  2692. }
  2693. $comlist_key[$array_key][$sector_id]['ad'] =is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
  2694. // 在 $page[$key][$setor_id] 中增加一个键值为 ad 的数组
  2695. // $page[$key][$setor_id]['ad'] = is_object($ad) ? (array)$ad->ad ?? [] : $ad['ad'] ?? [];
  2696. }else if($value['type_id'] == 14){
  2697. $type = 1;
  2698. $componentList['component_type'] = $type;
  2699. if($data['page_type'] == 1){
  2700. $component_data->componentData->name = '请选择导航..';
  2701. }
  2702. $componentData = $component_data->componentData;
  2703. $componentList['componentData'] = is_object($componentData) ? (array)$componentData ?? [] : $componentData ?? [];
  2704. $componentList['componentData']['listType'] = $listType->listType;
  2705. $ad = json_decode($value['ad']) ?? [];
  2706. $comlist_key[$array_key][$sector_id]['ad'] =is_object($ad) ? (array)$ad ?? [] : $ad ?? [];
  2707. }else{
  2708. }
  2709. if( in_array($sector_id,$sectors_key)){
  2710. $comlist_key[$array_key][$sector_id]['componentList'][$num-1] = $componentList;
  2711. $comlist_key[$array_key][$sector_id]['componentList'] = array_values($comlist_key[$array_key][$sector_id]['componentList']);
  2712. }else{
  2713. $comlist_key[$array_key][$sector_id]['componentList'] = $componentList;
  2714. }
  2715. $keys[$key] = $key;
  2716. }
  2717. // return Result::success($sector_arr_key);
  2718. // $page[$keys] = array_column($page,$sectors_key[0]);
  2719. $comlist_key = array_values($comlist_key);
  2720. // $sector_page = json_encode($comlist_key);
  2721. if(empty($comlist_key)){
  2722. return Result::error('通栏不存在!');
  2723. }
  2724. return Result::success($comlist_key);
  2725. // return Result::success(gettype($listType));
  2726. }
  2727. /**
  2728. * 自助建站-组件管理-获取组件预览图列表
  2729. */
  2730. public function getComponentImgList(array $data): array
  2731. {
  2732. $where['component_img.component_id'] = $data['component_id'];
  2733. if(isset($data['template_id']) && !empty($data['template_id'])){
  2734. $where['component_img.template_id'] = $data['template_id'];
  2735. }
  2736. $component_img = ComponentImg::where($where)
  2737. ->leftJoin('template','component_img.template_id','=','template.template_id')
  2738. ->leftJoin('component','component_img.component_id','=','component.component_type')
  2739. ->select('component_img.*','template.template_name','component.component_name')
  2740. ->orderBy('component_img.template_id')
  2741. ->orderBy('component_img.img_id')
  2742. ->paginate($data['page_size'], ['*'], 'page', $data['page']);
  2743. if($component_img->isEmpty()){
  2744. return Result::error('组件预览图不存在!');
  2745. }
  2746. $component_img = [
  2747. 'data' => $component_img->items(),
  2748. 'total' => $component_img->total(),
  2749. ];
  2750. return Result::success($component_img);
  2751. }
  2752. /**
  2753. * 自助建站-组件管理-添加组件预览图
  2754. */
  2755. public function addComponentImg(array $data): array
  2756. {
  2757. unset($data['user_id']);
  2758. $component = Component::where('component_type',$data['component_id'])->first();
  2759. if(empty($component)){
  2760. return Result::error('组件不存在!');
  2761. }
  2762. if($component['type_id'] == 11){
  2763. $img = ComponentImg::where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2764. if(!empty($img)){
  2765. return Result::error('该组件已存在此皮肤的预览图!');
  2766. }
  2767. }
  2768. $component = ComponentImg::where('template_id',$data['template_id'])->where('component_id',$data['component_id'])->where('img_id',$data['img_id'])->first();
  2769. if(!empty($component)){
  2770. return Result::error('组件预览图编号已存在!');
  2771. }
  2772. $component_img = ComponentImg::insertGetId($data);
  2773. if(empty($component_img)){
  2774. return Result::error('添加失败!');
  2775. }
  2776. return Result::success($component_img);
  2777. }
  2778. /**
  2779. * 自助建站-组件管理-删除组件预览图
  2780. */
  2781. public function delComponentImg(array $data): array
  2782. {
  2783. $component_img = ComponentImg::where('id',$data['id'])->delete();
  2784. if(empty($component_img)){
  2785. return Result::error('删除失败!');
  2786. }
  2787. return Result::success($component_img);
  2788. }
  2789. /**
  2790. * 自助建站-组件管理-更新组件预览图
  2791. */
  2792. public function updateComponentImg(array $data): array
  2793. {
  2794. $component = Component::where('component_type',$data['component_id'])->first();
  2795. if(empty($component)){
  2796. return Result::error('组件不存在!');
  2797. }
  2798. if($component['type_id'] == 11){
  2799. $img = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->get()->all();
  2800. if(!empty($img)){
  2801. return Result::error('该组件已存在此皮肤的预览图!');
  2802. }
  2803. }
  2804. $component = ComponentImg::where('id','!=',$data['id'])->where('component_id',$data['component_id'])->where('template_id',$data['template_id'])->where('img_id',$data['img_id'])->first();
  2805. if(!empty($component)){
  2806. return Result::error('组件预览图编号已存在!');
  2807. }
  2808. // return Result::success($component);
  2809. unset($data['user_id']);
  2810. // unset($data['updated_at']);
  2811. $id = $data['id'];
  2812. unset($data['id']);
  2813. // return Result::success($data);
  2814. $component_img = ComponentImg::where('id',$id)->update($data);
  2815. if(empty($component_img)){
  2816. return Result::error('更新失败!');
  2817. }
  2818. return Result::success($component_img);
  2819. }
  2820. /**
  2821. * 自助建站-获取通栏类型
  2822. */
  2823. public function getSectorType(array $data): array
  2824. {
  2825. $sector = SectorType::get()->all();
  2826. if(empty($sector)){
  2827. return Result::error('通栏类型不存在!');
  2828. }
  2829. return Result::success($sector);
  2830. }
  2831. /**
  2832. * 自助建站-随机获取模板
  2833. */
  2834. public function randomWebTemplate(array $data): array
  2835. {
  2836. $web = Website::where('id',$data['website_id'])->first();
  2837. if(empty($web)){
  2838. return Result::error('此网站不存在!');
  2839. }
  2840. $template = Template:: where('template_id',$data['template_id'])->first();
  2841. if(empty($template)){
  2842. return Result::error('此皮肤不存在!');
  2843. }
  2844. $rule = TemplateRule::first();
  2845. switch ($data['page']) {
  2846. case 1:
  2847. $index_rule = json_decode($rule['index_rule']);
  2848. break;
  2849. case 2:
  2850. $index_rule = json_decode($rule['class_rule']);
  2851. break;
  2852. case 3:
  2853. $index_rule = json_decode($rule['list_rule']);
  2854. break;
  2855. case 4:
  2856. $index_rule = json_decode($rule['info_rule']);
  2857. break;
  2858. case 5:
  2859. $index_rule = json_decode($rule['search_rule']);
  2860. break;
  2861. case 6:
  2862. $index_rule = json_decode($rule['footerlist_rule']);
  2863. break;
  2864. case 7:
  2865. $index_rule = json_decode($rule['footerinfo_rule']);
  2866. break;
  2867. default:
  2868. break;
  2869. }
  2870. // return Result::success($index_rule);
  2871. $tempalte['index'] = $this->randomPage($data,$data['page'],$index_rule);
  2872. // $tempalte['class'] = $this->randomPage($data,2,$class_rule);
  2873. // $tempalte['list'] = $this->randomPage($data,3);
  2874. // $tempalte['article'] = $this->randomPage($data,4);
  2875. // $tempalte['search'] = $this->randomPage($data,5);
  2876. // $tempalte['aloneList'] = $this->randomPage($data,6);
  2877. // $tempalte['aloneArticle'] = $this->randomPage($data,7);
  2878. // $index = $this->randomPage($data);
  2879. return Result::success($tempalte);
  2880. }
  2881. public function randomPage($data,$page,$rule){
  2882. // return $rule;
  2883. $sectors = Sector::where('sector.template_id',$data['template_id'])
  2884. ->where('sector.page_type','like','%'.$page.'%')
  2885. ->select('sector.sector_name', 'sector.sector_id','sector.pic_height','sector.sector_img',
  2886. 'sector.sector_type','sector.sector_code','sector.id','sector.sector_width')
  2887. ->orderBy('id')
  2888. ->get()->all();
  2889. if(empty($sectors)){
  2890. return Result::error('通栏不存在!');
  2891. }
  2892. // return $sectors;
  2893. // 获取每个类别的通栏数量
  2894. $type_num = array_count_values(array_column($sectors,'sector_type'));
  2895. // 此页面通栏的所有类别
  2896. $all_sector_types = array_keys($type_num);
  2897. //获取每个通栏类别在其中的位置 -------暂时不考虑先,应该是必需通栏类型或者非必需通栏类型,每个类型应该取一个,其余取可重复的通栏
  2898. $sector_type_keys = array_column($sectors,'sector_type');
  2899. // 将数组处理为二维数组,值相同的合并为同一个数组,保持原有键值
  2900. $sector_types = array_reduce(array_keys($sector_type_keys), function ($result, $key) use ($sector_type_keys) {
  2901. $value = $sector_type_keys[$key];
  2902. if (!isset($result[$value])) {
  2903. $result[$value] = [];
  2904. }
  2905. $result[$value][$key] = $key;
  2906. $result[$value] = array_values($result[$value]);
  2907. return $result;
  2908. }, []);
  2909. // return $sector_types;
  2910. $must_type = $rule->must_type;
  2911. $sector_num = $rule->max_num;
  2912. // 获取必需通栏类别与查询到的通栏类别的交集数量;
  2913. $is_must = count(array_intersect($must_type, $all_sector_types));
  2914. // $is_must交集数量若是等于必需通栏类别的数量相等则不缺必需通栏
  2915. if( $is_must != count($must_type)){
  2916. return '此页面缺少必要通栏!';
  2917. }
  2918. // var_dump("sector_types",$all_sector_types);
  2919. // var_dump("must_type",$must_type);
  2920. // return $must_type;
  2921. // 非必须通栏的随机处理
  2922. $not_must_type = array_diff($all_sector_types,$must_type);
  2923. $total = count($not_must_type);
  2924. $randomCount = mt_rand(0, $total); // 可能为0(无元素)或任意数量,最多等于数组长度
  2925. $and_type = $rule->and_type ?? [];
  2926. // return $not_must_type;
  2927. if ($randomCount === 0) {
  2928. $random_sector = [];
  2929. $rand_count = 0;
  2930. } else {
  2931. // 随机获取指定数量的键名(保留原键)
  2932. $randomKeys = array_rand($not_must_type, $randomCount);
  2933. // 处理单元素情况(array_rand返回字符串,需转为数组)
  2934. if (!is_array($randomKeys)) {
  2935. $randomKeys = [$randomKeys];
  2936. }
  2937. // 通栏关联性规则暂时无法实现---------------
  2938. // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
  2939. // 判断 $oneDimensionalArray 是否为一维数组
  2940. // if (isset($and_type) && is_array($and_type) && !array_is_list(array_filter($and_type, 'is_array'))) {
  2941. // 判断是否存在有关联通栏的交集
  2942. // if (isset($and_type) && !empty($and_type) && count(array_intersect($and_type, $randomKeys)) > 0) {
  2943. // // var_dump("11111",$and_type);
  2944. // // var_dump("22222",$randomKeys);
  2945. // // 将一维数组中的值都放到随机数组中
  2946. // $randomKeys = array_merge($randomKeys, $and_type);
  2947. // }
  2948. // var_dump(count(array_intersect($and_type, $randomKeys)));
  2949. // }
  2950. // -----------------------------
  2951. // 根据随机键名提取元素(保留原键值关系)
  2952. // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
  2953. // var_dump("randomKeys",$randomKeys);
  2954. $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
  2955. $rand_count = count($random_sector);
  2956. }
  2957. // var_dump("randomKeys",$randomKeys);
  2958. // var_dump("random_sector",$random_sector);
  2959. var_dump("rand_count",$rand_count);
  2960. // return $random_sector;
  2961. // 必需通栏
  2962. $must_type = array_intersect($all_sector_types,$must_type);
  2963. // return $must_type;
  2964. // 此代码用于从 $sectors 数组中筛选出必需的通栏数据。
  2965. // 1. array_keys($must_type) 获取 $must_type 数组的所有键名
  2966. // 2. array_flip() 将这些键名作为值,生成一个新数组
  2967. // 3. array_intersect_key() 比较 $sectors 和上述新数组的键名,返回键名相同的元素
  2968. // 4. array_values() 重新索引返回的数组,使其键名从 0 开始连续递增
  2969. // 最终得到的 $must_sector 数组包含了 $sectors 中对应 $must_type 键名的所有必需通栏数据
  2970. $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
  2971. $must_count = count($must_sector);
  2972. // var_dump("must_sector",$must_sector);
  2973. // var_dump("must_count",$must_count);
  2974. // return $must_sector;
  2975. // 计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
  2976. $repeat_num = $sector_num-$rand_count-$must_count;
  2977. // 组合起来通栏
  2978. $sector_zuhe1 = array_merge($must_sector,$random_sector);
  2979. $sector_ids = array_column($sector_zuhe1, 'id');
  2980. if($repeat_num > 0){
  2981. $rule_repeat = (array)$rule->type_max;
  2982. $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
  2983. // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
  2984. foreach($rule_repeat as $key => $value){
  2985. if(in_array($key,array_keys($sector_zuhe1_type))){
  2986. $rule_repeat[$key] = $value-1;
  2987. }
  2988. }
  2989. var_dump("repeat_num",$repeat_num);
  2990. // return array_intersect($sector_zuhe1,$must_sector);
  2991. if(count(array_intersect($sector_zuhe1,$must_sector)) != count($must_sector)){
  2992. return '此页面缺少必要通栏!';
  2993. }
  2994. // 剩余通栏进行重复处理
  2995. // $rule_repeat = (array)$rule->type_max;
  2996. // 取到可以重复的通栏类别
  2997. $rule_repaet_type = array_keys($rule_repeat);
  2998. $repeat_type = array_intersect_key($sector_types, array_flip($rule_repaet_type));
  2999. // 获取可以随机重复的通栏
  3000. $repaet_key = 0;
  3001. $num = 0;
  3002. $type = [];
  3003. // return $rule_repaet_type;
  3004. // 防止无限循环,设置最大迭代次数
  3005. $max_iterations = 20;
  3006. $iteration_count = 0;
  3007. // return [
  3008. // 'repaet_key' => $repaet_key,
  3009. // 'repeat_num' => $repeat_num,
  3010. // 'rule_repeat' => $rule_repeat,
  3011. // 'repeat_type' => $repeat_type,
  3012. // 'must_type' => $must_type,
  3013. // ];
  3014. while($repaet_key < $repeat_num && $iteration_count < $max_iterations){
  3015. $num++;
  3016. foreach($repeat_type as $key => $value){
  3017. if(!isset($type[$key])){
  3018. $type[$key] = 0;
  3019. }else{
  3020. $type[$key]++;
  3021. }
  3022. // $type代表每个类型的通栏数量,每个类型的通栏的数量应该≤规则限制的数量
  3023. if($repaet_key < $repeat_num && $type[$key] <= $rule_repeat[$key]){
  3024. // $max_repaert_num = $rule_repeat[$key];
  3025. // if($max_repaert_num < $num ){
  3026. // // $arr_key = $num+$key;
  3027. if (count($value) > 1) {
  3028. // 若 可以随机的通栏类型存在多个通栏,随机取其一
  3029. $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
  3030. } else {
  3031. $repeat_sector_key[$repaet_key] = $value[0];
  3032. }
  3033. $type[$repaet_key] = $repeat_sector_key[$repaet_key];
  3034. $repaet_key++;
  3035. // }
  3036. }
  3037. // else{
  3038. // break;
  3039. // }
  3040. }
  3041. // var_dump("num", $type[$key]);
  3042. // var_dump("repeat_sector_key", $repeat_sector_key);
  3043. $iteration_count++;
  3044. }
  3045. // return $repeat_sector_key;
  3046. // var_dump("num", $num);
  3047. // var_dump("repeat_sector_key", $repeat_sector_key);
  3048. // 随机的重复通栏键值转换成真正的通栏
  3049. $repeat_sector = [];
  3050. if(isset($repeat_sector_key) && !empty($repeat_sector_key) && is_array($repeat_sector_key)){
  3051. foreach ($repeat_sector_key as $key => $value) {
  3052. if (isset($sectors[$value])) {
  3053. $repeat_sector[$key] = $sectors[$value];
  3054. }
  3055. }
  3056. }
  3057. $repeat_count = count($repeat_sector);
  3058. var_dump("repeat_count",$repeat_count);
  3059. // var_dump("repeat_sector", $repeat_sector);
  3060. // return ($data['page'] == '2' || $data['page'] == 2);
  3061. $sector = array_merge($repeat_sector,$sector_zuhe1);
  3062. }else{
  3063. $sector = $sector_zuhe1;
  3064. }
  3065. // var_dump("sector_ids",$sector_ids);
  3066. // return [
  3067. // 'random_sector'=>$random_sector,
  3068. // 'must_sector'=>$must_sector,
  3069. // 'sector_ids'=>$sector_ids,
  3070. // ];
  3071. // 各类型通栏最大数量数量限制 = 规则最大数量-已经存在的通栏数量(目前的通栏不会有重复的,所以只是-1)
  3072. // var_dump("sector",$sector);
  3073. // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
  3074. $sector_specal_sort = array_column($sector,'sector_type');
  3075. if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort) || in_array(5,$sector_specal_sort)){
  3076. $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
  3077. $pic_key = array_search(7,$sector_specal_sort) ?? null;
  3078. $cat_key = array_search(5,$sector_specal_sort) ?? null;
  3079. $toutiao = $sector[$toutiao_key];
  3080. $pic = $sector[$pic_key];
  3081. $cat = $sector[$cat_key];
  3082. $sort_1 = $sector[0];
  3083. $sort_2 = $sector[1];
  3084. $sort_3 = $sector[2];
  3085. // return $sector_specal_sort;
  3086. // return [
  3087. // 'sector_ids' => array_column($sector_zuhe1,'id'),
  3088. // // 'repeat_sector' => $repeat_sector,
  3089. // 'sector_id' => array_column($sector,'id'),
  3090. // 'array_intersect' => count(array_intersect($sector,$must_sector)),
  3091. // 'old' => [ $toutiao_key,$pic_key,$cat_key],
  3092. // 'new' => [ $sort_1,$sort_2,$sort_3]
  3093. // ];
  3094. if($data['page'] == 1 || $data['page'] == '1'){
  3095. if($toutiao_key){
  3096. $sector[0] = $toutiao;
  3097. $sector[$toutiao_key] = $sort_1;
  3098. $sector[1] = $pic;
  3099. $sector[$pic_key] = $sort_2;
  3100. }
  3101. if($cat_key){
  3102. $sector[0] = $pic;
  3103. $sector[$pic_key] = $sort_1;
  3104. }
  3105. }
  3106. if($data['page'] == '2' || $data['page'] == 2 || in_array(5,$sector_specal_sort)){
  3107. $sector[0] = $cat;
  3108. $sector[$cat_key] = $sort_1;
  3109. if($toutiao_key && $pic_key){
  3110. $sector[1] = $toutiao;
  3111. $sector[$toutiao_key] = $sort_2;
  3112. $sector[2] = $pic;
  3113. $sector[$pic_key] = $sort_3;
  3114. }
  3115. if($pic_key){
  3116. $sector[1] = $pic;
  3117. $sector[$pic_key] = $sort_2;
  3118. }
  3119. }
  3120. }
  3121. $sector_id = array_column($sector,'id');
  3122. // if(count(array_intersect($sector,$must_sector)) != count($must_sector)){
  3123. // return '此页面缺少必要通栏!';
  3124. // }
  3125. $data['page'] = $page;
  3126. $component[] = $this->randomComponent($sector_id,$data,$sector);
  3127. var_dump("sector_id",$sector_id);
  3128. return ['sector_id'=>$sector_id,
  3129. // 'sector'=>$sector,
  3130. 'component'=>$component,
  3131. // 'sector'=>$sector,
  3132. ];
  3133. }
  3134. public function randomComponent($sector_id,$data,$sector){
  3135. $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
  3136. ->leftJoin('component','component.component_type','sector_component.component_id')
  3137. ->leftJoin('component_img','component_img.component_id','component.component_type')
  3138. ->where('component_img.template_id','=',$data['template_id'])
  3139. ->select('component.*','component_img.img_name','component_img.img_id','component_img.img_url',
  3140. 'sector_component.sort_id','sector_component.sectorid')
  3141. ->orderBy('sector_component.sort_id')
  3142. ->orderBy('component.component_type')
  3143. ->get();
  3144. // 按sectorid和sort_id分组数据
  3145. $groupedResults = [];
  3146. $sort_ids = [];
  3147. $sector_ids = [];
  3148. $component_list = [];
  3149. $component_ids = [];
  3150. $img_ids = [];
  3151. $sort_key = 0;
  3152. // return $rawResults;
  3153. // $groupedResults重构数组,将相同位置的组件合并,相同组件的样式合并(已经包括所有选择的组件及组件样式)
  3154. // $sort_component = array_column($rawResults,'sort_id');
  3155. foreach ($rawResults as $key => $item) {
  3156. $sectorId = $item->sectorid;
  3157. $sortId = $item->sort_id;
  3158. $componentId = $item->component_type;
  3159. $component_data = $item->component_data;
  3160. $component_column = $item->component_column;
  3161. $component_type = $item->type_id;
  3162. $component_ad = $item->ad;
  3163. $imgId = $item->img_id;
  3164. if (!isset($groupedResults[$sectorId])) {
  3165. $groupedResults[$sectorId] = [];
  3166. }
  3167. $componentData = !empty($component_data) && is_string($component_data) ? json_decode($component_data, true) : [];
  3168. $component_listtype = !empty($component_column) && is_string($component_column) ? json_decode($component_column, true) : [];
  3169. $component_code = !empty($component_code) && is_string($component_code) ? json_decode($component_code, true) : [];
  3170. $component_ad = !empty($component_ad) && is_string($component_ad) ? json_decode($component_ad, true) : [];
  3171. // 确保 $componentData['componentData'] 是数组,避免 array_push 传入 null
  3172. if (!isset($componentData['componentData']) || !is_array($componentData['componentData']) ) {
  3173. $componentData['componentData'] = [];
  3174. }
  3175. $componentData['componentData'] += $component_listtype;
  3176. $component_code = $componentData;
  3177. $component_list[$key] = $component_code;
  3178. // $component_list[$key]['listType'] = $component_listtype;
  3179. // }
  3180. $sort_id = intval($sortId-1);
  3181. // if(!isset($groupedResults[$sectorId][$sort_id])) {
  3182. // // $groupedResults[$sectorId][$sortId] = [];
  3183. // $com_key = 0;
  3184. // $groupedResults[$sectorId][$sort_id][$com_key] = [
  3185. // 'component_type' => $item->component_type,
  3186. // 'type_id' => $component_type,
  3187. // 'sort' => $sortId,
  3188. // 'width' => $item->component_width,
  3189. // 'height' => $item->component_height,
  3190. // 'images' => [
  3191. // 'img_name' => $item->img_name,
  3192. // 'img_id' => $item->img_id,
  3193. // 'img_url' => $item->img_url,
  3194. // 'component_type' => $component_type,
  3195. // ]
  3196. // ];
  3197. // // 检查 $sectorId 和 $sort_id 是否为合法的数组键类型(字符串或整数)
  3198. // }else{
  3199. // if((isset($component_ids) && in_array($componentId,$component_ids))){
  3200. // if(isset($img_arr)){
  3201. // $img_arr++;
  3202. // }else{
  3203. // $img_arr = 0;
  3204. // if(isset($com_key)){
  3205. // $com_key++;
  3206. // }
  3207. // }
  3208. // // $com_id_key = array_search($componentId,$component_ids);
  3209. // $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_arr] = [
  3210. // 'img_name' => $item->img_name,
  3211. // 'img_id' => $item->img_id,
  3212. // 'img_url' => $item->img_url,
  3213. // 'component_type' => $component_type,
  3214. // ];
  3215. // }else{
  3216. // $img_arr = 0;
  3217. // $com_key = $com_key ?? 0;
  3218. // $groupedResults[$sectorId][$sort_id][$com_key] = [
  3219. // 'component_type' => $item->component_type,
  3220. // 'type_id' => $component_type,
  3221. // 'sort' => $sortId,
  3222. // 'width' => $item->component_width,
  3223. // 'height' => $item->component_height,
  3224. // 'images' =>
  3225. // [
  3226. // $img_arr =>
  3227. // [
  3228. // 'img_name' => $item->img_name,
  3229. // 'img_id' => $item->img_id,
  3230. // 'img_url' => $item->img_url,
  3231. // 'component_type' => $component_type,
  3232. // ]
  3233. // ]
  3234. // ];
  3235. // }
  3236. // }
  3237. // $component = $groupedResults[$sectorId][$sort_id];
  3238. // if(count($component) > 1){
  3239. // $com_key = 0;
  3240. // }
  3241. // if(!in_array($sectorId,$sector_ids)){
  3242. // $sort_key = 0;
  3243. // $sector_ids[$sectorId][$sort_key] = $sectorId;
  3244. // }else{
  3245. // $sort_key++;
  3246. // $sector_ids[$sectorId][$sort_key] = $sectorId;
  3247. // }
  3248. if(!isset($com_key) || !in_array($sectorId,$sector_ids) || !isset($sort_ids[$sectorId]) || !in_array($sortId, $sort_ids[$sectorId])){
  3249. $com_key = 0;
  3250. // if(!in_array($componentId,$component_ids)){
  3251. // $img_key = 0;
  3252. // }
  3253. $img_key = 0;
  3254. }else if(in_array($sortId,$sort_ids[$sectorId]) && in_array($sectorId, $sector_ids) && !in_array($componentId,$component_ids)){
  3255. $com_key++;
  3256. }
  3257. if(in_array($componentId,$component_ids) && !in_array($imgId,$img_ids[$componentId])){
  3258. $img_key++;
  3259. }else{
  3260. $img_key=0;
  3261. }
  3262. if(!in_array($componentId,$component_ids)){
  3263. $groupedResults[$sectorId][$sort_id][$com_key] = [
  3264. 'component_type' => $item->component_type,
  3265. 'type_id' => $component_type,
  3266. 'sort' => $sortId,
  3267. 'width' => $item->component_width,
  3268. 'height' => $item->component_height,
  3269. ];
  3270. }
  3271. $groupedResults[$sectorId][$sort_id][$com_key]['images'][$img_key] = [
  3272. 'img_name' => $item->img_name,
  3273. 'img_id' => $imgId,
  3274. 'img_url' => $item->img_url,
  3275. 'component_type' => $componentId,
  3276. ];
  3277. // $component_type[$key] = $groupedResults[$sectorId][$sort_id]['component_type'];
  3278. // $sort_com = $groupedResults[$sectorId][$sort_id]['component_type'];
  3279. // if(count($sort_com) == 1){
  3280. // $com_key = 0;
  3281. // }
  3282. // // 组件分类:1.头条资讯;2.轮播图资讯;3.推荐图;4.最新资讯;5.推荐资讯;6.热点资讯;7.栏目资讯;8.列表类;9.详情类;
  3283. // // 10.二级导航类;11.广告类;12.静态类;13.单页导航类;14.资讯广告混合类;15.滚动图文类;16.搜索框类;17.单页列表类;18.单页详情类;
  3284. if (is_scalar($sectorId) && is_scalar($sort_id)) {
  3285. if(!in_array($component_type,[10,12,16])){
  3286. if($component_type == 11 || $component_type == 14){
  3287. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad'] = $component_ad;
  3288. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['ad']['website_id'] = $data['website_id'];
  3289. }
  3290. if($component_type != 11){
  3291. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = $component_code['componentData'];
  3292. }
  3293. }else{
  3294. $groupedResults[(int)$sectorId][(int)$sort_id][$com_key]['componentData'] = [];
  3295. }
  3296. } else {
  3297. // 处理非法键类型的情况,可根据实际需求修改错误处理逻辑
  3298. trigger_error('Illegal offset type for $sectorId or $sort_id', E_USER_WARNING);
  3299. }
  3300. // // 判断此组件是否存在数组中的依据(若是有重复的组件,一定是组件样式重复)
  3301. $component_ids[$key] = $componentId;
  3302. if (!isset($sort_ids[$sectorId])) {
  3303. $sort_ids[$sectorId] = [];
  3304. }
  3305. if (!in_array($sortId, $sort_ids[$sectorId])) {
  3306. $sort_ids[$sectorId][] = $sortId;
  3307. }
  3308. if (!isset($img_ids[$componentId])) {
  3309. $img_ids[$componentId] = [];
  3310. }
  3311. if (!in_array($imgId, $img_ids[$componentId])) {
  3312. $img_ids[$componentId][] = $imgId;
  3313. }
  3314. $sector_ids[$key] = $sectorId;
  3315. // $img_ids[$componentId] = $imgId;
  3316. }
  3317. // return $groupedResults;
  3318. $result = [];
  3319. // return $sectors;
  3320. $y_num = 0;
  3321. foreach ($sector as $key => $value) {
  3322. // array_push($value['sort_id'],$key);
  3323. // $sector_id_ = $value['sector_id'];
  3324. $sectorid = $value['id'];
  3325. $template_data[$key] = [
  3326. 'sectorName' => $value['sector_id'],
  3327. 'sort' => $key+1,
  3328. ];
  3329. $date = date('mdHisu' ); // 获取时分秒微秒,转换为纯数字格式
  3330. // $date = 11010630021;
  3331. if(substr($date,0,1) == 0){
  3332. $date = substr($date, 0, 10); // 截取前9位
  3333. }else{
  3334. $date = substr($date, 0, 9); // 截取前9位
  3335. }
  3336. $random_num = intval(intval($date) . rand(1000, 9999)); // 拼接4位随机数,组成13位随机数
  3337. $y_num = $y_num + $value['pic_height'];
  3338. $sector_width = $value['sector_width']/100;
  3339. $canvas_data[$key] = [
  3340. 'i' => $random_num,
  3341. 'x' => 0,
  3342. 'y' => $y_num,
  3343. 'w' => $sector_width,
  3344. 'h' => $value['pic_height'],
  3345. 'type' => $value['sector_id'],
  3346. 'content' => [
  3347. 'sectorName' => $value['sector_id'],
  3348. ]
  3349. ];
  3350. // $sector_key[$key] = array_keys($value );
  3351. $sector_component = [];
  3352. if(isset($groupedResults[$sectorid]) && !empty($groupedResults[$sectorid]) && is_array($groupedResults[$sectorid])){
  3353. foreach ($groupedResults[$sectorid] as $sortId => $components) {
  3354. // 通栏某一位置的随机组件
  3355. if(count($components) > 1){
  3356. $componentIds = count($components);
  3357. $selectedComponentId = rand(0, $componentIds - 1);
  3358. }else{
  3359. $selectedComponentId = 0;
  3360. }
  3361. // var_dump($components[$selectedComponentId]);
  3362. // $Component = $components[$selectedComponentId];
  3363. $Component['component_type'] = $components[$selectedComponentId]['component_type'] ?? 0;
  3364. $Component['sort'] = $components[$selectedComponentId]['sort'] ?? 1;
  3365. $Component['type_id'] = $components[$selectedComponentId]['type_id'] ?? 0;
  3366. // 通栏某一位置的随机组件样式图
  3367. // 检查 $Component['images'] 是否存在且为数组
  3368. if (isset($components[$selectedComponentId]['images']) && is_array($components[$selectedComponentId]['images'])) {
  3369. $images = count($components[$selectedComponentId]['images']);
  3370. $selectedImage = 0;
  3371. if ($images > 1) {
  3372. $selectedImage = rand(0, $images - 1);
  3373. }
  3374. if (isset($components[$selectedComponentId]['images'][$selectedImage])) {
  3375. $Component['component_style'] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage]->img_id : $components[$selectedComponentId]['images'][$selectedImage]['img_id'];
  3376. }
  3377. }
  3378. $image_info[$key] = is_object($components[$selectedComponentId]['images'][$selectedImage]) ? $components[$selectedComponentId]['images'][$selectedImage] : $components[$selectedComponentId]['images'][$selectedImage];
  3379. // var_dump($images);
  3380. $Component['componentData'] = $components[$selectedComponentId]['componentData'] ?? [];
  3381. if($data['page'] == 1 &&(isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15])) ){
  3382. $Component['componentData']['name'] = '请选择栏目';
  3383. }
  3384. if($data['page'] == 2 && (isset($components[$selectedComponentId]['type_id']) && in_array($components[$selectedComponentId]['type_id'],[7,14,15]))){
  3385. $Component['componentData']['name'] = '自动生成';
  3386. }
  3387. if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 11){
  3388. $ad = $components[$selectedComponentId]['ad'];
  3389. $ad['thumb'] = $image_info[$key]['img_url'];
  3390. $template_data[$key]['ad'] = $ad;
  3391. $canvas_adinfo[$key] = $ad;
  3392. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3393. // // 确保 $ad 和 $canvas_adinfo[$key] 元素数量一致,直接组合
  3394. // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3395. $Component['componentData'] = [];
  3396. }
  3397. if(isset($components[$selectedComponentId]['type_id']) && $components[$selectedComponentId]['type_id'] == 14){
  3398. $ad = $components[$selectedComponentId]['ad'];
  3399. // $ad['thumb'] = $image_info[$key]['img_url'];
  3400. $template_data[$key]['ad'] = $ad;
  3401. $canvas_adinfo[$key] = $ad;
  3402. $canvas_data[$key]['content']['ad'] = $canvas_adinfo[$key];
  3403. // // $canvas_data[$key]['ad'] = array_combine($ad, $canvas_adinfo[$key]);
  3404. // // $Component['componentData'] = [];
  3405. }
  3406. // var_dump('---------------------------',$components[$selectedComponentId]);
  3407. $sector_component[$sortId] = $Component;
  3408. // $canvas_component[$sortId] = $Component;
  3409. // $sector_component[$sortId] = $selectedComponentId;
  3410. }
  3411. // var_dump("image",$image);
  3412. }
  3413. $template_data[$key]['componentList'] = $sector_component;
  3414. $canvas_data[$key]['content']['componentList'] = $sector_component;
  3415. // $canvas_data[$key]['ad'] = $key;
  3416. $canvas_data[$key]['dataSort'] = $key;
  3417. $canvas_data[$key]['moved'] = false;
  3418. // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
  3419. }
  3420. // return $canvas_data;
  3421. return [
  3422. 'template_data' => $template_data,
  3423. 'canvas_data' => $canvas_data,
  3424. ];
  3425. }
  3426. }