WebsiteService.php 128 KB

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