WebsiteService.php 64 KB

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