WebsiteService.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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)->withCount(['children' => function ($query) use ($website_id) {
  591. $query->where($website_id);
  592. }])->orderBy('sort')->offset($placeid)->limit($num)->get();
  593. if(!empty($result)){
  594. return Result::success($result);
  595. }else{
  596. return Result::error("本网站暂无栏目",0);
  597. }
  598. }
  599. /**
  600. * 获取友情链接
  601. * @param array $data
  602. * @return array
  603. */
  604. public function selectWebsiteLinks(array $data): array
  605. {
  606. $where = [
  607. 'website_id' => $data['website_id'],
  608. 'status' => 1,
  609. 'type' => $data['type']
  610. ];
  611. $num=$data['num'];
  612. $result=Link::where($where)->orderBy('id')->limit($num)->get();
  613. if(!empty($result)){
  614. return Result::success($result);
  615. }else{
  616. return Result::error("本网站暂无此类型友情链接",0);
  617. }
  618. }
  619. /**
  620. * 网站首页数据统计, 管理员
  621. * @return void
  622. */
  623. public function getAdminIndex(array $data): array
  624. {
  625. var_dump("用户类型:", $data['type_id']);
  626. switch ($data['type_id']) {
  627. case 4:
  628. $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;');
  629. return Result::success($result);
  630. break;
  631. case 10000:
  632. $res = [];
  633. //网站
  634. $res['website']['count'] = 0;
  635. $res['website']['growth_rate'] = 0;
  636. //资讯
  637. $res['article']['count'] = 0;
  638. $res['article']['growth_rate'] = 0;
  639. //导航池
  640. $res['category']['count'] = 0;
  641. $res['category']['growth_rate'] = 0;
  642. //近一月数据
  643. $res['monthArticle'] = [];
  644. //用户类型
  645. $res['userType'] = [];
  646. $res['website']['count'] = Website::where([])->count();
  647. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  648. $res['category']['count'] = Category::where([])->count();
  649. $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;');
  650. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  651. return Result::success($res);
  652. }
  653. return [];
  654. }
  655. /**
  656. * 获取模板类型
  657. * @return void
  658. */
  659. public function getTemplateClass(array $data): array
  660. {
  661. $where = [];
  662. if (isset($data['name']) && $data['name']) {
  663. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  664. }
  665. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  666. if (empty($result)) {
  667. return Result::error("没有模板类型", 0);
  668. } else {
  669. return Result::success($result);
  670. }
  671. }
  672. /**
  673. * 添加模板类型
  674. * @param
  675. * @return void
  676. */
  677. public function addTemplateClass(array $data): array
  678. {
  679. $insertData = [
  680. 'name' => $data['name'],
  681. ];
  682. $result = TemplateClass::insertGetId($insertData);
  683. if (empty($result)) {
  684. return Result::error("创建失败", 0);
  685. } else {
  686. return Result::success(["id" => $result]);
  687. }
  688. }
  689. /**
  690. * 更新模板
  691. * @param array $data
  692. * @return array
  693. */
  694. public function upTemplateClass(array $data): array
  695. {
  696. $where = [
  697. 'id' => $data['id'],
  698. ];
  699. $insertData = [
  700. 'name' => $data['name'],
  701. ];
  702. $result = TemplateClass::where($where)->update($insertData);
  703. if (empty($result)) {
  704. return Result::error("更新失败", 0);
  705. } else {
  706. return Result::success();
  707. }
  708. }
  709. /**
  710. * 删除模板
  711. * @param array $data
  712. * @return array
  713. */
  714. public function delTemplateClass(array $data): array
  715. {
  716. $where = [
  717. 'id' => $data['id'],
  718. ];
  719. $result = TemplateClass::where($where)->delete();
  720. if (empty($result)) {
  721. return Result::error("删除失败", 0);
  722. } else {
  723. return Result::success();
  724. }
  725. }
  726. /**
  727. * 获取分类下的模板
  728. * @param array $data
  729. * @return array
  730. */
  731. public function getTemplate(array $data): array
  732. {
  733. $page = $data['page'];
  734. $pageSize = $data['pageSize'];
  735. $where = [];
  736. if (isset($data['template_class_id']) && $data['template_class_id']) {
  737. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  738. }
  739. $result = Template::where($where)
  740. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  741. $count = Template::where($where)->count();
  742. if (empty($result)) {
  743. return Result::error("没有数据", 0);
  744. }
  745. $data = [
  746. 'rows' => $result->toArray(),
  747. 'count' => $count,
  748. ];
  749. return Result::success($data);
  750. }
  751. /**
  752. * 创建模板
  753. * @param
  754. * @return void
  755. */
  756. public function addTemplate(array $data): array
  757. {
  758. $insertData = [
  759. 'template_name' => $data['template_name'],
  760. 'template_img' => json_encode($data['template_img']),
  761. 'template_class_id' => $data['template_class_id'],
  762. ];
  763. $result = Template::insertGetId($insertData);
  764. if (empty($result)) {
  765. return Result::error("创建模板失败", 0);
  766. } else {
  767. return Result::success(["id" => $result]);
  768. }
  769. }
  770. /**
  771. * 更新模板
  772. * @param array $data
  773. * @return array
  774. */
  775. public function upTemplate(array $data): array
  776. {
  777. $where = [
  778. 'id' => $data['id'],
  779. ];
  780. $insertData = [
  781. 'template_name' => $data['template_name'],
  782. 'template_img' => json_encode($data['template_img']),
  783. 'template_class_id' => $data['template_class_id'],
  784. ];
  785. $result = Template::where($where)->update($insertData);
  786. if (empty($result)) {
  787. return Result::error("更新模板失败", 0);
  788. } else {
  789. return Result::success();
  790. }
  791. }
  792. /**
  793. * 删除模板
  794. * @param array $data
  795. * @return array
  796. */
  797. public function delTemplate(array $data): array
  798. {
  799. $where = [
  800. 'id' => $data['id'],
  801. ];
  802. $result = Template::where($where)->delete();
  803. if (empty($result)) {
  804. return Result::error("删除模板失败", 0);
  805. } else {
  806. return Result::success();
  807. }
  808. }
  809. /**
  810. * 搜索网站
  811. * @param array $data
  812. * @return array
  813. */
  814. public function websiteList(array $data): array
  815. {
  816. $where = [];
  817. if (isset($data['keyword']) && !empty($data['keyword'])) {
  818. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  819. }
  820. $result = Website::where($where)->get();
  821. if ($result) {
  822. return Result::success($result);
  823. } else {
  824. return Result::error("没有网站", 0);
  825. }
  826. }
  827. public function addWebsiteCategory(array $data): array
  828. {
  829. $website_id = $data['website_id'];
  830. $category_arr_id = $data['category_arr_id'];
  831. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  832. $categoryListIds = [];
  833. if ($categoryList) {
  834. foreach ($categoryList->toArray() as $val) {
  835. array_push($categoryListIds, $val['id']);
  836. }
  837. }
  838. $arr = [];
  839. if ($categoryListIds) {
  840. foreach ($categoryListIds as $v) {
  841. $ids = $this->getUnderlingUIds(intval($v));
  842. $ids_arr = explode(",", $ids);
  843. array_push($arr, $ids_arr);
  844. }
  845. }
  846. $mergedArray = [];
  847. foreach ($arr as $subarray) {
  848. $mergedArray = array_merge($mergedArray, $subarray);
  849. }
  850. var_dump("所有:", $arr, $mergedArray);
  851. //查询出所有的分类进行分割插入 组装数据
  852. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  853. $categoryListData = $categoryListData->toArray();
  854. $insertData = [];
  855. if ($categoryListData) {
  856. foreach ($categoryListData as $key => $value) {
  857. $insertData[$key]['website_id'] = $website_id;
  858. $insertData[$key]['name'] = $value['name'];
  859. $insertData[$key]['sort'] = $value['sort'];
  860. $insertData[$key]['pid'] = $value['pid'];
  861. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  862. $insertData[$key]['seo_title'] = $value['seo_title'];
  863. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  864. $insertData[$key]['seo_description'] = $value['seo_description'];
  865. $insertData[$key]['alias'] = $value['name'];
  866. $insertData[$key]['category_id'] = $value['id'];
  867. }
  868. }
  869. $result = WebsiteCategory::insert($insertData);
  870. var_dump("插入数据状态:", $result);
  871. if ($result) {
  872. return Result::success($result);
  873. } else {
  874. return Result::error("创建失败", 0);
  875. }
  876. }
  877. /**
  878. * 删除网站导航
  879. * @param array $data
  880. * @return array
  881. */
  882. public function delWebsiteCategory(array $data): array
  883. {
  884. $website_id = $data['website_id'] ?? 0;
  885. $category_id = $data['category_id'] ?? 0;
  886. $ids = $this->getUnderlingUIds(intval($category_id));
  887. $ids_arr = explode(",", $ids);
  888. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  889. if ($result) {
  890. return Result::success($result);
  891. } else {
  892. return Result::error("删除失败", 0);
  893. }
  894. }
  895. /**
  896. * 获取网站导航
  897. * @param array $data
  898. * @return array
  899. */
  900. public function getAdminWebsiteCategory(array $data): array
  901. {
  902. $where = [
  903. 'website_id' => $data['website_id'],
  904. 'pid' => 0,
  905. ];
  906. $result = WebsiteCategory::where($where)->get();
  907. if ($result) {
  908. return Result::success($result);
  909. } else {
  910. return Result::error("查询失败", 0);
  911. }
  912. }
  913. /**
  914. * 更新网站导航
  915. * @param array $data
  916. * @return array
  917. */
  918. public function upWebsiteCategory(array $data): array
  919. {
  920. Db::beginTransaction();
  921. try {
  922. //合并栏目id
  923. $reqIds = array_merge($data['old_category_arr_id'], $data['new_category_arr_id']);
  924. //对比old 数组差异化,把差异化的删除
  925. $result = WebsiteCategory::where(['website_id' => $data['website_id'], 'pid' => 0])->get();
  926. $result = $result->toArray();
  927. $categoryIds = [];
  928. if ($result) {
  929. foreach ($result as $val) {
  930. array_push($categoryIds, $val['category_id']);
  931. }
  932. }
  933. //和原始数据对比取交际
  934. $reqidsIntersect = array_intersect($reqIds, $categoryIds);
  935. //再取差集 进行对比
  936. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds), array_diff($categoryIds, $reqidsIntersect));
  937. var_dump("差集:", $differenceIDS);
  938. $arr_ids = [];
  939. if (count($differenceIDS) > 0) {
  940. foreach ($differenceIDS as $vv) {
  941. $idV = $this->getUnderlingUIds(intval($vv));
  942. $ids_arrV = explode(",", $idV);
  943. array_push($arr_ids, $ids_arrV);
  944. }
  945. }
  946. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  947. //有差异 删除
  948. if (count($del_ids) > 0) {
  949. WebsiteCategory::where(['website_id' => $data['website_id']])->whereIn("category_id", $del_ids)->delete();
  950. }
  951. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  952. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect), array_diff($reqidsIntersect, $reqIds));
  953. var_dump("要存储的:", $insertIDS);
  954. //新的数组重新创建
  955. if (count($insertIDS) > 0) {
  956. $arr = [];
  957. $categoryListIds = $insertIDS;
  958. if ($categoryListIds) {
  959. foreach ($categoryListIds as $v) {
  960. $ids = $this->getUnderlingUIds(intval($v));
  961. $ids_arr = explode(",", $ids);
  962. array_push($arr, $ids_arr);
  963. }
  964. }
  965. $mergedArray = [];
  966. foreach ($arr as $subarray) {
  967. $mergedArray = array_merge($mergedArray, $subarray);
  968. }
  969. var_dump("要插入的ID:", $mergedArray);
  970. //查询出所有的分类进行分割插入 组装数据
  971. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  972. $categoryListData = $categoryListData->toArray();
  973. $insertData = [];
  974. if ($categoryListData) {
  975. foreach ($categoryListData as $key => $value) {
  976. $insertData[$key]['website_id'] = $data['website_id'];
  977. $insertData[$key]['name'] = $value['name'];
  978. $insertData[$key]['sort'] = $value['sort'];
  979. $insertData[$key]['pid'] = $value['pid'];
  980. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  981. $insertData[$key]['seo_title'] = $value['seo_title'];
  982. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  983. $insertData[$key]['seo_description'] = $value['seo_description'];
  984. $insertData[$key]['alias'] = $value['name'];
  985. $insertData[$key]['category_id'] = $value['id'];
  986. }
  987. }
  988. WebsiteCategory::insert($insertData);
  989. }
  990. Db::commit();
  991. } catch (\Throwable $ex) {
  992. Db::rollBack();
  993. var_dump($ex->getMessage());
  994. return Result::error("修改失败", 0);
  995. }
  996. return Result::success();
  997. }
  998. /**
  999. * 获取网站列表
  1000. * @param array $data
  1001. * @return array
  1002. */
  1003. public function getWebsiteCategoryList(array $data): array
  1004. {
  1005. $where = [];
  1006. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1007. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1008. }
  1009. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1010. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1011. }
  1012. $result = Website::where($where)
  1013. ->with(["websiteCategory" => function ($query) {
  1014. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1015. }])
  1016. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1017. ->get();
  1018. $count = Website::where($where)->count();
  1019. if (empty($result)) {
  1020. return Result::error("没有数据", 0);
  1021. }
  1022. $data = [
  1023. 'rows' => $result->toArray(),
  1024. 'count' => $count,
  1025. ];
  1026. if ($result) {
  1027. return Result::success($data);
  1028. } else {
  1029. return Result::error("查询失败", 0);
  1030. }
  1031. }
  1032. /**
  1033. * 删除网站下的所有导航
  1034. * @param array $data
  1035. * @return array
  1036. */
  1037. public function delWebsiteAllCategory(array $data): array
  1038. {
  1039. $website_id = $data['website_id'];
  1040. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1041. if ($result) {
  1042. return Result::success($result);
  1043. } else {
  1044. return Result::error("删除失败", 0);
  1045. }
  1046. }
  1047. /**
  1048. * 获取网站下的某一个导航
  1049. * @param array $data
  1050. * @return array
  1051. */
  1052. public function getWebsiteCategoryOnes(array $data): array
  1053. {
  1054. $website_id = $data['website_id'];
  1055. $category_id = $data['category_id'];
  1056. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1057. ->first();
  1058. if ($result) {
  1059. return Result::success($result);
  1060. } else {
  1061. return Result::error("查询失败", 0);
  1062. }
  1063. }
  1064. /**
  1065. * 更新网闸下的某一个导航
  1066. * @param array $data
  1067. * @return array
  1068. */
  1069. public function upWebsiteCategoryones(array $data): array
  1070. {
  1071. $where = [
  1072. 'website_id' => $data['website_id'],
  1073. 'category_id' => $data['category_id'],
  1074. ];
  1075. $result = WebsiteCategory::where($where)->update($data);
  1076. if ($result) {
  1077. return Result::success($result);
  1078. } else {
  1079. return Result::error("更新失败", 0);
  1080. }
  1081. }
  1082. /**
  1083. * 获取网站下的所有导航(包含子导航)
  1084. * @param array $data
  1085. * @return array
  1086. */
  1087. public function getWebsiteAllCategory(array $data): array
  1088. {
  1089. $where = [];
  1090. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1091. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1092. }
  1093. if (isset($data['name']) && !empty($data['name'])) {
  1094. array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1095. }
  1096. if (isset($data['alias']) && !empty($data['alias'])) {
  1097. array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1098. }
  1099. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1100. array_push($where, ['category.department_id', '=', $data['department_id']]);
  1101. }
  1102. if (isset($data['city_id']) && !empty($data['city_id'])) {
  1103. array_push($where, ['category.city_id', '=', $data['city_id']]);
  1104. }
  1105. $result = WebsiteCategory::where($where)
  1106. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1107. ->leftJoin("department", 'category.department_id', 'department.id')
  1108. ->leftJoin("district", 'category.city_id', 'district.id')
  1109. ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1110. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1111. ->get();
  1112. $count = WebsiteCategory::where($where)
  1113. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1114. ->leftJoin("department", 'category.department_id', 'department.id')
  1115. ->leftJoin("district", 'category.city_id', 'district.id')
  1116. ->count();
  1117. if (empty($result)) {
  1118. return Result::error("没有数据", 0);
  1119. }
  1120. $data = [
  1121. 'rows' => $result->toArray(),
  1122. 'count' => $count,
  1123. ];
  1124. if ($result) {
  1125. return Result::success($data);
  1126. } else {
  1127. return Result::error("查询失败", 0);
  1128. }
  1129. if ($result) {
  1130. return Result::success($result);
  1131. } else {
  1132. return Result::error("查询失败", 0);
  1133. }
  1134. }
  1135. /**
  1136. * 递归查询数据
  1137. * @param $id
  1138. * @param $ids
  1139. * @return string
  1140. */
  1141. public function getUnderlingUIds($id, $ids = '')
  1142. {
  1143. $back = Category::where(['pid' => $id])->get();
  1144. $back = $back->toArray();
  1145. if (!empty($back) && is_array($back)) {
  1146. foreach ($back as $v) {
  1147. //防止当前人的ID重复去查询,形成恶性循环
  1148. if ($v['id'] == $id) {
  1149. continue;
  1150. }
  1151. $back2 = Category::where(['pid' => $id])->count('id');
  1152. if ($back2 > 0) {
  1153. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1154. } else {
  1155. $ids .= ',' . $v['id'];
  1156. }
  1157. }
  1158. }
  1159. $ids = $id . ',' . $ids . ',';
  1160. $ids = str_replace(',,', ",", $ids);
  1161. $ids = trim($ids, ',');
  1162. return $ids;
  1163. }
  1164. /**
  1165. * 检测网站名称是否重复
  1166. * @param array $data
  1167. * @return array
  1168. */
  1169. public function checkWebsiteName(array $data): array
  1170. {
  1171. if (isset($data['id'])) {
  1172. $data[] = ['id', "!=", $data['id']];
  1173. unset($data['id']);
  1174. }
  1175. $websiteInfo = Website::query()->where($data)->first();
  1176. if (empty($websiteInfo)) {
  1177. return Result::error("找不到网站", 0);
  1178. }
  1179. return Result::success($websiteInfo->toArray());
  1180. }
  1181. /**
  1182. * 检测网站url是否重复
  1183. * @param array $data
  1184. * @return array
  1185. */
  1186. public function checkWebsiteUrl(array $data): array
  1187. {
  1188. $whereData = [];
  1189. if (isset($data['id'])) {
  1190. $whereData = [['id', "!=", $data['id']]];
  1191. unset($data['id']);
  1192. }
  1193. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1194. if (empty($websiteInfo)) {
  1195. return Result::error("找不到URL", 0);
  1196. }
  1197. return Result::success($websiteInfo->toArray());
  1198. }
  1199. /**
  1200. * 获取网站底部基础信息
  1201. * @param array $data
  1202. * @return array
  1203. */
  1204. public function getWebsiteFootInfo(array $data): array
  1205. {
  1206. if(isset($data['website_id'])){
  1207. $website = Website::query()->where('id',$data['website_id'])->first();
  1208. if (empty($website)) {
  1209. return Result::error("找不到网站",0);
  1210. }
  1211. }else{
  1212. return Result::error("参数错误",0);
  1213. }
  1214. $websiteInfo = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1215. if (empty($websiteInfo)) {
  1216. return Result::error("暂无底部基础信息",0);
  1217. }
  1218. return Result::success($websiteInfo->toArray());
  1219. }
  1220. /**
  1221. * 获取网站底部导航
  1222. * @param array $data
  1223. * @return array
  1224. */
  1225. public function getWebsiteFooterCategory(array $data): array
  1226. {
  1227. if(isset($data['website_id'])){
  1228. $website = Website::query()->where('id',$data['website_id'])->first();
  1229. if (empty($website)) {
  1230. return Result::error("找不到网站",0);
  1231. }
  1232. }else{
  1233. return Result::error("参数错误",0);
  1234. }
  1235. $result = FooterCategory::where('website_id',$data['website_id'])->get();
  1236. if (empty($result)) {
  1237. return Result::error("暂无底部导航",0);
  1238. }
  1239. return Result::success($result->toArray());
  1240. }
  1241. /**
  1242. * 获取网站底部导航列表
  1243. * @param array $data
  1244. * @return array
  1245. */
  1246. public function getWebsiteFooterCategoryList(array $data): array
  1247. {
  1248. if(isset($data['website_id'])){
  1249. $website = Website::query()->where('id',$data['website_id'])->first();
  1250. if (empty($website)) {
  1251. return Result::error("找不到网站",0);
  1252. }
  1253. }else{
  1254. return Result::error("参数错误",0);
  1255. }
  1256. $footercategory = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1257. // '底部导航类型 0:内容型;1:列表型;',
  1258. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1259. return Result::error("底部导航id错误",0);
  1260. }else{
  1261. $query = FooterContent::where('fcat_id',$data['fcat_id']);
  1262. if($query->count() == 0){
  1263. return Result::error("暂无底部导航列表",0);
  1264. }elseif($query->count() == 1){
  1265. $result = $query->first();
  1266. }else{
  1267. $result = $query->get();
  1268. }
  1269. }
  1270. return Result::success($result);
  1271. }
  1272. /**
  1273. * 获取网站底部基础信息
  1274. * @param array $data
  1275. * @return array
  1276. */
  1277. public function getWebsiteFooterCategoryInfo(array $data): array
  1278. {
  1279. if(isset($data['website_id'])){
  1280. $website = Website::query()->where('id',$data['website_id'])->first();
  1281. if (empty($website)) {
  1282. return Result::error("找不到网站",0);
  1283. }
  1284. }else{
  1285. return Result::error("参数错误",0);
  1286. }
  1287. if (isset($data['type']) && $data['type'] == 0) {
  1288. $fcatid = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1289. if (empty($fcatid)) {
  1290. return Result::error("底部导航id错误",0);
  1291. }
  1292. $result = FooterContent::where('fcat_id',$data['fcat_id'])->first();
  1293. }else{
  1294. $result = FooterContent::where('id',$data['fcat_id'])->first();
  1295. }
  1296. if (empty($result)) {
  1297. return Result::error("暂无底部导航内容",0);
  1298. }
  1299. return Result::success($result);
  1300. }
  1301. /**
  1302. * 获取并搜索 网站模板信息
  1303. * @param array $data
  1304. * @return array
  1305. */
  1306. public function getWebsiteintel(array $data): array
  1307. {
  1308. //查询所有网站模板信息
  1309. $query = Website::where('website.status', 1)
  1310. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1311. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1312. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1313. ->select(
  1314. "website_template_info.id",
  1315. "template_class.name",
  1316. "website.id as website_id",
  1317. "website.website_name",
  1318. "website.website_url",
  1319. "website_template_info.action_id",
  1320. "website_template_info.created_at",
  1321. "website_template_info.updated_at",
  1322. "website_template_info.page_type",
  1323. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1324. "template.template_name"
  1325. );
  1326. //若存在条件;则在$query的基础上进行筛选
  1327. if(isset($data['website_name']) && !empty($data['website_name'])){
  1328. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1329. }
  1330. if(isset($data['status']) && !empty($data['status'])){
  1331. $query->where('website_template_info.status', $data['status']);
  1332. }
  1333. $count = $query->count();
  1334. $query->limit($data['pageSize'])
  1335. ->offset(($data['page'] - 1) * $data['pageSize'])
  1336. ->orderBy("website_template_info.updated_at", "desc");
  1337. $rep = $query->get();
  1338. if ($rep->count() == 0) {
  1339. return Result::error("没有查找到相关数据", 0);
  1340. } else {
  1341. $rep->each(function ($item) {
  1342. if (!empty($item->page_type)) {
  1343. $pageTypeArray = json_decode($item->page_type, true);
  1344. if (is_array($pageTypeArray)) {
  1345. $item->page_type = $pageTypeArray;
  1346. }
  1347. }
  1348. });
  1349. $result = [
  1350. "rows" => $rep->toArray(),
  1351. "count" => $count,
  1352. ];
  1353. return Result::success($result);
  1354. }
  1355. // return Result::success($result);
  1356. }
  1357. /**
  1358. * 添加网站基础信息
  1359. * @param array $data
  1360. * @return array
  1361. */
  1362. public function addWebsiteTemplateintel(array $data): array
  1363. {
  1364. $website = Website::where('website.id', $data['website_id'])->first();
  1365. if (empty($website)) {
  1366. return Result::error("请输入正确的网站id!", 0);
  1367. }
  1368. if (empty($website['website_column_id'])) {
  1369. return Result::error("请先关联导航池!", 0);
  1370. }
  1371. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1372. $foot_type = FooterCategory::where('website_id',$data['website_id'])->pluck('type')->toArray();
  1373. if (empty($foot_type)) {
  1374. return Result::error("请先关联底部导航池!", 0);
  1375. } elseif (in_array(1, $foot_type)) {
  1376. $foot_type = 1;
  1377. } else {
  1378. $foot_type = 0;
  1379. }
  1380. $result['foot_type'] = $foot_type;
  1381. // 友情链接类型'1:图片 2:文字 3:底部';
  1382. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1383. $missingTypes = [];
  1384. if (!in_array(1, $types)) {
  1385. $missingTypes[] = "文字链接";
  1386. }
  1387. if (!in_array(2, $types)) {
  1388. $missingTypes[] = "图片链接";
  1389. }
  1390. if (!in_array(3, $types)) {
  1391. $missingTypes[] = "底部链接";
  1392. }
  1393. if (!empty($missingTypes)) {
  1394. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1395. return Result::error($errorMessage, 0);
  1396. }
  1397. if(isset($data['page_type'])){
  1398. // 删除重复元素并重新索引数组
  1399. $data['page_type'] = array_unique($data['page_type']);
  1400. $data['page_type'] = array_values($data['page_type']);
  1401. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1402. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1403. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1404. if (empty($info)) {
  1405. // 将数组转换为 JSON 字符串
  1406. $data['page_type'] = json_encode($data['page_type'])??'';
  1407. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1408. $data['action_id'] = 1;
  1409. try {
  1410. $result = WebsiteTemplateInfo::insertGetId($data);
  1411. } catch (\Exception $e) {
  1412. if ($e->getCode() == 22001) {
  1413. $errorMessage = $e->getMessage();
  1414. return Result::error("请检查表单,某个字段超出了长度限制!");
  1415. } else {
  1416. // 处理其他类型的数据库错误
  1417. return Result::error("插入操作失败: " . $e->getMessage());
  1418. }
  1419. return Result::error("添加失败!", 0);
  1420. }
  1421. } else {
  1422. return Result::error("该网站已经构建过了!", 0);
  1423. }
  1424. } else {
  1425. return Result::error("请先选择首页和底部导航详情页!", 0);
  1426. }
  1427. }
  1428. if (empty($result)) {
  1429. return Result::error("添加失败!", 0);
  1430. } else {
  1431. return Result::success($result);
  1432. }
  1433. }
  1434. /**
  1435. * 获取网站基础信息
  1436. *
  1437. * @return array
  1438. */
  1439. public function getWebsiteTemplateintel(array $data): array
  1440. {
  1441. $result = WebsiteTemplateInfo::where('website_template_info.website_id',$data['website_id'])
  1442. ->leftJoin('footer_category','footer_category.website_id','website_template_info.website_id')
  1443. ->leftJoin('website','website.id','website_template_info.website_id')
  1444. ->select(
  1445. "website_template_info.*",
  1446. "footer_category.type",
  1447. "website.website_name",
  1448. "website.id as website_id"
  1449. )
  1450. ->first();
  1451. if(empty($result['website_name'])){
  1452. return Result::error("请输入正确的网站id!", 0);
  1453. }
  1454. if ($result) {
  1455. $result['page_type'] = json_decode($result['page_type'], true);
  1456. return Result::success($result);
  1457. } else {
  1458. return Result::error("请先添加网站基础信息!", 0);
  1459. }
  1460. }
  1461. /**
  1462. * 修改网站基础信息
  1463. *
  1464. * @return array
  1465. */
  1466. public function upWebsiteTemplateintel(array $data): array
  1467. {
  1468. $web = Website::where('website.id', $data['website_id'])->first();
  1469. if (empty($web)) {
  1470. return Result::error("请输入正确的网站id!", 0);
  1471. }
  1472. $where = ['website_id' => $data['website_id']];
  1473. $result = WebsiteTemplateInfo::where($where)->first();
  1474. if (empty($result)) {
  1475. return Result::error("请先添加网站基础信息!", 0);
  1476. } else {
  1477. unset($data['website_id']);
  1478. // 删除重复元素
  1479. $data['page_type'] = array_unique($data['page_type']);
  1480. // 重新索引数组
  1481. $data['page_type'] = array_values($data['page_type']);
  1482. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1483. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1484. // 将数组转换为 JSON 字符串
  1485. $data['page_type'] = json_encode($data['page_type'])??'';
  1486. try {
  1487. $result = WebsiteTemplateInfo::where($where)->update($data);
  1488. } catch (\Exception $e) {
  1489. if ($e->getCode() == 22001) {
  1490. $errorMessage = $e->getMessage();
  1491. return Result::error("请检查表单,某个字段超出了长度限制!");
  1492. } else {
  1493. // 处理其他类型的数据库错误
  1494. return Result::error("修改操作失败: " . $e->getMessage());
  1495. }
  1496. return Result::error("修改失败!", 0);
  1497. }
  1498. }else{
  1499. return Result::error("请先选择首页和底部导航详情页!",0);
  1500. }
  1501. }
  1502. if (empty($result)) {
  1503. return Result::error("修改失败!", 0);
  1504. } else {
  1505. return Result::success($result);
  1506. }
  1507. }
  1508. /**
  1509. * 获取所有网站风格信息
  1510. *
  1511. * @return array
  1512. */
  1513. public function getAllTemplateClass(array $data): array
  1514. {
  1515. $result = TemplateClass::all();
  1516. if (empty($result)) {
  1517. return Result::error("没有查找到相关数据!", 0);
  1518. } else {
  1519. return Result::success($result);
  1520. }
  1521. }
  1522. /**
  1523. * 获取模板内容
  1524. * @return void
  1525. */
  1526. public function getWebsiteTemplateInfo(array $data): array //丢了
  1527. {
  1528. $wehre = [
  1529. "website_id" =>$data['website_id']
  1530. ];
  1531. $result = WebsiteTemplate::where($wehre)->first();
  1532. if($result){
  1533. return Result::success($result);
  1534. }else{
  1535. return Result::error("没有数据",0);
  1536. }
  1537. }
  1538. /**
  1539. * 搜索并获取网站模板信息
  1540. * @param array $data
  1541. * @return array
  1542. */
  1543. public function getWebsiteTemplateList(array $data): array
  1544. {
  1545. $website = Website::where('website.id',$data['website_id'])->first();
  1546. if(empty($website)){
  1547. return Result::error("请输入正确的网站id!",0);
  1548. }else{
  1549. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1550. if(empty($page_type)){
  1551. return Result::error("此网站还未添加基础信息!",0);
  1552. } else{
  1553. $where = json_decode($page_type['page_type'], true);
  1554. // $where = $data["page_type"];
  1555. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1556. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1557. if(empty($template_class)){
  1558. return Result::error("请输入正确的模板风格id!",0);
  1559. }else{
  1560. // 获取指定风格下的模板
  1561. $rep = Template::where('template_class_id', $data['template_class_id'])
  1562. ->where(function ($query) use ($where) {
  1563. foreach ($where as $value) {
  1564. $query->whereJsonContains('template_img', ['value' => $value]);
  1565. }
  1566. })
  1567. ->limit($data['pageSize'])
  1568. ->offset(($data['page'] - 1) * $data['pageSize'])
  1569. ->orderBy("template.updated_at", "desc")
  1570. ->get();
  1571. $count = Template::where('template_class_id', $data['template_class_id'])
  1572. ->where(function ($query) use ($where) {
  1573. foreach ($where as $value) {
  1574. $query->whereJsonContains('template_img', ['value' => $value]);
  1575. }
  1576. })->count();
  1577. }
  1578. }else{
  1579. //获取所有模板
  1580. $rep = Template::where(function ($query) use ($where) {
  1581. foreach ($where as $value) {
  1582. $query->whereJsonContains('template_img', ['value' => $value]);
  1583. }
  1584. })
  1585. ->limit($data['pageSize'])
  1586. ->offset(($data['page'] - 1) * $data['pageSize'])
  1587. ->orderBy("template.updated_at", "desc")
  1588. ->get();
  1589. $count = Template::where(function ($query) use ($where) {
  1590. foreach ($where as $value) {
  1591. $query->whereJsonContains('template_img', ['value' => $value]);
  1592. }
  1593. })->count();
  1594. }
  1595. if(empty($rep) || $rep->count()==0){
  1596. return Result::error("没有查找到相关数据!",0);
  1597. }else{
  1598. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1599. // $rep->each(function ($item) use ($where) {
  1600. // $template_img = json_decode($item->template_img, true);
  1601. // if(is_array($template_img)){
  1602. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1603. // return isset($img['value']) && in_array($img['value'], $where);
  1604. // });
  1605. // }
  1606. // $item->template_img = $filtered_img;
  1607. // });
  1608. // $result = [
  1609. // "rows" => $rep->toArray(),
  1610. // "count" => $count
  1611. // ];
  1612. $rep->each(function ($item) use ($where) {
  1613. $template_img = json_decode($item->template_img, true);
  1614. if (is_array($template_img)) {
  1615. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1616. return isset($img['value']) && in_array($img['value'], $where);
  1617. });
  1618. // 对过滤后的数组进行重新排序
  1619. $sorted_img = array_values($filtered_img);
  1620. $item->template_img = $sorted_img;
  1621. } else {
  1622. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1623. }
  1624. });
  1625. $result = [
  1626. "rows" => $rep->toArray(),
  1627. "count" => $count,
  1628. ];
  1629. }
  1630. }
  1631. }
  1632. return Result::success($result);
  1633. }
  1634. /**
  1635. * 添加网站模板
  1636. * @param array $data
  1637. */
  1638. public function addWebsiteTemplateclassintel(array $data): array
  1639. {
  1640. $template = Template::where('id', $data['template_id'])->first();
  1641. $web = Website::where('id', $data['website_id'])->first();
  1642. if (empty($template)) {
  1643. return Result::error("请输入正确的模板id", 0);
  1644. }
  1645. if (empty($web)) {
  1646. return Result::error("请输入正确的网站id", 0);
  1647. }
  1648. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1649. if(empty($result)){
  1650. return Result::error("添加失败",0);
  1651. }else{
  1652. return Result::success($result);
  1653. }
  1654. }
  1655. /**
  1656. * 获取网站模板信息
  1657. * @param array $data
  1658. */
  1659. public function getWebsiteTemplateclassintel(array $data): array
  1660. {
  1661. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1662. if (empty($template)) {
  1663. return Result::error("请输入正确的搭建网站id", 0);
  1664. }
  1665. $page_type = json_decode($template['page_type'], true);
  1666. // 获取指定网站下的基础信息和模板及风格信息
  1667. $result = WebsiteTemplateInfo::where([
  1668. ['website_template_info.website_id', $data['website_id']],
  1669. ['template_id', '!=', null],
  1670. ])
  1671. ->leftJoin('template','template.id','website_template_info.template_id')
  1672. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1673. ->where(function ($query) use ($page_type) {
  1674. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1675. foreach ($page_type as $pageType) {
  1676. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1677. }
  1678. })
  1679. ->select(
  1680. 'template.template_name',
  1681. 'template.template_img',
  1682. 'template.id as tid',
  1683. 'template_class.name',
  1684. 'template_class.id as class_id',
  1685. 'website_template_info.page_type',
  1686. 'website_template_info.website_id')
  1687. ->first();
  1688. if(empty($result)){
  1689. return Result::error("没有查找到相关数据",0);
  1690. }
  1691. // return Result::success($result);
  1692. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1693. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1694. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1695. foreach ($template_img as $key => $value) {
  1696. if (!in_array($value['value'], $page_type)) {
  1697. unset($template_img[$key]);
  1698. }
  1699. }
  1700. // return Result::success($result);
  1701. $result['template_img'] = array_values($template_img);
  1702. $result['page_type'] = $page_type;
  1703. // $templateList = $this->getWebsiteTemplateList($data);
  1704. // $count = $templateList['data']['count']??'';
  1705. // $pages = $count / $data['pageSize'];
  1706. // $found = false;
  1707. // while ($pages && !$found) {
  1708. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1709. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1710. // if ($index !== false) {
  1711. // $found = true;
  1712. // $result['page'] = $pages;
  1713. // $pages = 0;
  1714. // // echo "查询到的 template 在第 $page 页";
  1715. // }
  1716. // $pages--;
  1717. // }
  1718. return Result::success($result);
  1719. }
  1720. /**
  1721. * 保存网站模板
  1722. * @param array $data
  1723. * @return array
  1724. */
  1725. public function addWebsiteTemplate(array $data): array
  1726. {
  1727. $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())]);
  1728. if($result){
  1729. return Result::success($result);
  1730. }else{
  1731. return Result::error("创建失败",0);
  1732. }
  1733. }
  1734. /**
  1735. * 预览网站首页模板数据
  1736. * @param array $data
  1737. * @return array
  1738. */
  1739. public function getWebsiteTemplateData(array $data): array
  1740. {
  1741. $wehre = [
  1742. "website_id" =>$data['website_id']
  1743. ];
  1744. $result = WebsiteTemplate::where($wehre)->first();
  1745. if($result){
  1746. $templateData = json_decode($result['template_data'],true);
  1747. // var_dump("数据:",$templateData[1]);
  1748. $templateData[1] = $this->getNewsList($templateData[1]);
  1749. return Result::success($templateData);
  1750. }else{
  1751. return Result::error("没有数据",0);
  1752. }
  1753. }
  1754. /**
  1755. * 获取列表数据
  1756. * @return void
  1757. */
  1758. public function getNewsList(array $data): array
  1759. {
  1760. if($data){
  1761. foreach ($data as $key=>$val){
  1762. if($val){
  1763. foreach ($val['data'] as $k=>$item){
  1764. if($item['isReturn'] && intval($item['isReturn'])==1){
  1765. $imgList = [];
  1766. if($item['data']['imgNum'] && intval($item['data']['imgNum'])>0){
  1767. $imgList = Article::where('imgurl',"!=","")
  1768. ->where('catid',$item['data']['category_id'])
  1769. ->select($item['data']['selectField'])
  1770. ->orderBy("created_at","desc")
  1771. ->offset(0)
  1772. ->limit(intval($item['data']['imgNum']))
  1773. ->get();
  1774. $imgList = $imgList->toArray();
  1775. $listIds = [];
  1776. if ($imgList){
  1777. $listIds = array_column($imgList, 'id');
  1778. }
  1779. $dataList = Article::whereNotIn('id',$listIds)
  1780. ->where('catid',$item['data']['category_id'])
  1781. ->select($item['data']['selectField'])
  1782. ->orderBy("created_at","desc")
  1783. ->offset(0)
  1784. ->limit(intval($item['data']['pageSize'])-intval($item['data']['imgNum']))
  1785. ->get();
  1786. $dataList = $dataList->toArray();
  1787. var_dump("图片列表:",$imgList,"数据列表:",$dataList);
  1788. $datas = array_merge($imgList,$dataList);
  1789. // return $datas;
  1790. var_dump("合并列表:",$datas);
  1791. $data[$key]['data'][$k]['dataList'] = $datas;
  1792. }else{
  1793. $dataList = Article::where('catid',$item['data']['category_id'])
  1794. ->select($item['data']['selectField'])
  1795. ->orderBy("created_at","desc")
  1796. ->offset(0)
  1797. ->limit(intval($item['data']['pageSize']))
  1798. ->get();
  1799. $dataList = $dataList->toArray();
  1800. var_dump("数据列表:",$dataList);
  1801. }
  1802. }
  1803. }
  1804. }
  1805. }
  1806. }
  1807. return $data;
  1808. }
  1809. }