WebsiteService.php 64 KB

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