WebsiteService.php 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994
  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.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. $result = WebsiteImg::where('id', $data['id'])->update(['img_alias' => $data['img_alias']]);
  2637. if (empty($result)) {
  2638. return Result::error("修改失败", 0);
  2639. }
  2640. return Result::success($result);
  2641. }
  2642. // --自助建站-----------20250522fr----------------------end
  2643. /**
  2644. * 获取网站白名单路由地址
  2645. * @param array $data
  2646. * @return array
  2647. */
  2648. public function getWhiteRouterList(array $data): array
  2649. {
  2650. // 构建基础查询
  2651. $baseQuery = WhiteRouter::query();
  2652. // 功能名称模糊查询
  2653. if (!empty($data['function_name'])) {
  2654. $baseQuery->where('function_name', 'like', '%' . $data['function_name'] . '%');
  2655. }
  2656. // 网站ID查询 - 使用 JSON_CONTAINS 检查数组中是否包含指定的 website_id
  2657. if (!empty($data['website_id'])) {
  2658. $baseQuery->whereRaw('JSON_CONTAINS(website_id, ?)', [json_encode((int)$data['website_id'])]);
  2659. }
  2660. // 获取总数
  2661. $count = $baseQuery->count();
  2662. // 分页查询
  2663. $result = $baseQuery->orderBy('updated_at', 'desc')
  2664. ->limit($data['pageSize'])
  2665. ->offset(($data['page'] - 1) * $data['pageSize'])
  2666. ->get();
  2667. if (empty($result)) {
  2668. return Result::error("暂无数据", 0);
  2669. }
  2670. // 处理结果,将 website_id 转换为数组并获取对应的网站名称
  2671. $result->transform(function ($item) {
  2672. $websiteIds = json_decode($item->website_id, true);
  2673. $websiteNames = Website::whereIn('id', $websiteIds)
  2674. ->pluck('website_name')
  2675. ->toArray();
  2676. $item->website_names = $websiteNames;
  2677. $item->website_id = $websiteIds; // 将 website_id 转换为数组
  2678. return $item;
  2679. });
  2680. return Result::success([
  2681. 'rows' => $result->toArray(),
  2682. 'count' => $count
  2683. ]);
  2684. }
  2685. /**
  2686. * 添加网站白名单路由地址
  2687. * @param array $data
  2688. * @return array
  2689. */
  2690. public function addWhiteRouter(array $data): array
  2691. {
  2692. // 确保 website_id 是数组
  2693. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2694. // 过滤空值并去重
  2695. $websiteIds = array_filter($websiteIds);
  2696. $websiteIds = array_unique($websiteIds);
  2697. // 将数组中的值转换为整数
  2698. $websiteIds = array_map(function ($value) {
  2699. return (int)$value;
  2700. }, $websiteIds);
  2701. // 转换为 JSON 字符串存储
  2702. $data['website_id'] = json_encode($websiteIds);
  2703. $result = WhiteRouter::insertGetId($data);
  2704. if (empty($result)) {
  2705. return Result::error("添加失败", 0);
  2706. }
  2707. return Result::success($result);
  2708. }
  2709. /**
  2710. * 修改网站白名单路由地址
  2711. * @param array $data
  2712. * @return array
  2713. */
  2714. public function upWhiteRouter(array $data): array
  2715. {
  2716. $where = [
  2717. 'id' => $data['id'],
  2718. ];
  2719. // 确保 website_id 是数组
  2720. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  2721. // 过滤空值并去重
  2722. $websiteIds = array_filter($websiteIds);
  2723. $websiteIds = array_unique($websiteIds);
  2724. // 将数组中的值转换为整数
  2725. $websiteIds = array_map(function ($value) {
  2726. return (int)$value;
  2727. }, $websiteIds);
  2728. // 转换为 JSON 字符串存储
  2729. $data['website_id'] = json_encode($websiteIds);
  2730. $result = WhiteRouter::where($where)->update($data);
  2731. if (empty($result)) {
  2732. return Result::error("修改失败", 0);
  2733. }
  2734. return Result::success($result);
  2735. }
  2736. /**
  2737. * 删除网站白名单路由地址
  2738. * @param array $data
  2739. * @return array
  2740. */
  2741. public function delWhiteRouter(array $data): array
  2742. {
  2743. $where = [
  2744. 'id' => $data['id'],
  2745. ];
  2746. $result = WhiteRouter::where($where)->delete();
  2747. if (empty($result)) {
  2748. return Result::error("删除失败", 0);
  2749. }
  2750. return Result::success($result);
  2751. }
  2752. /**
  2753. * 获取网站白名单路由地址详情
  2754. * @param array $data
  2755. * @return array
  2756. */
  2757. public function getWhiteRouterInfo(array $data): array
  2758. {
  2759. $where = [
  2760. 'id' => $data['id'],
  2761. ];
  2762. $result = WhiteRouter::where($where)->first();
  2763. if (empty($result)) {
  2764. return Result::error("没有查找到相关数据", 0);
  2765. }
  2766. // 将 website_id 从 JSON 字符串转换为数组
  2767. $result->website_id = json_decode($result->website_id, true);
  2768. return Result::success($result);
  2769. }
  2770. /**
  2771. * 克隆网站
  2772. * @param array $data
  2773. * @return array
  2774. */
  2775. public function cloneWebsite(array $data): array
  2776. {
  2777. DB::beginTransaction();
  2778. try {
  2779. // 获取原始网站信息
  2780. $originalWebsite = Website::find($data['website_id']);
  2781. if (!$originalWebsite) {
  2782. return Result::error("找不到该网站", 0);
  2783. }
  2784. // 提取基础名称(去除已有 _copy 或 _copy_xxx)
  2785. $baseName = preg_replace('/_copy(\d+)?$/', '', $originalWebsite->website_name);
  2786. // 查询当前系统中已存在的克隆名称数量
  2787. $count = Website::where('website_name', 'like', $baseName . '_copy%')
  2788. ->orWhere('website_name', $baseName)
  2789. ->count();
  2790. // 构造新名称:第一次是 _copy,之后是 _copy_1, _copy_2 ...
  2791. $newName = $baseName . ($count === 0 ? '_copy' : '_copy_' . $count);
  2792. // 创建克隆网站实例
  2793. $clone_website = $originalWebsite->replicate();
  2794. $clone_website->website_name = $newName;
  2795. $clone_website->save();
  2796. $clone_website->website_url = "[]";
  2797. $clone_website->save();
  2798. // var_dump("返回值:",$clone_website->id);
  2799. //克隆网站基础信息 website_template_info website_category
  2800. $resultWebsiteTemplateInfo = WebsiteTemplateInfo::where([ 'website_id' => $data['website_id']])->first();
  2801. $clone_website_template_info = $resultWebsiteTemplateInfo->replicate();
  2802. $clone_website_template_info->website_id = $clone_website->id;
  2803. $clone_website_template_info->save();
  2804. //克隆网站导航 获取原始 website_id 对应的所有 WebsiteCategory 记录
  2805. $originalCategories = WebsiteCategory::where('website_id', $data['website_id'])->get();
  2806. // 克隆网站后的新 ID(假设 $clone_website->id 已存在)
  2807. $newWebsiteId = $clone_website->id;
  2808. // 遍历每条记录并克隆
  2809. foreach ($originalCategories as $category) {
  2810. // 使用 replicate() 方法创建模型副本(不包含主键)
  2811. $clonedCategory = $category->replicate();
  2812. // 修改外键字段指向新网站 ID
  2813. $clonedCategory->website_id = $newWebsiteId;
  2814. // 保存克隆后的记录到数据库
  2815. $clonedCategory->save();
  2816. }
  2817. //克隆友情链接 Link 对应的是多个
  2818. $originalLinks = Link::where('website_id', $data['website_id'])->get();
  2819. foreach ($originalLinks as $link) {
  2820. // 使用 replicate() 方法创建模型副本(不包含主键)
  2821. $clonedLink = $link->replicate();
  2822. // 修改外键字段指向新网站 ID
  2823. $clonedLink->website_id = $newWebsiteId;
  2824. // 保存克隆后的记录到数据库
  2825. $clonedLink->save();
  2826. }
  2827. //克隆网站底部信息 footer_category 表 和footer_content 表 footer_category.id = footer_content.fcat_id 都是一对多关系
  2828. $originalFooterCategories = FooterCategory::where('website_id', $data['website_id'])->get();
  2829. foreach ($originalFooterCategories as $footerCategory) {
  2830. $clonedFooterCategory = $footerCategory->replicate();
  2831. $clonedFooterCategory->website_id = $newWebsiteId;
  2832. $clonedFooterCategory->save();
  2833. $originalFooterContents = FooterContent::where('fcat_id', $footerCategory->id)->get();
  2834. foreach ($originalFooterContents as $footerContent){
  2835. $clonedFooterContent = $footerContent->replicate();
  2836. $clonedFooterContent->fcat_id = $clonedFooterCategory->id;
  2837. $clonedFooterContent->save();
  2838. }
  2839. }
  2840. Db::commit();
  2841. return Result::success([]);
  2842. } catch (\Exception $e) {
  2843. Db::rollBack();
  2844. return Result::error($e->getMessage(), 0);
  2845. }
  2846. }
  2847. }