WebsiteService.php 136 KB

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