WebsiteService.php 107 KB

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