WebsiteService.php 133 KB

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