WebsiteService.php 64 KB

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