WebsiteService.php 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\Ad;
  5. use App\Model\Category;
  6. use App\Model\AdPlace;
  7. use App\Model\Department;
  8. use App\Model\District;
  9. use App\Model\FooterCategory;
  10. use App\Model\FooterContent;
  11. use App\Model\WebsiteTemplateInfo;
  12. use App\Model\Link;
  13. use App\Model\LetterOfComplaint;
  14. use App\Model\TemplateClass;
  15. use App\Model\Template;
  16. use App\Model\User;
  17. use App\Model\UserInfo;
  18. use App\Model\WebsiteRole;
  19. use App\Model\WebsiteRoleUser;
  20. use App\Model\Website;
  21. use App\Model\WebsiteColumn;
  22. use App\Model\Good;
  23. use App\Model\Book;
  24. use App\Model\JobRecruiting;
  25. use App\Model\JobHunting;
  26. use App\Model\Notice;
  27. use App\Model\Complaint;
  28. use App\Model\Order;
  29. use App\Model\WebsiteImg;
  30. use Hyperf\HttpServer\Contract\RequestInterface;
  31. use Hyperf\DbConnection\Db;
  32. use Hyperf\RpcServer\Annotation\RpcService;
  33. use App\Tools\Result;
  34. use Carbon\Carbon;
  35. use App\Model\WebsiteCategory;
  36. use App\Model\WebsiteGroup;
  37. use App\Model\WebsiteTemplate;
  38. use App\Model\Sector;
  39. use PhpParser\Node\Stmt\Return_;
  40. use function PHPUnit\Framework\isNull;
  41. use Overtrue\Pinyin\Pinyin;
  42. use App\Tools\buildTree;
  43. use App\Model\WhiteRouter;
  44. use App\Model\Size;
  45. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  46. class WebsiteService implements WebsiteServiceInterface
  47. {
  48. /**
  49. * 获取网站列表
  50. * @param string $keyword
  51. * @param int $page
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getWebsitetList(array $data): array
  56. {
  57. // 构建查询
  58. $query = Website::when($data, function ($query) use ($data) {
  59. if (isset($data['keyword']) && !empty($data['keyword'])) {
  60. $query->where('website.website_name', 'like', '%' . trim($data['keyword']) . '%');
  61. }
  62. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  63. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  64. }
  65. if (isset($data['city_id']) && !empty($data['city_id'])) {
  66. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  67. }
  68. });
  69. // 执行查询并获取总数和结果
  70. $queryWithJoins = $query->leftJoin("website_column", "website.website_column_id", "website_column.id")
  71. ->leftJoin("district", "district.id", "website.city_id");
  72. $count = $queryWithJoins->count();
  73. $result = $queryWithJoins->select("website.*", "website_column.column_name", "district.name as city_name")
  74. ->limit($data['pageSize'])
  75. ->offset(($data['page'] - 1) * $data['pageSize'])
  76. ->orderBy("website.updated_at", "desc")
  77. ->get();
  78. if (empty($result)) {
  79. return Result::error("没有数据", 0);
  80. }
  81. $data = [
  82. 'rows' => $result->toArray(),
  83. 'count' => $count,
  84. ];
  85. return Result::success($data);
  86. }
  87. /**
  88. * @param array $data
  89. * @return array
  90. */
  91. public function createWebsite(array $data): array
  92. {
  93. var_dump("网站数据:", $data);
  94. $insertData = [
  95. 'website_name' => $data['website_name'],
  96. 'logo' => $data['logo'] ?? '',
  97. 'website_url' => $data['website_url'] ?? '',
  98. // 'city_id'=>$data['city_id']??0,
  99. 'website_column_id' => $data['website_column_id'],
  100. 'title' => $data['title'] ?? '',
  101. 'keywords' => $data['keywords'] ?? '',
  102. 'description' => $data['description'] ?? '',
  103. 'status' => $data['status'] ?? 1,
  104. 'website_column_arr_id' => $data['website_column_arr_id'],
  105. // 'city_arr_id'=>$data['city_arr_id']??[0],
  106. 'template_id' => $data['template_id'] ?? 0,
  107. 'suffix' => $data['suffix'] ?? "",
  108. ];
  109. $result = Website::insertGetId($insertData);
  110. if (empty($result)) {
  111. return Result::error("创建失败", 0);
  112. } else {
  113. return Result::success(["id" => $result]);
  114. }
  115. }
  116. /**
  117. * @param int $id
  118. * @param array $data
  119. * @return array
  120. */
  121. public function updateWebsite(int $id, array $data): array
  122. {
  123. $insertData = [
  124. 'website_name' => $data['website_name'],
  125. 'logo' => $data['logo'] ?? '',
  126. 'website_url' => $data['website_url'] ?? '',
  127. // 'city_id'=>$data['city_id']??0,
  128. 'website_column_id' => $data['website_column_id'],
  129. 'title' => $data['title'] ?? '',
  130. 'keywords' => $data['keywords'] ?? '',
  131. 'description' => $data['description'] ?? '',
  132. // 'status' => $data['status'] ?? 0,
  133. 'website_column_arr_id' => $data['website_column_arr_id'],
  134. // 'city_arr_id'=>$data['city_arr_id']??[0],
  135. 'template_id' => $data['template_id'] ?? 0,
  136. 'suffix' => $data['suffix'] ?? "",
  137. ];
  138. var_dump("=========:", $insertData);
  139. $result = Website::where('id', $id)->update($insertData);
  140. var_dump("更新站点", $result);
  141. if (empty($result)) {
  142. return Result::error("更新失败", 0);
  143. } else {
  144. return Result::success();
  145. }
  146. }
  147. /**
  148. * @param int $id
  149. * @return array
  150. */
  151. public function delWebsite(int $id): array
  152. {
  153. $result = Website::where('id', $id)->delete();
  154. if (empty($result)) {
  155. return Result::error("删除失败", 0);
  156. } else {
  157. return Result::success();
  158. }
  159. }
  160. /**
  161. * @param int $id
  162. * @return array
  163. */
  164. public function getWebsiteInfo(int $id): array
  165. {
  166. $where = [
  167. ['website.id', '=', $id],
  168. ];
  169. $result = Website::where($where)
  170. ->leftJoin("template", "template.id", "website.template_id")
  171. ->select("website.*", "template.template_name", "template.template_img")
  172. ->first();
  173. if (empty($result)) {
  174. return Result::error("数据不存在", 0);
  175. } else {
  176. return Result::success($result->toArray());
  177. }
  178. }
  179. /**
  180. * 查询所有的站点栏目
  181. * @return array
  182. */
  183. public function getWebsiteColumn(array $data): array
  184. {
  185. $result = WebsiteColumn::where($data)->get();
  186. if (empty($result)) {
  187. return Result::error("数据不存在", 0);
  188. } else {
  189. return Result::success($result->toArray());
  190. }
  191. }
  192. /**
  193. * @param string $keyword
  194. * @param int $page
  195. * @param int $pageSize
  196. * @return array
  197. */
  198. public function getWebsiteColumnList(array $data): array
  199. {
  200. $where = [];
  201. if (isset($data['keyword']) && $data['keyword']) {
  202. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  203. }
  204. $result = WebsiteColumn::where($where)
  205. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  206. ->select("website_column.*", "wc.column_name as parent_column_name")
  207. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  208. $count = WebsiteColumn::where($where)->count();
  209. if (empty($result)) {
  210. return Result::error("没有数据", 0);
  211. }
  212. $data = [
  213. 'rows' => $result->toArray(),
  214. 'count' => $count,
  215. ];
  216. return Result::success($data);
  217. }
  218. /**
  219. * @param array $data
  220. * @return array
  221. */
  222. public function createWebsiteColumn(array $data): array
  223. {
  224. $insertData = [
  225. 'column_name' => $data['column_name'],
  226. 'pid' => $data['pid'] ?? '',
  227. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  228. 'sort' => $data['sort'] ?? 0,
  229. 'remark' => $data['remark'] ?? '',
  230. ];
  231. $result = WebsiteColumn::insertGetId($insertData);
  232. if (empty($result)) {
  233. return Result::error("创建失败", 0);
  234. } else {
  235. return Result::success(["id" => $result]);
  236. }
  237. }
  238. /**
  239. * @param int $id
  240. * @param array $data
  241. * @return array
  242. */
  243. public function updateWebsiteColumn(int $id, array $data): array
  244. {
  245. $insertData = [
  246. 'column_name' => $data['column_name'],
  247. 'pid' => $data['pid'] ?? '',
  248. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  249. 'sort' => $data['sort'] ?? 0,
  250. 'remark' => $data['remark'] ?? '',
  251. ];
  252. $result = WebsiteColumn::where('id', $id)->update($insertData);
  253. if (empty($result)) {
  254. return Result::error("更新失败", 0);
  255. } else {
  256. return Result::success();
  257. }
  258. }
  259. /**
  260. * @param int $id
  261. * @return array
  262. */
  263. public function delWebsiteColumn(int $id): array
  264. {
  265. $list = WebsiteColumn::where(['pid' => $id])->get();
  266. if ($list) {
  267. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  268. }
  269. $result = WebsiteColumn::where('id', $id)->delete();
  270. if (empty($result)) {
  271. return Result::error("删除失败", 0);
  272. } else {
  273. return Result::success();
  274. }
  275. }
  276. /**
  277. * @param string $keyword
  278. * @param int $page
  279. * @param int $pageSize
  280. * @return array
  281. */
  282. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  283. {
  284. $where = [
  285. ['role.role_name', 'like', '%' . $keyword . '%'],
  286. ['website_role.website_id', '=', $websiteId],
  287. ];
  288. $result = WebsiteRole::where($where)
  289. ->leftJoin("role", "role.id", "website_role.role_id")
  290. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  291. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  292. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  293. if (empty($result)) {
  294. return Result::error("没有数据", 0);
  295. }
  296. $data = [
  297. 'rows' => $result->toArray(),
  298. 'count' => $count,
  299. ];
  300. return Result::success($data);
  301. }
  302. /**
  303. * @param array $data
  304. * @return array
  305. */
  306. public function createWebsiteRole(array $data): array
  307. {
  308. $insertData = [
  309. 'website_id' => $data['website_id'],
  310. 'role_id' => $data['role_id'] ?? '',
  311. ];
  312. $info = WebsiteRole::where($insertData)->first();
  313. if ($info) {
  314. return Result::error("不能重复添加角色", 0);
  315. }
  316. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  317. $insertData['type'] = $data['type'] ?? '';
  318. $result = WebsiteRole::insertGetId($insertData);
  319. if (empty($result)) {
  320. return Result::error("创建失败", 0);
  321. } else {
  322. return Result::success(["id" => $result]);
  323. }
  324. }
  325. /**
  326. * 暂时用不上
  327. * @param int $id
  328. * @param array $data
  329. * @return array
  330. */
  331. public function updateWebsiteRole(int $id, array $data): array
  332. {
  333. $insertData = [
  334. 'website_id' => $data['website_id'],
  335. 'type' => $data['type'] ?? '',
  336. ];
  337. $result = WebsiteRole::where('id', $id)->update($insertData);
  338. if (empty($result)) {
  339. return Result::error("更新失败", 0);
  340. } else {
  341. return Result::success();
  342. }
  343. }
  344. /**
  345. * @param int $id
  346. * @return array
  347. */
  348. public function delWebsiteRole(int $id): array
  349. {
  350. $result = WebsiteRole::where('id', $id)->delete();
  351. if (empty($result)) {
  352. return Result::error("删除失败", 0);
  353. } else {
  354. return Result::success();
  355. }
  356. }
  357. /**
  358. * @param string $keyword
  359. * @param int $page
  360. * @param int $pageSize
  361. * @return array
  362. */
  363. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  364. {
  365. $where = [
  366. ['website_role_user.website_id', '=', $websiteId],
  367. ['website_role_user.role_id', '=', $roleId],
  368. ];
  369. $count = WebsiteRoleUser::where($where)->count();
  370. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  371. $result = WebsiteRoleUser::where($where)
  372. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  373. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  374. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  375. ->select("u.*", "u.user_name", 'w.website_name', 'r.role_name', 'website_role_user.id as website_role_user_id', 'website_role_user.updated_at as user_update_at')
  376. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  377. if (empty($result)) {
  378. return Result::error("没有数据", 0);
  379. }
  380. $data = [
  381. 'rows' => $result->toArray(),
  382. 'count' => $count,
  383. ];
  384. return Result::success($data);
  385. }
  386. /**
  387. * @param array $data
  388. * @return array
  389. */
  390. public function createWebsiteRoleUser(array $data): array
  391. {
  392. $insertData = [
  393. 'website_id' => $data['website_id'],
  394. 'user_id' => $data['user_id'] ?? '',
  395. ];
  396. $info = WebsiteRoleUser::where($insertData)->first();
  397. if ($info) {
  398. return Result::error("不能重复添加角色用户", 0);
  399. }
  400. $insertData['role_id'] = $data['role_id'] ?? '';
  401. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  402. $insertData['type'] = $data['type'] ?? '';
  403. $result = WebsiteRoleUser::insertGetId($insertData);
  404. if (empty($result)) {
  405. return Result::error("创建失败", 0);
  406. } else {
  407. return Result::success(["id" => $result]);
  408. }
  409. }
  410. /**
  411. * @param int $id
  412. * @param array $data
  413. * @return array
  414. */
  415. public function updateWebsiteRoleUser(int $id, array $data): array
  416. {
  417. $insertData = [
  418. 'website_id' => $data['website_id'],
  419. 'type' => $data['type'] ?? '',
  420. 'type_id' => $data['type_id'] ?? '',
  421. 'role_id' => $data['role_id'] ?? '',
  422. ];
  423. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  424. if (empty($result)) {
  425. return Result::error("更新失败", 0);
  426. } else {
  427. return Result::success();
  428. }
  429. }
  430. /**
  431. * @param int $id
  432. * @return array
  433. */
  434. public function delWebsiteRoleUser(int $id): array
  435. {
  436. $result = WebsiteRoleUser::where('id', $id)->delete();
  437. if (empty($result)) {
  438. return Result::error("删除失败", 0);
  439. } else {
  440. return Result::success();
  441. }
  442. }
  443. /**
  444. * 根据域名获取网站 站点id
  445. * @param array $data
  446. * @return array
  447. */
  448. public function getWebsiteId(array $data): array
  449. {
  450. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  451. if (empty($result)) {
  452. return Result::error("查询站点失败", 0);
  453. } else {
  454. return Result::success($result);
  455. }
  456. }
  457. /**
  458. * 查询网站下面的导航
  459. * @param array $data
  460. * @return array
  461. */
  462. public function getWebsiteCategory(array $data): array
  463. {
  464. $where = [
  465. 'website_id' => $data['website_id'],
  466. ];
  467. // 使用递归查询构建树状结构
  468. $category = WebsiteCategory::where($where)
  469. ->with([
  470. 'children' => function ($query) {
  471. $query->orderBy('sort', 'asc');
  472. },
  473. ])
  474. ->orderBy('sort', 'asc')
  475. ->where('pid', 0) // 仅查询顶级节点
  476. ->get()->all();
  477. if (empty($category)) {
  478. return Result::error("查询站点栏目失败", 0);
  479. } else {
  480. $count = WebsiteCategory::where($where)->count();
  481. $result = [
  482. 'rep' => $category,
  483. 'count' => $count,
  484. ];
  485. return Result::success($result);
  486. }
  487. }
  488. /**
  489. * 查询网站的广告
  490. * @param array $data
  491. * @return array
  492. */
  493. public function getWebsiteAdvertisement(array $data): array
  494. {
  495. $where = [
  496. 'ad_place.status' => 1,
  497. ];
  498. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  499. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  500. $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
  501. // return Result::success($where);
  502. $result = AdPlace::where($where)
  503. ->leftJoin("ad", function ($join) use ($now) {
  504. $join->on("ad.pid", "=", "ad_place.id")
  505. ->where('ad.status', 1)
  506. ->where('ad.fromtime', '<=', $now)
  507. ->where('ad.totime', '>=', $now);
  508. })
  509. ->select(
  510. 'ad_place.name as place_name',
  511. 'ad_place.thumb',
  512. 'ad_place.ad_tag',
  513. 'ad_place.introduce',
  514. 'ad.name as ad_name',
  515. 'ad.image_src',
  516. 'ad.image_url',
  517. 'ad.image_alt'
  518. )
  519. ->get()->all();
  520. if (empty($result)) {
  521. return Result::error("此广告位不存在!", 0);
  522. }
  523. } else {
  524. return Result::error("请选择广告位!", 0);
  525. }
  526. return Result::success($result);
  527. }
  528. /**
  529. *
  530. * @param array $data
  531. * @return array
  532. */
  533. /**网站行政职能搜索 */
  534. public function selectWebsiteDepartment(array $data): array
  535. {
  536. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  537. if (isset($data['keyword']) && !empty($data['keyword'])) {
  538. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  539. if (empty($departments)) {
  540. $result['message'] = "未查询到与此相关职能部门";
  541. } else {
  542. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  543. $m = [
  544. 'department' => $depart,
  545. 'type' => $departments,
  546. 'count' => $count,
  547. ];
  548. }
  549. $result['sele'] = $m;
  550. return Result::success($result['sele']);
  551. }
  552. $result = $depart;
  553. return Result::success($result);
  554. }
  555. /**
  556. * 搜索地区
  557. * @param array $data
  558. * @return array
  559. */
  560. public function selectWebsiteArea(array $data): array
  561. {
  562. // return Result::success($data);
  563. $pid = $data['pid'] ?? 0;
  564. $result = District::leftJoin('district as district2', 'district.pid', '=', 'district2.id')
  565. ->where('district.pid', $pid)
  566. ->where('district.status', 1)
  567. ->select('district.*', 'district2.name as pid_name')
  568. ->orderBy('district.code')->get();
  569. if (empty($result)) {
  570. return Result::error("未查询到此地区", 0);
  571. } else {
  572. return Result::success($result);
  573. }
  574. }
  575. /**
  576. * 获取栏目
  577. * @param array $data
  578. * @return array
  579. */
  580. public function getWebsiteModelCategory(array $data): array
  581. {
  582. // return Result::success($data);
  583. $website_id = [
  584. 'website_id' => $data['website_id'],
  585. ];
  586. // 初始化 $pid 数组
  587. // $pid = [];
  588. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  589. $pidQuery = WebsiteCategory::where($website_id)
  590. ->where('pid', '!=', 0)
  591. ->orderBy('sort')
  592. ->select('pid', 'category_id', 'alias');
  593. $pid = $pidQuery->pluck('pid');
  594. $pid = array_values(array_unique($pid->toArray()));
  595. // 构建查询语句
  596. $query = WebsiteCategory::where($website_id)
  597. ->where('pid', $data['pid'])
  598. ->offset($data['placeid'])
  599. ->limit($data['num'])
  600. ->orderBy('sort');
  601. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  602. if (!empty($pid)) {
  603. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  604. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  605. } else {
  606. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  607. $query->select('website_category.*', DB::raw('0 as children_count'));
  608. }
  609. // 执行查询
  610. $placeid = $data['placeid'] - 1;
  611. $result = $query->offset($placeid)->limit($data['num'])->get();
  612. if (!empty($result)) {
  613. $pidResults = $pidQuery->get();
  614. if (isset($data['type']) && $data['type'] == 1) {
  615. $result = $result->map(function ($item) use ($pidResults) {
  616. $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
  617. if (!empty($child)) {
  618. return $child;
  619. }
  620. });
  621. // 重置索引,使 key 值从 0 开始
  622. $item->children = $children->values();
  623. return $item;
  624. });
  625. } else {
  626. $pidMap = $pidResults->keyBy('pid');
  627. $result->each(function ($record) use ($pidMap) {
  628. if ($pidMap->has($record->category_id)) {
  629. $pidResult = $pidMap->get($record->category_id);
  630. $record->chilid_id = $pidResult->category_id;
  631. $record->chilid_alias = $pidResult->alias;
  632. }
  633. });
  634. }
  635. return Result::success($result);
  636. } else {
  637. return Result::error("本网站暂无栏目", 0);
  638. }
  639. }
  640. /**
  641. * 获取友情链接
  642. * @param array $data
  643. * @return array
  644. */
  645. public function selectWebsiteLinks(array $data): array
  646. {
  647. $where = [
  648. 'website_id' => $data['website_id'],
  649. 'status' => 1,
  650. 'type' => $data['type'],
  651. ];
  652. $num = $data['num'];
  653. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  654. if (!empty($result)) {
  655. return Result::success($result);
  656. } else {
  657. return Result::error("本网站暂无此类型友情链接", 0);
  658. }
  659. }
  660. /**
  661. * 网站首页数据统计, 管理员
  662. * @return void
  663. */
  664. public function getAdminIndex(array $data): array
  665. {
  666. var_dump("用户类型:", $data['type_id']);
  667. switch ($data['type_id']) {
  668. case 4:
  669. $result = Db::select('SELECT DATE(created_at) AS date,COUNT(*) AS total_count FROM letter_of_complaint WHERE created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(created_at) ORDER BY date ASC;');
  670. $res = [];
  671. //查询已审核文章
  672. $user_id = $data['user_id'];
  673. $article = Article::where('status', 1)
  674. ->where('admin_user_id', $user_id)
  675. ->orderBy('updated_at', 'desc')
  676. ->limit(10)
  677. ->get();
  678. $res['letterOfComplaintList'] = $result;
  679. $res['article'] = $article;
  680. $res['article']['count'] = Article::whereNotIn('status', ['404'])->where('admin_user_id', $user_id)->count();
  681. $res['good']['count'] = Good::where([])->where('user_id', $user_id)->count();
  682. $res['book']['count'] = Book::where([])->where('user_id', $user_id)->count();
  683. $res['jobHunting']['count'] = JobHunting::where([])->where('user_id', $user_id)->count();
  684. $res['jobRecruiting']['count'] = JobRecruiting::where([])->where('user_id', $user_id)->count();
  685. $res['complaint']['count'] = Complaint::where([])->where('user_id', $user_id)->count();
  686. $res['notice']['count'] = Notice::where([])->where('user_id', $user_id)->count();
  687. //待审核数据
  688. $res['apply']['article'] = Article::where(['status' => 0])->where('admin_user_id', $user_id)->count(); //
  689. $res['apply']['good'] = Good::where(['status' => 1])->where('user_id', $user_id)->count();
  690. $res['apply']['book'] = Book::where(['status' => 1])->where('user_id', $user_id)->count();
  691. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->where('user_id', $user_id)->count();
  692. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->where('user_id', $user_id)->count();
  693. $res['apply']['complaint'] = Complaint::where(['status' => 1])->where('user_id', $user_id)->count();
  694. $res['apply']['notice'] = Notice::where(['status' => 1])->where('user_id', $user_id)->count();
  695. return Result::success($res);
  696. case 10000:
  697. $res = [];
  698. //网站
  699. $res['website']['count'] = 0;
  700. $res['website']['growth_rate'] = 0;
  701. //资讯
  702. $res['article']['count'] = 0;
  703. $res['article']['growth_rate'] = 0;
  704. //导航池
  705. $res['category']['count'] = 0;
  706. $res['category']['growth_rate'] = 0;
  707. //商品
  708. $res['good']['count'] = 0;
  709. $res['book']['count'] = 0;
  710. $res['jobHunting']['count'] = 0;
  711. $res['jobRecruiting']['count'] = 0;
  712. //近一月数据
  713. $res['monthArticle'] = [];
  714. //用户类型
  715. $res['userType'] = [];
  716. $res['website']['count'] = Website::where([])->count();
  717. $res['article']['count'] = Article::whereIn('status', [1])->count();
  718. $res['category']['count'] = Category::where([])->count();
  719. $res['good']['count'] = Good::whereIn('status', [2])->count();
  720. $res['book']['count'] = Book::whereIn('status', [2])->count();
  721. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])->count();
  722. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])->count();
  723. $res['complaint']['count'] = Complaint::whereIn('status', [2])->count();
  724. $res['notice']['count'] = Notice::whereIn('status', [2])->count();
  725. // $res['monthArticle'] = Db::select('SELECT DATE(created_at) AS date,COUNT(*) AS total_count FROM article WHERE created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(created_at) ORDER BY date ASC;');
  726. $res['monthArticle'] = Db::select(' SELECT DATE(article.created_at) AS date,COUNT(*) AS count ,user.type_id FROM article left JOIN user on article.admin_user_id = user.id WHERE
  727. article.status =1 and article.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(article.created_at),`user`.type_id ORDER BY date ASC');
  728. $res['monthGood'] = Db::select(' SELECT DATE(good.created_at) AS date,COUNT(*) AS count ,user.type_id FROM good left JOIN user on good.user_id = user.id WHERE
  729. good.status =2 and good.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(good.created_at),`user`.type_id ORDER BY date ASC');
  730. $res['monthBook'] = Db::select(' SELECT DATE(book.created_at) AS date,COUNT(*) AS count ,user.type_id FROM book left JOIN user on book.user_id = user.id WHERE book.status =2 and book.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(book.created_at),`user`.type_id ORDER BY date ASC');
  731. $res['monthJobHunting'] = Db::select(' SELECT DATE(job_hunting.created_at) AS date,COUNT(*) AS count ,user.type_id FROM job_hunting left JOIN user on job_hunting.user_id = user.id WHERE job_hunting.status =2 and job_hunting.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(job_hunting.created_at),`user`.type_id ORDER BY date ASC');
  732. $res['monthJobRecruiting'] = Db::select(' SELECT DATE(job_recruiting.created_at) AS date,COUNT(*) AS count ,user.type_id FROM job_recruiting left JOIN user on job_recruiting.user_id = user.id WHERE job_recruiting.status =1 and job_recruiting.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(job_recruiting.created_at),`user`.type_id ORDER BY date ASC');
  733. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  734. //待审核数据
  735. $res['apply']['article'] = Article::where(['status' => 0])->count(); //
  736. $res['apply']['good'] = Good::where(['status' => 1])->count();
  737. $res['apply']['book'] = Book::where(['status' => 1])->count();
  738. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->count();
  739. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->count();
  740. $res['apply']['complaint'] = Complaint::where(['status' => 1])->count();
  741. $res['apply']['notice'] = Notice::where(['status' => 1])->count(); //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束',
  742. $res['apply']['order'] = Order::where(['status' => 6])->count();
  743. return Result::success($res);
  744. case 1:
  745. case 2:
  746. case 3:
  747. $res = [];
  748. //查询已审核文章
  749. $user_id = $data['user_id'];
  750. $article = Article::where('status', 1)
  751. ->where('admin_user_id', $user_id)
  752. ->orderBy('updated_at', 'desc')
  753. ->limit(10)
  754. ->get();
  755. $res['article'] = $article;
  756. $res['website']['count'] = Website::where([])->count();
  757. $res['article']['count'] = Article::whereIn('status', [1])->where('admin_user_id', $user_id)->count();
  758. $res['category']['count'] = Category::where([])->count();
  759. $res['good']['count'] = Good::whereIn('status', [2])
  760. ->where('user_id', $user_id)->count();
  761. $res['book']['count'] = Book::whereIn('status', [2])
  762. ->where('user_id', $user_id)->count();
  763. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])
  764. ->where('user_id', $user_id)->count();
  765. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])
  766. ->where('user_id', $user_id)->count();
  767. $res['complaint']['count'] = Complaint::whereIn('status', [2])
  768. ->where('user_id', $user_id)->count();
  769. $res['notice']['count'] = Notice::whereIn('status', [2])
  770. ->where('user_id', $user_id)->count();
  771. //待审核数据
  772. $res['apply']['article'] = Article::where(['status' => 0])->where('admin_user_id', $user_id)->count(); //
  773. $res['apply']['good'] = Good::where(['status' => 1])->where('user_id', $user_id)->count();
  774. $res['apply']['book'] = Book::where(['status' => 1])->where('user_id', $user_id)->count();
  775. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->where('user_id', $user_id)->count();
  776. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->where('user_id', $user_id)->count();
  777. $res['apply']['complaint'] = Complaint::where(['status' => 1])->where('user_id', $user_id)->count();
  778. $res['apply']['notice'] = Notice::where(['status' => 1])->where('user_id', $user_id)->count();
  779. return Result::success($res);
  780. }
  781. return [];
  782. }
  783. /**
  784. * 获取分类下的模板
  785. * @param array $data
  786. * @return array
  787. */
  788. public function getTemplate(array $data): array
  789. {
  790. $page = $data['page'];
  791. $pageSize = $data['pageSize'];
  792. $where = [];
  793. if (isset($data['template_class_id']) && $data['template_class_id']) {
  794. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  795. }
  796. $result = Template::where($where)
  797. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  798. $count = Template::where($where)->count();
  799. if (empty($result)) {
  800. return Result::error("没有数据", 0);
  801. }
  802. $data = [
  803. 'rows' => $result->toArray(),
  804. 'count' => $count,
  805. ];
  806. return Result::success($data);
  807. }
  808. /**
  809. * 创建模板
  810. * @param
  811. * @return void
  812. */
  813. public function addTemplate(array $data): array
  814. {
  815. $insertData = [
  816. 'template_name' => $data['template_name'],
  817. 'template_img' => json_encode($data['template_img']),
  818. 'template_class_id' => $data['template_class_id'],
  819. ];
  820. $result = Template::insertGetId($insertData);
  821. if (empty($result)) {
  822. return Result::error("创建模板失败", 0);
  823. } else {
  824. return Result::success(["id" => $result]);
  825. }
  826. }
  827. /**
  828. * 更新模板
  829. * @param array $data
  830. * @return array
  831. */
  832. public function upTemplate(array $data): array
  833. {
  834. $where = [
  835. 'id' => $data['id'],
  836. ];
  837. $insertData = [
  838. 'template_name' => $data['template_name'],
  839. 'template_img' => json_encode($data['template_img']),
  840. 'template_class_id' => $data['template_class_id'],
  841. ];
  842. $result = Template::where($where)->update($insertData);
  843. if (empty($result)) {
  844. return Result::error("更新模板失败", 0);
  845. } else {
  846. return Result::success();
  847. }
  848. }
  849. /**
  850. * 删除模板
  851. * @param array $data
  852. * @return array
  853. */
  854. public function delTemplate(array $data): array
  855. {
  856. $where = [
  857. 'id' => $data['id'],
  858. ];
  859. $result = Template::where($where)->delete();
  860. if (empty($result)) {
  861. return Result::error("删除模板失败", 0);
  862. } else {
  863. return Result::success();
  864. }
  865. }
  866. /**
  867. * 搜索网站
  868. * @param array $data
  869. * @return array
  870. */
  871. public function websiteList(array $data): array
  872. {
  873. $where = [];
  874. if (isset($data['keyword']) && !empty($data['keyword'])) {
  875. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  876. }
  877. $result = Website::where($where)->get();
  878. if (isset($data['website_column_id'])) {
  879. $target = $data['website_column_id'];
  880. $result = $result->filter(function ($item) use ($target) {
  881. $arr = json_decode($item->website_column_arr_id, true);
  882. return !in_array($target, $arr);
  883. });
  884. }
  885. if ($result) {
  886. return Result::success($result);
  887. } else {
  888. return Result::error("没有网站", 0);
  889. }
  890. }
  891. public function addWebsiteCategory(array $data): array
  892. {
  893. $website_id = $data['website_id'];
  894. $category_arr_id = $data['category_arr_id'];
  895. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  896. $categoryListIds = [];
  897. if ($categoryList) {
  898. foreach ($categoryList->toArray() as $val) {
  899. array_push($categoryListIds, $val['id']);
  900. }
  901. }
  902. $arr = [];
  903. if ($categoryListIds) {
  904. foreach ($categoryListIds as $v) {
  905. $ids = $this->getUnderlingUIds(intval($v));
  906. $ids_arr = explode(",", $ids);
  907. array_push($arr, $ids_arr);
  908. }
  909. }
  910. $mergedArray = [];
  911. foreach ($arr as $subarray) {
  912. $mergedArray = array_merge($mergedArray, $subarray);
  913. }
  914. var_dump("所有:", $arr, $mergedArray);
  915. //查询出所有的分类进行分割插入 组装数据
  916. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  917. $categoryListData = $categoryListData->toArray();
  918. $insertData = [];
  919. if ($categoryListData) {
  920. foreach ($categoryListData as $key => $value) {
  921. $insertData[$key]['website_id'] = $website_id;
  922. $insertData[$key]['name'] = $value['name'];
  923. $insertData[$key]['sort'] = $value['sort'];
  924. $insertData[$key]['pid'] = $value['pid'];
  925. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  926. $insertData[$key]['seo_title'] = $value['seo_title'];
  927. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  928. $insertData[$key]['seo_description'] = $value['seo_description'];
  929. $insertData[$key]['alias'] = $value['name'];
  930. $insertData[$key]['category_id'] = $value['id'];
  931. }
  932. }
  933. $result = WebsiteCategory::insert($insertData);
  934. var_dump("插入数据状态:", $result);
  935. if ($result) {
  936. return Result::success($result);
  937. } else {
  938. return Result::error("创建失败", 0);
  939. }
  940. }
  941. /**
  942. * 删除网站导航
  943. * @param array $data
  944. * @return array
  945. */
  946. public function delWebsiteCategory(array $data): array
  947. {
  948. $website_id = $data['website_id'] ?? 0;
  949. $category_id = $data['category_id'] ?? 0;
  950. $ids = $this->getUnderlingUIds(intval($category_id));
  951. $ids_arr = explode(",", $ids);
  952. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  953. if ($result) {
  954. return Result::success($result);
  955. } else {
  956. return Result::error("删除失败", 0);
  957. }
  958. }
  959. /**
  960. * 获取网站导航
  961. * @param array $data
  962. * @return array
  963. */
  964. public function getAdminWebsiteCategory(array $data): array
  965. {
  966. $where = [
  967. 'website_id' => $data['website_id'],
  968. ];
  969. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  970. $list = [];
  971. if ($result) {
  972. foreach ($result->toArray() as $val) {
  973. array_push($list, json_decode($val['category_arr_id']));
  974. }
  975. }
  976. if ($result) {
  977. return Result::success($list);
  978. } else {
  979. return Result::error("查询失败", 0);
  980. }
  981. }
  982. /**
  983. * 更新网站导航
  984. * @param array $data
  985. * @return array
  986. */
  987. public function upWebsiteCategory(array $data): array
  988. {
  989. $pinyin = new Pinyin();
  990. Db::beginTransaction();
  991. try {
  992. if (!empty($data['category_arr_id'])) {
  993. $websiteId = $data['website_id'];
  994. // 1. 获取旧的 category_id 列表
  995. $oldCategoryListIds = WebsiteCategory::where('website_id', $websiteId)->pluck('category_id')->toArray();
  996. // 2. 获取新的 category_id 列表(去重)
  997. $newCategoryListIds = [];
  998. foreach ($data['category_arr_id'] as $arr) {
  999. $lastId = end($arr);
  1000. $newCategoryListIds[] = $lastId;
  1001. }
  1002. $newCategoryListIds = array_unique($newCategoryListIds);
  1003. // 3. 需要删除的 category_id
  1004. $toDelete = array_diff($oldCategoryListIds, $newCategoryListIds);
  1005. if (!empty($toDelete)) {
  1006. WebsiteCategory::where('website_id', $websiteId)
  1007. ->whereIn('category_id', $toDelete)
  1008. ->delete();
  1009. }
  1010. // 4. 批量查出所有需要的 category 信息
  1011. $categories = Category::whereIn('id', $newCategoryListIds)->get()->keyBy('id');
  1012. // 5. 组装 upsertData,确保 category_id 不重复
  1013. $upsertData = [];
  1014. $handledCategoryIds = [];
  1015. foreach ($data['category_arr_id'] as $arr) {
  1016. $categoryId = end($arr);
  1017. if (!isset($categories[$categoryId]) || in_array($categoryId, $handledCategoryIds)) continue;
  1018. $info = $categories[$categoryId];
  1019. $aLIas_pinyin = $pinyin->permalink($info->name, '');
  1020. $upsertData[] = [
  1021. 'website_id' => $websiteId,
  1022. 'category_id' => $categoryId,
  1023. 'name' => $info->name ?? '',
  1024. 'alias' => $info->name ?? '',
  1025. 'aLIas_pinyin' => $aLIas_pinyin ?? '',
  1026. 'sort' => $info->sort ?? 0,
  1027. 'pid' => $info->pid ?? 0,
  1028. 'pid_arr' => $info->pid_arr ?? json_encode([]),
  1029. 'seo_title' => $info->seo_title ?? "",
  1030. 'seo_keywords' => $info->seo_keywords ?? "",
  1031. 'seo_description' => $info->seo_description ?? "",
  1032. 'is_url' => $info->is_url ?? 0,
  1033. 'web_url' => $info->web_url ?? '',
  1034. 'type' => $info->type ?? 1,
  1035. 'category_arr_id' => json_encode($arr)
  1036. ];
  1037. $handledCategoryIds[] = $categoryId;
  1038. }
  1039. // 6. upsert
  1040. if (!empty($upsertData)) {
  1041. WebsiteCategory::upsert(
  1042. $upsertData,
  1043. ['website_id', 'category_id'],
  1044. [
  1045. 'aLIas_pinyin',
  1046. 'pid',
  1047. 'pid_arr',
  1048. 'is_url',
  1049. 'web_url',
  1050. 'category_arr_id'
  1051. ]
  1052. );
  1053. }
  1054. }
  1055. Db::commit();
  1056. return Result::success();
  1057. } catch (\Throwable $ex) {
  1058. Db::rollBack();
  1059. return Result::error("修改失败" . $ex->getMessage(), 0);
  1060. }
  1061. }
  1062. /**
  1063. * 获取网站列表
  1064. * @param array $data
  1065. * @return array
  1066. */
  1067. public function getWebsiteCategoryList(array $data): array
  1068. {
  1069. $where = [];
  1070. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1071. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1072. }
  1073. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1074. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1075. }
  1076. $result = Website::where($where)
  1077. ->with(["websiteCategory" => function ($query) {
  1078. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1079. }])
  1080. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1081. ->get();
  1082. $count = Website::where($where)->count();
  1083. if (empty($result)) {
  1084. return Result::error("没有数据", 0);
  1085. }
  1086. $data = [
  1087. 'rows' => $result->toArray(),
  1088. 'count' => $count,
  1089. ];
  1090. if ($result) {
  1091. return Result::success($data);
  1092. } else {
  1093. return Result::error("查询失败", 0);
  1094. }
  1095. }
  1096. /**
  1097. * 删除网站下的所有导航
  1098. * @param array $data
  1099. * @return array
  1100. */
  1101. public function delWebsiteAllCategory(array $data): array
  1102. {
  1103. $website_id = $data['website_id'];
  1104. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1105. if ($result) {
  1106. return Result::success($result);
  1107. } else {
  1108. return Result::error("删除失败", 0);
  1109. }
  1110. }
  1111. /**
  1112. * 获取网站下的某一个导航
  1113. * @param array $data
  1114. * @return array
  1115. */
  1116. public function getWebsiteCategoryOnes(array $data): array
  1117. {
  1118. $website_id = $data['website_id'];
  1119. $category_id = $data['category_id'];
  1120. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1121. ->first();
  1122. if ($result) {
  1123. return Result::success($result);
  1124. } else {
  1125. return Result::error("查询失败", 0);
  1126. }
  1127. }
  1128. /**
  1129. * 更新网闸下的某一个导航
  1130. * @param array $data
  1131. * @return array
  1132. */
  1133. public function upWebsiteCategoryones(array $data): array
  1134. {
  1135. //处理所有数据
  1136. //获取所有数据
  1137. if (isset($data['all']) && $data['all'] == 'all') {
  1138. $result = WebsiteCategory::get();
  1139. $result = $result->toArray();
  1140. var_dump($result, '------------');
  1141. $pinyin = new Pinyin();
  1142. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1143. for ($i = 0; $i < count($result); $i++) {
  1144. // $randss = substr(str_shuffle($letters), 0, 4);
  1145. $result[$i]['aLIas_pinyin'] = $pinyin->permalink($result[$i]['alias'], '');
  1146. WebsiteCategory::where(['id' => $result[$i]['id']])->update(['aLIas_pinyin' => $result[$i]['aLIas_pinyin']]);
  1147. }
  1148. var_dump($result, '--------222----');
  1149. if ($result) {
  1150. return Result::success($result);
  1151. } else {
  1152. return Result::error("更新失败", 0);
  1153. }
  1154. }
  1155. //处理所有数据
  1156. $where = [
  1157. 'website_id' => $data['website_id'],
  1158. 'category_id' => $data['category_id'],
  1159. ];
  1160. $alias = $data['alias'];
  1161. $pinyin = new Pinyin();
  1162. $aLIas_pinyin = $pinyin->permalink($alias, '');
  1163. //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
  1164. $result = WebsiteCategory::where($where)->first();
  1165. $result = $result->toArray();
  1166. //+四位随机字母
  1167. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1168. // 打乱字符串顺序并截取前四位
  1169. // $randss = substr(str_shuffle($letters), 0, 4);
  1170. if ($data['alias_pinyin'] == '') {
  1171. $data['aLIas_pinyin'] = $aLIas_pinyin;
  1172. }
  1173. $result = WebsiteCategory::where($where)->update($data);
  1174. if ($result) {
  1175. return Result::success($result);
  1176. } else {
  1177. return Result::error("更新失败", 0);
  1178. }
  1179. }
  1180. /**
  1181. * 获取网站下的所有导航(包含子导航)
  1182. * @param array $data
  1183. * @return array
  1184. */
  1185. public function getWebsiteAllCategory(array $data): array
  1186. {
  1187. $where = [];
  1188. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1189. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1190. }
  1191. // if (isset($data['name']) && !empty($data['name'])) {
  1192. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1193. // }
  1194. // if (isset($data['alias']) && !empty($data['alias'])) {
  1195. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1196. // }
  1197. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1198. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1199. // }
  1200. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1201. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1202. // }
  1203. $result = WebsiteCategory::where($where)
  1204. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1205. ->select("website_category.*", "category.name as name")
  1206. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1207. // ->leftJoin("department", 'category.department_id', 'department.id')
  1208. // ->leftJoin("district", 'category.city_id', 'district.id')
  1209. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1210. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1211. ->orderBy("sort", "asc")
  1212. ->get();
  1213. // $count = WebsiteCategory::where($where)
  1214. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1215. // ->leftJoin("department", 'category.department_id', 'department.id')
  1216. // ->leftJoin("district", 'category.city_id', 'district.id')
  1217. // ->count();
  1218. if (empty($result)) {
  1219. return Result::error("没有数据", 0);
  1220. }
  1221. // $data = [
  1222. // 'rows' => $result->toArray(),
  1223. // 'count' => $count,
  1224. // ];
  1225. if ($result) {
  1226. return Result::success($result->toArray());
  1227. } else {
  1228. return Result::error("查询失败", 0);
  1229. }
  1230. }
  1231. /**
  1232. * 递归查询数据
  1233. * @param $id
  1234. * @param $ids
  1235. * @return string
  1236. */
  1237. public function getUnderlingUIds($id, $ids = '')
  1238. {
  1239. $back = Category::where(['pid' => $id])->get();
  1240. $back = $back->toArray();
  1241. if (!empty($back) && is_array($back)) {
  1242. foreach ($back as $v) {
  1243. //防止当前人的ID重复去查询,形成恶性循环
  1244. if ($v['id'] == $id) {
  1245. continue;
  1246. }
  1247. $back2 = Category::where(['pid' => $id])->count('id');
  1248. if ($back2 > 0) {
  1249. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1250. } else {
  1251. $ids .= ',' . $v['id'];
  1252. }
  1253. }
  1254. }
  1255. $ids = $id . ',' . $ids . ',';
  1256. $ids = str_replace(',,', ",", $ids);
  1257. $ids = trim($ids, ',');
  1258. return $ids;
  1259. }
  1260. /**
  1261. * 检测网站名称是否重复
  1262. * @param array $data
  1263. * @return array
  1264. */
  1265. public function checkWebsiteName(array $data): array
  1266. {
  1267. if (isset($data['id'])) {
  1268. $data[] = ['id', "!=", $data['id']];
  1269. unset($data['id']);
  1270. }
  1271. $websiteInfo = Website::query()->where($data)->first();
  1272. if (empty($websiteInfo)) {
  1273. return Result::error("找不到网站", 0);
  1274. }
  1275. return Result::success($websiteInfo->toArray());
  1276. }
  1277. /**
  1278. * 检测网站url是否重复
  1279. * @param array $data
  1280. * @return array
  1281. */
  1282. public function checkWebsiteUrl(array $data): array
  1283. {
  1284. $whereData = [];
  1285. if (isset($data['id'])) {
  1286. $whereData = [['id', "!=", $data['id']]];
  1287. unset($data['id']);
  1288. }
  1289. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1290. if (empty($websiteInfo)) {
  1291. return Result::error("找不到URL", 0);
  1292. }
  1293. return Result::success($websiteInfo->toArray());
  1294. }
  1295. /**
  1296. * 获取网站底部基础信息
  1297. * @param array $data
  1298. * @return array
  1299. */
  1300. public function getWebsiteFootInfo(array $data): array
  1301. {
  1302. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1303. $website_head = Website::where('id', $data['website_id'])
  1304. ->where('status', 1)
  1305. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1306. ->first();
  1307. if (empty($website_head)) {
  1308. return Result::error("找不到网站", 0);
  1309. }
  1310. } else {
  1311. return Result::error("参数错误", 0);
  1312. }
  1313. $website_head = Website::where('id', $data['website_id'])
  1314. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1315. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1316. if (empty($website_foot)) {
  1317. return Result::error("暂无底部基础信息", 0);
  1318. }
  1319. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1320. $result = [
  1321. 'website_foot' => $website_foot,
  1322. 'website_head' => $website_head,
  1323. ];
  1324. return Result::success($result);
  1325. }
  1326. /**
  1327. * 获取网站底部导航
  1328. * @param array $data
  1329. * @return array
  1330. */
  1331. public function getWebsiteFooterCategory(array $data): array
  1332. {
  1333. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1334. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1335. if (empty($website)) {
  1336. return Result::error("找不到网站", 0);
  1337. }
  1338. } else {
  1339. return Result::error("参数错误", 0);
  1340. }
  1341. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1342. if (empty($result)) {
  1343. return Result::error("暂无底部导航", 0);
  1344. }
  1345. return Result::success($result->toArray());
  1346. }
  1347. /**
  1348. * 获取网站底部导航列表
  1349. * @param array $data
  1350. * @return array
  1351. */
  1352. public function getWebsiteFooterCategoryList(array $data): array
  1353. {
  1354. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1355. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1356. if (empty($website)) {
  1357. return Result::error("找不到网站", 0);
  1358. }
  1359. } else {
  1360. return Result::error("参数错误", 0);
  1361. }
  1362. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1363. // '底部导航类型 0:内容型;1:列表型;',
  1364. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1365. return Result::error("底部导航id错误", 0);
  1366. } else {
  1367. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1368. if ($query->count() == 0) {
  1369. return Result::error("暂无底部导航列表", 0);
  1370. } elseif ($query->count() == 1) {
  1371. $result = $query->first();
  1372. } else {
  1373. $result = $query->get();
  1374. }
  1375. }
  1376. return Result::success($result);
  1377. }
  1378. /**
  1379. * 获取网站底部导航
  1380. * @param array $data
  1381. * @return array
  1382. */
  1383. public function getWebsiteFooterCategoryInfo(array $data): array
  1384. {
  1385. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1386. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1387. if (empty($website)) {
  1388. return Result::error("找不到网站", 0);
  1389. }
  1390. } else {
  1391. return Result::error("参数错误", 0);
  1392. }
  1393. if (isset($data['type']) && $data['type'] == 0) {
  1394. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1395. if (empty($fcatid)) {
  1396. return Result::error("底部导航id错误", 0);
  1397. }
  1398. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1399. } else {
  1400. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1401. }
  1402. if (empty($result)) {
  1403. return Result::error("暂无底部导航内容", 0);
  1404. }
  1405. return Result::success($result);
  1406. }
  1407. /*
  1408. * 搜索网站二级导航
  1409. * @param array $data
  1410. * @return array
  1411. * */
  1412. public function selectWebsiteCategory(array $data): array
  1413. {
  1414. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1415. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1416. if (empty($website)) {
  1417. return Result::error("暂无该网站", 0);
  1418. }
  1419. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1420. // return Result::success($category);
  1421. if (empty($category)) {
  1422. return Result::error("暂无此导航", 0);
  1423. }
  1424. $query = Category::whereIn('id', $category);
  1425. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1426. $cityid = District::where('id', $data['cityid'])->first();
  1427. if (empty($cityid)) {
  1428. return Result::error("暂无此城市", 0);
  1429. } else {
  1430. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1431. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1432. if (empty($city_category)) {
  1433. return Result::error("暂无此城市下的导航", 0);
  1434. }
  1435. $result['catid'] = $city_category;
  1436. $city = 1;
  1437. // var_dump("城市====================",$result);
  1438. }
  1439. }
  1440. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1441. $departmentid = Department::where('id', $data['department_id'])->first();
  1442. if (empty($departmentid)) {
  1443. return Result::error("暂无此部门", 0);
  1444. } else {
  1445. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1446. if (empty($depart_category)) {
  1447. return Result::error("暂无此部门下的导航", 0);
  1448. }
  1449. $result['catid'] = $depart_category;
  1450. // var_dump("职能部门*******************",$result);
  1451. $department = 1;
  1452. }
  1453. }
  1454. if (!empty($city) && !empty($department)) {
  1455. // var_dump("城市和职能部门----------------------",$result);
  1456. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1457. if (empty($sel_category)) {
  1458. return Result::error("暂无此城市和职能部门下的导航", 0);
  1459. }
  1460. $result['catid'] = $sel_category;
  1461. }
  1462. if (empty($result)) {
  1463. return Result::error("暂无导航", 0);
  1464. }
  1465. // department_id
  1466. } else {
  1467. return Result::error("参数错误", 0);
  1468. }
  1469. return Result::success($result);
  1470. }
  1471. /**
  1472. * 获取网站栏目seo
  1473. * @param array $data
  1474. * @return array
  1475. */
  1476. public function getWebsiteCategoryHead(array $data): array
  1477. {
  1478. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1479. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1480. if (empty($website)) {
  1481. return Result::error("找不到网站", 0);
  1482. }
  1483. } else {
  1484. return Result::error("参数错误", 0);
  1485. }
  1486. if (isset($data['catid']) && !empty($data['catid'])) {
  1487. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1488. }
  1489. if (empty($result)) {
  1490. return Result::error("暂无导航", 0);
  1491. }
  1492. $result['website_name'] = $website['website_name'] ?? '';
  1493. $result['suffix'] = $website['suffix'] ?? '';
  1494. return Result::success($result);
  1495. }
  1496. /*
  1497. * 获取某个栏目
  1498. * @param array $data
  1499. * @return array
  1500. * */
  1501. public function getOneWebsiteCategory(array $data): array
  1502. {
  1503. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1504. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1505. if (empty($website)) {
  1506. return Result::error("暂无该网站", 0);
  1507. }
  1508. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1509. if (empty($category)) {
  1510. return Result::error("暂无此导航", 0);
  1511. }
  1512. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1513. $parent = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $category['pid'])->select('category_id as parent_id', 'alias as parent_name', 'aLIas_pinyin')->first();
  1514. $category['parent_id'] = $parent['parent_id'] ?? '';
  1515. $category['parent_pinyin'] = $parent['aLIas_pinyin'] ?? '';
  1516. $category['parent_name'] = $parent['parent_name'] ?? '';
  1517. return Result::success($category);
  1518. } else {
  1519. return Result::error("参数错误", 0);
  1520. }
  1521. }
  1522. // 自助建站--fr-------------------
  1523. /**
  1524. * 获取并搜索 网站模板信息
  1525. * @param array $data
  1526. * @return array
  1527. */
  1528. public function getWebsiteintel(array $data): array
  1529. {
  1530. //查询所有网站模板信息
  1531. $query = Website::where('website.status', 1)
  1532. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1533. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1534. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1535. ->select(
  1536. "website_template_info.id",
  1537. "template_class.name",
  1538. "website.id as website_id",
  1539. "website.website_name",
  1540. "website.website_url",
  1541. "website_template_info.action_id",
  1542. "website_template_info.created_at",
  1543. "website_template_info.updated_at",
  1544. "website_template_info.page_type",
  1545. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1546. "template.template_name"
  1547. );
  1548. //若存在条件;则在$query的基础上进行筛选
  1549. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1550. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1551. }
  1552. if (isset($data['status']) && !empty($data['status'])) {
  1553. $query->where('website_template_info.status', $data['status']);
  1554. }
  1555. $count = $query->count();
  1556. $query->limit($data['pageSize'])
  1557. ->offset(($data['page'] - 1) * $data['pageSize'])
  1558. ->orderBy("website_template_info.updated_at", "desc");
  1559. $rep = $query->get();
  1560. if ($rep->count() == 0) {
  1561. return Result::error("没有查找到相关数据", 0);
  1562. } else {
  1563. $rep->each(function ($item) {
  1564. if (!empty($item->page_type)) {
  1565. $pageTypeArray = json_decode($item->page_type, true);
  1566. if (is_array($pageTypeArray)) {
  1567. $item->page_type = $pageTypeArray;
  1568. }
  1569. }
  1570. });
  1571. $result = [
  1572. "rows" => $rep->toArray(),
  1573. "count" => $count,
  1574. ];
  1575. return Result::success($result);
  1576. }
  1577. // return Result::success($result);
  1578. }
  1579. /**
  1580. * 添加网站基础信息
  1581. * @param array $data
  1582. * @return array
  1583. */
  1584. public function addWebsiteTemplateintel(array $data): array
  1585. {
  1586. $website = Website::where('website.id', $data['website_id'])->first();
  1587. if (empty($website)) {
  1588. return Result::error("请输入正确的网站id!", 0);
  1589. }
  1590. if (empty($website['website_column_id'])) {
  1591. return Result::error("请先关联导航池!", 0);
  1592. }
  1593. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1594. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1595. if (empty($foot_type)) {
  1596. return Result::error("请先关联底部导航池!", 0);
  1597. } elseif (in_array(1, $foot_type)) {
  1598. $foot_type = 1;
  1599. } else {
  1600. $foot_type = 0;
  1601. }
  1602. $result['foot_type'] = $foot_type;
  1603. // 友情链接类型'1:图片 2:文字 3:底部';
  1604. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1605. $missingTypes = [];
  1606. if (!in_array(1, $types)) {
  1607. $missingTypes[] = "文字链接";
  1608. }
  1609. if (!in_array(2, $types)) {
  1610. $missingTypes[] = "图片链接";
  1611. }
  1612. if (!in_array(3, $types)) {
  1613. $missingTypes[] = "底部链接";
  1614. }
  1615. if (!empty($missingTypes)) {
  1616. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1617. return Result::error($errorMessage, 0);
  1618. }
  1619. if (isset($data['page_type'])) {
  1620. // 删除重复元素并重新索引数组
  1621. $data['page_type'] = array_unique($data['page_type']);
  1622. $data['page_type'] = array_values($data['page_type']);
  1623. // 1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1624. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1625. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1626. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1627. if (empty($info)) {
  1628. // 将数组转换为 JSON 字符串
  1629. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1630. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1631. $data['action_id'] = 1;
  1632. try {
  1633. $result = WebsiteTemplateInfo::insertGetId($data);
  1634. } catch (\Exception $e) {
  1635. if ($e->getCode() == 22001) {
  1636. $errorMessage = $e->getMessage();
  1637. return Result::error("请检查表单,某个字段超出了长度限制!");
  1638. } else {
  1639. // 处理其他类型的数据库错误
  1640. return Result::error("插入操作失败: " . $e->getMessage());
  1641. }
  1642. return Result::error("添加失败!", 0);
  1643. }
  1644. } else {
  1645. return Result::error("该网站已经构建过了!", 0);
  1646. }
  1647. } else {
  1648. return Result::error("请先选择首页和底部导航详情页!", 0);
  1649. }
  1650. }
  1651. if (empty($result)) {
  1652. return Result::error("添加失败!", 0);
  1653. } else {
  1654. return Result::success($result);
  1655. }
  1656. }
  1657. /**
  1658. * 获取网站基础信息
  1659. * @return array
  1660. *
  1661. */
  1662. public function getWebsiteTemplateintel(array $data): array
  1663. {
  1664. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])
  1665. ->leftJoin('footer_category', 'footer_category.website_id', 'website_template_info.website_id')
  1666. ->leftJoin('website', 'website.id', 'website_template_info.website_id')
  1667. ->select(
  1668. "website_template_info.*",
  1669. "footer_category.type",
  1670. "website.website_name",
  1671. "website.id as website_id"
  1672. )
  1673. ->first();
  1674. if (empty($result['website_name'])) {
  1675. return Result::error("请输入正确的网站id!", 0);
  1676. }
  1677. if ($result) {
  1678. $result['page_type'] = json_decode($result['page_type'], true);
  1679. return Result::success($result);
  1680. } else {
  1681. return Result::error("请先添加网站基础信息!", 0);
  1682. }
  1683. }
  1684. /**
  1685. * 修改网站基础信息
  1686. *
  1687. * @return array
  1688. */
  1689. public function upWebsiteTemplateintel(array $data): array
  1690. {
  1691. $web = Website::where('website.id', $data['website_id'])->first();
  1692. if (empty($web)) {
  1693. return Result::error("请输入正确的网站id!", 0);
  1694. }
  1695. $where = ['website_id' => $data['website_id']];
  1696. $result = WebsiteTemplateInfo::where($where)->first();
  1697. if (empty($result)) {
  1698. return Result::error("请先添加网站基础信息!", 0);
  1699. } else {
  1700. unset($data['website_id']);
  1701. // 删除重复元素
  1702. $data['page_type'] = array_unique($data['page_type']);
  1703. // 重新索引数组
  1704. $data['page_type'] = array_values($data['page_type']);
  1705. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1706. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1707. // 将数组转换为 JSON 字符串
  1708. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1709. try {
  1710. $result = WebsiteTemplateInfo::where($where)->update($data);
  1711. } catch (\Exception $e) {
  1712. if ($e->getCode() == 22001) {
  1713. $errorMessage = $e->getMessage();
  1714. return Result::error("请检查表单,某个字段超出了长度限制!");
  1715. } else {
  1716. // 处理其他类型的数据库错误
  1717. return Result::error("修改操作失败: " . $e->getMessage());
  1718. }
  1719. return Result::error("修改失败!", 0);
  1720. }
  1721. } else {
  1722. return Result::error("请先选择首页和底部导航详情页!", 0);
  1723. }
  1724. }
  1725. if (empty($result)) {
  1726. return Result::error("修改失败!", 0);
  1727. } else {
  1728. return Result::success($result);
  1729. }
  1730. }
  1731. /**
  1732. * 获取所有网站风格信息
  1733. *
  1734. * @return array
  1735. */
  1736. public function getAllTemplateClass(array $data): array
  1737. {
  1738. $result = TemplateClass::all();
  1739. if (empty($result)) {
  1740. return Result::error("没有查找到相关数据!", 0);
  1741. } else {
  1742. return Result::success($result);
  1743. }
  1744. }
  1745. /**
  1746. * 搜索并获取网站模板信息
  1747. * @param array $data
  1748. * @return array
  1749. */
  1750. public function getWebsiteTemplateList(array $data): array
  1751. {
  1752. $website = Website::where('website.id', $data['website_id'])->first();
  1753. if (empty($website)) {
  1754. return Result::error("请输入正确的网站id!", 0);
  1755. } else {
  1756. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1757. if (empty($page_type)) {
  1758. return Result::error("此网站还未添加基础信息!", 0);
  1759. } else {
  1760. $where = json_decode($page_type['page_type'], true);
  1761. // $where = $data["page_type"];
  1762. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1763. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1764. if (empty($template_class)) {
  1765. return Result::error("请输入正确的模板风格id!", 0);
  1766. } else {
  1767. // 获取指定风格下的模板
  1768. $rep = Template::where('template_class_id', $data['template_class_id'])
  1769. ->where(function ($query) use ($where) {
  1770. foreach ($where as $value) {
  1771. $query->whereJsonContains('template_img', ['value' => $value]);
  1772. }
  1773. })
  1774. ->limit($data['pageSize'])
  1775. ->offset(($data['page'] - 1) * $data['pageSize'])
  1776. ->orderBy("template.updated_at", "desc")
  1777. ->get();
  1778. $count = Template::where('template_class_id', $data['template_class_id'])
  1779. ->where(function ($query) use ($where) {
  1780. foreach ($where as $value) {
  1781. $query->whereJsonContains('template_img', ['value' => $value]);
  1782. }
  1783. })->count();
  1784. }
  1785. } else {
  1786. //获取所有模板
  1787. $rep = Template::where(function ($query) use ($where) {
  1788. foreach ($where as $value) {
  1789. $query->whereJsonContains('template_img', ['value' => $value]);
  1790. }
  1791. })
  1792. ->limit($data['pageSize'])
  1793. ->offset(($data['page'] - 1) * $data['pageSize'])
  1794. ->orderBy("template.updated_at", "desc")
  1795. ->get();
  1796. $count = Template::where(function ($query) use ($where) {
  1797. foreach ($where as $value) {
  1798. $query->whereJsonContains('template_img', ['value' => $value]);
  1799. }
  1800. })->count();
  1801. }
  1802. if (empty($rep) || $rep->count() == 0) {
  1803. return Result::error("没有查找到相关数据!", 0);
  1804. } else {
  1805. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1806. // $rep->each(function ($item) use ($where) {
  1807. // $template_img = json_decode($item->template_img, true);
  1808. // if(is_array($template_img)){
  1809. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1810. // return isset($img['value']) && in_array($img['value'], $where);
  1811. // });
  1812. // }
  1813. // $item->template_img = $filtered_img;
  1814. // });
  1815. // $result = [
  1816. // "rows" => $rep->toArray(),
  1817. // "count" => $count
  1818. // ];
  1819. $rep->each(function ($item) use ($where) {
  1820. $template_img = json_decode($item->template_img, true);
  1821. if (is_array($template_img)) {
  1822. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1823. return isset($img['value']) && in_array($img['value'], $where);
  1824. });
  1825. // 对过滤后的数组进行重新排序
  1826. $sorted_img = array_values($filtered_img);
  1827. $item->template_img = $sorted_img;
  1828. } else {
  1829. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1830. }
  1831. });
  1832. $result = [
  1833. "rows" => $rep->toArray(),
  1834. "count" => $count,
  1835. ];
  1836. }
  1837. }
  1838. }
  1839. return Result::success($result);
  1840. }
  1841. /**
  1842. * 添加网站模板
  1843. * @param array $data
  1844. */
  1845. public function addWebsiteTemplateclassintel(array $data): array
  1846. {
  1847. $template = Template::where('id', $data['template_id'])->first();
  1848. $web = Website::where('id', $data['website_id'])->first();
  1849. if (empty($template)) {
  1850. return Result::error("请输入正确的模板id", 0);
  1851. }
  1852. if (empty($web)) {
  1853. return Result::error("请输入正确的网站id", 0);
  1854. }
  1855. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1856. if (empty($result)) {
  1857. return Result::error("添加失败", 0);
  1858. } else {
  1859. return Result::success($result);
  1860. }
  1861. }
  1862. /**
  1863. * 获取网站模板信息
  1864. * @param array $data
  1865. */
  1866. public function getWebsiteTemplateclassintel(array $data): array
  1867. {
  1868. // return Result::success($data);
  1869. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1870. if (empty($template)) {
  1871. return Result::error("请输入正确的搭建网站id", 0);
  1872. }
  1873. $page_type = json_decode($template['page_type'], true);
  1874. // 获取指定网站下的基础信息和模板及风格信息
  1875. $result = WebsiteTemplateInfo::where([
  1876. ['website_template_info.website_id', $data['website_id']],
  1877. ['website_template_info.template_id', '!=', null],
  1878. ])
  1879. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1880. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1881. ->where(function ($query) use ($page_type) {
  1882. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1883. foreach ($page_type as $pageType) {
  1884. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1885. }
  1886. })
  1887. ->select(
  1888. 'template.template_name',
  1889. 'template.template_img',
  1890. 'template.id as tid',
  1891. 'template_class.name',
  1892. 'template_class.id as class_id',
  1893. 'website_template_info.page_type',
  1894. 'website_template_info.website_id'
  1895. )
  1896. ->first();
  1897. if (empty($result)) {
  1898. return Result::error("没有查找到相关数据", 0);
  1899. }
  1900. // return Result::success($result);
  1901. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1902. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1903. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1904. foreach ($template_img as $key => $value) {
  1905. if (!in_array($value['value'], $page_type)) {
  1906. unset($template_img[$key]);
  1907. }
  1908. }
  1909. // return Result::success($result);
  1910. $result['template_img'] = array_values($template_img);
  1911. $result['page_type'] = $page_type;
  1912. // $templateList = $this->getWebsiteTemplateList($data);
  1913. // $count = $templateList['data']['count']??'';
  1914. // $pages = $count / $data['pageSize'];
  1915. // $found = false;
  1916. // while ($pages && !$found) {
  1917. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1918. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1919. // if ($index !== false) {
  1920. // $found = true;
  1921. // $result['page'] = $pages;
  1922. // $pages = 0;
  1923. // // echo "查询到的 template 在第 $page 页";
  1924. // }
  1925. // $pages--;
  1926. // }
  1927. return Result::success($result);
  1928. }
  1929. /**
  1930. * 获取网站模板下的板块信息
  1931. * @param array $data
  1932. */
  1933. public function getWebsiteSectorList(array $data): array
  1934. {
  1935. $website = Website::where('id', $data['website_id'])->first();
  1936. if (empty($website)) {
  1937. return Result::error("请输入正确的网站id", 0);
  1938. }
  1939. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1940. if (empty($template['template_id'])) {
  1941. return Result::error("请先添加网站模板", 0);
  1942. }
  1943. $query = Sector::where('template_id', $template['template_id']);
  1944. if ($query->count() == 0) {
  1945. return Result::error("没有查找到相关板块数据", 0);
  1946. } elseif ($query->count() == 1) {
  1947. $sector = $query->first();
  1948. } else {
  1949. $sector = $query->get();
  1950. }
  1951. $result['page_type'] = $template['page_type'];
  1952. $result['tid'] = $template['template_id'];
  1953. $result = [
  1954. "tid" => $template['template_id'],
  1955. "page_type" => $template['page_type'],
  1956. "sector" => $sector,
  1957. ];
  1958. return Result::success($result);
  1959. }
  1960. // 自助建站---------------fr---------------
  1961. // 网站底基础信息--fr-------------------
  1962. /**
  1963. * 添加网站底基础信息
  1964. * @param array $data
  1965. */
  1966. public function addWebFootInfo(array $data): array
  1967. {
  1968. $website = Website::where('id', $data['website_id'])->first();
  1969. if (empty($website)) {
  1970. return Result::error("请输入正确的网站id!", 0);
  1971. }
  1972. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1973. if (!empty($info)) {
  1974. return Result::error("该网站已经添加过了!", 0);
  1975. }
  1976. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  1977. if (empty($result)) {
  1978. return Result::error("请先添加网站基础信息!", 0);
  1979. } else {
  1980. return Result::success($result);
  1981. }
  1982. }
  1983. /**
  1984. * 获取网站底基础信息
  1985. * @param array $data
  1986. */
  1987. public function getWebFootInfo(array $data): array
  1988. {
  1989. $website = Website::where('id', $data['website_id'])->first();
  1990. if (empty($website)) {
  1991. return Result::error("请输入正确的网站id!", 0);
  1992. }
  1993. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1994. if (empty($result)) {
  1995. return Result::error("请先添加网站基础信息!", 0);
  1996. }
  1997. return Result::success($result);
  1998. }
  1999. /**
  2000. * 修改网站底基础信息
  2001. * @param array $data
  2002. */
  2003. public function upWebFootInfo(array $data): array
  2004. {
  2005. // return Result::success($data);
  2006. $website = Website::where('id', $data['website_id'])->first();
  2007. if (empty($website)) {
  2008. return Result::error("请输入正确的网站id!", 0);
  2009. }
  2010. $web = $data['website_id'];
  2011. unset($data['website_id']);
  2012. $where = ['website_id' => $web];
  2013. // return Result::success($data);
  2014. $result = WebsiteTemplateInfo::where($where)->first();
  2015. if (empty($result)) {
  2016. $data['website_id'] = $web;
  2017. $result = WebsiteTemplateInfo::insertGetId($data);
  2018. // return Result::error("请先添加网站基础信息!",0);
  2019. } else {
  2020. $result = WebsiteTemplateInfo::where($where)->update($data);
  2021. }
  2022. // $where = ['website_id'=>$data['website_id']];
  2023. // unset($data['website_id']);
  2024. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  2025. if (empty($result)) {
  2026. return Result::error("修改失败!", 0);
  2027. }
  2028. return Result::success($result);
  2029. }
  2030. /*
  2031. * 获取父级/子级栏目------路由
  2032. * @param array $data
  2033. * @return array
  2034. * */
  2035. public function getWebsiteParentCategory(array $data): array
  2036. {
  2037. // return Result::success($data);
  2038. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2039. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2040. }
  2041. if (empty($website)) {
  2042. return Result::error("暂无该网站", 0);
  2043. }
  2044. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2045. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2046. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2047. ->where('pid', 0)
  2048. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2049. ->get() // 添加 get() 方法获取结果集合
  2050. ->all();
  2051. // 提前查询所有父级栏目,避免在 map 中多次查询
  2052. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2053. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2054. ->where('pid', 0)
  2055. ->select('category_id', 'aLIas_pinyin')
  2056. ->get()
  2057. ->keyBy('category_id');
  2058. // 直接查询子栏目信息
  2059. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2060. ->where('pid', '!=', 0)
  2061. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2062. ->get();
  2063. // 使用集合的 map 方法处理子栏目信息
  2064. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2065. $alias = $item->alias;
  2066. $category_id = $item->category_id;
  2067. $aLIas_pinyin = $item->aLIas_pinyin;
  2068. $parent = $parentCategories->get($item->pid);
  2069. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2070. $result = [
  2071. 'name' => $alias,
  2072. 'cid' => $category_id
  2073. ];
  2074. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2075. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2076. }
  2077. return $result;
  2078. })->values()->all();
  2079. if (empty($category)) {
  2080. return Result::error("暂无此导航", 0);
  2081. }
  2082. return Result::success($category);
  2083. }
  2084. /**
  2085. * 获取网站栏目信息
  2086. * @param array $data
  2087. */
  2088. public function getWebsiteFootAll(array $data): array
  2089. {
  2090. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2091. if (empty($website)) {
  2092. return Result::error("暂无该网站", 0);
  2093. }
  2094. // 1:图片 2:文字 3:底部
  2095. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2096. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2097. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2098. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2099. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2100. if (empty($result)) {
  2101. return Result::error("暂无此网站信息", 0);
  2102. }
  2103. return Result::success($result);
  2104. }
  2105. /**
  2106. * 获取网站顶部信息
  2107. * @param array $data
  2108. */
  2109. public function getWebsiteHead(array $data): array
  2110. {
  2111. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2112. if (empty($result)) {
  2113. return Result::error("暂无该网站", 0);
  2114. }
  2115. return Result::success($result);
  2116. }
  2117. /**
  2118. * 路由匹配
  2119. * @param array $data
  2120. */
  2121. public function getWebsiteRoute(array $data): array
  2122. {
  2123. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2124. if (empty($website)) {
  2125. return Result::error("暂无该网站", 0);
  2126. }
  2127. if (isset($data['pinyin']) && !empty($data['pinyin'])) {
  2128. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2129. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2130. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2131. $join->on('pc.pid', '=', 'website_category.category_id')
  2132. ->where('pc.website_id', '=', $data['website_id']);
  2133. })
  2134. ->select('website_category.category_id', 'website_category.alias', DB::raw('CASE WHEN pc.category_id IS NOT NULL THEN 1 ELSE 0 END as children_count'))
  2135. ->first();
  2136. }
  2137. if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
  2138. $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
  2139. }
  2140. if (!isset($result) || empty($result)) {
  2141. return Result::error("暂无该导航", 0);
  2142. }
  2143. return Result::success($result);
  2144. }
  2145. //20250212 网站标识
  2146. public function addWebsiteGroup(array $data): array
  2147. {
  2148. //判斷name是不是重复
  2149. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2150. if (!empty($websiteGroupInfo)) {
  2151. return Result::error("网站标识重复", 0);
  2152. }
  2153. //添加信息
  2154. $result = WebsiteGroup::insertGetId($data);
  2155. var_dump($result);
  2156. if (empty($result)) {
  2157. return Result::error("创建失败", 0);
  2158. } else {
  2159. return Result::success($result);
  2160. }
  2161. }
  2162. public function getWebsiteGroupList(array $data): array
  2163. {
  2164. $where = [];
  2165. if (isset($data['name']) && !empty($data['name'])) {
  2166. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2167. }
  2168. $result = WebsiteGroup::where($where)
  2169. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2170. ->get();
  2171. foreach ($result as $websiteGroup) {
  2172. $webIds = json_decode($websiteGroup->web_ids, true);
  2173. $websites = Website::whereIn('id', $webIds)->get();
  2174. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2175. $websiteGroup->website_names = $websiteNames;
  2176. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2177. }
  2178. $count = WebsiteGroup::where($where)->count();
  2179. if (empty($result)) {
  2180. return Result::error("没有数据", 0);
  2181. }
  2182. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2183. }
  2184. public function getWebsiteGroupInfo(array $data): array
  2185. {
  2186. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2187. $webIds = json_decode($websiteInfo->web_ids, true);
  2188. $websites = Website::whereIn('id', $webIds)->get();
  2189. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2190. $websiteInfo->website_names = $websiteNames;
  2191. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2192. if (empty($websiteInfo)) {
  2193. return Result::error("找不到URL", 0);
  2194. }
  2195. return Result::success($websiteInfo->toArray());
  2196. }
  2197. public function updateWebsiteGroup(array $data): array
  2198. {
  2199. $where = [
  2200. 'id' => $data['id'],
  2201. ];
  2202. $insertData = [
  2203. 'name' => $data['name'],
  2204. 'web_ids' => $data['web_ids'],
  2205. ];
  2206. //判斷name是不是重复
  2207. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2208. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2209. return Result::error("网站标识重复", 0);
  2210. }
  2211. $result = WebsiteGroup::where($where)->update($insertData);
  2212. var_dump($result, '------更新');
  2213. if (empty($result)) {
  2214. return Result::error("更新失败", 0);
  2215. } else {
  2216. return Result::success();
  2217. }
  2218. }
  2219. public function deleteWebsiteGroup(array $data): array
  2220. {
  2221. $where = [
  2222. 'id' => $data['id'],
  2223. ];
  2224. //看看user表是不是有这个id
  2225. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2226. if (!empty($userGroup)) {
  2227. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2228. }
  2229. $result = WebsiteGroup::where($where)->delete();
  2230. if (empty($result)) {
  2231. return Result::error("删除失败", 0);
  2232. } else {
  2233. return Result::success();
  2234. }
  2235. }
  2236. public function getWebsiteNavList(array $data): array
  2237. {
  2238. $data['type'] = isset($data['type']) ? $data['type'] : 1;
  2239. $where = [];
  2240. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2241. array_push($where, ['website_id', '=', $data['website_id']]);
  2242. }
  2243. if (isset($data['pid'])) {
  2244. array_push($where, ['pid', '=', $data['pid']]);
  2245. }
  2246. if (isset($data['type']) && !empty($data['type'])) {
  2247. array_push($where, ['type', '=', $data['type']]);
  2248. }
  2249. // 根据站点和type获取websiteCategory 中的id
  2250. $websiteCategoryids = WebsiteCategory::query()
  2251. ->where('website_id', '=', $data['website_id'])
  2252. ->where('type', '=', $data['type'])
  2253. ->pluck('category_arr_id')->toArray();
  2254. var_dump($websiteCategoryids, '------获取websiteCategory 中的id');
  2255. //合并去重
  2256. $category_ids = [];
  2257. foreach ($websiteCategoryids as $key => $value) {
  2258. $v = json_decode($value, true);
  2259. $category_ids = array_unique(array_merge($category_ids, $v));
  2260. }
  2261. // var_dump($category_ids, '------获取websiteCategory 中的id');
  2262. //根据pid取出所有的websiteCategory 中的id
  2263. $list = WebsiteCategory::query()
  2264. ->where('website_id', '=', $data['website_id'])
  2265. ->where('pid', '=', $data['pid'])
  2266. ->whereIn('category_id', $category_ids)
  2267. ->get();
  2268. if (empty($list)) {
  2269. return Result::error("获取失败", 0);
  2270. } else {
  2271. return Result::success($list);
  2272. }
  2273. }
  2274. // 20250307 根据网站标识获和导航获取站点
  2275. public function getWebsiteNavPoolSite(array $data): array
  2276. {
  2277. $where = [];
  2278. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2279. array_push($where, ['category_id', '=', $data['category_id']]);
  2280. }
  2281. $list = WebsiteCategory::query()->where($where)
  2282. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2283. ->select('website_category.*', 'website.website_name')
  2284. ->distinct()
  2285. ->get();
  2286. if (empty($list)) {
  2287. return Result::error("获取失败", 0);
  2288. } else {
  2289. return Result::success($list);
  2290. }
  2291. }
  2292. public function getWebsiteNavPool(array $data): array
  2293. {
  2294. // 初始化查询条件数组
  2295. $query = WebsiteCategory::query();
  2296. // 如果传入了 websiteids,则添加到查询条件中
  2297. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2298. $query->whereIn('website_id', $data['websiteids']);
  2299. }
  2300. // 如果传入了 pid,则添加到查询条件中
  2301. if (isset($data['pid'])) {
  2302. $query->where('pid', '=', $data['pid']);
  2303. }
  2304. // 执行查询 唯一性 id
  2305. $list = $query->distinct()->get();
  2306. // 根据查询结果返回相应的结果
  2307. if ($list->isEmpty()) {
  2308. return Result::error("获取失败", 0);
  2309. } else {
  2310. return Result::success($list->toArray());
  2311. }
  2312. }
  2313. /**
  2314. *
  2315. * @param array $data
  2316. * @return array
  2317. */
  2318. public function getAllCategory(array $data): array
  2319. {
  2320. $result = Category::when($data, function ($query) use ($data) {
  2321. if (isset($data['name']) && !empty($data['name'])) {
  2322. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2323. }
  2324. })->get();
  2325. if (empty($result)) {
  2326. return Result::error("获取失败", 0);
  2327. } else {
  2328. return Result::success($result);
  2329. }
  2330. }
  2331. /**
  2332. * 修改网站栏目排序
  2333. * @param array $data
  2334. * @return array
  2335. */
  2336. public function upWebsiteCategorySort(array $data): array
  2337. {
  2338. $where = [
  2339. 'website_id' => $data['website_id'],
  2340. 'id' => $data['id'],
  2341. ];
  2342. $result = WebsiteCategory::where($where)->update($data);
  2343. if ($result) {
  2344. return Result::success($result);
  2345. } else {
  2346. return Result::error("更新失败", 0);
  2347. }
  2348. }
  2349. //获取底部导航内容详情
  2350. public function getFooterContentInfo(array $data): array
  2351. {
  2352. $where = [
  2353. "id" => $data['content_id']
  2354. ];
  2355. $result = FooterContent::where($where)->first();
  2356. if ($result) {
  2357. return Result::success($result);
  2358. } else {
  2359. return Result::error("没有数据");
  2360. }
  2361. }
  2362. //获取底部导航内容列表
  2363. public function getFooterContentList(array $data): array
  2364. {
  2365. $where = [
  2366. "fcat_id" => $data['fcat_id'],
  2367. ];
  2368. switch (intval($data['type'])) {
  2369. case 0:
  2370. $result = FooterContent::where($where)->first();
  2371. break;
  2372. case 1:
  2373. $result = FooterContent::where($where)->get();
  2374. break;
  2375. }
  2376. if ($result) {
  2377. return Result::success($result);
  2378. } else {
  2379. return Result::error("没有数据");
  2380. }
  2381. }
  2382. /**
  2383. * 获取底部导航列表
  2384. * @param array $data
  2385. * @return array
  2386. */
  2387. public function getFooterCategoryList(array $data): array
  2388. {
  2389. $where = [
  2390. "website_id" => $data['website_id']
  2391. ];
  2392. $result = FooterCategory::where($where)->get();
  2393. if ($result) {
  2394. return Result::success($result);
  2395. } else {
  2396. return Result::error("没有数据");
  2397. }
  2398. }
  2399. // --自助建站-----------20250522fr----------------------start
  2400. /**
  2401. * 获取网站模板下的板块信息
  2402. * @param array $data
  2403. * @return array
  2404. */
  2405. public function getStaticResourceList(array $data): array
  2406. {
  2407. // return Result::success($data);
  2408. $where = [];
  2409. if (isset($data['img_alias']) && !empty($data['img_alias'])) {
  2410. array_push($where, ['website_img.img_alias', 'like', '%' . $data['img_alias'] . '%']);
  2411. }
  2412. if (isset($data['img_type']) && !empty($data['img_type'])) {
  2413. array_push($where, ['website_img.img_type', '=', $data['img_type']]);
  2414. }
  2415. if (isset($data['website_name']) && !empty($data['website_name'])) {
  2416. $website = Website::where('website_name', 'like', '%' . $data['website_name'] . '%')->first();
  2417. if (empty($website)) {
  2418. return Result::error("没有查找到相关数据", 0);
  2419. } else {
  2420. array_push($where, ['website_img.website_id', '=', $website['id']]);
  2421. }
  2422. }
  2423. // return Result::success($where);
  2424. $query = WebsiteImg::when(!empty($where), function ($query) use ($where) {
  2425. $query->where($where);
  2426. })
  2427. ->orderBy('updated_at', 'desc');
  2428. $count = $query->count();
  2429. $row = $query->leftJoin('website', 'website.id', '=', 'website_img.website_id')
  2430. ->select('website_img.*', 'website.website_name')
  2431. ->limit($data['pageSize'])
  2432. ->offset(($data['page'] - 1) * $data['pageSize'])
  2433. ->get();
  2434. if (empty($row)) {
  2435. return Result::error("没有查找到相关数据", 0);
  2436. } else {
  2437. $result = [
  2438. 'row' => $row,
  2439. 'count' => $count,
  2440. ];
  2441. }
  2442. return Result::success($result);
  2443. }
  2444. /**
  2445. * 添加网站模板下的板块信息
  2446. * @param array $data
  2447. */
  2448. public function addStaticResource(array $data): array
  2449. {
  2450. // $file = $data['img_url']->file('image');
  2451. // $size = $file->getSize();
  2452. $img_data = [
  2453. 'website_id' => $data['website_id'] ?? 0,
  2454. 'img_alias' => $data['img_alias'] ?? '',
  2455. 'img_url' => $data['img_url'],
  2456. 'img_size' => $data['img_size'],
  2457. ];
  2458. $img = WebsiteImg::insertGetId($img_data);
  2459. if (empty($img)) {
  2460. return Result::error("添加失败", 0);
  2461. }
  2462. $img_id['id'] = $img;
  2463. $url = WebsiteImg::where($img_id)->value('img_url');
  2464. // 1. 分割路径部分(以 / 为分隔符)
  2465. $pathSegments = explode('/', parse_url($url, PHP_URL_PATH));
  2466. // 2. 取最后一个元素作为文件名
  2467. $fileName = end($pathSegments); // 1743041040714597.jpg
  2468. // 3. 分割文件名获取扩展名
  2469. $fileParts = explode('.', $fileName);
  2470. $fileType = end($fileParts); // jpg
  2471. $img_datas = [
  2472. 'img_type' => $fileType,
  2473. 'name' => $fileName
  2474. ];
  2475. $up_img = WebsiteImg::where($img_id)->update($img_datas);
  2476. if (empty($up_img)) {
  2477. return Result::error("修改失败", 0);
  2478. }
  2479. return Result::success($up_img);
  2480. // 将字节转换为更友好的单位
  2481. // $friendlySize = $this->formatFileSize($size);
  2482. // $data['img_size'] = $friendlySize;
  2483. }
  2484. /**
  2485. * 修改网站模板下的板块信息
  2486. * @param array $data
  2487. */
  2488. public function delStaticResource(array $data): array
  2489. {
  2490. $where = [
  2491. 'id' => $data['id'],
  2492. ];
  2493. $result = WebsiteImg::where($where)->delete();
  2494. if (empty($result)) {
  2495. return Result::error("修改失败", 0);
  2496. }
  2497. return Result::success($result);
  2498. }
  2499. /**
  2500. * 获取尺寸列表
  2501. * @param array $data
  2502. */
  2503. public function getSizeList(array $data): array
  2504. {
  2505. $where = [];
  2506. if (isset($data['width']) && !empty($data['width'])) {
  2507. array_push($where, ['width', 'like', '%' . $data['width'] . '%']);
  2508. }
  2509. if (isset($data['height']) && !empty($data['height'])) {
  2510. array_push($where, ['height', 'like', '%' . $data['height'] . '%']);
  2511. }
  2512. $query = Size::when($where, function ($query) use ($where) {
  2513. $query->where($where);
  2514. })
  2515. ->orderBy('updated_at', 'desc');
  2516. $count = $query->count();
  2517. $rep = $query
  2518. ->offset(($data['page'] - 1) * $data['pageSize'])
  2519. ->limit($data['pageSize'])
  2520. ->get();
  2521. $result = [
  2522. 'row' => $rep,
  2523. 'count' => $count,
  2524. ];
  2525. if (empty($result)) {
  2526. return Result::error("获取失败", 0);
  2527. } else {
  2528. return Result::success($result);
  2529. }
  2530. }
  2531. /**
  2532. * 添加尺寸
  2533. * @param array $data
  2534. */
  2535. public function addSize(array $data): array
  2536. {
  2537. $size = Size::where('width', $data['width'])->where('height', $data['height'])->first();
  2538. if (!empty($size)) {
  2539. return Result::error("尺寸已存在", 0);
  2540. }
  2541. $result = Size::insertGetId($data);
  2542. if (empty($result)) {
  2543. return Result::error("添加失败", 0);
  2544. }
  2545. return Result::success($result);
  2546. }
  2547. /**
  2548. * 删除尺寸
  2549. * @param array $data
  2550. */
  2551. public function delSize(array $data): array
  2552. {
  2553. $where = [
  2554. 'id' => $data['id'],
  2555. ];
  2556. $result = Size::where($where)->delete();
  2557. if (empty($result)) {
  2558. return Result::error("删除失败", 0);
  2559. }
  2560. return Result::success($result);
  2561. }
  2562. /**
  2563. * 修改尺寸
  2564. * @param array $data
  2565. */
  2566. public function upSize(array $data): array
  2567. {
  2568. $where = [
  2569. 'id' => $data['id'],
  2570. ];
  2571. $size = Size::where('id', '!=', $data['id'])->where('width', $data['width'])->where('height', $data['height'])->first();
  2572. if (!empty($size)) {
  2573. return Result::error("尺寸已存在", 0);
  2574. }
  2575. $result = Size::where($where)->update($data);
  2576. if (empty($result)) {
  2577. return Result::error("修改失败", 0);
  2578. }
  2579. return Result::success($result);
  2580. }
  2581. /**
  2582. * 获取尺寸详情
  2583. * @param array $data
  2584. */
  2585. public function getSizeInfo(array $data): array
  2586. {
  2587. $where = [
  2588. 'id' => $data['id'],
  2589. ];
  2590. $result = Size::where($where)->first();
  2591. if (empty($result)) {
  2592. return Result::error("获取失败", 0);
  2593. } else {
  2594. return Result::success($result);
  2595. }
  2596. }
  2597. // --自助建站-----------20250522fr----------------------end
  2598. /**
  2599. * 获取网站白名单路由地址
  2600. * @param array $data
  2601. * @return array
  2602. */
  2603. public function getWhiteRouterList(array $data): array
  2604. {
  2605. // 构建基础查询
  2606. $baseQuery = WhiteRouter::query();
  2607. // 功能名称模糊查询
  2608. if (!empty($data['function_name'])) {
  2609. $baseQuery->where('function_name', 'like', '%' . $data['function_name'] . '%');
  2610. }
  2611. // 网站ID查询 - 使用 JSON_CONTAINS 检查数组中是否包含指定的 website_id
  2612. if (!empty($data['website_id'])) {
  2613. $baseQuery->whereRaw('JSON_CONTAINS(website_id, ?)', [json_encode((int)$data['website_id'])]);
  2614. }
  2615. // 获取总数
  2616. $count = $baseQuery->count();
  2617. // 分页查询
  2618. $result = $baseQuery->orderBy('updated_at', 'desc')
  2619. ->limit($data['pageSize'])
  2620. ->offset(($data['page'] - 1) * $data['pageSize'])
  2621. ->get();
  2622. if (empty($result)) {
  2623. return Result::error("暂无数据", 0);
  2624. }
  2625. // 处理结果,将 website_id 转换为数组并获取对应的网站名称
  2626. $result->transform(function ($item) {
  2627. $websiteIds = json_decode($item->website_id, true);
  2628. $websiteNames = Website::whereIn('id', $websiteIds)
  2629. ->pluck('website_name')
  2630. ->toArray();
  2631. $item->website_names = $websiteNames;
  2632. $item->website_id = $websiteIds; // 将 website_id 转换为数组
  2633. return $item;
  2634. });
  2635. return Result::success([
  2636. 'rows' => $result->toArray(),
  2637. 'count' => $count
  2638. ]);
  2639. }
  2640. /**
  2641. * 添加网站白名单路由地址
  2642. * @param array $data
  2643. * @return array
  2644. */
  2645. public function addWhiteRouter(array $data): array
  2646. {
  2647. // 确保 website_id 是数组
  2648. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2649. // 过滤空值并去重
  2650. $websiteIds = array_filter($websiteIds);
  2651. $websiteIds = array_unique($websiteIds);
  2652. // 将数组中的值转换为整数
  2653. $websiteIds = array_map(function ($value) {
  2654. return (int)$value;
  2655. }, $websiteIds);
  2656. // 转换为 JSON 字符串存储
  2657. $data['website_id'] = json_encode($websiteIds);
  2658. $result = WhiteRouter::insertGetId($data);
  2659. if (empty($result)) {
  2660. return Result::error("添加失败", 0);
  2661. }
  2662. return Result::success($result);
  2663. }
  2664. /**
  2665. * 修改网站白名单路由地址
  2666. * @param array $data
  2667. * @return array
  2668. */
  2669. public function upWhiteRouter(array $data): array
  2670. {
  2671. $where = [
  2672. 'id' => $data['id'],
  2673. ];
  2674. // 确保 website_id 是数组
  2675. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2676. // 过滤空值并去重
  2677. $websiteIds = array_filter($websiteIds);
  2678. $websiteIds = array_unique($websiteIds);
  2679. // 将数组中的值转换为整数
  2680. $websiteIds = array_map(function ($value) {
  2681. return (int)$value;
  2682. }, $websiteIds);
  2683. // 转换为 JSON 字符串存储
  2684. $data['website_id'] = json_encode($websiteIds);
  2685. $result = WhiteRouter::where($where)->update($data);
  2686. if (empty($result)) {
  2687. return Result::error("修改失败", 0);
  2688. }
  2689. return Result::success($result);
  2690. }
  2691. /**
  2692. * 删除网站白名单路由地址
  2693. * @param array $data
  2694. * @return array
  2695. */
  2696. public function delWhiteRouter(array $data): array
  2697. {
  2698. $where = [
  2699. 'id' => $data['id'],
  2700. ];
  2701. $result = WhiteRouter::where($where)->delete();
  2702. if (empty($result)) {
  2703. return Result::error("删除失败", 0);
  2704. }
  2705. return Result::success($result);
  2706. }
  2707. /**
  2708. * 获取网站白名单路由地址详情
  2709. * @param array $data
  2710. * @return array
  2711. */
  2712. public function getWhiteRouterInfo(array $data): array
  2713. {
  2714. $where = [
  2715. 'id' => $data['id'],
  2716. ];
  2717. $result = WhiteRouter::where($where)->first();
  2718. if (empty($result)) {
  2719. return Result::error("没有查找到相关数据", 0);
  2720. }
  2721. // 将 website_id 从 JSON 字符串转换为数组
  2722. $result->website_id = json_decode($result->website_id, true);
  2723. return Result::success($result);
  2724. }
  2725. /**
  2726. * 克隆网站
  2727. * @param array $data
  2728. * @return array
  2729. */
  2730. public function cloneWebsite(array $data): array
  2731. {
  2732. DB::beginTransaction();
  2733. try {
  2734. // 获取原始网站信息
  2735. $originalWebsite = Website::find($data['website_id']);
  2736. if (!$originalWebsite) {
  2737. return Result::error("找不到该网站", 0);
  2738. }
  2739. // 提取基础名称(去除已有 _copy 或 _copy_xxx)
  2740. $baseName = preg_replace('/_copy(\d+)?$/', '', $originalWebsite->website_name);
  2741. // 查询当前系统中已存在的克隆名称数量
  2742. $count = Website::where('website_name', 'like', $baseName . '_copy%')
  2743. ->orWhere('website_name', $baseName)
  2744. ->count();
  2745. // 构造新名称:第一次是 _copy,之后是 _copy_1, _copy_2 ...
  2746. $newName = $baseName . ($count === 0 ? '_copy' : '_copy_' . $count);
  2747. // 创建克隆网站实例
  2748. $clone_website = $originalWebsite->replicate();
  2749. $clone_website->website_name = $newName;
  2750. $clone_website->save();
  2751. $clone_website->website_url = "[]";
  2752. $clone_website->save();
  2753. // var_dump("返回值:",$clone_website->id);
  2754. //克隆网站基础信息 website_template_info website_category
  2755. $resultWebsiteTemplateInfo = WebsiteTemplateInfo::where([ 'website_id' => $data['website_id']])->first();
  2756. $clone_website_template_info = $resultWebsiteTemplateInfo->replicate();
  2757. $clone_website_template_info->website_id = $clone_website->id;
  2758. $clone_website_template_info->save();
  2759. //克隆网站导航 获取原始 website_id 对应的所有 WebsiteCategory 记录
  2760. $originalCategories = WebsiteCategory::where('website_id', $data['website_id'])->get();
  2761. // 克隆网站后的新 ID(假设 $clone_website->id 已存在)
  2762. $newWebsiteId = $clone_website->id;
  2763. // 遍历每条记录并克隆
  2764. foreach ($originalCategories as $category) {
  2765. // 使用 replicate() 方法创建模型副本(不包含主键)
  2766. $clonedCategory = $category->replicate();
  2767. // 修改外键字段指向新网站 ID
  2768. $clonedCategory->website_id = $newWebsiteId;
  2769. // 保存克隆后的记录到数据库
  2770. $clonedCategory->save();
  2771. }
  2772. //克隆友情链接 Link 对应的是多个
  2773. $originalLinks = Link::where('website_id', $data['website_id'])->get();
  2774. foreach ($originalLinks as $link) {
  2775. // 使用 replicate() 方法创建模型副本(不包含主键)
  2776. $clonedLink = $link->replicate();
  2777. // 修改外键字段指向新网站 ID
  2778. $clonedLink->website_id = $newWebsiteId;
  2779. // 保存克隆后的记录到数据库
  2780. $clonedLink->save();
  2781. }
  2782. //克隆网站底部信息 footer_category 表 和footer_content 表 footer_category.id = footer_content.fcat_id 都是一对多关系
  2783. $originalFooterCategories = FooterCategory::where('website_id', $data['website_id'])->get();
  2784. foreach ($originalFooterCategories as $footerCategory) {
  2785. $clonedFooterCategory = $footerCategory->replicate();
  2786. $clonedFooterCategory->website_id = $newWebsiteId;
  2787. $clonedFooterCategory->save();
  2788. $originalFooterContents = FooterContent::where('fcat_id', $footerCategory->id)->get();
  2789. foreach ($originalFooterContents as $footerContent){
  2790. $clonedFooterContent = $footerContent->replicate();
  2791. $clonedFooterContent->fcat_id = $clonedFooterCategory->id;
  2792. $clonedFooterContent->save();
  2793. }
  2794. }
  2795. Db::commit();
  2796. return Result::success([]);
  2797. } catch (\Exception $e) {
  2798. Db::rollBack();
  2799. return Result::error($e->getMessage(), 0);
  2800. }
  2801. }
  2802. }