WebsiteService.php 115 KB

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