PublicRpcService.php 122 KB

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