WebsiteService.php 98 KB

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