WebsiteService.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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\LetterOfComplaint;
  12. use App\Model\Link;
  13. use App\Model\TemplateClass;
  14. use App\Model\Template;
  15. use App\Model\User;
  16. use App\Model\WebsiteRole;
  17. use App\Model\WebsiteRoleUser;
  18. use App\Model\Website;
  19. use App\Model\WebsiteColumn;
  20. use Hyperf\DbConnection\Db;
  21. use Hyperf\RpcServer\Annotation\RpcService;
  22. use App\Tools\Result;
  23. use Carbon\Carbon;
  24. use App\Model\WebsiteCategory;
  25. use App\Model\WebsiteTemplate;
  26. use App\Model\WebsiteTemplateInfo;
  27. use Hamcrest\Arrays\IsArray;
  28. use PhpParser\Node\Stmt\Return_;
  29. use function PHPUnit\Framework\isNull;
  30. // use Illuminate\Support\Facades\DB;
  31. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  32. class WebsiteService implements WebsiteServiceInterface
  33. {
  34. /**
  35. * @param string $keyword
  36. * @param int $page
  37. * @param int $pageSize
  38. * @return array
  39. */
  40. public function getWebsitetList(array $data): array
  41. {
  42. $where = [];
  43. if (isset($data['keyword']) && !empty($data['keyword'])) {
  44. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  45. }
  46. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  47. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  48. }
  49. if (isset($data['city_id']) && !empty($data['city_id'])) {
  50. array_push($where, ['website.city_id', '=', $data['city_id']]);
  51. }
  52. $result = Website::where($where)
  53. ->leftJoin("website_column", "website.website_column_id", "website_column.id")
  54. ->leftJoin("district", "district.id", "website.city_id")
  55. ->select("website.*", "website_column.column_name", "district.name as city_name")
  56. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("website.id", "desc")->get();
  57. $count = Website::where($where)->count();
  58. if (empty($result)) {
  59. return Result::error("没有数据", 0);
  60. }
  61. $data = [
  62. 'rows' => $result->toArray(),
  63. 'count' => $count,
  64. ];
  65. return Result::success($data);
  66. }
  67. /**
  68. * @param array $data
  69. * @return array
  70. */
  71. public function createWebsite(array $data): array
  72. {
  73. var_dump("网站数据:", $data);
  74. $insertData = [
  75. 'website_name'=>$data['website_name'],
  76. 'logo'=>$data['logo']??'',
  77. 'website_url'=>$data['website_url']??'',
  78. // 'city_id'=>$data['city_id']??0,
  79. 'website_column_id'=>$data['website_column_id'],
  80. 'title'=>$data['title']??'',
  81. 'keywords'=>$data['keywords']??'',
  82. 'description'=>$data['description']??'',
  83. 'status'=>$data['status']??0,
  84. 'website_column_arr_id'=>$data['website_column_arr_id'],
  85. // 'city_arr_id'=>$data['city_arr_id']??[0],
  86. 'template_id' =>$data['template_id']??0,
  87. ];
  88. $result = Website::insertGetId($insertData);
  89. if (empty($result)) {
  90. return Result::error("创建失败", 0);
  91. } else {
  92. return Result::success(["id" => $result]);
  93. }
  94. }
  95. /**
  96. * @param int $id
  97. * @param array $data
  98. * @return array
  99. */
  100. public function updateWebsite(int $id, array $data): array
  101. {
  102. $insertData = [
  103. 'website_name'=>$data['website_name'],
  104. 'logo'=>$data['logo']??'',
  105. 'website_url'=>$data['website_url']??'',
  106. // 'city_id'=>$data['city_id']??0,
  107. 'website_column_id'=>$data['website_column_id'],
  108. 'title'=>$data['title']??'',
  109. 'keywords'=>$data['keywords']??'',
  110. 'description'=>$data['description']??'',
  111. 'status'=>$data['status']??0,
  112. 'website_column_arr_id'=>$data['website_column_arr_id'],
  113. // 'city_arr_id'=>$data['city_arr_id']??[0],
  114. 'template_id' =>$data['template_id']??0,
  115. ];
  116. $result = Website::where('id', $id)->update($insertData);
  117. var_dump("更新站点", $result);
  118. if (empty($result)) {
  119. return Result::error("更新失败", 0);
  120. } else {
  121. return Result::success();
  122. }
  123. }
  124. /**
  125. * @param int $id
  126. * @return array
  127. */
  128. public function delWebsite(int $id): array
  129. {
  130. $result = Website::where('id', $id)->delete();
  131. if (empty($result)) {
  132. return Result::error("删除失败", 0);
  133. } else {
  134. return Result::success();
  135. }
  136. }
  137. /**
  138. * @param int $id
  139. * @return array
  140. */
  141. public function getWebsiteInfo(int $id): array
  142. {
  143. $where = [
  144. ['website.id', '=', $id],
  145. ];
  146. $result = Website::where($where)
  147. ->leftJoin("template", "template.id", "website.template_id")
  148. ->select("website.*", "template.template_name", "template.template_img")
  149. ->first();
  150. if (empty($result)) {
  151. return Result::error("数据不存在", 0);
  152. } else {
  153. return Result::success($result->toArray());
  154. }
  155. }
  156. /**
  157. * 查询所有的站点栏目
  158. * @return array
  159. */
  160. public function getWebsiteColumn(array $data): array
  161. {
  162. $result = WebsiteColumn::where($data)->get();
  163. if (empty($result)) {
  164. return Result::error("数据不存在", 0);
  165. } else {
  166. return Result::success($result->toArray());
  167. }
  168. }
  169. /**
  170. * @param string $keyword
  171. * @param int $page
  172. * @param int $pageSize
  173. * @return array
  174. */
  175. public function getWebsiteColumnList(array $data): array
  176. {
  177. $where = [];
  178. if (isset($data['keyword']) && $data['keyword']) {
  179. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  180. }
  181. $result = WebsiteColumn::where($where)
  182. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  183. ->select("website_column.*", "wc.column_name as parent_column_name")
  184. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  185. $count = WebsiteColumn::where($where)->count();
  186. if (empty($result)) {
  187. return Result::error("没有数据", 0);
  188. }
  189. $data = [
  190. 'rows' => $result->toArray(),
  191. 'count' => $count,
  192. ];
  193. return Result::success($data);
  194. }
  195. /**
  196. * @param array $data
  197. * @return array
  198. */
  199. public function createWebsiteColumn(array $data): array
  200. {
  201. $insertData = [
  202. 'column_name' => $data['column_name'],
  203. 'pid' => $data['pid'] ?? '',
  204. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  205. 'sort' => $data['sort'] ?? 0,
  206. 'remark' => $data['remark'] ?? '',
  207. ];
  208. $result = WebsiteColumn::insertGetId($insertData);
  209. if (empty($result)) {
  210. return Result::error("创建失败", 0);
  211. } else {
  212. return Result::success(["id" => $result]);
  213. }
  214. }
  215. /**
  216. * @param int $id
  217. * @param array $data
  218. * @return array
  219. */
  220. public function updateWebsiteColumn(int $id, array $data): array
  221. {
  222. $insertData = [
  223. 'column_name' => $data['column_name'],
  224. 'pid' => $data['pid'] ?? '',
  225. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  226. 'sort' => $data['sort'] ?? 0,
  227. 'remark' => $data['remark'] ?? '',
  228. ];
  229. $result = WebsiteColumn::where('id', $id)->update($insertData);
  230. if (empty($result)) {
  231. return Result::error("更新失败", 0);
  232. } else {
  233. return Result::success();
  234. }
  235. }
  236. /**
  237. * @param int $id
  238. * @return array
  239. */
  240. public function delWebsiteColumn(int $id): array
  241. {
  242. $list = WebsiteColumn::where(['pid' => $id])->get();
  243. if ($list) {
  244. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  245. }
  246. $result = WebsiteColumn::where('id', $id)->delete();
  247. if (empty($result)) {
  248. return Result::error("删除失败", 0);
  249. } else {
  250. return Result::success();
  251. }
  252. }
  253. /**
  254. * @param string $keyword
  255. * @param int $page
  256. * @param int $pageSize
  257. * @return array
  258. */
  259. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  260. {
  261. $where = [
  262. ['role.role_name', 'like', '%' . $keyword . '%'],
  263. ['website_role.website_id', '=', $websiteId],
  264. ];
  265. $result = WebsiteRole::where($where)
  266. ->leftJoin("role", "role.id", "website_role.role_id")
  267. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  268. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  269. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  270. if (empty($result)) {
  271. return Result::error("没有数据", 0);
  272. }
  273. $data = [
  274. 'rows' => $result->toArray(),
  275. 'count' => $count,
  276. ];
  277. return Result::success($data);
  278. }
  279. /**
  280. * @param array $data
  281. * @return array
  282. */
  283. public function createWebsiteRole(array $data): array
  284. {
  285. $insertData = [
  286. 'website_id' => $data['website_id'],
  287. 'role_id' => $data['role_id'] ?? '',
  288. ];
  289. $info = WebsiteRole::where($insertData)->first();
  290. if ($info) {
  291. return Result::error("不能重复添加角色", 0);
  292. }
  293. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  294. $insertData['type'] = $data['type'] ?? '';
  295. $result = WebsiteRole::insertGetId($insertData);
  296. if (empty($result)) {
  297. return Result::error("创建失败", 0);
  298. } else {
  299. return Result::success(["id" => $result]);
  300. }
  301. }
  302. /**
  303. * 暂时用不上
  304. * @param int $id
  305. * @param array $data
  306. * @return array
  307. */
  308. public function updateWebsiteRole(int $id, array $data): array
  309. {
  310. $insertData = [
  311. 'website_id' => $data['website_id'],
  312. 'type' => $data['type'] ?? '',
  313. ];
  314. $result = WebsiteRole::where('id', $id)->update($insertData);
  315. if (empty($result)) {
  316. return Result::error("更新失败", 0);
  317. } else {
  318. return Result::success();
  319. }
  320. }
  321. /**
  322. * @param int $id
  323. * @return array
  324. */
  325. public function delWebsiteRole(int $id): array
  326. {
  327. $result = WebsiteRole::where('id', $id)->delete();
  328. if (empty($result)) {
  329. return Result::error("删除失败", 0);
  330. } else {
  331. return Result::success();
  332. }
  333. }
  334. /**
  335. * @param string $keyword
  336. * @param int $page
  337. * @param int $pageSize
  338. * @return array
  339. */
  340. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  341. {
  342. $where = [
  343. ['website_role_user.website_id', '=', $websiteId],
  344. ['website_role_user.role_id', '=', $roleId],
  345. ];
  346. $count = WebsiteRoleUser::where($where)->count();
  347. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  348. $result = WebsiteRoleUser::where($where)
  349. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  350. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  351. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  352. ->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')
  353. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  354. if (empty($result)) {
  355. return Result::error("没有数据", 0);
  356. }
  357. $data = [
  358. 'rows' => $result->toArray(),
  359. 'count' => $count,
  360. ];
  361. return Result::success($data);
  362. }
  363. /**
  364. * @param array $data
  365. * @return array
  366. */
  367. public function createWebsiteRoleUser(array $data): array
  368. {
  369. $insertData = [
  370. 'website_id' => $data['website_id'],
  371. 'user_id' => $data['user_id'] ?? '',
  372. ];
  373. $info = WebsiteRoleUser::where($insertData)->first();
  374. if ($info) {
  375. return Result::error("不能重复添加角色用户", 0);
  376. }
  377. $insertData['role_id'] = $data['role_id'] ?? '';
  378. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  379. $insertData['type'] = $data['type'] ?? '';
  380. $result = WebsiteRoleUser::insertGetId($insertData);
  381. if (empty($result)) {
  382. return Result::error("创建失败", 0);
  383. } else {
  384. return Result::success(["id" => $result]);
  385. }
  386. }
  387. /**
  388. * @param int $id
  389. * @param array $data
  390. * @return array
  391. */
  392. public function updateWebsiteRoleUser(int $id, array $data): array
  393. {
  394. $insertData = [
  395. 'website_id' => $data['website_id'],
  396. 'type' => $data['type'] ?? '',
  397. 'type_id' => $data['type_id'] ?? '',
  398. 'role_id' => $data['role_id'] ?? '',
  399. ];
  400. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  401. if (empty($result)) {
  402. return Result::error("更新失败", 0);
  403. } else {
  404. return Result::success();
  405. }
  406. }
  407. /**
  408. * @param int $id
  409. * @return array
  410. */
  411. public function delWebsiteRoleUser(int $id): array
  412. {
  413. $result = WebsiteRoleUser::where('id', $id)->delete();
  414. if (empty($result)) {
  415. return Result::error("删除失败", 0);
  416. } else {
  417. return Result::success();
  418. }
  419. }
  420. /**
  421. * 根据域名获取网站 站点id
  422. * @param array $data
  423. * @return array
  424. */
  425. public function getWebsiteId(array $data): array
  426. {
  427. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  428. if (empty($result)) {
  429. return Result::error("查询站点失败", 0);
  430. } else {
  431. return Result::success($result);
  432. }
  433. }
  434. /**
  435. * 查询网站下面的导航
  436. * @param array $data
  437. * @return array
  438. */
  439. public function getWebsiteCategory(array $data): array
  440. {
  441. $where = [
  442. 'website_id' => $data['website_id'],
  443. 'pid' => 0,
  444. ];
  445. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  446. if (empty($result)) {
  447. return Result::error("查询站点栏目失败", 0);
  448. } else {
  449. return Result::success($result);
  450. }
  451. }
  452. /**
  453. * 查询网站的广告
  454. * @param array $data
  455. * @return array
  456. */
  457. public function getWebsiteAdvertisement(array $data): array
  458. {
  459. $where = [
  460. 'website_id' => 1,
  461. 'id' => $data['ad_placeid']
  462. ];
  463. //查询这个广告位是否存在
  464. $ad_place = AdPlace::where($where)->orderBy('id','asc')->first();
  465. var_dump("==========",$ad_place);
  466. if(empty($ad_place)){
  467. return Result::error("error",0);
  468. }else{
  469. $adplaceid = $ad_place['id'];
  470. //查找有没有广告
  471. $ad=Ad::where('pid',$adplaceid['id'])->where('status',1)->orderBy('id','asc')->get();
  472. if(empty($ad)){
  473. //若没有生效的广告,则显示默认的缩略图
  474. $result=$adplaceid;
  475. return Result::success($result);
  476. }else{
  477. //查找在生效时间的广告
  478. $today = Carbon::now();
  479. foreach($ad as $i)
  480. {
  481. $starttime=Carbon::parse($i['fromtime']);
  482. $endtime=Carbon::parse($i['totime']);
  483. $time=$today->between($starttime,$endtime);
  484. if($time)
  485. {
  486. $result=$i;
  487. }
  488. }
  489. if(empty($result)){
  490. $result=$ad;
  491. return Result::success($result);
  492. }else{
  493. return Result::success($result);
  494. }
  495. }
  496. }
  497. }
  498. /**
  499. *
  500. * @param array $data
  501. * @return array
  502. */
  503. /**网站行政职能搜索 */
  504. public function selectWebsiteDepartment(array $data): array
  505. {
  506. $depart = Department::where('pid',0)->orderBy('id','asc')->limit(10)->get();
  507. if(isset($data['keyword']) && !empty($data['keyword'])){
  508. $departments= Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  509. if(empty($departments)){
  510. $result['message']="未查询到与此相关职能部门";
  511. }else{
  512. $count = Department::where('name','like',"%{$data['keyword']}%")->count();
  513. $m = [
  514. 'department'=>$depart,
  515. 'type'=>$departments,
  516. 'count'=>$count
  517. ];
  518. }
  519. $result['sele'] = $m;
  520. return Result::success($result['sele']);
  521. }
  522. $result=$depart;
  523. return Result::success($result);
  524. }
  525. /**
  526. * 搜索地区
  527. * @param array $data
  528. * @return array
  529. */
  530. public function selectWebsiteArea(array $data): array
  531. {
  532. $provinces=District::where('pid',0)->where('status',1)->get();
  533. if(isset($data['province'])){
  534. $province=District::where('pid',0)->where('status',1)->where('id',$data['province'])->orderBy('id')->get();
  535. $province=$province->toArray();
  536. if(!empty($province)){
  537. $citys=District::where('pid',$data['province'])->where('status',1)->orderBy('id')->get();
  538. if(!empty($citys) && isset($data['city']) && !empty($data['city'])){
  539. // $province = $province->toArray();
  540. $province_id=[];
  541. foreach($province as $val){
  542. array_push($province_id,$val['id']);
  543. }
  544. // var_dump($province_id);
  545. $city=District::whereIn('pid',$province_id)->where('status',1)->where('id',$data['city'])->orderBy('id')->get();
  546. if(!empty($city)){
  547. $city_id=[];
  548. foreach($city as $val){
  549. array_push($city_id,$val['id']);
  550. }
  551. $regions=District::whereIn('pid',$city_id)->where('status',1)->orderBy('id')->get();
  552. $result=[
  553. 'province' => $province,
  554. 'city' => $city,
  555. 'region' => $regions
  556. ];
  557. }else{
  558. return Result::error("未查询到此城市",0);
  559. }
  560. }else{
  561. $result=[
  562. 'province' => $province,
  563. 'city' => $citys,
  564. 'region' => null
  565. ];
  566. }
  567. }else{
  568. return Result::error("未查询到此省份",0);
  569. }
  570. }else{
  571. // $keys = array('data');
  572. $result = $provinces;
  573. }
  574. return Result::success($result);
  575. }
  576. /**
  577. * 获取栏目
  578. * @param array $data
  579. * @return array
  580. */
  581. public function getWebsiteModelCategory(array $data): array
  582. {
  583. $website_id=[
  584. 'website_id' => $data['website_id']
  585. ];
  586. $placeid=$data['placeid']-1;
  587. $pid=[
  588. 'pid' => $data['pid'],
  589. ];
  590. $num = $data['num'];
  591. $result=WebsiteCategory::where($website_id)->where($pid)->withCount(['children' => function ($query) use ($website_id) {
  592. $query->where($website_id);
  593. }])->orderBy('sort')->offset($placeid)->limit($num)->get();
  594. if(!empty($result)){
  595. return Result::success($result);
  596. }else{
  597. return Result::error("本网站暂无栏目",0);
  598. }
  599. }
  600. /**
  601. * 获取友情链接
  602. * @param array $data
  603. * @return array
  604. */
  605. public function selectWebsiteLinks(array $data): array
  606. {
  607. $where = [
  608. 'website_id' => $data['website_id'],
  609. 'status' => 1,
  610. 'type' => $data['type']
  611. ];
  612. $num=$data['num'];
  613. $result=Link::where($where)->orderBy('id')->limit($num)->get();
  614. if(!empty($result)){
  615. return Result::success($result);
  616. }else{
  617. return Result::error("本网站暂无此类型友情链接",0);
  618. }
  619. }
  620. /**
  621. * 网站首页数据统计, 管理员
  622. * @return void
  623. */
  624. public function getAdminIndex(array $data): array
  625. {
  626. var_dump("用户类型:", $data['type_id']);
  627. switch ($data['type_id']) {
  628. case 4:
  629. $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;');
  630. return Result::success($result);
  631. break;
  632. case 10000:
  633. $res = [];
  634. //网站
  635. $res['website']['count'] = 0;
  636. $res['website']['growth_rate'] = 0;
  637. //资讯
  638. $res['article']['count'] = 0;
  639. $res['article']['growth_rate'] = 0;
  640. //导航池
  641. $res['category']['count'] = 0;
  642. $res['category']['growth_rate'] = 0;
  643. //近一月数据
  644. $res['monthArticle'] = [];
  645. //用户类型
  646. $res['userType'] = [];
  647. $res['website']['count'] = Website::where([])->count();
  648. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  649. $res['category']['count'] = Category::where([])->count();
  650. $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;');
  651. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  652. return Result::success($res);
  653. }
  654. return [];
  655. }
  656. /**
  657. * 获取模板类型
  658. * @return void
  659. */
  660. public function getTemplateClass(array $data): array
  661. {
  662. $where = [];
  663. if (isset($data['name']) && $data['name']) {
  664. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  665. }
  666. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  667. if (empty($result)) {
  668. return Result::error("没有模板类型", 0);
  669. } else {
  670. return Result::success($result);
  671. }
  672. }
  673. /**
  674. * 添加模板类型
  675. * @param
  676. * @return void
  677. */
  678. public function addTemplateClass(array $data): array
  679. {
  680. $insertData = [
  681. 'name' => $data['name'],
  682. ];
  683. $result = TemplateClass::insertGetId($insertData);
  684. if (empty($result)) {
  685. return Result::error("创建失败", 0);
  686. } else {
  687. return Result::success(["id" => $result]);
  688. }
  689. }
  690. /**
  691. * 更新模板
  692. * @param array $data
  693. * @return array
  694. */
  695. public function upTemplateClass(array $data): array
  696. {
  697. $where = [
  698. 'id' => $data['id'],
  699. ];
  700. $insertData = [
  701. 'name' => $data['name'],
  702. ];
  703. $result = TemplateClass::where($where)->update($insertData);
  704. if (empty($result)) {
  705. return Result::error("更新失败", 0);
  706. } else {
  707. return Result::success();
  708. }
  709. }
  710. /**
  711. * 删除模板
  712. * @param array $data
  713. * @return array
  714. */
  715. public function delTemplateClass(array $data): array
  716. {
  717. $where = [
  718. 'id' => $data['id'],
  719. ];
  720. $result = TemplateClass::where($where)->delete();
  721. if (empty($result)) {
  722. return Result::error("删除失败", 0);
  723. } else {
  724. return Result::success();
  725. }
  726. }
  727. /**
  728. * 获取分类下的模板
  729. * @param array $data
  730. * @return array
  731. */
  732. public function getTemplate(array $data): array
  733. {
  734. $page = $data['page'];
  735. $pageSize = $data['pageSize'];
  736. $where = [];
  737. if (isset($data['template_class_id']) && $data['template_class_id']) {
  738. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  739. }
  740. $result = Template::where($where)
  741. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  742. $count = Template::where($where)->count();
  743. if (empty($result)) {
  744. return Result::error("没有数据", 0);
  745. }
  746. $data = [
  747. 'rows' => $result->toArray(),
  748. 'count' => $count,
  749. ];
  750. return Result::success($data);
  751. }
  752. /**
  753. * 创建模板
  754. * @param
  755. * @return void
  756. */
  757. public function addTemplate(array $data): array
  758. {
  759. $insertData = [
  760. 'template_name' => $data['template_name'],
  761. 'template_img' => json_encode($data['template_img']),
  762. 'template_class_id' => $data['template_class_id'],
  763. ];
  764. $result = Template::insertGetId($insertData);
  765. if (empty($result)) {
  766. return Result::error("创建模板失败", 0);
  767. } else {
  768. return Result::success(["id" => $result]);
  769. }
  770. }
  771. /**
  772. * 更新模板
  773. * @param array $data
  774. * @return array
  775. */
  776. public function upTemplate(array $data): array
  777. {
  778. $where = [
  779. 'id' => $data['id'],
  780. ];
  781. $insertData = [
  782. 'template_name' => $data['template_name'],
  783. 'template_img' => json_encode($data['template_img']),
  784. 'template_class_id' => $data['template_class_id'],
  785. ];
  786. $result = Template::where($where)->update($insertData);
  787. if (empty($result)) {
  788. return Result::error("更新模板失败", 0);
  789. } else {
  790. return Result::success();
  791. }
  792. }
  793. /**
  794. * 删除模板
  795. * @param array $data
  796. * @return array
  797. */
  798. public function delTemplate(array $data): array
  799. {
  800. $where = [
  801. 'id' => $data['id'],
  802. ];
  803. $result = Template::where($where)->delete();
  804. if (empty($result)) {
  805. return Result::error("删除模板失败", 0);
  806. } else {
  807. return Result::success();
  808. }
  809. }
  810. /**
  811. * 搜索网站
  812. * @param array $data
  813. * @return array
  814. */
  815. public function websiteList(array $data): array
  816. {
  817. $where = [];
  818. if (isset($data['keyword']) && !empty($data['keyword'])) {
  819. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  820. }
  821. $result = Website::where($where)->get();
  822. if ($result) {
  823. return Result::success($result);
  824. } else {
  825. return Result::error("没有网站", 0);
  826. }
  827. }
  828. public function addWebsiteCategory(array $data): array
  829. {
  830. $website_id = $data['website_id'];
  831. $category_arr_id = $data['category_arr_id'];
  832. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  833. $categoryListIds = [];
  834. if ($categoryList) {
  835. foreach ($categoryList->toArray() as $val) {
  836. array_push($categoryListIds, $val['id']);
  837. }
  838. }
  839. $arr = [];
  840. if ($categoryListIds) {
  841. foreach ($categoryListIds as $v) {
  842. $ids = $this->getUnderlingUIds(intval($v));
  843. $ids_arr = explode(",", $ids);
  844. array_push($arr, $ids_arr);
  845. }
  846. }
  847. $mergedArray = [];
  848. foreach ($arr as $subarray) {
  849. $mergedArray = array_merge($mergedArray, $subarray);
  850. }
  851. var_dump("所有:", $arr, $mergedArray);
  852. //查询出所有的分类进行分割插入 组装数据
  853. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  854. $categoryListData = $categoryListData->toArray();
  855. $insertData = [];
  856. if ($categoryListData) {
  857. foreach ($categoryListData as $key => $value) {
  858. $insertData[$key]['website_id'] = $website_id;
  859. $insertData[$key]['name'] = $value['name'];
  860. $insertData[$key]['sort'] = $value['sort'];
  861. $insertData[$key]['pid'] = $value['pid'];
  862. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  863. $insertData[$key]['seo_title'] = $value['seo_title'];
  864. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  865. $insertData[$key]['seo_description'] = $value['seo_description'];
  866. $insertData[$key]['alias'] = $value['name'];
  867. $insertData[$key]['category_id'] = $value['id'];
  868. }
  869. }
  870. $result = WebsiteCategory::insert($insertData);
  871. var_dump("插入数据状态:", $result);
  872. if ($result) {
  873. return Result::success($result);
  874. } else {
  875. return Result::error("创建失败", 0);
  876. }
  877. }
  878. /**
  879. * 删除网站导航
  880. * @param array $data
  881. * @return array
  882. */
  883. public function delWebsiteCategory(array $data): array
  884. {
  885. $website_id = $data['website_id'] ?? 0;
  886. $category_id = $data['category_id'] ?? 0;
  887. $ids = $this->getUnderlingUIds(intval($category_id));
  888. $ids_arr = explode(",", $ids);
  889. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  890. if ($result) {
  891. return Result::success($result);
  892. } else {
  893. return Result::error("删除失败", 0);
  894. }
  895. }
  896. /**
  897. * 获取网站导航
  898. * @param array $data
  899. * @return array
  900. */
  901. public function getAdminWebsiteCategory(array $data): array
  902. {
  903. $where = [
  904. 'website_id' => $data['website_id'],
  905. 'pid' => 0,
  906. ];
  907. $result = WebsiteCategory::where($where)->get();
  908. if ($result) {
  909. return Result::success($result);
  910. } else {
  911. return Result::error("查询失败", 0);
  912. }
  913. }
  914. /**
  915. * 更新网站导航
  916. * @param array $data
  917. * @return array
  918. */
  919. public function upWebsiteCategory(array $data): array
  920. {
  921. Db::beginTransaction();
  922. try {
  923. //合并栏目id
  924. $reqIds = array_merge($data['old_category_arr_id'], $data['new_category_arr_id']);
  925. //对比old 数组差异化,把差异化的删除
  926. $result = WebsiteCategory::where(['website_id' => $data['website_id'], 'pid' => 0])->get();
  927. $result = $result->toArray();
  928. $categoryIds = [];
  929. if ($result) {
  930. foreach ($result as $val) {
  931. array_push($categoryIds, $val['category_id']);
  932. }
  933. }
  934. //和原始数据对比取交际
  935. $reqidsIntersect = array_intersect($reqIds, $categoryIds);
  936. //再取差集 进行对比
  937. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds), array_diff($categoryIds, $reqidsIntersect));
  938. var_dump("差集:", $differenceIDS);
  939. $arr_ids = [];
  940. if (count($differenceIDS) > 0) {
  941. foreach ($differenceIDS as $vv) {
  942. $idV = $this->getUnderlingUIds(intval($vv));
  943. $ids_arrV = explode(",", $idV);
  944. array_push($arr_ids, $ids_arrV);
  945. }
  946. }
  947. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  948. //有差异 删除
  949. if (count($del_ids) > 0) {
  950. WebsiteCategory::where(['website_id' => $data['website_id']])->whereIn("category_id", $del_ids)->delete();
  951. }
  952. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  953. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect), array_diff($reqidsIntersect, $reqIds));
  954. var_dump("要存储的:", $insertIDS);
  955. //新的数组重新创建
  956. if (count($insertIDS) > 0) {
  957. $arr = [];
  958. $categoryListIds = $insertIDS;
  959. if ($categoryListIds) {
  960. foreach ($categoryListIds as $v) {
  961. $ids = $this->getUnderlingUIds(intval($v));
  962. $ids_arr = explode(",", $ids);
  963. array_push($arr, $ids_arr);
  964. }
  965. }
  966. $mergedArray = [];
  967. foreach ($arr as $subarray) {
  968. $mergedArray = array_merge($mergedArray, $subarray);
  969. }
  970. var_dump("要插入的ID:", $mergedArray);
  971. //查询出所有的分类进行分割插入 组装数据
  972. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  973. $categoryListData = $categoryListData->toArray();
  974. $insertData = [];
  975. if ($categoryListData) {
  976. foreach ($categoryListData as $key => $value) {
  977. $insertData[$key]['website_id'] = $data['website_id'];
  978. $insertData[$key]['name'] = $value['name'];
  979. $insertData[$key]['sort'] = $value['sort'];
  980. $insertData[$key]['pid'] = $value['pid'];
  981. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  982. $insertData[$key]['seo_title'] = $value['seo_title'];
  983. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  984. $insertData[$key]['seo_description'] = $value['seo_description'];
  985. $insertData[$key]['alias'] = $value['name'];
  986. $insertData[$key]['category_id'] = $value['id'];
  987. }
  988. }
  989. WebsiteCategory::insert($insertData);
  990. }
  991. Db::commit();
  992. } catch (\Throwable $ex) {
  993. Db::rollBack();
  994. var_dump($ex->getMessage());
  995. return Result::error("修改失败", 0);
  996. }
  997. return Result::success();
  998. }
  999. /**
  1000. * 获取网站列表
  1001. * @param array $data
  1002. * @return array
  1003. */
  1004. public function getWebsiteCategoryList(array $data): array
  1005. {
  1006. $where = [];
  1007. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1008. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1009. }
  1010. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1011. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1012. }
  1013. $result = Website::where($where)
  1014. ->with(["websiteCategory" => function ($query) {
  1015. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1016. }])
  1017. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1018. ->get();
  1019. $count = Website::where($where)->count();
  1020. if (empty($result)) {
  1021. return Result::error("没有数据", 0);
  1022. }
  1023. $data = [
  1024. 'rows' => $result->toArray(),
  1025. 'count' => $count,
  1026. ];
  1027. if ($result) {
  1028. return Result::success($data);
  1029. } else {
  1030. return Result::error("查询失败", 0);
  1031. }
  1032. }
  1033. /**
  1034. * 删除网站下的所有导航
  1035. * @param array $data
  1036. * @return array
  1037. */
  1038. public function delWebsiteAllCategory(array $data): array
  1039. {
  1040. $website_id = $data['website_id'];
  1041. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1042. if ($result) {
  1043. return Result::success($result);
  1044. } else {
  1045. return Result::error("删除失败", 0);
  1046. }
  1047. }
  1048. /**
  1049. * 获取网站下的某一个导航
  1050. * @param array $data
  1051. * @return array
  1052. */
  1053. public function getWebsiteCategoryOnes(array $data): array
  1054. {
  1055. $website_id = $data['website_id'];
  1056. $category_id = $data['category_id'];
  1057. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1058. ->first();
  1059. if ($result) {
  1060. return Result::success($result);
  1061. } else {
  1062. return Result::error("查询失败", 0);
  1063. }
  1064. }
  1065. /**
  1066. * 更新网闸下的某一个导航
  1067. * @param array $data
  1068. * @return array
  1069. */
  1070. public function upWebsiteCategoryones(array $data): array
  1071. {
  1072. $where = [
  1073. 'website_id' => $data['website_id'],
  1074. 'category_id' => $data['category_id'],
  1075. ];
  1076. $result = WebsiteCategory::where($where)->update($data);
  1077. if ($result) {
  1078. return Result::success($result);
  1079. } else {
  1080. return Result::error("更新失败", 0);
  1081. }
  1082. }
  1083. /**
  1084. * 获取网站下的所有导航(包含子导航)
  1085. * @param array $data
  1086. * @return array
  1087. */
  1088. public function getWebsiteAllCategory(array $data): array
  1089. {
  1090. $where = [];
  1091. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1092. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1093. }
  1094. if (isset($data['name']) && !empty($data['name'])) {
  1095. array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1096. }
  1097. if (isset($data['alias']) && !empty($data['alias'])) {
  1098. array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1099. }
  1100. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1101. array_push($where, ['category.department_id', '=', $data['department_id']]);
  1102. }
  1103. if (isset($data['city_id']) && !empty($data['city_id'])) {
  1104. array_push($where, ['category.city_id', '=', $data['city_id']]);
  1105. }
  1106. $result = WebsiteCategory::where($where)
  1107. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1108. ->leftJoin("department", 'category.department_id', 'department.id')
  1109. ->leftJoin("district", 'category.city_id', 'district.id')
  1110. ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1111. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1112. ->get();
  1113. $count = WebsiteCategory::where($where)
  1114. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1115. ->leftJoin("department", 'category.department_id', 'department.id')
  1116. ->leftJoin("district", 'category.city_id', 'district.id')
  1117. ->count();
  1118. if (empty($result)) {
  1119. return Result::error("没有数据", 0);
  1120. }
  1121. $data = [
  1122. 'rows' => $result->toArray(),
  1123. 'count' => $count,
  1124. ];
  1125. if ($result) {
  1126. return Result::success($data);
  1127. } else {
  1128. return Result::error("查询失败", 0);
  1129. }
  1130. if ($result) {
  1131. return Result::success($result);
  1132. } else {
  1133. return Result::error("查询失败", 0);
  1134. }
  1135. }
  1136. /**
  1137. * 递归查询数据
  1138. * @param $id
  1139. * @param $ids
  1140. * @return string
  1141. */
  1142. public function getUnderlingUIds($id, $ids = '')
  1143. {
  1144. $back = Category::where(['pid' => $id])->get();
  1145. $back = $back->toArray();
  1146. if (!empty($back) && is_array($back)) {
  1147. foreach ($back as $v) {
  1148. //防止当前人的ID重复去查询,形成恶性循环
  1149. if ($v['id'] == $id) {
  1150. continue;
  1151. }
  1152. $back2 = Category::where(['pid' => $id])->count('id');
  1153. if ($back2 > 0) {
  1154. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1155. } else {
  1156. $ids .= ',' . $v['id'];
  1157. }
  1158. }
  1159. }
  1160. $ids = $id . ',' . $ids . ',';
  1161. $ids = str_replace(',,', ",", $ids);
  1162. $ids = trim($ids, ',');
  1163. return $ids;
  1164. }
  1165. /**
  1166. * 检测网站名称是否重复
  1167. * @param array $data
  1168. * @return array
  1169. */
  1170. public function checkWebsiteName(array $data): array
  1171. {
  1172. if (isset($data['id'])) {
  1173. $data[] = ['id', "!=", $data['id']];
  1174. unset($data['id']);
  1175. }
  1176. $websiteInfo = Website::query()->where($data)->first();
  1177. if (empty($websiteInfo)) {
  1178. return Result::error("找不到网站", 0);
  1179. }
  1180. return Result::success($websiteInfo->toArray());
  1181. }
  1182. /**
  1183. * 检测网站url是否重复
  1184. * @param array $data
  1185. * @return array
  1186. */
  1187. public function checkWebsiteUrl(array $data): array
  1188. {
  1189. $whereData = [];
  1190. if (isset($data['id'])) {
  1191. $whereData = [['id', "!=", $data['id']]];
  1192. unset($data['id']);
  1193. }
  1194. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1195. if (empty($websiteInfo)) {
  1196. return Result::error("找不到URL", 0);
  1197. }
  1198. return Result::success($websiteInfo->toArray());
  1199. }
  1200. /**
  1201. * 获取网站底部基础信息
  1202. * @param array $data
  1203. * @return array
  1204. */
  1205. public function getWebsiteFootInfo(array $data): array
  1206. {
  1207. if(isset($data['website_id']) && !empty($data['website_id'])){
  1208. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1209. if (empty($website)) {
  1210. return Result::error("找不到网站",0);
  1211. }
  1212. }else{
  1213. return Result::error("参数错误",0);
  1214. }
  1215. $website_foot = WebsiteTemplateInfo::where('website_id',$data['website_id'])->where('status',2)->first();
  1216. $website_head = Website::where('id',$data['website_id'])
  1217. ->select('id','website_name','logo','title','keywords','description')->first();
  1218. if (empty($website_foot)) {
  1219. return Result::error("暂无底部基础信息",0);
  1220. }
  1221. if (empty($website_head)) {
  1222. return Result::error("暂无头部基础信息",0);
  1223. }
  1224. $result = [
  1225. 'website_foot'=>$website_foot,
  1226. 'website_head'=>$website_head
  1227. ];
  1228. return Result::success($result);
  1229. }
  1230. /**
  1231. * 获取网站底部导航
  1232. * @param array $data
  1233. * @return array
  1234. */
  1235. public function getWebsiteFooterCategory(array $data): array
  1236. {
  1237. if(isset($data['website_id']) && !empty($data['website_id'])){
  1238. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1239. if (empty($website)) {
  1240. return Result::error("找不到网站",0);
  1241. }
  1242. }else{
  1243. return Result::error("参数错误",0);
  1244. }
  1245. $result = FooterCategory::where('website_id',$data['website_id'])->get();
  1246. if (empty($result)) {
  1247. return Result::error("暂无底部导航",0);
  1248. }
  1249. return Result::success($result->toArray());
  1250. }
  1251. /**
  1252. * 获取网站底部导航列表
  1253. * @param array $data
  1254. * @return array
  1255. */
  1256. public function getWebsiteFooterCategoryList(array $data): array
  1257. {
  1258. if(isset($data['website_id']) && !empty($data['website_id'])){
  1259. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1260. if (empty($website)) {
  1261. return Result::error("找不到网站",0);
  1262. }
  1263. }else{
  1264. return Result::error("参数错误",0);
  1265. }
  1266. $footercategory = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1267. // '底部导航类型 0:内容型;1:列表型;',
  1268. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1269. return Result::error("底部导航id错误",0);
  1270. }else{
  1271. $query = FooterContent::where('fcat_id',$data['fcat_id']);
  1272. if($query->count() == 0){
  1273. return Result::error("暂无底部导航列表",0);
  1274. }elseif($query->count() == 1){
  1275. $result = $query->first();
  1276. }else{
  1277. $result = $query->get();
  1278. }
  1279. }
  1280. return Result::success($result);
  1281. }
  1282. /**
  1283. * 获取网站底部导航
  1284. * @param array $data
  1285. * @return array
  1286. */
  1287. public function getWebsiteFooterCategoryInfo(array $data): array
  1288. {
  1289. if(isset($data['website_id']) && !empty($data['website_id'])){
  1290. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1291. if (empty($website)) {
  1292. return Result::error("找不到网站",0);
  1293. }
  1294. }else{
  1295. return Result::error("参数错误",0);
  1296. }
  1297. if (isset($data['type']) && $data['type'] == 0) {
  1298. $fcatid = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1299. if (empty($fcatid)) {
  1300. return Result::error("底部导航id错误",0);
  1301. }
  1302. $result = FooterContent::where('fcat_id',$data['fcat_id'])->first();
  1303. }else{
  1304. $result = FooterContent::where('id',$data['fcat_id'])->first();
  1305. }
  1306. if (empty($result)) {
  1307. return Result::error("暂无底部导航内容",0);
  1308. }
  1309. return Result::success($result);
  1310. }
  1311. /**
  1312. * 获取并搜索 网站模板信息
  1313. * @param array $data
  1314. * @return array
  1315. */
  1316. public function getWebsiteintel(array $data): array
  1317. {
  1318. //查询所有网站模板信息
  1319. $query = Website::where('website.status', 1)
  1320. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1321. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1322. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1323. ->select(
  1324. "website_template_info.id",
  1325. "template_class.name",
  1326. "website.id as website_id",
  1327. "website.website_name",
  1328. "website.website_url",
  1329. "website_template_info.action_id",
  1330. "website_template_info.created_at",
  1331. "website_template_info.updated_at",
  1332. "website_template_info.page_type",
  1333. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1334. "template.template_name"
  1335. );
  1336. //若存在条件;则在$query的基础上进行筛选
  1337. if(isset($data['website_name']) && !empty($data['website_name'])){
  1338. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1339. }
  1340. if(isset($data['status']) && !empty($data['status'])){
  1341. $query->where('website_template_info.status', $data['status']);
  1342. }
  1343. $count = $query->count();
  1344. $query->limit($data['pageSize'])
  1345. ->offset(($data['page'] - 1) * $data['pageSize'])
  1346. ->orderBy("website_template_info.updated_at", "desc");
  1347. $rep = $query->get();
  1348. if ($rep->count() == 0) {
  1349. return Result::error("没有查找到相关数据", 0);
  1350. } else {
  1351. $rep->each(function ($item) {
  1352. if (!empty($item->page_type)) {
  1353. $pageTypeArray = json_decode($item->page_type, true);
  1354. if (is_array($pageTypeArray)) {
  1355. $item->page_type = $pageTypeArray;
  1356. }
  1357. }
  1358. });
  1359. $result = [
  1360. "rows" => $rep->toArray(),
  1361. "count" => $count,
  1362. ];
  1363. return Result::success($result);
  1364. }
  1365. // return Result::success($result);
  1366. }
  1367. /**
  1368. * 添加网站基础信息
  1369. * @param array $data
  1370. * @return array
  1371. */
  1372. public function addWebsiteTemplateintel(array $data): array
  1373. {
  1374. $website = Website::where('website.id', $data['website_id'])->first();
  1375. if (empty($website)) {
  1376. return Result::error("请输入正确的网站id!", 0);
  1377. }
  1378. if (empty($website['website_column_id'])) {
  1379. return Result::error("请先关联导航池!", 0);
  1380. }
  1381. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1382. $foot_type = FooterCategory::where('website_id',$data['website_id'])->pluck('type')->toArray();
  1383. if (empty($foot_type)) {
  1384. return Result::error("请先关联底部导航池!", 0);
  1385. } elseif (in_array(1, $foot_type)) {
  1386. $foot_type = 1;
  1387. } else {
  1388. $foot_type = 0;
  1389. }
  1390. $result['foot_type'] = $foot_type;
  1391. // 友情链接类型'1:图片 2:文字 3:底部';
  1392. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1393. $missingTypes = [];
  1394. if (!in_array(1, $types)) {
  1395. $missingTypes[] = "文字链接";
  1396. }
  1397. if (!in_array(2, $types)) {
  1398. $missingTypes[] = "图片链接";
  1399. }
  1400. if (!in_array(3, $types)) {
  1401. $missingTypes[] = "底部链接";
  1402. }
  1403. if (!empty($missingTypes)) {
  1404. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1405. return Result::error($errorMessage, 0);
  1406. }
  1407. if(isset($data['page_type'])){
  1408. // 删除重复元素并重新索引数组
  1409. $data['page_type'] = array_unique($data['page_type']);
  1410. $data['page_type'] = array_values($data['page_type']);
  1411. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1412. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1413. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1414. if (empty($info)) {
  1415. // 将数组转换为 JSON 字符串
  1416. $data['page_type'] = json_encode($data['page_type'])??'';
  1417. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1418. $data['action_id'] = 1;
  1419. try {
  1420. $result = WebsiteTemplateInfo::insertGetId($data);
  1421. } catch (\Exception $e) {
  1422. if ($e->getCode() == 22001) {
  1423. $errorMessage = $e->getMessage();
  1424. return Result::error("请检查表单,某个字段超出了长度限制!");
  1425. } else {
  1426. // 处理其他类型的数据库错误
  1427. return Result::error("插入操作失败: " . $e->getMessage());
  1428. }
  1429. return Result::error("添加失败!", 0);
  1430. }
  1431. } else {
  1432. return Result::error("该网站已经构建过了!", 0);
  1433. }
  1434. } else {
  1435. return Result::error("请先选择首页和底部导航详情页!", 0);
  1436. }
  1437. }
  1438. if (empty($result)) {
  1439. return Result::error("添加失败!", 0);
  1440. } else {
  1441. return Result::success($result);
  1442. }
  1443. }
  1444. /**
  1445. * 获取网站基础信息
  1446. *
  1447. * @return array
  1448. */
  1449. public function getWebsiteTemplateintel(array $data): array
  1450. {
  1451. $result = WebsiteTemplateInfo::where('website_template_info.website_id',$data['website_id'])
  1452. ->leftJoin('footer_category','footer_category.website_id','website_template_info.website_id')
  1453. ->leftJoin('website','website.id','website_template_info.website_id')
  1454. ->select(
  1455. "website_template_info.*",
  1456. "footer_category.type",
  1457. "website.website_name",
  1458. "website.id as website_id"
  1459. )
  1460. ->first();
  1461. if(empty($result['website_name'])){
  1462. return Result::error("请输入正确的网站id!", 0);
  1463. }
  1464. if ($result) {
  1465. $result['page_type'] = json_decode($result['page_type'], true);
  1466. return Result::success($result);
  1467. } else {
  1468. return Result::error("请先添加网站基础信息!", 0);
  1469. }
  1470. }
  1471. /**
  1472. * 修改网站基础信息
  1473. *
  1474. * @return array
  1475. */
  1476. public function upWebsiteTemplateintel(array $data): array
  1477. {
  1478. $web = Website::where('website.id', $data['website_id'])->first();
  1479. if (empty($web)) {
  1480. return Result::error("请输入正确的网站id!", 0);
  1481. }
  1482. $where = ['website_id' => $data['website_id']];
  1483. $result = WebsiteTemplateInfo::where($where)->first();
  1484. if (empty($result)) {
  1485. return Result::error("请先添加网站基础信息!", 0);
  1486. } else {
  1487. unset($data['website_id']);
  1488. // 删除重复元素
  1489. $data['page_type'] = array_unique($data['page_type']);
  1490. // 重新索引数组
  1491. $data['page_type'] = array_values($data['page_type']);
  1492. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1493. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1494. // 将数组转换为 JSON 字符串
  1495. $data['page_type'] = json_encode($data['page_type'])??'';
  1496. try {
  1497. $result = WebsiteTemplateInfo::where($where)->update($data);
  1498. } catch (\Exception $e) {
  1499. if ($e->getCode() == 22001) {
  1500. $errorMessage = $e->getMessage();
  1501. return Result::error("请检查表单,某个字段超出了长度限制!");
  1502. } else {
  1503. // 处理其他类型的数据库错误
  1504. return Result::error("修改操作失败: " . $e->getMessage());
  1505. }
  1506. return Result::error("修改失败!", 0);
  1507. }
  1508. }else{
  1509. return Result::error("请先选择首页和底部导航详情页!",0);
  1510. }
  1511. }
  1512. if (empty($result)) {
  1513. return Result::error("修改失败!", 0);
  1514. } else {
  1515. return Result::success($result);
  1516. }
  1517. }
  1518. /**
  1519. * 获取所有网站风格信息
  1520. *
  1521. * @return array
  1522. */
  1523. public function getAllTemplateClass(array $data): array
  1524. {
  1525. $result = TemplateClass::all();
  1526. if (empty($result)) {
  1527. return Result::error("没有查找到相关数据!", 0);
  1528. } else {
  1529. return Result::success($result);
  1530. }
  1531. }
  1532. /**
  1533. * 获取模板内容
  1534. * @return void
  1535. */
  1536. public function getWebsiteTemplateInfo(array $data): array //丢了
  1537. {
  1538. $wehre = [
  1539. "website_id" =>$data['website_id']
  1540. ];
  1541. $result = WebsiteTemplate::where($wehre)->first();
  1542. if($result){
  1543. return Result::success($result);
  1544. }else{
  1545. return Result::error("没有数据",0);
  1546. }
  1547. }
  1548. /**
  1549. * 搜索并获取网站模板信息
  1550. * @param array $data
  1551. * @return array
  1552. */
  1553. public function getWebsiteTemplateList(array $data): array
  1554. {
  1555. $website = Website::where('website.id',$data['website_id'])->first();
  1556. if(empty($website)){
  1557. return Result::error("请输入正确的网站id!",0);
  1558. }else{
  1559. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1560. if(empty($page_type)){
  1561. return Result::error("此网站还未添加基础信息!",0);
  1562. } else{
  1563. $where = json_decode($page_type['page_type'], true);
  1564. // $where = $data["page_type"];
  1565. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1566. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1567. if(empty($template_class)){
  1568. return Result::error("请输入正确的模板风格id!",0);
  1569. }else{
  1570. // 获取指定风格下的模板
  1571. $rep = Template::where('template_class_id', $data['template_class_id'])
  1572. ->where(function ($query) use ($where) {
  1573. foreach ($where as $value) {
  1574. $query->whereJsonContains('template_img', ['value' => $value]);
  1575. }
  1576. })
  1577. ->limit($data['pageSize'])
  1578. ->offset(($data['page'] - 1) * $data['pageSize'])
  1579. ->orderBy("template.updated_at", "desc")
  1580. ->get();
  1581. $count = Template::where('template_class_id', $data['template_class_id'])
  1582. ->where(function ($query) use ($where) {
  1583. foreach ($where as $value) {
  1584. $query->whereJsonContains('template_img', ['value' => $value]);
  1585. }
  1586. })->count();
  1587. }
  1588. }else{
  1589. //获取所有模板
  1590. $rep = Template::where(function ($query) use ($where) {
  1591. foreach ($where as $value) {
  1592. $query->whereJsonContains('template_img', ['value' => $value]);
  1593. }
  1594. })
  1595. ->limit($data['pageSize'])
  1596. ->offset(($data['page'] - 1) * $data['pageSize'])
  1597. ->orderBy("template.updated_at", "desc")
  1598. ->get();
  1599. $count = Template::where(function ($query) use ($where) {
  1600. foreach ($where as $value) {
  1601. $query->whereJsonContains('template_img', ['value' => $value]);
  1602. }
  1603. })->count();
  1604. }
  1605. if(empty($rep) || $rep->count()==0){
  1606. return Result::error("没有查找到相关数据!",0);
  1607. }else{
  1608. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1609. // $rep->each(function ($item) use ($where) {
  1610. // $template_img = json_decode($item->template_img, true);
  1611. // if(is_array($template_img)){
  1612. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1613. // return isset($img['value']) && in_array($img['value'], $where);
  1614. // });
  1615. // }
  1616. // $item->template_img = $filtered_img;
  1617. // });
  1618. // $result = [
  1619. // "rows" => $rep->toArray(),
  1620. // "count" => $count
  1621. // ];
  1622. $rep->each(function ($item) use ($where) {
  1623. $template_img = json_decode($item->template_img, true);
  1624. if (is_array($template_img)) {
  1625. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1626. return isset($img['value']) && in_array($img['value'], $where);
  1627. });
  1628. // 对过滤后的数组进行重新排序
  1629. $sorted_img = array_values($filtered_img);
  1630. $item->template_img = $sorted_img;
  1631. } else {
  1632. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1633. }
  1634. });
  1635. $result = [
  1636. "rows" => $rep->toArray(),
  1637. "count" => $count,
  1638. ];
  1639. }
  1640. }
  1641. }
  1642. return Result::success($result);
  1643. }
  1644. /**
  1645. * 添加网站模板
  1646. * @param array $data
  1647. */
  1648. public function addWebsiteTemplateclassintel(array $data): array
  1649. {
  1650. $template = Template::where('id', $data['template_id'])->first();
  1651. $web = Website::where('id', $data['website_id'])->first();
  1652. if (empty($template)) {
  1653. return Result::error("请输入正确的模板id", 0);
  1654. }
  1655. if (empty($web)) {
  1656. return Result::error("请输入正确的网站id", 0);
  1657. }
  1658. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1659. if(empty($result)){
  1660. return Result::error("添加失败",0);
  1661. }else{
  1662. return Result::success($result);
  1663. }
  1664. }
  1665. /**
  1666. * 获取网站模板信息
  1667. * @param array $data
  1668. */
  1669. public function getWebsiteTemplateclassintel(array $data): array
  1670. {
  1671. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1672. if (empty($template)) {
  1673. return Result::error("请输入正确的搭建网站id", 0);
  1674. }
  1675. $page_type = json_decode($template['page_type'], true);
  1676. // 获取指定网站下的基础信息和模板及风格信息
  1677. $result = WebsiteTemplateInfo::where([
  1678. ['website_template_info.website_id', $data['website_id']],
  1679. ['template_id', '!=', null],
  1680. ])
  1681. ->leftJoin('template','template.id','website_template_info.template_id')
  1682. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1683. ->where(function ($query) use ($page_type) {
  1684. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1685. foreach ($page_type as $pageType) {
  1686. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1687. }
  1688. })
  1689. ->select(
  1690. 'template.template_name',
  1691. 'template.template_img',
  1692. 'template.id as tid',
  1693. 'template_class.name',
  1694. 'template_class.id as class_id',
  1695. 'website_template_info.page_type',
  1696. 'website_template_info.website_id')
  1697. ->first();
  1698. if(empty($result)){
  1699. return Result::error("没有查找到相关数据",0);
  1700. }
  1701. // return Result::success($result);
  1702. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1703. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1704. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1705. foreach ($template_img as $key => $value) {
  1706. if (!in_array($value['value'], $page_type)) {
  1707. unset($template_img[$key]);
  1708. }
  1709. }
  1710. // return Result::success($result);
  1711. $result['template_img'] = array_values($template_img);
  1712. $result['page_type'] = $page_type;
  1713. // $templateList = $this->getWebsiteTemplateList($data);
  1714. // $count = $templateList['data']['count']??'';
  1715. // $pages = $count / $data['pageSize'];
  1716. // $found = false;
  1717. // while ($pages && !$found) {
  1718. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1719. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1720. // if ($index !== false) {
  1721. // $found = true;
  1722. // $result['page'] = $pages;
  1723. // $pages = 0;
  1724. // // echo "查询到的 template 在第 $page 页";
  1725. // }
  1726. // $pages--;
  1727. // }
  1728. return Result::success($result);
  1729. }
  1730. /**
  1731. * 保存网站模板
  1732. * @param array $data
  1733. * @return array
  1734. */
  1735. public function addWebsiteTemplate(array $data): array
  1736. {
  1737. $result = WebsiteTemplate::updateOrInsert(['website_id'=>$data['webSiteData']['base']['websiteId']],['template_data'=>json_encode($data['webSiteData']),'updated_at'=>date("Y-m-d H:i:s",time())]);
  1738. if($result){
  1739. return Result::success($result);
  1740. }else{
  1741. return Result::error("创建失败",0);
  1742. }
  1743. }
  1744. /**
  1745. * 预览网站首页模板数据
  1746. * @param array $data
  1747. * @return array
  1748. */
  1749. public function getWebsiteTemplateData(array $data): array
  1750. {
  1751. $wehre = [
  1752. "website_id" =>$data['website_id']
  1753. ];
  1754. $result = WebsiteTemplate::where($wehre)->first();
  1755. if($result){
  1756. $templateData = json_decode($result['template_data'],true);
  1757. // var_dump("数据:",$templateData[1]);
  1758. $templateData[1] = $this->getNewsList($templateData[1]);
  1759. return Result::success($templateData);
  1760. }else{
  1761. return Result::error("没有数据",0);
  1762. }
  1763. }
  1764. /**
  1765. * 获取列表数据
  1766. * @return void
  1767. */
  1768. public function getNewsList(array $data): array
  1769. {
  1770. if($data){
  1771. foreach ($data as $key=>$val){
  1772. if($val){
  1773. foreach ($val['data'] as $k=>$item){
  1774. if($item['isReturn'] && intval($item['isReturn'])==1){
  1775. $imgList = [];
  1776. if($item['data']['imgNum'] && intval($item['data']['imgNum'])>0){
  1777. $imgList = Article::where('imgurl',"!=","")
  1778. ->where('catid',$item['data']['category_id'])
  1779. ->select($item['data']['selectField'])
  1780. ->orderBy("created_at","desc")
  1781. ->offset(0)
  1782. ->limit(intval($item['data']['imgNum']))
  1783. ->get();
  1784. $imgList = $imgList->toArray();
  1785. $listIds = [];
  1786. if ($imgList){
  1787. $listIds = array_column($imgList, 'id');
  1788. }
  1789. $dataList = Article::whereNotIn('id',$listIds)
  1790. ->where('catid',$item['data']['category_id'])
  1791. ->select($item['data']['selectField'])
  1792. ->orderBy("created_at","desc")
  1793. ->offset(0)
  1794. ->limit(intval($item['data']['pageSize'])-intval($item['data']['imgNum']))
  1795. ->get();
  1796. $dataList = $dataList->toArray();
  1797. var_dump("图片列表:",$imgList,"数据列表:",$dataList);
  1798. $datas = array_merge($imgList,$dataList);
  1799. // return $datas;
  1800. var_dump("合并列表:",$datas);
  1801. $data[$key]['data'][$k]['dataList'] = $datas;
  1802. }else{
  1803. $dataList = Article::where('catid',$item['data']['category_id'])
  1804. ->select($item['data']['selectField'])
  1805. ->orderBy("created_at","desc")
  1806. ->offset(0)
  1807. ->limit(intval($item['data']['pageSize']))
  1808. ->get();
  1809. $dataList = $dataList->toArray();
  1810. var_dump("数据列表:",$dataList);
  1811. }
  1812. }
  1813. }
  1814. }
  1815. }
  1816. }
  1817. return $data;
  1818. }
  1819. }