WebsiteService.php 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\AdSize;
  4. use App\Model\Article;
  5. use App\Model\Ad;
  6. use App\Model\Category;
  7. use App\Model\AdPlace;
  8. use App\Model\Department;
  9. use App\Model\District;
  10. use App\Model\FooterCategory;
  11. use App\Model\FooterContent;
  12. use App\Model\LetterOfComplaint;
  13. use App\Model\Link;
  14. use App\Model\TemplateClass;
  15. use App\Model\Template;
  16. use App\Model\User;
  17. use App\Model\UserInfo;
  18. use App\Model\WebsiteRole;
  19. use App\Model\WebsiteRoleUser;
  20. use App\Model\Website;
  21. use App\Model\WebsiteColumn;
  22. use App\Model\Sector;
  23. use Hyperf\DbConnection\Db;
  24. use Hyperf\RpcServer\Annotation\RpcService;
  25. use App\Tools\Result;
  26. use Carbon\Carbon;
  27. use App\Model\WebsiteCategory;
  28. use App\Model\WebsiteTemplate;
  29. use App\Model\WebsiteTemplateInfo;
  30. use Hamcrest\Arrays\IsArray;
  31. use PhpParser\Node\Stmt\Return_;
  32. use App\Model\WebsiteGroup;
  33. use function PHPUnit\Framework\isNull;
  34. use App\Model\Component;
  35. // use Illuminate\Support\Facades\DB;
  36. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  37. class WebsiteService implements WebsiteServiceInterface
  38. {
  39. /**
  40. * @param string $keyword
  41. * @param int $page
  42. * @param int $pageSize
  43. * @return array
  44. */
  45. public function getWebsitetList(array $data): array
  46. {
  47. // 构建查询
  48. $query = Website::when($data, function ($query) use ($data) {
  49. if (isset($data['keyword']) && !empty($data['keyword'])) {
  50. $query->where('website.website_name', 'like', '%' . trim($data['keyword']) . '%');
  51. }
  52. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  53. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  54. }
  55. if (isset($data['city_id']) && !empty($data['city_id'])) {
  56. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  57. }
  58. });
  59. // 执行查询并获取总数和结果
  60. $queryWithJoins = $query->leftJoin("website_column", "website.website_column_id", "website_column.id")
  61. ->leftJoin("district", "district.id", "website.city_id");
  62. $count = $queryWithJoins->count();
  63. $result = $queryWithJoins->select("website.*", "website_column.column_name", "district.name as city_name")
  64. ->limit($data['pageSize'])
  65. ->offset(($data['page'] - 1) * $data['pageSize'])
  66. ->orderBy("website.updated_at", "desc")
  67. ->get();
  68. if (empty($result)) {
  69. return Result::error("没有数据", 0);
  70. }
  71. $data = [
  72. 'rows' => $result->toArray(),
  73. 'count' => $count,
  74. ];
  75. return Result::success($data);
  76. }
  77. /**
  78. * @param array $data
  79. * @return array
  80. */
  81. public function createWebsite(array $data): array
  82. {
  83. var_dump("网站数据:", $data);
  84. $insertData = [
  85. 'website_name' => $data['website_name'],
  86. 'logo' => $data['logo'] ?? '',
  87. 'website_url' => $data['website_url'] ?? '',
  88. // 'city_id'=>$data['city_id']??0,
  89. 'website_column_id' => $data['website_column_id'],
  90. 'title' => $data['title'] ?? '',
  91. 'keywords' => $data['keywords'] ?? '',
  92. 'description' => $data['description'] ?? '',
  93. 'status' => $data['status'] ?? 0,
  94. 'website_column_arr_id' => $data['website_column_arr_id'],
  95. // 'city_arr_id'=>$data['city_arr_id']??[0],
  96. 'template_id' => $data['template_id'] ?? 0,
  97. ];
  98. $result = Website::insertGetId($insertData);
  99. if (empty($result)) {
  100. return Result::error("创建失败", 0);
  101. } else {
  102. return Result::success(["id" => $result]);
  103. }
  104. }
  105. /**
  106. * @param int $id
  107. * @param array $data
  108. * @return array
  109. */
  110. public function updateWebsite(int $id, array $data): array
  111. {
  112. $insertData = [
  113. 'website_name' => $data['website_name'],
  114. 'logo' => $data['logo'] ?? '',
  115. 'website_url' => $data['website_url'] ?? '',
  116. // 'city_id'=>$data['city_id']??0,
  117. 'website_column_id' => $data['website_column_id'],
  118. 'title' => $data['title'] ?? '',
  119. 'keywords' => $data['keywords'] ?? '',
  120. 'description' => $data['description'] ?? '',
  121. 'status' => $data['status'] ?? 0,
  122. 'website_column_arr_id' => $data['website_column_arr_id'],
  123. // 'city_arr_id'=>$data['city_arr_id']??[0],
  124. 'template_id' => $data['template_id'] ?? 0,
  125. ];
  126. $result = Website::where('id', $id)->update($insertData);
  127. var_dump("更新站点", $result);
  128. if (empty($result)) {
  129. return Result::error("更新失败", 0);
  130. } else {
  131. return Result::success();
  132. }
  133. }
  134. /**
  135. * @param int $id
  136. * @return array
  137. */
  138. public function delWebsite(int $id): array
  139. {
  140. $result = Website::where('id', $id)->delete();
  141. if (empty($result)) {
  142. return Result::error("删除失败", 0);
  143. } else {
  144. return Result::success();
  145. }
  146. }
  147. /**
  148. * @param int $id
  149. * @return array
  150. */
  151. public function getWebsiteInfo(int $id): array
  152. {
  153. $where = [
  154. ['website.id', '=', $id],
  155. ];
  156. $result = Website::where($where)
  157. ->leftJoin("template", "template.id", "website.template_id")
  158. ->select("website.*", "template.template_name", "template.template_img")
  159. ->first();
  160. if (empty($result)) {
  161. return Result::error("数据不存在", 0);
  162. } else {
  163. return Result::success($result->toArray());
  164. }
  165. }
  166. /**
  167. * 查询所有的站点栏目
  168. * @return array
  169. */
  170. public function getWebsiteColumn(array $data): array
  171. {
  172. $result = WebsiteColumn::where($data)->get();
  173. if (empty($result)) {
  174. return Result::error("数据不存在", 0);
  175. } else {
  176. return Result::success($result->toArray());
  177. }
  178. }
  179. /**
  180. * @param string $keyword
  181. * @param int $page
  182. * @param int $pageSize
  183. * @return array
  184. */
  185. public function getWebsiteColumnList(array $data): array
  186. {
  187. $where = [];
  188. if (isset($data['keyword']) && $data['keyword']) {
  189. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  190. }
  191. $result = WebsiteColumn::where($where)
  192. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  193. ->select("website_column.*", "wc.column_name as parent_column_name")
  194. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  195. $count = WebsiteColumn::where($where)->count();
  196. if (empty($result)) {
  197. return Result::error("没有数据", 0);
  198. }
  199. $data = [
  200. 'rows' => $result->toArray(),
  201. 'count' => $count,
  202. ];
  203. return Result::success($data);
  204. }
  205. /**
  206. * @param array $data
  207. * @return array
  208. */
  209. public function createWebsiteColumn(array $data): array
  210. {
  211. $insertData = [
  212. 'column_name' => $data['column_name'],
  213. 'pid' => $data['pid'] ?? '',
  214. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  215. 'sort' => $data['sort'] ?? 0,
  216. 'remark' => $data['remark'] ?? '',
  217. ];
  218. $result = WebsiteColumn::insertGetId($insertData);
  219. if (empty($result)) {
  220. return Result::error("创建失败", 0);
  221. } else {
  222. return Result::success(["id" => $result]);
  223. }
  224. }
  225. /**
  226. * @param int $id
  227. * @param array $data
  228. * @return array
  229. */
  230. public function updateWebsiteColumn(int $id, array $data): array
  231. {
  232. $insertData = [
  233. 'column_name' => $data['column_name'],
  234. 'pid' => $data['pid'] ?? '',
  235. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  236. 'sort' => $data['sort'] ?? 0,
  237. 'remark' => $data['remark'] ?? '',
  238. ];
  239. $result = WebsiteColumn::where('id', $id)->update($insertData);
  240. if (empty($result)) {
  241. return Result::error("更新失败", 0);
  242. } else {
  243. return Result::success();
  244. }
  245. }
  246. /**
  247. * @param int $id
  248. * @return array
  249. */
  250. public function delWebsiteColumn(int $id): array
  251. {
  252. $list = WebsiteColumn::where(['pid' => $id])->get();
  253. if ($list) {
  254. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  255. }
  256. $result = WebsiteColumn::where('id', $id)->delete();
  257. if (empty($result)) {
  258. return Result::error("删除失败", 0);
  259. } else {
  260. return Result::success();
  261. }
  262. }
  263. /**
  264. * @param string $keyword
  265. * @param int $page
  266. * @param int $pageSize
  267. * @return array
  268. */
  269. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  270. {
  271. $where = [
  272. ['role.role_name', 'like', '%' . $keyword . '%'],
  273. ['website_role.website_id', '=', $websiteId],
  274. ];
  275. $result = WebsiteRole::where($where)
  276. ->leftJoin("role", "role.id", "website_role.role_id")
  277. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  278. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  279. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  280. if (empty($result)) {
  281. return Result::error("没有数据", 0);
  282. }
  283. $data = [
  284. 'rows' => $result->toArray(),
  285. 'count' => $count,
  286. ];
  287. return Result::success($data);
  288. }
  289. /**
  290. * @param array $data
  291. * @return array
  292. */
  293. public function createWebsiteRole(array $data): array
  294. {
  295. $insertData = [
  296. 'website_id' => $data['website_id'],
  297. 'role_id' => $data['role_id'] ?? '',
  298. ];
  299. $info = WebsiteRole::where($insertData)->first();
  300. if ($info) {
  301. return Result::error("不能重复添加角色", 0);
  302. }
  303. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  304. $insertData['type'] = $data['type'] ?? '';
  305. $result = WebsiteRole::insertGetId($insertData);
  306. if (empty($result)) {
  307. return Result::error("创建失败", 0);
  308. } else {
  309. return Result::success(["id" => $result]);
  310. }
  311. }
  312. /**
  313. * 暂时用不上
  314. * @param int $id
  315. * @param array $data
  316. * @return array
  317. */
  318. public function updateWebsiteRole(int $id, array $data): array
  319. {
  320. $insertData = [
  321. 'website_id' => $data['website_id'],
  322. 'type' => $data['type'] ?? '',
  323. ];
  324. $result = WebsiteRole::where('id', $id)->update($insertData);
  325. if (empty($result)) {
  326. return Result::error("更新失败", 0);
  327. } else {
  328. return Result::success();
  329. }
  330. }
  331. /**
  332. * @param int $id
  333. * @return array
  334. */
  335. public function delWebsiteRole(int $id): array
  336. {
  337. $result = WebsiteRole::where('id', $id)->delete();
  338. if (empty($result)) {
  339. return Result::error("删除失败", 0);
  340. } else {
  341. return Result::success();
  342. }
  343. }
  344. /**
  345. * @param string $keyword
  346. * @param int $page
  347. * @param int $pageSize
  348. * @return array
  349. */
  350. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  351. {
  352. $where = [
  353. ['website_role_user.website_id', '=', $websiteId],
  354. ['website_role_user.role_id', '=', $roleId],
  355. ];
  356. $count = WebsiteRoleUser::where($where)->count();
  357. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  358. $result = WebsiteRoleUser::where($where)
  359. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  360. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  361. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  362. ->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')
  363. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  364. if (empty($result)) {
  365. return Result::error("没有数据", 0);
  366. }
  367. $data = [
  368. 'rows' => $result->toArray(),
  369. 'count' => $count,
  370. ];
  371. return Result::success($data);
  372. }
  373. /**
  374. * @param array $data
  375. * @return array
  376. */
  377. public function createWebsiteRoleUser(array $data): array
  378. {
  379. $insertData = [
  380. 'website_id' => $data['website_id'],
  381. 'user_id' => $data['user_id'] ?? '',
  382. ];
  383. $info = WebsiteRoleUser::where($insertData)->first();
  384. if ($info) {
  385. return Result::error("不能重复添加角色用户", 0);
  386. }
  387. $insertData['role_id'] = $data['role_id'] ?? '';
  388. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  389. $insertData['type'] = $data['type'] ?? '';
  390. $result = WebsiteRoleUser::insertGetId($insertData);
  391. if (empty($result)) {
  392. return Result::error("创建失败", 0);
  393. } else {
  394. return Result::success(["id" => $result]);
  395. }
  396. }
  397. /**
  398. * @param int $id
  399. * @param array $data
  400. * @return array
  401. */
  402. public function updateWebsiteRoleUser(int $id, array $data): array
  403. {
  404. $insertData = [
  405. 'website_id' => $data['website_id'],
  406. 'type' => $data['type'] ?? '',
  407. 'type_id' => $data['type_id'] ?? '',
  408. 'role_id' => $data['role_id'] ?? '',
  409. ];
  410. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  411. if (empty($result)) {
  412. return Result::error("更新失败", 0);
  413. } else {
  414. return Result::success();
  415. }
  416. }
  417. /**
  418. * @param int $id
  419. * @return array
  420. */
  421. public function delWebsiteRoleUser(int $id): array
  422. {
  423. $result = WebsiteRoleUser::where('id', $id)->delete();
  424. if (empty($result)) {
  425. return Result::error("删除失败", 0);
  426. } else {
  427. return Result::success();
  428. }
  429. }
  430. /**
  431. * 根据域名获取网站 站点id
  432. * @param array $data
  433. * @return array
  434. */
  435. public function getWebsiteId(array $data): array
  436. {
  437. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  438. if (empty($result)) {
  439. return Result::error("查询站点失败", 0);
  440. } else {
  441. return Result::success($result);
  442. }
  443. }
  444. /**
  445. * 查询网站下面的导航
  446. * @param array $data
  447. * @return array
  448. */
  449. public function getWebsiteCategory(array $data): array
  450. {
  451. $where = [
  452. 'website_id' => $data['website_id'],
  453. ];
  454. // 使用递归查询构建树状结构
  455. $category = WebsiteCategory::where($where)
  456. ->with([
  457. 'children' => function ($query) {
  458. $query->orderBy('sort', 'asc');
  459. },
  460. ])
  461. ->orderBy('sort', 'asc')
  462. ->where('pid', 0) // 仅查询顶级节点
  463. ->get()->all();
  464. if (empty($category)) {
  465. return Result::error("查询站点栏目失败", 0);
  466. } else {
  467. $count = WebsiteCategory::where($where)->count();
  468. $result = [
  469. 'rep' => $category,
  470. 'count' => $count,
  471. ];
  472. return Result::success($result);
  473. }
  474. }
  475. /**
  476. * 查询网站的广告
  477. * @param array $data
  478. * @return array
  479. */
  480. public function getWebsiteAdvertisement(array $data): array
  481. {
  482. $where = [
  483. 'website_id' => $data['website_id'],
  484. 'ad_place.status' => 1,
  485. // 'ad.status' => 1,
  486. ];
  487. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  488. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  489. $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
  490. $result = AdPlace::where($where)
  491. ->leftJoin("ad", function ($join) use ($now) {
  492. $join->on("ad.pid", "=", "ad_place.id")
  493. ->where('ad.status', 1)
  494. ->where('ad.fromtime', '<=', $now)
  495. ->where('ad.totime', '>=', $now);
  496. })
  497. ->select("ad_place.*", 'ad.*', "ad_place.id as ad_place_id", "ad.name as ad_name")
  498. ->get()->all();
  499. if (empty($result)) {
  500. return Result::error("此广告位不存在!", 0);
  501. }
  502. } else {
  503. return Result::error("请选择广告位!", 0);
  504. }
  505. return Result::success($result);
  506. }
  507. /**
  508. *
  509. * @param array $data
  510. * @return array
  511. */
  512. /**网站行政职能搜索 */
  513. public function selectWebsiteDepartment(array $data): array
  514. {
  515. $depart = Department::where('pid',0)->orderBy('id','asc')->get();
  516. // if(isset($data['keyword']) && !empty($data['keyword'])){
  517. // $departments= Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  518. // if(empty($departments)){
  519. // $result['message']="未查询到与此相关职能部门";
  520. // }else{
  521. // $count = Department::where('name','like',"%{$data['keyword']}%")->count();
  522. // $m = [
  523. // 'department'=>$depart,
  524. // 'type'=>$departments,
  525. // 'count'=>$count
  526. // ];
  527. // }
  528. // $result['sele'] = $m;
  529. // return Result::success($result['sele']);
  530. // }
  531. $result=$depart;
  532. return Result::success($result);
  533. }
  534. /**
  535. * 搜索地区
  536. * @param array $data
  537. * @return array
  538. */
  539. public function selectWebsiteArea(array $data): array
  540. {
  541. $provinces=District::where('pid',0)->where('status',1)->orderBy('code')->get();
  542. if(isset($data['province'])){
  543. $province=District::where('pid',0)->where('status',1)->where('id',$data['province'])->orderBy('code')->get();
  544. $province=$province->toArray();
  545. if(!empty($province)){
  546. $citys=District::where('pid',$data['province'])->where('status',1)->orderBy('code')->get();
  547. if(!empty($citys) && isset($data['city']) && !empty($data['city'])){
  548. // $province = $province->toArray();
  549. $province_id = [];
  550. foreach ($province as $val) {
  551. array_push($province_id, $val['id']);
  552. }
  553. // var_dump($province_id);
  554. $city=District::whereIn('pid',$province_id)->where('status',1)->where('id',$data['city'])->orderBy('code')->get();
  555. if(!empty($city)){
  556. $city_id=[];
  557. foreach($city as $val){
  558. array_push($city_id,$val['id']);
  559. }
  560. $regions=District::whereIn('pid',$city_id)->where('status',1)->orderBy('code')->get();
  561. $result=[
  562. 'province' => $province,
  563. 'city' => $city,
  564. 'region' => $regions
  565. ];
  566. }else{
  567. return Result::error("未查询到此城市",0);
  568. }
  569. } else {
  570. $result = [
  571. 'province' => $province,
  572. 'city' => $citys,
  573. 'region' => null,
  574. ];
  575. }
  576. } else {
  577. return Result::error("未查询到此省份", 0);
  578. }
  579. } else {
  580. // $keys = array('data');
  581. $result = $provinces;
  582. }
  583. return Result::success($result);
  584. }
  585. /**
  586. * 获取栏目
  587. * @param array $data
  588. * @return array
  589. */
  590. public function getWebsiteModelCategory(array $data): array
  591. {
  592. $website_id = [
  593. 'website_category.website_id' => $data['website_id'],
  594. ];
  595. $placeid = $data['placeid'] - 1;
  596. $pid = [
  597. 'website_category.pid' => $data['pid'],
  598. ];
  599. var_dump("=======", $pid);
  600. $num = $data['num'];
  601. // return Result::success($data);
  602. $result = WebsiteCategory::where($website_id)
  603. ->leftJoin("category", 'website_category.category_id', 'category.id')
  604. ->select('website_category.*', 'category.is_url', 'category.web_url')
  605. ->where($pid)
  606. // ->with(['children' => function ($query) use ($data) {
  607. // $query->where('website_category.website_id', $data['website_id']);
  608. // }])
  609. ->orderBy('website_category.sort')
  610. ->offset($placeid)
  611. ->limit($num)
  612. ->get();
  613. foreach ($result as $k => $v) {
  614. $child[$k]['children_countent'] = WebsiteCategory::where($website_id)
  615. ->where('website_category.pid', $v['category_id'])
  616. ->count();
  617. if (!empty($child[$k]['children_countent'])) {
  618. $result[$k]['children_count'] = $child[$k]['children_countent'];
  619. } else {
  620. $result[$k]['children_count'] = 0;
  621. }
  622. }
  623. // return Result::success($result);
  624. if(!empty($result)){
  625. return Result::success($result);
  626. }else{
  627. return Result::error("本网站暂无栏目",0);
  628. }
  629. }
  630. /**
  631. * 获取友情链接
  632. * @param array $data
  633. * @return array
  634. */
  635. public function selectWebsiteLinks(array $data): array
  636. {
  637. $where = [
  638. 'website_id' => $data['website_id'],
  639. 'status' => 1,
  640. 'type' => $data['type'],
  641. ];
  642. $num = $data['num'];
  643. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  644. if (!empty($result)) {
  645. return Result::success($result);
  646. } else {
  647. return Result::error("本网站暂无此类型友情链接", 0);
  648. }
  649. }
  650. /**
  651. * 网站首页数据统计, 管理员
  652. * @return void
  653. */
  654. public function getAdminIndex(array $data): array
  655. {
  656. var_dump("用户类型:", $data['type_id']);
  657. switch ($data['type_id']) {
  658. case 4:
  659. $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;');
  660. return Result::success($result);
  661. break;
  662. case 10000:
  663. $res = [];
  664. //网站
  665. $res['website']['count'] = 0;
  666. $res['website']['growth_rate'] = 0;
  667. //资讯
  668. $res['article']['count'] = 0;
  669. $res['article']['growth_rate'] = 0;
  670. //导航池
  671. $res['category']['count'] = 0;
  672. $res['category']['growth_rate'] = 0;
  673. //近一月数据
  674. $res['monthArticle'] = [];
  675. //用户类型
  676. $res['userType'] = [];
  677. $res['website']['count'] = Website::where([])->count();
  678. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  679. $res['category']['count'] = Category::where([])->count();
  680. $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;');
  681. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  682. return Result::success($res);
  683. }
  684. return [];
  685. }
  686. /**
  687. * 获取模板类型
  688. * @return void
  689. */
  690. public function getTemplateClass(array $data): array
  691. {
  692. $where = [];
  693. if (isset($data['name']) && $data['name']) {
  694. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  695. }
  696. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  697. if (empty($result)) {
  698. return Result::error("没有模板类型", 0);
  699. } else {
  700. return Result::success($result);
  701. }
  702. }
  703. /**
  704. * 添加模板类型
  705. * @param
  706. * @return void
  707. */
  708. public function addTemplateClass(array $data): array
  709. {
  710. $insertData = [
  711. 'name' => $data['name'],
  712. ];
  713. $result = TemplateClass::insertGetId($insertData);
  714. if (empty($result)) {
  715. return Result::error("创建失败", 0);
  716. } else {
  717. return Result::success(["id" => $result]);
  718. }
  719. }
  720. /**
  721. * 更新模板
  722. * @param array $data
  723. * @return array
  724. */
  725. public function upTemplateClass(array $data): array
  726. {
  727. $where = [
  728. 'id' => $data['id'],
  729. ];
  730. $insertData = [
  731. 'name' => $data['name'],
  732. ];
  733. $result = TemplateClass::where($where)->update($insertData);
  734. if (empty($result)) {
  735. return Result::error("更新失败", 0);
  736. } else {
  737. return Result::success();
  738. }
  739. }
  740. /**
  741. * 删除模板
  742. * @param array $data
  743. * @return array
  744. */
  745. public function delTemplateClass(array $data): array
  746. {
  747. $where = [
  748. 'id' => $data['id'],
  749. ];
  750. $result = TemplateClass::where($where)->delete();
  751. if (empty($result)) {
  752. return Result::error("删除失败", 0);
  753. } else {
  754. return Result::success();
  755. }
  756. }
  757. /**
  758. * 获取分类下的模板
  759. * @param array $data
  760. * @return array
  761. */
  762. public function getTemplate(array $data): array
  763. {
  764. $page = $data['page'];
  765. $pageSize = $data['pageSize'];
  766. $where = [];
  767. if (isset($data['template_class_id']) && $data['template_class_id']) {
  768. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  769. }
  770. $result = Template::where($where)
  771. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  772. $count = Template::where($where)->count();
  773. if (empty($result)) {
  774. return Result::error("没有数据", 0);
  775. }
  776. $data = [
  777. 'rows' => $result->toArray(),
  778. 'count' => $count,
  779. ];
  780. return Result::success($data);
  781. }
  782. /**
  783. * 创建模板
  784. * @param
  785. * @return void
  786. */
  787. public function addTemplate(array $data): array
  788. {
  789. $insertData = [
  790. 'template_name' => $data['template_name'],
  791. 'template_img' => json_encode($data['template_img']),
  792. 'template_class_id' => $data['template_class_id'],
  793. ];
  794. $result = Template::insertGetId($insertData);
  795. if (empty($result)) {
  796. return Result::error("创建模板失败", 0);
  797. } else {
  798. return Result::success(["id" => $result]);
  799. }
  800. }
  801. /**
  802. * 更新模板
  803. * @param array $data
  804. * @return array
  805. */
  806. public function upTemplate(array $data): array
  807. {
  808. $where = [
  809. 'id' => $data['id'],
  810. ];
  811. $insertData = [
  812. 'template_name' => $data['template_name'],
  813. 'template_img' => json_encode($data['template_img']),
  814. 'template_class_id' => $data['template_class_id'],
  815. ];
  816. $result = Template::where($where)->update($insertData);
  817. if (empty($result)) {
  818. return Result::error("更新模板失败", 0);
  819. } else {
  820. return Result::success();
  821. }
  822. }
  823. /**
  824. * 删除模板
  825. * @param array $data
  826. * @return array
  827. */
  828. public function delTemplate(array $data): array
  829. {
  830. $where = [
  831. 'id' => $data['id'],
  832. ];
  833. $result = Template::where($where)->delete();
  834. if (empty($result)) {
  835. return Result::error("删除模板失败", 0);
  836. } else {
  837. return Result::success();
  838. }
  839. }
  840. /**
  841. * 搜索网站
  842. * @param array $data
  843. * @return array
  844. */
  845. public function websiteList(array $data): array
  846. {
  847. $where = [];
  848. if (isset($data['keyword']) && !empty($data['keyword'])) {
  849. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  850. }
  851. $result = Website::where($where)->get();
  852. if ($result) {
  853. return Result::success($result);
  854. } else {
  855. return Result::error("没有网站", 0);
  856. }
  857. }
  858. public function addWebsiteCategory(array $data): array
  859. {
  860. $website_id = $data['website_id'];
  861. $category_arr_id = $data['category_arr_id'];
  862. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  863. $categoryListIds = [];
  864. if ($categoryList) {
  865. foreach ($categoryList->toArray() as $val) {
  866. array_push($categoryListIds, $val['id']);
  867. }
  868. }
  869. $arr = [];
  870. if ($categoryListIds) {
  871. foreach ($categoryListIds as $v) {
  872. $ids = $this->getUnderlingUIds(intval($v));
  873. $ids_arr = explode(",", $ids);
  874. array_push($arr, $ids_arr);
  875. }
  876. }
  877. $mergedArray = [];
  878. foreach ($arr as $subarray) {
  879. $mergedArray = array_merge($mergedArray, $subarray);
  880. }
  881. var_dump("所有:", $arr, $mergedArray);
  882. //查询出所有的分类进行分割插入 组装数据
  883. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  884. $categoryListData = $categoryListData->toArray();
  885. $insertData = [];
  886. if ($categoryListData) {
  887. foreach ($categoryListData as $key => $value) {
  888. $insertData[$key]['website_id'] = $website_id;
  889. $insertData[$key]['name'] = $value['name'];
  890. $insertData[$key]['sort'] = $value['sort'];
  891. $insertData[$key]['pid'] = $value['pid'];
  892. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  893. $insertData[$key]['seo_title'] = $value['seo_title'];
  894. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  895. $insertData[$key]['seo_description'] = $value['seo_description'];
  896. $insertData[$key]['alias'] = $value['name'];
  897. $insertData[$key]['category_id'] = $value['id'];
  898. }
  899. }
  900. $result = WebsiteCategory::insert($insertData);
  901. var_dump("插入数据状态:", $result);
  902. if ($result) {
  903. return Result::success($result);
  904. } else {
  905. return Result::error("创建失败", 0);
  906. }
  907. }
  908. /**
  909. * 删除网站导航
  910. * @param array $data
  911. * @return array
  912. */
  913. public function delWebsiteCategory(array $data): array
  914. {
  915. $website_id = $data['website_id'] ?? 0;
  916. $category_id = $data['category_id'] ?? 0;
  917. $ids = $this->getUnderlingUIds(intval($category_id));
  918. $ids_arr = explode(",", $ids);
  919. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  920. if ($result) {
  921. return Result::success($result);
  922. } else {
  923. return Result::error("删除失败", 0);
  924. }
  925. }
  926. /**
  927. * 获取网站导航
  928. * @param array $data
  929. * @return array
  930. */
  931. public function getAdminWebsiteCategory(array $data): array
  932. {
  933. $where = [
  934. 'website_id' => $data['website_id'],
  935. ];
  936. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  937. $list = [];
  938. if($result){
  939. foreach ($result->toArray() as $val) {
  940. array_push($list, json_decode($val['category_arr_id']));
  941. }
  942. }
  943. if ($result) {
  944. return Result::success($list);
  945. } else {
  946. return Result::error("查询失败", 0);
  947. }
  948. }
  949. /**
  950. * 更新网站导航
  951. * @param array $data
  952. * @return array
  953. */
  954. public function upWebsiteCategory(array $data): array
  955. {
  956. Db::beginTransaction();
  957. try {
  958. if($data['category_arr_id']){
  959. $oldCategoryList = WebsiteCategory::where(['website_id' => $data['website_id']])->get();
  960. if($oldCategoryList){
  961. $oldCategoryList = $oldCategoryList->toArray();
  962. $oldCategoryListIds = [];
  963. foreach ($oldCategoryList as $val) {
  964. array_push($oldCategoryListIds, $val['category_id']);
  965. }
  966. $newCategoryListIds = [];
  967. foreach ($data['category_arr_id'] as $v){
  968. array_push($newCategoryListIds, end($v));
  969. }
  970. $differenceIDS = array_merge(array_diff($oldCategoryListIds, $newCategoryListIds), array_diff($newCategoryListIds, $oldCategoryListIds));
  971. if($differenceIDS){
  972. foreach ($differenceIDS as $v){
  973. WebsiteCategory::where(['website_id' => $data['website_id'], 'category_id' => $v])->delete();
  974. }
  975. }
  976. foreach ($data['category_arr_id'] as $key => $value){
  977. $info = Category::where(['id' => end($value)])->first();
  978. $info = $info->toArray();
  979. // 假设 $data、$value 和 $info 是已经定义好的变量
  980. $attributes = [
  981. 'website_id' => $data['website_id'],
  982. 'category_id' => end($value)
  983. ];
  984. // 先复制一份要更新或创建的数据
  985. $values = [
  986. 'website_id' => $data['website_id'],
  987. 'name' => $info['name']?? '',
  988. 'alias' => $info['name']?? '',
  989. 'sort' => $info['sort']?? 0,
  990. 'pid' => $info['pid']?? 0,
  991. 'pid_arr' => $info['pid_arr']?? json_encode([]),
  992. 'seo_title' => $info['seo_title']?? "",
  993. 'seo_keywords' => $info['seo_keywords']?? "",
  994. 'seo_description' => $info['seo_description']?? "",
  995. 'is_url' => $info['is_url']?? 0,
  996. 'web_url' => $info['web_url']?? '',
  997. 'category_id' => end($value),
  998. 'category_arr_id' => $value? json_encode($value) : json_encode([])
  999. ];
  1000. // 检查数据库中是否已有该记录
  1001. $existingRecord = WebsiteCategory::where($attributes)->first();
  1002. if ($existingRecord && $existingRecord->name) {
  1003. unset($values['name']);
  1004. }
  1005. if ($existingRecord && $existingRecord->sort) {
  1006. unset($values['sort']);
  1007. }
  1008. if ($existingRecord && $existingRecord->seo_title) {
  1009. unset($values['seo_title']);
  1010. }
  1011. if ($existingRecord && $existingRecord->seo_keywords) {
  1012. unset($values['seo_keywords']);
  1013. }
  1014. if ($existingRecord && $existingRecord->seo_description) {
  1015. unset($values['seo_description']);
  1016. }
  1017. if ($existingRecord && $existingRecord->alias) {
  1018. unset($values['alias']);
  1019. }
  1020. // 执行 updateOrCreate 操作
  1021. WebsiteCategory::updateOrCreate($attributes, $values);
  1022. }
  1023. }
  1024. }
  1025. Db::commit();
  1026. return Result::success();
  1027. } catch (\Throwable $ex) {
  1028. Db::rollBack();
  1029. // var_dump($ex->getMessage());
  1030. return Result::error("修改失败".$ex->getMessage(), 0);
  1031. }
  1032. }
  1033. /**
  1034. * 获取网站列表
  1035. * @param array $data
  1036. * @return array
  1037. */
  1038. public function getWebsiteCategoryList(array $data): array
  1039. {
  1040. $where = [];
  1041. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1042. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1043. }
  1044. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1045. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1046. }
  1047. $result = Website::where($where)
  1048. ->with(["websiteCategory" => function ($query) {
  1049. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1050. }])
  1051. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1052. ->get();
  1053. $count = Website::where($where)->count();
  1054. if (empty($result)) {
  1055. return Result::error("没有数据", 0);
  1056. }
  1057. $data = [
  1058. 'rows' => $result->toArray(),
  1059. 'count' => $count,
  1060. ];
  1061. if ($result) {
  1062. return Result::success($data);
  1063. } else {
  1064. return Result::error("查询失败", 0);
  1065. }
  1066. }
  1067. /**
  1068. * 删除网站下的所有导航
  1069. * @param array $data
  1070. * @return array
  1071. */
  1072. public function delWebsiteAllCategory(array $data): array
  1073. {
  1074. $website_id = $data['website_id'];
  1075. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1076. if ($result) {
  1077. return Result::success($result);
  1078. } else {
  1079. return Result::error("删除失败", 0);
  1080. }
  1081. }
  1082. /**
  1083. * 获取网站下的某一个导航
  1084. * @param array $data
  1085. * @return array
  1086. */
  1087. public function getWebsiteCategoryOnes(array $data): array
  1088. {
  1089. $website_id = $data['website_id'];
  1090. $category_id = $data['category_id'];
  1091. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1092. ->first();
  1093. if ($result) {
  1094. return Result::success($result);
  1095. } else {
  1096. return Result::error("查询失败", 0);
  1097. }
  1098. }
  1099. /**
  1100. * 更新网闸下的某一个导航
  1101. * @param array $data
  1102. * @return array
  1103. */
  1104. public function upWebsiteCategoryones(array $data): array
  1105. {
  1106. $where = [
  1107. 'website_id' => $data['website_id'],
  1108. 'category_id' => $data['category_id'],
  1109. ];
  1110. $data['sort'] = $data['sort'] ?? 0;
  1111. $result = WebsiteCategory::where($where)->update($data);
  1112. if ($result) {
  1113. return Result::success($result);
  1114. } else {
  1115. return Result::error("更新失败", 0);
  1116. }
  1117. }
  1118. /**
  1119. * 获取网站下的所有导航(包含子导航)
  1120. * @param array $data
  1121. * @return array
  1122. */
  1123. public function getWebsiteAllCategory(array $data): array
  1124. {
  1125. $where = [];
  1126. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1127. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1128. }
  1129. // if (isset($data['name']) && !empty($data['name'])) {
  1130. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1131. // }
  1132. // if (isset($data['alias']) && !empty($data['alias'])) {
  1133. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1134. // }
  1135. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1136. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1137. // }
  1138. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1139. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1140. // }
  1141. $result = WebsiteCategory::where($where)
  1142. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1143. // ->leftJoin("department", 'category.department_id', 'department.id')
  1144. // ->leftJoin("district", 'category.city_id', 'district.id')
  1145. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1146. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1147. ->orderBy("sort","asc")
  1148. ->get();
  1149. // $count = WebsiteCategory::where($where)
  1150. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1151. // ->leftJoin("department", 'category.department_id', 'department.id')
  1152. // ->leftJoin("district", 'category.city_id', 'district.id')
  1153. // ->count();
  1154. if (empty($result)) {
  1155. return Result::error("没有数据", 0);
  1156. }
  1157. // $data = [
  1158. // 'rows' => $result->toArray(),
  1159. // 'count' => $count,
  1160. // ];
  1161. if ($result) {
  1162. return Result::success( $result->toArray());
  1163. } else {
  1164. return Result::error("查询失败", 0);
  1165. }
  1166. }
  1167. /**
  1168. * 递归查询数据
  1169. * @param $id
  1170. * @param $ids
  1171. * @return string
  1172. */
  1173. public function getUnderlingUIds($id, $ids = '')
  1174. {
  1175. $back = Category::where(['pid' => $id])->get();
  1176. $back = $back->toArray();
  1177. if (!empty($back) && is_array($back)) {
  1178. foreach ($back as $v) {
  1179. //防止当前人的ID重复去查询,形成恶性循环
  1180. if ($v['id'] == $id) {
  1181. continue;
  1182. }
  1183. $back2 = Category::where(['pid' => $id])->count('id');
  1184. if ($back2 > 0) {
  1185. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1186. } else {
  1187. $ids .= ',' . $v['id'];
  1188. }
  1189. }
  1190. }
  1191. $ids = $id . ',' . $ids . ',';
  1192. $ids = str_replace(',,', ",", $ids);
  1193. $ids = trim($ids, ',');
  1194. return $ids;
  1195. }
  1196. /**
  1197. * 检测网站名称是否重复
  1198. * @param array $data
  1199. * @return array
  1200. */
  1201. public function checkWebsiteName(array $data): array
  1202. {
  1203. if (isset($data['id'])) {
  1204. $data[] = ['id', "!=", $data['id']];
  1205. unset($data['id']);
  1206. }
  1207. $websiteInfo = Website::query()->where($data)->first();
  1208. if (empty($websiteInfo)) {
  1209. return Result::error("找不到网站", 0);
  1210. }
  1211. return Result::success($websiteInfo->toArray());
  1212. }
  1213. /**
  1214. * 检测网站url是否重复
  1215. * @param array $data
  1216. * @return array
  1217. */
  1218. public function checkWebsiteUrl(array $data): array
  1219. {
  1220. $whereData = [];
  1221. if (isset($data['id'])) {
  1222. $whereData = [['id', "!=", $data['id']]];
  1223. unset($data['id']);
  1224. }
  1225. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1226. if (empty($websiteInfo)) {
  1227. return Result::error("找不到URL", 0);
  1228. }
  1229. return Result::success($websiteInfo->toArray());
  1230. }
  1231. /**
  1232. * 获取网站底部基础信息
  1233. * @param array $data
  1234. * @return array
  1235. */
  1236. public function getWebsiteFootInfo(array $data): array
  1237. {
  1238. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1239. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1240. if (empty($website)) {
  1241. return Result::error("找不到网站", 0);
  1242. }
  1243. } else {
  1244. return Result::error("参数错误", 0);
  1245. }
  1246. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->where('status', 2)->first();
  1247. $website_head = Website::where('id', $data['website_id'])
  1248. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description')->first();
  1249. if (empty($website_foot)) {
  1250. return Result::error("暂无底部基础信息", 0);
  1251. }
  1252. if (empty($website_head)) {
  1253. return Result::error("暂无头部基础信息", 0);
  1254. }
  1255. $result = [
  1256. 'website_foot' => $website_foot,
  1257. 'website_head' => $website_head,
  1258. ];
  1259. return Result::success($result);
  1260. }
  1261. /**
  1262. * 获取网站底部导航
  1263. * @param array $data
  1264. * @return array
  1265. */
  1266. public function getWebsiteFooterCategory(array $data): array
  1267. {
  1268. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1269. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1270. if (empty($website)) {
  1271. return Result::error("找不到网站", 0);
  1272. }
  1273. } else {
  1274. return Result::error("参数错误", 0);
  1275. }
  1276. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1277. if (empty($result)) {
  1278. return Result::error("暂无底部导航", 0);
  1279. }
  1280. return Result::success($result->toArray());
  1281. }
  1282. /**
  1283. * 获取网站底部导航列表
  1284. * @param array $data
  1285. * @return array
  1286. */
  1287. public function getWebsiteFooterCategoryList(array $data): array
  1288. {
  1289. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1290. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1291. if (empty($website)) {
  1292. return Result::error("找不到网站", 0);
  1293. }
  1294. } else {
  1295. return Result::error("参数错误", 0);
  1296. }
  1297. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1298. // '底部导航类型 0:内容型;1:列表型;',
  1299. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1300. return Result::error("底部导航id错误", 0);
  1301. } else {
  1302. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1303. if ($query->count() == 0) {
  1304. return Result::error("暂无底部导航列表", 0);
  1305. } elseif ($query->count() == 1) {
  1306. $result = $query->first();
  1307. } else {
  1308. $result = $query->get();
  1309. }
  1310. }
  1311. return Result::success($result);
  1312. }
  1313. /**
  1314. * 获取网站底部导航
  1315. * @param array $data
  1316. * @return array
  1317. */
  1318. public function getWebsiteFooterCategoryInfo(array $data): array
  1319. {
  1320. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1321. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1322. if (empty($website)) {
  1323. return Result::error("找不到网站", 0);
  1324. }
  1325. } else {
  1326. return Result::error("参数错误", 0);
  1327. }
  1328. if (isset($data['type']) && $data['type'] == 0) {
  1329. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1330. if (empty($fcatid)) {
  1331. return Result::error("底部导航id错误", 0);
  1332. }
  1333. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1334. } else {
  1335. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1336. }
  1337. if (empty($result)) {
  1338. return Result::error("暂无底部导航内容", 0);
  1339. }
  1340. return Result::success($result);
  1341. }
  1342. /*
  1343. * 搜索网站二级导航
  1344. * @param array $data
  1345. * @return array
  1346. * */
  1347. public function selectWebsiteCategory(array $data): array
  1348. {
  1349. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1350. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1351. if (empty($website)) {
  1352. return Result::error("暂无该网站", 0);
  1353. }
  1354. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1355. // return Result::success($category);
  1356. if (empty($category)) {
  1357. return Result::error("暂无此导航", 0);
  1358. }
  1359. $query = Category::whereIn('id', $category);
  1360. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1361. $cityid = District::where('id', $data['cityid'])->first();
  1362. if (empty($cityid)) {
  1363. return Result::error("暂无此城市", 0);
  1364. } else {
  1365. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1366. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1367. if (empty($city_category)) {
  1368. return Result::error("暂无此城市下的导航", 0);
  1369. }
  1370. $result['catid'] = $city_category;
  1371. $city = 1;
  1372. // var_dump("城市====================",$result);
  1373. }
  1374. }
  1375. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1376. $departmentid = Department::where('id', $data['department_id'])->first();
  1377. if (empty($departmentid)) {
  1378. return Result::error("暂无此部门", 0);
  1379. } else {
  1380. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1381. if (empty($depart_category)) {
  1382. return Result::error("暂无此部门下的导航", 0);
  1383. }
  1384. $result['catid'] = $depart_category;
  1385. // var_dump("职能部门*******************",$result);
  1386. $department = 1;
  1387. }
  1388. }
  1389. if (!empty($city) && !empty($department)) {
  1390. // var_dump("城市和职能部门----------------------",$result);
  1391. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1392. if (empty($sel_category)) {
  1393. return Result::error("暂无此城市和职能部门下的导航", 0);
  1394. }
  1395. $result['catid'] = $sel_category;
  1396. }
  1397. if (empty($result)) {
  1398. return Result::error("暂无导航", 0);
  1399. }
  1400. // department_id
  1401. } else {
  1402. return Result::error("参数错误", 0);
  1403. }
  1404. return Result::success($result);
  1405. }
  1406. /**
  1407. * 获取网站栏目seo
  1408. * @param array $data
  1409. * @return array
  1410. */
  1411. public function getWebsiteCategoryHead(array $data): array
  1412. {
  1413. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1414. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1415. if (empty($website)) {
  1416. return Result::error("找不到网站", 0);
  1417. }
  1418. } else {
  1419. return Result::error("参数错误", 0);
  1420. }
  1421. if (isset($data['catid']) && !empty($data['catid'])) {
  1422. $fcatid = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1423. if (empty($fcatid)) {
  1424. return Result::error("导航id错误", 0);
  1425. }
  1426. $result = Category::where('id', $data['catid'])->first();
  1427. }
  1428. if (empty($result)) {
  1429. return Result::error("暂无导航", 0);
  1430. }
  1431. return Result::success($result);
  1432. }
  1433. /*
  1434. * 获取某个栏目
  1435. * @param array $data
  1436. * @return array
  1437. * */
  1438. public function getOneWebsiteCategory(array $data): array
  1439. {
  1440. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1441. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1442. if (empty($website)) {
  1443. return Result::error("暂无该网站", 0);
  1444. }
  1445. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->select('category_id', 'alias')->first();
  1446. // return Result::success($category);
  1447. if (empty($result)) {
  1448. return Result::error("暂无此导航", 0);
  1449. }
  1450. // department_id
  1451. } else {
  1452. return Result::error("参数错误", 0);
  1453. }
  1454. return Result::success($result);
  1455. }
  1456. //20250212 网站标识
  1457. public function addWebsiteGroup(array $data): array
  1458. {
  1459. //判斷name是不是重复
  1460. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1461. if (!empty($websiteGroupInfo)) {
  1462. return Result::error("网站标识重复", 0);
  1463. }
  1464. //添加信息
  1465. $result = WebsiteGroup::insertGetId($data);
  1466. var_dump($result);
  1467. if (empty($result)) {
  1468. return Result::error("创建失败", 0);
  1469. } else {
  1470. return Result::success($result);
  1471. }
  1472. }
  1473. public function getWebsiteGroupList(array $data): array
  1474. {
  1475. $where = [];
  1476. if (isset($data['name']) && !empty($data['name'])) {
  1477. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  1478. }
  1479. $result = WebsiteGroup::where($where)
  1480. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1481. ->get();
  1482. foreach ($result as $websiteGroup) {
  1483. $webIds = json_decode($websiteGroup->web_ids, true);
  1484. $websites = Website::whereIn('id', $webIds)->get();
  1485. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1486. $websiteGroup->website_names = $websiteNames;
  1487. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  1488. }
  1489. $count = WebsiteGroup::where($where)->count();
  1490. if (empty($result)) {
  1491. return Result::error("没有数据", 0);
  1492. }
  1493. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  1494. }
  1495. public function getWebsiteGroupInfo(array $data): array
  1496. {
  1497. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  1498. $webIds = json_decode($websiteInfo->web_ids, true);
  1499. $websites = Website::whereIn('id', $webIds)->get();
  1500. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1501. $websiteInfo->website_names = $websiteNames;
  1502. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  1503. if (empty($websiteInfo)) {
  1504. return Result::error("找不到URL", 0);
  1505. }
  1506. return Result::success($websiteInfo->toArray());
  1507. }
  1508. public function updateWebsiteGroup(array $data): array
  1509. {
  1510. $where = [
  1511. 'id' => $data['id'],
  1512. ];
  1513. $insertData = [
  1514. 'name' => $data['name'],
  1515. 'web_ids' => $data['web_ids'],
  1516. ];
  1517. //判斷name是不是重复
  1518. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1519. if (!empty($websiteGroupInfo)) {
  1520. return Result::error("网站标识重复", 0);
  1521. }
  1522. $result = WebsiteGroup::where($where)->update($insertData);
  1523. var_dump($result, '------更新');
  1524. if (empty($result)) {
  1525. return Result::error("更新失败", 0);
  1526. } else {
  1527. return Result::success();
  1528. }
  1529. }
  1530. public function deleteWebsiteGroup(array $data): array
  1531. {
  1532. $where = [
  1533. 'id' => $data['id'],
  1534. ];
  1535. //看看user表是不是有这个id
  1536. $userGroup = User::query()->where('sszq', $data['id'])->first();
  1537. if (!empty($userGroup)) {
  1538. return Result::error("有用户在使用该网站标识,不能删除", 0);
  1539. }
  1540. $result = WebsiteGroup::where($where)->delete();
  1541. if (empty($result)) {
  1542. return Result::error("删除失败", 0);
  1543. } else {
  1544. return Result::success();
  1545. }
  1546. }
  1547. public function getWebsiteNavList(array $data): array
  1548. {
  1549. $where = [];
  1550. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1551. array_push($where, ['website_id', '=', $data['website_id']]);
  1552. }
  1553. if (isset($data['pid'])) {
  1554. array_push($where, ['pid', '=', $data['pid']]);
  1555. }
  1556. $list = WebsiteCategory::query()->where($where)->get();
  1557. if (empty($list)) {
  1558. return Result::error("获取失败", 0);
  1559. } else {
  1560. return Result::success($list);
  1561. }
  1562. }
  1563. //20250212 网站标识
  1564. // 自助建站--fr-------------------
  1565. /**
  1566. * 获取并搜索 网站模板信息
  1567. * @param array $data
  1568. * @return array
  1569. */
  1570. public function getWebsiteintel(array $data): array
  1571. {
  1572. //查询所有网站模板信息
  1573. $query = Website::where('website.status', 1)
  1574. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1575. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1576. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1577. ->select(
  1578. "website_template_info.id",
  1579. "template_class.name",
  1580. "website.id as website_id",
  1581. "website.website_name",
  1582. "website.website_url",
  1583. "website_template_info.action_id",
  1584. "website_template_info.created_at",
  1585. "website_template_info.updated_at",
  1586. "website_template_info.page_type",
  1587. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1588. "template.template_name"
  1589. );
  1590. //若存在条件;则在$query的基础上进行筛选
  1591. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1592. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1593. }
  1594. if (isset($data['status']) && !empty($data['status'])) {
  1595. $query->where('website_template_info.status', $data['status']);
  1596. }
  1597. $count = $query->count();
  1598. $query->limit($data['pageSize'])
  1599. ->offset(($data['page'] - 1) * $data['pageSize'])
  1600. ->orderBy("website_template_info.updated_at", "desc");
  1601. $rep = $query->get();
  1602. if ($rep->count() == 0) {
  1603. return Result::error("没有查找到相关数据", 0);
  1604. } else {
  1605. $rep->each(function ($item) {
  1606. if (!empty($item->page_type)) {
  1607. $pageTypeArray = json_decode($item->page_type, true);
  1608. if (is_array($pageTypeArray)) {
  1609. $item->page_type = $pageTypeArray;
  1610. }
  1611. }
  1612. });
  1613. $result = [
  1614. "rows" => $rep->toArray(),
  1615. "count" => $count,
  1616. ];
  1617. return Result::success($result);
  1618. }
  1619. // return Result::success($result);
  1620. }
  1621. /**
  1622. * 添加网站基础信息
  1623. * @param array $data
  1624. * @return array
  1625. */
  1626. public function addWebsiteTemplateintel(array $data): array
  1627. {
  1628. // var_dump("接收参数:",$data['webSiteData']['base']);
  1629. Db::beginTransaction();
  1630. try {
  1631. $websiteTemplateInfo = WebsiteTemplate::where(['website_id' => $data['webSiteData']['base']['websiteId']])->first();
  1632. // var_dump($websiteTemplateInfo);
  1633. //原始数据
  1634. $templateList = $data['webSiteData']['template'];
  1635. if ($websiteTemplateInfo) {
  1636. //模板存在-更新广告
  1637. } else {
  1638. //创建广告
  1639. $adList = $this->getAdlist($templateList);
  1640. if (!empty($adList)) {
  1641. AdPlace::insert($adList);
  1642. }
  1643. }
  1644. // $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())]);
  1645. Db::commit();
  1646. } catch (\Throwable $ex) {
  1647. Db::rollBack();
  1648. var_dump($ex->getMessage());
  1649. return Result::error("创建失败", 0);
  1650. $website = Website::where('website.id', $data['website_id'])->first();
  1651. if (empty($website)) {
  1652. return Result::error("请输入正确的网站id!", 0);
  1653. }
  1654. if (empty($website['website_column_id'])) {
  1655. return Result::error("请先关联导航池!", 0);
  1656. }
  1657. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1658. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1659. if (empty($foot_type)) {
  1660. return Result::error("请先关联底部导航池!", 0);
  1661. } elseif (in_array(1, $foot_type)) {
  1662. $foot_type = 1;
  1663. } else {
  1664. $foot_type = 0;
  1665. }
  1666. $result['foot_type'] = $foot_type;
  1667. // 友情链接类型'1:图片 2:文字 3:底部';
  1668. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1669. $missingTypes = [];
  1670. if (!in_array(1, $types)) {
  1671. $missingTypes[] = "文字链接";
  1672. }
  1673. if (!in_array(2, $types)) {
  1674. $missingTypes[] = "图片链接";
  1675. }
  1676. if (!in_array(3, $types)) {
  1677. $missingTypes[] = "底部链接";
  1678. }
  1679. if (!empty($missingTypes)) {
  1680. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1681. return Result::error($errorMessage, 0);
  1682. }
  1683. if (isset($data['page_type'])) {
  1684. // 删除重复元素并重新索引数组
  1685. $data['page_type'] = array_unique($data['page_type']);
  1686. $data['page_type'] = array_values($data['page_type']);
  1687. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1688. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1689. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1690. if (empty($info)) {
  1691. // 将数组转换为 JSON 字符串
  1692. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1693. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1694. $data['action_id'] = 1;
  1695. try {
  1696. $result = WebsiteTemplateInfo::insertGetId($data);
  1697. } catch (\Exception $e) {
  1698. if ($e->getCode() == 22001) {
  1699. $errorMessage = $e->getMessage();
  1700. return Result::error("请检查表单,某个字段超出了长度限制!");
  1701. } else {
  1702. // 处理其他类型的数据库错误
  1703. return Result::error("插入操作失败: " . $e->getMessage());
  1704. }
  1705. return Result::error("添加失败!", 0);
  1706. }
  1707. } else {
  1708. return Result::error("该网站已经构建过了!", 0);
  1709. }
  1710. } else {
  1711. return Result::error("请先选择首页和底部导航详情页!", 0);
  1712. }
  1713. }
  1714. }
  1715. if (empty($result)) {
  1716. return Result::error("添加失败!", 0);
  1717. } else {
  1718. return Result::success($result);
  1719. }
  1720. return Result::success();
  1721. }
  1722. //获取传过来的广告列表
  1723. public function getAdlist($templateList)
  1724. {
  1725. if ($templateList) {
  1726. $adList = [];
  1727. foreach ($templateList as $val) {
  1728. if ($val) {
  1729. foreach ($val as $v) {
  1730. if ($v['componentList']) {
  1731. foreach ($v['componentList'] as $p) {
  1732. if ($p['component_type'] == 3) {
  1733. // var_dump("ppppppppppppp:",$p);
  1734. $adSizeInfo = AdSize::where(['width' => $p['componentData']['width'], 'height' => $p['componentData']['height']])->first();
  1735. $adSizeInfo = $adSizeInfo->toArray();
  1736. $adData = [
  1737. 'website_id' => $data['webSiteData']['base']['websiteId'],
  1738. 'status' => 1,
  1739. 'name' => $p['componentData']['text'],
  1740. 'ad_id' => $p['componentData']['ad_id'],
  1741. 'ad_size_id' => $adSizeInfo['id'],
  1742. ];
  1743. array_push($adList, $adData);
  1744. }
  1745. }
  1746. }
  1747. }
  1748. }
  1749. }
  1750. if (count($adList) > 0) {
  1751. return $adList;
  1752. }
  1753. }
  1754. }
  1755. /**
  1756. * 获取网站基础信息
  1757. *
  1758. * @return array
  1759. */
  1760. public function getWebsiteTemplateintel(array $data): array
  1761. {
  1762. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])
  1763. ->leftJoin('footer_category', 'footer_category.website_id', 'website_template_info.website_id')
  1764. ->leftJoin('website', 'website.id', 'website_template_info.website_id')
  1765. ->select(
  1766. "website_template_info.*",
  1767. "footer_category.type",
  1768. "website.website_name",
  1769. "website.id as website_id"
  1770. )
  1771. ->first();
  1772. if (empty($result['website_name'])) {
  1773. return Result::error("请输入正确的网站id!", 0);
  1774. }
  1775. if ($result) {
  1776. $result['page_type'] = json_decode($result['page_type'], true);
  1777. return Result::success($result);
  1778. } else {
  1779. return Result::error("请先添加网站基础信息!", 0);
  1780. }
  1781. }
  1782. /**
  1783. * 修改网站基础信息
  1784. *
  1785. * @return array
  1786. */
  1787. public function upWebsiteTemplateintel(array $data): array
  1788. {
  1789. $web = Website::where('website.id', $data['website_id'])->first();
  1790. if (empty($web)) {
  1791. return Result::error("请输入正确的网站id!", 0);
  1792. }
  1793. $where = ['website_id' => $data['website_id']];
  1794. $result = WebsiteTemplateInfo::where($where)->first();
  1795. if (empty($result)) {
  1796. return Result::error("请先添加网站基础信息!", 0);
  1797. } else {
  1798. unset($data['website_id']);
  1799. // 删除重复元素
  1800. $data['page_type'] = array_unique($data['page_type']);
  1801. // 重新索引数组
  1802. $data['page_type'] = array_values($data['page_type']);
  1803. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1804. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1805. // 将数组转换为 JSON 字符串
  1806. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1807. try {
  1808. $result = WebsiteTemplateInfo::where($where)->update($data);
  1809. } catch (\Exception $e) {
  1810. if ($e->getCode() == 22001) {
  1811. $errorMessage = $e->getMessage();
  1812. return Result::error("请检查表单,某个字段超出了长度限制!");
  1813. } else {
  1814. // 处理其他类型的数据库错误
  1815. return Result::error("修改操作失败: " . $e->getMessage());
  1816. }
  1817. return Result::error("修改失败!", 0);
  1818. }
  1819. } else {
  1820. return Result::error("请先选择首页和底部导航详情页!", 0);
  1821. }
  1822. }
  1823. if (empty($result)) {
  1824. return Result::error("修改失败!", 0);
  1825. } else {
  1826. return Result::success($result);
  1827. }
  1828. }
  1829. /**
  1830. * 获取所有网站风格信息
  1831. *
  1832. * @return array
  1833. */
  1834. public function getAllTemplateClass(array $data): array
  1835. {
  1836. $result = TemplateClass::all();
  1837. if (empty($result)) {
  1838. return Result::error("没有查找到相关数据!", 0);
  1839. } else {
  1840. return Result::success($result);
  1841. }
  1842. }
  1843. /**
  1844. * 搜索并获取网站模板信息
  1845. * @param array $data
  1846. * @return array
  1847. */
  1848. public function getWebsiteTemplateList(array $data): array
  1849. {
  1850. $website = Website::where('website.id', $data['website_id'])->first();
  1851. if (empty($website)) {
  1852. return Result::error("请输入正确的网站id!", 0);
  1853. } else {
  1854. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1855. if (empty($page_type)) {
  1856. return Result::error("此网站还未添加基础信息!", 0);
  1857. } else {
  1858. $where = json_decode($page_type['page_type'], true);
  1859. // $where = $data["page_type"];
  1860. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1861. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1862. if (empty($template_class)) {
  1863. return Result::error("请输入正确的模板风格id!", 0);
  1864. } else {
  1865. // 获取指定风格下的模板
  1866. $rep = Template::where('template_class_id', $data['template_class_id'])
  1867. ->where(function ($query) use ($where) {
  1868. foreach ($where as $value) {
  1869. $query->whereJsonContains('template_img', ['value' => $value]);
  1870. }
  1871. })
  1872. ->limit($data['pageSize'])
  1873. ->offset(($data['page'] - 1) * $data['pageSize'])
  1874. ->orderBy("template.updated_at", "desc")
  1875. ->get();
  1876. $count = Template::where('template_class_id', $data['template_class_id'])
  1877. ->where(function ($query) use ($where) {
  1878. foreach ($where as $value) {
  1879. $query->whereJsonContains('template_img', ['value' => $value]);
  1880. }
  1881. })->count();
  1882. }
  1883. } else {
  1884. //获取所有模板
  1885. $rep = Template::where(function ($query) use ($where) {
  1886. foreach ($where as $value) {
  1887. $query->whereJsonContains('template_img', ['value' => $value]);
  1888. }
  1889. })
  1890. ->limit($data['pageSize'])
  1891. ->offset(($data['page'] - 1) * $data['pageSize'])
  1892. ->orderBy("template.updated_at", "desc")
  1893. ->get();
  1894. $count = Template::where(function ($query) use ($where) {
  1895. foreach ($where as $value) {
  1896. $query->whereJsonContains('template_img', ['value' => $value]);
  1897. }
  1898. })->count();
  1899. }
  1900. if (empty($rep) || $rep->count() == 0) {
  1901. return Result::error("没有查找到相关数据!", 0);
  1902. } else {
  1903. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1904. // $rep->each(function ($item) use ($where) {
  1905. // $template_img = json_decode($item->template_img, true);
  1906. // if(is_array($template_img)){
  1907. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1908. // return isset($img['value']) && in_array($img['value'], $where);
  1909. // });
  1910. // }
  1911. // $item->template_img = $filtered_img;
  1912. // });
  1913. // $result = [
  1914. // "rows" => $rep->toArray(),
  1915. // "count" => $count
  1916. // ];
  1917. $rep->each(function ($item) use ($where) {
  1918. $template_img = json_decode($item->template_img, true);
  1919. if (is_array($template_img)) {
  1920. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1921. return isset($img['value']) && in_array($img['value'], $where);
  1922. });
  1923. // 对过滤后的数组进行重新排序
  1924. $sorted_img = array_values($filtered_img);
  1925. $item->template_img = $sorted_img;
  1926. } else {
  1927. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1928. }
  1929. });
  1930. $result = [
  1931. "rows" => $rep->toArray(),
  1932. "count" => $count,
  1933. ];
  1934. }
  1935. }
  1936. }
  1937. return Result::success($result);
  1938. }
  1939. /**
  1940. * 添加网站模板
  1941. * @param array $data
  1942. */
  1943. public function addWebsiteTemplateclassintel(array $data): array
  1944. {
  1945. $template = Template::where('id', $data['template_id'])->first();
  1946. $web = Website::where('id', $data['website_id'])->first();
  1947. if (empty($template)) {
  1948. return Result::error("请输入正确的模板id", 0);
  1949. }
  1950. if (empty($web)) {
  1951. return Result::error("请输入正确的网站id", 0);
  1952. }
  1953. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1954. if (empty($result)) {
  1955. return Result::error("添加失败", 0);
  1956. } else {
  1957. return Result::success($result);
  1958. }
  1959. }
  1960. /**
  1961. * 获取网站模板信息
  1962. * @param array $data
  1963. */
  1964. public function getWebsiteTemplateclassintel(array $data): array
  1965. {
  1966. // return Result::success($data);
  1967. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1968. if (empty($template)) {
  1969. return Result::error("请输入正确的搭建网站id", 0);
  1970. }
  1971. $page_type = json_decode($template['page_type'], true);
  1972. // 获取指定网站下的基础信息和模板及风格信息
  1973. $result = WebsiteTemplateInfo::where([
  1974. ['website_template_info.website_id', $data['website_id']],
  1975. ['template_id', '!=', null],
  1976. ])
  1977. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1978. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1979. ->where(function ($query) use ($page_type) {
  1980. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1981. foreach ($page_type as $pageType) {
  1982. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1983. }
  1984. })
  1985. ->select(
  1986. 'template.template_name',
  1987. 'template.template_img',
  1988. 'template.id as tid',
  1989. 'template_class.name',
  1990. 'template_class.id as class_id',
  1991. 'website_template_info.page_type',
  1992. 'website_template_info.website_id')
  1993. ->first();
  1994. if (empty($result)) {
  1995. return Result::error("没有查找到相关数据", 0);
  1996. }
  1997. // return Result::success($result);
  1998. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1999. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  2000. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  2001. foreach ($template_img as $key => $value) {
  2002. if (!in_array($value['value'], $page_type)) {
  2003. unset($template_img[$key]);
  2004. }
  2005. }
  2006. // return Result::success($result);
  2007. $result['template_img'] = array_values($template_img);
  2008. $result['page_type'] = $page_type;
  2009. // $templateList = $this->getWebsiteTemplateList($data);
  2010. // $count = $templateList['data']['count']??'';
  2011. // $pages = $count / $data['pageSize'];
  2012. // $found = false;
  2013. // while ($pages && !$found) {
  2014. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  2015. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  2016. // if ($index !== false) {
  2017. // $found = true;
  2018. // $result['page'] = $pages;
  2019. // $pages = 0;
  2020. // // echo "查询到的 template 在第 $page 页";
  2021. // }
  2022. // $pages--;
  2023. // }
  2024. return Result::success($result);
  2025. }
  2026. /**
  2027. * 获取网站模板下的板块信息
  2028. * @param array $data
  2029. */
  2030. public function getWebsiteSectorList(array $data): array
  2031. {
  2032. $website = Website::where('id', $data['website_id'])->first();
  2033. if (empty($website)) {
  2034. return Result::error("请输入正确的网站id", 0);
  2035. }
  2036. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2037. if (empty($template['template_id'])) {
  2038. return Result::error("请先添加网站模板", 0);
  2039. }
  2040. $query = Sector::where('template_id', $template['template_id']);
  2041. if ($query->count() == 0) {
  2042. return Result::error("没有查找到相关板块数据", 0);
  2043. } elseif ($query->count() == 1) {
  2044. $sector = $query->first();
  2045. } else {
  2046. $sector = $query->get();
  2047. }
  2048. $result['page_type'] = $template['page_type'];
  2049. $result['tid'] = $template['template_id'];
  2050. $result = [
  2051. "tid" => $template['template_id'],
  2052. "page_type" => $template['page_type'],
  2053. "sector" => $sector,
  2054. ];
  2055. return Result::success($result);
  2056. }
  2057. // 自助建站---------------fr---------------
  2058. // 自助建站------liu-------------
  2059. /**
  2060. * 风格下的板块
  2061. * @return void
  2062. */
  2063. public function templateSectorList(array $data): array
  2064. {
  2065. $where = [
  2066. 'template_id' => $data['template_id'],
  2067. ];
  2068. $result = Sector::where($where)->whereJsonContains("page_type", $data['page_type'])
  2069. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  2070. ->get();
  2071. $count = Sector::where($where)->count();
  2072. if (empty($result)) {
  2073. return Result::error("没有数据", 0);
  2074. }
  2075. $data = [
  2076. 'rows' => $result->toArray(),
  2077. 'count' => $count,
  2078. ];
  2079. if (empty($result)) {
  2080. return Result::error("没有数据", 0);
  2081. }
  2082. return Result::success($data);
  2083. }
  2084. /**
  2085. * 板块下的组件
  2086. * @param array $data
  2087. * @return array
  2088. */
  2089. public function sectorComponentList(array $data): array
  2090. {
  2091. $where = [
  2092. 'template_id' => $data['template_id'],
  2093. 'sector_id' => $data['sector_id'],
  2094. ];
  2095. $result = Component::where($where)
  2096. ->select("*")
  2097. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  2098. ->get();
  2099. $count = Component::where($where)->count();
  2100. if (empty($result)) {
  2101. return Result::error("没有数据", 0);
  2102. }
  2103. $data = [
  2104. 'rows' => $result->toArray(),
  2105. 'count' => $count,
  2106. ];
  2107. if (empty($result)) {
  2108. return Result::error("没有数据", 0);
  2109. }
  2110. return Result::success($data);
  2111. }
  2112. /**
  2113. * 获取模板内容
  2114. * @return void
  2115. */
  2116. public function getWebsiteTemplateInfo(array $data): array//丢了
  2117. {
  2118. $wehre = [
  2119. "website_id" => $data['website_id'],
  2120. ];
  2121. $result = WebsiteTemplate::where($wehre)->first();
  2122. if ($result) {
  2123. return Result::success($result);
  2124. } else {
  2125. return Result::error("没有数据", 0);
  2126. }
  2127. }
  2128. /**
  2129. * 保存网站模板
  2130. * @param array $data
  2131. * @return array
  2132. */
  2133. public function addWebsiteTemplate(array $data): array
  2134. {
  2135. // var_dump("接收参数:",$data['webSiteData']['base']);
  2136. Db::beginTransaction();
  2137. try {
  2138. $websiteTemplateInfo = WebsiteTemplate::where(['website_id' => $data['webSiteData']['base']['websiteId']])->first();
  2139. // var_dump($websiteTemplateInfo);
  2140. //原始数据
  2141. $templateList = $data['webSiteData']['template'];
  2142. if ($websiteTemplateInfo) {
  2143. //模板存在-更新广告
  2144. } else {
  2145. //创建广告
  2146. $adList = $this->getAdlist($templateList);
  2147. if (!empty($adList)) {
  2148. AdPlace::insert($adList);
  2149. }
  2150. }
  2151. // $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())]);
  2152. Db::commit();
  2153. } catch (\Throwable $ex) {
  2154. Db::rollBack();
  2155. var_dump($ex->getMessage());
  2156. return Result::error("创建失败", 0);
  2157. }
  2158. return Result::success();
  2159. }
  2160. /**
  2161. * 预览网站首页模板数据
  2162. * @param array $data
  2163. * @return array
  2164. */
  2165. public function getWebsiteTemplateData(array $data): array
  2166. {
  2167. $wehre = [
  2168. "website_id" => $data['website_id'],
  2169. ];
  2170. $result = WebsiteTemplate::where($wehre)->first();
  2171. if ($result) {
  2172. $templateData = json_decode($result['template_data'], true);
  2173. // var_dump("数据:",$templateData[1]);
  2174. $templateData[1] = $this->getNewsList($templateData[1]);
  2175. return Result::success($templateData);
  2176. } else {
  2177. return Result::error("没有数据", 0);
  2178. }
  2179. }
  2180. /**
  2181. * 获取列表数据
  2182. * @return void
  2183. */
  2184. public function getNewsList(array $data): array
  2185. {
  2186. if ($data) {
  2187. foreach ($data as $key => $val) {
  2188. if ($val) {
  2189. foreach ($val['data'] as $k => $item) {
  2190. if ($item['isReturn'] && intval($item['isReturn']) == 1) {
  2191. $imgList = [];
  2192. if ($item['data']['imgNum'] && intval($item['data']['imgNum']) > 0) {
  2193. $imgList = Article::where('imgurl', "!=", "")
  2194. ->where('catid', $item['data']['category_id'])
  2195. ->select($item['data']['selectField'])
  2196. ->orderBy("created_at", "desc")
  2197. ->offset(0)
  2198. ->limit(intval($item['data']['imgNum']))
  2199. ->get();
  2200. $imgList = $imgList->toArray();
  2201. $listIds = [];
  2202. if ($imgList) {
  2203. $listIds = array_column($imgList, 'id');
  2204. }
  2205. $dataList = Article::whereNotIn('id', $listIds)
  2206. ->where('catid', $item['data']['category_id'])
  2207. ->select($item['data']['selectField'])
  2208. ->orderBy("created_at", "desc")
  2209. ->offset(0)
  2210. ->limit(intval($item['data']['pageSize']) - intval($item['data']['imgNum']))
  2211. ->get();
  2212. $dataList = $dataList->toArray();
  2213. var_dump("图片列表:", $imgList, "数据列表:", $dataList);
  2214. $datas = array_merge($imgList, $dataList);
  2215. // return $datas;
  2216. var_dump("合并列表:", $datas);
  2217. $data[$key]['data'][$k]['dataList'] = $datas;
  2218. } else {
  2219. $dataList = Article::where('catid', $item['data']['category_id'])
  2220. ->select($item['data']['selectField'])
  2221. ->orderBy("created_at", "desc")
  2222. ->offset(0)
  2223. ->limit(intval($item['data']['pageSize']))
  2224. ->get();
  2225. $dataList = $dataList->toArray();
  2226. var_dump("数据列表:", $dataList);
  2227. }
  2228. }
  2229. }
  2230. }
  2231. }
  2232. }
  2233. return $data;
  2234. }
  2235. /**
  2236. * 获取底部导航列表
  2237. * @param array $data
  2238. * @return array
  2239. */
  2240. public function getFooterCategoryList(array $data): array
  2241. {
  2242. $where = [
  2243. "website_id" => $data['website_id'],
  2244. ];
  2245. $result = FooterCategory::where($where)->get();
  2246. if ($result) {
  2247. return Result::success($result);
  2248. } else {
  2249. return Result::error("没有数据");
  2250. }
  2251. }
  2252. //获取底部导航内容列表
  2253. public function getFooterContentList(array $data): array
  2254. {
  2255. $where = [
  2256. "fcat_id" => $data['fcat_id'],
  2257. ];
  2258. switch (intval($data['type'])) {
  2259. case 0:
  2260. $result = FooterContent::where($where)->first();
  2261. break;
  2262. case 1:
  2263. $result = FooterContent::where($where)->get();
  2264. break;
  2265. }
  2266. if ($result) {
  2267. return Result::success($result);
  2268. } else {
  2269. return Result::error("没有数据");
  2270. }
  2271. }
  2272. //获取底部导航内容详情
  2273. public function getFooterContentInfo(array $data): array
  2274. {
  2275. $where = [
  2276. "id" => $data['content_id'],
  2277. ];
  2278. $result = FooterContent::where($where)->first();
  2279. if ($result) {
  2280. return Result::success($result);
  2281. } else {
  2282. return Result::error("没有数据");
  2283. }
  2284. }
  2285. // 自传建站---------------liu------------
  2286. // 20250307 根据网站标识获和导航获取站点
  2287. public function getWebsiteNavPoolSite(array $data): array
  2288. {
  2289. $where = [];
  2290. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2291. array_push($where, ['category_id', '=', $data['category_id']]);
  2292. }
  2293. $list = WebsiteCategory::query()->where($where)
  2294. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2295. ->select('website_category.*', 'website.website_name')
  2296. ->distinct()
  2297. ->get();
  2298. if (empty($list)) {
  2299. return Result::error("获取失败", 0);
  2300. } else {
  2301. return Result::success($list);
  2302. }
  2303. }
  2304. public function getWebsiteNavPool(array $data): array
  2305. {
  2306. // 初始化查询条件数组
  2307. $query = WebsiteCategory::query();
  2308. // 如果传入了 websiteids,则添加到查询条件中
  2309. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2310. $query->whereIn('website_id', $data['websiteids']);
  2311. }
  2312. // 如果传入了 pid,则添加到查询条件中
  2313. if (isset($data['pid'])) {
  2314. $query->where('pid', '=', $data['pid']);
  2315. }
  2316. // 执行查询 唯一性 id
  2317. $list = $query->distinct()->get();
  2318. // 根据查询结果返回相应的结果
  2319. if ($list->isEmpty()) {
  2320. return Result::error("获取失败", 0);
  2321. } else {
  2322. return Result::success($list->toArray());
  2323. }
  2324. }
  2325. // 20250307 根据网站标识获和导航获取站点
  2326. /**
  2327. *
  2328. * @param array $data
  2329. * @return array
  2330. */
  2331. public function getAllCategory(array $data): array
  2332. {
  2333. $result = Category::when($data, function ($query) use ($data) {
  2334. if (isset($data['name']) && !empty($data['name'])) {
  2335. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2336. }
  2337. })->get();
  2338. if (empty($result)) {
  2339. return Result::error("获取失败", 0);
  2340. } else {
  2341. return Result::success($result);
  2342. }
  2343. }
  2344. }