WebsiteService.php 135 KB

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