NewsService.php 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\ArticleData;
  5. use App\Model\Category;
  6. use App\Model\Website;
  7. use App\Model\WebsiteCategory;
  8. use App\Model\Good;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\RpcServer\Annotation\RpcService;
  11. use App\Tools\Result;
  12. use App\Model\ArticleSurvey;
  13. use App\Model\District;
  14. use App\Tools\buildMenuTree;
  15. use App\Model\Book;
  16. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  17. class NewsService implements NewsServiceInterface
  18. {
  19. /**
  20. * 获取导航池列表
  21. * @param array $data
  22. * @return array
  23. */
  24. public function getCategoryList(array $data): array
  25. {
  26. $where = [];
  27. if(isset($data['name']) && $data['name']){
  28. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  29. }
  30. if(isset($data['department_id']) && $data['department_id']){
  31. array_push($where, ['category.department_id','=',$data['department_id']]);
  32. }
  33. if(isset($data['city_id']) && $data['city_id']){
  34. array_push($where, ['category.city_id','=',$data['city_id']]);
  35. }
  36. $rep = Category::where($where)
  37. ->leftJoin('district','category.city_id','district.id')
  38. ->leftJoin('department','category.department_id','department.id')
  39. ->select("category.*","district.name as city_name","department.name as department_name")
  40. ->limit($data['pageSize'])->orderBy("category.sort","desc")->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  41. $count = Category::where($where)->count();
  42. $data = [
  43. 'rows'=>$rep->toArray(),
  44. 'count'=>$count
  45. ];
  46. if(empty($rep->toArray())){
  47. return Result::error("没有导航池数据");
  48. }
  49. return Result::success($data);
  50. }
  51. /**
  52. * @param array $data
  53. * @return array
  54. */
  55. public function categoryList(array $data): array
  56. {
  57. $where[] = [
  58. 'pid','=',$data['pid']
  59. ];
  60. if(isset($data['name'])){
  61. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  62. }
  63. var_dump($where);
  64. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  65. if(empty($result)){
  66. return Result::error("没有栏目数据");
  67. }
  68. return Result::success($result);
  69. }
  70. /**
  71. * @param array $data
  72. * @return array
  73. */
  74. public function addCategory(array $data): array
  75. {
  76. $id = Category::insertGetId($data);
  77. if(empty($id)){
  78. return Result::error("添加失败");
  79. }
  80. return Result::success(['id'=>$id]);
  81. }
  82. /**
  83. * @param array $data
  84. * @return array
  85. */
  86. public function delCategory(array $data): array
  87. {
  88. $categoryList = Category::where(['pid'=>$data['id']])->get();
  89. var_dump("分类列表:",$data,$categoryList);
  90. if($categoryList->toArray()){
  91. return Result::error("分类下面有子分类不能删除");
  92. }
  93. $articleList = Article::where(['catid'=>$data['id']])->get();
  94. var_dump("文章列表:",$articleList);
  95. if($articleList->toArray()){
  96. return Result::error("分类下面有资讯不能删除");
  97. }
  98. $result = Category::where($data)->delete();
  99. if(!$result){
  100. return Result::error("删除失败");
  101. }
  102. return Result::success($result);
  103. }
  104. /**
  105. * @param array $data
  106. * @return array
  107. */
  108. public function updateCategory(array $data): array
  109. {
  110. $where = [
  111. 'id'=>$data['id']
  112. ];
  113. $result = Category::where($where)->update($data);
  114. if($result){
  115. return Result::success($result);
  116. }else{
  117. return Result::error("更新失败");
  118. }
  119. }
  120. /**
  121. * 获取导航池信息
  122. * @param array $data
  123. * @return array
  124. */
  125. public function getCategoryInfo(array $data): array
  126. {
  127. $where = [
  128. 'id'=>$data['id']
  129. ];
  130. $result = Category::where($where)->first();
  131. if($result){
  132. return Result::success($result);
  133. }else{
  134. return Result::error("更新失败");
  135. }
  136. }
  137. /**
  138. * @param array $data
  139. * @return array
  140. */
  141. public function getArticleList(array $data): array
  142. {
  143. $where= [];
  144. if(isset($data['title']) && $data['title']){
  145. array_push($where,['article.title','like','%'.$data['title'].'%']);
  146. }
  147. if(isset($data['category_name']) && $data['category_name']){
  148. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  149. }
  150. if(isset($data['author']) && $data['author']){
  151. array_push($where,['article.author','=',$data['author']]);
  152. }
  153. if(isset($data['islink']) && $data['islink']!==""){
  154. array_push($where,['article.islink','=',$data['islink']]);
  155. }
  156. if(isset($data['status']) && $data['status']!==""){
  157. array_push($where,['article.status','=',$data['status']]);
  158. }
  159. $rep = Article::where($where)
  160. ->whereNotIn('article.status',[404])
  161. ->leftJoin('category','article.catid','category.id')
  162. ->select("article.*","category.name as category_name")
  163. ->orderBy("article.id","desc")
  164. ->limit($data['pageSize'])
  165. ->offset(($data['page']-1)*$data['pageSize'])->get();
  166. $count = Article::where($where)->whereNotIn('article.status',[404])
  167. ->leftJoin('category','article.catid','category.id')->count();
  168. $data = [
  169. 'rows'=>$rep->toArray(),
  170. 'count'=>$count
  171. ];
  172. if(empty($rep)){
  173. return Result::error("没有信息数据");
  174. }
  175. return Result::success($data);
  176. }
  177. /**
  178. * @param array $data
  179. * @return array
  180. */
  181. public function addArticle(array $data): array
  182. {
  183. Db::beginTransaction();
  184. try{
  185. $articleData = $data;
  186. unset($articleData['content']);
  187. $id = Article::insertGetId($articleData);
  188. $articleDataContent = [
  189. 'article_id'=>$id,
  190. 'content'=>$data['content']
  191. ];
  192. ArticleData::insertGetId($articleDataContent);
  193. Db::commit();
  194. } catch(\Throwable $ex){
  195. Db::rollBack();
  196. var_dump($ex->getMessage());
  197. return Result::error("创建失败",0);
  198. }
  199. return Result::success(['id'=>$id]);
  200. }
  201. /**
  202. * @param array $data
  203. * @return array
  204. */
  205. public function delArticle(array $data): array
  206. {
  207. $result = Article::where($data)->update(['status'=>404]);
  208. if(!$result){
  209. return Result::error("删除失败");
  210. }
  211. return Result::success($result);
  212. }
  213. /**
  214. * @param array $data
  215. * @return array
  216. */
  217. public function updateArticle(array $data): array
  218. {
  219. Db::beginTransaction();
  220. try{
  221. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  222. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  223. $articleData = $data;
  224. unset($articleData['content']);
  225. unset($articleData['status_name']);
  226. unset($articleData['name']);
  227. unset($articleData['content']);
  228. unset($articleData['pid_arr']);
  229. unset($articleData['pid']);
  230. $id = Article::where(['id'=>$data['id']])->update($articleData);
  231. $articleDataContent = [
  232. 'content'=>$data['content']
  233. ];
  234. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  235. } catch(\Throwable $ex){
  236. Db::rollBack();
  237. var_dump($ex->getMessage());
  238. return Result::error("更新失败",0);
  239. }
  240. return Result::success([]);
  241. }
  242. /**
  243. * 更新资讯状态
  244. * @param array $data
  245. * @return array
  246. */
  247. public function upArticleStatus(array $data):array
  248. {
  249. $result = Article::where(['id'=>$data['id']])->update($data);
  250. if($result){
  251. return Result::success();
  252. }else{
  253. return Result::error("更新状态失败",0);
  254. }
  255. }
  256. /**
  257. * 获取新闻详情
  258. * @param array $data
  259. * @return array
  260. */
  261. public function getArticleInfo(array $data): array
  262. {
  263. $where = [
  264. 'article.id'=>$data['id'],
  265. // 'article.status'=>1
  266. ];
  267. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  268. if(empty($result)){
  269. return Result::error("查询失败",0);
  270. }
  271. return Result::success($result);
  272. }
  273. /**
  274. * 获取头条新闻
  275. * @param array $data
  276. * @return array
  277. */
  278. public function getWebsiteArticlett(array $data): array
  279. {
  280. // return Result::success($data['website_id']);
  281. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  282. $category = array_values(array_unique($category->toArray()));
  283. if ($category) {
  284. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  285. $where = [
  286. 'status' => 1,
  287. ];
  288. var_dump($data, 'data-----------------');
  289. // level=7 根据文章key来匹配文章
  290. if (isset($data['level']) && $data['level'] == 7) {
  291. // 根据文章id获取key
  292. // $data['id'] = 50142;
  293. if (isset($data['id']) && !empty($data['id'])) {
  294. $keyword = Article::where('id', $data['id'])->value('keyword');
  295. $keywordArray = explode(',', $keyword);
  296. $whereL7 = [];
  297. foreach ($keywordArray as $k => $v) {
  298. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  299. }
  300. // 原始查询
  301. $result['level7'] = Article::where($whereL7)
  302. ->when(!empty($data['imgnum']), function ($query) use ($data) {
  303. $query->where('article.imgurl', '!=', '')
  304. ->select('article.*')
  305. ->offset($data['placeid'])
  306. ->limit($data['imgnum']);
  307. })
  308. ->when(!empty($data['textnum']), function ($query) use ($data) {
  309. $query->select('article.*')
  310. ->offset($data['placeid'])
  311. ->limit($data['textnum']);
  312. })
  313. ->orderBy('updated_at', 'desc')
  314. ->get()
  315. ->map(function ($article ) use ($data) {
  316. $catid = $article->catid;
  317. $pinyin = '';
  318. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  319. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  320. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  321. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  322. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  323. }
  324. } else {
  325. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  326. }
  327. $article->pinyin = $pinyin;
  328. return $article;
  329. });
  330. if (empty($result)) {
  331. return Result::success();
  332. }
  333. return Result::success($result);
  334. } else {
  335. return Result::error("参数错误level=7,id不能为空", 0);
  336. }
  337. }
  338. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  339. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  340. var_dump($where, 'where-----------------');
  341. // 初始化基础查询
  342. $query = Article::where($where)
  343. ->whereIn("catid", $category)
  344. ->where(function ($query) use ($data) {
  345. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  346. ->orWhereNull("ignore_ids");
  347. });
  348. // 处理 level 相关查询条件
  349. $query->when($data['level'] == 5, function ($query) {
  350. $query->inRandomOrder()
  351. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  352. })->when($data['level'] == 4, function ($query) {
  353. $query->orderBy("article.updated_at", "desc");
  354. })->when(!empty($data['level']), function ($query) use ($data) {
  355. if ($data['level'] != 4 && $data['level'] != 5) {
  356. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  357. ->orderBy("article.updated_at", "desc");
  358. }
  359. });
  360. // 初始化结果数组
  361. $result = [];
  362. // $input = $data['website_id'];
  363. // 处理图片新闻查询
  364. if (!empty($data['imgnum'])) {
  365. // 原错误提示表明 cleanBindings 方法需要传入数组类型的参数,而实际传入了 null。
  366. // 这里克隆查询构建器,确保绑定参数为数组,避免该错误。
  367. $imgQuery = clone $query;
  368. if ($imgQuery->getBindings() === null) {
  369. $imgQuery->setBindings([]);
  370. }
  371. $result['img'] = $imgQuery->where('article.imgurl', '!=', '')
  372. ->select('article.id',
  373. 'article.title',
  374. 'article.imgurl',
  375. 'article.author',
  376. 'article.updated_at',
  377. 'article.introduce',
  378. 'article.islink',
  379. 'article.linkurl',
  380. 'article.copyfrom',
  381. 'article.catid')
  382. ->offset($data['placeid'])
  383. ->limit($data['imgnum'])
  384. ->get()
  385. ->map(function ($article ) use ($data) {
  386. $catid = $article->catid;
  387. $pinyin = '';
  388. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  389. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  390. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  391. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  392. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  393. }
  394. } else {
  395. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  396. }
  397. $article->pinyin = $pinyin;
  398. return $article;
  399. });
  400. // $result['img'] = $this->processArticles($img_article, $input);
  401. }
  402. // 处理文字新闻查询
  403. if (!empty($data['textnum'])) {
  404. $textQuery = clone $query;
  405. $result['text'] = $textQuery
  406. ->select('article.id',
  407. 'article.title',
  408. 'article.imgurl',
  409. 'article.author',
  410. 'article.updated_at',
  411. 'article.introduce',
  412. 'article.islink',
  413. 'article.linkurl',
  414. 'article.copyfrom',
  415. 'article.catid')
  416. ->offset($data['placeid'])
  417. ->limit($data['textnum'])
  418. ->get()
  419. ->map(function ($article ) use ($data) {
  420. $catid = $article->catid;
  421. $pinyin = '';
  422. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  423. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  424. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  425. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  426. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  427. }
  428. } else {
  429. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  430. }
  431. $article->pinyin = $pinyin;
  432. return $article;
  433. });
  434. // $result['text'] = $this->processArticles($text_article, $input);
  435. }
  436. if (empty($result) || count($result) == 0) {
  437. return Result::error("暂无头条新闻");
  438. }
  439. return Result::success($result);
  440. } else {
  441. return Result::error("本网站下暂无相关栏目", 0);
  442. }
  443. }
  444. /**
  445. * 获取模块新闻
  446. * @param array $data
  447. * @return array
  448. */
  449. public function getWebsiteModelArticles(array $data): array
  450. {
  451. $catid = $data['catid'];
  452. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  453. $category = $category->toArray();
  454. if (!empty($category)) {
  455. $where = [
  456. 'status' => 1,
  457. 'catid' => $catid,
  458. ];
  459. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  460. // 1:文字新闻;2:轮播图;3:图文;
  461. // 级别:0:未分类
  462. // 3:推荐图片
  463. if ($data['level'] == 1) {
  464. $data['level'] = 0;
  465. }
  466. $result = Article::where($where)
  467. ->where(function ($query) use ($data) {
  468. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  469. ->orWhereNull("level")
  470. ->orWhereRaw("level = '[]'");
  471. })
  472. ->where(function ($query) use ($data) {
  473. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  474. ->orWhereNull("ignore_ids");
  475. })
  476. ->orderBy("updated_at", "desc")
  477. ->offset($placeid)
  478. ->limit($data['pagesize'])
  479. ->get();
  480. if (empty($result)) {
  481. return Result::error("此栏目暂无相关新闻", 0);
  482. }
  483. } else {
  484. return Result::error("此网站暂无此栏目", 0);
  485. }
  486. return Result::success($result);
  487. }
  488. /**
  489. *获取新闻列表
  490. * @param array $data
  491. * @return array
  492. */
  493. public function getWebsiteArticleList(array $data): array
  494. {
  495. // return Result::success($data);
  496. $where[] = ['status', '=', 1];
  497. if (isset($data['catid']) && !empty($data['catid'])) {
  498. if (is_array($data['catid'])) {
  499. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  500. array_push($where, ['catid', 'in', $data['catid']]);
  501. } else {
  502. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  503. array_push($where, ['catid', '=', $data['catid']]);
  504. }
  505. if (empty($category)) {
  506. return Result::error("此网站暂无此栏目", 0);
  507. }
  508. }
  509. // return Result::success($where);
  510. $rep = Article::where(function ($query) use ($where) {
  511. foreach ($where as $condition) {
  512. if ($condition[1] === 'in') {
  513. $query->whereIn($condition[0], $condition[2]);
  514. } else {
  515. $query->where($condition[0], $condition[1], $condition[2]);
  516. }
  517. }
  518. })
  519. ->where(function ($query) use ($data) {
  520. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  521. ->orWhereNull("ignore_ids");
  522. })
  523. ->select(
  524. 'article.id',
  525. 'article.title',
  526. 'article.imgurl',
  527. 'article.author',
  528. 'article.updated_at',
  529. 'article.introduce',
  530. 'article.islink',
  531. 'article.linkurl',
  532. 'article.copyfrom',
  533. 'article.catid')
  534. ->orderBy("updated_at", "desc")
  535. ->offset(($data['page'] - 1) * $data['pageSize'])
  536. ->limit($data['pageSize'])
  537. ->get()
  538. ->map(function ($article ) use ($data) {
  539. $catid = $article->catid ?? 0;
  540. $pinyin = '';
  541. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  542. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  543. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  544. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  545. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  546. }
  547. $article->pinyin = $pinyin;
  548. return $article;
  549. });
  550. $count = Article::where(function ($query) use ($where) {
  551. foreach ($where as $condition) {
  552. if ($condition[1] === 'in') {
  553. $query->whereIn($condition[0], $condition[2]);
  554. } else {
  555. $query->where($condition[0], $condition[1], $condition[2]);
  556. }
  557. }
  558. })->count();
  559. $data = [
  560. 'rows' => $rep->toArray(),
  561. 'count' => $count,
  562. ];
  563. if (empty($rep)) {
  564. return Result::error("没有信息数据");
  565. }
  566. return Result::success($data);
  567. }
  568. /**
  569. * 前端-获取新闻详情
  570. * @param array $data
  571. * @return array
  572. */
  573. public function selectWebsiteArticleInfo(array $data): array
  574. {
  575. $where = [
  576. 'article.id'=>$data['id'],
  577. 'article.status'=>1
  578. ];
  579. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  580. ->where(function ($query) use ($data) {
  581. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  582. ->orWhereNull("ignore_ids");
  583. })
  584. ->first();
  585. if(empty($result)){
  586. return Result::error("暂无此新闻!",0);
  587. }
  588. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  589. if(empty($category)){
  590. return Result::error("查询失败",0);
  591. }
  592. $result['category_id'] = $category['category_id'];
  593. $result['cat_name'] = $category['alias'];
  594. return Result::success($result);
  595. }
  596. /**
  597. * 前端-获取网站调查问卷
  598. * @param array $data
  599. * @return array
  600. */
  601. public function getWebsiteSurvey(array $data): array
  602. {
  603. if(isset($data['website_id']) && !empty($data['website_id'])){
  604. $website = Website::where('id',$data['website_id'])->first();
  605. if(empty($website)){
  606. return Result::error("暂无此网站",0);
  607. }
  608. }
  609. if(isset($data['art_id']) && !empty($data['art_id'])){
  610. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  611. if(empty($article)){
  612. return Result::error("暂无此文章",0);
  613. }
  614. // return Result::error($data,0);
  615. $where['art_id'] = $data['art_id'];
  616. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  617. }else{
  618. $survey = Article::where(function ($query) {
  619. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  620. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  621. })
  622. ->where('status',1)
  623. ->where('is_survey',1)
  624. ->select('survey_id')
  625. ->orderBy('updated_at','desc')
  626. ->first();
  627. if(empty($survey)){
  628. return Result::error("暂无调查问卷",0);
  629. }
  630. $where['sur_id'] = $survey['survey_id'];
  631. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  632. }
  633. // return Result::success($where);
  634. $result = ArticleSurvey::where($where)
  635. ->where(function ($query) {
  636. $query->where('is_other', 0)
  637. ->orWhere(function ($subQuery) {
  638. $subQuery->where('is_other', 1)
  639. ->where('other_id', 0);
  640. });
  641. })
  642. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  643. ->select('article_survey.*', 'article.survey_type')
  644. ->get()->all();
  645. if(empty($result)){
  646. return Result::error("此文章暂无调查问卷",0);
  647. }
  648. return Result::success($result);
  649. }
  650. /**
  651. * 前端-添加网站调查问卷选项
  652. * @param array $data
  653. * @return array
  654. */
  655. public function addWebsiteSurveyOption(array $data): array
  656. {
  657. if(isset($data['website_id']) && !empty($data['website_id'])){
  658. $website = Website::where('id',$data['website_id'])->first();
  659. if(empty($website)){
  660. return Result::error("暂无此网站",0);
  661. }
  662. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  663. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('is_other',1)->where('other_id',0)->first();
  664. if(empty($survey)){
  665. return Result::error("此调查问卷不可添加选项",0);
  666. }
  667. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  668. $choice = [
  669. 'art_id'=>$survey['art_id'],
  670. 'website_id'=>$data['website_id'],
  671. 'survey_name'=>$survey['survey_name'],
  672. 'choice_name'=>$data['choice_name'],
  673. 'sur_id'=>$survey['sur_id'],
  674. 'is_other'=>1,
  675. 'other_id'=>$survey['id'],
  676. ];
  677. $result = ArticleSurvey::insertGetId($choice);
  678. if(empty($result)){
  679. return Result::error("添加失败",0);
  680. }
  681. return Result::success($result);
  682. }
  683. }
  684. return Result::error("添加失败",0);
  685. }
  686. return Result::error("添加失败",0);
  687. }
  688. /**
  689. * 前端-调查问卷投票
  690. * @param array $data
  691. * @return array
  692. */
  693. public function addWebsiteSurveyVote(array $data): array
  694. {
  695. // return Result::success($data);
  696. if(isset($data['website_id']) && !empty($data['website_id'])){
  697. $website = Website::where('id',$data['website_id'])->first();
  698. if(empty($website)){
  699. return Result::error("暂无此网站",0);
  700. }
  701. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  702. $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
  703. // return Result::success($survey);
  704. if(empty($is_survey)){
  705. return Result::error("此调查问卷不存在",0);
  706. }
  707. // return Result::success($survey);
  708. // 调查问卷类型
  709. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  710. //多选 若是json型则转化成数组类型
  711. if (strpos($data['choice_id'], '[') === 0) {
  712. $data['choice_id'] = json_decode($data['choice_id'], true);
  713. } else {
  714. // 单选 也转换成数组
  715. $data['choice_id'] = [$data['choice_id']];
  716. }
  717. $data['choice_id'] = array_map('intval', $data['choice_id']);
  718. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  719. // ->where('website_id',$data['website_id'])
  720. ->where('is_other',1)
  721. ->where('other_id',0)
  722. ->first();
  723. if(!empty($other)){
  724. return Result::error("请选择已有的选项!",0);
  725. }
  726. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  727. // ->where('website_id',$data['website_id'])
  728. ->where('is_other',1)
  729. ->where('other_id','!=',0)
  730. ->first();
  731. // return Result::success($choice['other']);
  732. $choice_id = $data['choice_id'];
  733. if(!empty($choice['other'])){
  734. // array_push($data['choice_id'],$choice['other']['other_id']);
  735. if(!empty($choice_id)){
  736. $key = array_search($choice['other']['id'], $choice_id);
  737. if ($key!== false) {
  738. unset($choice_id[$key]);
  739. $choice_id = array_values($choice_id);
  740. }
  741. array_push($choice_id,$choice['other']['other_id']);
  742. }else{
  743. $choice_id[0] = $choice['other']['other_id'];
  744. }
  745. array_push($data['choice_id'],$choice['other']['other_id']);
  746. }
  747. // return Result::success($data);
  748. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  749. // ->where('website_id',$data['website_id'])
  750. ->increment('results', 1);
  751. if(empty($choice)){
  752. return Result::error("请选择已有的选项!",0);
  753. }
  754. $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
  755. // ->where('website_id',$data['website_id'])
  756. ->where('other_id', 0)
  757. ->get();
  758. $survey['choice'] = $choice_id;
  759. return Result::success($survey);
  760. }
  761. return Result::error("参数必填!");
  762. }
  763. return Result::error("此调查问卷不存在",0);
  764. }
  765. return Result::error("参数必填!");
  766. }
  767. /**
  768. * 后端-获取网站调查问卷列表
  769. * @param array $data
  770. * @return array
  771. */
  772. public function getSurveyList(array $data): array
  773. {
  774. $where = [];
  775. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  776. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  777. }
  778. if(isset($data['survey_type']) && $data['survey_type'] != null){
  779. array_push($where,['survey_type','=',$data['survey_type']]);
  780. }
  781. if(isset($data['is_survey']) && $data['is_survey'] != null){
  782. array_push($where,['is_survey','=',$data['is_survey']]);
  783. }
  784. // return Result::success($where);
  785. if (!empty($where)) {
  786. $query = Article::where($where)->where(function ($q) {
  787. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  788. });
  789. } else {
  790. $query = Article::where(function ($q) {
  791. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  792. });
  793. }
  794. $count = $query->count();
  795. $survey = $query->orderByDesc('id')
  796. ->limit($data['pageSize'])
  797. ->offset(($data['page']-1)*$data['pageSize'])
  798. ->get();
  799. if(empty($survey->toArray())){
  800. return Result::error("暂无调查问卷!",0);
  801. }
  802. $result = [
  803. 'rows'=>$survey,
  804. 'count'=>$count
  805. ];
  806. return Result::success($result);
  807. }
  808. /**
  809. * 后端-获取网站调查问卷详情
  810. * @param array $data
  811. * @return array
  812. */
  813. public function getSurveyInfo(array $data): array
  814. {
  815. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  816. $where = [ 'sur_id'=>$data['sur_id']];
  817. $choose = ArticleSurvey::where($where)->where('is_other',0)
  818. ->leftJoin('article','article_survey.art_id','article.id')
  819. ->select('article_survey.*','article.survey_type')
  820. ->get()->all();
  821. if(empty($choose)){
  822. return Result::error("此调查问卷不存在",0);
  823. }
  824. $resultsArray = array_column($choose, 'results');
  825. $total = array_sum($resultsArray);
  826. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  827. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  828. // $total = 0;
  829. if(!empty($other)){
  830. $total = $total + $other['results'];
  831. $other['choice_name'] ='(其他)';
  832. if(!empty($others)){
  833. $other['hasChildren'] = true;
  834. // array_push($other,['hasChildren','=',true]);
  835. $other['children'] = $others;
  836. $other_choices = [$other->toArray()];
  837. $mer_choice = array_merge($choose,$other_choices);
  838. $value_choice = array_values($mer_choice);
  839. }
  840. else{
  841. // return Result::error('1111');
  842. $other_choices = [$other->toArray()];
  843. $other_choices = array_merge($choose,$other_choices);
  844. $value_choice = array_values($other_choices);
  845. // return Result::success($result);
  846. }
  847. }else{
  848. $value_choice = $choose;
  849. }
  850. $result = [
  851. 'choose'=>$value_choice,
  852. 'total'=>$total
  853. ];
  854. }
  855. return Result::success($result);
  856. }
  857. /**
  858. * 前端-搜索新闻列表
  859. * @param array $data
  860. * @return array
  861. */
  862. public function selectWebsiteArticle(array $data): array
  863. {
  864. $where = [];
  865. // 初始化查询构造器
  866. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  867. $query = Article::where('status', 1)
  868. ->whereIn('catid', $category)
  869. ->where(function ($query) use ($data) {
  870. $query->where(function ($subQuery) use ($data) {
  871. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  872. })->orWhereNull("ignore_ids");
  873. });
  874. // return Result::success($all_articles);
  875. // 检查是否存在 cityid 参数
  876. if (isset($data['cityid']) && !empty($data['cityid'])) {
  877. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  878. }
  879. // 检查是否存在 department_id 参数
  880. if (isset($data['department_id']) && !empty($data['department_id'])) {
  881. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  882. }
  883. // 检查是否存在 keyword 参数
  884. if (isset($data['keyword']) && !empty($data['keyword'])) {
  885. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  886. }
  887. // 计算总数
  888. $count = $query->count();
  889. // 分页查询
  890. $articles = $query
  891. ->select(
  892. 'article.id',
  893. 'article.title',
  894. 'article.imgurl',
  895. 'article.author',
  896. 'article.updated_at',
  897. 'article.introduce',
  898. 'article.islink',
  899. 'article.linkurl',
  900. 'article.copyfrom',
  901. 'article.catid',
  902. 'article.department_arr_id',
  903. 'article.city_arr_id',)
  904. ->orderBy("updated_at", "desc")
  905. ->offset(($data['page'] - 1) * $data['pageSize'])
  906. ->limit($data['pageSize'])
  907. ->get()
  908. ->map(function ($article ) use ($data) {
  909. $catid = $article->catid ?? 0;
  910. $pinyin = '';
  911. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  912. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  913. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  914. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  915. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  916. }
  917. $article->pinyin = $pinyin;
  918. return $article;
  919. });
  920. if (empty($articles)) {
  921. return Result::error("没有符合条件的资讯数据");
  922. }
  923. $data = [
  924. 'rows' => $articles,
  925. 'count' => $count,
  926. ];
  927. return Result::success($data);
  928. }
  929. /**
  930. * 模块新闻加强版
  931. * @param array $data
  932. * @return array
  933. */
  934. public function getWebsiteCatidArticle(array $data): array
  935. {
  936. // return Result::success($data);
  937. $where = [
  938. // 'category.status' => 1,
  939. 'website_category.category_id' => $data['catid'],
  940. 'website_category.website_id' => $data['website_id'],
  941. // 'article.status' => 1,
  942. ];
  943. // $category = WebsiteCategory::where($where);
  944. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  945. $child_catnum = $data['child_catnum']?? 1;
  946. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  947. $childCategoryIds = array_column($category['child'], 'category_id');
  948. if(empty($childCategoryIds)){
  949. return Result::error("暂无子栏目",0);
  950. }
  951. $imgArticles = [];
  952. $textArticles = [];
  953. // return Result::success($childCategoryIds);
  954. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  955. // 初始化子分类图片新闻和文字新闻数组
  956. // 查询所有子级栏目的图文新闻
  957. $imgArticles = Article::where('catid', $childCategoryIds[0])
  958. ->where(function ($query) use ($data) {
  959. $query->where(function ($subQuery) use ($data) {
  960. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  961. })->orWhereNull("ignore_ids");
  962. })
  963. ->where('status', 1)
  964. ->where('imgurl', '!=', '')
  965. ->select('*')
  966. ->orderBy('updated_at', 'desc')
  967. ->limit($data['child_imgnum'])
  968. ->get();
  969. }
  970. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  971. // 查询所有子级栏目的文字新闻
  972. $textArticles = Article::where('catid', $childCategoryIds[0])
  973. ->where('status', 1)
  974. ->where(function ($query) use ($data) {
  975. $query->where(function ($subQuery) use ($data) {
  976. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  977. })->orWhereNull("ignore_ids");
  978. })
  979. // ->where(function ($query) {
  980. // $query->whereNull('imgurl')
  981. // ->orWhere('imgurl', '');
  982. // })
  983. ->select('*')
  984. ->orderBy('updated_at', 'desc')
  985. ->limit($data['child_textnum'])
  986. ->get();
  987. }
  988. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  989. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  990. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  991. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  992. return $child;
  993. }, $category['child']);
  994. }
  995. // }
  996. if (isset($data['img_num']) && !empty($data['img_num'])) {
  997. $category['img'] = WebsiteCategory::where($where)
  998. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  999. ->where('article.status', 1)
  1000. ->where('article.imgurl', '!=', '')
  1001. ->select('article.*','website_category.category_id','website_category.alias')
  1002. ->orderBy('article.updated_at', 'desc')
  1003. ->limit($data['img_num'])
  1004. ->get();
  1005. }
  1006. if (isset($data['text_num']) && !empty($data['text_num'])) {
  1007. $category['text'] = WebsiteCategory::where($where)
  1008. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  1009. ->where('article.status', 1)
  1010. // ->where(function ($query) {
  1011. // $query->whereNull('article.imgurl')
  1012. // ->orWhere('article.imgurl', '');
  1013. // })
  1014. ->select('article.*','website_category.category_id','website_category.alias')
  1015. ->orderBy('article.updated_at', 'desc')
  1016. ->limit($data['text_num'])
  1017. ->get();
  1018. }
  1019. // $category = $category->get();
  1020. if(empty($category)){
  1021. return Result::error("查询失败", 0);
  1022. }
  1023. return Result::success($category);
  1024. }
  1025. /**
  1026. * 模块新闻加强plus版
  1027. * @param array $data
  1028. * @return array
  1029. */
  1030. public function getWebsiteAllArticle(array $data): array
  1031. {
  1032. // 修正传入的字符串,将单引号替换为双引号
  1033. $input['id'] = $data['id'];
  1034. $input['website_id'] = $data['website_id'];
  1035. // 将 JSON 字符串转换为 PHP 数组
  1036. $data = json_decode($input['id'], true);
  1037. // 使用 array_map 处理每个元素
  1038. $result = array_map(function ($item) use ($input) {
  1039. // 检查parent元素是否存在且不是undefined
  1040. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1041. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1042. $website = [
  1043. 'website_id' => $input['website_id'],
  1044. ];
  1045. // 查询栏目名称
  1046. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1047. if(empty($category)){
  1048. $parent_alias = '';
  1049. $parent_pinyin = null;
  1050. $imgArticles = [];
  1051. $textArticles = [];
  1052. }else{
  1053. $parent_alias = $category->alias ?? '';
  1054. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1055. // 查找子分类ID数组
  1056. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1057. array_push($childCategoryIds,$parentCatId);
  1058. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1059. if($parentImgNum!=0){
  1060. // 查询图片新闻
  1061. $imgArticles = Article::where('article.status', 1)
  1062. ->where(function($query) use ($parentCatId) {
  1063. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1064. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1065. })
  1066. ->where(function ($query) use ($website) {
  1067. $query->where(function ($subQuery) use ($website) {
  1068. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1069. ->orWhereNull("ignore_ids");
  1070. });
  1071. })
  1072. ->where('imgurl', '!=', '')
  1073. ->leftJoin('website_category', function($join) use ($website) {
  1074. $join->on('article.catid', '=', 'website_category.category_id')
  1075. ->where('website_category.website_id', '=', $website['website_id']);
  1076. })
  1077. ->select(
  1078. 'article.id',
  1079. 'article.title',
  1080. 'article.imgurl',
  1081. 'article.author',
  1082. 'article.updated_at',
  1083. 'article.introduce',
  1084. 'article.islink',
  1085. 'article.linkurl',
  1086. 'article.copyfrom',
  1087. 'article.catid',
  1088. 'website_category.alias as category_name',
  1089. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1090. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1091. )
  1092. ->orderBy('updated_at', 'desc')
  1093. ->limit($parentImgNum)
  1094. ->get()->all();
  1095. // 查询文字新闻
  1096. }
  1097. if($parentTextNum!=0){
  1098. $textArticles = [];
  1099. $textArticles = Article::where('article.status', 1)
  1100. ->where(function($query) use ($parentCatId) {
  1101. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1102. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1103. })
  1104. ->where(function ($query) use ($website) {
  1105. $query->where(function ($subQuery) use ($website) {
  1106. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1107. ->orWhereNull("ignore_ids");
  1108. });
  1109. })
  1110. ->leftJoin('website_category', function($join) use ($website) {
  1111. $join->on('article.catid', '=', 'website_category.category_id')
  1112. ->where('website_category.website_id', '=', $website['website_id']);
  1113. })
  1114. ->select(
  1115. 'article.id',
  1116. 'article.title',
  1117. 'article.imgurl',
  1118. 'article.author',
  1119. 'article.updated_at',
  1120. 'article.introduce',
  1121. 'article.islink',
  1122. 'article.linkurl',
  1123. 'article.copyfrom',
  1124. 'article.catid',
  1125. 'website_category.alias as category_name',
  1126. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1127. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1128. )
  1129. ->orderBy('updated_at', 'desc')
  1130. ->limit($parentTextNum)
  1131. ->get()->all();
  1132. }
  1133. }
  1134. }
  1135. $resultItem = [
  1136. 'alias' => $parent_alias ?? '',
  1137. 'category_id' => $parentCatId ?? 0,
  1138. 'pinyin' => $parent_pinyin ?? null,
  1139. 'imgnum' => $imgArticles ?? [],
  1140. 'textnum' => $textArticles ?? [],
  1141. ];
  1142. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1143. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1144. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1145. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1146. ->get()->all();
  1147. if (!empty($childCategory)) {
  1148. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1149. // 查询子栏目名称
  1150. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1151. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1152. ->first();
  1153. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1154. $childImgArticles = [];
  1155. $childTextArticles = [];
  1156. $resultItem['child'] = [];
  1157. }else{
  1158. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1159. // 查询子栏目图片新闻
  1160. $childImgArticles = Article::where('catid', $childCatId)
  1161. ->where('status', 1)
  1162. ->where(function ($query) use ($website) {
  1163. $query->where(function ($subQuery) use ($website) {
  1164. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1165. })->orWhereNull("ignore_ids");
  1166. })
  1167. ->select(
  1168. 'article.id',
  1169. 'article.title',
  1170. 'article.imgurl',
  1171. 'article.author',
  1172. 'article.updated_at',
  1173. 'article.introduce',
  1174. 'article.islink',
  1175. 'article.linkurl',
  1176. 'article.copyfrom',
  1177. DB::raw("'$child_pinyin' as pinyin"))
  1178. ->where('imgurl', '!=', '')
  1179. ->orderBy('updated_at', 'desc')
  1180. ->limit($childImgNum)
  1181. ->get()->all();
  1182. // 查询子栏目文字新闻
  1183. $childTextArticles = Article::where('catid', $childCatId)
  1184. ->where('status', 1)
  1185. ->where(function ($query) use ($website) {
  1186. $query->where(function ($subQuery) use ($website) {
  1187. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1188. })->orWhereNull("ignore_ids");
  1189. })
  1190. ->select(
  1191. 'article.id',
  1192. 'article.title',
  1193. 'article.author',
  1194. 'article.updated_at',
  1195. 'article.introduce',
  1196. 'article.islink',
  1197. 'article.linkurl',
  1198. 'article.copyfrom',
  1199. DB::raw("'$child_pinyin' as pinyin"))
  1200. ->orderBy('updated_at', 'desc')
  1201. ->limit($childTextNum)
  1202. ->get()->all();
  1203. $resultItem['child'] = [
  1204. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1205. 'category_id' => $childCatId,
  1206. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1207. 'all_childcat' => $childCategory,
  1208. 'imgnum' => $childImgArticles,
  1209. 'textnum' => $childTextArticles,
  1210. ];
  1211. }
  1212. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1213. }
  1214. }else{
  1215. $resultItem['child'] = [];
  1216. }
  1217. return $resultItem;
  1218. }, $data);
  1219. return Result::success($result);
  1220. // return Result::success($data);
  1221. }
  1222. /**
  1223. * 乡村网-获取特殊新闻模块
  1224. * @param array $data
  1225. * @return array
  1226. */
  1227. public function getWebsiteArticles(array $data): array
  1228. {
  1229. $input['id'] = $data['id'];
  1230. $input['website_id'] = $data['website_id'];
  1231. $data = json_decode($input['id'], true);
  1232. // 使用 array_map 处理每个元素
  1233. $result = array_map(function ($item) use ($input) {
  1234. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1235. $website = [
  1236. 'website_id' => $input['website_id']
  1237. ];
  1238. // 查询栏目名称
  1239. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1240. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1241. if(empty($category)){
  1242. $imgArticles = [];
  1243. $textArticles = [];
  1244. // return Result::error("暂无此栏目",0);
  1245. }else{
  1246. $child_category = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1247. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1248. // return Result::success($website);
  1249. // 查询图片新闻
  1250. // 合并查询条件
  1251. $baseQuery = Article::where(function ($query) use ($website) {
  1252. $query->where(function ($subQuery) use ($website) {
  1253. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1254. })->orWhereNull("ignore_ids");
  1255. })
  1256. ->where('website_category.website_id', $website['website_id'])
  1257. ->where('article.status', 1)
  1258. ->leftJoin('website_category', 'website_category.category_id', 'article.catid');
  1259. // 查询文字新闻
  1260. $textArticles = clone $baseQuery;
  1261. $textArticles = $textArticles->whereIn('catid',$child_category)
  1262. ->select(
  1263. 'article.id',
  1264. 'article.title',
  1265. // 'article.imgurl',
  1266. 'article.author',
  1267. 'article.updated_at',
  1268. 'article.introduce',
  1269. 'article.islink',
  1270. 'article.linkurl',
  1271. 'article.copyfrom',
  1272. 'website_category.category_id',
  1273. 'website_category.alias',
  1274. 'website_category.aLIas_pinyin'
  1275. )
  1276. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1277. ->orderBy('article.updated_at', 'desc')
  1278. ->limit($parentTextNum)
  1279. ->get()
  1280. ->all();
  1281. if(empty($textArticles)){
  1282. $textArticles = clone $baseQuery;
  1283. $textArticles = $textArticles->where('catid',$parentCatId)
  1284. ->select(
  1285. 'article.id',
  1286. 'article.title',
  1287. // 'article.imgurl',
  1288. 'article.author',
  1289. 'article.updated_at',
  1290. 'article.introduce',
  1291. 'article.islink',
  1292. 'article.linkurl',
  1293. 'article.copyfrom',
  1294. 'website_category.category_id',
  1295. 'website_category.alias',
  1296. 'website_category.aLIas_pinyin'
  1297. )
  1298. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1299. ->orderBy('article.updated_at', 'desc')
  1300. ->limit($parentTextNum)
  1301. ->get()
  1302. ->all();
  1303. }
  1304. // 查询图片新闻
  1305. $imgArticles = clone $baseQuery;
  1306. $imgArticles = $imgArticles->where('imgurl', '!=', '')->whereIn('catid',$child_category)
  1307. ->select(
  1308. 'article.id',
  1309. 'article.title',
  1310. 'article.imgurl',
  1311. 'article.author',
  1312. 'article.updated_at',
  1313. 'article.introduce',
  1314. 'article.islink',
  1315. 'article.linkurl',
  1316. 'article.copyfrom',
  1317. 'website_category.category_id',
  1318. 'website_category.alias',
  1319. 'website_category.aLIas_pinyin'
  1320. )
  1321. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1322. ->orderBy('article.updated_at', 'desc')
  1323. ->limit($parentImgNum)
  1324. ->get()
  1325. ->all();
  1326. if(empty($imgArticles)){
  1327. $imgArticles = clone $baseQuery;
  1328. $imgArticles = $imgArticles->where('imgurl', '!=', '')->where('catid',$parentCatId)
  1329. ->select(
  1330. 'article.id',
  1331. 'article.title',
  1332. 'article.imgurl',
  1333. 'article.author',
  1334. 'article.updated_at',
  1335. 'article.introduce',
  1336. 'article.islink',
  1337. 'article.linkurl',
  1338. 'article.copyfrom',
  1339. 'website_category.category_id',
  1340. 'website_category.alias',
  1341. 'website_category.aLIas_pinyin'
  1342. )
  1343. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1344. ->orderBy('article.updated_at', 'desc')
  1345. ->limit($parentImgNum)
  1346. ->get()
  1347. ->all();
  1348. }
  1349. }
  1350. $resultItem = [
  1351. 'alias' => $category ? $category->alias : null,
  1352. 'category_id' => $parentCatId,
  1353. 'pinyin' => $pinyin ? $pinyin : null,
  1354. 'imgnum' => $imgArticles ?? [],
  1355. 'textnum' => $textArticles ?? [],
  1356. ];
  1357. return $resultItem;
  1358. }, $data);
  1359. return Result::success($result);
  1360. }
  1361. // 封装处理商品的路由问题
  1362. function processGoods($goods, $data) {
  1363. return $goods->map(function ($good) use ($data) {
  1364. $catid = $good->catid ?? 0;
  1365. $pinyin = '';
  1366. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1367. if (!empty($category->pid) && $category->pid != 0) {
  1368. $level = json_decode($category->category_arr_id);
  1369. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1370. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1371. ->get(['aLIas_pinyin'])
  1372. ->pluck('aLIas_pinyin')
  1373. ->implode('/');
  1374. } else {
  1375. $pinyin = $category->aLIas_pinyin ?? '';
  1376. }
  1377. if(isset($good->city_id) &&!empty($good->city_id)){
  1378. $city = District::where('id', $good->city_id)->first(['name']);
  1379. $good->city_name = $city->name ?? '';
  1380. }
  1381. // 解析imgurl JSON并取第一条数据
  1382. $imgUrls = json_decode($good->imgurl, true);
  1383. $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
  1384. $good->pinyin = $pinyin;
  1385. return $good;
  1386. });
  1387. }
  1388. /**
  1389. * 获取商品模块
  1390. * @param array $data
  1391. * @return array
  1392. * */
  1393. public function getWebsiteshop(array $data): array
  1394. {
  1395. $input['id'] = $data['id'];
  1396. $input['website_id'] = $data['website_id'];
  1397. // return Result::success($input);
  1398. $data = json_decode($input['id'] ?? '', true) ?? [];
  1399. $result = array_map(function ($item) use ($input) {
  1400. // 检查parent元素是否存在且不是undefined
  1401. if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
  1402. list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
  1403. $website = $input['website_id'];
  1404. $query = Good::where('good.status', 2)
  1405. ->where('good.website_id', $website);
  1406. switch ($Levelid) {
  1407. case 1:
  1408. case 2:
  1409. case 3:
  1410. $goods = $query->where(function($q) use ($Levelid) {
  1411. $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 0")
  1412. ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 0");
  1413. });
  1414. break;
  1415. case 4:
  1416. $goods = $query;
  1417. break;
  1418. case 5:
  1419. $goods = $query->where('type_id',1);
  1420. break;
  1421. case 6:
  1422. $goods = $query->where('type_id',2);
  1423. break;
  1424. default:
  1425. return [];
  1426. }
  1427. $all_goods = $goods
  1428. ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
  1429. 'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
  1430. 'good.website_id')
  1431. ->orderBy('updated_at','desc')
  1432. ->offset($goodStart)
  1433. ->limit($goodNum)
  1434. ->get();
  1435. $all_goods = $this->processGoods($all_goods, $input);
  1436. }
  1437. return $all_goods;
  1438. }, $data);
  1439. return Result::success($result);
  1440. }
  1441. /**
  1442. * 获取商品分类
  1443. * @param array $data
  1444. * @return array
  1445. * */
  1446. public function getWebsiteshopCat(array $data): array
  1447. {
  1448. $category = WebsiteCategory::where('website_id', $data['website_id'])
  1449. ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
  1450. ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
  1451. ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
  1452. ->orderBy('sort')
  1453. ->get()
  1454. ->map(function ($item) use ($data) {
  1455. $pinyin = [];
  1456. $level = json_decode($item->category_arr_id);
  1457. // 递归获取所有父级栏目的拼音
  1458. if($item->pid!= 0){
  1459. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1460. ->orderByRaw('FIELD(category_id, '.implode(',', $level).')')
  1461. ->get(['aLIas_pinyin'])
  1462. ->pluck('aLIas_pinyin')
  1463. ->implode('/');
  1464. }else{
  1465. $pinyin = $item->aLIas_pinyin;
  1466. }
  1467. $item->aLIas_pinyin = $pinyin;
  1468. return $item;
  1469. });
  1470. $goods = Good::where('website_id', $data['website_id'])
  1471. ->where('status', 2)
  1472. ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
  1473. ->latest('updated_at')
  1474. ->offset(($data['page']-1)*$data['pageSize'])
  1475. ->limit($data['pageSize'])
  1476. ->get();
  1477. $goods = $this->processGoods($goods, $data);
  1478. if(empty($category)){
  1479. return Result::error("栏目查询失败", 0);
  1480. }
  1481. if(empty($goods)){
  1482. return Result::error("商品查询失败", 0);
  1483. }
  1484. // 调用Tool中的buildMenuTree方法构建菜单树
  1485. $cat_tree = Result::buildMenuTree($category);
  1486. $result = [
  1487. 'category' => $cat_tree,
  1488. 'goods' => $goods,
  1489. ];
  1490. // $resul['goods'] = $goods;
  1491. if(empty($result)){
  1492. return Result::error("查询失败", 0);
  1493. }
  1494. return Result::success($result);
  1495. }
  1496. /*
  1497. * 获取商品列表
  1498. * @param array $data
  1499. * @return array
  1500. * */
  1501. public function getWebsiteshopList(array $data): array
  1502. {
  1503. // return Result::success($data);
  1504. $where = [
  1505. 'status' => 2,
  1506. 'website_id' => $data['website_id'],
  1507. ];
  1508. if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
  1509. return Result::error("查询失败", 0);
  1510. }
  1511. if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
  1512. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
  1513. $data['catid'] = $category->category_id ?? 0;
  1514. }
  1515. if(isset($data['keyword']) &&!empty($data['keyword'])){
  1516. array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
  1517. }
  1518. if(isset($data['type_id']) && !empty($data['type_id'])){
  1519. array_push($where, ['type_id', $data['type_id']]);
  1520. }
  1521. $query = Good::where($where)
  1522. ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
  1523. $query->where(function($q) use ($data) {
  1524. $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
  1525. ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
  1526. });
  1527. })
  1528. ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
  1529. $query->where(function($q) use ($data) {
  1530. $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
  1531. ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
  1532. });
  1533. })
  1534. ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at',
  1535. 'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
  1536. ->latest('updated_at');
  1537. // 获取 type_id 为 1 的数据
  1538. $result['type1'] = $this->processGoods(
  1539. $query->clone()
  1540. ->where('type_id', 1)
  1541. ->offset(($data['page'] - 1) * $data['pageSize'])
  1542. ->limit($data['pageSize'])
  1543. ->get(),
  1544. $data
  1545. );
  1546. // 获取 type_id 为 2 的数据
  1547. $result['type2'] = $this->processGoods(
  1548. $query->clone()
  1549. ->where('type_id', 2)
  1550. ->offset(($data['page'] - 1) * $data['pageSize'])
  1551. ->limit($data['pageSize'])
  1552. ->get(),
  1553. $data
  1554. );
  1555. if(empty($result)){
  1556. return Result::error("查询失败", 0);
  1557. }
  1558. return Result::success($result);
  1559. }
  1560. /**
  1561. * 获取商品详情
  1562. * @param array $data
  1563. * @return array
  1564. * */
  1565. public function getWebsiteshopInfo(array $data): array
  1566. {
  1567. $where = [
  1568. 'good.status' => 2,
  1569. 'good.website_id' => $data['website_id'],
  1570. ];
  1571. $goods = Good::where($where)
  1572. ->where('good.id', $data['id'])
  1573. ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
  1574. ->select('good.*','website_category.alias','website_category.category_id')
  1575. ->first();
  1576. if(empty($goods)){
  1577. return Result::error("查询失败", 0);
  1578. }
  1579. $goods->imgurl = json_decode($goods->imgurl, true);
  1580. return Result::success($goods);
  1581. }
  1582. /**
  1583. * 封装处理文章的路由问题
  1584. * */
  1585. function processArticles($articles, $data) {
  1586. if (!is_array($data)) {
  1587. // 可以根据实际情况进行日志记录或抛出异常
  1588. // 这里简单输出错误信息
  1589. echo "Error: \$data is not an array in processArticles. It is of type ". gettype($data). PHP_EOL;
  1590. return $articles;
  1591. }
  1592. return $articles->map(function ($article) use ($data) {
  1593. $catid = $article->cat_arr_id?? '';
  1594. $level = json_decode($catid, true);
  1595. $pinyin = '';
  1596. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1597. if (!empty($category->pid) && $category->pid!= 0) {
  1598. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1599. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1600. ->get(['aLIas_pinyin'])
  1601. ->pluck('aLIas_pinyin')
  1602. ->implode('/');
  1603. } else {
  1604. $pinyin = $category->aLIas_pinyin ?? '';
  1605. }
  1606. $article->pinyin = $pinyin;
  1607. return $article;
  1608. });
  1609. }
  1610. /**
  1611. * 获取书籍模块
  1612. * @param array $data
  1613. * @return array
  1614. * */
  1615. public function getWebsiteBook(array $data): array
  1616. {
  1617. $input['id'] = $data['id'];
  1618. $input['website_id'] = $data['website_id'];
  1619. // 将 JSON 字符串转换为 PHP 数组
  1620. $data = json_decode($input['id'], true);
  1621. // 使用 array_map 处理每个元素
  1622. $result = array_map(function ($item) use ($input) {
  1623. // 检查parent元素是否存在且不是undefined
  1624. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  1625. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1626. $website = [
  1627. 'website_id' => $input['website_id'],
  1628. ];
  1629. // 查询栏目名称
  1630. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1631. if(empty($category)){
  1632. $parent_alias = '';
  1633. $parent_pinyin = null;
  1634. $imgBooks = [];
  1635. $textBooks = [];
  1636. }else{
  1637. $parent_alias = $category->alias ?? '';
  1638. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1639. // 查找子分类ID数组
  1640. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1641. array_push($childCategoryIds,$parentCatId);
  1642. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  1643. if($parentImgNum!=0){
  1644. // 查询图片新闻
  1645. $imgBooks = Book::where(function($query) use ($parentCatId) {
  1646. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1647. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1648. })
  1649. ->where('book.status', 2)
  1650. ->where('book.img_url', '!=', '')
  1651. ->leftJoin('website_category', function($join) use ($website) {
  1652. $join->on('book.catid', '=', 'website_category.category_id')
  1653. ->where('website_category.website_id', '=', $website['website_id']);
  1654. })
  1655. ->select(
  1656. 'book.id',
  1657. 'book.title',
  1658. 'book.img_url',
  1659. 'book.price',
  1660. 'book.market_price',
  1661. 'book.description',
  1662. 'book.catid',
  1663. 'book.description',
  1664. 'book.updated_at',
  1665. 'website_category.alias as category_name',
  1666. DB::raw("CASE WHEN book.catid = $parentCatId THEN '$parent_pinyin'
  1667. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1668. )
  1669. ->orderBy('updated_at', 'desc')
  1670. ->limit($parentImgNum)
  1671. ->get()->all();
  1672. // 查询文字新闻
  1673. }
  1674. if($parentTextNum!=0){
  1675. $textBooks = [];
  1676. $textBooks = Book::where(function($query) use ($parentCatId) {
  1677. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1678. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1679. })
  1680. ->where('book.status', 2)
  1681. ->leftJoin('website_category', function($join) use ($website) {
  1682. $join->on('book.catid', '=', 'website_category.category_id')
  1683. ->where('website_category.website_id', '=', $website['website_id']);
  1684. })
  1685. ->select(
  1686. 'book.id',
  1687. 'book.title',
  1688. 'book.img_url',
  1689. 'book.price',
  1690. 'book.market_price',
  1691. 'book.description',
  1692. 'book.catid',
  1693. 'book.description',
  1694. 'book.updated_at',
  1695. 'website_category.alias as category_name',
  1696. DB::raw("CASE WHEN book.catid = $parentCatId THEN '$parent_pinyin'
  1697. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1698. )
  1699. ->orderBy('updated_at', 'desc')
  1700. ->limit($parentTextNum)
  1701. ->get()->all();
  1702. }
  1703. }
  1704. }
  1705. $resultItem = [
  1706. 'alias' => $parent_alias ?? '',
  1707. 'category_id' => $parentCatId ?? 0,
  1708. 'pinyin' => $parent_pinyin ?? null,
  1709. 'imgnum' => $imgBooks ?? [],
  1710. 'textnum' => $textBooks ?? [],
  1711. ];
  1712. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1713. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1714. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1715. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1716. ->get()->all();
  1717. if (!empty($childCategory)) {
  1718. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1719. // 查询子栏目名称
  1720. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1721. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1722. ->first();
  1723. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1724. $childImgArticles = [];
  1725. $childTextArticles = [];
  1726. $resultItem['child'] = [];
  1727. }else{
  1728. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1729. // 查询子栏目图片新闻
  1730. $childImgArticles = Book::where('catid', $childCatId)
  1731. ->where('status', 2)
  1732. ->where('img_url', '!=', '')
  1733. ->leftJoin('website_category', function($join) use ($website) {
  1734. $join->on('book.catid', '=', 'website_category.category_id')
  1735. ->where('website_category.website_id', '=', $website['website_id']);
  1736. })
  1737. ->select(
  1738. 'book.id',
  1739. 'book.title',
  1740. 'book.img_url',
  1741. 'book.price',
  1742. 'book.market_price',
  1743. 'book.description',
  1744. 'book.catid',
  1745. 'book.description',
  1746. 'book.updated_at',
  1747. DB::raw("'$child_pinyin' as pinyin"))
  1748. ->orderBy('updated_at', 'desc')
  1749. ->limit($childImgNum)
  1750. ->get()->all();
  1751. // 查询子栏目文字新闻
  1752. $childTextArticles = Book::where('catid', $childCatId)
  1753. ->where('status', 2)
  1754. ->leftJoin('website_category', function($join) use ($website) {
  1755. $join->on('book.catid', '=', 'website_category.category_id')
  1756. ->where('website_category.website_id', '=', $website['website_id']);
  1757. })
  1758. ->select(
  1759. 'book.id',
  1760. 'book.title',
  1761. 'book.img_url',
  1762. 'book.price',
  1763. 'book.market_price',
  1764. 'book.description',
  1765. 'book.catid',
  1766. 'book.description',
  1767. 'book.updated_at',
  1768. DB::raw("'$child_pinyin' as pinyin"))
  1769. ->orderBy('updated_at', 'desc')
  1770. ->limit($childTextNum)
  1771. ->get()->all();
  1772. $resultItem['child'] = [
  1773. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1774. 'category_id' => $childCatId,
  1775. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1776. 'all_childcat' => $childCategory,
  1777. 'imgnum' => $childImgArticles,
  1778. 'textnum' => $childTextArticles,
  1779. ];
  1780. }
  1781. }
  1782. }else{
  1783. $resultItem['child'] = [];
  1784. }
  1785. return $resultItem;
  1786. }, $data);
  1787. return Result::success($result);
  1788. }
  1789. /**
  1790. * 获取书刊列表
  1791. * @param array $data
  1792. * @return array
  1793. * */
  1794. public function getWebsiteBookList(array $data): array
  1795. {
  1796. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  1797. if(empty($web)){
  1798. return Result::error("查询失败", 0);
  1799. }
  1800. $catid = $data['id'];
  1801. // 此处代码在原逻辑中会依赖外部变量 $data、$catid,需确保这些变量在作用域内
  1802. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$catid)->orderBy('sort')->first(['pid','category_id','aLias_pinyin']);
  1803. $parent_categpory = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id',$category['pid'])->orderBy('sort')->first(['aLias_pinyin']);
  1804. $parent_pinyin = $parent_categpory ? $parent_categpory->aLias_pinyin ?? '' : '';
  1805. $category_pinyin = $category ? $category->aLias_pinyin : '';
  1806. $pinyin = $parent_pinyin . '/' . $category_pinyin ;
  1807. $categorys = WebsiteCategory::where('website_id', $data['website_id'])
  1808. ->where('pid',$category['pid'])
  1809. ->select(
  1810. '*',
  1811. DB::raw("CONCAT('$parent_pinyin', '/', aLIas_pinyin) as pinyin")
  1812. )
  1813. ->orderBy('sort')
  1814. ->get()->all();
  1815. if(empty($category)){
  1816. return Result::error("查询失败", 0);
  1817. }
  1818. $query = Book::where('book.status', 2)
  1819. ->where('book.website_id', $data['website_id'])
  1820. ->where('book.catid', $catid)
  1821. ->select(
  1822. 'book.id',
  1823. 'book.title',
  1824. 'book.img_url',
  1825. 'book.description',
  1826. 'book.updated_at',
  1827. 'book.catid',
  1828. DB::raw("'$pinyin' as pinyin")
  1829. )
  1830. ->orderBy('book.updated_at', 'desc');
  1831. $count = $query->count();
  1832. $query = clone $query;
  1833. $Book = $query
  1834. ->limit($data['pageSize'])
  1835. ->offset(($data['page']-1)*$data['pageSize'])
  1836. ->get()->all();
  1837. if(empty($Book)){
  1838. return Result::error("查询失败", 0);
  1839. }
  1840. $result = [
  1841. 'category' => $categorys,
  1842. 'books' => $Book,
  1843. 'count' =>$count,
  1844. ];
  1845. return Result::success($result);
  1846. }
  1847. /**
  1848. * 获取书刊详情
  1849. * @param array $data
  1850. * @return array
  1851. * */
  1852. public function getWebsiteBookInfo(array $data): array
  1853. {
  1854. $web = Website::where('id', $data['website_id'])->first(['id','website_name']);
  1855. if(empty($web)){
  1856. return Result::error("查询失败", 0);
  1857. }
  1858. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['pid','category_id']);
  1859. if(empty($category)){
  1860. return Result::error("查询失败", 0);
  1861. }
  1862. $book = Book::where('status', 2)
  1863. ->where('website_id', $data['website_id'])
  1864. ->where('id', $data['id'])
  1865. ->select(
  1866. 'book.*',
  1867. )
  1868. ->orderBy('updated_at', 'desc')
  1869. ->first();
  1870. if(empty($book)){
  1871. return Result::error("查询失败", 0);
  1872. }
  1873. $categorys = WebsiteCategory::where('website_id', $data['website_id'])
  1874. ->where('pid',$category['pid'])
  1875. ->select('category_id','alias','aLIas_pinyin')
  1876. ->orderBy('sort')->get()->all();
  1877. $result = [
  1878. 'category' => $categorys,
  1879. 'books' => $book,
  1880. ];
  1881. return Result::success($result);
  1882. }
  1883. /**
  1884. * 尝试
  1885. * @param array $data
  1886. * @return array
  1887. * */
  1888. public function test(array $data): array
  1889. {
  1890. $input['id'] = $data['id'];
  1891. $input['website_id'] = $data['website_id'];
  1892. // 将 JSON 字符串转换为 PHP 数组
  1893. $data = json_decode($input['id'], true);
  1894. $result = [];
  1895. $article = $data;
  1896. $result = array_map(function ($item) use ($input) {
  1897. $website = [
  1898. 'website_id' => $input['website_id'],
  1899. ];
  1900. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1901. $category = WebsiteCategory::where('pid', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1902. $parent = [
  1903. 0 => $category['category_id'] ?? [],
  1904. 1 => $parentImgNum?? [],
  1905. 2 => $parentTextNum?? [],
  1906. ];
  1907. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['child']);
  1908. $child = [];
  1909. $resultItem = [
  1910. // 'alias' => $parent_alias ?? '',
  1911. 'category_id' => $parentCatId ?? 0,
  1912. 'pinyin' => $parentImgNum ?? null,
  1913. 'imgnum' => $parentTextNum ?? [],
  1914. // 'textnum' => $textArticles ?? [],
  1915. ]; return $parent;
  1916. }, $data);
  1917. return Result::success($result);
  1918. // });
  1919. }
  1920. /**
  1921. * 封装处理文章的路由问题
  1922. * */
  1923. function processArticle($article, $data) {
  1924. $article['img'] = Article::where('article.status', 1)
  1925. ->whereIn('article.id', $$article[0])
  1926. ->where(function ($query) use ($data) {
  1927. $query->where(function ($subQuery) use ($data) {
  1928. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1929. })->orWhereNull("ignore_ids");
  1930. })
  1931. ->select(
  1932. 'article.id',
  1933. 'article.title',
  1934. 'article.imgurl',
  1935. 'article.author',
  1936. );
  1937. return $article->map(function ($article) use ($data) {
  1938. $catid = $article->cat_arr_id?? '';
  1939. $level = json_decode($catid, true);
  1940. $pinyin = '';
  1941. $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1942. if (!empty($category->pid) && $category->pid!= 0) {
  1943. $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1944. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1945. ->get(['aLIas_pinyin'])
  1946. ->pluck('aLIas_pinyin')
  1947. ->implode('/');
  1948. } else {
  1949. $pinyin = $category->aLIas_pinyin ?? '';
  1950. }
  1951. $article->pinyin = $pinyin;
  1952. return $article;
  1953. });
  1954. }
  1955. /**
  1956. * 封装处理栏目的路由问题
  1957. * */
  1958. function processCategory($parent_id, $data) {
  1959. if(empty($parent_id) || empty($data)){
  1960. return [];
  1961. }
  1962. $child = [];
  1963. $child = WebsiteCategory::where('pid', $parent_id)
  1964. ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1965. ->get(['aLIas_pinyin'])
  1966. ->pluck('aLIas_pinyin')
  1967. ->implode('/');
  1968. // return $parent_id->map(function ($article) use ($data) {
  1969. // $catid = $article->cat_arr_id?? '';
  1970. // $level = json_decode($catid, true);
  1971. // $pinyin = '';
  1972. // $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
  1973. // if (!empty($category->pid) && $category->pid!= 0) {
  1974. // $pinyin = WebsiteCategory::whereIn('category_id', $level)
  1975. // ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
  1976. // ->get(['aLIas_pinyin'])
  1977. // ->pluck('aLIas_pinyin')
  1978. // ->implode('/');
  1979. // } else {
  1980. // $pinyin = $category->aLIas_pinyin ?? '';
  1981. // }
  1982. // $article->pinyin = $pinyin;
  1983. // return $article;
  1984. // });
  1985. }
  1986. }