WebsiteService.php 115 KB

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