WebsiteService.php 135 KB

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