WebsiteService.php 127 KB

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