WebsiteService.php 124 KB

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