WebsiteService.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  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. * @param array $data
  1418. * @return array
  1419. */
  1420. public function getWebsiteTemplateList(array $data): array
  1421. {
  1422. $website = Website::where('website.id',$data['website_id'])->first();
  1423. if(empty($website)){
  1424. return Result::error("请输入正确的网站id!",0);
  1425. }else{
  1426. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1427. if(empty($page_type)){
  1428. return Result::error("此网站还未添加基础信息!",0);
  1429. } else{
  1430. $where = json_decode($page_type['page_type'], true);
  1431. // $where = $data["page_type"];
  1432. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1433. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1434. if(empty($template_class)){
  1435. return Result::error("请输入正确的模板风格id!",0);
  1436. }else{
  1437. // 获取指定风格下的模板
  1438. $rep = Template::where('template_class_id', $data['template_class_id'])
  1439. ->where(function ($query) use ($where) {
  1440. foreach ($where as $value) {
  1441. $query->whereJsonContains('template_img', ['value' => $value]);
  1442. }
  1443. })
  1444. ->limit($data['pageSize'])
  1445. ->offset(($data['page'] - 1) * $data['pageSize'])
  1446. ->orderBy("template.updated_at", "desc")
  1447. ->get();
  1448. $count = Template::where('template_class_id', $data['template_class_id'])
  1449. ->where(function ($query) use ($where) {
  1450. foreach ($where as $value) {
  1451. $query->whereJsonContains('template_img', ['value' => $value]);
  1452. }
  1453. })->count();
  1454. }
  1455. }else{
  1456. //获取所有模板
  1457. $rep = Template::where(function ($query) use ($where) {
  1458. foreach ($where as $value) {
  1459. $query->whereJsonContains('template_img', ['value' => $value]);
  1460. }
  1461. })
  1462. ->limit($data['pageSize'])
  1463. ->offset(($data['page'] - 1) * $data['pageSize'])
  1464. ->orderBy("template.updated_at", "desc")
  1465. ->get();
  1466. $count = Template::where(function ($query) use ($where) {
  1467. foreach ($where as $value) {
  1468. $query->whereJsonContains('template_img', ['value' => $value]);
  1469. }
  1470. })->count();
  1471. }
  1472. if(empty($rep) || $rep->count()==0){
  1473. return Result::error("没有查找到相关数据!",0);
  1474. }else{
  1475. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1476. // $rep->each(function ($item) use ($where) {
  1477. // $template_img = json_decode($item->template_img, true);
  1478. // if(is_array($template_img)){
  1479. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1480. // return isset($img['value']) && in_array($img['value'], $where);
  1481. // });
  1482. // }
  1483. // $item->template_img = $filtered_img;
  1484. // });
  1485. // $result = [
  1486. // "rows" => $rep->toArray(),
  1487. // "count" => $count
  1488. // ];
  1489. $rep->each(function ($item) use ($where) {
  1490. $template_img = json_decode($item->template_img, true);
  1491. if (is_array($template_img)) {
  1492. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1493. return isset($img['value']) && in_array($img['value'], $where);
  1494. });
  1495. // 对过滤后的数组进行重新排序
  1496. $sorted_img = array_values($filtered_img);
  1497. $item->template_img = $sorted_img;
  1498. } else {
  1499. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1500. }
  1501. });
  1502. $result = [
  1503. "rows" => $rep->toArray(),
  1504. "count" => $count,
  1505. ];
  1506. }
  1507. }
  1508. }
  1509. return Result::success($result);
  1510. }
  1511. /**
  1512. * 添加网站模板
  1513. * @param array $data
  1514. */
  1515. public function addWebsiteTemplateclassintel(array $data): array
  1516. {
  1517. $template = Template::where('id', $data['template_id'])->first();
  1518. $web = Website::where('id', $data['website_id'])->first();
  1519. if (empty($template)) {
  1520. return Result::error("请输入正确的模板id", 0);
  1521. }
  1522. if (empty($web)) {
  1523. return Result::error("请输入正确的网站id", 0);
  1524. }
  1525. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1526. if(empty($result)){
  1527. return Result::error("添加失败",0);
  1528. }else{
  1529. return Result::success($result);
  1530. }
  1531. }
  1532. /**
  1533. * 获取网站模板信息
  1534. * @param array $data
  1535. */
  1536. public function getWebsiteTemplateclassintel(array $data): array
  1537. {
  1538. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1539. if (empty($template)) {
  1540. return Result::error("请输入正确的搭建网站id", 0);
  1541. }
  1542. $page_type = json_decode($template['page_type'], true);
  1543. // 获取指定网站下的基础信息和模板及风格信息
  1544. $result = WebsiteTemplateInfo::where([
  1545. ['website_template_info.website_id', $data['website_id']],
  1546. ['template_id', '!=', null],
  1547. ])
  1548. ->leftJoin('template','template.id','website_template_info.template_id')
  1549. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1550. ->where(function ($query) use ($page_type) {
  1551. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1552. foreach ($page_type as $pageType) {
  1553. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1554. }
  1555. })
  1556. ->select(
  1557. 'template.template_name',
  1558. 'template.template_img',
  1559. 'template.id as tid',
  1560. 'template_class.name',
  1561. 'template_class.id as class_id',
  1562. 'website_template_info.page_type',
  1563. 'website_template_info.website_id')
  1564. ->first();
  1565. // return Result::success($result);
  1566. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1567. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1568. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1569. foreach ($template_img as $key => $value) {
  1570. if (!in_array($value['value'], $page_type)) {
  1571. unset($template_img[$key]);
  1572. }
  1573. }
  1574. // return Result::success($result);
  1575. $result['template_img'] = array_values($template_img);
  1576. $result['page_type'] = $page_type;
  1577. // $templateList = $this->getWebsiteTemplateList($data);
  1578. // $count = $templateList['data']['count']??'';
  1579. // $pages = $count / $data['pageSize'];
  1580. // $found = false;
  1581. // while ($pages && !$found) {
  1582. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1583. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1584. // if ($index !== false) {
  1585. // $found = true;
  1586. // $result['page'] = $pages;
  1587. // $pages = 0;
  1588. // // echo "查询到的 template 在第 $page 页";
  1589. // }
  1590. // $pages--;
  1591. // }
  1592. if(empty($result)){
  1593. return Result::error("没有查找到相关数据",0);
  1594. }else{
  1595. return Result::success($result);
  1596. }
  1597. }
  1598. /**
  1599. * 保存网站模板
  1600. * @param array $data
  1601. * @return array
  1602. */
  1603. public function addWebsiteTemplate(array $data): array
  1604. {
  1605. // var_dump("接收参数:",$data['template_data']);
  1606. $result = WebsiteTemplate::updateOrInsert(['website_id'=>$data['website_id']],['template_data'=>json_encode($data['template_data'])]);
  1607. if($result){
  1608. return Result::success($result);
  1609. }else{
  1610. return Result::error("创建失败",0);
  1611. }
  1612. }
  1613. /**
  1614. * 预览网站首页模板数据
  1615. * @param array $data
  1616. * @return array
  1617. */
  1618. public function getWebsiteTemplateData(array $data): array
  1619. {
  1620. $wehre = [
  1621. "website_id" =>$data['website_id']
  1622. ];
  1623. $result = WebsiteTemplate::where($wehre)->first();
  1624. if($result){
  1625. $templateData = json_decode($result['template_data'],true);
  1626. // var_dump("数据:",$templateData[1]);
  1627. $templateData[1] = $this->getNewsList($templateData[1]);
  1628. return Result::success($templateData);
  1629. }else{
  1630. return Result::error("没有数据",0);
  1631. }
  1632. }
  1633. /**
  1634. * 获取列表数据
  1635. * @return void
  1636. */
  1637. public function getNewsList(array $data): array
  1638. {
  1639. if($data){
  1640. foreach ($data as $key=>$val){
  1641. if($val){
  1642. foreach ($val['data'] as $k=>$item){
  1643. if($item['isReturn'] && intval($item['isReturn'])==1){
  1644. $imgList = [];
  1645. if($item['data']['imgNum'] && intval($item['data']['imgNum'])>0){
  1646. $imgList = Article::where('imgurl',"!=","")
  1647. ->where('catid',$item['data']['category_id'])
  1648. ->select($item['data']['selectField'])
  1649. ->orderBy("created_at","desc")
  1650. ->offset(0)
  1651. ->limit(intval($item['data']['imgNum']))
  1652. ->get();
  1653. $imgList = $imgList->toArray();
  1654. $listIds = [];
  1655. if ($imgList){
  1656. $listIds = array_column($imgList, 'id');
  1657. }
  1658. $dataList = Article::whereNotIn('id',$listIds)
  1659. ->where('catid',$item['data']['category_id'])
  1660. ->select($item['data']['selectField'])
  1661. ->orderBy("created_at","desc")
  1662. ->offset(0)
  1663. ->limit(intval($item['data']['pageSize'])-intval($item['data']['imgNum']))
  1664. ->get();
  1665. $dataList = $dataList->toArray();
  1666. var_dump("图片列表:",$imgList,"数据列表:",$dataList);
  1667. $datas = array_merge($imgList,$dataList);
  1668. // return $datas;
  1669. var_dump("合并列表:",$datas);
  1670. $data[$key]['data'][$k]['dataList'] = $datas;
  1671. }else{
  1672. $dataList = Article::where('catid',$item['data']['category_id'])
  1673. ->select($item['data']['selectField'])
  1674. ->orderBy("created_at","desc")
  1675. ->offset(0)
  1676. ->limit(intval($item['data']['pageSize']))
  1677. ->get();
  1678. $dataList = $dataList->toArray();
  1679. var_dump("数据列表:",$dataList);
  1680. }
  1681. }
  1682. }
  1683. }
  1684. }
  1685. }
  1686. return $data;
  1687. }
  1688. }