WebsiteService.php 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  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::where('website.status', 1)
  1582. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1583. ->leftJoin("template", "template.template_id", "website_template_info.template_id")
  1584. ->leftJoin("template_class", "template_class.class_id", "template.template_class_id")
  1585. ->leftJoin('website_column', 'website_column.id', 'website.website_column_id')
  1586. ->leftJoin('website_column as parent_column', function ($join) {
  1587. $join->on('website_column.pid', '=', 'parent_column.id')
  1588. ->where('website_column.pid', 2);
  1589. })
  1590. ->whereRaw('JSON_CONTAINS(website_column_arr_id,?)', [2])
  1591. ->select(
  1592. "website_template_info.id",
  1593. "template_class.name",
  1594. "website.id as website_id",
  1595. "website.website_name",
  1596. "website.website_url",
  1597. "website_template_info.action_id",
  1598. "website_template_info.created_at",
  1599. "website_template_info.updated_at",
  1600. "website_template_info.page_type",
  1601. 'website_column.column_name',
  1602. 'parent_column.column_name as parent_column_name',
  1603. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1604. "template.template_name"
  1605. );
  1606. //若存在条件;则在$query的基础上进行筛选
  1607. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1608. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1609. }
  1610. if (isset($data['status']) && !empty($data['status'])) {
  1611. $query->where('website_template_info.status', $data['status']);
  1612. }
  1613. $count = $query->count();
  1614. $query->limit($data['pageSize'])
  1615. ->offset(($data['page'] - 1) * $data['pageSize'])
  1616. ->orderBy("website_template_info.updated_at", "desc");
  1617. $rep = $query->get();
  1618. if ($rep->count() == 0) {
  1619. return Result::error("没有查找到相关数据", 0);
  1620. } else {
  1621. $rep->each(function ($item) {
  1622. if (!empty($item->page_type)) {
  1623. $pageTypeArray = json_decode($item->page_type, true);
  1624. if (is_array($pageTypeArray)) {
  1625. $item->page_type = $pageTypeArray;
  1626. }
  1627. }
  1628. });
  1629. $result = [
  1630. "rows" => $rep->toArray(),
  1631. "count" => $count,
  1632. ];
  1633. return Result::success($result);
  1634. }
  1635. // return Result::success($result);
  1636. }
  1637. /**
  1638. * 自助建站-验证搭建网站
  1639. * @param array $data
  1640. * @return array
  1641. */
  1642. public function checkWebsiteBuild(array $data): array
  1643. {
  1644. $website = Website::where('website.id', $data['website_id'])->first();
  1645. if (empty($website)) {
  1646. return Result::error("请输入正确的网站id!", 0);
  1647. }
  1648. if (empty($website['website_column_id'])) {
  1649. return Result::error("请先关联导航池!", 0);
  1650. }
  1651. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1652. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1653. if (empty($foot_type)) {
  1654. return Result::error("请先关联单页!", 0);
  1655. }
  1656. // if (!in_array(1, $foot_type) ) {
  1657. // $missingTypes[] = "列表型底部导航";
  1658. // }
  1659. if (!in_array(0, $foot_type) ) {
  1660. $missingTypes[] = "单页(详情)";
  1661. }
  1662. if (!empty($missingTypes)) {
  1663. $errorMessage = "请先添加" . $missingTypes . "!";
  1664. return Result::error($errorMessage, 0);
  1665. }
  1666. $result['foot_type'] = $foot_type;
  1667. // 友情链接类型'1:图片 2:文字 3:底部';
  1668. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1669. $missingTypes = [];
  1670. if (!in_array(1, $types)) {
  1671. $missingTypes[] = "文字链接";
  1672. }
  1673. if (!in_array(2, $types)) {
  1674. $missingTypes[] = "图片链接";
  1675. }
  1676. if (!in_array(3, $types)) {
  1677. $missingTypes[] = "底部链接";
  1678. }
  1679. if (!empty($missingTypes)) {
  1680. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1681. return Result::error($errorMessage, 0);
  1682. }
  1683. return Result::success($result);
  1684. }
  1685. /**
  1686. * 自助建站-验证编辑网站
  1687. * @return array
  1688. *
  1689. */
  1690. public function checkWebsiteEdit(array $data): array
  1691. {
  1692. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])->first();
  1693. if (empty($result)) {
  1694. return Result::error("请输入正确的网站id!", 0);
  1695. }
  1696. // 0:未构建 1:未应用 2:已应用
  1697. if($result['status'] == 2){
  1698. return Result::error("此网站的模板正在应用中,暂不可编辑!", 0);
  1699. }
  1700. // 订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  1701. $order = Order::where('website_id', $data['website_id'])->where('status', 1)->first();
  1702. if(!empty($order)){
  1703. return Result::error("此网站的广告位正在使用中,暂不可编辑!", 0);
  1704. }
  1705. $check = $this->checkWebsiteBuild($data);
  1706. if($check['code'] != 200){
  1707. return Result::error($check['message'], 0);
  1708. }
  1709. // return Result::success($check);
  1710. if ($result['page_type']) {
  1711. $result['page_type'] = json_decode($result['page_type'], true);
  1712. return Result::success('网站编辑验证通过!');
  1713. } else {
  1714. return Result::error("请先选择网站页面类型!", 0);
  1715. }
  1716. }
  1717. /**
  1718. * 修改网站基础信息
  1719. *
  1720. * @return array
  1721. */
  1722. public function upWebsiteTemplateintel(array $data): array
  1723. {
  1724. $web = Website::where('website.id', $data['website_id'])->first();
  1725. if (empty($web)) {
  1726. return Result::error("请输入正确的网站id!", 0);
  1727. }
  1728. $where = ['website_id' => $data['website_id']];
  1729. $result = WebsiteTemplateInfo::where($where)->first();
  1730. if (empty($result)) {
  1731. return Result::error("请先添加网站基础信息!", 0);
  1732. } else {
  1733. unset($data['website_id']);
  1734. // 删除重复元素
  1735. $data['page_type'] = array_unique($data['page_type']);
  1736. // 重新索引数组
  1737. $data['page_type'] = array_values($data['page_type']);
  1738. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1739. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1740. // 将数组转换为 JSON 字符串
  1741. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1742. try {
  1743. $result = WebsiteTemplateInfo::where($where)->update($data);
  1744. } catch (\Exception $e) {
  1745. if ($e->getCode() == 22001) {
  1746. $errorMessage = $e->getMessage();
  1747. return Result::error("请检查表单,某个字段超出了长度限制!");
  1748. } else {
  1749. // 处理其他类型的数据库错误
  1750. return Result::error("修改操作失败: " . $e->getMessage());
  1751. }
  1752. return Result::error("修改失败!", 0);
  1753. }
  1754. } else {
  1755. return Result::error("请先选择首页和单页(详情)!", 0);
  1756. }
  1757. }
  1758. if (empty($result)) {
  1759. return Result::error("修改失败!", 0);
  1760. } else {
  1761. return Result::success($result);
  1762. }
  1763. }
  1764. /**
  1765. * 获取所有网站风格信息
  1766. *
  1767. * @return array
  1768. */
  1769. public function getAllTemplateClass(array $data): array
  1770. {
  1771. $result = TemplateClass::all();
  1772. if (empty($result)) {
  1773. return Result::error("没有查找到相关数据!", 0);
  1774. } else {
  1775. return Result::success($result);
  1776. }
  1777. }
  1778. /**
  1779. * 搜索并获取网站模板信息
  1780. * @param array $data
  1781. * @return array
  1782. */
  1783. public function getWebsiteTemplateList(array $data): array
  1784. {
  1785. $website = Website::where('website.id', $data['website_id'])->first();
  1786. if (empty($website)) {
  1787. return Result::error("请输入正确的网站id!", 0);
  1788. } else {
  1789. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1790. if (empty($page_type)) {
  1791. return Result::error("此网站还未添加基础信息!", 0);
  1792. } else {
  1793. $where = json_decode($page_type['page_type'], true);
  1794. // $where = $data["page_type"];
  1795. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1796. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1797. if (empty($template_class)) {
  1798. return Result::error("请输入正确的模板风格id!", 0);
  1799. } else {
  1800. // 获取指定风格下的模板
  1801. $rep = Template::where('template_class_id', $data['template_class_id'])
  1802. ->where(function ($query) use ($where) {
  1803. foreach ($where as $value) {
  1804. $query->whereJsonContains('template_img', ['value' => $value]);
  1805. }
  1806. })
  1807. ->limit($data['pageSize'])
  1808. ->offset(($data['page'] - 1) * $data['pageSize'])
  1809. ->orderBy("template.updated_at", "desc")
  1810. ->get();
  1811. $count = Template::where('template_class_id', $data['template_class_id'])
  1812. ->where(function ($query) use ($where) {
  1813. foreach ($where as $value) {
  1814. $query->whereJsonContains('template_img', ['value' => $value]);
  1815. }
  1816. })->count();
  1817. }
  1818. } else {
  1819. //获取所有模板
  1820. $rep = Template::where(function ($query) use ($where) {
  1821. foreach ($where as $value) {
  1822. $query->whereJsonContains('template_img', ['value' => $value]);
  1823. }
  1824. })
  1825. ->limit($data['pageSize'])
  1826. ->offset(($data['page'] - 1) * $data['pageSize'])
  1827. ->orderBy("template.updated_at", "desc")
  1828. ->get();
  1829. $count = Template::where(function ($query) use ($where) {
  1830. foreach ($where as $value) {
  1831. $query->whereJsonContains('template_img', ['value' => $value]);
  1832. }
  1833. })->count();
  1834. }
  1835. if (empty($rep) || $rep->count() == 0) {
  1836. return Result::error("没有查找到相关数据!", 0);
  1837. } else {
  1838. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1839. // $rep->each(function ($item) use ($where) {
  1840. // $template_img = json_decode($item->template_img, true);
  1841. // if(is_array($template_img)){
  1842. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1843. // return isset($img['value']) && in_array($img['value'], $where);
  1844. // });
  1845. // }
  1846. // $item->template_img = $filtered_img;
  1847. // });
  1848. // $result = [
  1849. // "rows" => $rep->toArray(),
  1850. // "count" => $count
  1851. // ];
  1852. $rep->each(function ($item) use ($where) {
  1853. $template_img = json_decode($item->template_img, true);
  1854. if (is_array($template_img)) {
  1855. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1856. return isset($img['value']) && in_array($img['value'], $where);
  1857. });
  1858. // 对过滤后的数组进行重新排序
  1859. $sorted_img = array_values($filtered_img);
  1860. $item->template_img = $sorted_img;
  1861. } else {
  1862. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1863. }
  1864. });
  1865. $result = [
  1866. "rows" => $rep->toArray(),
  1867. "count" => $count,
  1868. ];
  1869. }
  1870. }
  1871. }
  1872. return Result::success($result);
  1873. }
  1874. /**
  1875. * 添加网站模板
  1876. * @param array $data
  1877. */
  1878. public function addWebsiteTemplateclassintel(array $data): array
  1879. {
  1880. $template = Template::where('id', $data['template_id'])->first();
  1881. $web = Website::where('id', $data['website_id'])->first();
  1882. if (empty($template)) {
  1883. return Result::error("请输入正确的模板id", 0);
  1884. }
  1885. if (empty($web)) {
  1886. return Result::error("请输入正确的网站id", 0);
  1887. }
  1888. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1889. if (empty($result)) {
  1890. return Result::error("添加失败", 0);
  1891. } else {
  1892. return Result::success($result);
  1893. }
  1894. }
  1895. /**
  1896. * 获取网站模板信息
  1897. * @param array $data
  1898. */
  1899. public function getWebsiteTemplateclassintel(array $data): array
  1900. {
  1901. // return Result::success($data);
  1902. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1903. if (empty($template)) {
  1904. return Result::error("请输入正确的搭建网站id", 0);
  1905. }
  1906. $page_type = json_decode($template['page_type'], true);
  1907. // 获取指定网站下的基础信息和模板及风格信息
  1908. $result = WebsiteTemplateInfo::where([
  1909. ['website_template_info.website_id', $data['website_id']],
  1910. ['website_template_info.template_id', '!=', null],
  1911. ])
  1912. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1913. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1914. ->where(function ($query) use ($page_type) {
  1915. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1916. foreach ($page_type as $pageType) {
  1917. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1918. }
  1919. })
  1920. ->select(
  1921. 'template.template_name',
  1922. 'template.template_img',
  1923. 'template.id as tid',
  1924. 'template_class.name',
  1925. 'template_class.id as class_id',
  1926. 'website_template_info.page_type',
  1927. 'website_template_info.website_id'
  1928. )
  1929. ->first();
  1930. if (empty($result)) {
  1931. return Result::error("没有查找到相关数据", 0);
  1932. }
  1933. // return Result::success($result);
  1934. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1935. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1936. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1937. foreach ($template_img as $key => $value) {
  1938. if (!in_array($value['value'], $page_type)) {
  1939. unset($template_img[$key]);
  1940. }
  1941. }
  1942. // return Result::success($result);
  1943. $result['template_img'] = array_values($template_img);
  1944. $result['page_type'] = $page_type;
  1945. // $templateList = $this->getWebsiteTemplateList($data);
  1946. // $count = $templateList['data']['count']??'';
  1947. // $pages = $count / $data['pageSize'];
  1948. // $found = false;
  1949. // while ($pages && !$found) {
  1950. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1951. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1952. // if ($index !== false) {
  1953. // $found = true;
  1954. // $result['page'] = $pages;
  1955. // $pages = 0;
  1956. // // echo "查询到的 template 在第 $page 页";
  1957. // }
  1958. // $pages--;
  1959. // }
  1960. return Result::success($result);
  1961. }
  1962. /**
  1963. * 获取网站模板下的板块信息
  1964. * @param array $data
  1965. */
  1966. public function getWebsiteSectorList(array $data): array
  1967. {
  1968. $website = Website::where('id', $data['website_id'])->first();
  1969. if (empty($website)) {
  1970. return Result::error("请输入正确的网站id", 0);
  1971. }
  1972. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1973. if (empty($template['template_id'])) {
  1974. return Result::error("请先添加网站模板", 0);
  1975. }
  1976. $query = Sector::where('template_id', $template['template_id']);
  1977. if ($query->count() == 0) {
  1978. return Result::error("没有查找到相关板块数据", 0);
  1979. } elseif ($query->count() == 1) {
  1980. $sector = $query->first();
  1981. } else {
  1982. $sector = $query->get();
  1983. }
  1984. $result['page_type'] = $template['page_type'];
  1985. $result['tid'] = $template['template_id'];
  1986. $result = [
  1987. "tid" => $template['template_id'],
  1988. "page_type" => $template['page_type'],
  1989. "sector" => $sector,
  1990. ];
  1991. return Result::success($result);
  1992. }
  1993. // 自助建站---------------fr---------------
  1994. // 网站底基础信息--fr-------------------
  1995. /**
  1996. * 添加网站底基础信息
  1997. * @param array $data
  1998. */
  1999. public function addWebFootInfo(array $data): array
  2000. {
  2001. $website = Website::where('id', $data['website_id'])->first();
  2002. if (empty($website)) {
  2003. return Result::error("请输入正确的网站id!", 0);
  2004. }
  2005. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2006. if (!empty($info)) {
  2007. return Result::error("该网站已经添加过了!", 0);
  2008. }
  2009. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  2010. if (empty($result)) {
  2011. return Result::error("请先添加网站基础信息!", 0);
  2012. } else {
  2013. return Result::success($result);
  2014. }
  2015. }
  2016. /**
  2017. * 获取网站底基础信息
  2018. * @param array $data
  2019. */
  2020. public function getWebFootInfo(array $data): array
  2021. {
  2022. $website = Website::where('id', $data['website_id'])->first();
  2023. if (empty($website)) {
  2024. return Result::error("请输入正确的网站id!", 0);
  2025. }
  2026. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2027. if (empty($result)) {
  2028. return Result::error("请先添加网站基础信息!", 0);
  2029. }
  2030. return Result::success($result);
  2031. }
  2032. /**
  2033. * 修改网站底基础信息
  2034. * @param array $data
  2035. */
  2036. public function upWebFootInfo(array $data): array
  2037. {
  2038. // return Result::success($data);
  2039. $website = Website::where('id', $data['website_id'])->first();
  2040. if (empty($website)) {
  2041. return Result::error("请输入正确的网站id!", 0);
  2042. }
  2043. $web = $data['website_id'];
  2044. unset($data['website_id']);
  2045. $where = ['website_id' => $web];
  2046. // return Result::success($data);
  2047. $result = WebsiteTemplateInfo::where($where)->first();
  2048. if (empty($result)) {
  2049. $data['website_id'] = $web;
  2050. $result = WebsiteTemplateInfo::insertGetId($data);
  2051. // return Result::error("请先添加网站基础信息!",0);
  2052. } else {
  2053. $result = WebsiteTemplateInfo::where($where)->update($data);
  2054. }
  2055. // $where = ['website_id'=>$data['website_id']];
  2056. // unset($data['website_id']);
  2057. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  2058. if (empty($result)) {
  2059. return Result::error("修改失败!", 0);
  2060. }
  2061. return Result::success($result);
  2062. }
  2063. /*
  2064. * 获取父级/子级栏目------路由
  2065. * @param array $data
  2066. * @return array
  2067. * */
  2068. public function getWebsiteParentCategory(array $data): array
  2069. {
  2070. // return Result::success($data);
  2071. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2072. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2073. }
  2074. if (empty($website)) {
  2075. return Result::error("暂无该网站", 0);
  2076. }
  2077. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2078. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2079. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2080. ->where('pid', 0)
  2081. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2082. ->get() // 添加 get() 方法获取结果集合
  2083. ->all();
  2084. // 提前查询所有父级栏目,避免在 map 中多次查询
  2085. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2086. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2087. ->where('pid', 0)
  2088. ->select('category_id', 'aLIas_pinyin')
  2089. ->get()
  2090. ->keyBy('category_id');
  2091. // 直接查询子栏目信息
  2092. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2093. ->where('pid', '!=', 0)
  2094. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2095. ->get();
  2096. // 使用集合的 map 方法处理子栏目信息
  2097. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2098. $alias = $item->alias;
  2099. $category_id = $item->category_id;
  2100. $aLIas_pinyin = $item->aLIas_pinyin;
  2101. $parent = $parentCategories->get($item->pid);
  2102. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2103. $result = [
  2104. 'name' => $alias,
  2105. 'cid' => $category_id
  2106. ];
  2107. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2108. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2109. }
  2110. return $result;
  2111. })->values()->all();
  2112. if (empty($category)) {
  2113. return Result::error("暂无此导航", 0);
  2114. }
  2115. return Result::success($category);
  2116. }
  2117. /**
  2118. * 获取网站栏目信息
  2119. * @param array $data
  2120. */
  2121. public function getWebsiteFootAll(array $data): array
  2122. {
  2123. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2124. if (empty($website)) {
  2125. return Result::error("暂无该网站", 0);
  2126. }
  2127. // 1:图片 2:文字 3:底部
  2128. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2129. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2130. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2131. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2132. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  2133. if (empty($result)) {
  2134. return Result::error("暂无此网站信息", 0);
  2135. }
  2136. return Result::success($result);
  2137. }
  2138. /**
  2139. * 获取网站顶部信息
  2140. * @param array $data
  2141. */
  2142. public function getWebsiteHead(array $data): array
  2143. {
  2144. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2145. if (empty($result)) {
  2146. return Result::error("暂无该网站", 0);
  2147. }
  2148. return Result::success($result);
  2149. }
  2150. /**
  2151. * 路由匹配
  2152. * @param array $data
  2153. */
  2154. public function getWebsiteRoute(array $data): array
  2155. {
  2156. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2157. if (empty($website)) {
  2158. return Result::error("暂无该网站", 0);
  2159. }
  2160. if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
  2161. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2162. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2163. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2164. $join->on('pc.pid', '=', 'website_category.category_id')
  2165. ->where('pc.website_id', '=', $data['website_id']);
  2166. })
  2167. ->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'))
  2168. ->first();
  2169. }
  2170. if(isset($data['foot_pinyin']) &&!empty($data['foot_pinyin'])){
  2171. $result = FooterCategory::where('website_id',$data['website_id'])->where('name_pinyin',$data['foot_pinyin'])->first(['id']);
  2172. }
  2173. if (!isset($result) || empty($result)) {
  2174. return Result::error("暂无该导航", 0);
  2175. }
  2176. return Result::success($result);
  2177. }
  2178. //20250212 网站标识
  2179. public function addWebsiteGroup(array $data): array
  2180. {
  2181. //判斷name是不是重复
  2182. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2183. if (!empty($websiteGroupInfo)) {
  2184. return Result::error("网站标识重复", 0);
  2185. }
  2186. //添加信息
  2187. $result = WebsiteGroup::insertGetId($data);
  2188. var_dump($result);
  2189. if (empty($result)) {
  2190. return Result::error("创建失败", 0);
  2191. } else {
  2192. return Result::success($result);
  2193. }
  2194. }
  2195. public function getWebsiteGroupList(array $data): array
  2196. {
  2197. $where = [];
  2198. if (isset($data['name']) && !empty($data['name'])) {
  2199. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2200. }
  2201. $result = WebsiteGroup::where($where)
  2202. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2203. ->get();
  2204. foreach ($result as $websiteGroup) {
  2205. $webIds = json_decode($websiteGroup->web_ids, true);
  2206. $websites = Website::whereIn('id', $webIds)->get();
  2207. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2208. $websiteGroup->website_names = $websiteNames;
  2209. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2210. }
  2211. $count = WebsiteGroup::where($where)->count();
  2212. if (empty($result)) {
  2213. return Result::error("没有数据", 0);
  2214. }
  2215. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2216. }
  2217. public function getWebsiteGroupInfo(array $data): array
  2218. {
  2219. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2220. $webIds = json_decode($websiteInfo->web_ids, true);
  2221. $websites = Website::whereIn('id', $webIds)->get();
  2222. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2223. $websiteInfo->website_names = $websiteNames;
  2224. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2225. if (empty($websiteInfo)) {
  2226. return Result::error("找不到URL", 0);
  2227. }
  2228. return Result::success($websiteInfo->toArray());
  2229. }
  2230. public function updateWebsiteGroup(array $data): array
  2231. {
  2232. $where = [
  2233. 'id' => $data['id'],
  2234. ];
  2235. $insertData = [
  2236. 'name' => $data['name'],
  2237. 'web_ids' => $data['web_ids'],
  2238. ];
  2239. //判斷name是不是重复
  2240. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2241. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2242. return Result::error("网站标识重复", 0);
  2243. }
  2244. $result = WebsiteGroup::where($where)->update($insertData);
  2245. var_dump($result, '------更新');
  2246. if (empty($result)) {
  2247. return Result::error("更新失败", 0);
  2248. } else {
  2249. return Result::success();
  2250. }
  2251. }
  2252. public function deleteWebsiteGroup(array $data): array
  2253. {
  2254. $where = [
  2255. 'id' => $data['id'],
  2256. ];
  2257. //看看user表是不是有这个id
  2258. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2259. if (!empty($userGroup)) {
  2260. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2261. }
  2262. $result = WebsiteGroup::where($where)->delete();
  2263. if (empty($result)) {
  2264. return Result::error("删除失败", 0);
  2265. } else {
  2266. return Result::success();
  2267. }
  2268. }
  2269. public function getWebsiteNavList(array $data): array
  2270. {
  2271. $data['type'] = isset($data['type']) ? $data['type'] : 1;
  2272. $where = [];
  2273. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2274. array_push($where, ['website_id', '=', $data['website_id']]);
  2275. }
  2276. if (isset($data['pid'])) {
  2277. array_push($where, ['pid', '=', $data['pid']]);
  2278. }
  2279. if (isset($data['type']) && !empty($data['type'])) {
  2280. array_push($where, ['type', '=', $data['type']]);
  2281. }
  2282. // 根据站点和type获取websiteCategory 中的id
  2283. $websiteCategoryids = WebsiteCategory::query()
  2284. ->where('website_id', '=', $data['website_id'])
  2285. ->where('type', '=', $data['type'])
  2286. ->pluck('category_arr_id')->toArray();
  2287. var_dump($websiteCategoryids, '------获取websiteCategory 中的id');
  2288. //合并去重
  2289. $category_ids = [];
  2290. foreach ($websiteCategoryids as $key => $value) {
  2291. $v = json_decode($value, true);
  2292. $category_ids = array_unique(array_merge($category_ids, $v));
  2293. }
  2294. // var_dump($category_ids, '------获取websiteCategory 中的id');
  2295. //根据pid取出所有的websiteCategory 中的id
  2296. $list = WebsiteCategory::query()
  2297. ->where('website_id', '=', $data['website_id'])
  2298. ->where('pid', '=', $data['pid'])
  2299. ->whereIn('category_id', $category_ids)
  2300. ->get();
  2301. if (empty($list)) {
  2302. return Result::error("获取失败", 0);
  2303. } else {
  2304. return Result::success($list);
  2305. }
  2306. }
  2307. // 20250307 根据网站标识获和导航获取站点
  2308. public function getWebsiteNavPoolSite(array $data): array
  2309. {
  2310. $where = [];
  2311. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2312. array_push($where, ['category_id', '=', $data['category_id']]);
  2313. }
  2314. $list = WebsiteCategory::query()->where($where)
  2315. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2316. ->select('website_category.*', 'website.website_name')
  2317. ->distinct()
  2318. ->get();
  2319. if (empty($list)) {
  2320. return Result::error("获取失败", 0);
  2321. } else {
  2322. return Result::success($list);
  2323. }
  2324. }
  2325. public function getWebsiteNavPool(array $data): array
  2326. {
  2327. // 初始化查询条件数组
  2328. $query = WebsiteCategory::query();
  2329. // 如果传入了 websiteids,则添加到查询条件中
  2330. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2331. $query->whereIn('website_id', $data['websiteids']);
  2332. }
  2333. // 如果传入了 pid,则添加到查询条件中
  2334. if (isset($data['pid'])) {
  2335. $query->where('pid', '=', $data['pid']);
  2336. }
  2337. // 执行查询 唯一性 id
  2338. $list = $query->distinct()->get();
  2339. // 根据查询结果返回相应的结果
  2340. if ($list->isEmpty()) {
  2341. return Result::error("获取失败", 0);
  2342. } else {
  2343. return Result::success($list->toArray());
  2344. }
  2345. }
  2346. /**
  2347. *
  2348. * @param array $data
  2349. * @return array
  2350. */
  2351. public function getAllCategory(array $data): array
  2352. {
  2353. $result = Category::when($data, function ($query) use ($data) {
  2354. if (isset($data['name']) && !empty($data['name'])) {
  2355. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2356. }
  2357. })->get();
  2358. if (empty($result)) {
  2359. return Result::error("获取失败", 0);
  2360. } else {
  2361. return Result::success($result);
  2362. }
  2363. }
  2364. /**
  2365. * 修改网站栏目排序
  2366. * @param array $data
  2367. * @return array
  2368. */
  2369. public function upWebsiteCategorySort(array $data): array
  2370. {
  2371. $where = [
  2372. 'website_id' => $data['website_id'],
  2373. 'id' => $data['id'],
  2374. ];
  2375. $result = WebsiteCategory::where($where)->update($data);
  2376. if ($result) {
  2377. return Result::success($result);
  2378. } else {
  2379. return Result::error("更新失败", 0);
  2380. }
  2381. }
  2382. // --自助建站-----------20250522fr----------------------start
  2383. /**
  2384. * 获取网站模板下的板块信息
  2385. * @param array $data
  2386. * @return array
  2387. */
  2388. public function getStaticResourceList(array $data): array
  2389. {
  2390. // return Result::success($data);
  2391. $where = [];
  2392. if(isset($data['img_alias']) &&!empty($data['img_alias'])){
  2393. array_push($where, ['website_img.img_alias', 'like', '%'. $data['img_alias']. '%']);
  2394. }
  2395. if(isset($data['img_type']) &&!empty($data['img_type'])){
  2396. array_push($where, ['website_img.img_type', '=', $data['img_type']]);
  2397. }
  2398. if(isset($data['website_name']) &&!empty($data['website_name'])){
  2399. $website = Website::where('website_name','like','%'.$data['website_name'].'%')->first();
  2400. if(empty($website)){
  2401. return Result::error("没有查找到相关数据", 0);
  2402. }else{
  2403. array_push($where, ['website_img.website_id', '=', $website['id']]);
  2404. }
  2405. }
  2406. // return Result::success($where);
  2407. $query = WebsiteImg::when(!empty($where), function ($query) use ($where) {
  2408. $query->where($where);
  2409. })
  2410. ->orderBy('updated_at', 'desc');
  2411. $count = $query->count();
  2412. $row = $query->leftJoin('website', 'website.id', '=', 'website_img.website_id')
  2413. ->select('website_img.*', 'website.website_name')
  2414. ->limit($data['pageSize'])
  2415. ->offset(($data['page'] - 1) * $data['pageSize'])
  2416. ->get();
  2417. if (empty($row)) {
  2418. return Result::error("没有查找到相关数据", 0);
  2419. } else {
  2420. $result = [
  2421. 'row' => $row,
  2422. 'count' => $count,
  2423. ];}
  2424. return Result::success($result);
  2425. }
  2426. /**
  2427. * 添加网站模板下的板块信息
  2428. * @param array $data
  2429. */
  2430. public function addStaticResource(array $data): array
  2431. {
  2432. // $file = $data['img_url']->file('image');
  2433. // $size = $file->getSize();
  2434. $img_data = [
  2435. 'website_id' => $data['website_id'] ?? 0,
  2436. 'img_alias' => $data['img_alias'] ?? '',
  2437. 'img_url' => $data['img_url'],
  2438. 'img_size' => $data['img_size'],
  2439. ];
  2440. $img = WebsiteImg::insertGetId($img_data);
  2441. if (empty($img)) {
  2442. return Result::error("添加失败", 0);
  2443. }
  2444. $img_id['id'] = $img;
  2445. $url = WebsiteImg::where($img_id)->value('img_url');
  2446. // 1. 分割路径部分(以 / 为分隔符)
  2447. $pathSegments = explode('/', parse_url($url, PHP_URL_PATH));
  2448. // 2. 取最后一个元素作为文件名
  2449. $fileName = end($pathSegments); // 1743041040714597.jpg
  2450. // 3. 分割文件名获取扩展名
  2451. $fileParts = explode('.', $fileName);
  2452. $fileType = end($fileParts); // jpg
  2453. $img_datas = [
  2454. 'img_type' => $fileType,
  2455. 'name' => $fileName
  2456. ];
  2457. $up_img = WebsiteImg::where($img_id)->update($img_datas);
  2458. if (empty($up_img)) {
  2459. return Result::error("修改失败", 0);
  2460. }
  2461. return Result::success($up_img);
  2462. // 将字节转换为更友好的单位
  2463. // $friendlySize = $this->formatFileSize($size);
  2464. // $data['img_size'] = $friendlySize;
  2465. }
  2466. /**
  2467. * 修改网站模板下的板块信息
  2468. * @param array $data
  2469. */
  2470. public function delStaticResource(array $data): array
  2471. {
  2472. $where = [
  2473. 'id' => $data['id'],
  2474. ];
  2475. $result = WebsiteImg::where($where)->delete();
  2476. if (empty($result)) {
  2477. return Result::error("修改失败", 0);
  2478. }
  2479. return Result::success($result);
  2480. }
  2481. /**
  2482. * 获取尺寸列表
  2483. * @param array $data
  2484. */
  2485. public function getSizeList(array $data): array
  2486. {
  2487. $where = [];
  2488. if(isset($data['width']) &&!empty($data['width'])){
  2489. array_push($where, ['width','like','%'.$data['width'].'%']);
  2490. }
  2491. if(isset($data['height']) &&!empty($data['height'])){
  2492. array_push($where, ['height','like','%'.$data['height'].'%']);
  2493. }
  2494. $query = Size::when($where, function ($query) use ($where) {
  2495. $query->where($where);
  2496. })
  2497. ->orderBy('updated_at', 'desc');
  2498. $count = $query->count();
  2499. $rep = $query
  2500. ->offset(($data['page'] - 1) * $data['pageSize'])
  2501. ->limit($data['pageSize'])
  2502. ->get();
  2503. $result = [
  2504. 'row' => $rep,
  2505. 'count' => $count,
  2506. ];
  2507. if (empty($result)) {
  2508. return Result::error("获取失败", 0);
  2509. } else {
  2510. return Result::success($result);
  2511. }
  2512. }
  2513. /**
  2514. * 添加尺寸
  2515. * @param array $data
  2516. */
  2517. public function addSize(array $data): array
  2518. {
  2519. $size = Size::where('width', $data['width'])->where('height', $data['height'])->first();
  2520. if (!empty($size)) {
  2521. return Result::error("尺寸已存在", 0);
  2522. }
  2523. $result = Size::insertGetId($data);
  2524. if (empty($result)) {
  2525. return Result::error("添加失败", 0);
  2526. }
  2527. return Result::success($result);
  2528. }
  2529. /**
  2530. * 删除尺寸
  2531. * @param array $data
  2532. */
  2533. public function delSize(array $data): array
  2534. {
  2535. $where = [
  2536. 'id' => $data['id'],
  2537. ];
  2538. $result = Size::where($where)->delete();
  2539. if (empty($result)) {
  2540. return Result::error("删除失败", 0);
  2541. }
  2542. return Result::success($result);
  2543. }
  2544. /**
  2545. * 修改尺寸
  2546. * @param array $data
  2547. */
  2548. public function upSize(array $data): array
  2549. {
  2550. $where = [
  2551. 'id' => $data['id'],
  2552. ];
  2553. $size = Size::where('id','!=', $data['id'])->where('width',$data['width'])->where('height',$data['height'])->first();
  2554. if (!empty($size)) {
  2555. return Result::error("尺寸已存在", 0);
  2556. }
  2557. $result = Size::where($where)->update($data);
  2558. if (empty($result)) {
  2559. return Result::error("修改失败", 0);
  2560. }
  2561. return Result::success($result);
  2562. }
  2563. /**
  2564. * 获取尺寸详情
  2565. * @param array $data
  2566. */
  2567. public function getSizeInfo(array $data): array
  2568. {
  2569. $where = [
  2570. 'id' => $data['id'],
  2571. ];
  2572. $result = Size::where($where)->first();
  2573. if (empty($result)) {
  2574. return Result::error("获取失败", 0);
  2575. } else {
  2576. return Result::success($result);
  2577. }
  2578. }
  2579. /**
  2580. * 获取网站名称缩写
  2581. * @param array $data
  2582. */
  2583. public function getWebsiteAdkey(array $data): array
  2584. {
  2585. $result = PinyinHelper::getFirstLetter($data['website_name']);
  2586. if (empty($result)) {
  2587. return Result::error("没有数据", 0);
  2588. } else {
  2589. return Result::success($result);
  2590. }
  2591. }
  2592. /**
  2593. * 静态资源管理-修改图片别名
  2594. * @param array $data
  2595. */
  2596. public function upStaticResource(array $data): array
  2597. {
  2598. $id = $data['id'];
  2599. unset($data['id']);
  2600. $result = WebsiteImg::where('id', $id)->update($data);
  2601. if (empty($result)) {
  2602. return Result::error("修改失败", 0);
  2603. }
  2604. return Result::success($result);
  2605. }
  2606. /**
  2607. * 自助建站-修改网站应用状态
  2608. * @param array $data
  2609. */
  2610. public function upWebsiteStatus(array $data): array
  2611. {
  2612. $where = [
  2613. 'website_id' => $data['website_id'],
  2614. ];
  2615. $website_template = WebsiteTemplate::where($where)->first();
  2616. unset($data['website_id']);
  2617. if($data['status'] == 2){
  2618. $template_status = 6;
  2619. }else{
  2620. $template_status = 1;
  2621. }
  2622. if (empty($website_template)) {
  2623. return Result::error("网站预制模板获取失败", 0);
  2624. } else {
  2625. Db::beginTransaction();
  2626. try {
  2627. $result['website_template'] = WebsiteTemplate::where($where)->update(['status'=>$template_status]);
  2628. if (empty($result['website_template'])) {
  2629. return Result::error("修改网站模板状态失败", 0);
  2630. }
  2631. $result['website_template_info'] = WebsiteTemplateInfo::where($where)->update(['status'=>$data['status']]);
  2632. if (empty($result['website_template_info'])) {
  2633. return Result::error("修改网站基础信息状态失败", 0);
  2634. }
  2635. Db::commit();
  2636. return Result::success($result);
  2637. } catch (\Exception $e) {
  2638. Db::rollBack();
  2639. return Result::error($e->getMessage(), 0);
  2640. }
  2641. }
  2642. }
  2643. // --自助建站-----------20250522fr----------------------end
  2644. }