WebsiteService.php 128 KB

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