WebsiteService.php 132 KB

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