WebsiteService.php 101 KB

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