WebsiteService.php 143 KB

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