WebsiteService.php 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808
  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\ChatRecords;
  25. use App\Model\ChatTopic;
  26. use App\Model\JobRecruiting;
  27. use App\Model\JobHunting;
  28. use App\Model\Notice;
  29. use App\Model\Complaint;
  30. use App\Model\Order;
  31. use App\Model\WebsiteImg;
  32. use App\Model\Company;
  33. use App\Model\Project;
  34. use App\Model\ChatTopicsReply;
  35. use Hyperf\HttpServer\Contract\RequestInterface;
  36. use Hyperf\DbConnection\Db;
  37. use Hyperf\RpcServer\Annotation\RpcService;
  38. use App\Tools\Result;
  39. use Carbon\Carbon;
  40. use App\Model\WebsiteCategory;
  41. use App\Model\WebsiteGroup;
  42. use App\Model\WebsiteTemplate;
  43. use App\Model\Sector;
  44. use PhpParser\Node\Stmt\Return_;
  45. use SimpleKafkaClient\Metadata\Topic;
  46. use function PHPUnit\Framework\isNull;
  47. use Overtrue\Pinyin\Pinyin;
  48. use App\Tools\buildTree;
  49. use App\Model\WhiteRouter;
  50. use App\Model\Size;
  51. use App\Tools\PinyinHelper;
  52. use Hyperf\HttpServer\Annotation\AutoController;
  53. use Hyperf\Utils\Parallel;
  54. use Hyperf\Coroutine\Concurrent;
  55. use function Hyperf\Coroutine\batch;
  56. use Swoole\Coroutine;
  57. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  58. class WebsiteService implements WebsiteServiceInterface
  59. {
  60. /**
  61. * 获取网站列表
  62. * @param string $keyword
  63. * @param int $page
  64. * @param int $pageSize
  65. * @return array
  66. */
  67. public function getWebsitetList(array $data): array
  68. {
  69. // 构建查询
  70. $query = Website::when($data, function ($query) use ($data) {
  71. if (isset($data['keyword']) && !empty($data['keyword'])) {
  72. $query->where('website.website_name', 'like', '%' . trim($data['keyword']) . '%');
  73. }
  74. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  75. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  76. }
  77. if (isset($data['city_id']) && !empty($data['city_id'])) {
  78. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  79. }
  80. });
  81. // 执行查询并获取总数和结果
  82. $queryWithJoins = $query->leftJoin("website_column", "website.website_column_id", "website_column.id")
  83. ->leftJoin("district", "district.id", "website.city_id");
  84. $count = $queryWithJoins->count();
  85. $result = $queryWithJoins->select("website.*", "website_column.column_name", "district.name as city_name")
  86. ->limit($data['pageSize'])
  87. ->offset(($data['page'] - 1) * $data['pageSize'])
  88. ->orderBy("website.updated_at", "desc")
  89. ->get();
  90. if (empty($result)) {
  91. return Result::error("没有数据", 0);
  92. }
  93. $data = [
  94. 'rows' => $result->toArray(),
  95. 'count' => $count,
  96. ];
  97. return Result::success($data);
  98. }
  99. /**
  100. * @param array $data
  101. */
  102. public function createWebsite(array $data): array
  103. {
  104. var_dump("网站数据:", $data);
  105. // $wzbs = $this->getWebsitetAdkey($data);
  106. // return Result::success($wzbs);
  107. $web = Website::where('ad_key', $data['ad_key'])->first();
  108. if (!empty($web)) {
  109. return Result::error("网站名称简写重复,请填写", 0);
  110. }
  111. $insertData = [
  112. 'website_name' => $data['website_name'],
  113. 'logo' => $data['logo'] ?? '',
  114. 'website_url' => $data['website_url'] ?? '',
  115. //'city_id'=>$data['city_id']??0,
  116. 'website_column_id' => $data['website_column_id'],
  117. 'title' => $data['title'] ?? '',
  118. 'keywords' => $data['keywords'] ?? '',
  119. 'description' => $data['description'] ?? '',
  120. 'status' => $data['status'] ?? 1,
  121. 'website_column_arr_id' => $data['website_column_arr_id'],
  122. //'city_arr_id'=>$data['city_arr_id']??[0],
  123. 'template_id' => $data['template_id'] ?? 0,
  124. 'suffix' => $data['suffix'] ?? "",
  125. 'ad_key' => $data['ad_key'] ?? "",
  126. ];
  127. $result = Website::insertGetId($insertData);
  128. if (empty($result)) {
  129. return Result::error("创建网站失败", 0);
  130. } else {
  131. return Result::success(["id" => $result]);
  132. }
  133. }
  134. /**
  135. * 修改网站头部基础信息
  136. * @param array $data
  137. */
  138. public function updateWebsite(int $id, array $data): array
  139. {
  140. $website = Website::where('id', $id)->first();
  141. if (empty($website)) {
  142. return Result::error("网站不存在", 0);
  143. }
  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. 'ad_key' => $data['ad_key'] ?? "",
  159. ];
  160. // $web_templsate = WebsiteTemplateInfo::where('website_id', $data['id'])->first();
  161. // `status` tinyint(1) DEFAULT '0' COMMENT '0:未构建 1:未应用 2:已应用',
  162. // if (!empty($web_templsate) && $web_templsate['status'] == 2) {
  163. // return Result::error("此网站已应用,不可修改基本信息", 0);
  164. // }
  165. Db::beginTransaction();
  166. try {
  167. if ($data['ad_key'] != $website['ad_key']) {
  168. $web = Website::where(function ($query) use ($data) {
  169. $query->where('id', '!=', $data['id'])
  170. ->where('ad_key', $data['ad_key']);
  171. })->first();
  172. if (!empty($web)) {
  173. Db::rollBack();
  174. return Result::error("网站名称简写重复,请填写", 0);
  175. }
  176. $ad_place = AdPlace::where('website_id', $data['id'])->select('ad_tag', 'id')->get()->toArray();
  177. if (!empty($ad_place)) {
  178. foreach ($ad_place as $key => $val) {
  179. $ad_keys = explode('_', $val['ad_tag']);
  180. $ad_keys[0] = $data['ad_key'];
  181. $ad_place[$key]['ad_tag'] = implode('_', $ad_keys);
  182. // $up_adtags = AdPlace::where('id', $val['id'])->update(['ad_tag' => $ad_tags[$key]]);
  183. }
  184. $up_adtags = AdPlace::upsert($ad_place, ['id'], ['ad_tag']);
  185. if (empty($up_adtags)) {
  186. Db::rollBack();
  187. return Result::error('广告位标识同步失败', 0);
  188. }
  189. }
  190. $template = WebsiteTemplate::where('website_id', $data['id'])->first();
  191. if (!empty($template)) {
  192. // Eloquent 没有 replace 方法,推测你可能是想替换模板中的字符串,假设是更新 content 字段
  193. $uptemplate = WebsiteTemplate::where('website_id', $data['id'])->update([
  194. 'template_data' => DB::raw("REPLACE(template_data, '{$website['ad_key']}', '{$data['ad_key']}')")
  195. ]);
  196. if (empty($uptemplate)) {
  197. Db::rollBack();
  198. return Result::error("更新模板失败!", 0);
  199. }
  200. }
  201. }
  202. // return Result::success($UP_template);
  203. $result = Website::where('id', $data['id'])->update($insertData);
  204. if (empty($result)) {
  205. Db::rollBack();
  206. return Result::error("更新网站失败!", 0);
  207. }
  208. Db::commit();
  209. return Result::success("更新成功");
  210. } catch (\Exception $e) {
  211. Db::rollBack();
  212. return Result::error($e->getMessage(), 0);
  213. }
  214. }
  215. /**
  216. * @param int $id
  217. * @return array
  218. */
  219. public function delWebsite(int $id): array
  220. {
  221. $result = Website::where('id', $id)->delete();
  222. if (empty($result)) {
  223. return Result::error("删除失败", 0);
  224. } else {
  225. return Result::success();
  226. }
  227. }
  228. /**
  229. * @param int $id
  230. * @return array
  231. */
  232. public function getWebsiteInfo(int $id): array
  233. {
  234. $where = [
  235. ['website.id', '=', $id],
  236. ];
  237. $result = Website::where($where)
  238. ->leftJoin("template", "template.id", "website.template_id")
  239. ->select("website.*", "template.template_name", "template.template_img")
  240. ->first();
  241. if (empty($result)) {
  242. return Result::error("数据不存在", 0);
  243. } else {
  244. return Result::success($result->toArray());
  245. }
  246. }
  247. /**
  248. * 查询所有的站点栏目
  249. * @return array
  250. */
  251. public function getWebsiteColumn(array $data): array
  252. {
  253. $result = WebsiteColumn::where($data)->get();
  254. if (empty($result)) {
  255. return Result::error("数据不存在", 0);
  256. } else {
  257. return Result::success($result->toArray());
  258. }
  259. }
  260. /**
  261. * @param string $keyword
  262. * @param int $page
  263. * @param int $pageSize
  264. * @return array
  265. */
  266. public function getWebsiteColumnList(array $data): array
  267. {
  268. $where = [];
  269. if (isset($data['keyword']) && $data['keyword']) {
  270. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  271. }
  272. $result = WebsiteColumn::where($where)
  273. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  274. ->select("website_column.*", "wc.column_name as parent_column_name")
  275. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  276. $count = WebsiteColumn::where($where)->count();
  277. if (empty($result)) {
  278. return Result::error("没有数据", 0);
  279. }
  280. $data = [
  281. 'rows' => $result->toArray(),
  282. 'count' => $count,
  283. ];
  284. return Result::success($data);
  285. }
  286. /**
  287. * @param array $data
  288. * @return array
  289. */
  290. public function createWebsiteColumn(array $data): array
  291. {
  292. $insertData = [
  293. 'column_name' => $data['column_name'],
  294. 'pid' => $data['pid'] ?? '',
  295. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  296. 'sort' => $data['sort'] ?? 0,
  297. 'remark' => $data['remark'] ?? '',
  298. ];
  299. $result = WebsiteColumn::insertGetId($insertData);
  300. if (empty($result)) {
  301. return Result::error("创建失败", 0);
  302. } else {
  303. return Result::success(["id" => $result]);
  304. }
  305. }
  306. /**
  307. * @param int $id
  308. * @param array $data
  309. * @return array
  310. */
  311. public function updateWebsiteColumn(int $id, array $data): array
  312. {
  313. $insertData = [
  314. 'column_name' => $data['column_name'],
  315. 'pid' => $data['pid'] ?? '',
  316. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  317. 'sort' => $data['sort'] ?? 0,
  318. 'remark' => $data['remark'] ?? '',
  319. ];
  320. $result = WebsiteColumn::where('id', $id)->update($insertData);
  321. if (empty($result)) {
  322. return Result::error("更新失败", 0);
  323. } else {
  324. return Result::success();
  325. }
  326. }
  327. /**
  328. * @param int $id
  329. * @return array
  330. */
  331. public function delWebsiteColumn(int $id): array
  332. {
  333. $list = WebsiteColumn::where(['pid' => $id])->get();
  334. if ($list) {
  335. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  336. }
  337. $result = WebsiteColumn::where('id', $id)->delete();
  338. if (empty($result)) {
  339. return Result::error("删除失败", 0);
  340. } else {
  341. return Result::success();
  342. }
  343. }
  344. /**
  345. * @param string $keyword
  346. * @param int $page
  347. * @param int $pageSize
  348. * @return array
  349. */
  350. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  351. {
  352. $where = [
  353. ['role.role_name', 'like', '%' . $keyword . '%'],
  354. ['website_role.website_id', '=', $websiteId],
  355. ];
  356. $result = WebsiteRole::where($where)
  357. ->leftJoin("role", "role.id", "website_role.role_id")
  358. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  359. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  360. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  361. if (empty($result)) {
  362. return Result::error("没有数据", 0);
  363. }
  364. $data = [
  365. 'rows' => $result->toArray(),
  366. 'count' => $count,
  367. ];
  368. return Result::success($data);
  369. }
  370. /**
  371. * @param array $data
  372. * @return array
  373. */
  374. public function createWebsiteRole(array $data): array
  375. {
  376. $insertData = [
  377. 'website_id' => $data['website_id'],
  378. 'role_id' => $data['role_id'] ?? '',
  379. ];
  380. $info = WebsiteRole::where($insertData)->first();
  381. if ($info) {
  382. return Result::error("不能重复添加角色", 0);
  383. }
  384. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  385. $insertData['type'] = $data['type'] ?? '';
  386. $result = WebsiteRole::insertGetId($insertData);
  387. if (empty($result)) {
  388. return Result::error("创建失败", 0);
  389. } else {
  390. return Result::success(["id" => $result]);
  391. }
  392. }
  393. /**
  394. * 暂时用不上
  395. * @param int $id
  396. * @param array $data
  397. * @return array
  398. */
  399. public function updateWebsiteRole(int $id, array $data): array
  400. {
  401. $insertData = [
  402. 'website_id' => $data['website_id'],
  403. 'type' => $data['type'] ?? '',
  404. ];
  405. $result = WebsiteRole::where('id', $id)->update($insertData);
  406. if (empty($result)) {
  407. return Result::error("更新失败", 0);
  408. } else {
  409. return Result::success();
  410. }
  411. }
  412. /**
  413. * @param int $id
  414. * @return array
  415. */
  416. public function delWebsiteRole(int $id): array
  417. {
  418. $result = WebsiteRole::where('id', $id)->delete();
  419. if (empty($result)) {
  420. return Result::error("删除失败", 0);
  421. } else {
  422. return Result::success();
  423. }
  424. }
  425. /**
  426. * @param string $keyword
  427. * @param int $page
  428. * @param int $pageSize
  429. * @return array
  430. */
  431. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  432. {
  433. $where = [
  434. ['website_role_user.website_id', '=', $websiteId],
  435. ['website_role_user.role_id', '=', $roleId],
  436. ];
  437. $count = WebsiteRoleUser::where($where)->count();
  438. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  439. $result = WebsiteRoleUser::where($where)
  440. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  441. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  442. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  443. ->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')
  444. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  445. if (empty($result)) {
  446. return Result::error("没有数据", 0);
  447. }
  448. $data = [
  449. 'rows' => $result->toArray(),
  450. 'count' => $count,
  451. ];
  452. return Result::success($data);
  453. }
  454. /**
  455. * @param array $data
  456. * @return array
  457. */
  458. public function createWebsiteRoleUser(array $data): array
  459. {
  460. $insertData = [
  461. 'website_id' => $data['website_id'],
  462. 'user_id' => $data['user_id'] ?? '',
  463. ];
  464. $info = WebsiteRoleUser::where($insertData)->first();
  465. if ($info) {
  466. return Result::error("不能重复添加角色用户", 0);
  467. }
  468. $insertData['role_id'] = $data['role_id'] ?? '';
  469. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  470. $insertData['type'] = $data['type'] ?? '';
  471. $result = WebsiteRoleUser::insertGetId($insertData);
  472. if (empty($result)) {
  473. return Result::error("创建失败", 0);
  474. } else {
  475. return Result::success(["id" => $result]);
  476. }
  477. }
  478. /**
  479. * @param int $id
  480. * @param array $data
  481. * @return array
  482. */
  483. public function updateWebsiteRoleUser(int $id, array $data): array
  484. {
  485. $insertData = [
  486. 'website_id' => $data['website_id'],
  487. 'type' => $data['type'] ?? '',
  488. 'type_id' => $data['type_id'] ?? '',
  489. 'role_id' => $data['role_id'] ?? '',
  490. ];
  491. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  492. if (empty($result)) {
  493. return Result::error("更新失败", 0);
  494. } else {
  495. return Result::success();
  496. }
  497. }
  498. /**
  499. * @param int $id
  500. * @return array
  501. */
  502. public function delWebsiteRoleUser(int $id): array
  503. {
  504. $result = WebsiteRoleUser::where('id', $id)->delete();
  505. if (empty($result)) {
  506. return Result::error("删除失败", 0);
  507. } else {
  508. return Result::success();
  509. }
  510. }
  511. /**
  512. * 根据域名获取网站 站点id
  513. * @param array $data
  514. * @return array
  515. */
  516. public function getWebsiteId(array $data): array
  517. {
  518. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  519. if (empty($result)) {
  520. return Result::error("查询站点失败", 0);
  521. } else {
  522. return Result::success($result);
  523. }
  524. }
  525. /**
  526. * 查询网站下面的导航
  527. * @param array $data
  528. * @return array
  529. */
  530. public function getWebsiteCategory(array $data): array
  531. {
  532. $where = [
  533. 'website_id' => $data['website_id'],
  534. ];
  535. // 使用递归查询构建树状结构
  536. $category = WebsiteCategory::where($where)
  537. ->with([
  538. 'children' => function ($query) {
  539. $query->orderBy('sort', 'asc');
  540. },
  541. ])
  542. ->orderBy('sort', 'asc')
  543. ->where('pid', 0) // 仅查询顶级节点
  544. ->get()->all();
  545. if (empty($category)) {
  546. return Result::error("查询站点栏目失败", 0);
  547. } else {
  548. $count = WebsiteCategory::where($where)->count();
  549. $result = [
  550. 'rep' => $category,
  551. 'count' => $count,
  552. ];
  553. return Result::success($result);
  554. }
  555. }
  556. /**
  557. * 查询网站的广告
  558. * @param array $data
  559. * @return array
  560. */
  561. public function getWebsiteAdvertisement(array $data): array
  562. {
  563. $where = [
  564. 'ad_place.status' => 1,
  565. ];
  566. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  567. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  568. $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
  569. // return Result::success($where);
  570. $result = AdPlace::where($where)
  571. ->leftJoin("ad", function ($join) use ($now) {
  572. $join->on("ad.pid", "=", "ad_place.id")
  573. ->where('ad.status', 1)
  574. ->where('ad.fromtime', '<=', $now)
  575. ->where('ad.totime', '>=', $now);
  576. })
  577. ->select(
  578. 'ad_place.name as place_name',
  579. 'ad_place.thumb',
  580. 'ad_place.ad_tag',
  581. 'ad_place.introduce',
  582. 'ad.name as ad_name',
  583. 'ad.image_src',
  584. 'ad.image_url',
  585. 'ad.image_alt',
  586. 'ad_place.ad_url'
  587. )
  588. ->get()->all();
  589. } else {
  590. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  591. // return Result::success($where);
  592. $result = AdPlace::where($where)
  593. ->where('ad_place.website_id', $data['website_id'])
  594. ->leftJoin("ad", function ($join) use ($now) {
  595. $join->on("ad.pid", "=", "ad_place.id")
  596. ->where('ad.status', 1)
  597. ->where('ad.fromtime', '<=', $now)
  598. ->where('ad.totime', '>=', $now);
  599. })
  600. ->select(
  601. 'ad_place.name as place_name',
  602. 'ad_place.thumb',
  603. 'ad_place.ad_tag',
  604. 'ad_place.introduce',
  605. 'ad.name as ad_name',
  606. 'ad.image_src',
  607. 'ad.image_url',
  608. 'ad.image_alt',
  609. 'ad_place.ad_url',
  610. )
  611. ->get()->all();
  612. // return Result::error("请选择广告位!",0);
  613. }
  614. if (empty($result)) {
  615. return Result::error("此广告位不存在!", 0);
  616. }
  617. return Result::success($result);
  618. }
  619. /**
  620. *
  621. * @param array $data
  622. * @return array
  623. */
  624. /**网站行政职能搜索 */
  625. public function selectWebsiteDepartment(array $data): array
  626. {
  627. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  628. if (isset($data['keyword']) && !empty($data['keyword'])) {
  629. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  630. if (empty($departments)) {
  631. $result['message'] = "未查询到与此相关职能部门";
  632. } else {
  633. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  634. $m = [
  635. 'department' => $depart,
  636. 'type' => $departments,
  637. 'count' => $count,
  638. ];
  639. }
  640. $result['sele'] = $m;
  641. return Result::success($result['sele']);
  642. }
  643. $result = $depart;
  644. return Result::success($result);
  645. }
  646. /**
  647. * 搜索地区
  648. * @param array $data
  649. * @return array
  650. */
  651. public function selectWebsiteArea(array $data): array
  652. {
  653. // return Result::success($data);
  654. $pid = $data['pid'] ?? 0;
  655. $result = District::leftJoin('district as district2', 'district.pid', '=', 'district2.id')
  656. ->where('district.pid', $pid)
  657. ->where('district.status', 1)
  658. ->select('district.*', 'district2.name as pid_name')
  659. ->orderBy('district.code')->get();
  660. if (empty($result)) {
  661. return Result::error("未查询到此地区", 0);
  662. } else {
  663. return Result::success($result);
  664. }
  665. }
  666. /**
  667. * 获取栏目
  668. * @param array $data
  669. * @return array
  670. */
  671. public function getWebsiteModelCategory(array $data): array
  672. {
  673. // return Result::success($data);
  674. $website_id = [
  675. 'website_id' => $data['website_id'],
  676. ];
  677. $website_column_arr = Website::where('id', $data['website_id'])->value('website_column_arr_id');
  678. if (empty($website_column_arr)) {
  679. return Result::error("未查询到此网站所属网系!", 0);
  680. }
  681. $website_column_arr = json_decode($website_column_arr, true);
  682. // return Result::success(!in_array(3,$website_column_arr) && $data['pid'] == 0);
  683. // 初始化 $pid 数组
  684. // $pid = [];
  685. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  686. $pidQuery = WebsiteCategory::where($website_id)
  687. ->where('pid', '!=', 0)
  688. ->orderBy('sort')
  689. ->select('pid', 'category_id', 'alias', 'aLIas_pinyin');
  690. $pid = $pidQuery->pluck('pid');
  691. $pid = array_values(array_unique($pid->toArray()));
  692. // 构建查询语句
  693. $query = WebsiteCategory::where($website_id)
  694. ->when(!in_array(3, $website_column_arr) && $data['pid'] == 0, function ($query) use ($website_column_arr) {
  695. $query->where('is_show', 1);
  696. })
  697. ->when(in_array(3, $website_column_arr) || $data['pid'] != 0, function ($query) use ($data) {
  698. $query->where('pid', $data['pid']);
  699. })
  700. ->offset($data['placeid'])
  701. ->limit($data['num'])
  702. ->orderBy('sort')
  703. ->orderBy('updated_at', 'desc');
  704. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  705. if (!empty($pid)) {
  706. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  707. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  708. } else {
  709. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  710. $query->select('website_category.*', DB::raw('0 as children_count'));
  711. }
  712. // 执行查询
  713. $placeid = $data['placeid'] - 1;
  714. $result = $query->offset($placeid)->limit($data['num'])->get();
  715. if (!empty($result)) {
  716. $pidResults = $pidQuery->get();
  717. if (isset($data['type']) && $data['type'] == 1) {
  718. $result = $result->map(function ($item) use ($pidResults) {
  719. $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
  720. if (!empty($child)) {
  721. return $child;
  722. }
  723. });
  724. // 重置索引,使 key 值从 0 开始
  725. $item->children = $children->values();
  726. return $item;
  727. });
  728. } else {
  729. $pidMap = $pidResults->keyBy('pid');
  730. $result->each(function ($record) use ($pidMap, $data) {
  731. if ($data['pid'] == 0) {
  732. $record->aLIas_pinyin = $record->path;
  733. }
  734. // $record->aLIas_pinyin = $record->path;
  735. if ($pidMap->has($record->category_id)) {
  736. $pidResult = $pidMap->get($record->category_id);
  737. $record->chilid_id = $pidResult->category_id;
  738. $record->chilid_alias = $pidResult->alias;
  739. // $record->aLIas_pinyin = $pidResult->aLIas_pinyin;
  740. }
  741. });
  742. }
  743. return Result::success($result);
  744. } else {
  745. return Result::error("本网站暂无栏目", 0);
  746. }
  747. }
  748. /**
  749. * 获取友情链接
  750. * @param array $data
  751. * @return array
  752. */
  753. public function selectWebsiteLinks(array $data): array
  754. {
  755. $where = [
  756. 'website_id' => $data['website_id'],
  757. 'status' => 1,
  758. 'type' => $data['type'],
  759. ];
  760. $num = $data['num'];
  761. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  762. if (!empty($result)) {
  763. return Result::success($result);
  764. } else {
  765. return Result::error("本网站暂无此类型友情链接", 0);
  766. }
  767. }
  768. /**
  769. * 网站首页数据统计, 管理员
  770. * @return void
  771. */
  772. public function getAdminIndex(array $data): array
  773. {
  774. var_dump("用户类型:", $data);
  775. switch ($data['type_id']) {
  776. case 10000:
  777. $res = [];
  778. //网站
  779. $res['website']['count'] = 0;
  780. $res['website']['growth_rate'] = 0;
  781. //资讯
  782. $res['article']['count'] = 0;
  783. $res['article']['growth_rate'] = 0;
  784. //导航池
  785. $res['category']['count'] = 0;
  786. $res['category']['growth_rate'] = 0;
  787. //商品
  788. $res['good']['count'] = 0;
  789. $res['book']['count'] = 0;
  790. $res['jobHunting']['count'] = 0;
  791. $res['jobRecruiting']['count'] = 0;
  792. //近一月数据
  793. $res['monthArticle'] = [];
  794. //用户类型
  795. $res['userType'] = [];
  796. $res['website']['count'] = Website::where([])->count();
  797. $res['article']['count'] = Article::whereIn('status', [1])->count();
  798. $res['category']['count'] = Category::where([])->count();
  799. $res['good']['count'] = Good::whereIn('status', [2])->count();
  800. $res['book']['count'] = Book::whereIn('status', [2])->count();
  801. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])->count();
  802. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])->count();
  803. $res['complaint']['count'] = Complaint::whereIn('status', [2])->count();
  804. $res['notice']['count'] = Notice::whereIn('status', [2])->count();
  805. //已审核数量
  806. $res['company']['count'] = Company::whereIn('status', [1])->count();
  807. $res['project']['count'] = Project::whereIn('status', [1])->count();
  808. // $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;');
  809. $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
  810. article.status =1 and article.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(article.created_at),`user`.type_id ORDER BY date ASC');
  811. $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
  812. good.status =2 and good.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(good.created_at),`user`.type_id ORDER BY date ASC');
  813. $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');
  814. $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');
  815. $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');
  816. //company project 1 已审核
  817. $res['monthCompany'] = Db::select(' SELECT DATE(company.created_at) AS date,COUNT(*) AS count ,user.type_id FROM company left JOIN user on company.user_id = user.id WHERE company.status =1 and company.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(company.created_at),`user`.type_id ORDER BY date ASC');
  818. $res['monthProject'] = Db::select(' SELECT DATE(project.created_at) AS date,COUNT(*) AS count ,user.type_id FROM project left JOIN user on project.user_id = user.id WHERE project.status =1 and project.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(project.created_at),`user`.type_id ORDER BY date ASC');
  819. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  820. //待审核数据
  821. $res['apply']['article'] = Article::where(['status' => 0])->count(); //
  822. $res['apply']['good'] = Good::where(['status' => 1])->count();
  823. $res['apply']['book'] = Book::where(['status' => 1])->count();
  824. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->count();
  825. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->count();
  826. $res['apply']['complaint'] = Complaint::where(['status' => 1])->count();
  827. $res['apply']['notice'] = Notice::where(['status' => 1])->count(); //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束',
  828. $res['apply']['order'] = Order::where(['status' => 6])->count();
  829. $res['apply']['company'] = Company::where(['status' => 0])->count();
  830. $res['apply']['project'] = Project::where(['status' => 0])->count();
  831. return Result::success($res);
  832. case 1:
  833. case 2:
  834. case 3:
  835. case 4:
  836. $res = [];
  837. if ($data['type_id'] == 4) {
  838. $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;');
  839. $res = [];
  840. //查询已审核文章
  841. $user_id = $data['user_id'];
  842. $article = Article::where('status', 1)
  843. ->where('admin_user_id', $user_id)
  844. ->orderBy('updated_at', 'desc')
  845. ->limit(10)
  846. ->get();
  847. $res['letterOfComplaintList'] = $result;
  848. $res['article'] = $article;
  849. }
  850. //查询已审核文章
  851. $user_id = $data['user_id'];
  852. $now = Carbon::now();
  853. // 系统提醒审核消息
  854. $sys_msg = [];
  855. $article = Article::where('status', 0)
  856. ->where('admin_user_id', $user_id)
  857. ->orderBy('updated_at', 'desc')
  858. ->limit(5)
  859. ->get(['id', 'title', 'updated_at'])
  860. ->map(function ($item) use ($now) {
  861. $item->class = '资讯';
  862. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  863. return $item;
  864. });
  865. var_dump($article, '---1--');
  866. $good = Good::where('status', 1)
  867. ->where('user_id', $user_id)
  868. ->orderBy('updated_at', 'desc')
  869. ->get(['id', 'name as title', 'updated_at'])
  870. ->map(function ($item) use ($now) {
  871. //去掉name的属性
  872. $item->class = '商品';
  873. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  874. return $item;
  875. });
  876. $book = Book::query()
  877. ->where('status', 1)
  878. ->where('user_id', $user_id)
  879. ->orderBy('updated_at', 'desc')
  880. ->get(['id', 'title', 'updated_at'])
  881. ->map(function ($item) use ($now) {
  882. //去掉name的属性
  883. $item->class = '书刊';
  884. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  885. return $item;
  886. });
  887. //招聘 求职
  888. $jobhunting = JobHunting::query()
  889. ->where('status', 1)
  890. ->where('user_id', $user_id)
  891. ->orderBy('updated_at', 'desc')
  892. ->get(['id', 'name as title', 'updated_at'])
  893. ->map(function ($item) use ($now) {
  894. //去掉name的属性
  895. $item->class = '求职';
  896. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  897. return $item;
  898. });
  899. $jobrecruiting = JobRecruiting::query()
  900. ->where('status', 0)
  901. ->where('user_id', $user_id)
  902. ->orderBy('updated_at', 'desc')
  903. ->get(['id', 'title', 'updated_at'])
  904. ->map(function ($item) use ($now) {
  905. //去掉name的属性
  906. $item->class = '招聘';
  907. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  908. return $item;
  909. });
  910. //通知 投诉
  911. $notice = Notice::query()
  912. ->where('status', 1)
  913. ->where('user_id', $user_id)
  914. ->orderBy('updated_at', 'desc')
  915. ->get(['id', 'title', 'updated_at'])
  916. ->map(function ($item) use ($now) {
  917. //去掉name的属性
  918. $item->class = $item->type == 1 ? "通知" : "公告";
  919. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  920. return $item;
  921. });
  922. $complaint = Complaint::query()
  923. ->where('status', 1)
  924. ->where('user_id', $user_id)
  925. ->orderBy('updated_at', 'desc')
  926. ->get(['id', 'title', 'updated_at'])
  927. ->map(function ($item) use ($now) {
  928. //去掉name的属性
  929. $item->class = "投诉";
  930. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  931. return $item;
  932. });
  933. //项目 公司 待审核 :0:未审核;1:已审核;2:已拒绝
  934. $project = Project::query()
  935. ->where('status', 0)
  936. ->where('user_id', $user_id)
  937. ->orderBy('updated_at', 'desc')
  938. ->get(['id', 'title', 'updated_at'])
  939. ->map(function ($item) use ($now) {
  940. //去掉name的属性
  941. $item->class = "项目";
  942. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  943. return $item;
  944. });
  945. $company = Company::query()
  946. ->where('status', 0)
  947. ->where('user_id', $user_id)
  948. ->orderBy('updated_at', 'desc')
  949. ->get(['id', 'title', 'updated_at'])
  950. ->map(function ($item) use ($now) {
  951. //去掉name的属性
  952. $item->class = "公司";
  953. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  954. return $item;
  955. });
  956. // var_dump($jobrecruiting, '-1--------------------11111111111111--');
  957. // var_dump($good, '-3--');
  958. // $s = $article->toArray();
  959. // var_dump($s, '=====31111111111111111');
  960. // 合并两个集合
  961. // 合并两个集合,并按 updated_at 倒序排序
  962. // $combined = $article->merge($good)->sortByDesc('updated_at');
  963. $combined = $article
  964. ->concat($good)
  965. ->concat($book)
  966. ->concat($jobhunting)
  967. ->concat($jobrecruiting)
  968. ->concat($notice)
  969. ->concat($complaint)
  970. ->concat($project)
  971. ->concat($company)
  972. ->sortByDesc(function ($item) {
  973. return Carbon::createFromFormat('Y-m-d H:i:s', $item->updated_at);
  974. });
  975. // ->sortBy('updated_at');
  976. // var_dump($combined, '--4--');
  977. // 转换为数组输出
  978. $res['sys_msg'] = $combined->values()->all();
  979. // 互动消息
  980. $chat_msg = [];
  981. // 通知投诉
  982. $notice_msg = [];
  983. // $res['sys_msg'] = $sys_msg;
  984. $chat_single_msg = ChatRecords::where('user_id', $user_id)
  985. ->where('is_read', 0)
  986. ->where('talk_type', 1)
  987. ->orderBy('updated_at', 'desc')
  988. ->limit(5)
  989. ->get(['id', 'content', 'updated_at'])
  990. ->map(function ($item) use ($now) {
  991. $item->class = "单聊";
  992. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  993. return $item;
  994. });
  995. $chat_group_msg = ChatRecords::where('user_id', $user_id)
  996. ->where('is_read', 0)
  997. ->where('talk_type', 2)
  998. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  999. ->orderBy('updated_at', 'desc')
  1000. ->limit(5)
  1001. ->get(['chat_records.id', 'content as content1', 'chat_records.updated_at', 'chat_groups.group_name as content'])
  1002. ->map(function ($item) use ($now) {
  1003. $item->class = "群聊";
  1004. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1005. return $item;
  1006. });
  1007. $topic_msg = ChatTopic::where('chat_topics.user_id', $user_id)
  1008. ->whereNotNull('chat_topics_reply.updated_at')
  1009. ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
  1010. ->select('chat_topics.id', 'chat_topics.title as content', 'chat_topics_reply.updated_at as updated_at')
  1011. ->orderBy('updated_at', 'desc')
  1012. ->limit(5)
  1013. ->get(['chat_topics.id', 'chat_topics.content', 'chat_topics.updated_at'])
  1014. ->map(function ($item) use ($now) {
  1015. $item->class = "话题";
  1016. // $item->content = $item->content;
  1017. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1018. return $item;
  1019. });
  1020. // 合并单聊和群聊消息
  1021. $chat_msg = $chat_single_msg
  1022. ->concat($chat_group_msg)
  1023. ->concat($topic_msg)
  1024. ->sortByDesc(function ($item) {
  1025. return Carbon::createFromFormat('Y-m-d H:i:s', $item->updated_at);
  1026. })->values()->all();
  1027. $res['chat_msg'] = $chat_msg;
  1028. //未读消息
  1029. $notce_recieve_msg = Notice::where('status', 1)
  1030. ->whereJsonContains('re_user_ids', $user_id)
  1031. ->where(function ($query) use ($user_id) {
  1032. $query->whereNull('read_id')
  1033. ->orWhereRaw('NOT JSON_CONTAINS(`read_id`, ?)', [$user_id]);
  1034. })
  1035. ->orderBy('updated_at', 'desc')
  1036. ->limit(5)
  1037. ->get(['id', 'title', 'updated_at'])
  1038. ->map(function ($item) use ($now) {
  1039. $item->class = "通知";
  1040. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1041. return $item;
  1042. });
  1043. $complaint_recieve_msg = Complaint::where('status', 1)
  1044. ->whereJsonContains('re_user_ids', $user_id)
  1045. ->where(function ($query) use ($user_id) {
  1046. $query->whereNull('read_id')
  1047. ->orWhereRaw('NOT JSON_CONTAINS(`read_id`, ?)', [$user_id]);
  1048. })
  1049. ->orderBy('updated_at', 'desc')
  1050. ->limit(5)
  1051. ->get(['id', 'title', 'updated_at'])
  1052. ->map(function ($item) use ($now) {
  1053. $item->class = "投诉";
  1054. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1055. return $item;
  1056. });
  1057. // 合并消息
  1058. $notice_msg = $notce_recieve_msg
  1059. ->concat($complaint_recieve_msg)
  1060. ->sortByDesc(function ($item) {
  1061. return Carbon::createFromFormat('Y-m-d H:i:s', $item->updated_at);
  1062. })->values()->all();
  1063. $res['notice_msg'] = $notice_msg;
  1064. // $res['article'] = $article;
  1065. $res['website']['count'] = Website::where([])->count();
  1066. $res['article']['count'] = Article::whereIn('status', [1])->where('admin_user_id', $user_id)->count();
  1067. $res['category']['count'] = Category::where([])->count();
  1068. $res['good']['count'] = Good::whereIn('status', [2])
  1069. ->where('user_id', $user_id)->count();
  1070. $res['book']['count'] = Book::whereIn('status', [2])
  1071. ->where('user_id', $user_id)->count();
  1072. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])
  1073. ->where('user_id', $user_id)->count();
  1074. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])
  1075. ->where('user_id', $user_id)->count();
  1076. $res['complaint']['count'] = Complaint::whereIn('status', [2])
  1077. ->where('user_id', $user_id)->count();
  1078. $res['notice']['count'] = Notice::whereIn('status', [2])
  1079. ->where('user_id', $user_id)->count();
  1080. //company project 0 待审核 1 已审核
  1081. $res['company']['count'] = Company::whereIn('status', [1])
  1082. ->where('user_id', $user_id)->count();
  1083. $res['project']['count'] = Project::whereIn('status', [1])
  1084. ->where('user_id', $user_id)->count();
  1085. //待处理的complaint
  1086. $res['complaint_deal']['count'] = Complaint::where(['status' => 2])
  1087. ->where('deal', 1)
  1088. // ->where('user_id', $user_id)
  1089. ->whereJsonContains('re_user_ids', $user_id)
  1090. ->count();
  1091. //待审核数据
  1092. $res['apply']['article'] = Article::where(['status' => 0])->where('admin_user_id', $user_id)->count(); //
  1093. $res['apply']['good'] = Good::where(['status' => 1])->where('user_id', $user_id)->count();
  1094. $res['apply']['book'] = Book::where(['status' => 1])->where('user_id', $user_id)->count();
  1095. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->where('user_id', $user_id)->count();
  1096. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->where('user_id', $user_id)->count();
  1097. $res['apply']['complaint'] = Complaint::where(['status' => 1])->where('user_id', $user_id)->count();
  1098. $res['apply']['notice'] = Notice::where(['status' => 1])->where('user_id', $user_id)->count();
  1099. //project company
  1100. $res['apply']['project'] = Project::whereIn('status', [0])
  1101. ->where('user_id', $user_id)->count();
  1102. $res['apply']['company'] = Company::whereIn('status', [0])
  1103. ->where('user_id', $user_id)->count();
  1104. return Result::success($res);
  1105. }
  1106. return [];
  1107. }
  1108. private function timeAgo(string $date, Carbon $now): string
  1109. {
  1110. $diff = (new Carbon($date))->diffInMinutes($now);
  1111. if ($diff < 1) {
  1112. return '刚刚';
  1113. } elseif ($diff < 60) {
  1114. return "{$diff} 分钟前";
  1115. } elseif ($diff < 1440) {
  1116. return (int)($diff / 60) . " 小时前";
  1117. } elseif ($diff < 2880) { // 两天内
  1118. return "1 天前";
  1119. } elseif ($diff < 7 * 1440) {
  1120. return (int)($diff / 1440) . " 天前";
  1121. } else {
  1122. return (new Carbon($date))->format('Y-m-d');
  1123. }
  1124. }
  1125. /**
  1126. * 获取分类下的模板
  1127. * @param array $data
  1128. * @return array
  1129. */
  1130. public function getTemplate(array $data): array
  1131. {
  1132. $page = $data['page'];
  1133. $pageSize = $data['pageSize'];
  1134. $where = [];
  1135. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1136. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  1137. }
  1138. $result = Template::where($where)
  1139. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  1140. $count = Template::where($where)->count();
  1141. if (empty($result)) {
  1142. return Result::error("没有数据", 0);
  1143. }
  1144. $data = [
  1145. 'rows' => $result->toArray(),
  1146. 'count' => $count,
  1147. ];
  1148. return Result::success($data);
  1149. }
  1150. /**
  1151. * 创建模板
  1152. * @param
  1153. * @return void
  1154. */
  1155. public function addTemplate(array $data): array
  1156. {
  1157. $insertData = [
  1158. 'template_name' => $data['template_name'],
  1159. 'template_img' => json_encode($data['template_img']),
  1160. 'template_class_id' => $data['template_class_id'],
  1161. ];
  1162. $result = Template::insertGetId($insertData);
  1163. if (empty($result)) {
  1164. return Result::error("创建模板失败", 0);
  1165. } else {
  1166. return Result::success(["id" => $result]);
  1167. }
  1168. }
  1169. /**
  1170. * 更新模板
  1171. * @param array $data
  1172. * @return array
  1173. */
  1174. public function upTemplate(array $data): array
  1175. {
  1176. $where = [
  1177. 'id' => $data['id'],
  1178. ];
  1179. $insertData = [
  1180. 'template_name' => $data['template_name'],
  1181. 'template_img' => json_encode($data['template_img']),
  1182. 'template_class_id' => $data['template_class_id'],
  1183. ];
  1184. $result = Template::where($where)->update($insertData);
  1185. if (empty($result)) {
  1186. return Result::error("更新模板失败", 0);
  1187. } else {
  1188. return Result::success();
  1189. }
  1190. }
  1191. /**
  1192. * 删除模板
  1193. * @param array $data
  1194. * @return array
  1195. */
  1196. public function delTemplate(array $data): array
  1197. {
  1198. $where = [
  1199. 'id' => $data['id'],
  1200. ];
  1201. $result = Template::where($where)->delete();
  1202. if (empty($result)) {
  1203. return Result::error("删除模板失败", 0);
  1204. } else {
  1205. return Result::success();
  1206. }
  1207. }
  1208. /**
  1209. * 搜索网站
  1210. * @param array $data
  1211. * @return array
  1212. */
  1213. public function websiteList(array $data): array
  1214. {
  1215. $where = [];
  1216. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1217. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1218. }
  1219. $result = Website::where($where)->get();
  1220. if (isset($data['website_column_id'])) {
  1221. $target = $data['website_column_id'];
  1222. $result = $result->filter(function ($item) use ($target) {
  1223. $arr = json_decode($item->website_column_arr_id, true);
  1224. return !in_array($target, $arr);
  1225. });
  1226. }
  1227. if ($result) {
  1228. return Result::success($result);
  1229. } else {
  1230. return Result::error("没有网站", 0);
  1231. }
  1232. }
  1233. public function addWebsiteCategory(array $data): array
  1234. {
  1235. $website_id = $data['website_id'];
  1236. $category_arr_id = $data['category_arr_id'];
  1237. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  1238. $categoryListIds = [];
  1239. if ($categoryList) {
  1240. foreach ($categoryList->toArray() as $val) {
  1241. array_push($categoryListIds, $val['id']);
  1242. }
  1243. }
  1244. $arr = [];
  1245. if ($categoryListIds) {
  1246. foreach ($categoryListIds as $v) {
  1247. $ids = $this->getUnderlingUIds(intval($v));
  1248. $ids_arr = explode(",", $ids);
  1249. array_push($arr, $ids_arr);
  1250. }
  1251. }
  1252. $mergedArray = [];
  1253. foreach ($arr as $subarray) {
  1254. $mergedArray = array_merge($mergedArray, $subarray);
  1255. }
  1256. var_dump("所有:", $arr, $mergedArray);
  1257. //查询出所有的分类进行分割插入 组装数据
  1258. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  1259. $categoryListData = $categoryListData->toArray();
  1260. $insertData = [];
  1261. if ($categoryListData) {
  1262. foreach ($categoryListData as $key => $value) {
  1263. $insertData[$key]['website_id'] = $website_id;
  1264. $insertData[$key]['name'] = $value['name'];
  1265. $insertData[$key]['sort'] = $value['sort'];
  1266. $insertData[$key]['pid'] = $value['pid'];
  1267. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  1268. $insertData[$key]['seo_title'] = $value['seo_title'];
  1269. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  1270. $insertData[$key]['seo_description'] = $value['seo_description'];
  1271. $insertData[$key]['alias'] = $value['name'];
  1272. $insertData[$key]['category_id'] = $value['id'];
  1273. }
  1274. }
  1275. $result = WebsiteCategory::insert($insertData);
  1276. var_dump("插入数据状态:", $result);
  1277. if ($result) {
  1278. return Result::success($result);
  1279. } else {
  1280. return Result::error("创建失败", 0);
  1281. }
  1282. }
  1283. /**
  1284. * 删除网站导航
  1285. * @param array $data
  1286. * @return array
  1287. */
  1288. public function delWebsiteCategory(array $data): array
  1289. {
  1290. $website_id = $data['website_id'] ?? 0;
  1291. $category_id = $data['category_id'] ?? 0;
  1292. $ids = $this->getUnderlingUIds(intval($category_id));
  1293. $ids_arr = explode(",", $ids);
  1294. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  1295. if ($result) {
  1296. return Result::success($result);
  1297. } else {
  1298. return Result::error("删除失败", 0);
  1299. }
  1300. }
  1301. /**
  1302. * 获取网站导航
  1303. * @param array $data
  1304. * @return array
  1305. */
  1306. public function getAdminWebsiteCategory(array $data): array
  1307. {
  1308. $where = [
  1309. 'website_id' => $data['website_id'],
  1310. ];
  1311. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  1312. $list = [];
  1313. if ($result) {
  1314. foreach ($result->toArray() as $val) {
  1315. array_push($list, json_decode($val['category_arr_id']));
  1316. }
  1317. }
  1318. if ($result) {
  1319. return Result::success($list);
  1320. } else {
  1321. return Result::error("查询失败", 0);
  1322. }
  1323. }
  1324. /**
  1325. * 更新网站导航
  1326. * @param array $data
  1327. * @return array
  1328. */
  1329. public function upWebsiteCategory(array $data): array
  1330. {
  1331. $pinyin = new Pinyin();
  1332. var_dump("更新数据:", $data);
  1333. Db::beginTransaction();
  1334. try {
  1335. if (!empty($data['category_arr_id'])) {
  1336. $websiteId = $data['website_id'];
  1337. // 1. 获取旧的 category_id 列表
  1338. $oldCategoryListIds = WebsiteCategory::where('website_id', $websiteId)->pluck('category_id')->toArray();
  1339. // 2. 获取新的 category_id 列表(去重)
  1340. $newCategoryListIds = [];
  1341. foreach ($data['category_arr_id'] as $arr) {
  1342. $lastId = end($arr);
  1343. $newCategoryListIds[] = $lastId;
  1344. }
  1345. $newCategoryListIds = array_unique($newCategoryListIds);
  1346. // 3. 需要删除的 category_id
  1347. $toDelete = array_diff($oldCategoryListIds, $newCategoryListIds);
  1348. if (!empty($toDelete)) {
  1349. WebsiteCategory::where('website_id', $websiteId)
  1350. ->whereIn('category_id', $toDelete)
  1351. ->delete();
  1352. }
  1353. // 4. 批量查出所有需要的 category 信息
  1354. $categories = Category::whereIn('id', $newCategoryListIds)->get()->keyBy('id');
  1355. // 5. 组装 upsertData,确保 category_id 不重复
  1356. $upsertData = [];
  1357. $handledCategoryIds = [];
  1358. foreach ($data['category_arr_id'] as $arr) {
  1359. $categoryId = end($arr);
  1360. if (!isset($categories[$categoryId]) || in_array($categoryId, $handledCategoryIds)) continue;
  1361. $info = $categories[$categoryId];
  1362. $aLIas_pinyin = $pinyin->permalink($info->name, '');
  1363. $upsertData[] = [
  1364. 'website_id' => $websiteId,
  1365. 'category_id' => $categoryId,
  1366. 'name' => $info->name ?? '',
  1367. 'alias' => $info->name ?? '',
  1368. 'aLIas_pinyin' => $aLIas_pinyin ?? '',
  1369. 'sort' => $info->sort ?? 0,
  1370. 'pid' => $info->pid ?? 0,
  1371. 'pid_arr' => $info->pid_arr ?? json_encode([]),
  1372. 'seo_title' => $info->seo_title ?? "",
  1373. 'seo_keywords' => $info->seo_keywords ?? "",
  1374. 'seo_description' => $info->seo_description ?? "",
  1375. 'is_url' => $info->is_url ?? 0,
  1376. 'web_url' => $info->web_url ?? '',
  1377. 'type' => $info->type ?? 1,
  1378. 'category_arr_id' => json_encode($arr)
  1379. ];
  1380. $handledCategoryIds[] = $categoryId;
  1381. }
  1382. // 6. upsert
  1383. if (!empty($upsertData)) {
  1384. WebsiteCategory::upsert(
  1385. $upsertData,
  1386. ['website_id', 'category_id'],
  1387. [
  1388. 'aLIas_pinyin',
  1389. 'pid',
  1390. 'pid_arr',
  1391. 'is_url',
  1392. 'web_url',
  1393. 'category_arr_id'
  1394. ]
  1395. );
  1396. }
  1397. }
  1398. //处理所有path
  1399. var_dump($data, '--------111----');
  1400. $website_id = $data['website_id'];
  1401. //获取所有数据
  1402. $result = WebsiteCategory::where(['website_id' => $websiteId, 'path' => NULL])->get()->toArray();
  1403. if (empty($result)) {
  1404. // return Result::error("没有数据", 0);
  1405. }
  1406. //处理path
  1407. foreach ($result as $key => $value) {
  1408. $array_category_arr_id = json_decode($value['category_arr_id'], true);
  1409. $result[$key]['path'] = WebsiteCategory::where('website_id', $websiteId)
  1410. ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
  1411. ->whereIn('category_id', $array_category_arr_id)
  1412. ->pluck('aLIas_pinyin')
  1413. ->implode('/');
  1414. }
  1415. //批量更新数据path
  1416. $updateData = [];
  1417. foreach ($result as $item) {
  1418. $updateData[] = [
  1419. 'id' => $item['id'],
  1420. 'path' => $item['path']
  1421. ];
  1422. }
  1423. //批量更新
  1424. DB::beginTransaction();
  1425. try {
  1426. foreach ($result as $item) {
  1427. WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
  1428. }
  1429. DB::commit();
  1430. } catch (\Exception $e) {
  1431. DB::rollBack();
  1432. throw $e; // 或者返回错误信息
  1433. }
  1434. //更换级别操作 形成新的上下级;
  1435. $website_all = WebsiteCategory::where('website_id', $website_id)
  1436. ->where('pid', '>', 0)
  1437. ->get()->toArray();
  1438. foreach ($website_all as $key => $value) {
  1439. $category_arr_id = json_decode($value['category_arr_id'], true);
  1440. $validPids = [];
  1441. $pid = 0;
  1442. foreach ($category_arr_id as $pidid => $pid) {
  1443. $exists = WebsiteCategory::where('website_id', $website_id)
  1444. ->where('category_id', $pid)->exists();
  1445. if ($exists) {
  1446. $validPids[] = $pid;
  1447. }
  1448. }
  1449. $pid = $validPids[0];
  1450. if (count($validPids) != count($category_arr_id)) {
  1451. if (count($validPids) == 1) {
  1452. $pid = 0;
  1453. }
  1454. // $category_arr_id = array_push($validPids, $value['category_id']);
  1455. WebsiteCategory::where('id', $value['id'])->update([
  1456. 'real_category_arr_id' => json_encode($validPids),
  1457. 'real_pid' => $pid,
  1458. 'path' => WebsiteCategory::where('website_id', $website_id)
  1459. ->orderByRaw('FIELD(category_id, ' . implode(',', $validPids) . ')')
  1460. ->whereIn('category_id', $validPids)
  1461. ->pluck('aLIas_pinyin')
  1462. ->implode('/'),
  1463. ]);
  1464. } else {
  1465. WebsiteCategory::where('id', $value['id'])->update([
  1466. 'real_category_arr_id' => null,
  1467. 'real_pid' => null,
  1468. 'path' => WebsiteCategory::where('website_id', $website_id)
  1469. ->orderByRaw('FIELD(category_id, ' . implode(',', $validPids) . ')')
  1470. ->whereIn('category_id', $validPids)
  1471. ->pluck('aLIas_pinyin')
  1472. ->implode('/'),
  1473. ]);
  1474. }
  1475. }
  1476. Db::commit();
  1477. var_dump("更新数据1:", $data);
  1478. return Result::success();
  1479. } catch (\Throwable $ex) {
  1480. Db::rollBack();
  1481. return Result::error("修改失败" . $ex->getMessage(), 0);
  1482. }
  1483. }
  1484. /**
  1485. * 获取网站列表
  1486. * @param array $data
  1487. * @return array
  1488. */
  1489. public function getWebsiteCategoryList(array $data): array
  1490. {
  1491. $where = [];
  1492. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1493. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1494. }
  1495. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1496. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1497. }
  1498. $result = Website::where($where)
  1499. ->with(["websiteCategory" => function ($query) {
  1500. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1501. }])
  1502. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1503. ->get();
  1504. $count = Website::where($where)->count();
  1505. if (empty($result)) {
  1506. return Result::error("没有数据", 0);
  1507. }
  1508. $data = [
  1509. 'rows' => $result->toArray(),
  1510. 'count' => $count,
  1511. ];
  1512. if ($result) {
  1513. return Result::success($data);
  1514. } else {
  1515. return Result::error("查询失败", 0);
  1516. }
  1517. }
  1518. /**
  1519. * 删除网站下的所有导航
  1520. * @param array $data
  1521. * @return array
  1522. */
  1523. public function delWebsiteAllCategory(array $data): array
  1524. {
  1525. $website_id = $data['website_id'];
  1526. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1527. if ($result) {
  1528. return Result::success($result);
  1529. } else {
  1530. return Result::error("删除失败", 0);
  1531. }
  1532. }
  1533. /**
  1534. * 获取网站下的某一个导航
  1535. * @param array $data
  1536. * @return array
  1537. */
  1538. public function getWebsiteCategoryOnes(array $data): array
  1539. {
  1540. $website_id = $data['website_id'];
  1541. $category_id = $data['category_id'];
  1542. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1543. ->first();
  1544. if ($result) {
  1545. return Result::success($result);
  1546. } else {
  1547. return Result::error("查询失败", 0);
  1548. }
  1549. }
  1550. /**
  1551. * 更新网闸下的某一个导航
  1552. * @param array $data
  1553. * @return array
  1554. */
  1555. public function upWebsiteCategoryones(array $data): array
  1556. {
  1557. //处理所有数据
  1558. //获取所有数据
  1559. if (isset($data['all']) && $data['all'] == 'all') {
  1560. $result = WebsiteCategory::get();
  1561. $result = $result->toArray();
  1562. var_dump($result, '------------');
  1563. $pinyin = new Pinyin();
  1564. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1565. for ($i = 0; $i < count($result); $i++) {
  1566. // $randss = substr(str_shuffle($letters), 0, 4);
  1567. $result[$i]['aLIas_pinyin'] = $pinyin->permalink($result[$i]['alias'], '');
  1568. WebsiteCategory::where(['id' => $result[$i]['id']])->update(['aLIas_pinyin' => $result[$i]['aLIas_pinyin']]);
  1569. }
  1570. var_dump($result, '--------222----');
  1571. if ($result) {
  1572. return Result::success($result);
  1573. } else {
  1574. return Result::error("更新失败", 0);
  1575. }
  1576. }
  1577. //处理所有path
  1578. if (isset($data['pathone']) && $data['pathone'] == 'pathone') {
  1579. var_dump($data, '--------111----');
  1580. $website_id = $data['website_id'];
  1581. //获取所有数据
  1582. $result = WebsiteCategory::where(['website_id' => $website_id, 'path' => NULL])->get()->toArray();
  1583. if (empty($result)) {
  1584. return Result::error("没有数据", 0);
  1585. }
  1586. //处理path
  1587. foreach ($result as $key => $value) {
  1588. $array_category_arr_id = json_decode($value['category_arr_id'], true);
  1589. $result[$key]['path'] = WebsiteCategory::where('website_id', $website_id)
  1590. ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
  1591. ->whereIn('category_id', $array_category_arr_id)
  1592. ->pluck('aLIas_pinyin')
  1593. ->implode('/');
  1594. }
  1595. //批量更新数据path
  1596. $updateData = [];
  1597. foreach ($result as $item) {
  1598. $updateData[] = [
  1599. 'id' => $item['id'],
  1600. 'path' => $item['path']
  1601. ];
  1602. }
  1603. //批量更新
  1604. DB::beginTransaction();
  1605. try {
  1606. foreach ($result as $item) {
  1607. WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
  1608. }
  1609. DB::commit();
  1610. } catch (\Exception $e) {
  1611. DB::rollBack();
  1612. throw $e; // 或者返回错误信息
  1613. }
  1614. return Result::success($result);
  1615. }
  1616. if (isset($data['pathall']) && $data['pathall'] == 'pathall') {
  1617. //获取所有数据
  1618. $result = WebsiteCategory::where(['path' => NULL])->get()->toArray();
  1619. if (empty($result)) {
  1620. return Result::error("没有数据", 0);
  1621. }
  1622. //处理path
  1623. foreach ($result as $key => $value) {
  1624. if (empty($value['category_arr_id'])) continue;
  1625. if ($key >= 4) { // 改为 >= 4 如果你想处理最多4个元素
  1626. // break;
  1627. }
  1628. $array_category_arr_id = json_decode($value['category_arr_id'], true);
  1629. $result[$key]['path'] = WebsiteCategory::where('website_id', $value['website_id'])
  1630. ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
  1631. ->whereIn('category_id', $array_category_arr_id)
  1632. ->pluck('aLIas_pinyin')
  1633. ->implode('/');
  1634. }
  1635. //批量更新数据path
  1636. $updateData = [];
  1637. foreach ($result as $item) {
  1638. $updateData[] = [
  1639. 'id' => $item['id'],
  1640. 'category_arr_id' => $item['category_arr_id'],
  1641. 'path' => $item['path']
  1642. ];
  1643. }
  1644. // var_dump($updateData);
  1645. // return Result::success($updateData);
  1646. //批量更新
  1647. DB::beginTransaction();
  1648. try {
  1649. foreach ($result as $item) {
  1650. WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
  1651. }
  1652. DB::commit();
  1653. } catch (\Exception $e) {
  1654. DB::rollBack();
  1655. throw $e; // 或者返回错误信息
  1656. }
  1657. return Result::success($result);
  1658. }
  1659. $where = [
  1660. 'website_id' => $data['website_id'],
  1661. 'category_id' => $data['category_id'],
  1662. ];
  1663. $alias = $data['alias'];
  1664. $pinyin = new Pinyin();
  1665. $aLIas_pinyin = $pinyin->permalink($alias, '');
  1666. //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
  1667. $result = WebsiteCategory::where($where)->first();
  1668. if (empty($result)) {
  1669. return Result::error("没有数据", 0);
  1670. }
  1671. $result = $result->toArray();
  1672. $oldAliasPinyin = $data['alias_pinyin']; // 修正键名大小写
  1673. var_dump($oldAliasPinyin, $result, '----old-new---');
  1674. //+四位随机字母
  1675. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1676. // 打乱字符串顺序并截取前四位
  1677. // $randss = substr(str_shuffle($letters), 0, 4);
  1678. $data['aLIas_pinyin'] = $aLIas_pinyin;
  1679. //数据库先更新一下,再改path
  1680. WebsiteCategory::where($where)->update(['aLIas_pinyin' => $data['aLIas_pinyin']]);
  1681. //处理path
  1682. $category_arr_id = json_decode($result['category_arr_id'], true);
  1683. //取出来设置path
  1684. $path = WebsiteCategory::where('website_id', $data['website_id'])
  1685. ->orderByRaw('FIELD(category_id, ' . implode(',', $category_arr_id) . ')')
  1686. ->whereIn('category_id', $category_arr_id)
  1687. ->pluck('aLIas_pinyin')
  1688. ->implode('/');
  1689. $data['path'] = $path;
  1690. //处理包含此id的所有path
  1691. // 取出category_arr_id包含此id的所有数据
  1692. $all_other = WebsiteCategory::where('website_id', $data['website_id'])
  1693. ->whereJsonContains('category_arr_id', $data['category_id'])
  1694. ->get();
  1695. // var_dump($all_other, '------------');
  1696. // 遍历所有数据,更新path
  1697. $newAliasPinyin = $aLIas_pinyin; // 添加空值判断
  1698. // 收集需要更新的ID和路径
  1699. $updateData = [];
  1700. foreach ($all_other as $item) {
  1701. $newPath = str_replace($oldAliasPinyin, $newAliasPinyin, $item->path);
  1702. var_dump($newPath, '---------zz--', $item->path, $oldAliasPinyin, $newAliasPinyin);
  1703. if ($newPath !== $item->path) { // 仅在路径变化时更新
  1704. $updateData[] = [
  1705. 'id' => $item->id,
  1706. // 'category_id' => $item->category_id,
  1707. 'path' => $newPath
  1708. ];
  1709. }
  1710. }
  1711. var_dump($updateData, '-----------1-');
  1712. // 批量更新提高性能
  1713. if (!empty($updateData)) {
  1714. // 假设模型对应的数据表为 'table_name',请替换为实际表名
  1715. // WebsiteCategory::upsert($updateData, ['id'], ['path']);
  1716. foreach ($updateData as $datapath) {
  1717. WebsiteCategory::updateOrInsert(
  1718. ['id' => $datapath['id']], // 匹配条件
  1719. ['path' => $datapath['path']] // 更新字段
  1720. );
  1721. }
  1722. }
  1723. $result = WebsiteCategory::where($where)->update($data);
  1724. if ($result) {
  1725. return Result::success($result);
  1726. } else {
  1727. return Result::error("更新失败", 0);
  1728. }
  1729. }
  1730. /**
  1731. * 获取网站下的所有导航(包含子导航)
  1732. * @param array $data
  1733. * @return array
  1734. */
  1735. public function getWebsiteAllCategory(array $data): array
  1736. {
  1737. $where = [];
  1738. var_dump($data, '---1');
  1739. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1740. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1741. }
  1742. // if (isset($data['name']) && !empty($data['name'])) {
  1743. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1744. // }
  1745. // if (isset($data['alias']) && !empty($data['alias'])) {
  1746. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1747. // }
  1748. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1749. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1750. // }
  1751. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1752. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1753. // }
  1754. if (isset($data['is_show'])) {
  1755. array_push($where, ['website_category.is_show', '=', $data['is_show']]);
  1756. }
  1757. var_dump($where, '---2');
  1758. $result_real = WebsiteCategory::where($where)
  1759. // ->where('website_category.is_show', 1) //显示
  1760. ->whereNotNull('website_category.real_pid') //只显示顶级
  1761. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1762. ->select(
  1763. "website_category.*",
  1764. DB::raw('real_pid AS pid'),
  1765. DB::raw('real_category_arr_id AS category_arr_id'),
  1766. "category.name as name"
  1767. )
  1768. ->orderBy("sort", "asc")
  1769. ->orderBy("updated_at", "desc")
  1770. ->get()->toArray();
  1771. $result = WebsiteCategory::where($where)
  1772. // ->where('website_category.is_show', 1) //显示
  1773. ->whereNull('website_category.real_pid') //只显示顶级
  1774. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1775. ->select(
  1776. "website_category.*",
  1777. // DB::raw('real_pid AS pid'),
  1778. // DB::raw('real_category_arr_id AS category_arr_id'),
  1779. "category.name as name"
  1780. )
  1781. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1782. // ->leftJoin("department", 'category.department_id', 'department.id')
  1783. // ->leftJoin("district", 'category.city_id', 'district.id')
  1784. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1785. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1786. ->orderBy("sort", "asc")
  1787. ->orderBy("updated_at", "desc")
  1788. ->get()->toArray();
  1789. $result = array_merge($result, $result_real);
  1790. // var_dump($result, '-----------1-');
  1791. if (empty($result)) {
  1792. return Result::error("没有数据", 0);
  1793. }
  1794. if ($result) {
  1795. return Result::success($result);
  1796. } else {
  1797. return Result::error("查询失败", 0);
  1798. }
  1799. }
  1800. /**
  1801. * 递归查询数据
  1802. * @param $id
  1803. * @param $ids
  1804. * @return string
  1805. */
  1806. public function getUnderlingUIds($id, $ids = '')
  1807. {
  1808. $back = Category::where(['pid' => $id])->get();
  1809. $back = $back->toArray();
  1810. if (!empty($back) && is_array($back)) {
  1811. foreach ($back as $v) {
  1812. //防止当前人的ID重复去查询,形成恶性循环
  1813. if ($v['id'] == $id) {
  1814. continue;
  1815. }
  1816. $back2 = Category::where(['pid' => $id])->count('id');
  1817. if ($back2 > 0) {
  1818. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1819. } else {
  1820. $ids .= ',' . $v['id'];
  1821. }
  1822. }
  1823. }
  1824. $ids = $id . ',' . $ids . ',';
  1825. $ids = str_replace(',,', ",", $ids);
  1826. $ids = trim($ids, ',');
  1827. return $ids;
  1828. }
  1829. /**
  1830. * 检测网站名称是否重复
  1831. * @param array $data
  1832. * @return array
  1833. */
  1834. public function checkWebsiteName(array $data): array
  1835. {
  1836. if (isset($data['id'])) {
  1837. $data[] = ['id', "!=", $data['id']];
  1838. unset($data['id']);
  1839. }
  1840. $websiteInfo = Website::query()->where($data)->first();
  1841. if (empty($websiteInfo)) {
  1842. return Result::error("找不到网站", 0);
  1843. }
  1844. return Result::success($websiteInfo->toArray());
  1845. }
  1846. /**
  1847. * 检测网站url是否重复
  1848. * @param array $data
  1849. * @return array
  1850. */
  1851. public function checkWebsiteUrl(array $data): array
  1852. {
  1853. $whereData = [];
  1854. if (isset($data['id'])) {
  1855. $whereData = [['id', "!=", $data['id']]];
  1856. unset($data['id']);
  1857. }
  1858. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1859. if (empty($websiteInfo)) {
  1860. return Result::error("找不到URL", 0);
  1861. }
  1862. return Result::success($websiteInfo->toArray());
  1863. }
  1864. /**
  1865. * 获取网站底部基础信息
  1866. * @param array $data
  1867. * @return array
  1868. */
  1869. public function getWebsiteFootInfo(array $data): array
  1870. {
  1871. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1872. $website_head = Website::where('id', $data['website_id'])
  1873. ->where('status', 1)
  1874. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1875. ->first();
  1876. if (empty($website_head)) {
  1877. return Result::error("找不到网站", 0);
  1878. }
  1879. } else {
  1880. return Result::error("参数错误", 0);
  1881. }
  1882. $website_head = Website::where('id', $data['website_id'])
  1883. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1884. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1885. if (empty($website_foot)) {
  1886. return Result::error("暂无底部基础信息", 0);
  1887. }
  1888. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1889. $result = [
  1890. 'website_foot' => $website_foot,
  1891. 'website_head' => $website_head,
  1892. ];
  1893. return Result::success($result);
  1894. }
  1895. /**
  1896. * 获取网站底部导航
  1897. * @param array $data
  1898. * @return array
  1899. */
  1900. public function getWebsiteFooterCategory(array $data): array
  1901. {
  1902. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1903. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1904. if (empty($website)) {
  1905. return Result::error("找不到网站", 0);
  1906. }
  1907. } else {
  1908. return Result::error("参数错误", 0);
  1909. }
  1910. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1911. if (empty($result)) {
  1912. return Result::error("暂无底部导航", 0);
  1913. }
  1914. return Result::success($result->toArray());
  1915. }
  1916. /**
  1917. * 获取网站底部导航列表
  1918. * @param array $data
  1919. * @return array
  1920. */
  1921. public function getWebsiteFooterCategoryList(array $data): array
  1922. {
  1923. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1924. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1925. if (empty($website)) {
  1926. return Result::error("找不到网站", 0);
  1927. }
  1928. } else {
  1929. return Result::error("参数错误", 0);
  1930. }
  1931. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1932. // '底部导航类型 0:内容型;1:列表型;',
  1933. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1934. return Result::error("底部导航id错误", 0);
  1935. } else {
  1936. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1937. if ($query->count() == 0) {
  1938. return Result::error("暂无底部导航列表", 0);
  1939. } elseif ($query->count() == 1) {
  1940. $result = $query->first();
  1941. } else {
  1942. $result = $query->get();
  1943. }
  1944. }
  1945. return Result::success($result);
  1946. }
  1947. /**
  1948. * 获取网站底部导航
  1949. * @param array $data
  1950. * @return array
  1951. */
  1952. public function getWebsiteFooterCategoryInfo(array $data): array
  1953. {
  1954. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1955. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1956. if (empty($website)) {
  1957. return Result::error("找不到网站", 0);
  1958. }
  1959. } else {
  1960. return Result::error("参数错误", 0);
  1961. }
  1962. if (isset($data['type']) && $data['type'] == 0) {
  1963. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1964. if (empty($fcatid)) {
  1965. return Result::error("底部导航id错误", 0);
  1966. }
  1967. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1968. } else {
  1969. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1970. }
  1971. if (empty($result)) {
  1972. return Result::error("暂无底部导航内容", 0);
  1973. }
  1974. return Result::success($result);
  1975. }
  1976. /*
  1977. * 搜索网站二级导航
  1978. * @param array $data
  1979. * @return array
  1980. * */
  1981. public function selectWebsiteCategory(array $data): array
  1982. {
  1983. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1984. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1985. if (empty($website)) {
  1986. return Result::error("暂无该网站", 0);
  1987. }
  1988. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1989. // return Result::success($category);
  1990. if (empty($category)) {
  1991. return Result::error("暂无此导航", 0);
  1992. }
  1993. $query = Category::whereIn('id', $category);
  1994. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1995. $cityid = District::where('id', $data['cityid'])->first();
  1996. if (empty($cityid)) {
  1997. return Result::error("暂无此城市", 0);
  1998. } else {
  1999. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  2000. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  2001. if (empty($city_category)) {
  2002. return Result::error("暂无此城市下的导航", 0);
  2003. }
  2004. $result['catid'] = $city_category;
  2005. $city = 1;
  2006. // var_dump("城市====================",$result);
  2007. }
  2008. }
  2009. if (isset($data['department_id']) && !empty($data['department_id'])) {
  2010. $departmentid = Department::where('id', $data['department_id'])->first();
  2011. if (empty($departmentid)) {
  2012. return Result::error("暂无此部门", 0);
  2013. } else {
  2014. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  2015. if (empty($depart_category)) {
  2016. return Result::error("暂无此部门下的导航", 0);
  2017. }
  2018. $result['catid'] = $depart_category;
  2019. // var_dump("职能部门*******************",$result);
  2020. $department = 1;
  2021. }
  2022. }
  2023. if (!empty($city) && !empty($department)) {
  2024. // var_dump("城市和职能部门----------------------",$result);
  2025. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  2026. if (empty($sel_category)) {
  2027. return Result::error("暂无此城市和职能部门下的导航", 0);
  2028. }
  2029. $result['catid'] = $sel_category;
  2030. }
  2031. if (empty($result)) {
  2032. return Result::error("暂无导航", 0);
  2033. }
  2034. // department_id
  2035. } else {
  2036. return Result::error("参数错误", 0);
  2037. }
  2038. return Result::success($result);
  2039. }
  2040. /**
  2041. * 获取网站栏目seo
  2042. * @param array $data
  2043. * @return array
  2044. */
  2045. public function getWebsiteCategoryHead(array $data): array
  2046. {
  2047. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2048. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2049. if (empty($website)) {
  2050. return Result::error("找不到网站", 0);
  2051. }
  2052. } else {
  2053. return Result::error("参数错误", 0);
  2054. }
  2055. if (isset($data['catid']) && !empty($data['catid'])) {
  2056. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  2057. }
  2058. if (empty($result)) {
  2059. return Result::error("暂无导航", 0);
  2060. }
  2061. $result['aLIas_pinyin'] = $result['path'] ?? '';
  2062. $result['website_name'] = $website['website_name'] ?? '';
  2063. $result['suffix'] = $website['suffix'] ?? '';
  2064. return Result::success($result);
  2065. }
  2066. /*
  2067. * 获取某个栏目
  2068. * @param array $data
  2069. * @return array
  2070. * */
  2071. public function getOneWebsiteCategory(array $data): array
  2072. {
  2073. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2074. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2075. if (empty($website)) {
  2076. return Result::error("暂无该网站", 0);
  2077. }
  2078. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  2079. if (empty($category)) {
  2080. return Result::error("暂无此导航", 0);
  2081. }
  2082. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  2083. $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();
  2084. // return Result::success($parent);
  2085. // $category['aLIas_pinyin'] = $category['path'] ?? '';
  2086. $category['parent_id'] = $parent['parent_id'] ?? '';
  2087. $category['parent_pinyin'] = $parent['aLIas_pinyin'] ?? '';
  2088. $category['parent_name'] = $parent['parent_name'] ?? '';
  2089. return Result::success($category);
  2090. } else {
  2091. return Result::error("参数错误", 0);
  2092. }
  2093. }
  2094. // 自助建站--fr-------------------
  2095. /**
  2096. * 获取并搜索 网站模板信息
  2097. * @param array $data
  2098. * @return array
  2099. */
  2100. public function getWebsiteintel(array $data): array
  2101. {
  2102. //查询所有网站模板信息
  2103. $query = Website::leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  2104. ->leftJoin("template", "template.template_id", "website_template_info.template_id")
  2105. ->leftJoin("template_class", "template_class.class_id", "template.template_class_id")
  2106. ->leftJoin('website_column', 'website_column.id', 'website.website_column_id')
  2107. ->leftJoin('website_column as parent_column', function ($join) {
  2108. $join->on('website_column.pid', '=', 'parent_column.id')
  2109. ->where('website_column.pid', 2);
  2110. })
  2111. ->whereRaw('NOT JSON_CONTAINS(website_column_arr_id, \'3\')')
  2112. ->select(
  2113. "website_template_info.id",
  2114. "template_class.name",
  2115. "website.id as website_id",
  2116. "website.website_name",
  2117. "website.website_url",
  2118. "website_template_info.action_id",
  2119. "website_template_info.created_at",
  2120. "website_template_info.updated_at",
  2121. "website_template_info.page_type",
  2122. 'website_column.column_name',
  2123. 'parent_column.column_name as parent_column_name',
  2124. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  2125. "template.template_name"
  2126. );
  2127. //若存在条件;则在$query的基础上进行筛选
  2128. if (isset($data['website_name']) && !empty($data['website_name'])) {
  2129. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  2130. }
  2131. if (isset($data['status']) && !empty($data['status'])) {
  2132. $query->where('website_template_info.status', $data['status']);
  2133. }
  2134. $count = $query->count();
  2135. $query->limit($data['pageSize'])
  2136. ->offset(($data['page'] - 1) * $data['pageSize'])
  2137. ->orderBy("website_template_info.updated_at", "desc");
  2138. $rep = $query->get();
  2139. if ($rep->count() == 0) {
  2140. return Result::error("没有查找到相关数据", 0);
  2141. } else {
  2142. $rep->each(function ($item) {
  2143. if (!empty($item->page_type)) {
  2144. $pageTypeArray = json_decode($item->page_type, true);
  2145. if (is_array($pageTypeArray)) {
  2146. $item->page_type = $pageTypeArray;
  2147. }
  2148. }
  2149. });
  2150. $result = [
  2151. "rows" => $rep->toArray(),
  2152. "count" => $count,
  2153. ];
  2154. return Result::success($result);
  2155. }
  2156. // return Result::success($result);
  2157. }
  2158. /**
  2159. * 自助建站-验证搭建网站
  2160. * @param array $data
  2161. * @return array
  2162. */
  2163. public function checkWebsiteBuild(array $data): array
  2164. {
  2165. $website = Website::where('website.id', $data['website_id'])->first();
  2166. if (empty($website)) {
  2167. return Result::error("请输入正确的网站id!", 0);
  2168. }
  2169. if (empty($website['website_column_id'])) {
  2170. return Result::error("请先关联导航池!", 0);
  2171. }
  2172. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  2173. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  2174. if (empty($foot_type)) {
  2175. return Result::error("请先关联单页!", 0);
  2176. }
  2177. // if (!in_array(1, $foot_type) ) {
  2178. // $missingTypes[] = "列表型底部导航";
  2179. // }
  2180. if (!in_array(0, $foot_type) ) {
  2181. $missingTypes = "单页(详情)";
  2182. }
  2183. if (!empty($missingTypes)) {
  2184. $errorMessage = "请先添加" . $missingTypes . "!";
  2185. return Result::error($errorMessage, 0);
  2186. }
  2187. $result['foot_type'] = $foot_type;
  2188. // 友情链接类型'1:图片 2:文字 3:底部';
  2189. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  2190. $missingTypes = [];
  2191. if (!in_array(1, $types)) {
  2192. $missingTypes[] = "文字链接";
  2193. }
  2194. if (!in_array(2, $types)) {
  2195. $missingTypes[] = "图片链接";
  2196. }
  2197. if (!in_array(3, $types)) {
  2198. $missingTypes[] = "底部链接";
  2199. }
  2200. if (!empty($missingTypes)) {
  2201. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  2202. return Result::error($errorMessage, 0);
  2203. }
  2204. return Result::success($result);
  2205. }
  2206. /**
  2207. * 自助建站-验证编辑网站
  2208. * @return array
  2209. *
  2210. */
  2211. public function checkWebsiteEdit(array $data): array
  2212. {
  2213. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])->first();
  2214. if (empty($result)) {
  2215. return Result::error("请输入正确的网站id!", 0);
  2216. }
  2217. // 0:未构建 1:未应用 2:已应用
  2218. if ($result['status'] == 2) {
  2219. return Result::error("此网站的模板正在应用中,暂不可编辑!", 0);
  2220. }
  2221. // 订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  2222. $order = Order::where('website_id', $data['website_id'])->where('status', 1)->first();
  2223. if (!empty($order)) {
  2224. return Result::error("此网站的广告位正在使用中,暂不可编辑!", 0);
  2225. }
  2226. $check = $this->checkWebsiteBuild($data);
  2227. if ($check['code'] != 200) {
  2228. return Result::error($check['message'], 0);
  2229. }
  2230. // return Result::success($check);
  2231. if ($result['page_type']) {
  2232. $result['page_type'] = json_decode($result['page_type'], true);
  2233. return Result::success('网站编辑验证通过!');
  2234. } else {
  2235. return Result::error("请先选择网站页面类型!", 0);
  2236. }
  2237. }
  2238. /**
  2239. * 修改网站基础信息
  2240. *
  2241. * @return array
  2242. */
  2243. public function upWebsiteTemplateintel(array $data): array
  2244. {
  2245. $web = Website::where('website.id', $data['website_id'])->first();
  2246. if (empty($web)) {
  2247. return Result::error("请输入正确的网站id!", 0);
  2248. }
  2249. $where = ['website_id' => $data['website_id']];
  2250. $result = WebsiteTemplateInfo::where($where)->first();
  2251. if (empty($result)) {
  2252. return Result::error("请先添加网站基础信息!", 0);
  2253. } else {
  2254. unset($data['website_id']);
  2255. // 删除重复元素
  2256. $data['page_type'] = array_unique($data['page_type']);
  2257. // 重新索引数组
  2258. $data['page_type'] = array_values($data['page_type']);
  2259. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  2260. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  2261. // 将数组转换为 JSON 字符串
  2262. $data['page_type'] = json_encode($data['page_type']) ?? '';
  2263. try {
  2264. $result = WebsiteTemplateInfo::where($where)->update($data);
  2265. } catch (\Exception $e) {
  2266. if ($e->getCode() == 22001) {
  2267. $errorMessage = $e->getMessage();
  2268. return Result::error("请检查表单,某个字段超出了长度限制!");
  2269. } else {
  2270. // 处理其他类型的数据库错误
  2271. return Result::error("修改操作失败: " . $e->getMessage());
  2272. }
  2273. return Result::error("修改失败!", 0);
  2274. }
  2275. } else {
  2276. return Result::error("请先选择首页和单页(详情)!", 0);
  2277. }
  2278. }
  2279. if (empty($result)) {
  2280. return Result::error("修改失败!", 0);
  2281. } else {
  2282. return Result::success($result);
  2283. }
  2284. }
  2285. /**
  2286. * 获取所有网站风格信息
  2287. *
  2288. * @return array
  2289. */
  2290. public function getAllTemplateClass(array $data): array
  2291. {
  2292. $result = TemplateClass::all();
  2293. if (empty($result)) {
  2294. return Result::error("没有查找到相关数据!", 0);
  2295. } else {
  2296. return Result::success($result);
  2297. }
  2298. }
  2299. /**
  2300. * 搜索并获取网站模板信息
  2301. * @param array $data
  2302. * @return array
  2303. */
  2304. public function getWebsiteTemplateList(array $data): array
  2305. {
  2306. $website = Website::where('website.id', $data['website_id'])->first();
  2307. if (empty($website)) {
  2308. return Result::error("请输入正确的网站id!", 0);
  2309. } else {
  2310. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  2311. if (empty($page_type)) {
  2312. return Result::error("此网站还未添加基础信息!", 0);
  2313. } else {
  2314. $where = json_decode($page_type['page_type'], true);
  2315. // $where = $data["page_type"];
  2316. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  2317. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  2318. if (empty($template_class)) {
  2319. return Result::error("请输入正确的模板风格id!", 0);
  2320. } else {
  2321. // 获取指定风格下的模板
  2322. $rep = Template::where('template_class_id', $data['template_class_id'])
  2323. ->where(function ($query) use ($where) {
  2324. foreach ($where as $value) {
  2325. $query->whereJsonContains('template_img', ['value' => $value]);
  2326. }
  2327. })
  2328. ->limit($data['pageSize'])
  2329. ->offset(($data['page'] - 1) * $data['pageSize'])
  2330. ->orderBy("template.updated_at", "desc")
  2331. ->get();
  2332. $count = Template::where('template_class_id', $data['template_class_id'])
  2333. ->where(function ($query) use ($where) {
  2334. foreach ($where as $value) {
  2335. $query->whereJsonContains('template_img', ['value' => $value]);
  2336. }
  2337. })->count();
  2338. }
  2339. } else {
  2340. //获取所有模板
  2341. $rep = Template::where(function ($query) use ($where) {
  2342. foreach ($where as $value) {
  2343. $query->whereJsonContains('template_img', ['value' => $value]);
  2344. }
  2345. })
  2346. ->limit($data['pageSize'])
  2347. ->offset(($data['page'] - 1) * $data['pageSize'])
  2348. ->orderBy("template.updated_at", "desc")
  2349. ->get();
  2350. $count = Template::where(function ($query) use ($where) {
  2351. foreach ($where as $value) {
  2352. $query->whereJsonContains('template_img', ['value' => $value]);
  2353. }
  2354. })->count();
  2355. }
  2356. if (empty($rep) || $rep->count() == 0) {
  2357. return Result::error("没有查找到相关数据!", 0);
  2358. } else {
  2359. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  2360. // $rep->each(function ($item) use ($where) {
  2361. // $template_img = json_decode($item->template_img, true);
  2362. // if(is_array($template_img)){
  2363. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  2364. // return isset($img['value']) && in_array($img['value'], $where);
  2365. // });
  2366. // }
  2367. // $item->template_img = $filtered_img;
  2368. // });
  2369. // $result = [
  2370. // "rows" => $rep->toArray(),
  2371. // "count" => $count
  2372. // ];
  2373. $rep->each(function ($item) use ($where) {
  2374. $template_img = json_decode($item->template_img, true);
  2375. if (is_array($template_img)) {
  2376. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  2377. return isset($img['value']) && in_array($img['value'], $where);
  2378. });
  2379. // 对过滤后的数组进行重新排序
  2380. $sorted_img = array_values($filtered_img);
  2381. $item->template_img = $sorted_img;
  2382. } else {
  2383. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  2384. }
  2385. });
  2386. $result = [
  2387. "rows" => $rep->toArray(),
  2388. "count" => $count,
  2389. ];
  2390. }
  2391. }
  2392. }
  2393. return Result::success($result);
  2394. }
  2395. /**
  2396. * 添加网站模板
  2397. * @param array $data
  2398. */
  2399. public function addWebsiteTemplateclassintel(array $data): array
  2400. {
  2401. $template = Template::where('id', $data['template_id'])->first();
  2402. $web = Website::where('id', $data['website_id'])->first();
  2403. if (empty($template)) {
  2404. return Result::error("请输入正确的模板id", 0);
  2405. }
  2406. if (empty($web)) {
  2407. return Result::error("请输入正确的网站id", 0);
  2408. }
  2409. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  2410. if (empty($result)) {
  2411. return Result::error("添加失败", 0);
  2412. } else {
  2413. return Result::success($result);
  2414. }
  2415. }
  2416. /**
  2417. * 获取网站模板信息
  2418. * @param array $data
  2419. */
  2420. public function getWebsiteTemplateclassintel(array $data): array
  2421. {
  2422. // return Result::success($data);
  2423. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2424. if (empty($template)) {
  2425. return Result::error("请输入正确的搭建网站id", 0);
  2426. }
  2427. $page_type = json_decode($template['page_type'], true);
  2428. // 获取指定网站下的基础信息和模板及风格信息
  2429. $result = WebsiteTemplateInfo::where([
  2430. ['website_template_info.website_id', $data['website_id']],
  2431. ['website_template_info.template_id', '!=', null],
  2432. ])
  2433. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  2434. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  2435. ->where(function ($query) use ($page_type) {
  2436. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  2437. foreach ($page_type as $pageType) {
  2438. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  2439. }
  2440. })
  2441. ->select(
  2442. 'template.template_name',
  2443. 'template.template_img',
  2444. 'template.id as tid',
  2445. 'template_class.name',
  2446. 'template_class.id as class_id',
  2447. 'website_template_info.page_type',
  2448. 'website_template_info.website_id'
  2449. )
  2450. ->first();
  2451. if (empty($result)) {
  2452. return Result::error("没有查找到相关数据", 0);
  2453. }
  2454. // return Result::success($result);
  2455. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  2456. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  2457. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  2458. foreach ($template_img as $key => $value) {
  2459. if (!in_array($value['value'], $page_type)) {
  2460. unset($template_img[$key]);
  2461. }
  2462. }
  2463. // return Result::success($result);
  2464. $result['template_img'] = array_values($template_img);
  2465. $result['page_type'] = $page_type;
  2466. // $templateList = $this->getWebsiteTemplateList($data);
  2467. // $count = $templateList['data']['count']??'';
  2468. // $pages = $count / $data['pageSize'];
  2469. // $found = false;
  2470. // while ($pages && !$found) {
  2471. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  2472. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  2473. // if ($index !== false) {
  2474. // $found = true;
  2475. // $result['page'] = $pages;
  2476. // $pages = 0;
  2477. // // echo "查询到的 template 在第 $page 页";
  2478. // }
  2479. // $pages--;
  2480. // }
  2481. return Result::success($result);
  2482. }
  2483. /**
  2484. * 获取网站模板下的板块信息
  2485. * @param array $data
  2486. */
  2487. public function getWebsiteSectorList(array $data): array
  2488. {
  2489. $website = Website::where('id', $data['website_id'])->first();
  2490. if (empty($website)) {
  2491. return Result::error("请输入正确的网站id", 0);
  2492. }
  2493. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2494. if (empty($template['template_id'])) {
  2495. return Result::error("请先添加网站模板", 0);
  2496. }
  2497. $query = Sector::where('template_id', $template['template_id']);
  2498. if ($query->count() == 0) {
  2499. return Result::error("没有查找到相关板块数据", 0);
  2500. } elseif ($query->count() == 1) {
  2501. $sector = $query->first();
  2502. } else {
  2503. $sector = $query->get();
  2504. }
  2505. $result['page_type'] = $template['page_type'];
  2506. $result['tid'] = $template['template_id'];
  2507. $result = [
  2508. "tid" => $template['template_id'],
  2509. "page_type" => $template['page_type'],
  2510. "sector" => $sector,
  2511. ];
  2512. return Result::success($result);
  2513. }
  2514. // 自助建站---------------fr---------------
  2515. // 网站底基础信息--fr-------------------
  2516. /**
  2517. * 添加网站底基础信息
  2518. * @param array $data
  2519. */
  2520. public function addWebFootInfo(array $data): array
  2521. {
  2522. $website = Website::where('id', $data['website_id'])->first();
  2523. if (empty($website)) {
  2524. return Result::error("请输入正确的网站id!", 0);
  2525. }
  2526. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2527. if (!empty($info)) {
  2528. return Result::error("该网站已经添加过了!", 0);
  2529. }
  2530. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  2531. if (empty($result)) {
  2532. return Result::error("请先添加网站基础信息!", 0);
  2533. } else {
  2534. return Result::success($result);
  2535. }
  2536. }
  2537. /**
  2538. * 获取网站底基础信息
  2539. * @param array $data
  2540. */
  2541. public function getWebFootInfo(array $data): array
  2542. {
  2543. $website = Website::where('id', $data['website_id'])->first();
  2544. if (empty($website)) {
  2545. return Result::error("请输入正确的网站id!", 0);
  2546. }
  2547. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2548. if (empty($result)) {
  2549. return Result::error("请先添加网站基础信息!", 0);
  2550. }
  2551. return Result::success($result);
  2552. }
  2553. /**
  2554. * 修改网站底基础信息
  2555. * @param array $data
  2556. */
  2557. public function upWebFootInfo(array $data): array
  2558. {
  2559. // return Result::success($data);
  2560. $website = Website::where('id', $data['website_id'])->first();
  2561. if (empty($website)) {
  2562. return Result::error("请输入正确的网站id!", 0);
  2563. }
  2564. $web = $data['website_id'];
  2565. unset($data['website_id']);
  2566. $where = ['website_id' => $web];
  2567. // return Result::success($data);
  2568. $result = WebsiteTemplateInfo::where($where)->first();
  2569. if (empty($result)) {
  2570. $data['website_id'] = $web;
  2571. $result = WebsiteTemplateInfo::insertGetId($data);
  2572. // return Result::error("请先添加网站基础信息!",0);
  2573. } else {
  2574. $result = WebsiteTemplateInfo::where($where)->update($data);
  2575. }
  2576. // $where = ['website_id'=>$data['website_id']];
  2577. // unset($data['website_id']);
  2578. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  2579. if (empty($result)) {
  2580. return Result::error("修改失败!", 0);
  2581. }
  2582. return Result::success($result);
  2583. }
  2584. /*
  2585. * 获取父级/子级栏目------路由
  2586. * @param array $data
  2587. * @return array
  2588. * */
  2589. public function getWebsiteParentCategory(array $data): array
  2590. {
  2591. // return Result::success($data);
  2592. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2593. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2594. }
  2595. if (empty($website)) {
  2596. return Result::error("暂无该网站", 0);
  2597. }
  2598. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2599. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2600. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2601. ->where('pid', 0)
  2602. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2603. ->get() // 添加 get() 方法获取结果集合
  2604. ->all();
  2605. // 提前查询所有父级栏目,避免在 map 中多次查询
  2606. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2607. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2608. ->where('pid', 0)
  2609. ->select('category_id', 'aLIas_pinyin')
  2610. ->get()
  2611. ->keyBy('category_id');
  2612. // 直接查询子栏目信息
  2613. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2614. ->where('pid', '!=', 0)
  2615. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2616. ->get();
  2617. // 使用集合的 map 方法处理子栏目信息
  2618. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2619. $alias = $item->alias;
  2620. $category_id = $item->category_id;
  2621. $aLIas_pinyin = $item->aLIas_pinyin;
  2622. $parent = $parentCategories->get($item->pid);
  2623. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2624. $result = [
  2625. 'name' => $alias,
  2626. 'cid' => $category_id
  2627. ];
  2628. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2629. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2630. }
  2631. return $result;
  2632. })->values()->all();
  2633. if (empty($category)) {
  2634. return Result::error("暂无此导航", 0);
  2635. }
  2636. return Result::success($category);
  2637. }
  2638. /**
  2639. * 获取网站栏目信息
  2640. * @param array $data
  2641. */
  2642. public function getWebsiteFootAll(array $data): array
  2643. {
  2644. if(!isset($data['is_diyweb']) || empty($data['is_diyweb'])){
  2645. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2646. }else{
  2647. $website = Website::where('id',$data['website_id'])->first();
  2648. }
  2649. if (empty($website)) {
  2650. return Result::error("暂无该网站", 0);
  2651. }
  2652. // 1:图片 2:文字 3:底部
  2653. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2654. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2655. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2656. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2657. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2658. if (empty($result)) {
  2659. return Result::error("暂无此网站信息", 0);
  2660. }
  2661. return Result::success($result);
  2662. }
  2663. /**
  2664. * 获取网站顶部信息
  2665. * @param array $data
  2666. */
  2667. public function getWebsiteHead(array $data): array
  2668. {
  2669. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2670. if (empty($result)) {
  2671. return Result::error("暂无该网站", 0);
  2672. }
  2673. return Result::success($result);
  2674. }
  2675. /**
  2676. * 路由匹配
  2677. * @param array $data
  2678. */
  2679. public function getWebsiteRoute(array $data): array
  2680. {
  2681. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2682. if (empty($website)) {
  2683. return Result::error("暂无该网站", 0);
  2684. }
  2685. if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
  2686. $result = WebsiteCategory::where('website_id',$data['website_id'])
  2687. ->where('path',$data['pinyin'])
  2688. // $result = WebsiteCategory::where('category_id', $category_id)
  2689. // ->where('website_category.path', $data['pinyin'])
  2690. // ->leftJoin('website_category as pc', function ($join) use ($data) {
  2691. // $join->on('pc.pid', '=', 'website_category.category_id')
  2692. // ->where('pc.website_id', '=', $data['website_id']);
  2693. // })
  2694. ->select('website_category.category_id', 'website_category.type', 'website_category.alias',)
  2695. ->first();
  2696. }
  2697. if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
  2698. $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
  2699. }
  2700. if (!isset($result) || empty($result)) {
  2701. return Result::error("暂无该导航", 0);
  2702. }
  2703. return Result::success($result);
  2704. }
  2705. /**
  2706. * 获取网站栏目信息
  2707. * @param array $data
  2708. */
  2709. public function getWebsiteAllinfo(array $data): array
  2710. {
  2711. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2712. if (empty($website)) {
  2713. return Result::error("暂无该网站", 0);
  2714. }
  2715. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2716. $website_head = Website::where('id', $data['website_id'])
  2717. ->where('status', 1)
  2718. ->select(
  2719. 'id',
  2720. 'website_name',
  2721. 'logo',
  2722. 'title',
  2723. 'keywords',
  2724. 'description',
  2725. 'suffix',
  2726. 'website_url',
  2727. 'ad_key',
  2728. 'api_url',
  2729. 'login_url',
  2730. 'weblog_url'
  2731. )
  2732. ->first();
  2733. if (empty($website_head)) {
  2734. return Result::error("找不到网站", 0);
  2735. }
  2736. } else {
  2737. return Result::error("参数错误", 0);
  2738. }
  2739. $website_foot['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2740. if (empty($website_foot)) {
  2741. return Result::error("暂无底部基础信息", 0);
  2742. }
  2743. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  2744. $result['website_head'] = $website_head;
  2745. $result['website_foot'] = $website_foot;
  2746. // return Result::success($result);
  2747. // 1:图片 2:文字 3:底部
  2748. $result['website_foot']['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2749. $result['website_foot']['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2750. $result['website_foot']['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2751. $result['website_foot']['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2752. if (empty($result)) {
  2753. return Result::error("暂无此网站信息", 0);
  2754. }
  2755. return Result::success($result);
  2756. }
  2757. //20250212 网站标识
  2758. public function addWebsiteGroup(array $data): array
  2759. {
  2760. //判斷name是不是重复
  2761. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2762. if (!empty($websiteGroupInfo)) {
  2763. return Result::error("网站标识重复", 0);
  2764. }
  2765. //添加信息
  2766. $result = WebsiteGroup::insertGetId($data);
  2767. var_dump($result);
  2768. if (empty($result)) {
  2769. return Result::error("创建失败", 0);
  2770. } else {
  2771. return Result::success($result);
  2772. }
  2773. }
  2774. public function getWebsiteGroupList(array $data): array
  2775. {
  2776. $where = [];
  2777. if (isset($data['name']) && !empty($data['name'])) {
  2778. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2779. }
  2780. $result = WebsiteGroup::where($where)
  2781. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2782. ->get();
  2783. foreach ($result as $websiteGroup) {
  2784. $webIds = json_decode($websiteGroup->web_ids, true);
  2785. $websites = Website::whereIn('id', $webIds)->get();
  2786. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2787. $websiteGroup->website_names = $websiteNames;
  2788. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2789. }
  2790. $count = WebsiteGroup::where($where)->count();
  2791. if (empty($result)) {
  2792. return Result::error("没有数据", 0);
  2793. }
  2794. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2795. }
  2796. public function getWebsiteGroupInfo(array $data): array
  2797. {
  2798. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2799. $webIds = json_decode($websiteInfo->web_ids, true);
  2800. $websites = Website::whereIn('id', $webIds)->get();
  2801. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2802. $websiteInfo->website_names = $websiteNames;
  2803. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2804. if (empty($websiteInfo)) {
  2805. return Result::error("找不到URL", 0);
  2806. }
  2807. return Result::success($websiteInfo->toArray());
  2808. }
  2809. public function updateWebsiteGroup(array $data): array
  2810. {
  2811. $where = [
  2812. 'id' => $data['id'],
  2813. ];
  2814. $insertData = [
  2815. 'name' => $data['name'],
  2816. 'web_ids' => $data['web_ids'],
  2817. ];
  2818. //判斷name是不是重复
  2819. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2820. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2821. return Result::error("网站标识重复", 0);
  2822. }
  2823. $result = WebsiteGroup::where($where)->update($insertData);
  2824. var_dump($result, '------更新');
  2825. if (empty($result)) {
  2826. return Result::error("更新失败", 0);
  2827. } else {
  2828. return Result::success();
  2829. }
  2830. }
  2831. public function deleteWebsiteGroup(array $data): array
  2832. {
  2833. $where = [
  2834. 'id' => $data['id'],
  2835. ];
  2836. //看看user表是不是有这个id
  2837. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2838. if (!empty($userGroup)) {
  2839. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2840. }
  2841. $result = WebsiteGroup::where($where)->delete();
  2842. if (empty($result)) {
  2843. return Result::error("删除失败", 0);
  2844. } else {
  2845. return Result::success();
  2846. }
  2847. }
  2848. public function getWebsiteNavList(array $data): array
  2849. {
  2850. $data['type'] = isset($data['type']) ? $data['type'] : 1;
  2851. $where = [];
  2852. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2853. array_push($where, ['website_id', '=', $data['website_id']]);
  2854. }
  2855. if (isset($data['pid'])) {
  2856. array_push($where, ['pid', '=', $data['pid']]);
  2857. }
  2858. if (isset($data['type']) && !empty($data['type'])) {
  2859. array_push($where, ['type', '=', $data['type']]);
  2860. }
  2861. // 根据站点和type获取websiteCategory 中的id
  2862. $websiteCategoryids = WebsiteCategory::query()
  2863. ->where('website_id', '=', $data['website_id'])
  2864. ->where('type', '=', $data['type'])
  2865. ->pluck('category_arr_id')->toArray();
  2866. var_dump($websiteCategoryids, '------获取websiteCategory 中的id');
  2867. //合并去重
  2868. $category_ids = [];
  2869. foreach ($websiteCategoryids as $key => $value) {
  2870. $v = json_decode($value, true);
  2871. $category_ids = array_unique(array_merge($category_ids, $v));
  2872. }
  2873. // var_dump($category_ids, '------获取websiteCategory 中的id');
  2874. $realList = WebsiteCategory::query()
  2875. ->where('website_id', '=', $data['website_id'])
  2876. ->where('is_url', '!=', 1)
  2877. ->where('real_pid', '=', $data['pid'])
  2878. ->select(
  2879. '*',
  2880. DB::raw('real_pid AS pid'),
  2881. DB::raw('real_category_arr_id AS category_arr_id')
  2882. )
  2883. ->whereIn('category_id', $category_ids)
  2884. ->get();
  2885. //根据pid取出所有的websiteCategory 中的id
  2886. $list = WebsiteCategory::query()
  2887. ->where('website_id', '=', $data['website_id'])
  2888. ->where('is_url', '!=', 1)
  2889. ->where('pid', '=', $data['pid'])
  2890. ->whereIn('category_id', $category_ids)
  2891. ->get();
  2892. $list = $list->merge($realList);
  2893. if (empty($list)) {
  2894. return Result::error("获取失败", 0);
  2895. } else {
  2896. return Result::success($list);
  2897. }
  2898. }
  2899. // 20250307 根据网站标识获和导航获取站点
  2900. public function getWebsiteNavPoolSite(array $data): array
  2901. {
  2902. $where = [];
  2903. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2904. array_push($where, ['category_id', '=', $data['category_id']]);
  2905. }
  2906. if($data['article_id'] != 0){
  2907. $list = Article::where('article.id', $data['article_id'])
  2908. ->leftJoin('website_category', 'article.catid', '=', 'website_category.category_id')
  2909. // WebsiteCategory::query()->where($where)
  2910. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2911. ->leftJoin('article_ignore', 'website_category.website_id', '=', 'article_ignore.website_id') // 左连接 article_ignore 表
  2912. ->when(isset($data['article_id']), function ($query) use ($data) {
  2913. return $query->where(function ($query) use ($data) {
  2914. $query->where('article_ignore.article_id', $data['article_id'])
  2915. ->orWhereNull('article_ignore.article_id');
  2916. });
  2917. })
  2918. ->select('website_category.*', 'website.website_name', 'article_ignore.c_show_time') // 选择需要的字段,不存在记录时 c_show_time 为 null
  2919. ->distinct()
  2920. ->get()
  2921. // ->array_map(function ($item) {
  2922. // $item['c_show_time'] = json_decode($item['category_arr_id'], true);
  2923. // return $item;
  2924. // })
  2925. ;
  2926. }else{
  2927. $list = WebsiteCategory::query()->where($where)
  2928. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2929. ->select('website_category.*', 'website.website_name')
  2930. ->distinct()
  2931. ->get();
  2932. }
  2933. if (empty($list)) {
  2934. return Result::error("获取失败", 0);
  2935. } else {
  2936. return Result::success($list);
  2937. }
  2938. }
  2939. public function getWebsiteNavPool(array $data): array
  2940. {
  2941. // 初始化查询条件数组
  2942. $query = WebsiteCategory::query();
  2943. // 如果传入了 websiteids,则添加到查询条件中
  2944. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2945. $query->whereIn('website_id', $data['websiteids']);
  2946. }
  2947. // 如果传入了 pid,则添加到查询条件中
  2948. if (isset($data['pid'])) {
  2949. $query->where('pid', '=', $data['pid']);
  2950. }
  2951. // 执行查询 唯一性 id
  2952. $list = $query->distinct()->get();
  2953. // 根据查询结果返回相应的结果
  2954. if ($list->isEmpty()) {
  2955. return Result::error("获取失败", 0);
  2956. } else {
  2957. return Result::success($list->toArray());
  2958. }
  2959. }
  2960. /**
  2961. *
  2962. * @param array $data
  2963. * @return array
  2964. */
  2965. public function getAllCategory(array $data): array
  2966. {
  2967. $total = Category::count();
  2968. $pageSize = 2000;
  2969. $pages = (int) ceil($total / $pageSize);
  2970. $results = [];
  2971. $concurrency = 5; // 最大并发数
  2972. $running = 0;
  2973. $chan = new \Swoole\Coroutine\Channel($pages);
  2974. for ($i = 0; $i < $pages; $i++) {
  2975. Coroutine::create(function () use ($i, $pageSize, $chan) {
  2976. $offset = $i * $pageSize;
  2977. $data = Category::useIndex('idx_covering')
  2978. ->orderBy('id', 'asc')
  2979. ->select('id', 'name', 'pid')
  2980. ->offset($offset)
  2981. ->limit($pageSize)
  2982. ->get()
  2983. ->toArray();
  2984. $chan->push($data);
  2985. });
  2986. }
  2987. for ($i = 0; $i < $pages; $i++) {
  2988. $res = $chan->pop();
  2989. if (is_array($res)) {
  2990. $results = array_merge($results, $res);
  2991. }
  2992. }
  2993. return Result::success($results);
  2994. }
  2995. /**
  2996. * 修改网站栏目排序
  2997. * @param array $data
  2998. * @return array
  2999. */
  3000. public function upWebsiteCategorySort(array $data): array
  3001. {
  3002. $where = [
  3003. 'website_id' => $data['website_id'],
  3004. 'id' => $data['id'],
  3005. ];
  3006. $result = WebsiteCategory::where($where)->update($data);
  3007. if ($result) {
  3008. return Result::success($result);
  3009. } else {
  3010. return Result::error("更新失败", 0);
  3011. }
  3012. }
  3013. //获取底部导航内容详情
  3014. public function getFooterContentInfo(array $data): array
  3015. {
  3016. $where = [
  3017. "id" => $data['content_id']
  3018. ];
  3019. $result = FooterContent::where($where)->first();
  3020. if ($result) {
  3021. return Result::success($result);
  3022. } else {
  3023. return Result::error("没有数据");
  3024. }
  3025. }
  3026. //获取底部导航内容列表
  3027. public function getFooterContentList(array $data): array
  3028. {
  3029. $where = [
  3030. "fcat_id" => $data['fcat_id'],
  3031. ];
  3032. switch (intval($data['type'])) {
  3033. case 0:
  3034. $result = FooterContent::where($where)->first();
  3035. break;
  3036. case 1:
  3037. $result = FooterContent::where($where)->get();
  3038. break;
  3039. }
  3040. if ($result) {
  3041. return Result::success($result);
  3042. } else {
  3043. return Result::error("没有数据");
  3044. }
  3045. }
  3046. /**
  3047. * 获取底部导航列表
  3048. * @param array $data
  3049. * @return array
  3050. */
  3051. public function getFooterCategoryList(array $data): array
  3052. {
  3053. $where = [
  3054. "website_id" => $data['website_id']
  3055. ];
  3056. $result = FooterCategory::where($where)->get();
  3057. if ($result) {
  3058. return Result::success($result);
  3059. } else {
  3060. return Result::error("没有数据");
  3061. }
  3062. }
  3063. // --自助建站-----------20250522fr----------------------start
  3064. /**
  3065. * 获取网站模板下的板块信息
  3066. * @param array $data
  3067. * @return array
  3068. */
  3069. public function getStaticResourceList(array $data): array
  3070. {
  3071. // return Result::success($data);
  3072. $where = [];
  3073. if (isset($data['img_alias']) && !empty($data['img_alias'])) {
  3074. array_push($where, ['website_img.img_alias', 'like', '%' . $data['img_alias'] . '%']);
  3075. }
  3076. if (isset($data['img_type']) && !empty($data['img_type'])) {
  3077. array_push($where, ['website_img.img_type', '=', $data['img_type']]);
  3078. }
  3079. if (isset($data['website_name']) && !empty($data['website_name'])) {
  3080. $website = Website::where('website_name', 'like', '%' . $data['website_name'] . '%')->first();
  3081. if (empty($website)) {
  3082. return Result::error("没有查找到相关数据", 0);
  3083. } else {
  3084. array_push($where, ['website_img.website_id', '=', $website['id']]);
  3085. }
  3086. }
  3087. // return Result::success($where);
  3088. $query = WebsiteImg::when(!empty($where), function ($query) use ($where) {
  3089. $query->where($where);
  3090. })
  3091. ->orderBy('updated_at', 'desc');
  3092. $count = $query->count();
  3093. $row = $query->leftJoin('website', 'website.id', '=', 'website_img.website_id')
  3094. ->select('website_img.*', 'website.website_name')
  3095. ->limit($data['pageSize'])
  3096. ->offset(($data['page'] - 1) * $data['pageSize'])
  3097. ->get();
  3098. if (empty($row)) {
  3099. return Result::error("没有查找到相关数据", 0);
  3100. } else {
  3101. $result = [
  3102. 'row' => $row,
  3103. 'count' => $count,
  3104. ];
  3105. }
  3106. return Result::success($result);
  3107. }
  3108. /**
  3109. * 添加网站模板下的板块信息
  3110. * @param array $data
  3111. */
  3112. public function addStaticResource(array $data): array
  3113. {
  3114. // $file = $data['img_url']->file('image');
  3115. // $size = $file->getSize();
  3116. $img_data = [
  3117. 'website_id' => $data['website_id'] ?? 0,
  3118. 'img_alias' => $data['img_alias'] ?? '',
  3119. 'img_url' => $data['img_url'],
  3120. 'img_size' => $data['img_size'],
  3121. ];
  3122. $img = WebsiteImg::insertGetId($img_data);
  3123. if (empty($img)) {
  3124. return Result::error("添加失败", 0);
  3125. }
  3126. $img_id['id'] = $img;
  3127. $url = WebsiteImg::where($img_id)->value('img_url');
  3128. // 1. 分割路径部分(以 / 为分隔符)
  3129. $pathSegments = explode('/', parse_url($url, PHP_URL_PATH));
  3130. // 2. 取最后一个元素作为文件名
  3131. $fileName = end($pathSegments); // 1743041040714597.jpg
  3132. // 3. 分割文件名获取扩展名
  3133. $fileParts = explode('.', $fileName);
  3134. $fileType = end($fileParts); // jpg
  3135. $img_datas = [
  3136. 'img_type' => $fileType,
  3137. 'name' => $fileName
  3138. ];
  3139. $up_img = WebsiteImg::where($img_id)->update($img_datas);
  3140. if (empty($up_img)) {
  3141. return Result::error("修改失败", 0);
  3142. }
  3143. return Result::success($up_img);
  3144. // 将字节转换为更友好的单位
  3145. // $friendlySize = $this->formatFileSize($size);
  3146. // $data['img_size'] = $friendlySize;
  3147. }
  3148. /**
  3149. * 修改网站模板下的板块信息
  3150. * @param array $data
  3151. */
  3152. public function delStaticResource(array $data): array
  3153. {
  3154. $where = [
  3155. 'id' => $data['id'],
  3156. ];
  3157. $result = WebsiteImg::where($where)->delete();
  3158. if (empty($result)) {
  3159. return Result::error("修改失败", 0);
  3160. }
  3161. return Result::success($result);
  3162. }
  3163. /**
  3164. * 获取尺寸列表
  3165. * @param array $data
  3166. */
  3167. public function getSizeList(array $data): array
  3168. {
  3169. $where = [];
  3170. if (isset($data['width']) && !empty($data['width'])) {
  3171. array_push($where, ['width', 'like', '%' . $data['width'] . '%']);
  3172. }
  3173. if (isset($data['height']) && !empty($data['height'])) {
  3174. array_push($where, ['height', 'like', '%' . $data['height'] . '%']);
  3175. }
  3176. $query = Size::when($where, function ($query) use ($where) {
  3177. $query->where($where);
  3178. })
  3179. ->orderBy('updated_at', 'desc');
  3180. $count = $query->count();
  3181. $rep = $query
  3182. ->offset(($data['page'] - 1) * $data['pageSize'])
  3183. ->limit($data['pageSize'])
  3184. ->get();
  3185. $result = [
  3186. 'row' => $rep,
  3187. 'count' => $count,
  3188. ];
  3189. if (empty($result)) {
  3190. return Result::error("获取失败", 0);
  3191. } else {
  3192. return Result::success($result);
  3193. }
  3194. }
  3195. /**
  3196. * 添加尺寸
  3197. * @param array $data
  3198. */
  3199. public function addSize(array $data): array
  3200. {
  3201. $size = Size::where('width', $data['width'])->where('height', $data['height'])->first();
  3202. if (!empty($size)) {
  3203. return Result::error("尺寸已存在", 0);
  3204. }
  3205. $result = Size::insertGetId($data);
  3206. if (empty($result)) {
  3207. return Result::error("添加失败", 0);
  3208. }
  3209. return Result::success($result);
  3210. }
  3211. /**
  3212. * 删除尺寸
  3213. * @param array $data
  3214. */
  3215. public function delSize(array $data): array
  3216. {
  3217. $where = [
  3218. 'id' => $data['id'],
  3219. ];
  3220. $result = Size::where($where)->delete();
  3221. if (empty($result)) {
  3222. return Result::error("删除失败", 0);
  3223. }
  3224. return Result::success($result);
  3225. }
  3226. /**
  3227. * 修改尺寸
  3228. * @param array $data
  3229. */
  3230. public function upSize(array $data): array
  3231. {
  3232. $where = [
  3233. 'id' => $data['id'],
  3234. ];
  3235. $size = Size::where('id', '!=', $data['id'])->where('width', $data['width'])->where('height', $data['height'])->first();
  3236. if (!empty($size)) {
  3237. return Result::error("尺寸已存在", 0);
  3238. }
  3239. $result = Size::where($where)->update($data);
  3240. if (empty($result)) {
  3241. return Result::error("修改失败", 0);
  3242. }
  3243. return Result::success($result);
  3244. }
  3245. /**
  3246. * 获取尺寸详情
  3247. * @param array $data
  3248. */
  3249. public function getSizeInfo(array $data): array
  3250. {
  3251. $where = [
  3252. 'id' => $data['id'],
  3253. ];
  3254. $result = Size::where($where)->first();
  3255. if (empty($result)) {
  3256. return Result::error("获取失败", 0);
  3257. } else {
  3258. return Result::success($result);
  3259. }
  3260. }
  3261. /**
  3262. * 获取网站名称缩写
  3263. * @param array $data
  3264. */
  3265. public function getWebsiteAdkey(array $data): array
  3266. {
  3267. $result = PinyinHelper::getFirstLetter($data['website_name']);
  3268. if (empty($result)) {
  3269. return Result::error("没有数据", 0);
  3270. } else {
  3271. return Result::success($result);
  3272. }
  3273. }
  3274. /**
  3275. * 静态资源管理-修改图片别名
  3276. * @param array $data
  3277. */
  3278. public function upStaticResource(array $data): array
  3279. {
  3280. $id = $data['id'];
  3281. unset($data['id']);
  3282. $result = WebsiteImg::where('id', $id)->update($data);
  3283. if (empty($result)) {
  3284. return Result::error("修改失败", 0);
  3285. }
  3286. return Result::success($result);
  3287. }
  3288. /**
  3289. * 自助建站-修改网站应用状态
  3290. * @param array $data
  3291. */
  3292. public function upWebsiteStatus(array $data): array
  3293. {
  3294. $where = [
  3295. 'website_id' => $data['website_id'],
  3296. ];
  3297. $website_template = WebsiteTemplate::where($where)->first();
  3298. unset($data['website_id']);
  3299. // if($data['status'] == 2){
  3300. // $template_status = 6;
  3301. // }else{
  3302. // $template_status = 1;
  3303. // }
  3304. if (empty($website_template)) {
  3305. return Result::error("网站预制模板获取失败", 0);
  3306. } else {
  3307. // Db::beginTransaction();
  3308. // try {
  3309. // $result['website_template'] = WebsiteTemplate::where($where)->update(['status'=>$template_status]);
  3310. // if (empty($result['website_template'])) {
  3311. // return Result::error("修改网站模板状态失败", 0);
  3312. // }
  3313. $result['website_template_info'] = WebsiteTemplateInfo::where($where)->update(['status' => $data['status']]);
  3314. if (empty($result['website_template_info'])) {
  3315. return Result::error("修改网站基础信息状态失败", 0);
  3316. }
  3317. // Db::commit();
  3318. return Result::success($result);
  3319. // } catch (\Exception $e) {
  3320. // Db::rollBack();
  3321. // return Result::error($e->getMessage(), 0);
  3322. // }
  3323. }
  3324. }
  3325. // --自助建站-----------20250522fr----------------------end
  3326. /**
  3327. * 获取网站白名单路由地址
  3328. * @param array $data
  3329. * @return array
  3330. */
  3331. public function getWhiteRouterList(array $data): array
  3332. {
  3333. // 构建基础查询
  3334. $baseQuery = WhiteRouter::query();
  3335. // 功能名称模糊查询
  3336. if (!empty($data['function_name'])) {
  3337. $baseQuery->where('function_name', 'like', '%' . $data['function_name'] . '%');
  3338. }
  3339. // 网站ID查询 - 使用 JSON_CONTAINS 检查数组中是否包含指定的 website_id
  3340. if (!empty($data['website_id'])) {
  3341. $baseQuery->whereRaw('JSON_CONTAINS(website_id, ?)', [json_encode((int)$data['website_id'])]);
  3342. }
  3343. // 获取总数
  3344. $count = $baseQuery->count();
  3345. // 分页查询
  3346. $result = $baseQuery->orderBy('updated_at', 'desc')
  3347. ->limit($data['pageSize'])
  3348. ->offset(($data['page'] - 1) * $data['pageSize'])
  3349. ->get();
  3350. if (empty($result)) {
  3351. return Result::error("暂无数据", 0);
  3352. }
  3353. // 处理结果,将 website_id 转换为数组并获取对应的网站名称
  3354. $result->transform(function ($item) {
  3355. $websiteIds = json_decode($item->website_id, true);
  3356. $websiteNames = Website::whereIn('id', $websiteIds)
  3357. ->pluck('website_name')
  3358. ->toArray();
  3359. $item->website_names = $websiteNames;
  3360. $item->website_id = $websiteIds; // 将 website_id 转换为数组
  3361. return $item;
  3362. });
  3363. return Result::success([
  3364. 'rows' => $result->toArray(),
  3365. 'count' => $count
  3366. ]);
  3367. }
  3368. /**
  3369. * 添加网站白名单路由地址
  3370. * @param array $data
  3371. * @return array
  3372. */
  3373. public function addWhiteRouter(array $data): array
  3374. {
  3375. // 确保 website_id 是数组
  3376. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  3377. // 过滤空值并去重
  3378. $websiteIds = array_filter($websiteIds);
  3379. $websiteIds = array_unique($websiteIds);
  3380. // 将数组中的值转换为整数
  3381. $websiteIds = array_map(function ($value) {
  3382. return (int)$value;
  3383. }, $websiteIds);
  3384. // 转换为 JSON 字符串存储
  3385. $data['website_id'] = json_encode($websiteIds);
  3386. $result = WhiteRouter::insertGetId($data);
  3387. if (empty($result)) {
  3388. return Result::error("添加失败", 0);
  3389. }
  3390. return Result::success($result);
  3391. }
  3392. /**
  3393. * 修改网站白名单路由地址
  3394. * @param array $data
  3395. * @return array
  3396. */
  3397. public function upWhiteRouter(array $data): array
  3398. {
  3399. $where = [
  3400. 'id' => $data['id'],
  3401. ];
  3402. // 确保 website_id 是数组
  3403. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  3404. // 过滤空值并去重
  3405. $websiteIds = array_filter($websiteIds);
  3406. $websiteIds = array_unique($websiteIds);
  3407. // 将数组中的值转换为整数
  3408. $websiteIds = array_map(function ($value) {
  3409. return (int)$value;
  3410. }, $websiteIds);
  3411. // 转换为 JSON 字符串存储
  3412. $data['website_id'] = json_encode($websiteIds);
  3413. $result = WhiteRouter::where($where)->update($data);
  3414. if (empty($result)) {
  3415. return Result::error("修改失败", 0);
  3416. }
  3417. return Result::success($result);
  3418. }
  3419. /**
  3420. * 删除网站白名单路由地址
  3421. * @param array $data
  3422. * @return array
  3423. */
  3424. public function delWhiteRouter(array $data): array
  3425. {
  3426. $where = [
  3427. 'id' => $data['id'],
  3428. ];
  3429. $result = WhiteRouter::where($where)->delete();
  3430. if (empty($result)) {
  3431. return Result::error("删除失败", 0);
  3432. }
  3433. return Result::success($result);
  3434. }
  3435. /**
  3436. * 获取网站白名单路由地址详情
  3437. * @param array $data
  3438. * @return array
  3439. */
  3440. public function getWhiteRouterInfo(array $data): array
  3441. {
  3442. $where = [
  3443. 'id' => $data['id'],
  3444. ];
  3445. $result = WhiteRouter::where($where)->first();
  3446. if (empty($result)) {
  3447. return Result::error("没有查找到相关数据", 0);
  3448. }
  3449. // 将 website_id 从 JSON 字符串转换为数组
  3450. $result->website_id = json_decode($result->website_id, true);
  3451. return Result::success($result);
  3452. }
  3453. /**
  3454. * 克隆网站
  3455. * @param array $data
  3456. * @return array
  3457. */
  3458. public function cloneWebsite(array $data): array
  3459. {
  3460. DB::beginTransaction();
  3461. try {
  3462. // 获取原始网站信息
  3463. $originalWebsite = Website::find($data['website_id']);
  3464. if (!$originalWebsite) {
  3465. return Result::error("找不到该网站", 0);
  3466. }
  3467. // 提取基础名称(去除已有 _copy 或 _copy_xxx)
  3468. $baseName = preg_replace('/_copy(\d+)?$/', '', $originalWebsite->website_name);
  3469. // 查询当前系统中已存在的克隆名称数量
  3470. $count = Website::where('website_name', 'like', $baseName . '_copy%')
  3471. ->orWhere('website_name', $baseName)
  3472. ->count();
  3473. // 构造新名称:第一次是 _copy,之后是 _copy_1, _copy_2 ...
  3474. $newName = $baseName . ($count === 0 ? '_copy' : '_copy_' . $count);
  3475. // 创建克隆网站实例
  3476. $clone_website = $originalWebsite->replicate();
  3477. $clone_website->website_name = $newName;
  3478. $clone_website->save();
  3479. $clone_website->website_url = "[]";
  3480. $clone_website->save();
  3481. // var_dump("返回值:",$clone_website->id);
  3482. //克隆网站基础信息 website_template_info website_category
  3483. $resultWebsiteTemplateInfo = WebsiteTemplateInfo::where(['website_id' => $data['website_id']])->first();
  3484. $clone_website_template_info = $resultWebsiteTemplateInfo->replicate();
  3485. $clone_website_template_info->status = 0;
  3486. $clone_website_template_info->action_id = 0;
  3487. $clone_website_template_info->website_id = $clone_website->id;
  3488. $clone_website_template_info->save();
  3489. //克隆网站导航 获取原始 website_id 对应的所有 WebsiteCategory 记录
  3490. $originalCategories = WebsiteCategory::where('website_id', $data['website_id'])->get();
  3491. // 克隆网站后的新 ID(假设 $clone_website->id 已存在)
  3492. $newWebsiteId = $clone_website->id;
  3493. if ($originalCategories && count($originalCategories) > 0) {
  3494. // 遍历每条记录并克隆
  3495. foreach ($originalCategories as $category) {
  3496. // 使用 replicate() 方法创建模型副本(不包含主键)
  3497. $clonedCategory = $category->replicate();
  3498. // 修改外键字段指向新网站 ID
  3499. $clonedCategory->website_id = $newWebsiteId;
  3500. // 保存克隆后的记录到数据库
  3501. $clonedCategory->save();
  3502. }
  3503. }
  3504. //克隆友情链接 Link 对应的是多个
  3505. $originalLinks = Link::where('website_id', $data['website_id'])->get();
  3506. if ($originalLinks && count($originalLinks) > 0) {
  3507. foreach ($originalLinks as $link) {
  3508. // 使用 replicate() 方法创建模型副本(不包含主键)
  3509. $clonedLink = $link->replicate();
  3510. // 修改外键字段指向新网站 ID
  3511. $clonedLink->website_id = $newWebsiteId;
  3512. // 保存克隆后的记录到数据库
  3513. $clonedLink->save();
  3514. }
  3515. }
  3516. //克隆网站底部信息 footer_category 表 和footer_content 表 footer_category.id = footer_content.fcat_id 都是一对多关系
  3517. $originalFooterCategories = FooterCategory::where('website_id', $data['website_id'])->get();
  3518. if ($originalFooterCategories && count($originalFooterCategories) > 0) {
  3519. foreach ($originalFooterCategories as $footerCategory) {
  3520. $clonedFooterCategory = $footerCategory->replicate();
  3521. $clonedFooterCategory->website_id = $newWebsiteId;
  3522. $clonedFooterCategory->save();
  3523. $originalFooterContents = FooterContent::where('fcat_id', $footerCategory->id)->get();
  3524. if ($originalFooterContents && count($originalFooterContents) > 0) {
  3525. foreach ($originalFooterContents as $footerContent) {
  3526. $clonedFooterContent = $footerContent->replicate();
  3527. $clonedFooterContent->fcat_id = $clonedFooterCategory->id;
  3528. $clonedFooterContent->save();
  3529. }
  3530. }
  3531. }
  3532. }
  3533. Db::commit();
  3534. return Result::success([]);
  3535. } catch (\Exception $e) {
  3536. Db::rollBack();
  3537. return Result::error($e->getMessage(), 0);
  3538. }
  3539. }
  3540. /**
  3541. * 更新网站状态
  3542. * @param array $data
  3543. * @return array
  3544. */
  3545. public function updateWebsiteStatus(array $data): array
  3546. {
  3547. try {
  3548. Website::where(["id" => $data['id']])->update(["status" => intval($data['status'])]);
  3549. return Result::success([]);
  3550. } catch (\Exception $e) {
  3551. return Result::error($e->getMessage(), 0);
  3552. }
  3553. }
  3554. /**
  3555. * 删除网站url
  3556. * @param array $data
  3557. * @return array
  3558. */
  3559. public function delWebsiteUrl(array $data): array
  3560. {
  3561. try {
  3562. $wensiteInfo = Website::where(["id" => $data['website_id']])->first();
  3563. if (empty($wensiteInfo)) {
  3564. return Result::error("找不到该网站", 0);
  3565. }
  3566. $websiteUrlArray = [];
  3567. if (!empty($wensiteInfo->website_url)) {
  3568. $decoded = json_decode((string) $wensiteInfo->website_url, true);
  3569. $websiteUrlArray = is_array($decoded) ? $decoded : [];
  3570. }
  3571. // 从数组中按值删除对应的 URL
  3572. $index = array_search($data['website_url'] ?? null, $websiteUrlArray, true);
  3573. if ($index !== false) {
  3574. unset($websiteUrlArray[$index]);
  3575. // 重新索引,保持为连续的 JSON 数组
  3576. $websiteUrlArray = array_values($websiteUrlArray);
  3577. }
  3578. $newWebsiteUrlJson = json_encode($websiteUrlArray, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  3579. Website::where(["id" => $data['website_id']])->update(["website_url" => $newWebsiteUrlJson]);
  3580. return Result::success([]);
  3581. } catch (\Exception $e) {
  3582. return Result::error($e->getMessage(), 0);
  3583. }
  3584. }
  3585. public function checkPath(array $data): array
  3586. {
  3587. var_dump("检查路径");
  3588. // return Result::success([]);
  3589. // website_category查找相同path的数据,返回那些数据重复了
  3590. // 查找所有具有相同path的重复记录
  3591. $duplicatePaths = WebsiteCategory::select('path', DB::raw('COUNT(*) as count'))
  3592. ->where('website_id', $data['website_id'])
  3593. ->groupBy('path')
  3594. ->havingRaw('COUNT(*) > 1')
  3595. ->get();
  3596. if ($duplicatePaths->isEmpty()) {
  3597. return Result::success([]);
  3598. }
  3599. // 获取所有重复记录的详细信息
  3600. $duplicateRecords = [];
  3601. foreach ($duplicatePaths as $duplicatePath) {
  3602. $records = WebsiteCategory::where(['path' => $duplicatePath->path])
  3603. ->where('website_id', $data['website_id'])
  3604. ->orderBy('id')
  3605. ->get();
  3606. $duplicateRecords[] = [
  3607. 'path' => $duplicatePath->path,
  3608. 'count' => $duplicatePath->count,
  3609. 'records' => $records
  3610. ];
  3611. }
  3612. return Result::success($duplicateRecords);
  3613. }
  3614. }