WebsiteService.php 64 KB

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