WebsiteService.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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'])){
  1208. $website = Website::query()->where('id',$data['website_id'])->first();
  1209. if (empty($website)) {
  1210. return Result::error("找不到网站",0);
  1211. }
  1212. }else{
  1213. return Result::error("参数错误",0);
  1214. }
  1215. $websiteInfo = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1216. if (empty($websiteInfo)) {
  1217. return Result::error("暂无底部基础信息",0);
  1218. }
  1219. return Result::success($websiteInfo->toArray());
  1220. }
  1221. /**
  1222. * 获取网站底部导航
  1223. * @param array $data
  1224. * @return array
  1225. */
  1226. public function getWebsiteFooterCategory(array $data): array
  1227. {
  1228. if(isset($data['website_id'])){
  1229. $website = Website::query()->where('id',$data['website_id'])->first();
  1230. if (empty($website)) {
  1231. return Result::error("找不到网站",0);
  1232. }
  1233. }else{
  1234. return Result::error("参数错误",0);
  1235. }
  1236. $result = FooterCategory::where('website_id',$data['website_id'])->get();
  1237. if (empty($result)) {
  1238. return Result::error("暂无底部导航",0);
  1239. }
  1240. return Result::success($result->toArray());
  1241. }
  1242. /**
  1243. * 获取网站底部导航列表
  1244. * @param array $data
  1245. * @return array
  1246. */
  1247. public function getWebsiteFooterCategoryList(array $data): array
  1248. {
  1249. if(isset($data['website_id'])){
  1250. $website = Website::query()->where('id',$data['website_id'])->first();
  1251. if (empty($website)) {
  1252. return Result::error("找不到网站",0);
  1253. }
  1254. }else{
  1255. return Result::error("参数错误",0);
  1256. }
  1257. $footercategory = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1258. // '底部导航类型 0:内容型;1:列表型;',
  1259. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1260. return Result::error("底部导航id错误",0);
  1261. }else{
  1262. $query = FooterContent::where('fcat_id',$data['fcat_id']);
  1263. if($query->count() == 0){
  1264. return Result::error("暂无底部导航列表",0);
  1265. }elseif($query->count() == 1){
  1266. $result = $query->first();
  1267. }else{
  1268. $result = $query->get();
  1269. }
  1270. }
  1271. return Result::success($result);
  1272. }
  1273. /**
  1274. * 获取网站底部基础信息
  1275. * @param array $data
  1276. * @return array
  1277. */
  1278. public function getWebsiteFooterCategoryInfo(array $data): array
  1279. {
  1280. if(isset($data['website_id'])){
  1281. $website = Website::query()->where('id',$data['website_id'])->first();
  1282. if (empty($website)) {
  1283. return Result::error("找不到网站",0);
  1284. }
  1285. }else{
  1286. return Result::error("参数错误",0);
  1287. }
  1288. if (isset($data['type']) && $data['type'] == 0) {
  1289. $fcatid = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1290. if (empty($fcatid)) {
  1291. return Result::error("底部导航id错误",0);
  1292. }
  1293. $result = FooterContent::where('fcat_id',$data['fcat_id'])->first();
  1294. }else{
  1295. $result = FooterContent::where('id',$data['fcat_id'])->first();
  1296. }
  1297. if (empty($result)) {
  1298. return Result::error("暂无底部导航内容",0);
  1299. }
  1300. return Result::success($result);
  1301. }
  1302. /**
  1303. * 获取并搜索 网站模板信息
  1304. * @param array $data
  1305. * @return array
  1306. */
  1307. public function getWebsiteintel(array $data): array
  1308. {
  1309. //查询所有网站模板信息
  1310. $query = Website::where('website.status', 1)
  1311. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1312. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1313. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1314. ->select(
  1315. "website_template_info.id",
  1316. "template_class.name",
  1317. "website.id as website_id",
  1318. "website.website_name",
  1319. "website.website_url",
  1320. "website_template_info.action_id",
  1321. "website_template_info.created_at",
  1322. "website_template_info.updated_at",
  1323. "website_template_info.page_type",
  1324. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1325. "template.template_name"
  1326. );
  1327. //若存在条件;则在$query的基础上进行筛选
  1328. if(isset($data['website_name']) && !empty($data['website_name'])){
  1329. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1330. }
  1331. if(isset($data['status']) && !empty($data['status'])){
  1332. $query->where('website_template_info.status', $data['status']);
  1333. }
  1334. $count = $query->count();
  1335. $query->limit($data['pageSize'])
  1336. ->offset(($data['page'] - 1) * $data['pageSize'])
  1337. ->orderBy("website_template_info.updated_at", "desc");
  1338. $rep = $query->get();
  1339. if ($rep->count() == 0) {
  1340. return Result::error("没有查找到相关数据", 0);
  1341. } else {
  1342. $rep->each(function ($item) {
  1343. if (!empty($item->page_type)) {
  1344. $pageTypeArray = json_decode($item->page_type, true);
  1345. if (is_array($pageTypeArray)) {
  1346. $item->page_type = $pageTypeArray;
  1347. }
  1348. }
  1349. });
  1350. $result = [
  1351. "rows" => $rep->toArray(),
  1352. "count" => $count,
  1353. ];
  1354. return Result::success($result);
  1355. }
  1356. // return Result::success($result);
  1357. }
  1358. /**
  1359. * 添加网站基础信息
  1360. * @param array $data
  1361. * @return array
  1362. */
  1363. public function addWebsiteTemplateintel(array $data): array
  1364. {
  1365. $website = Website::where('website.id', $data['website_id'])->first();
  1366. if (empty($website)) {
  1367. return Result::error("请输入正确的网站id!", 0);
  1368. }
  1369. if (empty($website['website_column_id'])) {
  1370. return Result::error("请先关联导航池!", 0);
  1371. }
  1372. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1373. $foot_type = FooterCategory::where('website_id',$data['website_id'])->pluck('type')->toArray();
  1374. if (empty($foot_type)) {
  1375. return Result::error("请先关联底部导航池!", 0);
  1376. } elseif (in_array(1, $foot_type)) {
  1377. $foot_type = 1;
  1378. } else {
  1379. $foot_type = 0;
  1380. }
  1381. $result['foot_type'] = $foot_type;
  1382. // 友情链接类型'1:图片 2:文字 3:底部';
  1383. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1384. $missingTypes = [];
  1385. if (!in_array(1, $types)) {
  1386. $missingTypes[] = "文字链接";
  1387. }
  1388. if (!in_array(2, $types)) {
  1389. $missingTypes[] = "图片链接";
  1390. }
  1391. if (!in_array(3, $types)) {
  1392. $missingTypes[] = "底部链接";
  1393. }
  1394. if (!empty($missingTypes)) {
  1395. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1396. return Result::error($errorMessage, 0);
  1397. }
  1398. if(isset($data['page_type'])){
  1399. // 删除重复元素并重新索引数组
  1400. $data['page_type'] = array_unique($data['page_type']);
  1401. $data['page_type'] = array_values($data['page_type']);
  1402. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1403. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1404. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1405. if (empty($info)) {
  1406. // 将数组转换为 JSON 字符串
  1407. $data['page_type'] = json_encode($data['page_type'])??'';
  1408. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1409. $data['action_id'] = 1;
  1410. try {
  1411. $result = WebsiteTemplateInfo::insertGetId($data);
  1412. } catch (\Exception $e) {
  1413. if ($e->getCode() == 22001) {
  1414. $errorMessage = $e->getMessage();
  1415. return Result::error("请检查表单,某个字段超出了长度限制!");
  1416. } else {
  1417. // 处理其他类型的数据库错误
  1418. return Result::error("插入操作失败: " . $e->getMessage());
  1419. }
  1420. return Result::error("添加失败!", 0);
  1421. }
  1422. } else {
  1423. return Result::error("该网站已经构建过了!", 0);
  1424. }
  1425. } else {
  1426. return Result::error("请先选择首页和底部导航详情页!", 0);
  1427. }
  1428. }
  1429. if (empty($result)) {
  1430. return Result::error("添加失败!", 0);
  1431. } else {
  1432. return Result::success($result);
  1433. }
  1434. }
  1435. /**
  1436. * 获取网站基础信息
  1437. *
  1438. * @return array
  1439. */
  1440. public function getWebsiteTemplateintel(array $data): array
  1441. {
  1442. $result = WebsiteTemplateInfo::where('website_template_info.website_id',$data['website_id'])
  1443. ->leftJoin('footer_category','footer_category.website_id','website_template_info.website_id')
  1444. ->leftJoin('website','website.id','website_template_info.website_id')
  1445. ->select(
  1446. "website_template_info.*",
  1447. "footer_category.type",
  1448. "website.website_name",
  1449. "website.id as website_id"
  1450. )
  1451. ->first();
  1452. if(empty($result['website_name'])){
  1453. return Result::error("请输入正确的网站id!", 0);
  1454. }
  1455. if ($result) {
  1456. $result['page_type'] = json_decode($result['page_type'], true);
  1457. return Result::success($result);
  1458. } else {
  1459. return Result::error("请先添加网站基础信息!", 0);
  1460. }
  1461. }
  1462. /**
  1463. * 修改网站基础信息
  1464. *
  1465. * @return array
  1466. */
  1467. public function upWebsiteTemplateintel(array $data): array
  1468. {
  1469. $web = Website::where('website.id', $data['website_id'])->first();
  1470. if (empty($web)) {
  1471. return Result::error("请输入正确的网站id!", 0);
  1472. }
  1473. $where = ['website_id' => $data['website_id']];
  1474. $result = WebsiteTemplateInfo::where($where)->first();
  1475. if (empty($result)) {
  1476. return Result::error("请先添加网站基础信息!", 0);
  1477. } else {
  1478. unset($data['website_id']);
  1479. // 删除重复元素
  1480. $data['page_type'] = array_unique($data['page_type']);
  1481. // 重新索引数组
  1482. $data['page_type'] = array_values($data['page_type']);
  1483. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1484. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1485. // 将数组转换为 JSON 字符串
  1486. $data['page_type'] = json_encode($data['page_type'])??'';
  1487. try {
  1488. $result = WebsiteTemplateInfo::where($where)->update($data);
  1489. } catch (\Exception $e) {
  1490. if ($e->getCode() == 22001) {
  1491. $errorMessage = $e->getMessage();
  1492. return Result::error("请检查表单,某个字段超出了长度限制!");
  1493. } else {
  1494. // 处理其他类型的数据库错误
  1495. return Result::error("修改操作失败: " . $e->getMessage());
  1496. }
  1497. return Result::error("修改失败!", 0);
  1498. }
  1499. }else{
  1500. return Result::error("请先选择首页和底部导航详情页!",0);
  1501. }
  1502. }
  1503. if (empty($result)) {
  1504. return Result::error("修改失败!", 0);
  1505. } else {
  1506. return Result::success($result);
  1507. }
  1508. }
  1509. /**
  1510. * 获取所有网站风格信息
  1511. *
  1512. * @return array
  1513. */
  1514. public function getAllTemplateClass(array $data): array
  1515. {
  1516. $result = TemplateClass::all();
  1517. if (empty($result)) {
  1518. return Result::error("没有查找到相关数据!", 0);
  1519. } else {
  1520. return Result::success($result);
  1521. }
  1522. }
  1523. /**
  1524. * 获取模板内容
  1525. * @return void
  1526. */
  1527. public function getWebsiteTemplateInfo(array $data): array //丢了
  1528. {
  1529. $wehre = [
  1530. "website_id" =>$data['website_id']
  1531. ];
  1532. $result = WebsiteTemplate::where($wehre)->first();
  1533. if($result){
  1534. return Result::success($result);
  1535. }else{
  1536. return Result::error("没有数据",0);
  1537. }
  1538. }
  1539. /**
  1540. * 搜索并获取网站模板信息
  1541. * @param array $data
  1542. * @return array
  1543. */
  1544. public function getWebsiteTemplateList(array $data): array
  1545. {
  1546. $website = Website::where('website.id',$data['website_id'])->first();
  1547. if(empty($website)){
  1548. return Result::error("请输入正确的网站id!",0);
  1549. }else{
  1550. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1551. if(empty($page_type)){
  1552. return Result::error("此网站还未添加基础信息!",0);
  1553. } else{
  1554. $where = json_decode($page_type['page_type'], true);
  1555. // $where = $data["page_type"];
  1556. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1557. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1558. if(empty($template_class)){
  1559. return Result::error("请输入正确的模板风格id!",0);
  1560. }else{
  1561. // 获取指定风格下的模板
  1562. $rep = Template::where('template_class_id', $data['template_class_id'])
  1563. ->where(function ($query) use ($where) {
  1564. foreach ($where as $value) {
  1565. $query->whereJsonContains('template_img', ['value' => $value]);
  1566. }
  1567. })
  1568. ->limit($data['pageSize'])
  1569. ->offset(($data['page'] - 1) * $data['pageSize'])
  1570. ->orderBy("template.updated_at", "desc")
  1571. ->get();
  1572. $count = Template::where('template_class_id', $data['template_class_id'])
  1573. ->where(function ($query) use ($where) {
  1574. foreach ($where as $value) {
  1575. $query->whereJsonContains('template_img', ['value' => $value]);
  1576. }
  1577. })->count();
  1578. }
  1579. }else{
  1580. //获取所有模板
  1581. $rep = Template::where(function ($query) use ($where) {
  1582. foreach ($where as $value) {
  1583. $query->whereJsonContains('template_img', ['value' => $value]);
  1584. }
  1585. })
  1586. ->limit($data['pageSize'])
  1587. ->offset(($data['page'] - 1) * $data['pageSize'])
  1588. ->orderBy("template.updated_at", "desc")
  1589. ->get();
  1590. $count = Template::where(function ($query) use ($where) {
  1591. foreach ($where as $value) {
  1592. $query->whereJsonContains('template_img', ['value' => $value]);
  1593. }
  1594. })->count();
  1595. }
  1596. if(empty($rep) || $rep->count()==0){
  1597. return Result::error("没有查找到相关数据!",0);
  1598. }else{
  1599. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1600. // $rep->each(function ($item) use ($where) {
  1601. // $template_img = json_decode($item->template_img, true);
  1602. // if(is_array($template_img)){
  1603. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1604. // return isset($img['value']) && in_array($img['value'], $where);
  1605. // });
  1606. // }
  1607. // $item->template_img = $filtered_img;
  1608. // });
  1609. // $result = [
  1610. // "rows" => $rep->toArray(),
  1611. // "count" => $count
  1612. // ];
  1613. $rep->each(function ($item) use ($where) {
  1614. $template_img = json_decode($item->template_img, true);
  1615. if (is_array($template_img)) {
  1616. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1617. return isset($img['value']) && in_array($img['value'], $where);
  1618. });
  1619. // 对过滤后的数组进行重新排序
  1620. $sorted_img = array_values($filtered_img);
  1621. $item->template_img = $sorted_img;
  1622. } else {
  1623. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1624. }
  1625. });
  1626. $result = [
  1627. "rows" => $rep->toArray(),
  1628. "count" => $count,
  1629. ];
  1630. }
  1631. }
  1632. }
  1633. return Result::success($result);
  1634. }
  1635. /**
  1636. * 添加网站模板
  1637. * @param array $data
  1638. */
  1639. public function addWebsiteTemplateclassintel(array $data): array
  1640. {
  1641. $template = Template::where('id', $data['template_id'])->first();
  1642. $web = Website::where('id', $data['website_id'])->first();
  1643. if (empty($template)) {
  1644. return Result::error("请输入正确的模板id", 0);
  1645. }
  1646. if (empty($web)) {
  1647. return Result::error("请输入正确的网站id", 0);
  1648. }
  1649. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1650. if(empty($result)){
  1651. return Result::error("添加失败",0);
  1652. }else{
  1653. return Result::success($result);
  1654. }
  1655. }
  1656. /**
  1657. * 获取网站模板信息
  1658. * @param array $data
  1659. */
  1660. public function getWebsiteTemplateclassintel(array $data): array
  1661. {
  1662. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1663. if (empty($template)) {
  1664. return Result::error("请输入正确的搭建网站id", 0);
  1665. }
  1666. $page_type = json_decode($template['page_type'], true);
  1667. // 获取指定网站下的基础信息和模板及风格信息
  1668. $result = WebsiteTemplateInfo::where([
  1669. ['website_template_info.website_id', $data['website_id']],
  1670. ['template_id', '!=', null],
  1671. ])
  1672. ->leftJoin('template','template.id','website_template_info.template_id')
  1673. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1674. ->where(function ($query) use ($page_type) {
  1675. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1676. foreach ($page_type as $pageType) {
  1677. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1678. }
  1679. })
  1680. ->select(
  1681. 'template.template_name',
  1682. 'template.template_img',
  1683. 'template.id as tid',
  1684. 'template_class.name',
  1685. 'template_class.id as class_id',
  1686. 'website_template_info.page_type',
  1687. 'website_template_info.website_id')
  1688. ->first();
  1689. if(empty($result)){
  1690. return Result::error("没有查找到相关数据",0);
  1691. }
  1692. // return Result::success($result);
  1693. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1694. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1695. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1696. foreach ($template_img as $key => $value) {
  1697. if (!in_array($value['value'], $page_type)) {
  1698. unset($template_img[$key]);
  1699. }
  1700. }
  1701. // return Result::success($result);
  1702. $result['template_img'] = array_values($template_img);
  1703. $result['page_type'] = $page_type;
  1704. // $templateList = $this->getWebsiteTemplateList($data);
  1705. // $count = $templateList['data']['count']??'';
  1706. // $pages = $count / $data['pageSize'];
  1707. // $found = false;
  1708. // while ($pages && !$found) {
  1709. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1710. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1711. // if ($index !== false) {
  1712. // $found = true;
  1713. // $result['page'] = $pages;
  1714. // $pages = 0;
  1715. // // echo "查询到的 template 在第 $page 页";
  1716. // }
  1717. // $pages--;
  1718. // }
  1719. return Result::success($result);
  1720. }
  1721. /**
  1722. * 保存网站模板
  1723. * @param array $data
  1724. * @return array
  1725. */
  1726. public function addWebsiteTemplate(array $data): array
  1727. {
  1728. $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())]);
  1729. if($result){
  1730. return Result::success($result);
  1731. }else{
  1732. return Result::error("创建失败",0);
  1733. }
  1734. }
  1735. /**
  1736. * 预览网站首页模板数据
  1737. * @param array $data
  1738. * @return array
  1739. */
  1740. public function getWebsiteTemplateData(array $data): array
  1741. {
  1742. $wehre = [
  1743. "website_id" =>$data['website_id']
  1744. ];
  1745. $result = WebsiteTemplate::where($wehre)->first();
  1746. if($result){
  1747. $templateData = json_decode($result['template_data'],true);
  1748. // var_dump("数据:",$templateData[1]);
  1749. $templateData[1] = $this->getNewsList($templateData[1]);
  1750. return Result::success($templateData);
  1751. }else{
  1752. return Result::error("没有数据",0);
  1753. }
  1754. }
  1755. /**
  1756. * 获取列表数据
  1757. * @return void
  1758. */
  1759. public function getNewsList(array $data): array
  1760. {
  1761. if($data){
  1762. foreach ($data as $key=>$val){
  1763. if($val){
  1764. foreach ($val['data'] as $k=>$item){
  1765. if($item['isReturn'] && intval($item['isReturn'])==1){
  1766. $imgList = [];
  1767. if($item['data']['imgNum'] && intval($item['data']['imgNum'])>0){
  1768. $imgList = Article::where('imgurl',"!=","")
  1769. ->where('catid',$item['data']['category_id'])
  1770. ->select($item['data']['selectField'])
  1771. ->orderBy("created_at","desc")
  1772. ->offset(0)
  1773. ->limit(intval($item['data']['imgNum']))
  1774. ->get();
  1775. $imgList = $imgList->toArray();
  1776. $listIds = [];
  1777. if ($imgList){
  1778. $listIds = array_column($imgList, 'id');
  1779. }
  1780. $dataList = Article::whereNotIn('id',$listIds)
  1781. ->where('catid',$item['data']['category_id'])
  1782. ->select($item['data']['selectField'])
  1783. ->orderBy("created_at","desc")
  1784. ->offset(0)
  1785. ->limit(intval($item['data']['pageSize'])-intval($item['data']['imgNum']))
  1786. ->get();
  1787. $dataList = $dataList->toArray();
  1788. var_dump("图片列表:",$imgList,"数据列表:",$dataList);
  1789. $datas = array_merge($imgList,$dataList);
  1790. // return $datas;
  1791. var_dump("合并列表:",$datas);
  1792. $data[$key]['data'][$k]['dataList'] = $datas;
  1793. }else{
  1794. $dataList = Article::where('catid',$item['data']['category_id'])
  1795. ->select($item['data']['selectField'])
  1796. ->orderBy("created_at","desc")
  1797. ->offset(0)
  1798. ->limit(intval($item['data']['pageSize']))
  1799. ->get();
  1800. $dataList = $dataList->toArray();
  1801. var_dump("数据列表:",$dataList);
  1802. }
  1803. }
  1804. }
  1805. }
  1806. }
  1807. }
  1808. return $data;
  1809. }
  1810. }