WebsiteService.php 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\Ad;
  5. use App\Model\Category;
  6. use App\Model\AdPlace;
  7. use App\Model\Department;
  8. use App\Model\District;
  9. use App\Model\FooterCategory;
  10. use App\Model\FooterContent;
  11. use App\Model\WebsiteTemplateInfo;
  12. use App\Model\Link;
  13. use App\Model\LetterOfComplaint;
  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\Good;
  23. use App\Model\Book;
  24. use App\Model\JobRecruiting;
  25. use App\Model\JobHunting;
  26. use App\Model\Notice;
  27. use App\Model\Complaint;
  28. use App\Model\Order;
  29. use App\Model\WebsiteImg;
  30. use Hyperf\HttpServer\Contract\RequestInterface;
  31. use Hyperf\DbConnection\Db;
  32. use Hyperf\RpcServer\Annotation\RpcService;
  33. use App\Tools\Result;
  34. use Carbon\Carbon;
  35. use App\Model\WebsiteCategory;
  36. use App\Model\WebsiteGroup;
  37. use App\Model\WebsiteTemplate;
  38. use App\Model\Sector;
  39. use PhpParser\Node\Stmt\Return_;
  40. use function PHPUnit\Framework\isNull;
  41. use Overtrue\Pinyin\Pinyin;
  42. use App\Tools\buildTree;
  43. use App\Model\WhiteRouter;
  44. use App\Model\Size;
  45. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  46. class WebsiteService implements WebsiteServiceInterface
  47. {
  48. /**
  49. * 获取网站列表
  50. * @param string $keyword
  51. * @param int $page
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getWebsitetList(array $data): array
  56. {
  57. // 构建查询
  58. $query = Website::when($data, function ($query) use ($data) {
  59. if (isset($data['keyword']) && !empty($data['keyword'])) {
  60. $query->where('website.website_name', 'like', '%' . trim($data['keyword']) . '%');
  61. }
  62. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  63. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  64. }
  65. if (isset($data['city_id']) && !empty($data['city_id'])) {
  66. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  67. }
  68. });
  69. // 执行查询并获取总数和结果
  70. $queryWithJoins = $query->leftJoin("website_column", "website.website_column_id", "website_column.id")
  71. ->leftJoin("district", "district.id", "website.city_id");
  72. $count = $queryWithJoins->count();
  73. $result = $queryWithJoins->select("website.*", "website_column.column_name", "district.name as city_name")
  74. ->limit($data['pageSize'])
  75. ->offset(($data['page'] - 1) * $data['pageSize'])
  76. ->orderBy("website.updated_at", "desc")
  77. ->get();
  78. if (empty($result)) {
  79. return Result::error("没有数据", 0);
  80. }
  81. $data = [
  82. 'rows' => $result->toArray(),
  83. 'count' => $count,
  84. ];
  85. return Result::success($data);
  86. }
  87. /**
  88. * @param array $data
  89. * @return array
  90. */
  91. public function createWebsite(array $data): array
  92. {
  93. var_dump("网站数据:", $data);
  94. $insertData = [
  95. 'website_name' => $data['website_name'],
  96. 'logo' => $data['logo'] ?? '',
  97. 'website_url' => $data['website_url'] ?? '',
  98. // 'city_id'=>$data['city_id']??0,
  99. 'website_column_id' => $data['website_column_id'],
  100. 'title' => $data['title'] ?? '',
  101. 'keywords' => $data['keywords'] ?? '',
  102. 'description' => $data['description'] ?? '',
  103. 'status' => $data['status'] ?? 1,
  104. 'website_column_arr_id' => $data['website_column_arr_id'],
  105. // 'city_arr_id'=>$data['city_arr_id']??[0],
  106. 'template_id' => $data['template_id'] ?? 0,
  107. 'suffix' => $data['suffix'] ?? "",
  108. ];
  109. $result = Website::insertGetId($insertData);
  110. if (empty($result)) {
  111. return Result::error("创建失败", 0);
  112. } else {
  113. return Result::success(["id" => $result]);
  114. }
  115. }
  116. /**
  117. * @param int $id
  118. * @param array $data
  119. * @return array
  120. */
  121. public function updateWebsite(int $id, array $data): array
  122. {
  123. $insertData = [
  124. 'website_name' => $data['website_name'],
  125. 'logo' => $data['logo'] ?? '',
  126. 'website_url' => $data['website_url'] ?? '',
  127. // 'city_id'=>$data['city_id']??0,
  128. 'website_column_id' => $data['website_column_id'],
  129. 'title' => $data['title'] ?? '',
  130. 'keywords' => $data['keywords'] ?? '',
  131. 'description' => $data['description'] ?? '',
  132. // 'status' => $data['status'] ?? 0,
  133. 'website_column_arr_id' => $data['website_column_arr_id'],
  134. // 'city_arr_id'=>$data['city_arr_id']??[0],
  135. 'template_id' => $data['template_id'] ?? 0,
  136. 'suffix' => $data['suffix'] ?? "",
  137. ];
  138. var_dump("=========:", $insertData);
  139. $result = Website::where('id', $id)->update($insertData);
  140. var_dump("更新站点", $result);
  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 delWebsite(int $id): array
  152. {
  153. $result = Website::where('id', $id)->delete();
  154. if (empty($result)) {
  155. return Result::error("删除失败", 0);
  156. } else {
  157. return Result::success();
  158. }
  159. }
  160. /**
  161. * @param int $id
  162. * @return array
  163. */
  164. public function getWebsiteInfo(int $id): array
  165. {
  166. $where = [
  167. ['website.id', '=', $id],
  168. ];
  169. $result = Website::where($where)
  170. ->leftJoin("template", "template.id", "website.template_id")
  171. ->select("website.*", "template.template_name", "template.template_img")
  172. ->first();
  173. if (empty($result)) {
  174. return Result::error("数据不存在", 0);
  175. } else {
  176. return Result::success($result->toArray());
  177. }
  178. }
  179. /**
  180. * 查询所有的站点栏目
  181. * @return array
  182. */
  183. public function getWebsiteColumn(array $data): array
  184. {
  185. $result = WebsiteColumn::where($data)->get();
  186. if (empty($result)) {
  187. return Result::error("数据不存在", 0);
  188. } else {
  189. return Result::success($result->toArray());
  190. }
  191. }
  192. /**
  193. * @param string $keyword
  194. * @param int $page
  195. * @param int $pageSize
  196. * @return array
  197. */
  198. public function getWebsiteColumnList(array $data): array
  199. {
  200. $where = [];
  201. if (isset($data['keyword']) && $data['keyword']) {
  202. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  203. }
  204. $result = WebsiteColumn::where($where)
  205. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  206. ->select("website_column.*", "wc.column_name as parent_column_name")
  207. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  208. $count = WebsiteColumn::where($where)->count();
  209. if (empty($result)) {
  210. return Result::error("没有数据", 0);
  211. }
  212. $data = [
  213. 'rows' => $result->toArray(),
  214. 'count' => $count,
  215. ];
  216. return Result::success($data);
  217. }
  218. /**
  219. * @param array $data
  220. * @return array
  221. */
  222. public function createWebsiteColumn(array $data): array
  223. {
  224. $insertData = [
  225. 'column_name' => $data['column_name'],
  226. 'pid' => $data['pid'] ?? '',
  227. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  228. 'sort' => $data['sort'] ?? 0,
  229. 'remark' => $data['remark'] ?? '',
  230. ];
  231. $result = WebsiteColumn::insertGetId($insertData);
  232. if (empty($result)) {
  233. return Result::error("创建失败", 0);
  234. } else {
  235. return Result::success(["id" => $result]);
  236. }
  237. }
  238. /**
  239. * @param int $id
  240. * @param array $data
  241. * @return array
  242. */
  243. public function updateWebsiteColumn(int $id, array $data): array
  244. {
  245. $insertData = [
  246. 'column_name' => $data['column_name'],
  247. 'pid' => $data['pid'] ?? '',
  248. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  249. 'sort' => $data['sort'] ?? 0,
  250. 'remark' => $data['remark'] ?? '',
  251. ];
  252. $result = WebsiteColumn::where('id', $id)->update($insertData);
  253. if (empty($result)) {
  254. return Result::error("更新失败", 0);
  255. } else {
  256. return Result::success();
  257. }
  258. }
  259. /**
  260. * @param int $id
  261. * @return array
  262. */
  263. public function delWebsiteColumn(int $id): array
  264. {
  265. $list = WebsiteColumn::where(['pid' => $id])->get();
  266. if ($list) {
  267. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  268. }
  269. $result = WebsiteColumn::where('id', $id)->delete();
  270. if (empty($result)) {
  271. return Result::error("删除失败", 0);
  272. } else {
  273. return Result::success();
  274. }
  275. }
  276. /**
  277. * @param string $keyword
  278. * @param int $page
  279. * @param int $pageSize
  280. * @return array
  281. */
  282. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  283. {
  284. $where = [
  285. ['role.role_name', 'like', '%' . $keyword . '%'],
  286. ['website_role.website_id', '=', $websiteId],
  287. ];
  288. $result = WebsiteRole::where($where)
  289. ->leftJoin("role", "role.id", "website_role.role_id")
  290. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  291. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  292. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  293. if (empty($result)) {
  294. return Result::error("没有数据", 0);
  295. }
  296. $data = [
  297. 'rows' => $result->toArray(),
  298. 'count' => $count,
  299. ];
  300. return Result::success($data);
  301. }
  302. /**
  303. * @param array $data
  304. * @return array
  305. */
  306. public function createWebsiteRole(array $data): array
  307. {
  308. $insertData = [
  309. 'website_id' => $data['website_id'],
  310. 'role_id' => $data['role_id'] ?? '',
  311. ];
  312. $info = WebsiteRole::where($insertData)->first();
  313. if ($info) {
  314. return Result::error("不能重复添加角色", 0);
  315. }
  316. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  317. $insertData['type'] = $data['type'] ?? '';
  318. $result = WebsiteRole::insertGetId($insertData);
  319. if (empty($result)) {
  320. return Result::error("创建失败", 0);
  321. } else {
  322. return Result::success(["id" => $result]);
  323. }
  324. }
  325. /**
  326. * 暂时用不上
  327. * @param int $id
  328. * @param array $data
  329. * @return array
  330. */
  331. public function updateWebsiteRole(int $id, array $data): array
  332. {
  333. $insertData = [
  334. 'website_id' => $data['website_id'],
  335. 'type' => $data['type'] ?? '',
  336. ];
  337. $result = WebsiteRole::where('id', $id)->update($insertData);
  338. if (empty($result)) {
  339. return Result::error("更新失败", 0);
  340. } else {
  341. return Result::success();
  342. }
  343. }
  344. /**
  345. * @param int $id
  346. * @return array
  347. */
  348. public function delWebsiteRole(int $id): array
  349. {
  350. $result = WebsiteRole::where('id', $id)->delete();
  351. if (empty($result)) {
  352. return Result::error("删除失败", 0);
  353. } else {
  354. return Result::success();
  355. }
  356. }
  357. /**
  358. * @param string $keyword
  359. * @param int $page
  360. * @param int $pageSize
  361. * @return array
  362. */
  363. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  364. {
  365. $where = [
  366. ['website_role_user.website_id', '=', $websiteId],
  367. ['website_role_user.role_id', '=', $roleId],
  368. ];
  369. $count = WebsiteRoleUser::where($where)->count();
  370. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  371. $result = WebsiteRoleUser::where($where)
  372. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  373. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  374. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  375. ->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')
  376. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  377. if (empty($result)) {
  378. return Result::error("没有数据", 0);
  379. }
  380. $data = [
  381. 'rows' => $result->toArray(),
  382. 'count' => $count,
  383. ];
  384. return Result::success($data);
  385. }
  386. /**
  387. * @param array $data
  388. * @return array
  389. */
  390. public function createWebsiteRoleUser(array $data): array
  391. {
  392. $insertData = [
  393. 'website_id' => $data['website_id'],
  394. 'user_id' => $data['user_id'] ?? '',
  395. ];
  396. $info = WebsiteRoleUser::where($insertData)->first();
  397. if ($info) {
  398. return Result::error("不能重复添加角色用户", 0);
  399. }
  400. $insertData['role_id'] = $data['role_id'] ?? '';
  401. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  402. $insertData['type'] = $data['type'] ?? '';
  403. $result = WebsiteRoleUser::insertGetId($insertData);
  404. if (empty($result)) {
  405. return Result::error("创建失败", 0);
  406. } else {
  407. return Result::success(["id" => $result]);
  408. }
  409. }
  410. /**
  411. * @param int $id
  412. * @param array $data
  413. * @return array
  414. */
  415. public function updateWebsiteRoleUser(int $id, array $data): array
  416. {
  417. $insertData = [
  418. 'website_id' => $data['website_id'],
  419. 'type' => $data['type'] ?? '',
  420. 'type_id' => $data['type_id'] ?? '',
  421. 'role_id' => $data['role_id'] ?? '',
  422. ];
  423. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  424. if (empty($result)) {
  425. return Result::error("更新失败", 0);
  426. } else {
  427. return Result::success();
  428. }
  429. }
  430. /**
  431. * @param int $id
  432. * @return array
  433. */
  434. public function delWebsiteRoleUser(int $id): array
  435. {
  436. $result = WebsiteRoleUser::where('id', $id)->delete();
  437. if (empty($result)) {
  438. return Result::error("删除失败", 0);
  439. } else {
  440. return Result::success();
  441. }
  442. }
  443. /**
  444. * 根据域名获取网站 站点id
  445. * @param array $data
  446. * @return array
  447. */
  448. public function getWebsiteId(array $data): array
  449. {
  450. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  451. if (empty($result)) {
  452. return Result::error("查询站点失败", 0);
  453. } else {
  454. return Result::success($result);
  455. }
  456. }
  457. /**
  458. * 查询网站下面的导航
  459. * @param array $data
  460. * @return array
  461. */
  462. public function getWebsiteCategory(array $data): array
  463. {
  464. $where = [
  465. 'website_id' => $data['website_id'],
  466. ];
  467. // 使用递归查询构建树状结构
  468. $category = WebsiteCategory::where($where)
  469. ->with([
  470. 'children' => function ($query) {
  471. $query->orderBy('sort', 'asc');
  472. },
  473. ])
  474. ->orderBy('sort', 'asc')
  475. ->where('pid', 0) // 仅查询顶级节点
  476. ->get()->all();
  477. if (empty($category)) {
  478. return Result::error("查询站点栏目失败", 0);
  479. } else {
  480. $count = WebsiteCategory::where($where)->count();
  481. $result = [
  482. 'rep' => $category,
  483. 'count' => $count,
  484. ];
  485. return Result::success($result);
  486. }
  487. }
  488. /**
  489. * 查询网站的广告
  490. * @param array $data
  491. * @return array
  492. */
  493. public function getWebsiteAdvertisement(array $data): array
  494. {
  495. $where = [
  496. 'ad_place.status' => 1,
  497. ];
  498. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  499. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  500. $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
  501. // return Result::success($where);
  502. $result = AdPlace::where($where)
  503. ->leftJoin("ad", function ($join) use ($now) {
  504. $join->on("ad.pid", "=", "ad_place.id")
  505. ->where('ad.status', 1)
  506. ->where('ad.fromtime', '<=', $now)
  507. ->where('ad.totime', '>=', $now);
  508. })
  509. ->select(
  510. 'ad_place.name as place_name',
  511. 'ad_place.thumb',
  512. 'ad_place.ad_tag',
  513. 'ad_place.introduce',
  514. 'ad.name as ad_name',
  515. 'ad.image_src',
  516. 'ad.image_url',
  517. 'ad.image_alt'
  518. )
  519. ->get()->all();
  520. if (empty($result)) {
  521. return Result::error("此广告位不存在!", 0);
  522. }
  523. } else {
  524. return Result::error("请选择广告位!", 0);
  525. }
  526. return Result::success($result);
  527. }
  528. /**
  529. *
  530. * @param array $data
  531. * @return array
  532. */
  533. /**网站行政职能搜索 */
  534. public function selectWebsiteDepartment(array $data): array
  535. {
  536. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  537. if (isset($data['keyword']) && !empty($data['keyword'])) {
  538. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  539. if (empty($departments)) {
  540. $result['message'] = "未查询到与此相关职能部门";
  541. } else {
  542. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  543. $m = [
  544. 'department' => $depart,
  545. 'type' => $departments,
  546. 'count' => $count,
  547. ];
  548. }
  549. $result['sele'] = $m;
  550. return Result::success($result['sele']);
  551. }
  552. $result = $depart;
  553. return Result::success($result);
  554. }
  555. /**
  556. * 搜索地区
  557. * @param array $data
  558. * @return array
  559. */
  560. public function selectWebsiteArea(array $data): array
  561. {
  562. // return Result::success($data);
  563. $pid = $data['pid'] ?? 0;
  564. $result = District::leftJoin('district as district2', 'district.pid', '=', 'district2.id')
  565. ->where('district.pid', $pid)
  566. ->where('district.status', 1)
  567. ->select('district.*', 'district2.name as pid_name')
  568. ->orderBy('district.code')->get();
  569. if (empty($result)) {
  570. return Result::error("未查询到此地区", 0);
  571. } else {
  572. return Result::success($result);
  573. }
  574. }
  575. /**
  576. * 获取栏目
  577. * @param array $data
  578. * @return array
  579. */
  580. public function getWebsiteModelCategory(array $data): array
  581. {
  582. // return Result::success($data);
  583. $website_id = [
  584. 'website_id' => $data['website_id'],
  585. ];
  586. // 初始化 $pid 数组
  587. // $pid = [];
  588. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  589. $pidQuery = WebsiteCategory::where($website_id)
  590. ->where('pid', '!=', 0)
  591. ->orderBy('sort')
  592. ->select('pid', 'category_id', 'alias');
  593. $pid = $pidQuery->pluck('pid');
  594. $pid = array_values(array_unique($pid->toArray()));
  595. // 构建查询语句
  596. $query = WebsiteCategory::where($website_id)
  597. ->where('pid', $data['pid'])
  598. ->offset($data['placeid'])
  599. ->limit($data['num'])
  600. ->orderBy('sort');
  601. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  602. if (!empty($pid)) {
  603. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  604. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  605. } else {
  606. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  607. $query->select('website_category.*', DB::raw('0 as children_count'));
  608. }
  609. // 执行查询
  610. $placeid = $data['placeid'] - 1;
  611. $result = $query->offset($placeid)->limit($data['num'])->get();
  612. if (!empty($result)) {
  613. $pidResults = $pidQuery->get();
  614. if (isset($data['type']) && $data['type'] == 1) {
  615. $result = $result->map(function ($item) use ($pidResults) {
  616. $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
  617. if (!empty($child)) {
  618. return $child;
  619. }
  620. });
  621. // 重置索引,使 key 值从 0 开始
  622. $item->children = $children->values();
  623. return $item;
  624. });
  625. } else {
  626. $pidMap = $pidResults->keyBy('pid');
  627. $result->each(function ($record) use ($pidMap) {
  628. if ($pidMap->has($record->category_id)) {
  629. $pidResult = $pidMap->get($record->category_id);
  630. $record->chilid_id = $pidResult->category_id;
  631. $record->chilid_alias = $pidResult->alias;
  632. }
  633. });
  634. }
  635. return Result::success($result);
  636. } else {
  637. return Result::error("本网站暂无栏目", 0);
  638. }
  639. }
  640. /**
  641. * 获取友情链接
  642. * @param array $data
  643. * @return array
  644. */
  645. public function selectWebsiteLinks(array $data): array
  646. {
  647. $where = [
  648. 'website_id' => $data['website_id'],
  649. 'status' => 1,
  650. 'type' => $data['type'],
  651. ];
  652. $num = $data['num'];
  653. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  654. if (!empty($result)) {
  655. return Result::success($result);
  656. } else {
  657. return Result::error("本网站暂无此类型友情链接", 0);
  658. }
  659. }
  660. /**
  661. * 网站首页数据统计, 管理员
  662. * @return void
  663. */
  664. public function getAdminIndex(array $data): array
  665. {
  666. var_dump("用户类型:", $data['type_id']);
  667. switch ($data['type_id']) {
  668. case 4:
  669. $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;');
  670. $res = [];
  671. //查询已审核文章
  672. $user_id = $data['user_id'];
  673. $article = Article::where('status', 1)
  674. ->where('admin_user_id', $user_id)
  675. ->orderBy('updated_at', 'desc')
  676. ->limit(10)
  677. ->get();
  678. $res['letterOfComplaintList'] = $result;
  679. $res['article'] = $article;
  680. $res['article']['count'] = Article::whereNotIn('status', ['404'])->where('admin_user_id', $user_id)->count();
  681. $res['good']['count'] = Good::where([])->where('user_id', $user_id)->count();
  682. $res['book']['count'] = Book::where([])->where('user_id', $user_id)->count();
  683. $res['jobHunting']['count'] = JobHunting::where([])->where('user_id', $user_id)->count();
  684. $res['jobRecruiting']['count'] = JobRecruiting::where([])->where('user_id', $user_id)->count();
  685. $res['complaint']['count'] = Complaint::where([])->where('user_id', $user_id)->count();
  686. $res['notice']['count'] = Notice::where([])->where('user_id', $user_id)->count();
  687. //待审核数据
  688. $res['apply']['article'] = Article::where(['status' => 0])->where('admin_user_id', $user_id)->count(); //
  689. $res['apply']['good'] = Good::where(['status' => 1])->where('user_id', $user_id)->count();
  690. $res['apply']['book'] = Book::where(['status' => 1])->where('user_id', $user_id)->count();
  691. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->where('user_id', $user_id)->count();
  692. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->where('user_id', $user_id)->count();
  693. $res['apply']['complaint'] = Complaint::where(['status' => 1])->where('user_id', $user_id)->count();
  694. $res['apply']['notice'] = Notice::where(['status' => 1])->where('user_id', $user_id)->count();
  695. return Result::success($res);
  696. case 10000:
  697. $res = [];
  698. //网站
  699. $res['website']['count'] = 0;
  700. $res['website']['growth_rate'] = 0;
  701. //资讯
  702. $res['article']['count'] = 0;
  703. $res['article']['growth_rate'] = 0;
  704. //导航池
  705. $res['category']['count'] = 0;
  706. $res['category']['growth_rate'] = 0;
  707. //商品
  708. $res['good']['count'] = 0;
  709. $res['book']['count'] = 0;
  710. $res['jobHunting']['count'] = 0;
  711. $res['jobRecruiting']['count'] = 0;
  712. //近一月数据
  713. $res['monthArticle'] = [];
  714. //用户类型
  715. $res['userType'] = [];
  716. $res['website']['count'] = Website::where([])->count();
  717. $res['article']['count'] = Article::whereIn('status', [1])->count();
  718. $res['category']['count'] = Category::where([])->count();
  719. $res['good']['count'] = Good::whereIn('status', [2])->count();
  720. $res['book']['count'] = Book::whereIn('status', [2])->count();
  721. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])->count();
  722. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])->count();
  723. $res['complaint']['count'] = Complaint::whereIn('status', [2])->count();
  724. $res['notice']['count'] = Notice::whereIn('status', [2])->count();
  725. // $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;');
  726. $res['monthArticle'] = Db::select(' SELECT DATE(article.created_at) AS date,COUNT(*) AS count ,user.type_id FROM article left JOIN user on article.admin_user_id = user.id WHERE
  727. article.status =1 and article.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(article.created_at),`user`.type_id ORDER BY date ASC');
  728. $res['monthGood'] = Db::select(' SELECT DATE(good.created_at) AS date,COUNT(*) AS count ,user.type_id FROM good left JOIN user on good.user_id = user.id WHERE
  729. good.status =2 and good.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(good.created_at),`user`.type_id ORDER BY date ASC');
  730. $res['monthBook'] = Db::select(' SELECT DATE(book.created_at) AS date,COUNT(*) AS count ,user.type_id FROM book left JOIN user on book.user_id = user.id WHERE book.status =2 and book.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(book.created_at),`user`.type_id ORDER BY date ASC');
  731. $res['monthJobHunting'] = Db::select(' SELECT DATE(job_hunting.created_at) AS date,COUNT(*) AS count ,user.type_id FROM job_hunting left JOIN user on job_hunting.user_id = user.id WHERE job_hunting.status =2 and job_hunting.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(job_hunting.created_at),`user`.type_id ORDER BY date ASC');
  732. $res['monthJobRecruiting'] = Db::select(' SELECT DATE(job_recruiting.created_at) AS date,COUNT(*) AS count ,user.type_id FROM job_recruiting left JOIN user on job_recruiting.user_id = user.id WHERE job_recruiting.status =1 and job_recruiting.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(job_recruiting.created_at),`user`.type_id ORDER BY date ASC');
  733. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  734. //待审核数据
  735. $res['apply']['article'] = Article::where(['status' => 0])->count(); //
  736. $res['apply']['good'] = Good::where(['status' => 1])->count();
  737. $res['apply']['book'] = Book::where(['status' => 1])->count();
  738. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->count();
  739. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->count();
  740. $res['apply']['complaint'] = Complaint::where(['status' => 1])->count();
  741. $res['apply']['notice'] = Notice::where(['status' => 1])->count(); //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束',
  742. $res['apply']['order'] = Order::where(['status' => 6])->count();
  743. return Result::success($res);
  744. case 1:
  745. case 2:
  746. case 3:
  747. $res = [];
  748. //查询已审核文章
  749. $user_id = $data['user_id'];
  750. $article = Article::where('status', 1)
  751. ->where('admin_user_id', $user_id)
  752. ->orderBy('updated_at', 'desc')
  753. ->limit(10)
  754. ->get();
  755. $res['article'] = $article;
  756. $res['website']['count'] = Website::where([])->count();
  757. $res['article']['count'] = Article::whereIn('status', [1])->where('admin_user_id', $user_id)->count();
  758. $res['category']['count'] = Category::where([])->count();
  759. $res['good']['count'] = Good::whereIn('status', [2])
  760. ->where('user_id', $user_id)->count();
  761. $res['book']['count'] = Book::whereIn('status', [2])
  762. ->where('user_id', $user_id)->count();
  763. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])
  764. ->where('user_id', $user_id)->count();
  765. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])
  766. ->where('user_id', $user_id)->count();
  767. $res['complaint']['count'] = Complaint::whereIn('status', [2])
  768. ->where('user_id', $user_id)->count();
  769. $res['notice']['count'] = Notice::whereIn('status', [2])
  770. ->where('user_id', $user_id)->count();
  771. //待审核数据
  772. $res['apply']['article'] = Article::where(['status' => 0])->where('admin_user_id', $user_id)->count(); //
  773. $res['apply']['good'] = Good::where(['status' => 1])->where('user_id', $user_id)->count();
  774. $res['apply']['book'] = Book::where(['status' => 1])->where('user_id', $user_id)->count();
  775. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->where('user_id', $user_id)->count();
  776. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->where('user_id', $user_id)->count();
  777. $res['apply']['complaint'] = Complaint::where(['status' => 1])->where('user_id', $user_id)->count();
  778. $res['apply']['notice'] = Notice::where(['status' => 1])->where('user_id', $user_id)->count();
  779. return Result::success($res);
  780. }
  781. return [];
  782. }
  783. /**
  784. * 获取风格
  785. * @return void
  786. */
  787. public function getTemplateClass(array $data): array
  788. {
  789. // return Result::success($data);
  790. $where = [];
  791. if (isset($data['name']) && $data['name']) {
  792. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  793. }
  794. $template = TemplateClass::when($where, function ($query) use ($where) {
  795. $query->where($where);
  796. })
  797. ->orderBy('updated_at', 'desc');
  798. $count = $template->count();
  799. $row = $template
  800. ->offset(($data['page'] - 1) * $data['pageSize'])
  801. ->limit($data['pageSize'])
  802. ->get();
  803. $result = [
  804. 'rows' => $row,
  805. 'count' => $count,
  806. ];
  807. if (empty($result)) {
  808. return Result::error("暂无风格", 0);
  809. } else {
  810. return Result::success($result);
  811. }
  812. }
  813. /**
  814. * 添加风格
  815. * @param
  816. * @return void
  817. */
  818. public function addTemplateClass(array $data): array
  819. {
  820. $data['keyword'] = json_encode($data['keyword']);
  821. $template = TemplateClass::where(['name' => $data['name']])->first();
  822. if ($template) {
  823. return Result::error("风格名称已存在", 0);
  824. }
  825. // return Result::success($data);
  826. $result = TemplateClass::insertGetId($data);
  827. if (empty($result)) {
  828. return Result::error("创建风格失败", 0);
  829. } else {
  830. return Result::success(["id" => $result]);
  831. }
  832. }
  833. /**
  834. * 更新风格
  835. * @param array $data
  836. * @return array
  837. */
  838. public function upTemplateClass(array $data): array
  839. {
  840. $where = [
  841. 'id' => $data['id'],
  842. ];
  843. $template = TemplateClass::where($where)->first();
  844. if (empty($template)) {
  845. return Result::error("未查询到风格", 0);
  846. }
  847. if ($template->type == 1) {
  848. return Result::error("默认风格不能修改", 0);
  849. }
  850. $template = TemplateClass::where(['name' => $data['name']])->where('id', '!=', 'id')->first();
  851. if ($template) {
  852. return Result::error("风格名称已存在", 0);
  853. }
  854. $updateData = [
  855. 'name' => $data['name'],
  856. 'keyword' => json_encode($data['keyword']),
  857. ];
  858. $result = TemplateClass::where($where)->update($updateData);
  859. if (empty($result)) {
  860. return Result::error("更新失败", 0);
  861. } else {
  862. return Result::success($result);
  863. }
  864. }
  865. /**
  866. * 删除风格
  867. * @param array $data
  868. * @return array
  869. */
  870. public function delTemplateClass(array $data): array
  871. {
  872. $where = [
  873. 'id' => $data['id'],
  874. ];
  875. $template = TemplateClass::where($where)->first();
  876. if (empty($template)) {
  877. return Result::error("未查询到风格", 0);
  878. }
  879. if ($template->type == 1) {
  880. return Result::error("默认风格不能删除", 0);
  881. }
  882. $result = TemplateClass::where($where)->delete();
  883. if (empty($result)) {
  884. return Result::error("删除失败", 0);
  885. } else {
  886. return Result::success($result);
  887. }
  888. }
  889. /**
  890. * 获取分类下的模板
  891. * @param array $data
  892. * @return array
  893. */
  894. public function getTemplate(array $data): array
  895. {
  896. $page = $data['page'];
  897. $pageSize = $data['pageSize'];
  898. $where = [];
  899. if (isset($data['template_class_id']) && $data['template_class_id']) {
  900. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  901. }
  902. $result = Template::where($where)
  903. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  904. $count = Template::where($where)->count();
  905. if (empty($result)) {
  906. return Result::error("没有数据", 0);
  907. }
  908. $data = [
  909. 'rows' => $result->toArray(),
  910. 'count' => $count,
  911. ];
  912. return Result::success($data);
  913. }
  914. /**
  915. * 创建模板
  916. * @param
  917. * @return void
  918. */
  919. public function addTemplate(array $data): array
  920. {
  921. $insertData = [
  922. 'template_name' => $data['template_name'],
  923. 'template_img' => json_encode($data['template_img']),
  924. 'template_class_id' => $data['template_class_id'],
  925. ];
  926. $result = Template::insertGetId($insertData);
  927. if (empty($result)) {
  928. return Result::error("创建模板失败", 0);
  929. } else {
  930. return Result::success(["id" => $result]);
  931. }
  932. }
  933. /**
  934. * 更新模板
  935. * @param array $data
  936. * @return array
  937. */
  938. public function upTemplate(array $data): array
  939. {
  940. $where = [
  941. 'id' => $data['id'],
  942. ];
  943. $insertData = [
  944. 'template_name' => $data['template_name'],
  945. 'template_img' => json_encode($data['template_img']),
  946. 'template_class_id' => $data['template_class_id'],
  947. ];
  948. $result = Template::where($where)->update($insertData);
  949. if (empty($result)) {
  950. return Result::error("更新模板失败", 0);
  951. } else {
  952. return Result::success();
  953. }
  954. }
  955. /**
  956. * 删除模板
  957. * @param array $data
  958. * @return array
  959. */
  960. public function delTemplate(array $data): array
  961. {
  962. $where = [
  963. 'id' => $data['id'],
  964. ];
  965. $result = Template::where($where)->delete();
  966. if (empty($result)) {
  967. return Result::error("删除模板失败", 0);
  968. } else {
  969. return Result::success();
  970. }
  971. }
  972. /**
  973. * 搜索网站
  974. * @param array $data
  975. * @return array
  976. */
  977. public function websiteList(array $data): array
  978. {
  979. $where = [];
  980. if (isset($data['keyword']) && !empty($data['keyword'])) {
  981. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  982. }
  983. $result = Website::where($where)->get();
  984. if ($result) {
  985. return Result::success($result);
  986. } else {
  987. return Result::error("没有网站", 0);
  988. }
  989. }
  990. public function addWebsiteCategory(array $data): array
  991. {
  992. $website_id = $data['website_id'];
  993. $category_arr_id = $data['category_arr_id'];
  994. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  995. $categoryListIds = [];
  996. if ($categoryList) {
  997. foreach ($categoryList->toArray() as $val) {
  998. array_push($categoryListIds, $val['id']);
  999. }
  1000. }
  1001. $arr = [];
  1002. if ($categoryListIds) {
  1003. foreach ($categoryListIds as $v) {
  1004. $ids = $this->getUnderlingUIds(intval($v));
  1005. $ids_arr = explode(",", $ids);
  1006. array_push($arr, $ids_arr);
  1007. }
  1008. }
  1009. $mergedArray = [];
  1010. foreach ($arr as $subarray) {
  1011. $mergedArray = array_merge($mergedArray, $subarray);
  1012. }
  1013. var_dump("所有:", $arr, $mergedArray);
  1014. //查询出所有的分类进行分割插入 组装数据
  1015. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  1016. $categoryListData = $categoryListData->toArray();
  1017. $insertData = [];
  1018. if ($categoryListData) {
  1019. foreach ($categoryListData as $key => $value) {
  1020. $insertData[$key]['website_id'] = $website_id;
  1021. $insertData[$key]['name'] = $value['name'];
  1022. $insertData[$key]['sort'] = $value['sort'];
  1023. $insertData[$key]['pid'] = $value['pid'];
  1024. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  1025. $insertData[$key]['seo_title'] = $value['seo_title'];
  1026. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  1027. $insertData[$key]['seo_description'] = $value['seo_description'];
  1028. $insertData[$key]['alias'] = $value['name'];
  1029. $insertData[$key]['category_id'] = $value['id'];
  1030. }
  1031. }
  1032. $result = WebsiteCategory::insert($insertData);
  1033. var_dump("插入数据状态:", $result);
  1034. if ($result) {
  1035. return Result::success($result);
  1036. } else {
  1037. return Result::error("创建失败", 0);
  1038. }
  1039. }
  1040. /**
  1041. * 删除网站导航
  1042. * @param array $data
  1043. * @return array
  1044. */
  1045. public function delWebsiteCategory(array $data): array
  1046. {
  1047. $website_id = $data['website_id'] ?? 0;
  1048. $category_id = $data['category_id'] ?? 0;
  1049. $ids = $this->getUnderlingUIds(intval($category_id));
  1050. $ids_arr = explode(",", $ids);
  1051. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  1052. if ($result) {
  1053. return Result::success($result);
  1054. } else {
  1055. return Result::error("删除失败", 0);
  1056. }
  1057. }
  1058. /**
  1059. * 获取网站导航
  1060. * @param array $data
  1061. * @return array
  1062. */
  1063. public function getAdminWebsiteCategory(array $data): array
  1064. {
  1065. $where = [
  1066. 'website_id' => $data['website_id'],
  1067. ];
  1068. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  1069. $list = [];
  1070. if ($result) {
  1071. foreach ($result->toArray() as $val) {
  1072. array_push($list, json_decode($val['category_arr_id']));
  1073. }
  1074. }
  1075. if ($result) {
  1076. return Result::success($list);
  1077. } else {
  1078. return Result::error("查询失败", 0);
  1079. }
  1080. }
  1081. /**
  1082. * 更新网站导航
  1083. * @param array $data
  1084. * @return array
  1085. */
  1086. public function upWebsiteCategory(array $data): array
  1087. {
  1088. $pinyin = new Pinyin();
  1089. Db::beginTransaction();
  1090. try {
  1091. if (!empty($data['category_arr_id'])) {
  1092. $websiteId = $data['website_id'];
  1093. // 1. 获取旧的 category_id 列表
  1094. $oldCategoryListIds = WebsiteCategory::where('website_id', $websiteId)->pluck('category_id')->toArray();
  1095. // 2. 获取新的 category_id 列表(去重)
  1096. $newCategoryListIds = [];
  1097. foreach ($data['category_arr_id'] as $arr) {
  1098. $lastId = end($arr);
  1099. $newCategoryListIds[] = $lastId;
  1100. }
  1101. $newCategoryListIds = array_unique($newCategoryListIds);
  1102. // 3. 需要删除的 category_id
  1103. $toDelete = array_diff($oldCategoryListIds, $newCategoryListIds);
  1104. if (!empty($toDelete)) {
  1105. WebsiteCategory::where('website_id', $websiteId)
  1106. ->whereIn('category_id', $toDelete)
  1107. ->delete();
  1108. }
  1109. // 4. 批量查出所有需要的 category 信息
  1110. $categories = Category::whereIn('id', $newCategoryListIds)->get()->keyBy('id');
  1111. // 5. 组装 upsertData,确保 category_id 不重复
  1112. $upsertData = [];
  1113. $handledCategoryIds = [];
  1114. foreach ($data['category_arr_id'] as $arr) {
  1115. $categoryId = end($arr);
  1116. if (!isset($categories[$categoryId]) || in_array($categoryId, $handledCategoryIds)) continue;
  1117. $info = $categories[$categoryId];
  1118. $aLIas_pinyin = $pinyin->permalink($info->name, '');
  1119. $upsertData[] = [
  1120. 'website_id' => $websiteId,
  1121. 'category_id' => $categoryId,
  1122. 'name' => $info->name ?? '',
  1123. 'alias' => $info->name ?? '',
  1124. 'aLIas_pinyin' => $aLIas_pinyin ?? '',
  1125. 'sort' => $info->sort ?? 0,
  1126. 'pid' => $info->pid ?? 0,
  1127. 'pid_arr' => $info->pid_arr ?? json_encode([]),
  1128. 'seo_title' => $info->seo_title ?? "",
  1129. 'seo_keywords' => $info->seo_keywords ?? "",
  1130. 'seo_description' => $info->seo_description ?? "",
  1131. 'is_url' => $info->is_url ?? 0,
  1132. 'web_url' => $info->web_url ?? '',
  1133. 'type' => $info->type ?? 1,
  1134. 'category_arr_id' => json_encode($arr)
  1135. ];
  1136. $handledCategoryIds[] = $categoryId;
  1137. }
  1138. // 6. upsert
  1139. if (!empty($upsertData)) {
  1140. WebsiteCategory::upsert(
  1141. $upsertData,
  1142. ['website_id', 'category_id'],
  1143. [
  1144. 'aLIas_pinyin',
  1145. 'pid',
  1146. 'pid_arr',
  1147. 'is_url',
  1148. 'web_url',
  1149. 'category_arr_id'
  1150. ]
  1151. );
  1152. }
  1153. }
  1154. Db::commit();
  1155. return Result::success();
  1156. } catch (\Throwable $ex) {
  1157. Db::rollBack();
  1158. return Result::error("修改失败" . $ex->getMessage(), 0);
  1159. }
  1160. }
  1161. /**
  1162. * 获取网站列表
  1163. * @param array $data
  1164. * @return array
  1165. */
  1166. public function getWebsiteCategoryList(array $data): array
  1167. {
  1168. $where = [];
  1169. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1170. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1171. }
  1172. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1173. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1174. }
  1175. $result = Website::where($where)
  1176. ->with(["websiteCategory" => function ($query) {
  1177. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1178. }])
  1179. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1180. ->get();
  1181. $count = Website::where($where)->count();
  1182. if (empty($result)) {
  1183. return Result::error("没有数据", 0);
  1184. }
  1185. $data = [
  1186. 'rows' => $result->toArray(),
  1187. 'count' => $count,
  1188. ];
  1189. if ($result) {
  1190. return Result::success($data);
  1191. } else {
  1192. return Result::error("查询失败", 0);
  1193. }
  1194. }
  1195. /**
  1196. * 删除网站下的所有导航
  1197. * @param array $data
  1198. * @return array
  1199. */
  1200. public function delWebsiteAllCategory(array $data): array
  1201. {
  1202. $website_id = $data['website_id'];
  1203. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1204. if ($result) {
  1205. return Result::success($result);
  1206. } else {
  1207. return Result::error("删除失败", 0);
  1208. }
  1209. }
  1210. /**
  1211. * 获取网站下的某一个导航
  1212. * @param array $data
  1213. * @return array
  1214. */
  1215. public function getWebsiteCategoryOnes(array $data): array
  1216. {
  1217. $website_id = $data['website_id'];
  1218. $category_id = $data['category_id'];
  1219. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1220. ->first();
  1221. if ($result) {
  1222. return Result::success($result);
  1223. } else {
  1224. return Result::error("查询失败", 0);
  1225. }
  1226. }
  1227. /**
  1228. * 更新网闸下的某一个导航
  1229. * @param array $data
  1230. * @return array
  1231. */
  1232. public function upWebsiteCategoryones(array $data): array
  1233. {
  1234. //处理所有数据
  1235. //获取所有数据
  1236. if (isset($data['all']) && $data['all'] == 'all') {
  1237. $result = WebsiteCategory::get();
  1238. $result = $result->toArray();
  1239. var_dump($result, '------------');
  1240. $pinyin = new Pinyin();
  1241. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1242. for ($i = 0; $i < count($result); $i++) {
  1243. // $randss = substr(str_shuffle($letters), 0, 4);
  1244. $result[$i]['aLIas_pinyin'] = $pinyin->permalink($result[$i]['alias'], '');
  1245. WebsiteCategory::where(['id' => $result[$i]['id']])->update(['aLIas_pinyin' => $result[$i]['aLIas_pinyin']]);
  1246. }
  1247. var_dump($result, '--------222----');
  1248. if ($result) {
  1249. return Result::success($result);
  1250. } else {
  1251. return Result::error("更新失败", 0);
  1252. }
  1253. }
  1254. //处理所有数据
  1255. $where = [
  1256. 'website_id' => $data['website_id'],
  1257. 'category_id' => $data['category_id'],
  1258. ];
  1259. $alias = $data['alias'];
  1260. $pinyin = new Pinyin();
  1261. $aLIas_pinyin = $pinyin->permalink($alias, '');
  1262. //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
  1263. $result = WebsiteCategory::where($where)->first();
  1264. $result = $result->toArray();
  1265. //+四位随机字母
  1266. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1267. // 打乱字符串顺序并截取前四位
  1268. // $randss = substr(str_shuffle($letters), 0, 4);
  1269. if ($data['alias_pinyin'] == '') {
  1270. $data['aLIas_pinyin'] = $aLIas_pinyin;
  1271. }
  1272. $result = WebsiteCategory::where($where)->update($data);
  1273. if ($result) {
  1274. return Result::success($result);
  1275. } else {
  1276. return Result::error("更新失败", 0);
  1277. }
  1278. }
  1279. /**
  1280. * 获取网站下的所有导航(包含子导航)
  1281. * @param array $data
  1282. * @return array
  1283. */
  1284. public function getWebsiteAllCategory(array $data): array
  1285. {
  1286. $where = [];
  1287. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1288. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1289. }
  1290. // if (isset($data['name']) && !empty($data['name'])) {
  1291. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1292. // }
  1293. // if (isset($data['alias']) && !empty($data['alias'])) {
  1294. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1295. // }
  1296. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1297. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1298. // }
  1299. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1300. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1301. // }
  1302. $result = WebsiteCategory::where($where)
  1303. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1304. ->select("website_category.*", "category.name as name")
  1305. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1306. // ->leftJoin("department", 'category.department_id', 'department.id')
  1307. // ->leftJoin("district", 'category.city_id', 'district.id')
  1308. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1309. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1310. ->orderBy("sort", "asc")
  1311. ->get();
  1312. // $count = WebsiteCategory::where($where)
  1313. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1314. // ->leftJoin("department", 'category.department_id', 'department.id')
  1315. // ->leftJoin("district", 'category.city_id', 'district.id')
  1316. // ->count();
  1317. if (empty($result)) {
  1318. return Result::error("没有数据", 0);
  1319. }
  1320. // $data = [
  1321. // 'rows' => $result->toArray(),
  1322. // 'count' => $count,
  1323. // ];
  1324. if ($result) {
  1325. return Result::success($result->toArray());
  1326. } else {
  1327. return Result::error("查询失败", 0);
  1328. }
  1329. }
  1330. /**
  1331. * 递归查询数据
  1332. * @param $id
  1333. * @param $ids
  1334. * @return string
  1335. */
  1336. public function getUnderlingUIds($id, $ids = '')
  1337. {
  1338. $back = Category::where(['pid' => $id])->get();
  1339. $back = $back->toArray();
  1340. if (!empty($back) && is_array($back)) {
  1341. foreach ($back as $v) {
  1342. //防止当前人的ID重复去查询,形成恶性循环
  1343. if ($v['id'] == $id) {
  1344. continue;
  1345. }
  1346. $back2 = Category::where(['pid' => $id])->count('id');
  1347. if ($back2 > 0) {
  1348. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1349. } else {
  1350. $ids .= ',' . $v['id'];
  1351. }
  1352. }
  1353. }
  1354. $ids = $id . ',' . $ids . ',';
  1355. $ids = str_replace(',,', ",", $ids);
  1356. $ids = trim($ids, ',');
  1357. return $ids;
  1358. }
  1359. /**
  1360. * 检测网站名称是否重复
  1361. * @param array $data
  1362. * @return array
  1363. */
  1364. public function checkWebsiteName(array $data): array
  1365. {
  1366. if (isset($data['id'])) {
  1367. $data[] = ['id', "!=", $data['id']];
  1368. unset($data['id']);
  1369. }
  1370. $websiteInfo = Website::query()->where($data)->first();
  1371. if (empty($websiteInfo)) {
  1372. return Result::error("找不到网站", 0);
  1373. }
  1374. return Result::success($websiteInfo->toArray());
  1375. }
  1376. /**
  1377. * 检测网站url是否重复
  1378. * @param array $data
  1379. * @return array
  1380. */
  1381. public function checkWebsiteUrl(array $data): array
  1382. {
  1383. $whereData = [];
  1384. if (isset($data['id'])) {
  1385. $whereData = [['id', "!=", $data['id']]];
  1386. unset($data['id']);
  1387. }
  1388. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1389. if (empty($websiteInfo)) {
  1390. return Result::error("找不到URL", 0);
  1391. }
  1392. return Result::success($websiteInfo->toArray());
  1393. }
  1394. /**
  1395. * 获取网站底部基础信息
  1396. * @param array $data
  1397. * @return array
  1398. */
  1399. public function getWebsiteFootInfo(array $data): array
  1400. {
  1401. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1402. $website_head = Website::where('id', $data['website_id'])
  1403. ->where('status', 1)
  1404. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1405. ->first();
  1406. if (empty($website_head)) {
  1407. return Result::error("找不到网站", 0);
  1408. }
  1409. } else {
  1410. return Result::error("参数错误", 0);
  1411. }
  1412. $website_head = Website::where('id', $data['website_id'])
  1413. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1414. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1415. if (empty($website_foot)) {
  1416. return Result::error("暂无底部基础信息", 0);
  1417. }
  1418. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1419. $result = [
  1420. 'website_foot' => $website_foot,
  1421. 'website_head' => $website_head,
  1422. ];
  1423. return Result::success($result);
  1424. }
  1425. /**
  1426. * 获取网站底部导航
  1427. * @param array $data
  1428. * @return array
  1429. */
  1430. public function getWebsiteFooterCategory(array $data): array
  1431. {
  1432. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1433. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1434. if (empty($website)) {
  1435. return Result::error("找不到网站", 0);
  1436. }
  1437. } else {
  1438. return Result::error("参数错误", 0);
  1439. }
  1440. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1441. if (empty($result)) {
  1442. return Result::error("暂无底部导航", 0);
  1443. }
  1444. return Result::success($result->toArray());
  1445. }
  1446. /**
  1447. * 获取网站底部导航列表
  1448. * @param array $data
  1449. * @return array
  1450. */
  1451. public function getWebsiteFooterCategoryList(array $data): array
  1452. {
  1453. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1454. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1455. if (empty($website)) {
  1456. return Result::error("找不到网站", 0);
  1457. }
  1458. } else {
  1459. return Result::error("参数错误", 0);
  1460. }
  1461. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1462. // '底部导航类型 0:内容型;1:列表型;',
  1463. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1464. return Result::error("底部导航id错误", 0);
  1465. } else {
  1466. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1467. if ($query->count() == 0) {
  1468. return Result::error("暂无底部导航列表", 0);
  1469. } elseif ($query->count() == 1) {
  1470. $result = $query->first();
  1471. } else {
  1472. $result = $query->get();
  1473. }
  1474. }
  1475. return Result::success($result);
  1476. }
  1477. /**
  1478. * 获取网站底部导航
  1479. * @param array $data
  1480. * @return array
  1481. */
  1482. public function getWebsiteFooterCategoryInfo(array $data): array
  1483. {
  1484. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1485. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1486. if (empty($website)) {
  1487. return Result::error("找不到网站", 0);
  1488. }
  1489. } else {
  1490. return Result::error("参数错误", 0);
  1491. }
  1492. if (isset($data['type']) && $data['type'] == 0) {
  1493. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1494. if (empty($fcatid)) {
  1495. return Result::error("底部导航id错误", 0);
  1496. }
  1497. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1498. } else {
  1499. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1500. }
  1501. if (empty($result)) {
  1502. return Result::error("暂无底部导航内容", 0);
  1503. }
  1504. return Result::success($result);
  1505. }
  1506. /*
  1507. * 搜索网站二级导航
  1508. * @param array $data
  1509. * @return array
  1510. * */
  1511. public function selectWebsiteCategory(array $data): array
  1512. {
  1513. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1514. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1515. if (empty($website)) {
  1516. return Result::error("暂无该网站", 0);
  1517. }
  1518. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1519. // return Result::success($category);
  1520. if (empty($category)) {
  1521. return Result::error("暂无此导航", 0);
  1522. }
  1523. $query = Category::whereIn('id', $category);
  1524. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1525. $cityid = District::where('id', $data['cityid'])->first();
  1526. if (empty($cityid)) {
  1527. return Result::error("暂无此城市", 0);
  1528. } else {
  1529. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1530. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1531. if (empty($city_category)) {
  1532. return Result::error("暂无此城市下的导航", 0);
  1533. }
  1534. $result['catid'] = $city_category;
  1535. $city = 1;
  1536. // var_dump("城市====================",$result);
  1537. }
  1538. }
  1539. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1540. $departmentid = Department::where('id', $data['department_id'])->first();
  1541. if (empty($departmentid)) {
  1542. return Result::error("暂无此部门", 0);
  1543. } else {
  1544. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1545. if (empty($depart_category)) {
  1546. return Result::error("暂无此部门下的导航", 0);
  1547. }
  1548. $result['catid'] = $depart_category;
  1549. // var_dump("职能部门*******************",$result);
  1550. $department = 1;
  1551. }
  1552. }
  1553. if (!empty($city) && !empty($department)) {
  1554. // var_dump("城市和职能部门----------------------",$result);
  1555. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1556. if (empty($sel_category)) {
  1557. return Result::error("暂无此城市和职能部门下的导航", 0);
  1558. }
  1559. $result['catid'] = $sel_category;
  1560. }
  1561. if (empty($result)) {
  1562. return Result::error("暂无导航", 0);
  1563. }
  1564. // department_id
  1565. } else {
  1566. return Result::error("参数错误", 0);
  1567. }
  1568. return Result::success($result);
  1569. }
  1570. /**
  1571. * 获取网站栏目seo
  1572. * @param array $data
  1573. * @return array
  1574. */
  1575. public function getWebsiteCategoryHead(array $data): array
  1576. {
  1577. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1578. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1579. if (empty($website)) {
  1580. return Result::error("找不到网站", 0);
  1581. }
  1582. } else {
  1583. return Result::error("参数错误", 0);
  1584. }
  1585. if (isset($data['catid']) && !empty($data['catid'])) {
  1586. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1587. }
  1588. if (empty($result)) {
  1589. return Result::error("暂无导航", 0);
  1590. }
  1591. $result['website_name'] = $website['website_name'] ?? '';
  1592. $result['suffix'] = $website['suffix'] ?? '';
  1593. return Result::success($result);
  1594. }
  1595. /*
  1596. * 获取某个栏目
  1597. * @param array $data
  1598. * @return array
  1599. * */
  1600. public function getOneWebsiteCategory(array $data): array
  1601. {
  1602. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1603. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1604. if (empty($website)) {
  1605. return Result::error("暂无该网站", 0);
  1606. }
  1607. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1608. if (empty($category)) {
  1609. return Result::error("暂无此导航", 0);
  1610. }
  1611. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1612. $parent = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $category['pid'])->select('category_id as parent_id', 'alias as parent_name', 'aLIas_pinyin')->first();
  1613. $category['parent_id'] = $parent['parent_id'] ?? '';
  1614. $category['parent_pinyin'] = $parent['aLIas_pinyin'] ?? '';
  1615. $category['parent_name'] = $parent['parent_name'] ?? '';
  1616. return Result::success($category);
  1617. } else {
  1618. return Result::error("参数错误", 0);
  1619. }
  1620. }
  1621. // 自助建站--fr-------------------
  1622. /**
  1623. * 获取并搜索 网站模板信息
  1624. * @param array $data
  1625. * @return array
  1626. */
  1627. public function getWebsiteintel(array $data): array
  1628. {
  1629. //查询所有网站模板信息
  1630. $query = Website::where('website.status', 1)
  1631. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1632. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1633. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1634. ->select(
  1635. "website_template_info.id",
  1636. "template_class.name",
  1637. "website.id as website_id",
  1638. "website.website_name",
  1639. "website.website_url",
  1640. "website_template_info.action_id",
  1641. "website_template_info.created_at",
  1642. "website_template_info.updated_at",
  1643. "website_template_info.page_type",
  1644. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1645. "template.template_name"
  1646. );
  1647. //若存在条件;则在$query的基础上进行筛选
  1648. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1649. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1650. }
  1651. if (isset($data['status']) && !empty($data['status'])) {
  1652. $query->where('website_template_info.status', $data['status']);
  1653. }
  1654. $count = $query->count();
  1655. $query->limit($data['pageSize'])
  1656. ->offset(($data['page'] - 1) * $data['pageSize'])
  1657. ->orderBy("website_template_info.updated_at", "desc");
  1658. $rep = $query->get();
  1659. if ($rep->count() == 0) {
  1660. return Result::error("没有查找到相关数据", 0);
  1661. } else {
  1662. $rep->each(function ($item) {
  1663. if (!empty($item->page_type)) {
  1664. $pageTypeArray = json_decode($item->page_type, true);
  1665. if (is_array($pageTypeArray)) {
  1666. $item->page_type = $pageTypeArray;
  1667. }
  1668. }
  1669. });
  1670. $result = [
  1671. "rows" => $rep->toArray(),
  1672. "count" => $count,
  1673. ];
  1674. return Result::success($result);
  1675. }
  1676. // return Result::success($result);
  1677. }
  1678. /**
  1679. * 添加网站基础信息
  1680. * @param array $data
  1681. * @return array
  1682. */
  1683. public function addWebsiteTemplateintel(array $data): array
  1684. {
  1685. $website = Website::where('website.id', $data['website_id'])->first();
  1686. if (empty($website)) {
  1687. return Result::error("请输入正确的网站id!", 0);
  1688. }
  1689. if (empty($website['website_column_id'])) {
  1690. return Result::error("请先关联导航池!", 0);
  1691. }
  1692. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1693. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1694. if (empty($foot_type)) {
  1695. return Result::error("请先关联底部导航池!", 0);
  1696. } elseif (in_array(1, $foot_type)) {
  1697. $foot_type = 1;
  1698. } else {
  1699. $foot_type = 0;
  1700. }
  1701. $result['foot_type'] = $foot_type;
  1702. // 友情链接类型'1:图片 2:文字 3:底部';
  1703. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1704. $missingTypes = [];
  1705. if (!in_array(1, $types)) {
  1706. $missingTypes[] = "文字链接";
  1707. }
  1708. if (!in_array(2, $types)) {
  1709. $missingTypes[] = "图片链接";
  1710. }
  1711. if (!in_array(3, $types)) {
  1712. $missingTypes[] = "底部链接";
  1713. }
  1714. if (!empty($missingTypes)) {
  1715. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1716. return Result::error($errorMessage, 0);
  1717. }
  1718. if (isset($data['page_type'])) {
  1719. // 删除重复元素并重新索引数组
  1720. $data['page_type'] = array_unique($data['page_type']);
  1721. $data['page_type'] = array_values($data['page_type']);
  1722. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1723. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1724. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1725. if (empty($info)) {
  1726. // 将数组转换为 JSON 字符串
  1727. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1728. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1729. $data['action_id'] = 1;
  1730. try {
  1731. $result = WebsiteTemplateInfo::insertGetId($data);
  1732. } catch (\Exception $e) {
  1733. if ($e->getCode() == 22001) {
  1734. $errorMessage = $e->getMessage();
  1735. return Result::error("请检查表单,某个字段超出了长度限制!");
  1736. } else {
  1737. // 处理其他类型的数据库错误
  1738. return Result::error("插入操作失败: " . $e->getMessage());
  1739. }
  1740. return Result::error("添加失败!", 0);
  1741. }
  1742. } else {
  1743. return Result::error("该网站已经构建过了!", 0);
  1744. }
  1745. } else {
  1746. return Result::error("请先选择首页和底部导航详情页!", 0);
  1747. }
  1748. }
  1749. if (empty($result)) {
  1750. return Result::error("添加失败!", 0);
  1751. } else {
  1752. return Result::success($result);
  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. )
  1994. ->first();
  1995. if (empty($result)) {
  1996. return Result::error("没有查找到相关数据", 0);
  1997. }
  1998. // return Result::success($result);
  1999. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  2000. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  2001. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  2002. foreach ($template_img as $key => $value) {
  2003. if (!in_array($value['value'], $page_type)) {
  2004. unset($template_img[$key]);
  2005. }
  2006. }
  2007. // return Result::success($result);
  2008. $result['template_img'] = array_values($template_img);
  2009. $result['page_type'] = $page_type;
  2010. // $templateList = $this->getWebsiteTemplateList($data);
  2011. // $count = $templateList['data']['count']??'';
  2012. // $pages = $count / $data['pageSize'];
  2013. // $found = false;
  2014. // while ($pages && !$found) {
  2015. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  2016. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  2017. // if ($index !== false) {
  2018. // $found = true;
  2019. // $result['page'] = $pages;
  2020. // $pages = 0;
  2021. // // echo "查询到的 template 在第 $page 页";
  2022. // }
  2023. // $pages--;
  2024. // }
  2025. return Result::success($result);
  2026. }
  2027. /**
  2028. * 获取网站模板下的板块信息
  2029. * @param array $data
  2030. */
  2031. public function getWebsiteSectorList(array $data): array
  2032. {
  2033. $website = Website::where('id', $data['website_id'])->first();
  2034. if (empty($website)) {
  2035. return Result::error("请输入正确的网站id", 0);
  2036. }
  2037. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2038. if (empty($template['template_id'])) {
  2039. return Result::error("请先添加网站模板", 0);
  2040. }
  2041. $query = Sector::where('template_id', $template['template_id']);
  2042. if ($query->count() == 0) {
  2043. return Result::error("没有查找到相关板块数据", 0);
  2044. } elseif ($query->count() == 1) {
  2045. $sector = $query->first();
  2046. } else {
  2047. $sector = $query->get();
  2048. }
  2049. $result['page_type'] = $template['page_type'];
  2050. $result['tid'] = $template['template_id'];
  2051. $result = [
  2052. "tid" => $template['template_id'],
  2053. "page_type" => $template['page_type'],
  2054. "sector" => $sector,
  2055. ];
  2056. return Result::success($result);
  2057. }
  2058. // 自助建站---------------fr---------------
  2059. // 网站底基础信息--fr-------------------
  2060. /**
  2061. * 添加网站底基础信息
  2062. * @param array $data
  2063. */
  2064. public function addWebFootInfo(array $data): array
  2065. {
  2066. $website = Website::where('id', $data['website_id'])->first();
  2067. if (empty($website)) {
  2068. return Result::error("请输入正确的网站id!", 0);
  2069. }
  2070. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2071. if (!empty($info)) {
  2072. return Result::error("该网站已经添加过了!", 0);
  2073. }
  2074. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  2075. if (empty($result)) {
  2076. return Result::error("请先添加网站基础信息!", 0);
  2077. } else {
  2078. return Result::success($result);
  2079. }
  2080. }
  2081. /**
  2082. * 获取网站底基础信息
  2083. * @param array $data
  2084. */
  2085. public function getWebFootInfo(array $data): array
  2086. {
  2087. $website = Website::where('id', $data['website_id'])->first();
  2088. if (empty($website)) {
  2089. return Result::error("请输入正确的网站id!", 0);
  2090. }
  2091. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2092. if (empty($result)) {
  2093. return Result::error("请先添加网站基础信息!", 0);
  2094. }
  2095. return Result::success($result);
  2096. }
  2097. /**
  2098. * 修改网站底基础信息
  2099. * @param array $data
  2100. */
  2101. public function upWebFootInfo(array $data): array
  2102. {
  2103. // return Result::success($data);
  2104. $website = Website::where('id', $data['website_id'])->first();
  2105. if (empty($website)) {
  2106. return Result::error("请输入正确的网站id!", 0);
  2107. }
  2108. $web = $data['website_id'];
  2109. unset($data['website_id']);
  2110. $where = ['website_id' => $web];
  2111. // return Result::success($data);
  2112. $result = WebsiteTemplateInfo::where($where)->first();
  2113. if (empty($result)) {
  2114. $data['website_id'] = $web;
  2115. $result = WebsiteTemplateInfo::insertGetId($data);
  2116. // return Result::error("请先添加网站基础信息!",0);
  2117. } else {
  2118. $result = WebsiteTemplateInfo::where($where)->update($data);
  2119. }
  2120. // $where = ['website_id'=>$data['website_id']];
  2121. // unset($data['website_id']);
  2122. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  2123. if (empty($result)) {
  2124. return Result::error("修改失败!", 0);
  2125. }
  2126. return Result::success($result);
  2127. }
  2128. /*
  2129. * 获取父级/子级栏目------路由
  2130. * @param array $data
  2131. * @return array
  2132. * */
  2133. public function getWebsiteParentCategory(array $data): array
  2134. {
  2135. // return Result::success($data);
  2136. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2137. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2138. }
  2139. if (empty($website)) {
  2140. return Result::error("暂无该网站", 0);
  2141. }
  2142. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2143. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2144. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2145. ->where('pid', 0)
  2146. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2147. ->get() // 添加 get() 方法获取结果集合
  2148. ->all();
  2149. // 提前查询所有父级栏目,避免在 map 中多次查询
  2150. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2151. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2152. ->where('pid', 0)
  2153. ->select('category_id', 'aLIas_pinyin')
  2154. ->get()
  2155. ->keyBy('category_id');
  2156. // 直接查询子栏目信息
  2157. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2158. ->where('pid', '!=', 0)
  2159. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2160. ->get();
  2161. // 使用集合的 map 方法处理子栏目信息
  2162. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2163. $alias = $item->alias;
  2164. $category_id = $item->category_id;
  2165. $aLIas_pinyin = $item->aLIas_pinyin;
  2166. $parent = $parentCategories->get($item->pid);
  2167. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2168. $result = [
  2169. 'name' => $alias,
  2170. 'cid' => $category_id
  2171. ];
  2172. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2173. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2174. }
  2175. return $result;
  2176. })->values()->all();
  2177. if (empty($category)) {
  2178. return Result::error("暂无此导航", 0);
  2179. }
  2180. return Result::success($category);
  2181. }
  2182. /**
  2183. * 获取网站栏目信息
  2184. * @param array $data
  2185. */
  2186. public function getWebsiteFootAll(array $data): array
  2187. {
  2188. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2189. if (empty($website)) {
  2190. return Result::error("暂无该网站", 0);
  2191. }
  2192. // 1:图片 2:文字 3:底部
  2193. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2194. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2195. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2196. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2197. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2198. if (empty($result)) {
  2199. return Result::error("暂无此网站信息", 0);
  2200. }
  2201. return Result::success($result);
  2202. }
  2203. /**
  2204. * 获取网站顶部信息
  2205. * @param array $data
  2206. */
  2207. public function getWebsiteHead(array $data): array
  2208. {
  2209. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2210. if (empty($result)) {
  2211. return Result::error("暂无该网站", 0);
  2212. }
  2213. return Result::success($result);
  2214. }
  2215. /**
  2216. * 路由匹配
  2217. * @param array $data
  2218. */
  2219. public function getWebsiteRoute(array $data): array
  2220. {
  2221. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2222. if (empty($website)) {
  2223. return Result::error("暂无该网站", 0);
  2224. }
  2225. if (isset($data['pinyin']) && !empty($data['pinyin'])) {
  2226. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2227. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2228. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2229. $join->on('pc.pid', '=', 'website_category.category_id')
  2230. ->where('pc.website_id', '=', $data['website_id']);
  2231. })
  2232. ->select('website_category.category_id', 'website_category.alias', DB::raw('CASE WHEN pc.category_id IS NOT NULL THEN 1 ELSE 0 END as children_count'))
  2233. ->first();
  2234. }
  2235. if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
  2236. $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
  2237. }
  2238. if (!isset($result) || empty($result)) {
  2239. return Result::error("暂无该导航", 0);
  2240. }
  2241. return Result::success($result);
  2242. }
  2243. //20250212 网站标识
  2244. public function addWebsiteGroup(array $data): array
  2245. {
  2246. //判斷name是不是重复
  2247. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2248. if (!empty($websiteGroupInfo)) {
  2249. return Result::error("网站标识重复", 0);
  2250. }
  2251. //添加信息
  2252. $result = WebsiteGroup::insertGetId($data);
  2253. var_dump($result);
  2254. if (empty($result)) {
  2255. return Result::error("创建失败", 0);
  2256. } else {
  2257. return Result::success($result);
  2258. }
  2259. }
  2260. public function getWebsiteGroupList(array $data): array
  2261. {
  2262. $where = [];
  2263. if (isset($data['name']) && !empty($data['name'])) {
  2264. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2265. }
  2266. $result = WebsiteGroup::where($where)
  2267. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2268. ->get();
  2269. foreach ($result as $websiteGroup) {
  2270. $webIds = json_decode($websiteGroup->web_ids, true);
  2271. $websites = Website::whereIn('id', $webIds)->get();
  2272. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2273. $websiteGroup->website_names = $websiteNames;
  2274. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2275. }
  2276. $count = WebsiteGroup::where($where)->count();
  2277. if (empty($result)) {
  2278. return Result::error("没有数据", 0);
  2279. }
  2280. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2281. }
  2282. public function getWebsiteGroupInfo(array $data): array
  2283. {
  2284. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2285. $webIds = json_decode($websiteInfo->web_ids, true);
  2286. $websites = Website::whereIn('id', $webIds)->get();
  2287. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2288. $websiteInfo->website_names = $websiteNames;
  2289. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2290. if (empty($websiteInfo)) {
  2291. return Result::error("找不到URL", 0);
  2292. }
  2293. return Result::success($websiteInfo->toArray());
  2294. }
  2295. public function updateWebsiteGroup(array $data): array
  2296. {
  2297. $where = [
  2298. 'id' => $data['id'],
  2299. ];
  2300. $insertData = [
  2301. 'name' => $data['name'],
  2302. 'web_ids' => $data['web_ids'],
  2303. ];
  2304. //判斷name是不是重复
  2305. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2306. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2307. return Result::error("网站标识重复", 0);
  2308. }
  2309. $result = WebsiteGroup::where($where)->update($insertData);
  2310. var_dump($result, '------更新');
  2311. if (empty($result)) {
  2312. return Result::error("更新失败", 0);
  2313. } else {
  2314. return Result::success();
  2315. }
  2316. }
  2317. public function deleteWebsiteGroup(array $data): array
  2318. {
  2319. $where = [
  2320. 'id' => $data['id'],
  2321. ];
  2322. //看看user表是不是有这个id
  2323. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2324. if (!empty($userGroup)) {
  2325. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2326. }
  2327. $result = WebsiteGroup::where($where)->delete();
  2328. if (empty($result)) {
  2329. return Result::error("删除失败", 0);
  2330. } else {
  2331. return Result::success();
  2332. }
  2333. }
  2334. public function getWebsiteNavList(array $data): array
  2335. {
  2336. $data['type'] = isset($data['type']) ? $data['type'] : 1;
  2337. $where = [];
  2338. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2339. array_push($where, ['website_id', '=', $data['website_id']]);
  2340. }
  2341. if (isset($data['pid'])) {
  2342. array_push($where, ['pid', '=', $data['pid']]);
  2343. }
  2344. if (isset($data['type']) && !empty($data['type'])) {
  2345. array_push($where, ['type', '=', $data['type']]);
  2346. }
  2347. // 根据站点和type获取websiteCategory 中的id
  2348. $websiteCategoryids = WebsiteCategory::query()
  2349. ->where('website_id', '=', $data['website_id'])
  2350. ->where('type', '=', $data['type'])
  2351. ->pluck('category_arr_id')->toArray();
  2352. var_dump($websiteCategoryids, '------获取websiteCategory 中的id');
  2353. //合并去重
  2354. $category_ids = [];
  2355. foreach ($websiteCategoryids as $key => $value) {
  2356. $v = json_decode($value, true);
  2357. $category_ids = array_unique(array_merge($category_ids, $v));
  2358. }
  2359. // var_dump($category_ids, '------获取websiteCategory 中的id');
  2360. //根据pid取出所有的websiteCategory 中的id
  2361. $list = WebsiteCategory::query()
  2362. ->where('website_id', '=', $data['website_id'])
  2363. ->where('pid', '=', $data['pid'])
  2364. ->whereIn('category_id', $category_ids)
  2365. ->get();
  2366. if (empty($list)) {
  2367. return Result::error("获取失败", 0);
  2368. } else {
  2369. return Result::success($list);
  2370. }
  2371. }
  2372. // 20250307 根据网站标识获和导航获取站点
  2373. public function getWebsiteNavPoolSite(array $data): array
  2374. {
  2375. $where = [];
  2376. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2377. array_push($where, ['category_id', '=', $data['category_id']]);
  2378. }
  2379. $list = WebsiteCategory::query()->where($where)
  2380. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2381. ->select('website_category.*', 'website.website_name')
  2382. ->distinct()
  2383. ->get();
  2384. if (empty($list)) {
  2385. return Result::error("获取失败", 0);
  2386. } else {
  2387. return Result::success($list);
  2388. }
  2389. }
  2390. public function getWebsiteNavPool(array $data): array
  2391. {
  2392. // 初始化查询条件数组
  2393. $query = WebsiteCategory::query();
  2394. // 如果传入了 websiteids,则添加到查询条件中
  2395. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2396. $query->whereIn('website_id', $data['websiteids']);
  2397. }
  2398. // 如果传入了 pid,则添加到查询条件中
  2399. if (isset($data['pid'])) {
  2400. $query->where('pid', '=', $data['pid']);
  2401. }
  2402. // 执行查询 唯一性 id
  2403. $list = $query->distinct()->get();
  2404. // 根据查询结果返回相应的结果
  2405. if ($list->isEmpty()) {
  2406. return Result::error("获取失败", 0);
  2407. } else {
  2408. return Result::success($list->toArray());
  2409. }
  2410. }
  2411. /**
  2412. *
  2413. * @param array $data
  2414. * @return array
  2415. */
  2416. public function getAllCategory(array $data): array
  2417. {
  2418. $result = Category::when($data, function ($query) use ($data) {
  2419. if (isset($data['name']) && !empty($data['name'])) {
  2420. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2421. }
  2422. })->get();
  2423. if (empty($result)) {
  2424. return Result::error("获取失败", 0);
  2425. } else {
  2426. return Result::success($result);
  2427. }
  2428. }
  2429. /**
  2430. * 修改网站栏目排序
  2431. * @param array $data
  2432. * @return array
  2433. */
  2434. public function upWebsiteCategorySort(array $data): array
  2435. {
  2436. $where = [
  2437. 'website_id' => $data['website_id'],
  2438. 'id' => $data['id'],
  2439. ];
  2440. $result = WebsiteCategory::where($where)->update($data);
  2441. if ($result) {
  2442. return Result::success($result);
  2443. } else {
  2444. return Result::error("更新失败", 0);
  2445. }
  2446. }
  2447. //获取底部导航内容详情
  2448. public function getFooterContentInfo(array $data): array
  2449. {
  2450. $where = [
  2451. "id" => $data['content_id']
  2452. ];
  2453. $result = FooterContent::where($where)->first();
  2454. if ($result) {
  2455. return Result::success($result);
  2456. } else {
  2457. return Result::error("没有数据");
  2458. }
  2459. }
  2460. //获取底部导航内容列表
  2461. public function getFooterContentList(array $data): array
  2462. {
  2463. $where = [
  2464. "fcat_id" => $data['fcat_id'],
  2465. ];
  2466. switch (intval($data['type'])) {
  2467. case 0:
  2468. $result = FooterContent::where($where)->first();
  2469. break;
  2470. case 1:
  2471. $result = FooterContent::where($where)->get();
  2472. break;
  2473. }
  2474. if ($result) {
  2475. return Result::success($result);
  2476. } else {
  2477. return Result::error("没有数据");
  2478. }
  2479. }
  2480. /**
  2481. * 获取底部导航列表
  2482. * @param array $data
  2483. * @return array
  2484. */
  2485. public function getFooterCategoryList(array $data): array
  2486. {
  2487. $where = [
  2488. "website_id" => $data['website_id']
  2489. ];
  2490. $result = FooterCategory::where($where)->get();
  2491. if ($result) {
  2492. return Result::success($result);
  2493. } else {
  2494. return Result::error("没有数据");
  2495. }
  2496. }
  2497. // --自助建站-----------20250522fr----------------------start
  2498. /**
  2499. * 获取网站模板下的板块信息
  2500. * @param array $data
  2501. * @return array
  2502. */
  2503. public function getStaticResourceList(array $data): array
  2504. {
  2505. // return Result::success($data);
  2506. $where = [];
  2507. if (isset($data['img_alias']) && !empty($data['img_alias'])) {
  2508. array_push($where, ['website_img.img_alias', 'like', '%' . $data['img_alias'] . '%']);
  2509. }
  2510. if (isset($data['img_type']) && !empty($data['img_type'])) {
  2511. array_push($where, ['website_img.img_type', '=', $data['img_type']]);
  2512. }
  2513. if (isset($data['website_name']) && !empty($data['website_name'])) {
  2514. $website = Website::where('website_name', 'like', '%' . $data['website_name'] . '%')->first();
  2515. if (empty($website)) {
  2516. return Result::error("没有查找到相关数据", 0);
  2517. } else {
  2518. array_push($where, ['website_img.website_id', '=', $website['id']]);
  2519. }
  2520. }
  2521. // return Result::success($where);
  2522. $query = WebsiteImg::when(!empty($where), function ($query) use ($where) {
  2523. $query->where($where);
  2524. })
  2525. ->orderBy('updated_at', 'desc');
  2526. $count = $query->count();
  2527. $row = $query->leftJoin('website', 'website.id', '=', 'website_img.website_id')
  2528. ->select('website_img.*', 'website.website_name')
  2529. ->limit($data['pageSize'])
  2530. ->offset(($data['page'] - 1) * $data['pageSize'])
  2531. ->get();
  2532. if (empty($row)) {
  2533. return Result::error("没有查找到相关数据", 0);
  2534. } else {
  2535. $result = [
  2536. 'row' => $row,
  2537. 'count' => $count,
  2538. ];
  2539. }
  2540. return Result::success($result);
  2541. }
  2542. /**
  2543. * 添加网站模板下的板块信息
  2544. * @param array $data
  2545. */
  2546. public function addStaticResource(array $data): array
  2547. {
  2548. // $file = $data['img_url']->file('image');
  2549. // $size = $file->getSize();
  2550. $img_data = [
  2551. 'website_id' => $data['website_id'] ?? 0,
  2552. 'img_alias' => $data['img_alias'] ?? '',
  2553. 'img_url' => $data['img_url'],
  2554. 'img_size' => $data['img_size'],
  2555. ];
  2556. $img = WebsiteImg::insertGetId($img_data);
  2557. if (empty($img)) {
  2558. return Result::error("添加失败", 0);
  2559. }
  2560. $img_id['id'] = $img;
  2561. $url = WebsiteImg::where($img_id)->value('img_url');
  2562. // 1. 分割路径部分(以 / 为分隔符)
  2563. $pathSegments = explode('/', parse_url($url, PHP_URL_PATH));
  2564. // 2. 取最后一个元素作为文件名
  2565. $fileName = end($pathSegments); // 1743041040714597.jpg
  2566. // 3. 分割文件名获取扩展名
  2567. $fileParts = explode('.', $fileName);
  2568. $fileType = end($fileParts); // jpg
  2569. $img_datas = [
  2570. 'img_type' => $fileType,
  2571. 'name' => $fileName
  2572. ];
  2573. $up_img = WebsiteImg::where($img_id)->update($img_datas);
  2574. if (empty($up_img)) {
  2575. return Result::error("修改失败", 0);
  2576. }
  2577. return Result::success($up_img);
  2578. // 将字节转换为更友好的单位
  2579. // $friendlySize = $this->formatFileSize($size);
  2580. // $data['img_size'] = $friendlySize;
  2581. }
  2582. /**
  2583. * 修改网站模板下的板块信息
  2584. * @param array $data
  2585. */
  2586. public function delStaticResource(array $data): array
  2587. {
  2588. $where = [
  2589. 'id' => $data['id'],
  2590. ];
  2591. $result = WebsiteImg::where($where)->delete();
  2592. if (empty($result)) {
  2593. return Result::error("修改失败", 0);
  2594. }
  2595. return Result::success($result);
  2596. }
  2597. /**
  2598. * 获取尺寸列表
  2599. * @param array $data
  2600. */
  2601. public function getSizeList(array $data): array
  2602. {
  2603. $where = [];
  2604. if (isset($data['width']) && !empty($data['width'])) {
  2605. array_push($where, ['width', 'like', '%' . $data['width'] . '%']);
  2606. }
  2607. if (isset($data['height']) && !empty($data['height'])) {
  2608. array_push($where, ['height', 'like', '%' . $data['height'] . '%']);
  2609. }
  2610. $query = Size::when($where, function ($query) use ($where) {
  2611. $query->where($where);
  2612. })
  2613. ->orderBy('updated_at', 'desc');
  2614. $count = $query->count();
  2615. $rep = $query
  2616. ->offset(($data['page'] - 1) * $data['pageSize'])
  2617. ->limit($data['pageSize'])
  2618. ->get();
  2619. $result = [
  2620. 'row' => $rep,
  2621. 'count' => $count,
  2622. ];
  2623. if (empty($result)) {
  2624. return Result::error("获取失败", 0);
  2625. } else {
  2626. return Result::success($result);
  2627. }
  2628. }
  2629. /**
  2630. * 添加尺寸
  2631. * @param array $data
  2632. */
  2633. public function addSize(array $data): array
  2634. {
  2635. $size = Size::where('width', $data['width'])->where('height', $data['height'])->first();
  2636. if (!empty($size)) {
  2637. return Result::error("尺寸已存在", 0);
  2638. }
  2639. $result = Size::insertGetId($data);
  2640. if (empty($result)) {
  2641. return Result::error("添加失败", 0);
  2642. }
  2643. return Result::success($result);
  2644. }
  2645. /**
  2646. * 删除尺寸
  2647. * @param array $data
  2648. */
  2649. public function delSize(array $data): array
  2650. {
  2651. $where = [
  2652. 'id' => $data['id'],
  2653. ];
  2654. $result = Size::where($where)->delete();
  2655. if (empty($result)) {
  2656. return Result::error("删除失败", 0);
  2657. }
  2658. return Result::success($result);
  2659. }
  2660. /**
  2661. * 修改尺寸
  2662. * @param array $data
  2663. */
  2664. public function upSize(array $data): array
  2665. {
  2666. $where = [
  2667. 'id' => $data['id'],
  2668. ];
  2669. $size = Size::where('id', '!=', $data['id'])->where('width', $data['width'])->where('height', $data['height'])->first();
  2670. if (!empty($size)) {
  2671. return Result::error("尺寸已存在", 0);
  2672. }
  2673. $result = Size::where($where)->update($data);
  2674. if (empty($result)) {
  2675. return Result::error("修改失败", 0);
  2676. }
  2677. return Result::success($result);
  2678. }
  2679. /**
  2680. * 获取尺寸详情
  2681. * @param array $data
  2682. */
  2683. public function getSizeInfo(array $data): array
  2684. {
  2685. $where = [
  2686. 'id' => $data['id'],
  2687. ];
  2688. $result = Size::where($where)->first();
  2689. if (empty($result)) {
  2690. return Result::error("获取失败", 0);
  2691. } else {
  2692. return Result::success($result);
  2693. }
  2694. }
  2695. // --自助建站-----------20250522fr----------------------end
  2696. /**
  2697. * 获取网站白名单路由地址
  2698. * @param array $data
  2699. * @return array
  2700. */
  2701. public function getWhiteRouterList(array $data): array
  2702. {
  2703. // 构建基础查询
  2704. $baseQuery = WhiteRouter::query();
  2705. // 功能名称模糊查询
  2706. if (!empty($data['function_name'])) {
  2707. $baseQuery->where('function_name', 'like', '%' . $data['function_name'] . '%');
  2708. }
  2709. // 网站ID查询 - 使用 JSON_CONTAINS 检查数组中是否包含指定的 website_id
  2710. if (!empty($data['website_id'])) {
  2711. $baseQuery->whereRaw('JSON_CONTAINS(website_id, ?)', [json_encode((int)$data['website_id'])]);
  2712. }
  2713. // 获取总数
  2714. $count = $baseQuery->count();
  2715. // 分页查询
  2716. $result = $baseQuery->orderBy('updated_at', 'desc')
  2717. ->limit($data['pageSize'])
  2718. ->offset(($data['page'] - 1) * $data['pageSize'])
  2719. ->get();
  2720. if (empty($result)) {
  2721. return Result::error("暂无数据", 0);
  2722. }
  2723. // 处理结果,将 website_id 转换为数组并获取对应的网站名称
  2724. $result->transform(function ($item) {
  2725. $websiteIds = json_decode($item->website_id, true);
  2726. $websiteNames = Website::whereIn('id', $websiteIds)
  2727. ->pluck('website_name')
  2728. ->toArray();
  2729. $item->website_names = $websiteNames;
  2730. $item->website_id = $websiteIds; // 将 website_id 转换为数组
  2731. return $item;
  2732. });
  2733. return Result::success([
  2734. 'rows' => $result->toArray(),
  2735. 'count' => $count
  2736. ]);
  2737. }
  2738. /**
  2739. * 添加网站白名单路由地址
  2740. * @param array $data
  2741. * @return array
  2742. */
  2743. public function addWhiteRouter(array $data): array
  2744. {
  2745. // 确保 website_id 是数组
  2746. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2747. // 过滤空值并去重
  2748. $websiteIds = array_filter($websiteIds);
  2749. $websiteIds = array_unique($websiteIds);
  2750. // 将数组中的值转换为整数
  2751. $websiteIds = array_map(function ($value) {
  2752. return (int)$value;
  2753. }, $websiteIds);
  2754. // 转换为 JSON 字符串存储
  2755. $data['website_id'] = json_encode($websiteIds);
  2756. $result = WhiteRouter::insertGetId($data);
  2757. if (empty($result)) {
  2758. return Result::error("添加失败", 0);
  2759. }
  2760. return Result::success($result);
  2761. }
  2762. /**
  2763. * 修改网站白名单路由地址
  2764. * @param array $data
  2765. * @return array
  2766. */
  2767. public function upWhiteRouter(array $data): array
  2768. {
  2769. $where = [
  2770. 'id' => $data['id'],
  2771. ];
  2772. // 确保 website_id 是数组
  2773. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2774. // 过滤空值并去重
  2775. $websiteIds = array_filter($websiteIds);
  2776. $websiteIds = array_unique($websiteIds);
  2777. // 将数组中的值转换为整数
  2778. $websiteIds = array_map(function ($value) {
  2779. return (int)$value;
  2780. }, $websiteIds);
  2781. // 转换为 JSON 字符串存储
  2782. $data['website_id'] = json_encode($websiteIds);
  2783. $result = WhiteRouter::where($where)->update($data);
  2784. if (empty($result)) {
  2785. return Result::error("修改失败", 0);
  2786. }
  2787. return Result::success($result);
  2788. }
  2789. /**
  2790. * 删除网站白名单路由地址
  2791. * @param array $data
  2792. * @return array
  2793. */
  2794. public function delWhiteRouter(array $data): array
  2795. {
  2796. $where = [
  2797. 'id' => $data['id'],
  2798. ];
  2799. $result = WhiteRouter::where($where)->delete();
  2800. if (empty($result)) {
  2801. return Result::error("删除失败", 0);
  2802. }
  2803. return Result::success($result);
  2804. }
  2805. /**
  2806. * 获取网站白名单路由地址详情
  2807. * @param array $data
  2808. * @return array
  2809. */
  2810. public function getWhiteRouterInfo(array $data): array
  2811. {
  2812. $where = [
  2813. 'id' => $data['id'],
  2814. ];
  2815. $result = WhiteRouter::where($where)->first();
  2816. if (empty($result)) {
  2817. return Result::error("没有查找到相关数据", 0);
  2818. }
  2819. // 将 website_id 从 JSON 字符串转换为数组
  2820. $result->website_id = json_decode($result->website_id, true);
  2821. return Result::success($result);
  2822. }
  2823. }