WebsiteService.php 124 KB

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