WebsiteService.php 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  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 ($result) {
  879. return Result::success($result);
  880. } else {
  881. return Result::error("没有网站", 0);
  882. }
  883. }
  884. public function addWebsiteCategory(array $data): array
  885. {
  886. $website_id = $data['website_id'];
  887. $category_arr_id = $data['category_arr_id'];
  888. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  889. $categoryListIds = [];
  890. if ($categoryList) {
  891. foreach ($categoryList->toArray() as $val) {
  892. array_push($categoryListIds, $val['id']);
  893. }
  894. }
  895. $arr = [];
  896. if ($categoryListIds) {
  897. foreach ($categoryListIds as $v) {
  898. $ids = $this->getUnderlingUIds(intval($v));
  899. $ids_arr = explode(",", $ids);
  900. array_push($arr, $ids_arr);
  901. }
  902. }
  903. $mergedArray = [];
  904. foreach ($arr as $subarray) {
  905. $mergedArray = array_merge($mergedArray, $subarray);
  906. }
  907. var_dump("所有:", $arr, $mergedArray);
  908. //查询出所有的分类进行分割插入 组装数据
  909. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  910. $categoryListData = $categoryListData->toArray();
  911. $insertData = [];
  912. if ($categoryListData) {
  913. foreach ($categoryListData as $key => $value) {
  914. $insertData[$key]['website_id'] = $website_id;
  915. $insertData[$key]['name'] = $value['name'];
  916. $insertData[$key]['sort'] = $value['sort'];
  917. $insertData[$key]['pid'] = $value['pid'];
  918. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  919. $insertData[$key]['seo_title'] = $value['seo_title'];
  920. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  921. $insertData[$key]['seo_description'] = $value['seo_description'];
  922. $insertData[$key]['alias'] = $value['name'];
  923. $insertData[$key]['category_id'] = $value['id'];
  924. }
  925. }
  926. $result = WebsiteCategory::insert($insertData);
  927. var_dump("插入数据状态:", $result);
  928. if ($result) {
  929. return Result::success($result);
  930. } else {
  931. return Result::error("创建失败", 0);
  932. }
  933. }
  934. /**
  935. * 删除网站导航
  936. * @param array $data
  937. * @return array
  938. */
  939. public function delWebsiteCategory(array $data): array
  940. {
  941. $website_id = $data['website_id'] ?? 0;
  942. $category_id = $data['category_id'] ?? 0;
  943. $ids = $this->getUnderlingUIds(intval($category_id));
  944. $ids_arr = explode(",", $ids);
  945. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  946. if ($result) {
  947. return Result::success($result);
  948. } else {
  949. return Result::error("删除失败", 0);
  950. }
  951. }
  952. /**
  953. * 获取网站导航
  954. * @param array $data
  955. * @return array
  956. */
  957. public function getAdminWebsiteCategory(array $data): array
  958. {
  959. $where = [
  960. 'website_id' => $data['website_id'],
  961. ];
  962. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  963. $list = [];
  964. if ($result) {
  965. foreach ($result->toArray() as $val) {
  966. array_push($list, json_decode($val['category_arr_id']));
  967. }
  968. }
  969. if ($result) {
  970. return Result::success($list);
  971. } else {
  972. return Result::error("查询失败", 0);
  973. }
  974. }
  975. /**
  976. * 更新网站导航
  977. * @param array $data
  978. * @return array
  979. */
  980. public function upWebsiteCategory(array $data): array
  981. {
  982. $pinyin = new Pinyin();
  983. Db::beginTransaction();
  984. try {
  985. if (!empty($data['category_arr_id'])) {
  986. $websiteId = $data['website_id'];
  987. // 1. 获取旧的 category_id 列表
  988. $oldCategoryListIds = WebsiteCategory::where('website_id', $websiteId)->pluck('category_id')->toArray();
  989. // 2. 获取新的 category_id 列表(去重)
  990. $newCategoryListIds = [];
  991. foreach ($data['category_arr_id'] as $arr) {
  992. $lastId = end($arr);
  993. $newCategoryListIds[] = $lastId;
  994. }
  995. $newCategoryListIds = array_unique($newCategoryListIds);
  996. // 3. 需要删除的 category_id
  997. $toDelete = array_diff($oldCategoryListIds, $newCategoryListIds);
  998. if (!empty($toDelete)) {
  999. WebsiteCategory::where('website_id', $websiteId)
  1000. ->whereIn('category_id', $toDelete)
  1001. ->delete();
  1002. }
  1003. // 4. 批量查出所有需要的 category 信息
  1004. $categories = Category::whereIn('id', $newCategoryListIds)->get()->keyBy('id');
  1005. // 5. 组装 upsertData,确保 category_id 不重复
  1006. $upsertData = [];
  1007. $handledCategoryIds = [];
  1008. foreach ($data['category_arr_id'] as $arr) {
  1009. $categoryId = end($arr);
  1010. if (!isset($categories[$categoryId]) || in_array($categoryId, $handledCategoryIds)) continue;
  1011. $info = $categories[$categoryId];
  1012. $aLIas_pinyin = $pinyin->permalink($info->name, '');
  1013. $upsertData[] = [
  1014. 'website_id' => $websiteId,
  1015. 'category_id' => $categoryId,
  1016. 'name' => $info->name ?? '',
  1017. 'alias' => $info->name ?? '',
  1018. 'aLIas_pinyin' => $aLIas_pinyin ?? '',
  1019. 'sort' => $info->sort ?? 0,
  1020. 'pid' => $info->pid ?? 0,
  1021. 'pid_arr' => $info->pid_arr ?? json_encode([]),
  1022. 'seo_title' => $info->seo_title ?? "",
  1023. 'seo_keywords' => $info->seo_keywords ?? "",
  1024. 'seo_description' => $info->seo_description ?? "",
  1025. 'is_url' => $info->is_url ?? 0,
  1026. 'web_url' => $info->web_url ?? '',
  1027. 'type' => $info->type ?? 1,
  1028. 'category_arr_id' => json_encode($arr)
  1029. ];
  1030. $handledCategoryIds[] = $categoryId;
  1031. }
  1032. // 6. upsert
  1033. if (!empty($upsertData)) {
  1034. WebsiteCategory::upsert(
  1035. $upsertData,
  1036. ['website_id', 'category_id'],
  1037. [
  1038. 'aLIas_pinyin',
  1039. 'pid',
  1040. 'pid_arr',
  1041. 'is_url',
  1042. 'web_url',
  1043. 'category_arr_id'
  1044. ]
  1045. );
  1046. }
  1047. }
  1048. Db::commit();
  1049. return Result::success();
  1050. } catch (\Throwable $ex) {
  1051. Db::rollBack();
  1052. return Result::error("修改失败" . $ex->getMessage(), 0);
  1053. }
  1054. }
  1055. /**
  1056. * 获取网站列表
  1057. * @param array $data
  1058. * @return array
  1059. */
  1060. public function getWebsiteCategoryList(array $data): array
  1061. {
  1062. $where = [];
  1063. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1064. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1065. }
  1066. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1067. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1068. }
  1069. $result = Website::where($where)
  1070. ->with(["websiteCategory" => function ($query) {
  1071. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1072. }])
  1073. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1074. ->get();
  1075. $count = Website::where($where)->count();
  1076. if (empty($result)) {
  1077. return Result::error("没有数据", 0);
  1078. }
  1079. $data = [
  1080. 'rows' => $result->toArray(),
  1081. 'count' => $count,
  1082. ];
  1083. if ($result) {
  1084. return Result::success($data);
  1085. } else {
  1086. return Result::error("查询失败", 0);
  1087. }
  1088. }
  1089. /**
  1090. * 删除网站下的所有导航
  1091. * @param array $data
  1092. * @return array
  1093. */
  1094. public function delWebsiteAllCategory(array $data): array
  1095. {
  1096. $website_id = $data['website_id'];
  1097. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1098. if ($result) {
  1099. return Result::success($result);
  1100. } else {
  1101. return Result::error("删除失败", 0);
  1102. }
  1103. }
  1104. /**
  1105. * 获取网站下的某一个导航
  1106. * @param array $data
  1107. * @return array
  1108. */
  1109. public function getWebsiteCategoryOnes(array $data): array
  1110. {
  1111. $website_id = $data['website_id'];
  1112. $category_id = $data['category_id'];
  1113. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1114. ->first();
  1115. if ($result) {
  1116. return Result::success($result);
  1117. } else {
  1118. return Result::error("查询失败", 0);
  1119. }
  1120. }
  1121. /**
  1122. * 更新网闸下的某一个导航
  1123. * @param array $data
  1124. * @return array
  1125. */
  1126. public function upWebsiteCategoryones(array $data): array
  1127. {
  1128. //处理所有数据
  1129. //获取所有数据
  1130. if (isset($data['all']) && $data['all'] == 'all') {
  1131. $result = WebsiteCategory::get();
  1132. $result = $result->toArray();
  1133. var_dump($result, '------------');
  1134. $pinyin = new Pinyin();
  1135. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1136. for ($i = 0; $i < count($result); $i++) {
  1137. // $randss = substr(str_shuffle($letters), 0, 4);
  1138. $result[$i]['aLIas_pinyin'] = $pinyin->permalink($result[$i]['alias'], '');
  1139. WebsiteCategory::where(['id' => $result[$i]['id']])->update(['aLIas_pinyin' => $result[$i]['aLIas_pinyin']]);
  1140. }
  1141. var_dump($result, '--------222----');
  1142. if ($result) {
  1143. return Result::success($result);
  1144. } else {
  1145. return Result::error("更新失败", 0);
  1146. }
  1147. }
  1148. //处理所有数据
  1149. $where = [
  1150. 'website_id' => $data['website_id'],
  1151. 'category_id' => $data['category_id'],
  1152. ];
  1153. $alias = $data['alias'];
  1154. $pinyin = new Pinyin();
  1155. $aLIas_pinyin = $pinyin->permalink($alias, '');
  1156. //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
  1157. $result = WebsiteCategory::where($where)->first();
  1158. $result = $result->toArray();
  1159. //+四位随机字母
  1160. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1161. // 打乱字符串顺序并截取前四位
  1162. // $randss = substr(str_shuffle($letters), 0, 4);
  1163. if ($data['alias_pinyin'] == '') {
  1164. $data['aLIas_pinyin'] = $aLIas_pinyin;
  1165. }
  1166. $result = WebsiteCategory::where($where)->update($data);
  1167. if ($result) {
  1168. return Result::success($result);
  1169. } else {
  1170. return Result::error("更新失败", 0);
  1171. }
  1172. }
  1173. /**
  1174. * 获取网站下的所有导航(包含子导航)
  1175. * @param array $data
  1176. * @return array
  1177. */
  1178. public function getWebsiteAllCategory(array $data): array
  1179. {
  1180. $where = [];
  1181. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1182. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1183. }
  1184. // if (isset($data['name']) && !empty($data['name'])) {
  1185. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1186. // }
  1187. // if (isset($data['alias']) && !empty($data['alias'])) {
  1188. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1189. // }
  1190. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1191. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1192. // }
  1193. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1194. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1195. // }
  1196. $result = WebsiteCategory::where($where)
  1197. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1198. ->select("website_category.*", "category.name as name")
  1199. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1200. // ->leftJoin("department", 'category.department_id', 'department.id')
  1201. // ->leftJoin("district", 'category.city_id', 'district.id')
  1202. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1203. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1204. ->orderBy("sort", "asc")
  1205. ->get();
  1206. // $count = WebsiteCategory::where($where)
  1207. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1208. // ->leftJoin("department", 'category.department_id', 'department.id')
  1209. // ->leftJoin("district", 'category.city_id', 'district.id')
  1210. // ->count();
  1211. if (empty($result)) {
  1212. return Result::error("没有数据", 0);
  1213. }
  1214. // $data = [
  1215. // 'rows' => $result->toArray(),
  1216. // 'count' => $count,
  1217. // ];
  1218. if ($result) {
  1219. return Result::success($result->toArray());
  1220. } else {
  1221. return Result::error("查询失败", 0);
  1222. }
  1223. }
  1224. /**
  1225. * 递归查询数据
  1226. * @param $id
  1227. * @param $ids
  1228. * @return string
  1229. */
  1230. public function getUnderlingUIds($id, $ids = '')
  1231. {
  1232. $back = Category::where(['pid' => $id])->get();
  1233. $back = $back->toArray();
  1234. if (!empty($back) && is_array($back)) {
  1235. foreach ($back as $v) {
  1236. //防止当前人的ID重复去查询,形成恶性循环
  1237. if ($v['id'] == $id) {
  1238. continue;
  1239. }
  1240. $back2 = Category::where(['pid' => $id])->count('id');
  1241. if ($back2 > 0) {
  1242. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1243. } else {
  1244. $ids .= ',' . $v['id'];
  1245. }
  1246. }
  1247. }
  1248. $ids = $id . ',' . $ids . ',';
  1249. $ids = str_replace(',,', ",", $ids);
  1250. $ids = trim($ids, ',');
  1251. return $ids;
  1252. }
  1253. /**
  1254. * 检测网站名称是否重复
  1255. * @param array $data
  1256. * @return array
  1257. */
  1258. public function checkWebsiteName(array $data): array
  1259. {
  1260. if (isset($data['id'])) {
  1261. $data[] = ['id', "!=", $data['id']];
  1262. unset($data['id']);
  1263. }
  1264. $websiteInfo = Website::query()->where($data)->first();
  1265. if (empty($websiteInfo)) {
  1266. return Result::error("找不到网站", 0);
  1267. }
  1268. return Result::success($websiteInfo->toArray());
  1269. }
  1270. /**
  1271. * 检测网站url是否重复
  1272. * @param array $data
  1273. * @return array
  1274. */
  1275. public function checkWebsiteUrl(array $data): array
  1276. {
  1277. $whereData = [];
  1278. if (isset($data['id'])) {
  1279. $whereData = [['id', "!=", $data['id']]];
  1280. unset($data['id']);
  1281. }
  1282. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1283. if (empty($websiteInfo)) {
  1284. return Result::error("找不到URL", 0);
  1285. }
  1286. return Result::success($websiteInfo->toArray());
  1287. }
  1288. /**
  1289. * 获取网站底部基础信息
  1290. * @param array $data
  1291. * @return array
  1292. */
  1293. public function getWebsiteFootInfo(array $data): array
  1294. {
  1295. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1296. $website_head = Website::where('id', $data['website_id'])
  1297. ->where('status', 1)
  1298. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1299. ->first();
  1300. if (empty($website_head)) {
  1301. return Result::error("找不到网站", 0);
  1302. }
  1303. } else {
  1304. return Result::error("参数错误", 0);
  1305. }
  1306. $website_head = Website::where('id', $data['website_id'])
  1307. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1308. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1309. if (empty($website_foot)) {
  1310. return Result::error("暂无底部基础信息", 0);
  1311. }
  1312. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1313. $result = [
  1314. 'website_foot' => $website_foot,
  1315. 'website_head' => $website_head,
  1316. ];
  1317. return Result::success($result);
  1318. }
  1319. /**
  1320. * 获取网站底部导航
  1321. * @param array $data
  1322. * @return array
  1323. */
  1324. public function getWebsiteFooterCategory(array $data): array
  1325. {
  1326. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1327. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1328. if (empty($website)) {
  1329. return Result::error("找不到网站", 0);
  1330. }
  1331. } else {
  1332. return Result::error("参数错误", 0);
  1333. }
  1334. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1335. if (empty($result)) {
  1336. return Result::error("暂无底部导航", 0);
  1337. }
  1338. return Result::success($result->toArray());
  1339. }
  1340. /**
  1341. * 获取网站底部导航列表
  1342. * @param array $data
  1343. * @return array
  1344. */
  1345. public function getWebsiteFooterCategoryList(array $data): array
  1346. {
  1347. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1348. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1349. if (empty($website)) {
  1350. return Result::error("找不到网站", 0);
  1351. }
  1352. } else {
  1353. return Result::error("参数错误", 0);
  1354. }
  1355. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1356. // '底部导航类型 0:内容型;1:列表型;',
  1357. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1358. return Result::error("底部导航id错误", 0);
  1359. } else {
  1360. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1361. if ($query->count() == 0) {
  1362. return Result::error("暂无底部导航列表", 0);
  1363. } elseif ($query->count() == 1) {
  1364. $result = $query->first();
  1365. } else {
  1366. $result = $query->get();
  1367. }
  1368. }
  1369. return Result::success($result);
  1370. }
  1371. /**
  1372. * 获取网站底部导航
  1373. * @param array $data
  1374. * @return array
  1375. */
  1376. public function getWebsiteFooterCategoryInfo(array $data): array
  1377. {
  1378. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1379. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1380. if (empty($website)) {
  1381. return Result::error("找不到网站", 0);
  1382. }
  1383. } else {
  1384. return Result::error("参数错误", 0);
  1385. }
  1386. if (isset($data['type']) && $data['type'] == 0) {
  1387. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1388. if (empty($fcatid)) {
  1389. return Result::error("底部导航id错误", 0);
  1390. }
  1391. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1392. } else {
  1393. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1394. }
  1395. if (empty($result)) {
  1396. return Result::error("暂无底部导航内容", 0);
  1397. }
  1398. return Result::success($result);
  1399. }
  1400. /*
  1401. * 搜索网站二级导航
  1402. * @param array $data
  1403. * @return array
  1404. * */
  1405. public function selectWebsiteCategory(array $data): array
  1406. {
  1407. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1408. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1409. if (empty($website)) {
  1410. return Result::error("暂无该网站", 0);
  1411. }
  1412. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1413. // return Result::success($category);
  1414. if (empty($category)) {
  1415. return Result::error("暂无此导航", 0);
  1416. }
  1417. $query = Category::whereIn('id', $category);
  1418. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1419. $cityid = District::where('id', $data['cityid'])->first();
  1420. if (empty($cityid)) {
  1421. return Result::error("暂无此城市", 0);
  1422. } else {
  1423. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1424. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1425. if (empty($city_category)) {
  1426. return Result::error("暂无此城市下的导航", 0);
  1427. }
  1428. $result['catid'] = $city_category;
  1429. $city = 1;
  1430. // var_dump("城市====================",$result);
  1431. }
  1432. }
  1433. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1434. $departmentid = Department::where('id', $data['department_id'])->first();
  1435. if (empty($departmentid)) {
  1436. return Result::error("暂无此部门", 0);
  1437. } else {
  1438. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1439. if (empty($depart_category)) {
  1440. return Result::error("暂无此部门下的导航", 0);
  1441. }
  1442. $result['catid'] = $depart_category;
  1443. // var_dump("职能部门*******************",$result);
  1444. $department = 1;
  1445. }
  1446. }
  1447. if (!empty($city) && !empty($department)) {
  1448. // var_dump("城市和职能部门----------------------",$result);
  1449. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1450. if (empty($sel_category)) {
  1451. return Result::error("暂无此城市和职能部门下的导航", 0);
  1452. }
  1453. $result['catid'] = $sel_category;
  1454. }
  1455. if (empty($result)) {
  1456. return Result::error("暂无导航", 0);
  1457. }
  1458. // department_id
  1459. } else {
  1460. return Result::error("参数错误", 0);
  1461. }
  1462. return Result::success($result);
  1463. }
  1464. /**
  1465. * 获取网站栏目seo
  1466. * @param array $data
  1467. * @return array
  1468. */
  1469. public function getWebsiteCategoryHead(array $data): array
  1470. {
  1471. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1472. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1473. if (empty($website)) {
  1474. return Result::error("找不到网站", 0);
  1475. }
  1476. } else {
  1477. return Result::error("参数错误", 0);
  1478. }
  1479. if (isset($data['catid']) && !empty($data['catid'])) {
  1480. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1481. }
  1482. if (empty($result)) {
  1483. return Result::error("暂无导航", 0);
  1484. }
  1485. $result['website_name'] = $website['website_name'] ?? '';
  1486. $result['suffix'] = $website['suffix'] ?? '';
  1487. return Result::success($result);
  1488. }
  1489. /*
  1490. * 获取某个栏目
  1491. * @param array $data
  1492. * @return array
  1493. * */
  1494. public function getOneWebsiteCategory(array $data): array
  1495. {
  1496. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1497. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1498. if (empty($website)) {
  1499. return Result::error("暂无该网站", 0);
  1500. }
  1501. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1502. if (empty($category)) {
  1503. return Result::error("暂无此导航", 0);
  1504. }
  1505. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1506. $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();
  1507. $category['parent_id'] = $parent['parent_id'] ?? '';
  1508. $category['parent_pinyin'] = $parent['aLIas_pinyin'] ?? '';
  1509. $category['parent_name'] = $parent['parent_name'] ?? '';
  1510. return Result::success($category);
  1511. } else {
  1512. return Result::error("参数错误", 0);
  1513. }
  1514. }
  1515. // 自助建站--fr-------------------
  1516. /**
  1517. * 获取并搜索 网站模板信息
  1518. * @param array $data
  1519. * @return array
  1520. */
  1521. public function getWebsiteintel(array $data): array
  1522. {
  1523. //查询所有网站模板信息
  1524. $query = Website::where('website.status', 1)
  1525. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1526. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1527. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1528. ->select(
  1529. "website_template_info.id",
  1530. "template_class.name",
  1531. "website.id as website_id",
  1532. "website.website_name",
  1533. "website.website_url",
  1534. "website_template_info.action_id",
  1535. "website_template_info.created_at",
  1536. "website_template_info.updated_at",
  1537. "website_template_info.page_type",
  1538. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1539. "template.template_name"
  1540. );
  1541. //若存在条件;则在$query的基础上进行筛选
  1542. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1543. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1544. }
  1545. if (isset($data['status']) && !empty($data['status'])) {
  1546. $query->where('website_template_info.status', $data['status']);
  1547. }
  1548. $count = $query->count();
  1549. $query->limit($data['pageSize'])
  1550. ->offset(($data['page'] - 1) * $data['pageSize'])
  1551. ->orderBy("website_template_info.updated_at", "desc");
  1552. $rep = $query->get();
  1553. if ($rep->count() == 0) {
  1554. return Result::error("没有查找到相关数据", 0);
  1555. } else {
  1556. $rep->each(function ($item) {
  1557. if (!empty($item->page_type)) {
  1558. $pageTypeArray = json_decode($item->page_type, true);
  1559. if (is_array($pageTypeArray)) {
  1560. $item->page_type = $pageTypeArray;
  1561. }
  1562. }
  1563. });
  1564. $result = [
  1565. "rows" => $rep->toArray(),
  1566. "count" => $count,
  1567. ];
  1568. return Result::success($result);
  1569. }
  1570. // return Result::success($result);
  1571. }
  1572. /**
  1573. * 添加网站基础信息
  1574. * @param array $data
  1575. * @return array
  1576. */
  1577. public function addWebsiteTemplateintel(array $data): array
  1578. {
  1579. $website = Website::where('website.id', $data['website_id'])->first();
  1580. if (empty($website)) {
  1581. return Result::error("请输入正确的网站id!", 0);
  1582. }
  1583. if (empty($website['website_column_id'])) {
  1584. return Result::error("请先关联导航池!", 0);
  1585. }
  1586. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1587. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1588. if (empty($foot_type)) {
  1589. return Result::error("请先关联底部导航池!", 0);
  1590. } elseif (in_array(1, $foot_type)) {
  1591. $foot_type = 1;
  1592. } else {
  1593. $foot_type = 0;
  1594. }
  1595. $result['foot_type'] = $foot_type;
  1596. // 友情链接类型'1:图片 2:文字 3:底部';
  1597. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1598. $missingTypes = [];
  1599. if (!in_array(1, $types)) {
  1600. $missingTypes[] = "文字链接";
  1601. }
  1602. if (!in_array(2, $types)) {
  1603. $missingTypes[] = "图片链接";
  1604. }
  1605. if (!in_array(3, $types)) {
  1606. $missingTypes[] = "底部链接";
  1607. }
  1608. if (!empty($missingTypes)) {
  1609. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1610. return Result::error($errorMessage, 0);
  1611. }
  1612. if (isset($data['page_type'])) {
  1613. // 删除重复元素并重新索引数组
  1614. $data['page_type'] = array_unique($data['page_type']);
  1615. $data['page_type'] = array_values($data['page_type']);
  1616. // 1:首页 2:分类页 3:列表页 4:详情页 5:搜索页 6:特殊列表 7:特殊详情页
  1617. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1618. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1619. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1620. if (empty($info)) {
  1621. // 将数组转换为 JSON 字符串
  1622. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1623. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1624. $data['action_id'] = 1;
  1625. try {
  1626. $result = WebsiteTemplateInfo::insertGetId($data);
  1627. } catch (\Exception $e) {
  1628. if ($e->getCode() == 22001) {
  1629. $errorMessage = $e->getMessage();
  1630. return Result::error("请检查表单,某个字段超出了长度限制!");
  1631. } else {
  1632. // 处理其他类型的数据库错误
  1633. return Result::error("插入操作失败: " . $e->getMessage());
  1634. }
  1635. return Result::error("添加失败!", 0);
  1636. }
  1637. } else {
  1638. return Result::error("该网站已经构建过了!", 0);
  1639. }
  1640. } else {
  1641. return Result::error("请先选择首页和底部导航详情页!", 0);
  1642. }
  1643. }
  1644. if (empty($result)) {
  1645. return Result::error("添加失败!", 0);
  1646. } else {
  1647. return Result::success($result);
  1648. }
  1649. }
  1650. /**
  1651. * 获取网站基础信息
  1652. * @return array
  1653. *
  1654. */
  1655. public function getWebsiteTemplateintel(array $data): array
  1656. {
  1657. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])
  1658. ->leftJoin('footer_category', 'footer_category.website_id', 'website_template_info.website_id')
  1659. ->leftJoin('website', 'website.id', 'website_template_info.website_id')
  1660. ->select(
  1661. "website_template_info.*",
  1662. "footer_category.type",
  1663. "website.website_name",
  1664. "website.id as website_id"
  1665. )
  1666. ->first();
  1667. if (empty($result['website_name'])) {
  1668. return Result::error("请输入正确的网站id!", 0);
  1669. }
  1670. if ($result) {
  1671. $result['page_type'] = json_decode($result['page_type'], true);
  1672. return Result::success($result);
  1673. } else {
  1674. return Result::error("请先添加网站基础信息!", 0);
  1675. }
  1676. }
  1677. /**
  1678. * 修改网站基础信息
  1679. *
  1680. * @return array
  1681. */
  1682. public function upWebsiteTemplateintel(array $data): array
  1683. {
  1684. $web = Website::where('website.id', $data['website_id'])->first();
  1685. if (empty($web)) {
  1686. return Result::error("请输入正确的网站id!", 0);
  1687. }
  1688. $where = ['website_id' => $data['website_id']];
  1689. $result = WebsiteTemplateInfo::where($where)->first();
  1690. if (empty($result)) {
  1691. return Result::error("请先添加网站基础信息!", 0);
  1692. } else {
  1693. unset($data['website_id']);
  1694. // 删除重复元素
  1695. $data['page_type'] = array_unique($data['page_type']);
  1696. // 重新索引数组
  1697. $data['page_type'] = array_values($data['page_type']);
  1698. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1699. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1700. // 将数组转换为 JSON 字符串
  1701. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1702. try {
  1703. $result = WebsiteTemplateInfo::where($where)->update($data);
  1704. } catch (\Exception $e) {
  1705. if ($e->getCode() == 22001) {
  1706. $errorMessage = $e->getMessage();
  1707. return Result::error("请检查表单,某个字段超出了长度限制!");
  1708. } else {
  1709. // 处理其他类型的数据库错误
  1710. return Result::error("修改操作失败: " . $e->getMessage());
  1711. }
  1712. return Result::error("修改失败!", 0);
  1713. }
  1714. } else {
  1715. return Result::error("请先选择首页和底部导航详情页!", 0);
  1716. }
  1717. }
  1718. if (empty($result)) {
  1719. return Result::error("修改失败!", 0);
  1720. } else {
  1721. return Result::success($result);
  1722. }
  1723. }
  1724. /**
  1725. * 获取所有网站风格信息
  1726. *
  1727. * @return array
  1728. */
  1729. public function getAllTemplateClass(array $data): array
  1730. {
  1731. $result = TemplateClass::all();
  1732. if (empty($result)) {
  1733. return Result::error("没有查找到相关数据!", 0);
  1734. } else {
  1735. return Result::success($result);
  1736. }
  1737. }
  1738. /**
  1739. * 搜索并获取网站模板信息
  1740. * @param array $data
  1741. * @return array
  1742. */
  1743. public function getWebsiteTemplateList(array $data): array
  1744. {
  1745. $website = Website::where('website.id', $data['website_id'])->first();
  1746. if (empty($website)) {
  1747. return Result::error("请输入正确的网站id!", 0);
  1748. } else {
  1749. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1750. if (empty($page_type)) {
  1751. return Result::error("此网站还未添加基础信息!", 0);
  1752. } else {
  1753. $where = json_decode($page_type['page_type'], true);
  1754. // $where = $data["page_type"];
  1755. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1756. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1757. if (empty($template_class)) {
  1758. return Result::error("请输入正确的模板风格id!", 0);
  1759. } else {
  1760. // 获取指定风格下的模板
  1761. $rep = Template::where('template_class_id', $data['template_class_id'])
  1762. ->where(function ($query) use ($where) {
  1763. foreach ($where as $value) {
  1764. $query->whereJsonContains('template_img', ['value' => $value]);
  1765. }
  1766. })
  1767. ->limit($data['pageSize'])
  1768. ->offset(($data['page'] - 1) * $data['pageSize'])
  1769. ->orderBy("template.updated_at", "desc")
  1770. ->get();
  1771. $count = Template::where('template_class_id', $data['template_class_id'])
  1772. ->where(function ($query) use ($where) {
  1773. foreach ($where as $value) {
  1774. $query->whereJsonContains('template_img', ['value' => $value]);
  1775. }
  1776. })->count();
  1777. }
  1778. } else {
  1779. //获取所有模板
  1780. $rep = Template::where(function ($query) use ($where) {
  1781. foreach ($where as $value) {
  1782. $query->whereJsonContains('template_img', ['value' => $value]);
  1783. }
  1784. })
  1785. ->limit($data['pageSize'])
  1786. ->offset(($data['page'] - 1) * $data['pageSize'])
  1787. ->orderBy("template.updated_at", "desc")
  1788. ->get();
  1789. $count = Template::where(function ($query) use ($where) {
  1790. foreach ($where as $value) {
  1791. $query->whereJsonContains('template_img', ['value' => $value]);
  1792. }
  1793. })->count();
  1794. }
  1795. if (empty($rep) || $rep->count() == 0) {
  1796. return Result::error("没有查找到相关数据!", 0);
  1797. } else {
  1798. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1799. // $rep->each(function ($item) use ($where) {
  1800. // $template_img = json_decode($item->template_img, true);
  1801. // if(is_array($template_img)){
  1802. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1803. // return isset($img['value']) && in_array($img['value'], $where);
  1804. // });
  1805. // }
  1806. // $item->template_img = $filtered_img;
  1807. // });
  1808. // $result = [
  1809. // "rows" => $rep->toArray(),
  1810. // "count" => $count
  1811. // ];
  1812. $rep->each(function ($item) use ($where) {
  1813. $template_img = json_decode($item->template_img, true);
  1814. if (is_array($template_img)) {
  1815. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1816. return isset($img['value']) && in_array($img['value'], $where);
  1817. });
  1818. // 对过滤后的数组进行重新排序
  1819. $sorted_img = array_values($filtered_img);
  1820. $item->template_img = $sorted_img;
  1821. } else {
  1822. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1823. }
  1824. });
  1825. $result = [
  1826. "rows" => $rep->toArray(),
  1827. "count" => $count,
  1828. ];
  1829. }
  1830. }
  1831. }
  1832. return Result::success($result);
  1833. }
  1834. /**
  1835. * 添加网站模板
  1836. * @param array $data
  1837. */
  1838. public function addWebsiteTemplateclassintel(array $data): array
  1839. {
  1840. $template = Template::where('id', $data['template_id'])->first();
  1841. $web = Website::where('id', $data['website_id'])->first();
  1842. if (empty($template)) {
  1843. return Result::error("请输入正确的模板id", 0);
  1844. }
  1845. if (empty($web)) {
  1846. return Result::error("请输入正确的网站id", 0);
  1847. }
  1848. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1849. if (empty($result)) {
  1850. return Result::error("添加失败", 0);
  1851. } else {
  1852. return Result::success($result);
  1853. }
  1854. }
  1855. /**
  1856. * 获取网站模板信息
  1857. * @param array $data
  1858. */
  1859. public function getWebsiteTemplateclassintel(array $data): array
  1860. {
  1861. // return Result::success($data);
  1862. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1863. if (empty($template)) {
  1864. return Result::error("请输入正确的搭建网站id", 0);
  1865. }
  1866. $page_type = json_decode($template['page_type'], true);
  1867. // 获取指定网站下的基础信息和模板及风格信息
  1868. $result = WebsiteTemplateInfo::where([
  1869. ['website_template_info.website_id', $data['website_id']],
  1870. ['website_template_info.template_id', '!=', null],
  1871. ])
  1872. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1873. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1874. ->where(function ($query) use ($page_type) {
  1875. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1876. foreach ($page_type as $pageType) {
  1877. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1878. }
  1879. })
  1880. ->select(
  1881. 'template.template_name',
  1882. 'template.template_img',
  1883. 'template.id as tid',
  1884. 'template_class.name',
  1885. 'template_class.id as class_id',
  1886. 'website_template_info.page_type',
  1887. 'website_template_info.website_id'
  1888. )
  1889. ->first();
  1890. if (empty($result)) {
  1891. return Result::error("没有查找到相关数据", 0);
  1892. }
  1893. // return Result::success($result);
  1894. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1895. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1896. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1897. foreach ($template_img as $key => $value) {
  1898. if (!in_array($value['value'], $page_type)) {
  1899. unset($template_img[$key]);
  1900. }
  1901. }
  1902. // return Result::success($result);
  1903. $result['template_img'] = array_values($template_img);
  1904. $result['page_type'] = $page_type;
  1905. // $templateList = $this->getWebsiteTemplateList($data);
  1906. // $count = $templateList['data']['count']??'';
  1907. // $pages = $count / $data['pageSize'];
  1908. // $found = false;
  1909. // while ($pages && !$found) {
  1910. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1911. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1912. // if ($index !== false) {
  1913. // $found = true;
  1914. // $result['page'] = $pages;
  1915. // $pages = 0;
  1916. // // echo "查询到的 template 在第 $page 页";
  1917. // }
  1918. // $pages--;
  1919. // }
  1920. return Result::success($result);
  1921. }
  1922. /**
  1923. * 获取网站模板下的板块信息
  1924. * @param array $data
  1925. */
  1926. public function getWebsiteSectorList(array $data): array
  1927. {
  1928. $website = Website::where('id', $data['website_id'])->first();
  1929. if (empty($website)) {
  1930. return Result::error("请输入正确的网站id", 0);
  1931. }
  1932. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1933. if (empty($template['template_id'])) {
  1934. return Result::error("请先添加网站模板", 0);
  1935. }
  1936. $query = Sector::where('template_id', $template['template_id']);
  1937. if ($query->count() == 0) {
  1938. return Result::error("没有查找到相关板块数据", 0);
  1939. } elseif ($query->count() == 1) {
  1940. $sector = $query->first();
  1941. } else {
  1942. $sector = $query->get();
  1943. }
  1944. $result['page_type'] = $template['page_type'];
  1945. $result['tid'] = $template['template_id'];
  1946. $result = [
  1947. "tid" => $template['template_id'],
  1948. "page_type" => $template['page_type'],
  1949. "sector" => $sector,
  1950. ];
  1951. return Result::success($result);
  1952. }
  1953. // 自助建站---------------fr---------------
  1954. // 网站底基础信息--fr-------------------
  1955. /**
  1956. * 添加网站底基础信息
  1957. * @param array $data
  1958. */
  1959. public function addWebFootInfo(array $data): array
  1960. {
  1961. $website = Website::where('id', $data['website_id'])->first();
  1962. if (empty($website)) {
  1963. return Result::error("请输入正确的网站id!", 0);
  1964. }
  1965. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1966. if (!empty($info)) {
  1967. return Result::error("该网站已经添加过了!", 0);
  1968. }
  1969. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  1970. if (empty($result)) {
  1971. return Result::error("请先添加网站基础信息!", 0);
  1972. } else {
  1973. return Result::success($result);
  1974. }
  1975. }
  1976. /**
  1977. * 获取网站底基础信息
  1978. * @param array $data
  1979. */
  1980. public function getWebFootInfo(array $data): array
  1981. {
  1982. $website = Website::where('id', $data['website_id'])->first();
  1983. if (empty($website)) {
  1984. return Result::error("请输入正确的网站id!", 0);
  1985. }
  1986. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1987. if (empty($result)) {
  1988. return Result::error("请先添加网站基础信息!", 0);
  1989. }
  1990. return Result::success($result);
  1991. }
  1992. /**
  1993. * 修改网站底基础信息
  1994. * @param array $data
  1995. */
  1996. public function upWebFootInfo(array $data): array
  1997. {
  1998. // return Result::success($data);
  1999. $website = Website::where('id', $data['website_id'])->first();
  2000. if (empty($website)) {
  2001. return Result::error("请输入正确的网站id!", 0);
  2002. }
  2003. $web = $data['website_id'];
  2004. unset($data['website_id']);
  2005. $where = ['website_id' => $web];
  2006. // return Result::success($data);
  2007. $result = WebsiteTemplateInfo::where($where)->first();
  2008. if (empty($result)) {
  2009. $data['website_id'] = $web;
  2010. $result = WebsiteTemplateInfo::insertGetId($data);
  2011. // return Result::error("请先添加网站基础信息!",0);
  2012. } else {
  2013. $result = WebsiteTemplateInfo::where($where)->update($data);
  2014. }
  2015. // $where = ['website_id'=>$data['website_id']];
  2016. // unset($data['website_id']);
  2017. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  2018. if (empty($result)) {
  2019. return Result::error("修改失败!", 0);
  2020. }
  2021. return Result::success($result);
  2022. }
  2023. /*
  2024. * 获取父级/子级栏目------路由
  2025. * @param array $data
  2026. * @return array
  2027. * */
  2028. public function getWebsiteParentCategory(array $data): array
  2029. {
  2030. // return Result::success($data);
  2031. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2032. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2033. }
  2034. if (empty($website)) {
  2035. return Result::error("暂无该网站", 0);
  2036. }
  2037. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2038. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2039. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2040. ->where('pid', 0)
  2041. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2042. ->get() // 添加 get() 方法获取结果集合
  2043. ->all();
  2044. // 提前查询所有父级栏目,避免在 map 中多次查询
  2045. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2046. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2047. ->where('pid', 0)
  2048. ->select('category_id', 'aLIas_pinyin')
  2049. ->get()
  2050. ->keyBy('category_id');
  2051. // 直接查询子栏目信息
  2052. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2053. ->where('pid', '!=', 0)
  2054. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2055. ->get();
  2056. // 使用集合的 map 方法处理子栏目信息
  2057. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2058. $alias = $item->alias;
  2059. $category_id = $item->category_id;
  2060. $aLIas_pinyin = $item->aLIas_pinyin;
  2061. $parent = $parentCategories->get($item->pid);
  2062. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2063. $result = [
  2064. 'name' => $alias,
  2065. 'cid' => $category_id
  2066. ];
  2067. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2068. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2069. }
  2070. return $result;
  2071. })->values()->all();
  2072. if (empty($category)) {
  2073. return Result::error("暂无此导航", 0);
  2074. }
  2075. return Result::success($category);
  2076. }
  2077. /**
  2078. * 获取网站栏目信息
  2079. * @param array $data
  2080. */
  2081. public function getWebsiteFootAll(array $data): array
  2082. {
  2083. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2084. if (empty($website)) {
  2085. return Result::error("暂无该网站", 0);
  2086. }
  2087. // 1:图片 2:文字 3:底部
  2088. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2089. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2090. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2091. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2092. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2093. if (empty($result)) {
  2094. return Result::error("暂无此网站信息", 0);
  2095. }
  2096. return Result::success($result);
  2097. }
  2098. /**
  2099. * 获取网站顶部信息
  2100. * @param array $data
  2101. */
  2102. public function getWebsiteHead(array $data): array
  2103. {
  2104. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2105. if (empty($result)) {
  2106. return Result::error("暂无该网站", 0);
  2107. }
  2108. return Result::success($result);
  2109. }
  2110. /**
  2111. * 路由匹配
  2112. * @param array $data
  2113. */
  2114. public function getWebsiteRoute(array $data): array
  2115. {
  2116. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2117. if (empty($website)) {
  2118. return Result::error("暂无该网站", 0);
  2119. }
  2120. if (isset($data['pinyin']) && !empty($data['pinyin'])) {
  2121. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2122. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2123. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2124. $join->on('pc.pid', '=', 'website_category.category_id')
  2125. ->where('pc.website_id', '=', $data['website_id']);
  2126. })
  2127. ->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'))
  2128. ->first();
  2129. }
  2130. if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
  2131. $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
  2132. }
  2133. if (!isset($result) || empty($result)) {
  2134. return Result::error("暂无该导航", 0);
  2135. }
  2136. return Result::success($result);
  2137. }
  2138. //20250212 网站标识
  2139. public function addWebsiteGroup(array $data): array
  2140. {
  2141. //判斷name是不是重复
  2142. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2143. if (!empty($websiteGroupInfo)) {
  2144. return Result::error("网站标识重复", 0);
  2145. }
  2146. //添加信息
  2147. $result = WebsiteGroup::insertGetId($data);
  2148. var_dump($result);
  2149. if (empty($result)) {
  2150. return Result::error("创建失败", 0);
  2151. } else {
  2152. return Result::success($result);
  2153. }
  2154. }
  2155. public function getWebsiteGroupList(array $data): array
  2156. {
  2157. $where = [];
  2158. if (isset($data['name']) && !empty($data['name'])) {
  2159. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2160. }
  2161. $result = WebsiteGroup::where($where)
  2162. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2163. ->get();
  2164. foreach ($result as $websiteGroup) {
  2165. $webIds = json_decode($websiteGroup->web_ids, true);
  2166. $websites = Website::whereIn('id', $webIds)->get();
  2167. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2168. $websiteGroup->website_names = $websiteNames;
  2169. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2170. }
  2171. $count = WebsiteGroup::where($where)->count();
  2172. if (empty($result)) {
  2173. return Result::error("没有数据", 0);
  2174. }
  2175. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2176. }
  2177. public function getWebsiteGroupInfo(array $data): array
  2178. {
  2179. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2180. $webIds = json_decode($websiteInfo->web_ids, true);
  2181. $websites = Website::whereIn('id', $webIds)->get();
  2182. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2183. $websiteInfo->website_names = $websiteNames;
  2184. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2185. if (empty($websiteInfo)) {
  2186. return Result::error("找不到URL", 0);
  2187. }
  2188. return Result::success($websiteInfo->toArray());
  2189. }
  2190. public function updateWebsiteGroup(array $data): array
  2191. {
  2192. $where = [
  2193. 'id' => $data['id'],
  2194. ];
  2195. $insertData = [
  2196. 'name' => $data['name'],
  2197. 'web_ids' => $data['web_ids'],
  2198. ];
  2199. //判斷name是不是重复
  2200. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2201. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2202. return Result::error("网站标识重复", 0);
  2203. }
  2204. $result = WebsiteGroup::where($where)->update($insertData);
  2205. var_dump($result, '------更新');
  2206. if (empty($result)) {
  2207. return Result::error("更新失败", 0);
  2208. } else {
  2209. return Result::success();
  2210. }
  2211. }
  2212. public function deleteWebsiteGroup(array $data): array
  2213. {
  2214. $where = [
  2215. 'id' => $data['id'],
  2216. ];
  2217. //看看user表是不是有这个id
  2218. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2219. if (!empty($userGroup)) {
  2220. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2221. }
  2222. $result = WebsiteGroup::where($where)->delete();
  2223. if (empty($result)) {
  2224. return Result::error("删除失败", 0);
  2225. } else {
  2226. return Result::success();
  2227. }
  2228. }
  2229. public function getWebsiteNavList(array $data): array
  2230. {
  2231. $data['type'] = isset($data['type']) ? $data['type'] : 1;
  2232. $where = [];
  2233. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2234. array_push($where, ['website_id', '=', $data['website_id']]);
  2235. }
  2236. if (isset($data['pid'])) {
  2237. array_push($where, ['pid', '=', $data['pid']]);
  2238. }
  2239. if (isset($data['type']) && !empty($data['type'])) {
  2240. array_push($where, ['type', '=', $data['type']]);
  2241. }
  2242. // 根据站点和type获取websiteCategory 中的id
  2243. $websiteCategoryids = WebsiteCategory::query()
  2244. ->where('website_id', '=', $data['website_id'])
  2245. ->where('type', '=', $data['type'])
  2246. ->pluck('category_arr_id')->toArray();
  2247. var_dump($websiteCategoryids, '------获取websiteCategory 中的id');
  2248. //合并去重
  2249. $category_ids = [];
  2250. foreach ($websiteCategoryids as $key => $value) {
  2251. $v = json_decode($value, true);
  2252. $category_ids = array_unique(array_merge($category_ids, $v));
  2253. }
  2254. // var_dump($category_ids, '------获取websiteCategory 中的id');
  2255. //根据pid取出所有的websiteCategory 中的id
  2256. $list = WebsiteCategory::query()
  2257. ->where('website_id', '=', $data['website_id'])
  2258. ->where('pid', '=', $data['pid'])
  2259. ->whereIn('category_id', $category_ids)
  2260. ->get();
  2261. if (empty($list)) {
  2262. return Result::error("获取失败", 0);
  2263. } else {
  2264. return Result::success($list);
  2265. }
  2266. }
  2267. // 20250307 根据网站标识获和导航获取站点
  2268. public function getWebsiteNavPoolSite(array $data): array
  2269. {
  2270. $where = [];
  2271. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2272. array_push($where, ['category_id', '=', $data['category_id']]);
  2273. }
  2274. $list = WebsiteCategory::query()->where($where)
  2275. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2276. ->select('website_category.*', 'website.website_name')
  2277. ->distinct()
  2278. ->get();
  2279. if (empty($list)) {
  2280. return Result::error("获取失败", 0);
  2281. } else {
  2282. return Result::success($list);
  2283. }
  2284. }
  2285. public function getWebsiteNavPool(array $data): array
  2286. {
  2287. // 初始化查询条件数组
  2288. $query = WebsiteCategory::query();
  2289. // 如果传入了 websiteids,则添加到查询条件中
  2290. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2291. $query->whereIn('website_id', $data['websiteids']);
  2292. }
  2293. // 如果传入了 pid,则添加到查询条件中
  2294. if (isset($data['pid'])) {
  2295. $query->where('pid', '=', $data['pid']);
  2296. }
  2297. // 执行查询 唯一性 id
  2298. $list = $query->distinct()->get();
  2299. // 根据查询结果返回相应的结果
  2300. if ($list->isEmpty()) {
  2301. return Result::error("获取失败", 0);
  2302. } else {
  2303. return Result::success($list->toArray());
  2304. }
  2305. }
  2306. /**
  2307. *
  2308. * @param array $data
  2309. * @return array
  2310. */
  2311. public function getAllCategory(array $data): array
  2312. {
  2313. $result = Category::when($data, function ($query) use ($data) {
  2314. if (isset($data['name']) && !empty($data['name'])) {
  2315. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2316. }
  2317. })->get();
  2318. if (empty($result)) {
  2319. return Result::error("获取失败", 0);
  2320. } else {
  2321. return Result::success($result);
  2322. }
  2323. }
  2324. /**
  2325. * 修改网站栏目排序
  2326. * @param array $data
  2327. * @return array
  2328. */
  2329. public function upWebsiteCategorySort(array $data): array
  2330. {
  2331. $where = [
  2332. 'website_id' => $data['website_id'],
  2333. 'id' => $data['id'],
  2334. ];
  2335. $result = WebsiteCategory::where($where)->update($data);
  2336. if ($result) {
  2337. return Result::success($result);
  2338. } else {
  2339. return Result::error("更新失败", 0);
  2340. }
  2341. }
  2342. //获取底部导航内容详情
  2343. public function getFooterContentInfo(array $data): array
  2344. {
  2345. $where = [
  2346. "id" => $data['content_id']
  2347. ];
  2348. $result = FooterContent::where($where)->first();
  2349. if ($result) {
  2350. return Result::success($result);
  2351. } else {
  2352. return Result::error("没有数据");
  2353. }
  2354. }
  2355. //获取底部导航内容列表
  2356. public function getFooterContentList(array $data): array
  2357. {
  2358. $where = [
  2359. "fcat_id" => $data['fcat_id'],
  2360. ];
  2361. switch (intval($data['type'])) {
  2362. case 0:
  2363. $result = FooterContent::where($where)->first();
  2364. break;
  2365. case 1:
  2366. $result = FooterContent::where($where)->get();
  2367. break;
  2368. }
  2369. if ($result) {
  2370. return Result::success($result);
  2371. } else {
  2372. return Result::error("没有数据");
  2373. }
  2374. }
  2375. /**
  2376. * 获取底部导航列表
  2377. * @param array $data
  2378. * @return array
  2379. */
  2380. public function getFooterCategoryList(array $data): array
  2381. {
  2382. $where = [
  2383. "website_id" => $data['website_id']
  2384. ];
  2385. $result = FooterCategory::where($where)->get();
  2386. if ($result) {
  2387. return Result::success($result);
  2388. } else {
  2389. return Result::error("没有数据");
  2390. }
  2391. }
  2392. // --自助建站-----------20250522fr----------------------start
  2393. /**
  2394. * 获取网站模板下的板块信息
  2395. * @param array $data
  2396. * @return array
  2397. */
  2398. public function getStaticResourceList(array $data): array
  2399. {
  2400. // return Result::success($data);
  2401. $where = [];
  2402. if (isset($data['img_alias']) && !empty($data['img_alias'])) {
  2403. array_push($where, ['website_img.img_alias', 'like', '%' . $data['img_alias'] . '%']);
  2404. }
  2405. if (isset($data['img_type']) && !empty($data['img_type'])) {
  2406. array_push($where, ['website_img.img_type', '=', $data['img_type']]);
  2407. }
  2408. if (isset($data['website_name']) && !empty($data['website_name'])) {
  2409. $website = Website::where('website_name', 'like', '%' . $data['website_name'] . '%')->first();
  2410. if (empty($website)) {
  2411. return Result::error("没有查找到相关数据", 0);
  2412. } else {
  2413. array_push($where, ['website_img.website_id', '=', $website['id']]);
  2414. }
  2415. }
  2416. // return Result::success($where);
  2417. $query = WebsiteImg::when(!empty($where), function ($query) use ($where) {
  2418. $query->where($where);
  2419. })
  2420. ->orderBy('updated_at', 'desc');
  2421. $count = $query->count();
  2422. $row = $query->leftJoin('website', 'website.id', '=', 'website_img.website_id')
  2423. ->select('website_img.*', 'website.website_name')
  2424. ->limit($data['pageSize'])
  2425. ->offset(($data['page'] - 1) * $data['pageSize'])
  2426. ->get();
  2427. if (empty($row)) {
  2428. return Result::error("没有查找到相关数据", 0);
  2429. } else {
  2430. $result = [
  2431. 'row' => $row,
  2432. 'count' => $count,
  2433. ];
  2434. }
  2435. return Result::success($result);
  2436. }
  2437. /**
  2438. * 添加网站模板下的板块信息
  2439. * @param array $data
  2440. */
  2441. public function addStaticResource(array $data): array
  2442. {
  2443. // $file = $data['img_url']->file('image');
  2444. // $size = $file->getSize();
  2445. $img_data = [
  2446. 'website_id' => $data['website_id'] ?? 0,
  2447. 'img_alias' => $data['img_alias'] ?? '',
  2448. 'img_url' => $data['img_url'],
  2449. 'img_size' => $data['img_size'],
  2450. ];
  2451. $img = WebsiteImg::insertGetId($img_data);
  2452. if (empty($img)) {
  2453. return Result::error("添加失败", 0);
  2454. }
  2455. $img_id['id'] = $img;
  2456. $url = WebsiteImg::where($img_id)->value('img_url');
  2457. // 1. 分割路径部分(以 / 为分隔符)
  2458. $pathSegments = explode('/', parse_url($url, PHP_URL_PATH));
  2459. // 2. 取最后一个元素作为文件名
  2460. $fileName = end($pathSegments); // 1743041040714597.jpg
  2461. // 3. 分割文件名获取扩展名
  2462. $fileParts = explode('.', $fileName);
  2463. $fileType = end($fileParts); // jpg
  2464. $img_datas = [
  2465. 'img_type' => $fileType,
  2466. 'name' => $fileName
  2467. ];
  2468. $up_img = WebsiteImg::where($img_id)->update($img_datas);
  2469. if (empty($up_img)) {
  2470. return Result::error("修改失败", 0);
  2471. }
  2472. return Result::success($up_img);
  2473. // 将字节转换为更友好的单位
  2474. // $friendlySize = $this->formatFileSize($size);
  2475. // $data['img_size'] = $friendlySize;
  2476. }
  2477. /**
  2478. * 修改网站模板下的板块信息
  2479. * @param array $data
  2480. */
  2481. public function delStaticResource(array $data): array
  2482. {
  2483. $where = [
  2484. 'id' => $data['id'],
  2485. ];
  2486. $result = WebsiteImg::where($where)->delete();
  2487. if (empty($result)) {
  2488. return Result::error("修改失败", 0);
  2489. }
  2490. return Result::success($result);
  2491. }
  2492. /**
  2493. * 获取尺寸列表
  2494. * @param array $data
  2495. */
  2496. public function getSizeList(array $data): array
  2497. {
  2498. $where = [];
  2499. if (isset($data['width']) && !empty($data['width'])) {
  2500. array_push($where, ['width', 'like', '%' . $data['width'] . '%']);
  2501. }
  2502. if (isset($data['height']) && !empty($data['height'])) {
  2503. array_push($where, ['height', 'like', '%' . $data['height'] . '%']);
  2504. }
  2505. $query = Size::when($where, function ($query) use ($where) {
  2506. $query->where($where);
  2507. })
  2508. ->orderBy('updated_at', 'desc');
  2509. $count = $query->count();
  2510. $rep = $query
  2511. ->offset(($data['page'] - 1) * $data['pageSize'])
  2512. ->limit($data['pageSize'])
  2513. ->get();
  2514. $result = [
  2515. 'row' => $rep,
  2516. 'count' => $count,
  2517. ];
  2518. if (empty($result)) {
  2519. return Result::error("获取失败", 0);
  2520. } else {
  2521. return Result::success($result);
  2522. }
  2523. }
  2524. /**
  2525. * 添加尺寸
  2526. * @param array $data
  2527. */
  2528. public function addSize(array $data): array
  2529. {
  2530. $size = Size::where('width', $data['width'])->where('height', $data['height'])->first();
  2531. if (!empty($size)) {
  2532. return Result::error("尺寸已存在", 0);
  2533. }
  2534. $result = Size::insertGetId($data);
  2535. if (empty($result)) {
  2536. return Result::error("添加失败", 0);
  2537. }
  2538. return Result::success($result);
  2539. }
  2540. /**
  2541. * 删除尺寸
  2542. * @param array $data
  2543. */
  2544. public function delSize(array $data): array
  2545. {
  2546. $where = [
  2547. 'id' => $data['id'],
  2548. ];
  2549. $result = Size::where($where)->delete();
  2550. if (empty($result)) {
  2551. return Result::error("删除失败", 0);
  2552. }
  2553. return Result::success($result);
  2554. }
  2555. /**
  2556. * 修改尺寸
  2557. * @param array $data
  2558. */
  2559. public function upSize(array $data): array
  2560. {
  2561. $where = [
  2562. 'id' => $data['id'],
  2563. ];
  2564. $size = Size::where('id', '!=', $data['id'])->where('width', $data['width'])->where('height', $data['height'])->first();
  2565. if (!empty($size)) {
  2566. return Result::error("尺寸已存在", 0);
  2567. }
  2568. $result = Size::where($where)->update($data);
  2569. if (empty($result)) {
  2570. return Result::error("修改失败", 0);
  2571. }
  2572. return Result::success($result);
  2573. }
  2574. /**
  2575. * 获取尺寸详情
  2576. * @param array $data
  2577. */
  2578. public function getSizeInfo(array $data): array
  2579. {
  2580. $where = [
  2581. 'id' => $data['id'],
  2582. ];
  2583. $result = Size::where($where)->first();
  2584. if (empty($result)) {
  2585. return Result::error("获取失败", 0);
  2586. } else {
  2587. return Result::success($result);
  2588. }
  2589. }
  2590. // --自助建站-----------20250522fr----------------------end
  2591. /**
  2592. * 获取网站白名单路由地址
  2593. * @param array $data
  2594. * @return array
  2595. */
  2596. public function getWhiteRouterList(array $data): array
  2597. {
  2598. // 构建基础查询
  2599. $baseQuery = WhiteRouter::query();
  2600. // 功能名称模糊查询
  2601. if (!empty($data['function_name'])) {
  2602. $baseQuery->where('function_name', 'like', '%' . $data['function_name'] . '%');
  2603. }
  2604. // 网站ID查询 - 使用 JSON_CONTAINS 检查数组中是否包含指定的 website_id
  2605. if (!empty($data['website_id'])) {
  2606. $baseQuery->whereRaw('JSON_CONTAINS(website_id, ?)', [json_encode((int)$data['website_id'])]);
  2607. }
  2608. // 获取总数
  2609. $count = $baseQuery->count();
  2610. // 分页查询
  2611. $result = $baseQuery->orderBy('updated_at', 'desc')
  2612. ->limit($data['pageSize'])
  2613. ->offset(($data['page'] - 1) * $data['pageSize'])
  2614. ->get();
  2615. if (empty($result)) {
  2616. return Result::error("暂无数据", 0);
  2617. }
  2618. // 处理结果,将 website_id 转换为数组并获取对应的网站名称
  2619. $result->transform(function ($item) {
  2620. $websiteIds = json_decode($item->website_id, true);
  2621. $websiteNames = Website::whereIn('id', $websiteIds)
  2622. ->pluck('website_name')
  2623. ->toArray();
  2624. $item->website_names = $websiteNames;
  2625. $item->website_id = $websiteIds; // 将 website_id 转换为数组
  2626. return $item;
  2627. });
  2628. return Result::success([
  2629. 'rows' => $result->toArray(),
  2630. 'count' => $count
  2631. ]);
  2632. }
  2633. /**
  2634. * 添加网站白名单路由地址
  2635. * @param array $data
  2636. * @return array
  2637. */
  2638. public function addWhiteRouter(array $data): array
  2639. {
  2640. // 确保 website_id 是数组
  2641. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2642. // 过滤空值并去重
  2643. $websiteIds = array_filter($websiteIds);
  2644. $websiteIds = array_unique($websiteIds);
  2645. // 将数组中的值转换为整数
  2646. $websiteIds = array_map(function ($value) {
  2647. return (int)$value;
  2648. }, $websiteIds);
  2649. // 转换为 JSON 字符串存储
  2650. $data['website_id'] = json_encode($websiteIds);
  2651. $result = WhiteRouter::insertGetId($data);
  2652. if (empty($result)) {
  2653. return Result::error("添加失败", 0);
  2654. }
  2655. return Result::success($result);
  2656. }
  2657. /**
  2658. * 修改网站白名单路由地址
  2659. * @param array $data
  2660. * @return array
  2661. */
  2662. public function upWhiteRouter(array $data): array
  2663. {
  2664. $where = [
  2665. 'id' => $data['id'],
  2666. ];
  2667. // 确保 website_id 是数组
  2668. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2669. // 过滤空值并去重
  2670. $websiteIds = array_filter($websiteIds);
  2671. $websiteIds = array_unique($websiteIds);
  2672. // 将数组中的值转换为整数
  2673. $websiteIds = array_map(function ($value) {
  2674. return (int)$value;
  2675. }, $websiteIds);
  2676. // 转换为 JSON 字符串存储
  2677. $data['website_id'] = json_encode($websiteIds);
  2678. $result = WhiteRouter::where($where)->update($data);
  2679. if (empty($result)) {
  2680. return Result::error("修改失败", 0);
  2681. }
  2682. return Result::success($result);
  2683. }
  2684. /**
  2685. * 删除网站白名单路由地址
  2686. * @param array $data
  2687. * @return array
  2688. */
  2689. public function delWhiteRouter(array $data): array
  2690. {
  2691. $where = [
  2692. 'id' => $data['id'],
  2693. ];
  2694. $result = WhiteRouter::where($where)->delete();
  2695. if (empty($result)) {
  2696. return Result::error("删除失败", 0);
  2697. }
  2698. return Result::success($result);
  2699. }
  2700. /**
  2701. * 获取网站白名单路由地址详情
  2702. * @param array $data
  2703. * @return array
  2704. */
  2705. public function getWhiteRouterInfo(array $data): array
  2706. {
  2707. $where = [
  2708. 'id' => $data['id'],
  2709. ];
  2710. $result = WhiteRouter::where($where)->first();
  2711. if (empty($result)) {
  2712. return Result::error("没有查找到相关数据", 0);
  2713. }
  2714. // 将 website_id 从 JSON 字符串转换为数组
  2715. $result->website_id = json_decode($result->website_id, true);
  2716. return Result::success($result);
  2717. }
  2718. /**
  2719. * 克隆网站
  2720. * @param array $data
  2721. * @return array
  2722. */
  2723. public function cloneWebsite(array $data): array
  2724. {
  2725. DB::beginTransaction();
  2726. try {
  2727. // 获取原始网站信息
  2728. $originalWebsite = Website::find($data['website_id']);
  2729. if (!$originalWebsite) {
  2730. return Result::error("找不到该网站", 0);
  2731. }
  2732. // 提取基础名称(去除已有 _copy 或 _copy_xxx)
  2733. $baseName = preg_replace('/_copy(\d+)?$/', '', $originalWebsite->website_name);
  2734. // 查询当前系统中已存在的克隆名称数量
  2735. $count = Website::where('website_name', 'like', $baseName . '_copy%')
  2736. ->orWhere('website_name', $baseName)
  2737. ->count();
  2738. // 构造新名称:第一次是 _copy,之后是 _copy_1, _copy_2 ...
  2739. $newName = $baseName . ($count === 0 ? '_copy' : '_copy_' . $count);
  2740. // 创建克隆网站实例
  2741. $clone_website = $originalWebsite->replicate();
  2742. $clone_website->website_name = $newName;
  2743. $clone_website->save();
  2744. $clone_website->website_url = "[]";
  2745. $clone_website->save();
  2746. // var_dump("返回值:",$clone_website->id);
  2747. //克隆网站基础信息 website_template_info website_category
  2748. $resultWebsiteTemplateInfo = WebsiteTemplateInfo::where([ 'website_id' => $data['website_id']])->first();
  2749. $clone_website_template_info = $resultWebsiteTemplateInfo->replicate();
  2750. $clone_website_template_info->website_id = $clone_website->id;
  2751. $clone_website_template_info->save();
  2752. //克隆网站导航 获取原始 website_id 对应的所有 WebsiteCategory 记录
  2753. $originalCategories = WebsiteCategory::where('website_id', $data['website_id'])->get();
  2754. // 克隆网站后的新 ID(假设 $clone_website->id 已存在)
  2755. $newWebsiteId = $clone_website->id;
  2756. // 遍历每条记录并克隆
  2757. foreach ($originalCategories as $category) {
  2758. // 使用 replicate() 方法创建模型副本(不包含主键)
  2759. $clonedCategory = $category->replicate();
  2760. // 修改外键字段指向新网站 ID
  2761. $clonedCategory->website_id = $newWebsiteId;
  2762. // 保存克隆后的记录到数据库
  2763. $clonedCategory->save();
  2764. }
  2765. //克隆友情链接 Link 对应的是多个
  2766. $originalLinks = Link::where('website_id', $data['website_id'])->get();
  2767. foreach ($originalLinks as $link) {
  2768. // 使用 replicate() 方法创建模型副本(不包含主键)
  2769. $clonedLink = $link->replicate();
  2770. // 修改外键字段指向新网站 ID
  2771. $clonedLink->website_id = $newWebsiteId;
  2772. // 保存克隆后的记录到数据库
  2773. $clonedLink->save();
  2774. }
  2775. //克隆网站底部信息 footer_category 表 和footer_content 表 footer_category.id = footer_content.fcat_id 都是一对多关系
  2776. $originalFooterCategories = FooterCategory::where('website_id', $data['website_id'])->get();
  2777. foreach ($originalFooterCategories as $footerCategory) {
  2778. $clonedFooterCategory = $footerCategory->replicate();
  2779. $clonedFooterCategory->website_id = $newWebsiteId;
  2780. $clonedFooterCategory->save();
  2781. $originalFooterContents = FooterContent::where('fcat_id', $footerCategory->id)->get();
  2782. foreach ($originalFooterContents as $footerContent){
  2783. $clonedFooterContent = $footerContent->replicate();
  2784. $clonedFooterContent->fcat_id = $clonedFooterCategory->id;
  2785. $clonedFooterContent->save();
  2786. }
  2787. }
  2788. Db::commit();
  2789. return Result::success([]);
  2790. } catch (\Exception $e) {
  2791. Db::rollBack();
  2792. return Result::error($e->getMessage(), 0);
  2793. }
  2794. }
  2795. }