WebsiteService.php 147 KB

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