WebsiteService.php 107 KB

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