WebsiteService.php 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623
  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. // 初始化 $pid 数组
  678. // $pid = [];
  679. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  680. $pidQuery = WebsiteCategory::where($website_id)
  681. ->where('pid', '!=', 0)
  682. ->orderBy('sort')
  683. ->select('pid', 'category_id', 'alias', 'aLIas_pinyin');
  684. $pid = $pidQuery->pluck('pid');
  685. $pid = array_values(array_unique($pid->toArray()));
  686. // 构建查询语句
  687. $query = WebsiteCategory::where($website_id)
  688. ->where('pid', $data['pid'])
  689. ->offset($data['placeid'])
  690. ->limit($data['num'])
  691. ->orderBy('sort')
  692. ->orderBy('updated_at', 'desc');
  693. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  694. if (!empty($pid)) {
  695. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  696. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  697. } else {
  698. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  699. $query->select('website_category.*', DB::raw('0 as children_count'));
  700. }
  701. // 执行查询
  702. $placeid = $data['placeid'] - 1;
  703. $result = $query->offset($placeid)->limit($data['num'])->get();
  704. if (!empty($result)) {
  705. $pidResults = $pidQuery->get();
  706. if (isset($data['type']) && $data['type'] == 1) {
  707. $result = $result->map(function ($item) use ($pidResults) {
  708. $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
  709. if (!empty($child)) {
  710. return $child;
  711. }
  712. });
  713. // 重置索引,使 key 值从 0 开始
  714. $item->children = $children->values();
  715. return $item;
  716. });
  717. } else {
  718. $pidMap = $pidResults->keyBy('pid');
  719. $result->each(function ($record) use ($pidMap) {
  720. if ($pidMap->has($record->category_id)) {
  721. $pidResult = $pidMap->get($record->category_id);
  722. $record->chilid_id = $pidResult->category_id;
  723. $record->chilid_alias = $pidResult->alias;
  724. // $record->aLIas_pinyin = $pidResult->aLIas_pinyin;
  725. }
  726. });
  727. }
  728. return Result::success($result);
  729. } else {
  730. return Result::error("本网站暂无栏目", 0);
  731. }
  732. }
  733. /**
  734. * 获取友情链接
  735. * @param array $data
  736. * @return array
  737. */
  738. public function selectWebsiteLinks(array $data): array
  739. {
  740. $where = [
  741. 'website_id' => $data['website_id'],
  742. 'status' => 1,
  743. 'type' => $data['type'],
  744. ];
  745. $num = $data['num'];
  746. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  747. if (!empty($result)) {
  748. return Result::success($result);
  749. } else {
  750. return Result::error("本网站暂无此类型友情链接", 0);
  751. }
  752. }
  753. /**
  754. * 网站首页数据统计, 管理员
  755. * @return void
  756. */
  757. public function getAdminIndex(array $data): array
  758. {
  759. var_dump("用户类型:", $data);
  760. switch ($data['type_id']) {
  761. case 10000:
  762. $res = [];
  763. //网站
  764. $res['website']['count'] = 0;
  765. $res['website']['growth_rate'] = 0;
  766. //资讯
  767. $res['article']['count'] = 0;
  768. $res['article']['growth_rate'] = 0;
  769. //导航池
  770. $res['category']['count'] = 0;
  771. $res['category']['growth_rate'] = 0;
  772. //商品
  773. $res['good']['count'] = 0;
  774. $res['book']['count'] = 0;
  775. $res['jobHunting']['count'] = 0;
  776. $res['jobRecruiting']['count'] = 0;
  777. //近一月数据
  778. $res['monthArticle'] = [];
  779. //用户类型
  780. $res['userType'] = [];
  781. $res['website']['count'] = Website::where([])->count();
  782. $res['article']['count'] = Article::whereIn('status', [1])->count();
  783. $res['category']['count'] = Category::where([])->count();
  784. $res['good']['count'] = Good::whereIn('status', [2])->count();
  785. $res['book']['count'] = Book::whereIn('status', [2])->count();
  786. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])->count();
  787. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])->count();
  788. $res['complaint']['count'] = Complaint::whereIn('status', [2])->count();
  789. $res['notice']['count'] = Notice::whereIn('status', [2])->count();
  790. //已审核数量
  791. $res['company']['count'] = Company::whereIn('status', [1])->count();
  792. $res['project']['count'] = Project::whereIn('status', [1])->count();
  793. // $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;');
  794. $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
  795. article.status =1 and article.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(article.created_at),`user`.type_id ORDER BY date ASC');
  796. $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
  797. good.status =2 and good.created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(good.created_at),`user`.type_id ORDER BY date ASC');
  798. $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');
  799. $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');
  800. $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');
  801. //company project 1 已审核
  802. $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');
  803. $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');
  804. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  805. //待审核数据
  806. $res['apply']['article'] = Article::where(['status' => 0])->count(); //
  807. $res['apply']['good'] = Good::where(['status' => 1])->count();
  808. $res['apply']['book'] = Book::where(['status' => 1])->count();
  809. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->count();
  810. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->count();
  811. $res['apply']['complaint'] = Complaint::where(['status' => 1])->count();
  812. $res['apply']['notice'] = Notice::where(['status' => 1])->count(); //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束',
  813. $res['apply']['order'] = Order::where(['status' => 6])->count();
  814. $res['apply']['company'] = Company::where(['status' => 1])->count();
  815. $res['apply']['project'] = Project::where(['status' => 1])->count();
  816. return Result::success($res);
  817. case 1:
  818. case 2:
  819. case 3:
  820. case 4:
  821. $res = [];
  822. if ($data['type_id'] == 4) {
  823. $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;');
  824. $res = [];
  825. //查询已审核文章
  826. $user_id = $data['user_id'];
  827. $article = Article::where('status', 1)
  828. ->where('admin_user_id', $user_id)
  829. ->orderBy('updated_at', 'desc')
  830. ->limit(10)
  831. ->get();
  832. $res['letterOfComplaintList'] = $result;
  833. $res['article'] = $article;
  834. }
  835. //查询已审核文章
  836. $user_id = $data['user_id'];
  837. $now = Carbon::now();
  838. // 系统提醒审核消息
  839. $sys_msg = [];
  840. $article = Article::where('status', 0)
  841. ->where('admin_user_id', $user_id)
  842. ->orderBy('updated_at', 'desc')
  843. ->limit(5)
  844. ->get(['id', 'title', 'updated_at'])
  845. ->map(function ($item) use ($now) {
  846. $item->class = '资讯';
  847. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  848. return $item;
  849. });
  850. var_dump($article, '---1--');
  851. $good = Good::where('status', 1)
  852. ->where('user_id', $user_id)
  853. ->orderBy('updated_at', 'desc')
  854. ->get(['id', 'name as title', 'updated_at'])
  855. ->map(function ($item) use ($now) {
  856. //去掉name的属性
  857. $item->class = '商品';
  858. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  859. return $item;
  860. });
  861. $book = Book::query()
  862. ->where('status', 1)
  863. ->where('user_id', $user_id)
  864. ->orderBy('updated_at', 'desc')
  865. ->get(['id', 'title', 'updated_at'])
  866. ->map(function ($item) use ($now) {
  867. //去掉name的属性
  868. $item->class = '书刊';
  869. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  870. return $item;
  871. });
  872. //招聘 求职
  873. $jobhunting = JobHunting::query()
  874. ->where('status', 1)
  875. ->where('user_id', $user_id)
  876. ->orderBy('updated_at', 'desc')
  877. ->get(['id', 'name as title', 'updated_at'])
  878. ->map(function ($item) use ($now) {
  879. //去掉name的属性
  880. $item->class = '求职';
  881. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  882. return $item;
  883. });
  884. $jobrecruiting = JobRecruiting::query()
  885. ->where('status', 0)
  886. ->where('user_id', $user_id)
  887. ->orderBy('updated_at', 'desc')
  888. ->get(['id', 'title', 'updated_at'])
  889. ->map(function ($item) use ($now) {
  890. //去掉name的属性
  891. $item->class = '招聘';
  892. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  893. return $item;
  894. });
  895. //通知 投诉
  896. $notice = Notice::query()
  897. ->where('status', 1)
  898. ->where('user_id', $user_id)
  899. ->orderBy('updated_at', 'desc')
  900. ->get(['id', 'title', 'updated_at'])
  901. ->map(function ($item) use ($now) {
  902. //去掉name的属性
  903. $item->class = $item->type == 1 ? "通知" : "公告";
  904. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  905. return $item;
  906. });
  907. $complaint = Complaint::query()
  908. ->where('status', 1)
  909. ->where('user_id', $user_id)
  910. ->orderBy('updated_at', 'desc')
  911. ->get(['id', 'title', 'updated_at'])
  912. ->map(function ($item) use ($now) {
  913. //去掉name的属性
  914. $item->class = "投诉";
  915. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  916. return $item;
  917. });
  918. //项目 公司 待审核 :0:未审核;1:已审核;2:已拒绝
  919. $project = Project::query()
  920. ->where('status', 0)
  921. ->where('user_id', $user_id)
  922. ->orderBy('updated_at', 'desc')
  923. ->get(['id', 'title', 'updated_at'])
  924. ->map(function ($item) use ($now) {
  925. //去掉name的属性
  926. $item->class = "项目";
  927. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  928. return $item;
  929. });
  930. $company = Company::query()
  931. ->where('status', 0)
  932. ->where('user_id', $user_id)
  933. ->orderBy('updated_at', 'desc')
  934. ->get(['id', 'title', 'updated_at'])
  935. ->map(function ($item) use ($now) {
  936. //去掉name的属性
  937. $item->class = "公司";
  938. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  939. return $item;
  940. });
  941. // var_dump($jobrecruiting, '-1--------------------11111111111111--');
  942. // var_dump($good, '-3--');
  943. // $s = $article->toArray();
  944. // var_dump($s, '=====31111111111111111');
  945. // 合并两个集合
  946. // 合并两个集合,并按 updated_at 倒序排序
  947. // $combined = $article->merge($good)->sortByDesc('updated_at');
  948. $combined = $article
  949. ->concat($good)
  950. ->concat($book)
  951. ->concat($jobhunting)
  952. ->concat($jobrecruiting)
  953. ->concat($notice)
  954. ->concat($complaint)
  955. ->concat($project)
  956. ->concat($company)
  957. ->sortByDesc(function ($item) {
  958. return Carbon::createFromFormat('Y-m-d H:i:s', $item->updated_at);
  959. });
  960. // ->sortBy('updated_at');
  961. // var_dump($combined, '--4--');
  962. // 转换为数组输出
  963. $res['sys_msg'] = $combined->values()->all();
  964. // 互动消息
  965. $chat_msg = [];
  966. // 通知投诉
  967. $notice_msg = [];
  968. // $res['sys_msg'] = $sys_msg;
  969. $chat_single_msg = ChatRecords::where('user_id', $user_id)
  970. ->where('is_read', 0)
  971. ->where('talk_type', 1)
  972. ->orderBy('updated_at', 'desc')
  973. ->limit(5)
  974. ->get(['id', 'content', 'updated_at'])
  975. ->map(function ($item) use ($now) {
  976. $item->class = "单聊";
  977. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  978. return $item;
  979. });
  980. $chat_group_msg = ChatRecords::where('user_id', $user_id)
  981. ->where('is_read', 0)
  982. ->where('talk_type', 2)
  983. ->leftJoin('chat_groups', 'chat_records.receiver_id', '=', 'chat_groups.id')
  984. ->orderBy('updated_at', 'desc')
  985. ->limit(5)
  986. ->get(['chat_records.id', 'content as content1', 'chat_records.updated_at', 'chat_groups.group_name as content'])
  987. ->map(function ($item) use ($now) {
  988. $item->class = "群聊";
  989. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  990. return $item;
  991. });
  992. $topic_msg = ChatTopic::where('chat_topics.user_id', $user_id)
  993. ->whereNotNull('chat_topics_reply.updated_at')
  994. ->leftJoin('chat_topics_reply', 'chat_topics.id', '=', 'chat_topics_reply.topic_id')
  995. ->select('chat_topics.id', 'chat_topics.title as content', 'chat_topics_reply.updated_at as updated_at')
  996. ->orderBy('updated_at', 'desc')
  997. ->limit(5)
  998. ->get(['chat_topics.id', 'chat_topics.content', 'chat_topics.updated_at'])
  999. ->map(function ($item) use ($now) {
  1000. $item->class = "话题";
  1001. // $item->content = $item->content;
  1002. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1003. return $item;
  1004. });
  1005. // 合并单聊和群聊消息
  1006. $chat_msg = $chat_single_msg
  1007. ->concat($chat_group_msg)
  1008. ->concat($topic_msg)
  1009. ->sortByDesc(function ($item) {
  1010. return Carbon::createFromFormat('Y-m-d H:i:s', $item->updated_at);
  1011. })->values()->all();
  1012. $res['chat_msg'] = $chat_msg;
  1013. //未读消息
  1014. $notce_recieve_msg = Notice::where('status', 1)
  1015. ->whereJsonContains('re_user_ids', $user_id)
  1016. ->where(function ($query) use ($user_id) {
  1017. $query->whereNull('read_id')
  1018. ->orWhereRaw('NOT JSON_CONTAINS(`read_id`, ?)', [$user_id]);
  1019. })
  1020. ->orderBy('updated_at', 'desc')
  1021. ->limit(5)
  1022. ->get(['id', 'title', 'updated_at'])
  1023. ->map(function ($item) use ($now) {
  1024. $item->class = "通知";
  1025. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1026. return $item;
  1027. });
  1028. $complaint_recieve_msg = Complaint::where('status', 1)
  1029. ->whereJsonContains('re_user_ids', $user_id)
  1030. ->where(function ($query) use ($user_id) {
  1031. $query->whereNull('read_id')
  1032. ->orWhereRaw('NOT JSON_CONTAINS(`read_id`, ?)', [$user_id]);
  1033. })
  1034. ->orderBy('updated_at', 'desc')
  1035. ->limit(5)
  1036. ->get(['id', 'title', 'updated_at'])
  1037. ->map(function ($item) use ($now) {
  1038. $item->class = "投诉";
  1039. $item->formatted_updated_at = $this->timeAgo($item->updated_at, $now);
  1040. return $item;
  1041. });
  1042. // 合并消息
  1043. $notice_msg = $notce_recieve_msg
  1044. ->concat($complaint_recieve_msg)
  1045. ->sortByDesc(function ($item) {
  1046. return Carbon::createFromFormat('Y-m-d H:i:s', $item->updated_at);
  1047. })->values()->all();
  1048. $res['notice_msg'] = $notice_msg;
  1049. // $res['article'] = $article;
  1050. $res['website']['count'] = Website::where([])->count();
  1051. $res['article']['count'] = Article::whereIn('status', [1])->where('admin_user_id', $user_id)->count();
  1052. $res['category']['count'] = Category::where([])->count();
  1053. $res['good']['count'] = Good::whereIn('status', [2])
  1054. ->where('user_id', $user_id)->count();
  1055. $res['book']['count'] = Book::whereIn('status', [2])
  1056. ->where('user_id', $user_id)->count();
  1057. $res['jobHunting']['count'] = JobHunting::whereIn('status', [2])
  1058. ->where('user_id', $user_id)->count();
  1059. $res['jobRecruiting']['count'] = JobRecruiting::whereIn('status', [1])
  1060. ->where('user_id', $user_id)->count();
  1061. $res['complaint']['count'] = Complaint::whereIn('status', [2])
  1062. ->where('user_id', $user_id)->count();
  1063. $res['notice']['count'] = Notice::whereIn('status', [2])
  1064. ->where('user_id', $user_id)->count();
  1065. //company project 0 待审核 1 已审核
  1066. $res['company']['count'] = Company::whereIn('status', [1])
  1067. ->where('user_id', $user_id)->count();
  1068. $res['project']['count'] = Project::whereIn('status', [1])
  1069. ->where('user_id', $user_id)->count();
  1070. //待处理的complaint
  1071. $res['complaint_deal']['count'] = Complaint::where(['status' => 2])
  1072. ->where('deal', 1)
  1073. // ->where('user_id', $user_id)
  1074. ->whereJsonContains('re_user_ids', $user_id)
  1075. ->count();
  1076. //待审核数据
  1077. $res['apply']['article'] = Article::where(['status' => 0])->where('admin_user_id', $user_id)->count(); //
  1078. $res['apply']['good'] = Good::where(['status' => 1])->where('user_id', $user_id)->count();
  1079. $res['apply']['book'] = Book::where(['status' => 1])->where('user_id', $user_id)->count();
  1080. $res['apply']['jobHunting'] = JobHunting::where(['status' => 1])->where('user_id', $user_id)->count();
  1081. $res['apply']['jobRecruiting'] = JobRecruiting::where(['status' => 0])->where('user_id', $user_id)->count();
  1082. $res['apply']['complaint'] = Complaint::where(['status' => 1])->where('user_id', $user_id)->count();
  1083. $res['apply']['notice'] = Notice::where(['status' => 1])->where('user_id', $user_id)->count();
  1084. //project company
  1085. $res['apply']['project'] = Project::whereIn('status', [0])
  1086. ->where('user_id', $user_id)->count();
  1087. $res['apply']['company'] = Company::whereIn('status', [0])
  1088. ->where('user_id', $user_id)->count();
  1089. return Result::success($res);
  1090. }
  1091. return [];
  1092. }
  1093. private function timeAgo(string $date, Carbon $now): string
  1094. {
  1095. $diff = (new Carbon($date))->diffInMinutes($now);
  1096. if ($diff < 1) {
  1097. return '刚刚';
  1098. } elseif ($diff < 60) {
  1099. return "{$diff} 分钟前";
  1100. } elseif ($diff < 1440) {
  1101. return (int)($diff / 60) . " 小时前";
  1102. } elseif ($diff < 2880) { // 两天内
  1103. return "1 天前";
  1104. } elseif ($diff < 7 * 1440) {
  1105. return (int)($diff / 1440) . " 天前";
  1106. } else {
  1107. return (new Carbon($date))->format('Y-m-d');
  1108. }
  1109. }
  1110. /**
  1111. * 获取分类下的模板
  1112. * @param array $data
  1113. * @return array
  1114. */
  1115. public function getTemplate(array $data): array
  1116. {
  1117. $page = $data['page'];
  1118. $pageSize = $data['pageSize'];
  1119. $where = [];
  1120. if (isset($data['template_class_id']) && $data['template_class_id']) {
  1121. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  1122. }
  1123. $result = Template::where($where)
  1124. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  1125. $count = Template::where($where)->count();
  1126. if (empty($result)) {
  1127. return Result::error("没有数据", 0);
  1128. }
  1129. $data = [
  1130. 'rows' => $result->toArray(),
  1131. 'count' => $count,
  1132. ];
  1133. return Result::success($data);
  1134. }
  1135. /**
  1136. * 创建模板
  1137. * @param
  1138. * @return void
  1139. */
  1140. public function addTemplate(array $data): array
  1141. {
  1142. $insertData = [
  1143. 'template_name' => $data['template_name'],
  1144. 'template_img' => json_encode($data['template_img']),
  1145. 'template_class_id' => $data['template_class_id'],
  1146. ];
  1147. $result = Template::insertGetId($insertData);
  1148. if (empty($result)) {
  1149. return Result::error("创建模板失败", 0);
  1150. } else {
  1151. return Result::success(["id" => $result]);
  1152. }
  1153. }
  1154. /**
  1155. * 更新模板
  1156. * @param array $data
  1157. * @return array
  1158. */
  1159. public function upTemplate(array $data): array
  1160. {
  1161. $where = [
  1162. 'id' => $data['id'],
  1163. ];
  1164. $insertData = [
  1165. 'template_name' => $data['template_name'],
  1166. 'template_img' => json_encode($data['template_img']),
  1167. 'template_class_id' => $data['template_class_id'],
  1168. ];
  1169. $result = Template::where($where)->update($insertData);
  1170. if (empty($result)) {
  1171. return Result::error("更新模板失败", 0);
  1172. } else {
  1173. return Result::success();
  1174. }
  1175. }
  1176. /**
  1177. * 删除模板
  1178. * @param array $data
  1179. * @return array
  1180. */
  1181. public function delTemplate(array $data): array
  1182. {
  1183. $where = [
  1184. 'id' => $data['id'],
  1185. ];
  1186. $result = Template::where($where)->delete();
  1187. if (empty($result)) {
  1188. return Result::error("删除模板失败", 0);
  1189. } else {
  1190. return Result::success();
  1191. }
  1192. }
  1193. /**
  1194. * 搜索网站
  1195. * @param array $data
  1196. * @return array
  1197. */
  1198. public function websiteList(array $data): array
  1199. {
  1200. $where = [];
  1201. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1202. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1203. }
  1204. $result = Website::where($where)->get();
  1205. if (isset($data['website_column_id'])) {
  1206. $target = $data['website_column_id'];
  1207. $result = $result->filter(function ($item) use ($target) {
  1208. $arr = json_decode($item->website_column_arr_id, true);
  1209. return !in_array($target, $arr);
  1210. });
  1211. }
  1212. if ($result) {
  1213. return Result::success($result);
  1214. } else {
  1215. return Result::error("没有网站", 0);
  1216. }
  1217. }
  1218. public function addWebsiteCategory(array $data): array
  1219. {
  1220. $website_id = $data['website_id'];
  1221. $category_arr_id = $data['category_arr_id'];
  1222. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  1223. $categoryListIds = [];
  1224. if ($categoryList) {
  1225. foreach ($categoryList->toArray() as $val) {
  1226. array_push($categoryListIds, $val['id']);
  1227. }
  1228. }
  1229. $arr = [];
  1230. if ($categoryListIds) {
  1231. foreach ($categoryListIds as $v) {
  1232. $ids = $this->getUnderlingUIds(intval($v));
  1233. $ids_arr = explode(",", $ids);
  1234. array_push($arr, $ids_arr);
  1235. }
  1236. }
  1237. $mergedArray = [];
  1238. foreach ($arr as $subarray) {
  1239. $mergedArray = array_merge($mergedArray, $subarray);
  1240. }
  1241. var_dump("所有:", $arr, $mergedArray);
  1242. //查询出所有的分类进行分割插入 组装数据
  1243. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  1244. $categoryListData = $categoryListData->toArray();
  1245. $insertData = [];
  1246. if ($categoryListData) {
  1247. foreach ($categoryListData as $key => $value) {
  1248. $insertData[$key]['website_id'] = $website_id;
  1249. $insertData[$key]['name'] = $value['name'];
  1250. $insertData[$key]['sort'] = $value['sort'];
  1251. $insertData[$key]['pid'] = $value['pid'];
  1252. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  1253. $insertData[$key]['seo_title'] = $value['seo_title'];
  1254. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  1255. $insertData[$key]['seo_description'] = $value['seo_description'];
  1256. $insertData[$key]['alias'] = $value['name'];
  1257. $insertData[$key]['category_id'] = $value['id'];
  1258. }
  1259. }
  1260. $result = WebsiteCategory::insert($insertData);
  1261. var_dump("插入数据状态:", $result);
  1262. if ($result) {
  1263. return Result::success($result);
  1264. } else {
  1265. return Result::error("创建失败", 0);
  1266. }
  1267. }
  1268. /**
  1269. * 删除网站导航
  1270. * @param array $data
  1271. * @return array
  1272. */
  1273. public function delWebsiteCategory(array $data): array
  1274. {
  1275. $website_id = $data['website_id'] ?? 0;
  1276. $category_id = $data['category_id'] ?? 0;
  1277. $ids = $this->getUnderlingUIds(intval($category_id));
  1278. $ids_arr = explode(",", $ids);
  1279. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  1280. if ($result) {
  1281. return Result::success($result);
  1282. } else {
  1283. return Result::error("删除失败", 0);
  1284. }
  1285. }
  1286. /**
  1287. * 获取网站导航
  1288. * @param array $data
  1289. * @return array
  1290. */
  1291. public function getAdminWebsiteCategory(array $data): array
  1292. {
  1293. $where = [
  1294. 'website_id' => $data['website_id'],
  1295. ];
  1296. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  1297. $list = [];
  1298. if ($result) {
  1299. foreach ($result->toArray() as $val) {
  1300. array_push($list, json_decode($val['category_arr_id']));
  1301. }
  1302. }
  1303. if ($result) {
  1304. return Result::success($list);
  1305. } else {
  1306. return Result::error("查询失败", 0);
  1307. }
  1308. }
  1309. /**
  1310. * 更新网站导航
  1311. * @param array $data
  1312. * @return array
  1313. */
  1314. public function upWebsiteCategory(array $data): array
  1315. {
  1316. $pinyin = new Pinyin();
  1317. Db::beginTransaction();
  1318. try {
  1319. if (!empty($data['category_arr_id'])) {
  1320. $websiteId = $data['website_id'];
  1321. // 1. 获取旧的 category_id 列表
  1322. $oldCategoryListIds = WebsiteCategory::where('website_id', $websiteId)->pluck('category_id')->toArray();
  1323. // 2. 获取新的 category_id 列表(去重)
  1324. $newCategoryListIds = [];
  1325. foreach ($data['category_arr_id'] as $arr) {
  1326. $lastId = end($arr);
  1327. $newCategoryListIds[] = $lastId;
  1328. }
  1329. $newCategoryListIds = array_unique($newCategoryListIds);
  1330. // 3. 需要删除的 category_id
  1331. $toDelete = array_diff($oldCategoryListIds, $newCategoryListIds);
  1332. if (!empty($toDelete)) {
  1333. WebsiteCategory::where('website_id', $websiteId)
  1334. ->whereIn('category_id', $toDelete)
  1335. ->delete();
  1336. }
  1337. // 4. 批量查出所有需要的 category 信息
  1338. $categories = Category::whereIn('id', $newCategoryListIds)->get()->keyBy('id');
  1339. // 5. 组装 upsertData,确保 category_id 不重复
  1340. $upsertData = [];
  1341. $handledCategoryIds = [];
  1342. foreach ($data['category_arr_id'] as $arr) {
  1343. $categoryId = end($arr);
  1344. if (!isset($categories[$categoryId]) || in_array($categoryId, $handledCategoryIds)) continue;
  1345. $info = $categories[$categoryId];
  1346. $aLIas_pinyin = $pinyin->permalink($info->name, '');
  1347. $upsertData[] = [
  1348. 'website_id' => $websiteId,
  1349. 'category_id' => $categoryId,
  1350. 'name' => $info->name ?? '',
  1351. 'alias' => $info->name ?? '',
  1352. 'aLIas_pinyin' => $aLIas_pinyin ?? '',
  1353. 'sort' => $info->sort ?? 0,
  1354. 'pid' => $info->pid ?? 0,
  1355. 'pid_arr' => $info->pid_arr ?? json_encode([]),
  1356. 'seo_title' => $info->seo_title ?? "",
  1357. 'seo_keywords' => $info->seo_keywords ?? "",
  1358. 'seo_description' => $info->seo_description ?? "",
  1359. 'is_url' => $info->is_url ?? 0,
  1360. 'web_url' => $info->web_url ?? '',
  1361. 'type' => $info->type ?? 1,
  1362. 'category_arr_id' => json_encode($arr)
  1363. ];
  1364. $handledCategoryIds[] = $categoryId;
  1365. }
  1366. // 6. upsert
  1367. if (!empty($upsertData)) {
  1368. WebsiteCategory::upsert(
  1369. $upsertData,
  1370. ['website_id', 'category_id'],
  1371. [
  1372. 'aLIas_pinyin',
  1373. 'pid',
  1374. 'pid_arr',
  1375. 'is_url',
  1376. 'web_url',
  1377. 'category_arr_id'
  1378. ]
  1379. );
  1380. }
  1381. }
  1382. //处理所有path
  1383. var_dump($data, '--------111----');
  1384. $website_id = $data['website_id'];
  1385. //获取所有数据
  1386. $result = WebsiteCategory::where(['website_id' => $website_id, 'path' => NULL])->get()->toArray();
  1387. if (empty($result)) {
  1388. return Result::error("没有数据", 0);
  1389. }
  1390. //处理path
  1391. foreach ($result as $key => $value) {
  1392. $array_category_arr_id = json_decode($value['category_arr_id'], true);
  1393. $result[$key]['path'] = WebsiteCategory::where('website_id', $website_id)
  1394. ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
  1395. ->whereIn('category_id', $array_category_arr_id)
  1396. ->pluck('aLIas_pinyin')
  1397. ->implode('/');
  1398. }
  1399. //批量更新数据path
  1400. $updateData = [];
  1401. foreach ($result as $item) {
  1402. $updateData[] = [
  1403. 'id' => $item['id'],
  1404. 'path' => $item['path']
  1405. ];
  1406. }
  1407. //批量更新
  1408. DB::beginTransaction();
  1409. try {
  1410. foreach ($result as $item) {
  1411. WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
  1412. }
  1413. DB::commit();
  1414. } catch (\Exception $e) {
  1415. DB::rollBack();
  1416. throw $e; // 或者返回错误信息
  1417. }
  1418. Db::commit();
  1419. return Result::success();
  1420. } catch (\Throwable $ex) {
  1421. Db::rollBack();
  1422. return Result::error("修改失败" . $ex->getMessage(), 0);
  1423. }
  1424. }
  1425. /**
  1426. * 获取网站列表
  1427. * @param array $data
  1428. * @return array
  1429. */
  1430. public function getWebsiteCategoryList(array $data): array
  1431. {
  1432. $where = [];
  1433. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1434. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1435. }
  1436. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1437. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1438. }
  1439. $result = Website::where($where)
  1440. ->with(["websiteCategory" => function ($query) {
  1441. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1442. }])
  1443. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1444. ->get();
  1445. $count = Website::where($where)->count();
  1446. if (empty($result)) {
  1447. return Result::error("没有数据", 0);
  1448. }
  1449. $data = [
  1450. 'rows' => $result->toArray(),
  1451. 'count' => $count,
  1452. ];
  1453. if ($result) {
  1454. return Result::success($data);
  1455. } else {
  1456. return Result::error("查询失败", 0);
  1457. }
  1458. }
  1459. /**
  1460. * 删除网站下的所有导航
  1461. * @param array $data
  1462. * @return array
  1463. */
  1464. public function delWebsiteAllCategory(array $data): array
  1465. {
  1466. $website_id = $data['website_id'];
  1467. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1468. if ($result) {
  1469. return Result::success($result);
  1470. } else {
  1471. return Result::error("删除失败", 0);
  1472. }
  1473. }
  1474. /**
  1475. * 获取网站下的某一个导航
  1476. * @param array $data
  1477. * @return array
  1478. */
  1479. public function getWebsiteCategoryOnes(array $data): array
  1480. {
  1481. $website_id = $data['website_id'];
  1482. $category_id = $data['category_id'];
  1483. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1484. ->first();
  1485. if ($result) {
  1486. return Result::success($result);
  1487. } else {
  1488. return Result::error("查询失败", 0);
  1489. }
  1490. }
  1491. /**
  1492. * 更新网闸下的某一个导航
  1493. * @param array $data
  1494. * @return array
  1495. */
  1496. public function upWebsiteCategoryones(array $data): array
  1497. {
  1498. //处理所有数据
  1499. //获取所有数据
  1500. if (isset($data['all']) && $data['all'] == 'all') {
  1501. $result = WebsiteCategory::get();
  1502. $result = $result->toArray();
  1503. var_dump($result, '------------');
  1504. $pinyin = new Pinyin();
  1505. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1506. for ($i = 0; $i < count($result); $i++) {
  1507. // $randss = substr(str_shuffle($letters), 0, 4);
  1508. $result[$i]['aLIas_pinyin'] = $pinyin->permalink($result[$i]['alias'], '');
  1509. WebsiteCategory::where(['id' => $result[$i]['id']])->update(['aLIas_pinyin' => $result[$i]['aLIas_pinyin']]);
  1510. }
  1511. var_dump($result, '--------222----');
  1512. if ($result) {
  1513. return Result::success($result);
  1514. } else {
  1515. return Result::error("更新失败", 0);
  1516. }
  1517. }
  1518. //处理所有path
  1519. if (isset($data['pathone']) && $data['pathone'] == 'pathone') {
  1520. var_dump($data, '--------111----');
  1521. $website_id = $data['website_id'];
  1522. //获取所有数据
  1523. $result = WebsiteCategory::where(['website_id' => $website_id, 'path' => NULL])->get()->toArray();
  1524. if (empty($result)) {
  1525. return Result::error("没有数据", 0);
  1526. }
  1527. //处理path
  1528. foreach ($result as $key => $value) {
  1529. $array_category_arr_id = json_decode($value['category_arr_id'], true);
  1530. $result[$key]['path'] = WebsiteCategory::where('website_id', $website_id)
  1531. ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
  1532. ->whereIn('category_id', $array_category_arr_id)
  1533. ->pluck('aLIas_pinyin')
  1534. ->implode('/');
  1535. }
  1536. //批量更新数据path
  1537. $updateData = [];
  1538. foreach ($result as $item) {
  1539. $updateData[] = [
  1540. 'id' => $item['id'],
  1541. 'path' => $item['path']
  1542. ];
  1543. }
  1544. //批量更新
  1545. DB::beginTransaction();
  1546. try {
  1547. foreach ($result as $item) {
  1548. WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
  1549. }
  1550. DB::commit();
  1551. } catch (\Exception $e) {
  1552. DB::rollBack();
  1553. throw $e; // 或者返回错误信息
  1554. }
  1555. return Result::success($result);
  1556. }
  1557. if (isset($data['pathall']) && $data['pathall'] == 'pathall') {
  1558. //获取所有数据
  1559. $result = WebsiteCategory::where(['path' => NULL])->get()->toArray();
  1560. if (empty($result)) {
  1561. return Result::error("没有数据", 0);
  1562. }
  1563. //处理path
  1564. foreach ($result as $key => $value) {
  1565. if (empty($value['category_arr_id'])) continue;
  1566. if ($key >= 4) { // 改为 >= 4 如果你想处理最多4个元素
  1567. // break;
  1568. }
  1569. $array_category_arr_id = json_decode($value['category_arr_id'], true);
  1570. $result[$key]['path'] = WebsiteCategory::where('website_id', $value['website_id'])
  1571. ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
  1572. ->whereIn('category_id', $array_category_arr_id)
  1573. ->pluck('aLIas_pinyin')
  1574. ->implode('/');
  1575. }
  1576. //批量更新数据path
  1577. $updateData = [];
  1578. foreach ($result as $item) {
  1579. $updateData[] = [
  1580. 'id' => $item['id'],
  1581. 'category_arr_id' => $item['category_arr_id'],
  1582. 'path' => $item['path']
  1583. ];
  1584. }
  1585. // var_dump($updateData);
  1586. // return Result::success($updateData);
  1587. //批量更新
  1588. DB::beginTransaction();
  1589. try {
  1590. foreach ($result as $item) {
  1591. WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
  1592. }
  1593. DB::commit();
  1594. } catch (\Exception $e) {
  1595. DB::rollBack();
  1596. throw $e; // 或者返回错误信息
  1597. }
  1598. return Result::success($result);
  1599. }
  1600. $where = [
  1601. 'website_id' => $data['website_id'],
  1602. 'category_id' => $data['category_id'],
  1603. ];
  1604. $alias = $data['alias'];
  1605. $pinyin = new Pinyin();
  1606. $aLIas_pinyin = $pinyin->permalink($alias, '');
  1607. //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
  1608. $result = WebsiteCategory::where($where)->first();
  1609. if (empty($result)) {
  1610. return Result::error("没有数据", 0);
  1611. }
  1612. $result = $result->toArray();
  1613. //+四位随机字母
  1614. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1615. // 打乱字符串顺序并截取前四位
  1616. // $randss = substr(str_shuffle($letters), 0, 4);
  1617. $data['aLIas_pinyin'] = $aLIas_pinyin;
  1618. //数据库先更新一下,再改path
  1619. WebsiteCategory::where($where)->update(['aLIas_pinyin' => $data['aLIas_pinyin']]);
  1620. //处理path
  1621. $category_arr_id = json_decode($result['category_arr_id'], true);
  1622. //取出来设置path
  1623. $path = WebsiteCategory::where('website_id', $data['website_id'])
  1624. ->orderByRaw('FIELD(category_id, ' . implode(',', $category_arr_id) . ')')
  1625. ->whereIn('category_id', $category_arr_id)
  1626. ->pluck('aLIas_pinyin')
  1627. ->implode('/');
  1628. $data['path'] = $path;
  1629. //处理包含此id的所有path
  1630. // 取出category_arr_id包含此id的所有数据
  1631. $all_other = WebsiteCategory::where('website_id', $data['website_id'])
  1632. ->whereJsonContains('category_arr_id', $data['category_id'])
  1633. ->get();
  1634. var_dump($all_other, '------------');
  1635. // 遍历所有数据,更新path
  1636. $newAliasPinyin = $aLIas_pinyin ?? ''; // 添加空值判断
  1637. $oldAliasPinyin = $result['alias_pinyin'] ?? ''; // 修正键名大小写
  1638. // 收集需要更新的ID和路径
  1639. $updateData = [];
  1640. foreach ($all_other as $item) {
  1641. $newPath = str_replace($oldAliasPinyin, $newAliasPinyin, $item->path);
  1642. if ($newPath !== $item->path) { // 仅在路径变化时更新
  1643. $updateData[] = [
  1644. 'id' => $item->id,
  1645. 'path' => $newPath
  1646. ];
  1647. }
  1648. }
  1649. // 批量更新提高性能
  1650. if (!empty($updateData)) {
  1651. // 假设模型对应的数据表为 'table_name',请替换为实际表名
  1652. Db::table('website_category')->updateOrInsert(
  1653. ['id' => DB::raw('VALUES(id)')],
  1654. $updateData
  1655. );
  1656. }
  1657. $result = WebsiteCategory::where($where)->update($data);
  1658. if ($result) {
  1659. return Result::success($result);
  1660. } else {
  1661. return Result::error("更新失败", 0);
  1662. }
  1663. }
  1664. /**
  1665. * 获取网站下的所有导航(包含子导航)
  1666. * @param array $data
  1667. * @return array
  1668. */
  1669. public function getWebsiteAllCategory(array $data): array
  1670. {
  1671. $where = [];
  1672. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1673. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1674. }
  1675. // if (isset($data['name']) && !empty($data['name'])) {
  1676. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1677. // }
  1678. // if (isset($data['alias']) && !empty($data['alias'])) {
  1679. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1680. // }
  1681. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1682. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1683. // }
  1684. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1685. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1686. // }
  1687. $result = WebsiteCategory::where($where)
  1688. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1689. ->select("website_category.*", "category.name as name")
  1690. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1691. // ->leftJoin("department", 'category.department_id', 'department.id')
  1692. // ->leftJoin("district", 'category.city_id', 'district.id')
  1693. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1694. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1695. ->orderBy("sort", "asc")
  1696. ->orderBy("updated_at", "desc")
  1697. ->get();
  1698. // $count = WebsiteCategory::where($where)
  1699. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1700. // ->leftJoin("department", 'category.department_id', 'department.id')
  1701. // ->leftJoin("district", 'category.city_id', 'district.id')
  1702. // ->count();
  1703. if (empty($result)) {
  1704. return Result::error("没有数据", 0);
  1705. }
  1706. // $data = [
  1707. // 'rows' => $result->toArray(),
  1708. // 'count' => $count,
  1709. // ];
  1710. if ($result) {
  1711. return Result::success($result->toArray());
  1712. } else {
  1713. return Result::error("查询失败", 0);
  1714. }
  1715. }
  1716. /**
  1717. * 递归查询数据
  1718. * @param $id
  1719. * @param $ids
  1720. * @return string
  1721. */
  1722. public function getUnderlingUIds($id, $ids = '')
  1723. {
  1724. $back = Category::where(['pid' => $id])->get();
  1725. $back = $back->toArray();
  1726. if (!empty($back) && is_array($back)) {
  1727. foreach ($back as $v) {
  1728. //防止当前人的ID重复去查询,形成恶性循环
  1729. if ($v['id'] == $id) {
  1730. continue;
  1731. }
  1732. $back2 = Category::where(['pid' => $id])->count('id');
  1733. if ($back2 > 0) {
  1734. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1735. } else {
  1736. $ids .= ',' . $v['id'];
  1737. }
  1738. }
  1739. }
  1740. $ids = $id . ',' . $ids . ',';
  1741. $ids = str_replace(',,', ",", $ids);
  1742. $ids = trim($ids, ',');
  1743. return $ids;
  1744. }
  1745. /**
  1746. * 检测网站名称是否重复
  1747. * @param array $data
  1748. * @return array
  1749. */
  1750. public function checkWebsiteName(array $data): array
  1751. {
  1752. if (isset($data['id'])) {
  1753. $data[] = ['id', "!=", $data['id']];
  1754. unset($data['id']);
  1755. }
  1756. $websiteInfo = Website::query()->where($data)->first();
  1757. if (empty($websiteInfo)) {
  1758. return Result::error("找不到网站", 0);
  1759. }
  1760. return Result::success($websiteInfo->toArray());
  1761. }
  1762. /**
  1763. * 检测网站url是否重复
  1764. * @param array $data
  1765. * @return array
  1766. */
  1767. public function checkWebsiteUrl(array $data): array
  1768. {
  1769. $whereData = [];
  1770. if (isset($data['id'])) {
  1771. $whereData = [['id', "!=", $data['id']]];
  1772. unset($data['id']);
  1773. }
  1774. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1775. if (empty($websiteInfo)) {
  1776. return Result::error("找不到URL", 0);
  1777. }
  1778. return Result::success($websiteInfo->toArray());
  1779. }
  1780. /**
  1781. * 获取网站底部基础信息
  1782. * @param array $data
  1783. * @return array
  1784. */
  1785. public function getWebsiteFootInfo(array $data): array
  1786. {
  1787. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1788. $website_head = Website::where('id', $data['website_id'])
  1789. ->where('status', 1)
  1790. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1791. ->first();
  1792. if (empty($website_head)) {
  1793. return Result::error("找不到网站", 0);
  1794. }
  1795. } else {
  1796. return Result::error("参数错误", 0);
  1797. }
  1798. $website_head = Website::where('id', $data['website_id'])
  1799. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1800. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1801. if (empty($website_foot)) {
  1802. return Result::error("暂无底部基础信息", 0);
  1803. }
  1804. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1805. $result = [
  1806. 'website_foot' => $website_foot,
  1807. 'website_head' => $website_head,
  1808. ];
  1809. return Result::success($result);
  1810. }
  1811. /**
  1812. * 获取网站底部导航
  1813. * @param array $data
  1814. * @return array
  1815. */
  1816. public function getWebsiteFooterCategory(array $data): array
  1817. {
  1818. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1819. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1820. if (empty($website)) {
  1821. return Result::error("找不到网站", 0);
  1822. }
  1823. } else {
  1824. return Result::error("参数错误", 0);
  1825. }
  1826. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1827. if (empty($result)) {
  1828. return Result::error("暂无底部导航", 0);
  1829. }
  1830. return Result::success($result->toArray());
  1831. }
  1832. /**
  1833. * 获取网站底部导航列表
  1834. * @param array $data
  1835. * @return array
  1836. */
  1837. public function getWebsiteFooterCategoryList(array $data): array
  1838. {
  1839. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1840. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1841. if (empty($website)) {
  1842. return Result::error("找不到网站", 0);
  1843. }
  1844. } else {
  1845. return Result::error("参数错误", 0);
  1846. }
  1847. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1848. // '底部导航类型 0:内容型;1:列表型;',
  1849. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1850. return Result::error("底部导航id错误", 0);
  1851. } else {
  1852. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1853. if ($query->count() == 0) {
  1854. return Result::error("暂无底部导航列表", 0);
  1855. } elseif ($query->count() == 1) {
  1856. $result = $query->first();
  1857. } else {
  1858. $result = $query->get();
  1859. }
  1860. }
  1861. return Result::success($result);
  1862. }
  1863. /**
  1864. * 获取网站底部导航
  1865. * @param array $data
  1866. * @return array
  1867. */
  1868. public function getWebsiteFooterCategoryInfo(array $data): array
  1869. {
  1870. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1871. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1872. if (empty($website)) {
  1873. return Result::error("找不到网站", 0);
  1874. }
  1875. } else {
  1876. return Result::error("参数错误", 0);
  1877. }
  1878. if (isset($data['type']) && $data['type'] == 0) {
  1879. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1880. if (empty($fcatid)) {
  1881. return Result::error("底部导航id错误", 0);
  1882. }
  1883. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1884. } else {
  1885. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1886. }
  1887. if (empty($result)) {
  1888. return Result::error("暂无底部导航内容", 0);
  1889. }
  1890. return Result::success($result);
  1891. }
  1892. /*
  1893. * 搜索网站二级导航
  1894. * @param array $data
  1895. * @return array
  1896. * */
  1897. public function selectWebsiteCategory(array $data): array
  1898. {
  1899. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1900. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1901. if (empty($website)) {
  1902. return Result::error("暂无该网站", 0);
  1903. }
  1904. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1905. // return Result::success($category);
  1906. if (empty($category)) {
  1907. return Result::error("暂无此导航", 0);
  1908. }
  1909. $query = Category::whereIn('id', $category);
  1910. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1911. $cityid = District::where('id', $data['cityid'])->first();
  1912. if (empty($cityid)) {
  1913. return Result::error("暂无此城市", 0);
  1914. } else {
  1915. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1916. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1917. if (empty($city_category)) {
  1918. return Result::error("暂无此城市下的导航", 0);
  1919. }
  1920. $result['catid'] = $city_category;
  1921. $city = 1;
  1922. // var_dump("城市====================",$result);
  1923. }
  1924. }
  1925. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1926. $departmentid = Department::where('id', $data['department_id'])->first();
  1927. if (empty($departmentid)) {
  1928. return Result::error("暂无此部门", 0);
  1929. } else {
  1930. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1931. if (empty($depart_category)) {
  1932. return Result::error("暂无此部门下的导航", 0);
  1933. }
  1934. $result['catid'] = $depart_category;
  1935. // var_dump("职能部门*******************",$result);
  1936. $department = 1;
  1937. }
  1938. }
  1939. if (!empty($city) && !empty($department)) {
  1940. // var_dump("城市和职能部门----------------------",$result);
  1941. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1942. if (empty($sel_category)) {
  1943. return Result::error("暂无此城市和职能部门下的导航", 0);
  1944. }
  1945. $result['catid'] = $sel_category;
  1946. }
  1947. if (empty($result)) {
  1948. return Result::error("暂无导航", 0);
  1949. }
  1950. // department_id
  1951. } else {
  1952. return Result::error("参数错误", 0);
  1953. }
  1954. return Result::success($result);
  1955. }
  1956. /**
  1957. * 获取网站栏目seo
  1958. * @param array $data
  1959. * @return array
  1960. */
  1961. public function getWebsiteCategoryHead(array $data): array
  1962. {
  1963. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1964. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1965. if (empty($website)) {
  1966. return Result::error("找不到网站", 0);
  1967. }
  1968. } else {
  1969. return Result::error("参数错误", 0);
  1970. }
  1971. if (isset($data['catid']) && !empty($data['catid'])) {
  1972. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1973. }
  1974. if (empty($result)) {
  1975. return Result::error("暂无导航", 0);
  1976. }
  1977. $result['website_name'] = $website['website_name'] ?? '';
  1978. $result['suffix'] = $website['suffix'] ?? '';
  1979. return Result::success($result);
  1980. }
  1981. /*
  1982. * 获取某个栏目
  1983. * @param array $data
  1984. * @return array
  1985. * */
  1986. public function getOneWebsiteCategory(array $data): array
  1987. {
  1988. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1989. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1990. if (empty($website)) {
  1991. return Result::error("暂无该网站", 0);
  1992. }
  1993. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1994. if (empty($category)) {
  1995. return Result::error("暂无此导航", 0);
  1996. }
  1997. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1998. $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();
  1999. $category['parent_id'] = $parent['parent_id'] ?? '';
  2000. $category['parent_pinyin'] = $parent['aLIas_pinyin'] ?? '';
  2001. $category['parent_name'] = $parent['parent_name'] ?? '';
  2002. return Result::success($category);
  2003. } else {
  2004. return Result::error("参数错误", 0);
  2005. }
  2006. }
  2007. // 自助建站--fr-------------------
  2008. /**
  2009. * 获取并搜索 网站模板信息
  2010. * @param array $data
  2011. * @return array
  2012. */
  2013. public function getWebsiteintel(array $data): array
  2014. {
  2015. //查询所有网站模板信息
  2016. $query = Website::where('website.status', 1)
  2017. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  2018. ->leftJoin("template", "template.template_id", "website_template_info.template_id")
  2019. ->leftJoin("template_class", "template_class.class_id", "template.template_class_id")
  2020. ->leftJoin('website_column', 'website_column.id', 'website.website_column_id')
  2021. ->leftJoin('website_column as parent_column', function ($join) {
  2022. $join->on('website_column.pid', '=', 'parent_column.id')
  2023. ->where('website_column.pid', 2);
  2024. })
  2025. ->whereRaw('JSON_CONTAINS(website_column_arr_id,?)', [2])
  2026. ->select(
  2027. "website_template_info.id",
  2028. "template_class.name",
  2029. "website.id as website_id",
  2030. "website.website_name",
  2031. "website.website_url",
  2032. "website_template_info.action_id",
  2033. "website_template_info.created_at",
  2034. "website_template_info.updated_at",
  2035. "website_template_info.page_type",
  2036. 'website_column.column_name',
  2037. 'parent_column.column_name as parent_column_name',
  2038. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  2039. "template.template_name"
  2040. );
  2041. //若存在条件;则在$query的基础上进行筛选
  2042. if (isset($data['website_name']) && !empty($data['website_name'])) {
  2043. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  2044. }
  2045. if (isset($data['status']) && !empty($data['status'])) {
  2046. $query->where('website_template_info.status', $data['status']);
  2047. }
  2048. $count = $query->count();
  2049. $query->limit($data['pageSize'])
  2050. ->offset(($data['page'] - 1) * $data['pageSize'])
  2051. ->orderBy("website_template_info.updated_at", "desc");
  2052. $rep = $query->get();
  2053. if ($rep->count() == 0) {
  2054. return Result::error("没有查找到相关数据", 0);
  2055. } else {
  2056. $rep->each(function ($item) {
  2057. if (!empty($item->page_type)) {
  2058. $pageTypeArray = json_decode($item->page_type, true);
  2059. if (is_array($pageTypeArray)) {
  2060. $item->page_type = $pageTypeArray;
  2061. }
  2062. }
  2063. });
  2064. $result = [
  2065. "rows" => $rep->toArray(),
  2066. "count" => $count,
  2067. ];
  2068. return Result::success($result);
  2069. }
  2070. // return Result::success($result);
  2071. }
  2072. /**
  2073. * 自助建站-验证搭建网站
  2074. * @param array $data
  2075. * @return array
  2076. */
  2077. public function checkWebsiteBuild(array $data): array
  2078. {
  2079. $website = Website::where('website.id', $data['website_id'])->first();
  2080. if (empty($website)) {
  2081. return Result::error("请输入正确的网站id!", 0);
  2082. }
  2083. if (empty($website['website_column_id'])) {
  2084. return Result::error("请先关联导航池!", 0);
  2085. }
  2086. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  2087. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  2088. if (empty($foot_type)) {
  2089. return Result::error("请先关联单页!", 0);
  2090. }
  2091. // if (!in_array(1, $foot_type) ) {
  2092. // $missingTypes[] = "列表型底部导航";
  2093. // }
  2094. if (!in_array(0, $foot_type)) {
  2095. $missingTypes[] = "单页(详情)";
  2096. }
  2097. if (!empty($missingTypes)) {
  2098. $errorMessage = "请先添加" . $missingTypes . "!";
  2099. return Result::error($errorMessage, 0);
  2100. }
  2101. $result['foot_type'] = $foot_type;
  2102. // 友情链接类型'1:图片 2:文字 3:底部';
  2103. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  2104. $missingTypes = [];
  2105. if (!in_array(1, $types)) {
  2106. $missingTypes[] = "文字链接";
  2107. }
  2108. if (!in_array(2, $types)) {
  2109. $missingTypes[] = "图片链接";
  2110. }
  2111. if (!in_array(3, $types)) {
  2112. $missingTypes[] = "底部链接";
  2113. }
  2114. if (!empty($missingTypes)) {
  2115. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  2116. return Result::error($errorMessage, 0);
  2117. }
  2118. return Result::success($result);
  2119. }
  2120. /**
  2121. * 自助建站-验证编辑网站
  2122. * @return array
  2123. *
  2124. */
  2125. public function checkWebsiteEdit(array $data): array
  2126. {
  2127. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])->first();
  2128. if (empty($result)) {
  2129. return Result::error("请输入正确的网站id!", 0);
  2130. }
  2131. // 订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  2132. $order = Order::where('website_id', $data['website_id'])->where('status', 1)->first();
  2133. if (!empty($order)) {
  2134. return Result::error("此网站的广告位正在使用中,暂不可编辑!", 0);
  2135. }
  2136. $check = $this->checkWebsiteBuild($data);
  2137. if ($check['code'] != 200) {
  2138. return Result::error($check['message'], 0);
  2139. }
  2140. // return Result::success($check);
  2141. if ($result['page_type']) {
  2142. $result['page_type'] = json_decode($result['page_type'], true);
  2143. return Result::success('网站编辑验证通过!');
  2144. } else {
  2145. return Result::error("请先选择网站页面类型!", 0);
  2146. }
  2147. }
  2148. /**
  2149. * 修改网站基础信息
  2150. *
  2151. * @return array
  2152. */
  2153. public function upWebsiteTemplateintel(array $data): array
  2154. {
  2155. $web = Website::where('website.id', $data['website_id'])->first();
  2156. if (empty($web)) {
  2157. return Result::error("请输入正确的网站id!", 0);
  2158. }
  2159. $where = ['website_id' => $data['website_id']];
  2160. $result = WebsiteTemplateInfo::where($where)->first();
  2161. if (empty($result)) {
  2162. return Result::error("请先添加网站基础信息!", 0);
  2163. } else {
  2164. unset($data['website_id']);
  2165. // 删除重复元素
  2166. $data['page_type'] = array_unique($data['page_type']);
  2167. // 重新索引数组
  2168. $data['page_type'] = array_values($data['page_type']);
  2169. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  2170. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  2171. // 将数组转换为 JSON 字符串
  2172. $data['page_type'] = json_encode($data['page_type']) ?? '';
  2173. try {
  2174. $result = WebsiteTemplateInfo::where($where)->update($data);
  2175. } catch (\Exception $e) {
  2176. if ($e->getCode() == 22001) {
  2177. $errorMessage = $e->getMessage();
  2178. return Result::error("请检查表单,某个字段超出了长度限制!");
  2179. } else {
  2180. // 处理其他类型的数据库错误
  2181. return Result::error("修改操作失败: " . $e->getMessage());
  2182. }
  2183. return Result::error("修改失败!", 0);
  2184. }
  2185. } else {
  2186. return Result::error("请先选择首页和单页(详情)!", 0);
  2187. }
  2188. }
  2189. if (empty($result)) {
  2190. return Result::error("修改失败!", 0);
  2191. } else {
  2192. return Result::success($result);
  2193. }
  2194. }
  2195. /**
  2196. * 获取所有网站风格信息
  2197. *
  2198. * @return array
  2199. */
  2200. public function getAllTemplateClass(array $data): array
  2201. {
  2202. $result = TemplateClass::all();
  2203. if (empty($result)) {
  2204. return Result::error("没有查找到相关数据!", 0);
  2205. } else {
  2206. return Result::success($result);
  2207. }
  2208. }
  2209. /**
  2210. * 搜索并获取网站模板信息
  2211. * @param array $data
  2212. * @return array
  2213. */
  2214. public function getWebsiteTemplateList(array $data): array
  2215. {
  2216. $website = Website::where('website.id', $data['website_id'])->first();
  2217. if (empty($website)) {
  2218. return Result::error("请输入正确的网站id!", 0);
  2219. } else {
  2220. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  2221. if (empty($page_type)) {
  2222. return Result::error("此网站还未添加基础信息!", 0);
  2223. } else {
  2224. $where = json_decode($page_type['page_type'], true);
  2225. // $where = $data["page_type"];
  2226. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  2227. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  2228. if (empty($template_class)) {
  2229. return Result::error("请输入正确的模板风格id!", 0);
  2230. } else {
  2231. // 获取指定风格下的模板
  2232. $rep = Template::where('template_class_id', $data['template_class_id'])
  2233. ->where(function ($query) use ($where) {
  2234. foreach ($where as $value) {
  2235. $query->whereJsonContains('template_img', ['value' => $value]);
  2236. }
  2237. })
  2238. ->limit($data['pageSize'])
  2239. ->offset(($data['page'] - 1) * $data['pageSize'])
  2240. ->orderBy("template.updated_at", "desc")
  2241. ->get();
  2242. $count = Template::where('template_class_id', $data['template_class_id'])
  2243. ->where(function ($query) use ($where) {
  2244. foreach ($where as $value) {
  2245. $query->whereJsonContains('template_img', ['value' => $value]);
  2246. }
  2247. })->count();
  2248. }
  2249. } else {
  2250. //获取所有模板
  2251. $rep = Template::where(function ($query) use ($where) {
  2252. foreach ($where as $value) {
  2253. $query->whereJsonContains('template_img', ['value' => $value]);
  2254. }
  2255. })
  2256. ->limit($data['pageSize'])
  2257. ->offset(($data['page'] - 1) * $data['pageSize'])
  2258. ->orderBy("template.updated_at", "desc")
  2259. ->get();
  2260. $count = Template::where(function ($query) use ($where) {
  2261. foreach ($where as $value) {
  2262. $query->whereJsonContains('template_img', ['value' => $value]);
  2263. }
  2264. })->count();
  2265. }
  2266. if (empty($rep) || $rep->count() == 0) {
  2267. return Result::error("没有查找到相关数据!", 0);
  2268. } else {
  2269. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  2270. // $rep->each(function ($item) use ($where) {
  2271. // $template_img = json_decode($item->template_img, true);
  2272. // if(is_array($template_img)){
  2273. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  2274. // return isset($img['value']) && in_array($img['value'], $where);
  2275. // });
  2276. // }
  2277. // $item->template_img = $filtered_img;
  2278. // });
  2279. // $result = [
  2280. // "rows" => $rep->toArray(),
  2281. // "count" => $count
  2282. // ];
  2283. $rep->each(function ($item) use ($where) {
  2284. $template_img = json_decode($item->template_img, true);
  2285. if (is_array($template_img)) {
  2286. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  2287. return isset($img['value']) && in_array($img['value'], $where);
  2288. });
  2289. // 对过滤后的数组进行重新排序
  2290. $sorted_img = array_values($filtered_img);
  2291. $item->template_img = $sorted_img;
  2292. } else {
  2293. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  2294. }
  2295. });
  2296. $result = [
  2297. "rows" => $rep->toArray(),
  2298. "count" => $count,
  2299. ];
  2300. }
  2301. }
  2302. }
  2303. return Result::success($result);
  2304. }
  2305. /**
  2306. * 添加网站模板
  2307. * @param array $data
  2308. */
  2309. public function addWebsiteTemplateclassintel(array $data): array
  2310. {
  2311. $template = Template::where('id', $data['template_id'])->first();
  2312. $web = Website::where('id', $data['website_id'])->first();
  2313. if (empty($template)) {
  2314. return Result::error("请输入正确的模板id", 0);
  2315. }
  2316. if (empty($web)) {
  2317. return Result::error("请输入正确的网站id", 0);
  2318. }
  2319. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  2320. if (empty($result)) {
  2321. return Result::error("添加失败", 0);
  2322. } else {
  2323. return Result::success($result);
  2324. }
  2325. }
  2326. /**
  2327. * 获取网站模板信息
  2328. * @param array $data
  2329. */
  2330. public function getWebsiteTemplateclassintel(array $data): array
  2331. {
  2332. // return Result::success($data);
  2333. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2334. if (empty($template)) {
  2335. return Result::error("请输入正确的搭建网站id", 0);
  2336. }
  2337. $page_type = json_decode($template['page_type'], true);
  2338. // 获取指定网站下的基础信息和模板及风格信息
  2339. $result = WebsiteTemplateInfo::where([
  2340. ['website_template_info.website_id', $data['website_id']],
  2341. ['website_template_info.template_id', '!=', null],
  2342. ])
  2343. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  2344. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  2345. ->where(function ($query) use ($page_type) {
  2346. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  2347. foreach ($page_type as $pageType) {
  2348. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  2349. }
  2350. })
  2351. ->select(
  2352. 'template.template_name',
  2353. 'template.template_img',
  2354. 'template.id as tid',
  2355. 'template_class.name',
  2356. 'template_class.id as class_id',
  2357. 'website_template_info.page_type',
  2358. 'website_template_info.website_id'
  2359. )
  2360. ->first();
  2361. if (empty($result)) {
  2362. return Result::error("没有查找到相关数据", 0);
  2363. }
  2364. // return Result::success($result);
  2365. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  2366. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  2367. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  2368. foreach ($template_img as $key => $value) {
  2369. if (!in_array($value['value'], $page_type)) {
  2370. unset($template_img[$key]);
  2371. }
  2372. }
  2373. // return Result::success($result);
  2374. $result['template_img'] = array_values($template_img);
  2375. $result['page_type'] = $page_type;
  2376. // $templateList = $this->getWebsiteTemplateList($data);
  2377. // $count = $templateList['data']['count']??'';
  2378. // $pages = $count / $data['pageSize'];
  2379. // $found = false;
  2380. // while ($pages && !$found) {
  2381. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  2382. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  2383. // if ($index !== false) {
  2384. // $found = true;
  2385. // $result['page'] = $pages;
  2386. // $pages = 0;
  2387. // // echo "查询到的 template 在第 $page 页";
  2388. // }
  2389. // $pages--;
  2390. // }
  2391. return Result::success($result);
  2392. }
  2393. /**
  2394. * 获取网站模板下的板块信息
  2395. * @param array $data
  2396. */
  2397. public function getWebsiteSectorList(array $data): array
  2398. {
  2399. $website = Website::where('id', $data['website_id'])->first();
  2400. if (empty($website)) {
  2401. return Result::error("请输入正确的网站id", 0);
  2402. }
  2403. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2404. if (empty($template['template_id'])) {
  2405. return Result::error("请先添加网站模板", 0);
  2406. }
  2407. $query = Sector::where('template_id', $template['template_id']);
  2408. if ($query->count() == 0) {
  2409. return Result::error("没有查找到相关板块数据", 0);
  2410. } elseif ($query->count() == 1) {
  2411. $sector = $query->first();
  2412. } else {
  2413. $sector = $query->get();
  2414. }
  2415. $result['page_type'] = $template['page_type'];
  2416. $result['tid'] = $template['template_id'];
  2417. $result = [
  2418. "tid" => $template['template_id'],
  2419. "page_type" => $template['page_type'],
  2420. "sector" => $sector,
  2421. ];
  2422. return Result::success($result);
  2423. }
  2424. // 自助建站---------------fr---------------
  2425. // 网站底基础信息--fr-------------------
  2426. /**
  2427. * 添加网站底基础信息
  2428. * @param array $data
  2429. */
  2430. public function addWebFootInfo(array $data): array
  2431. {
  2432. $website = Website::where('id', $data['website_id'])->first();
  2433. if (empty($website)) {
  2434. return Result::error("请输入正确的网站id!", 0);
  2435. }
  2436. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2437. if (!empty($info)) {
  2438. return Result::error("该网站已经添加过了!", 0);
  2439. }
  2440. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  2441. if (empty($result)) {
  2442. return Result::error("请先添加网站基础信息!", 0);
  2443. } else {
  2444. return Result::success($result);
  2445. }
  2446. }
  2447. /**
  2448. * 获取网站底基础信息
  2449. * @param array $data
  2450. */
  2451. public function getWebFootInfo(array $data): array
  2452. {
  2453. $website = Website::where('id', $data['website_id'])->first();
  2454. if (empty($website)) {
  2455. return Result::error("请输入正确的网站id!", 0);
  2456. }
  2457. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2458. if (empty($result)) {
  2459. return Result::error("请先添加网站基础信息!", 0);
  2460. }
  2461. return Result::success($result);
  2462. }
  2463. /**
  2464. * 修改网站底基础信息
  2465. * @param array $data
  2466. */
  2467. public function upWebFootInfo(array $data): array
  2468. {
  2469. // return Result::success($data);
  2470. $website = Website::where('id', $data['website_id'])->first();
  2471. if (empty($website)) {
  2472. return Result::error("请输入正确的网站id!", 0);
  2473. }
  2474. $web = $data['website_id'];
  2475. unset($data['website_id']);
  2476. $where = ['website_id' => $web];
  2477. // return Result::success($data);
  2478. $result = WebsiteTemplateInfo::where($where)->first();
  2479. if (empty($result)) {
  2480. $data['website_id'] = $web;
  2481. $result = WebsiteTemplateInfo::insertGetId($data);
  2482. // return Result::error("请先添加网站基础信息!",0);
  2483. } else {
  2484. $result = WebsiteTemplateInfo::where($where)->update($data);
  2485. }
  2486. // $where = ['website_id'=>$data['website_id']];
  2487. // unset($data['website_id']);
  2488. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  2489. if (empty($result)) {
  2490. return Result::error("修改失败!", 0);
  2491. }
  2492. return Result::success($result);
  2493. }
  2494. /*
  2495. * 获取父级/子级栏目------路由
  2496. * @param array $data
  2497. * @return array
  2498. * */
  2499. public function getWebsiteParentCategory(array $data): array
  2500. {
  2501. // return Result::success($data);
  2502. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2503. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2504. }
  2505. if (empty($website)) {
  2506. return Result::error("暂无该网站", 0);
  2507. }
  2508. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2509. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2510. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2511. ->where('pid', 0)
  2512. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2513. ->get() // 添加 get() 方法获取结果集合
  2514. ->all();
  2515. // 提前查询所有父级栏目,避免在 map 中多次查询
  2516. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2517. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2518. ->where('pid', 0)
  2519. ->select('category_id', 'aLIas_pinyin')
  2520. ->get()
  2521. ->keyBy('category_id');
  2522. // 直接查询子栏目信息
  2523. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2524. ->where('pid', '!=', 0)
  2525. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2526. ->get();
  2527. // 使用集合的 map 方法处理子栏目信息
  2528. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2529. $alias = $item->alias;
  2530. $category_id = $item->category_id;
  2531. $aLIas_pinyin = $item->aLIas_pinyin;
  2532. $parent = $parentCategories->get($item->pid);
  2533. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2534. $result = [
  2535. 'name' => $alias,
  2536. 'cid' => $category_id
  2537. ];
  2538. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2539. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2540. }
  2541. return $result;
  2542. })->values()->all();
  2543. if (empty($category)) {
  2544. return Result::error("暂无此导航", 0);
  2545. }
  2546. return Result::success($category);
  2547. }
  2548. /**
  2549. * 获取网站栏目信息
  2550. * @param array $data
  2551. */
  2552. public function getWebsiteFootAll(array $data): array
  2553. {
  2554. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2555. if (empty($website)) {
  2556. return Result::error("暂无该网站", 0);
  2557. }
  2558. // 1:图片 2:文字 3:底部
  2559. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2560. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2561. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2562. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2563. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2564. if (empty($result)) {
  2565. return Result::error("暂无此网站信息", 0);
  2566. }
  2567. return Result::success($result);
  2568. }
  2569. /**
  2570. * 获取网站顶部信息
  2571. * @param array $data
  2572. */
  2573. public function getWebsiteHead(array $data): array
  2574. {
  2575. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2576. if (empty($result)) {
  2577. return Result::error("暂无该网站", 0);
  2578. }
  2579. return Result::success($result);
  2580. }
  2581. /**
  2582. * 路由匹配
  2583. * @param array $data
  2584. */
  2585. public function getWebsiteRoute(array $data): array
  2586. {
  2587. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2588. if (empty($website)) {
  2589. return Result::error("暂无该网站", 0);
  2590. }
  2591. if (isset($data['pinyin']) && !empty($data['pinyin'])) {
  2592. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2593. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2594. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2595. $join->on('pc.pid', '=', 'website_category.category_id')
  2596. ->where('pc.website_id', '=', $data['website_id']);
  2597. })
  2598. ->select('website_category.category_id', 'website_category.type', 'website_category.alias', DB::raw('CASE WHEN pc.category_id IS NOT NULL THEN 1 ELSE 0 END as children_count'))
  2599. ->first();
  2600. }
  2601. if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
  2602. $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
  2603. }
  2604. if (!isset($result) || empty($result)) {
  2605. return Result::error("暂无该导航", 0);
  2606. }
  2607. return Result::success($result);
  2608. }
  2609. /**
  2610. * 获取网站栏目信息
  2611. * @param array $data
  2612. */
  2613. public function getWebsiteAllinfo(array $data): array
  2614. {
  2615. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2616. if (empty($website)) {
  2617. return Result::error("暂无该网站", 0);
  2618. }
  2619. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2620. $website_head = Website::where('id', $data['website_id'])
  2621. ->where('status', 1)
  2622. ->select(
  2623. 'id',
  2624. 'website_name',
  2625. 'logo',
  2626. 'title',
  2627. 'keywords',
  2628. 'description',
  2629. 'suffix',
  2630. 'website_url',
  2631. 'ad_key',
  2632. 'api_url',
  2633. 'login_url',
  2634. 'weblog_url'
  2635. )
  2636. ->first();
  2637. if (empty($website_head)) {
  2638. return Result::error("找不到网站", 0);
  2639. }
  2640. } else {
  2641. return Result::error("参数错误", 0);
  2642. }
  2643. $website_foot['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2644. if (empty($website_foot)) {
  2645. return Result::error("暂无底部基础信息", 0);
  2646. }
  2647. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  2648. $result['website_head'] = $website_head;
  2649. $result['website_foot'] = $website_foot;
  2650. // return Result::success($result);
  2651. // 1:图片 2:文字 3:底部
  2652. $result['website_foot']['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2653. $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();
  2654. $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();
  2655. $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();
  2656. if (empty($result)) {
  2657. return Result::error("暂无此网站信息", 0);
  2658. }
  2659. return Result::success($result);
  2660. }
  2661. //20250212 网站标识
  2662. public function addWebsiteGroup(array $data): array
  2663. {
  2664. //判斷name是不是重复
  2665. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2666. if (!empty($websiteGroupInfo)) {
  2667. return Result::error("网站标识重复", 0);
  2668. }
  2669. //添加信息
  2670. $result = WebsiteGroup::insertGetId($data);
  2671. var_dump($result);
  2672. if (empty($result)) {
  2673. return Result::error("创建失败", 0);
  2674. } else {
  2675. return Result::success($result);
  2676. }
  2677. }
  2678. public function getWebsiteGroupList(array $data): array
  2679. {
  2680. $where = [];
  2681. if (isset($data['name']) && !empty($data['name'])) {
  2682. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2683. }
  2684. $result = WebsiteGroup::where($where)
  2685. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2686. ->get();
  2687. foreach ($result as $websiteGroup) {
  2688. $webIds = json_decode($websiteGroup->web_ids, true);
  2689. $websites = Website::whereIn('id', $webIds)->get();
  2690. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2691. $websiteGroup->website_names = $websiteNames;
  2692. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2693. }
  2694. $count = WebsiteGroup::where($where)->count();
  2695. if (empty($result)) {
  2696. return Result::error("没有数据", 0);
  2697. }
  2698. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2699. }
  2700. public function getWebsiteGroupInfo(array $data): array
  2701. {
  2702. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2703. $webIds = json_decode($websiteInfo->web_ids, true);
  2704. $websites = Website::whereIn('id', $webIds)->get();
  2705. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2706. $websiteInfo->website_names = $websiteNames;
  2707. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2708. if (empty($websiteInfo)) {
  2709. return Result::error("找不到URL", 0);
  2710. }
  2711. return Result::success($websiteInfo->toArray());
  2712. }
  2713. public function updateWebsiteGroup(array $data): array
  2714. {
  2715. $where = [
  2716. 'id' => $data['id'],
  2717. ];
  2718. $insertData = [
  2719. 'name' => $data['name'],
  2720. 'web_ids' => $data['web_ids'],
  2721. ];
  2722. //判斷name是不是重复
  2723. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2724. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2725. return Result::error("网站标识重复", 0);
  2726. }
  2727. $result = WebsiteGroup::where($where)->update($insertData);
  2728. var_dump($result, '------更新');
  2729. if (empty($result)) {
  2730. return Result::error("更新失败", 0);
  2731. } else {
  2732. return Result::success();
  2733. }
  2734. }
  2735. public function deleteWebsiteGroup(array $data): array
  2736. {
  2737. $where = [
  2738. 'id' => $data['id'],
  2739. ];
  2740. //看看user表是不是有这个id
  2741. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2742. if (!empty($userGroup)) {
  2743. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2744. }
  2745. $result = WebsiteGroup::where($where)->delete();
  2746. if (empty($result)) {
  2747. return Result::error("删除失败", 0);
  2748. } else {
  2749. return Result::success();
  2750. }
  2751. }
  2752. public function getWebsiteNavList(array $data): array
  2753. {
  2754. $data['type'] = isset($data['type']) ? $data['type'] : 1;
  2755. $where = [];
  2756. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2757. array_push($where, ['website_id', '=', $data['website_id']]);
  2758. }
  2759. if (isset($data['pid'])) {
  2760. array_push($where, ['pid', '=', $data['pid']]);
  2761. }
  2762. if (isset($data['type']) && !empty($data['type'])) {
  2763. array_push($where, ['type', '=', $data['type']]);
  2764. }
  2765. // 根据站点和type获取websiteCategory 中的id
  2766. $websiteCategoryids = WebsiteCategory::query()
  2767. ->where('website_id', '=', $data['website_id'])
  2768. ->where('type', '=', $data['type'])
  2769. ->pluck('category_arr_id')->toArray();
  2770. var_dump($websiteCategoryids, '------获取websiteCategory 中的id');
  2771. //合并去重
  2772. $category_ids = [];
  2773. foreach ($websiteCategoryids as $key => $value) {
  2774. $v = json_decode($value, true);
  2775. $category_ids = array_unique(array_merge($category_ids, $v));
  2776. }
  2777. // var_dump($category_ids, '------获取websiteCategory 中的id');
  2778. //根据pid取出所有的websiteCategory 中的id
  2779. $list = WebsiteCategory::query()
  2780. ->where('website_id', '=', $data['website_id'])
  2781. ->where('is_url', '!=', 1)
  2782. ->where('pid', '=', $data['pid'])
  2783. ->whereIn('category_id', $category_ids)
  2784. ->get();
  2785. if (empty($list)) {
  2786. return Result::error("获取失败", 0);
  2787. } else {
  2788. return Result::success($list);
  2789. }
  2790. }
  2791. // 20250307 根据网站标识获和导航获取站点
  2792. public function getWebsiteNavPoolSite(array $data): array
  2793. {
  2794. $where = [];
  2795. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2796. array_push($where, ['category_id', '=', $data['category_id']]);
  2797. }
  2798. $list = WebsiteCategory::query()->where($where)
  2799. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2800. ->select('website_category.*', 'website.website_name')
  2801. ->distinct()
  2802. ->get();
  2803. if (empty($list)) {
  2804. return Result::error("获取失败", 0);
  2805. } else {
  2806. return Result::success($list);
  2807. }
  2808. }
  2809. public function getWebsiteNavPool(array $data): array
  2810. {
  2811. // 初始化查询条件数组
  2812. $query = WebsiteCategory::query();
  2813. // 如果传入了 websiteids,则添加到查询条件中
  2814. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2815. $query->whereIn('website_id', $data['websiteids']);
  2816. }
  2817. // 如果传入了 pid,则添加到查询条件中
  2818. if (isset($data['pid'])) {
  2819. $query->where('pid', '=', $data['pid']);
  2820. }
  2821. // 执行查询 唯一性 id
  2822. $list = $query->distinct()->get();
  2823. // 根据查询结果返回相应的结果
  2824. if ($list->isEmpty()) {
  2825. return Result::error("获取失败", 0);
  2826. } else {
  2827. return Result::success($list->toArray());
  2828. }
  2829. }
  2830. /**
  2831. *
  2832. * @param array $data
  2833. * @return array
  2834. */
  2835. public function getAllCategory(array $data): array
  2836. {
  2837. $total = Category::count();
  2838. $pageSize = 2000;
  2839. $pages = (int) ceil($total / $pageSize);
  2840. $results = [];
  2841. $concurrency = 5; // 最大并发数
  2842. $running = 0;
  2843. $chan = new \Swoole\Coroutine\Channel($pages);
  2844. for ($i = 0; $i < $pages; $i++) {
  2845. Coroutine::create(function () use ($i, $pageSize, $chan) {
  2846. $offset = $i * $pageSize;
  2847. $data = Category::useIndex('idx_covering')
  2848. ->orderBy('id', 'asc')
  2849. ->select('id', 'name', 'pid')
  2850. ->offset($offset)
  2851. ->limit($pageSize)
  2852. ->get()
  2853. ->toArray();
  2854. $chan->push($data);
  2855. });
  2856. }
  2857. for ($i = 0; $i < $pages; $i++) {
  2858. $res = $chan->pop();
  2859. if (is_array($res)) {
  2860. $results = array_merge($results, $res);
  2861. }
  2862. }
  2863. return Result::success($results);
  2864. }
  2865. /**
  2866. * 修改网站栏目排序
  2867. * @param array $data
  2868. * @return array
  2869. */
  2870. public function upWebsiteCategorySort(array $data): array
  2871. {
  2872. $where = [
  2873. 'website_id' => $data['website_id'],
  2874. 'id' => $data['id'],
  2875. ];
  2876. $result = WebsiteCategory::where($where)->update($data);
  2877. if ($result) {
  2878. return Result::success($result);
  2879. } else {
  2880. return Result::error("更新失败", 0);
  2881. }
  2882. }
  2883. //获取底部导航内容详情
  2884. public function getFooterContentInfo(array $data): array
  2885. {
  2886. $where = [
  2887. "id" => $data['content_id']
  2888. ];
  2889. $result = FooterContent::where($where)->first();
  2890. if ($result) {
  2891. return Result::success($result);
  2892. } else {
  2893. return Result::error("没有数据");
  2894. }
  2895. }
  2896. //获取底部导航内容列表
  2897. public function getFooterContentList(array $data): array
  2898. {
  2899. $where = [
  2900. "fcat_id" => $data['fcat_id'],
  2901. ];
  2902. switch (intval($data['type'])) {
  2903. case 0:
  2904. $result = FooterContent::where($where)->first();
  2905. break;
  2906. case 1:
  2907. $result = FooterContent::where($where)->get();
  2908. break;
  2909. }
  2910. if ($result) {
  2911. return Result::success($result);
  2912. } else {
  2913. return Result::error("没有数据");
  2914. }
  2915. }
  2916. /**
  2917. * 获取底部导航列表
  2918. * @param array $data
  2919. * @return array
  2920. */
  2921. public function getFooterCategoryList(array $data): array
  2922. {
  2923. $where = [
  2924. "website_id" => $data['website_id']
  2925. ];
  2926. $result = FooterCategory::where($where)->get();
  2927. if ($result) {
  2928. return Result::success($result);
  2929. } else {
  2930. return Result::error("没有数据");
  2931. }
  2932. }
  2933. // --自助建站-----------20250522fr----------------------start
  2934. /**
  2935. * 获取网站模板下的板块信息
  2936. * @param array $data
  2937. * @return array
  2938. */
  2939. public function getStaticResourceList(array $data): array
  2940. {
  2941. // return Result::success($data);
  2942. $where = [];
  2943. if (isset($data['img_alias']) && !empty($data['img_alias'])) {
  2944. array_push($where, ['website_img.img_alias', 'like', '%' . $data['img_alias'] . '%']);
  2945. }
  2946. if (isset($data['img_type']) && !empty($data['img_type'])) {
  2947. array_push($where, ['website_img.img_type', '=', $data['img_type']]);
  2948. }
  2949. if (isset($data['website_name']) && !empty($data['website_name'])) {
  2950. $website = Website::where('website_name', 'like', '%' . $data['website_name'] . '%')->first();
  2951. if (empty($website)) {
  2952. return Result::error("没有查找到相关数据", 0);
  2953. } else {
  2954. array_push($where, ['website_img.website_id', '=', $website['id']]);
  2955. }
  2956. }
  2957. // return Result::success($where);
  2958. $query = WebsiteImg::when(!empty($where), function ($query) use ($where) {
  2959. $query->where($where);
  2960. })
  2961. ->orderBy('updated_at', 'desc');
  2962. $count = $query->count();
  2963. $row = $query->leftJoin('website', 'website.id', '=', 'website_img.website_id')
  2964. ->select('website_img.*', 'website.website_name')
  2965. ->limit($data['pageSize'])
  2966. ->offset(($data['page'] - 1) * $data['pageSize'])
  2967. ->get();
  2968. if (empty($row)) {
  2969. return Result::error("没有查找到相关数据", 0);
  2970. } else {
  2971. $result = [
  2972. 'row' => $row,
  2973. 'count' => $count,
  2974. ];
  2975. }
  2976. return Result::success($result);
  2977. }
  2978. /**
  2979. * 添加网站模板下的板块信息
  2980. * @param array $data
  2981. */
  2982. public function addStaticResource(array $data): array
  2983. {
  2984. // $file = $data['img_url']->file('image');
  2985. // $size = $file->getSize();
  2986. $img_data = [
  2987. 'website_id' => $data['website_id'] ?? 0,
  2988. 'img_alias' => $data['img_alias'] ?? '',
  2989. 'img_url' => $data['img_url'],
  2990. 'img_size' => $data['img_size'],
  2991. ];
  2992. $img = WebsiteImg::insertGetId($img_data);
  2993. if (empty($img)) {
  2994. return Result::error("添加失败", 0);
  2995. }
  2996. $img_id['id'] = $img;
  2997. $url = WebsiteImg::where($img_id)->value('img_url');
  2998. // 1. 分割路径部分(以 / 为分隔符)
  2999. $pathSegments = explode('/', parse_url($url, PHP_URL_PATH));
  3000. // 2. 取最后一个元素作为文件名
  3001. $fileName = end($pathSegments); // 1743041040714597.jpg
  3002. // 3. 分割文件名获取扩展名
  3003. $fileParts = explode('.', $fileName);
  3004. $fileType = end($fileParts); // jpg
  3005. $img_datas = [
  3006. 'img_type' => $fileType,
  3007. 'name' => $fileName
  3008. ];
  3009. $up_img = WebsiteImg::where($img_id)->update($img_datas);
  3010. if (empty($up_img)) {
  3011. return Result::error("修改失败", 0);
  3012. }
  3013. return Result::success($up_img);
  3014. // 将字节转换为更友好的单位
  3015. // $friendlySize = $this->formatFileSize($size);
  3016. // $data['img_size'] = $friendlySize;
  3017. }
  3018. /**
  3019. * 修改网站模板下的板块信息
  3020. * @param array $data
  3021. */
  3022. public function delStaticResource(array $data): array
  3023. {
  3024. $where = [
  3025. 'id' => $data['id'],
  3026. ];
  3027. $result = WebsiteImg::where($where)->delete();
  3028. if (empty($result)) {
  3029. return Result::error("修改失败", 0);
  3030. }
  3031. return Result::success($result);
  3032. }
  3033. /**
  3034. * 获取尺寸列表
  3035. * @param array $data
  3036. */
  3037. public function getSizeList(array $data): array
  3038. {
  3039. $where = [];
  3040. if (isset($data['width']) && !empty($data['width'])) {
  3041. array_push($where, ['width', 'like', '%' . $data['width'] . '%']);
  3042. }
  3043. if (isset($data['height']) && !empty($data['height'])) {
  3044. array_push($where, ['height', 'like', '%' . $data['height'] . '%']);
  3045. }
  3046. $query = Size::when($where, function ($query) use ($where) {
  3047. $query->where($where);
  3048. })
  3049. ->orderBy('updated_at', 'desc');
  3050. $count = $query->count();
  3051. $rep = $query
  3052. ->offset(($data['page'] - 1) * $data['pageSize'])
  3053. ->limit($data['pageSize'])
  3054. ->get();
  3055. $result = [
  3056. 'row' => $rep,
  3057. 'count' => $count,
  3058. ];
  3059. if (empty($result)) {
  3060. return Result::error("获取失败", 0);
  3061. } else {
  3062. return Result::success($result);
  3063. }
  3064. }
  3065. /**
  3066. * 添加尺寸
  3067. * @param array $data
  3068. */
  3069. public function addSize(array $data): array
  3070. {
  3071. $size = Size::where('width', $data['width'])->where('height', $data['height'])->first();
  3072. if (!empty($size)) {
  3073. return Result::error("尺寸已存在", 0);
  3074. }
  3075. $result = Size::insertGetId($data);
  3076. if (empty($result)) {
  3077. return Result::error("添加失败", 0);
  3078. }
  3079. return Result::success($result);
  3080. }
  3081. /**
  3082. * 删除尺寸
  3083. * @param array $data
  3084. */
  3085. public function delSize(array $data): array
  3086. {
  3087. $where = [
  3088. 'id' => $data['id'],
  3089. ];
  3090. $result = Size::where($where)->delete();
  3091. if (empty($result)) {
  3092. return Result::error("删除失败", 0);
  3093. }
  3094. return Result::success($result);
  3095. }
  3096. /**
  3097. * 修改尺寸
  3098. * @param array $data
  3099. */
  3100. public function upSize(array $data): array
  3101. {
  3102. $where = [
  3103. 'id' => $data['id'],
  3104. ];
  3105. $size = Size::where('id', '!=', $data['id'])->where('width', $data['width'])->where('height', $data['height'])->first();
  3106. if (!empty($size)) {
  3107. return Result::error("尺寸已存在", 0);
  3108. }
  3109. $result = Size::where($where)->update($data);
  3110. if (empty($result)) {
  3111. return Result::error("修改失败", 0);
  3112. }
  3113. return Result::success($result);
  3114. }
  3115. /**
  3116. * 获取尺寸详情
  3117. * @param array $data
  3118. */
  3119. public function getSizeInfo(array $data): array
  3120. {
  3121. $where = [
  3122. 'id' => $data['id'],
  3123. ];
  3124. $result = Size::where($where)->first();
  3125. if (empty($result)) {
  3126. return Result::error("获取失败", 0);
  3127. } else {
  3128. return Result::success($result);
  3129. }
  3130. }
  3131. /**
  3132. * 获取网站名称缩写
  3133. * @param array $data
  3134. */
  3135. public function getWebsiteAdkey(array $data): array
  3136. {
  3137. $result = PinyinHelper::getFirstLetter($data['website_name']);
  3138. if (empty($result)) {
  3139. return Result::error("没有数据", 0);
  3140. } else {
  3141. return Result::success($result);
  3142. }
  3143. }
  3144. /**
  3145. * 静态资源管理-修改图片别名
  3146. * @param array $data
  3147. */
  3148. public function upStaticResource(array $data): array
  3149. {
  3150. $id = $data['id'];
  3151. unset($data['id']);
  3152. $result = WebsiteImg::where('id', $id)->update($data);
  3153. if (empty($result)) {
  3154. return Result::error("修改失败", 0);
  3155. }
  3156. return Result::success($result);
  3157. }
  3158. /**
  3159. * 自助建站-修改网站应用状态
  3160. * @param array $data
  3161. */
  3162. public function upWebsiteStatus(array $data): array
  3163. {
  3164. $where = [
  3165. 'website_id' => $data['website_id'],
  3166. ];
  3167. $website_template = WebsiteTemplate::where($where)->first();
  3168. unset($data['website_id']);
  3169. if ($data['status'] == 2) {
  3170. $template_status = 6;
  3171. } else {
  3172. $template_status = 1;
  3173. }
  3174. if (empty($website_template)) {
  3175. return Result::error("网站预制模板获取失败", 0);
  3176. } else {
  3177. Db::beginTransaction();
  3178. try {
  3179. $result['website_template'] = WebsiteTemplate::where($where)->update(['status' => $template_status]);
  3180. if (empty($result['website_template'])) {
  3181. return Result::error("修改网站模板状态失败", 0);
  3182. }
  3183. $result['website_template_info'] = WebsiteTemplateInfo::where($where)->update(['status' => $data['status']]);
  3184. if (empty($result['website_template_info'])) {
  3185. return Result::error("修改网站基础信息状态失败", 0);
  3186. }
  3187. Db::commit();
  3188. return Result::success($result);
  3189. } catch (\Exception $e) {
  3190. Db::rollBack();
  3191. return Result::error($e->getMessage(), 0);
  3192. }
  3193. }
  3194. }
  3195. // --自助建站-----------20250522fr----------------------end
  3196. /**
  3197. * 获取网站白名单路由地址
  3198. * @param array $data
  3199. * @return array
  3200. */
  3201. public function getWhiteRouterList(array $data): array
  3202. {
  3203. // 构建基础查询
  3204. $baseQuery = WhiteRouter::query();
  3205. // 功能名称模糊查询
  3206. if (!empty($data['function_name'])) {
  3207. $baseQuery->where('function_name', 'like', '%' . $data['function_name'] . '%');
  3208. }
  3209. // 网站ID查询 - 使用 JSON_CONTAINS 检查数组中是否包含指定的 website_id
  3210. if (!empty($data['website_id'])) {
  3211. $baseQuery->whereRaw('JSON_CONTAINS(website_id, ?)', [json_encode((int)$data['website_id'])]);
  3212. }
  3213. // 获取总数
  3214. $count = $baseQuery->count();
  3215. // 分页查询
  3216. $result = $baseQuery->orderBy('updated_at', 'desc')
  3217. ->limit($data['pageSize'])
  3218. ->offset(($data['page'] - 1) * $data['pageSize'])
  3219. ->get();
  3220. if (empty($result)) {
  3221. return Result::error("暂无数据", 0);
  3222. }
  3223. // 处理结果,将 website_id 转换为数组并获取对应的网站名称
  3224. $result->transform(function ($item) {
  3225. $websiteIds = json_decode($item->website_id, true);
  3226. $websiteNames = Website::whereIn('id', $websiteIds)
  3227. ->pluck('website_name')
  3228. ->toArray();
  3229. $item->website_names = $websiteNames;
  3230. $item->website_id = $websiteIds; // 将 website_id 转换为数组
  3231. return $item;
  3232. });
  3233. return Result::success([
  3234. 'rows' => $result->toArray(),
  3235. 'count' => $count
  3236. ]);
  3237. }
  3238. /**
  3239. * 添加网站白名单路由地址
  3240. * @param array $data
  3241. * @return array
  3242. */
  3243. public function addWhiteRouter(array $data): array
  3244. {
  3245. // 确保 website_id 是数组
  3246. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  3247. // 过滤空值并去重
  3248. $websiteIds = array_filter($websiteIds);
  3249. $websiteIds = array_unique($websiteIds);
  3250. // 将数组中的值转换为整数
  3251. $websiteIds = array_map(function ($value) {
  3252. return (int)$value;
  3253. }, $websiteIds);
  3254. // 转换为 JSON 字符串存储
  3255. $data['website_id'] = json_encode($websiteIds);
  3256. $result = WhiteRouter::insertGetId($data);
  3257. if (empty($result)) {
  3258. return Result::error("添加失败", 0);
  3259. }
  3260. return Result::success($result);
  3261. }
  3262. /**
  3263. * 修改网站白名单路由地址
  3264. * @param array $data
  3265. * @return array
  3266. */
  3267. public function upWhiteRouter(array $data): array
  3268. {
  3269. $where = [
  3270. 'id' => $data['id'],
  3271. ];
  3272. // 确保 website_id 是数组
  3273. $websiteIds = is_array($data['website_id']) ? $data['website_id'] : [$data['website_id']];
  3274. // 过滤空值并去重
  3275. $websiteIds = array_filter($websiteIds);
  3276. $websiteIds = array_unique($websiteIds);
  3277. // 将数组中的值转换为整数
  3278. $websiteIds = array_map(function ($value) {
  3279. return (int)$value;
  3280. }, $websiteIds);
  3281. // 转换为 JSON 字符串存储
  3282. $data['website_id'] = json_encode($websiteIds);
  3283. $result = WhiteRouter::where($where)->update($data);
  3284. if (empty($result)) {
  3285. return Result::error("修改失败", 0);
  3286. }
  3287. return Result::success($result);
  3288. }
  3289. /**
  3290. * 删除网站白名单路由地址
  3291. * @param array $data
  3292. * @return array
  3293. */
  3294. public function delWhiteRouter(array $data): array
  3295. {
  3296. $where = [
  3297. 'id' => $data['id'],
  3298. ];
  3299. $result = WhiteRouter::where($where)->delete();
  3300. if (empty($result)) {
  3301. return Result::error("删除失败", 0);
  3302. }
  3303. return Result::success($result);
  3304. }
  3305. /**
  3306. * 获取网站白名单路由地址详情
  3307. * @param array $data
  3308. * @return array
  3309. */
  3310. public function getWhiteRouterInfo(array $data): array
  3311. {
  3312. $where = [
  3313. 'id' => $data['id'],
  3314. ];
  3315. $result = WhiteRouter::where($where)->first();
  3316. if (empty($result)) {
  3317. return Result::error("没有查找到相关数据", 0);
  3318. }
  3319. // 将 website_id 从 JSON 字符串转换为数组
  3320. $result->website_id = json_decode($result->website_id, true);
  3321. return Result::success($result);
  3322. }
  3323. /**
  3324. * 克隆网站
  3325. * @param array $data
  3326. * @return array
  3327. */
  3328. public function cloneWebsite(array $data): array
  3329. {
  3330. DB::beginTransaction();
  3331. try {
  3332. // 获取原始网站信息
  3333. $originalWebsite = Website::find($data['website_id']);
  3334. if (!$originalWebsite) {
  3335. return Result::error("找不到该网站", 0);
  3336. }
  3337. // 提取基础名称(去除已有 _copy 或 _copy_xxx)
  3338. $baseName = preg_replace('/_copy(\d+)?$/', '', $originalWebsite->website_name);
  3339. // 查询当前系统中已存在的克隆名称数量
  3340. $count = Website::where('website_name', 'like', $baseName . '_copy%')
  3341. ->orWhere('website_name', $baseName)
  3342. ->count();
  3343. // 构造新名称:第一次是 _copy,之后是 _copy_1, _copy_2 ...
  3344. $newName = $baseName . ($count === 0 ? '_copy' : '_copy_' . $count);
  3345. // 创建克隆网站实例
  3346. $clone_website = $originalWebsite->replicate();
  3347. $clone_website->website_name = $newName;
  3348. $clone_website->save();
  3349. $clone_website->website_url = "[]";
  3350. $clone_website->save();
  3351. // var_dump("返回值:",$clone_website->id);
  3352. //克隆网站基础信息 website_template_info website_category
  3353. $resultWebsiteTemplateInfo = WebsiteTemplateInfo::where(['website_id' => $data['website_id']])->first();
  3354. $clone_website_template_info = $resultWebsiteTemplateInfo->replicate();
  3355. $clone_website_template_info->website_id = $clone_website->id;
  3356. $clone_website_template_info->save();
  3357. //克隆网站导航 获取原始 website_id 对应的所有 WebsiteCategory 记录
  3358. $originalCategories = WebsiteCategory::where('website_id', $data['website_id'])->get();
  3359. // 克隆网站后的新 ID(假设 $clone_website->id 已存在)
  3360. $newWebsiteId = $clone_website->id;
  3361. // 遍历每条记录并克隆
  3362. foreach ($originalCategories as $category) {
  3363. // 使用 replicate() 方法创建模型副本(不包含主键)
  3364. $clonedCategory = $category->replicate();
  3365. // 修改外键字段指向新网站 ID
  3366. $clonedCategory->website_id = $newWebsiteId;
  3367. // 保存克隆后的记录到数据库
  3368. $clonedCategory->save();
  3369. }
  3370. //克隆友情链接 Link 对应的是多个
  3371. $originalLinks = Link::where('website_id', $data['website_id'])->get();
  3372. foreach ($originalLinks as $link) {
  3373. // 使用 replicate() 方法创建模型副本(不包含主键)
  3374. $clonedLink = $link->replicate();
  3375. // 修改外键字段指向新网站 ID
  3376. $clonedLink->website_id = $newWebsiteId;
  3377. // 保存克隆后的记录到数据库
  3378. $clonedLink->save();
  3379. }
  3380. //克隆网站底部信息 footer_category 表 和footer_content 表 footer_category.id = footer_content.fcat_id 都是一对多关系
  3381. $originalFooterCategories = FooterCategory::where('website_id', $data['website_id'])->get();
  3382. foreach ($originalFooterCategories as $footerCategory) {
  3383. $clonedFooterCategory = $footerCategory->replicate();
  3384. $clonedFooterCategory->website_id = $newWebsiteId;
  3385. $clonedFooterCategory->save();
  3386. $originalFooterContents = FooterContent::where('fcat_id', $footerCategory->id)->get();
  3387. foreach ($originalFooterContents as $footerContent) {
  3388. $clonedFooterContent = $footerContent->replicate();
  3389. $clonedFooterContent->fcat_id = $clonedFooterCategory->id;
  3390. $clonedFooterContent->save();
  3391. }
  3392. }
  3393. Db::commit();
  3394. return Result::success([]);
  3395. } catch (\Exception $e) {
  3396. Db::rollBack();
  3397. return Result::error($e->getMessage(), 0);
  3398. }
  3399. }
  3400. /**
  3401. * 更新网站状态
  3402. * @param array $data
  3403. * @return array
  3404. */
  3405. public function updateWebsiteStatus(array $data): array
  3406. {
  3407. try {
  3408. Website::where(["id" => $data['id']])->update(["status" => intval($data['status'])]);
  3409. return Result::success([]);
  3410. } catch (\Exception $e) {
  3411. return Result::error($e->getMessage(), 0);
  3412. }
  3413. }
  3414. /**
  3415. * 删除网站url
  3416. * @param array $data
  3417. * @return array
  3418. */
  3419. public function delWebsiteUrl(array $data): array
  3420. {
  3421. try {
  3422. $wensiteInfo = Website::where(["id" => $data['website_id']])->first();
  3423. if (empty($wensiteInfo)) {
  3424. return Result::error("找不到该网站", 0);
  3425. }
  3426. $websiteUrlArray = [];
  3427. if (!empty($wensiteInfo->website_url)) {
  3428. $decoded = json_decode((string) $wensiteInfo->website_url, true);
  3429. $websiteUrlArray = is_array($decoded) ? $decoded : [];
  3430. }
  3431. // 从数组中按值删除对应的 URL
  3432. $index = array_search($data['website_url'] ?? null, $websiteUrlArray, true);
  3433. if ($index !== false) {
  3434. unset($websiteUrlArray[$index]);
  3435. // 重新索引,保持为连续的 JSON 数组
  3436. $websiteUrlArray = array_values($websiteUrlArray);
  3437. }
  3438. $newWebsiteUrlJson = json_encode($websiteUrlArray, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  3439. Website::where(["id" => $data['website_id']])->update(["website_url" => $newWebsiteUrlJson]);
  3440. return Result::success([]);
  3441. } catch (\Exception $e) {
  3442. return Result::error($e->getMessage(), 0);
  3443. }
  3444. }
  3445. }