PublicRpcService.php 161 KB

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