PublicRpcService.php 146 KB

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