NewsService.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 Hyperf\DbConnection\Db;
  9. use Hyperf\RpcServer\Annotation\RpcService;
  10. use App\Tools\Result;
  11. use App\Model\ArticleSurvey;
  12. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  13. class NewsService implements NewsServiceInterface
  14. {
  15. /**
  16. * 获取导航池列表
  17. * @param array $data
  18. * @return array
  19. */
  20. public function getCategoryList(array $data): array
  21. {
  22. $where = [];
  23. if(isset($data['name']) && $data['name']){
  24. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  25. }
  26. if(isset($data['department_id']) && $data['department_id']){
  27. array_push($where, ['category.department_id','=',$data['department_id']]);
  28. }
  29. if(isset($data['city_id']) && $data['city_id']){
  30. array_push($where, ['category.city_id','=',$data['city_id']]);
  31. }
  32. $rep = Category::where($where)
  33. ->leftJoin('district','category.city_id','district.id')
  34. ->leftJoin('department','category.department_id','department.id')
  35. ->select("category.*","district.name as city_name","department.name as department_name")
  36. ->limit($data['pageSize'])->orderBy("category.sort","desc")->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  37. $count = Category::where($where)->count();
  38. $data = [
  39. 'rows'=>$rep->toArray(),
  40. 'count'=>$count
  41. ];
  42. if(empty($rep->toArray())){
  43. return Result::error("没有导航池数据");
  44. }
  45. return Result::success($data);
  46. }
  47. /**
  48. * @param array $data
  49. * @return array
  50. */
  51. public function categoryList(array $data): array
  52. {
  53. $where[] = [
  54. 'pid','=',$data['pid']
  55. ];
  56. if(isset($data['name'])){
  57. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  58. }
  59. var_dump($where);
  60. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  61. if(empty($result)){
  62. return Result::error("没有栏目数据");
  63. }
  64. return Result::success($result);
  65. }
  66. /**
  67. * @param array $data
  68. * @return array
  69. */
  70. public function addCategory(array $data): array
  71. {
  72. $id = Category::insertGetId($data);
  73. if(empty($id)){
  74. return Result::error("添加失败");
  75. }
  76. return Result::success(['id'=>$id]);
  77. }
  78. /**
  79. * @param array $data
  80. * @return array
  81. */
  82. public function delCategory(array $data): array
  83. {
  84. $categoryList = Category::where(['pid'=>$data['id']])->get();
  85. var_dump("分类列表:",$data,$categoryList);
  86. if($categoryList->toArray()){
  87. return Result::error("分类下面有子分类不能删除");
  88. }
  89. $articleList = Article::where(['catid'=>$data['id']])->get();
  90. var_dump("文章列表:",$articleList);
  91. if($articleList->toArray()){
  92. return Result::error("分类下面有资讯不能删除");
  93. }
  94. $result = Category::where($data)->delete();
  95. if(!$result){
  96. return Result::error("删除失败");
  97. }
  98. return Result::success($result);
  99. }
  100. /**
  101. * @param array $data
  102. * @return array
  103. */
  104. public function updateCategory(array $data): array
  105. {
  106. $where = [
  107. 'id'=>$data['id']
  108. ];
  109. $result = Category::where($where)->update($data);
  110. if($result){
  111. return Result::success($result);
  112. }else{
  113. return Result::error("更新失败");
  114. }
  115. }
  116. /**
  117. * 获取导航池信息
  118. * @param array $data
  119. * @return array
  120. */
  121. public function getCategoryInfo(array $data): array
  122. {
  123. $where = [
  124. 'id'=>$data['id']
  125. ];
  126. $result = Category::where($where)->first();
  127. if($result){
  128. return Result::success($result);
  129. }else{
  130. return Result::error("更新失败");
  131. }
  132. }
  133. /**
  134. * @param array $data
  135. * @return array
  136. */
  137. public function getArticleList(array $data): array
  138. {
  139. $where= [];
  140. if(isset($data['title']) && $data['title']){
  141. array_push($where,['article.title','like','%'.$data['title'].'%']);
  142. }
  143. if(isset($data['category_name']) && $data['category_name']){
  144. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  145. }
  146. if(isset($data['author']) && $data['author']){
  147. array_push($where,['article.author','=',$data['author']]);
  148. }
  149. if(isset($data['islink']) && $data['islink']!==""){
  150. array_push($where,['article.islink','=',$data['islink']]);
  151. }
  152. if(isset($data['status']) && $data['status']!==""){
  153. array_push($where,['article.status','=',$data['status']]);
  154. }
  155. $rep = Article::where($where)
  156. ->whereNotIn('article.status',[404])
  157. ->leftJoin('category','article.catid','category.id')
  158. ->select("article.*","category.name as category_name")
  159. ->orderBy("article.id","desc")
  160. ->limit($data['pageSize'])
  161. ->offset(($data['page']-1)*$data['pageSize'])->get();
  162. $count = Article::where($where)->whereNotIn('article.status',[404])
  163. ->leftJoin('category','article.catid','category.id')->count();
  164. $data = [
  165. 'rows'=>$rep->toArray(),
  166. 'count'=>$count
  167. ];
  168. if(empty($rep)){
  169. return Result::error("没有信息数据");
  170. }
  171. return Result::success($data);
  172. }
  173. /**
  174. * @param array $data
  175. * @return array
  176. */
  177. public function addArticle(array $data): array
  178. {
  179. Db::beginTransaction();
  180. try{
  181. $articleData = $data;
  182. unset($articleData['content']);
  183. $id = Article::insertGetId($articleData);
  184. $articleDataContent = [
  185. 'article_id'=>$id,
  186. 'content'=>$data['content']
  187. ];
  188. ArticleData::insertGetId($articleDataContent);
  189. Db::commit();
  190. } catch(\Throwable $ex){
  191. Db::rollBack();
  192. var_dump($ex->getMessage());
  193. return Result::error("创建失败",0);
  194. }
  195. return Result::success(['id'=>$id]);
  196. }
  197. /**
  198. * @param array $data
  199. * @return array
  200. */
  201. public function delArticle(array $data): array
  202. {
  203. $result = Article::where($data)->update(['status'=>404]);
  204. if(!$result){
  205. return Result::error("删除失败");
  206. }
  207. return Result::success($result);
  208. }
  209. /**
  210. * @param array $data
  211. * @return array
  212. */
  213. public function updateArticle(array $data): array
  214. {
  215. Db::beginTransaction();
  216. try{
  217. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  218. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  219. $articleData = $data;
  220. unset($articleData['content']);
  221. unset($articleData['status_name']);
  222. unset($articleData['name']);
  223. unset($articleData['content']);
  224. unset($articleData['pid_arr']);
  225. unset($articleData['pid']);
  226. $id = Article::where(['id'=>$data['id']])->update($articleData);
  227. $articleDataContent = [
  228. 'content'=>$data['content']
  229. ];
  230. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  231. } catch(\Throwable $ex){
  232. Db::rollBack();
  233. var_dump($ex->getMessage());
  234. return Result::error("更新失败",0);
  235. }
  236. return Result::success([]);
  237. }
  238. /**
  239. * 更新资讯状态
  240. * @param array $data
  241. * @return array
  242. */
  243. public function upArticleStatus(array $data):array
  244. {
  245. $result = Article::where(['id'=>$data['id']])->update($data);
  246. if($result){
  247. return Result::success();
  248. }else{
  249. return Result::error("更新状态失败",0);
  250. }
  251. }
  252. /**
  253. * 获取新闻详情
  254. * @param array $data
  255. * @return array
  256. */
  257. public function getArticleInfo(array $data): array
  258. {
  259. $where = [
  260. 'article.id'=>$data['id'],
  261. // 'article.status'=>1
  262. ];
  263. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  264. if(empty($result)){
  265. return Result::error("查询失败",0);
  266. }
  267. return Result::success($result);
  268. }
  269. /**
  270. * 获取头条新闻
  271. * @param array $data
  272. * @return array
  273. */
  274. public function getWebsiteArticlett(array $data): array
  275. {
  276. // return Result::success($data['website_id']);
  277. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  278. $category = array_values(array_unique($category->toArray()));
  279. $result = [];
  280. if ($category) {
  281. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  282. $where = [
  283. 'status' => 1,
  284. ];
  285. var_dump($data, 'data-----------------');
  286. // level=7 根据文章key来匹配文章
  287. if (isset($data['level']) && $data['level'] == 7) {
  288. // 根据文章id获取key
  289. // $data['id'] = 50142;
  290. if (isset($data['id']) && !empty($data['id'])) {
  291. $keyword = Article::where('id', $data['id'])->value('keyword');
  292. $keywordArray = explode(',', $keyword);
  293. $whereL7 = [];
  294. foreach ($keywordArray as $k => $v) {
  295. $whereL7[] = ['keyword', 'like', '%' . $v . '%'];
  296. }
  297. // 原始查询
  298. $result = Article::where($whereL7)
  299. ->offset($placeid)
  300. ->limit($data['pageSize'])
  301. ->orderBy('updated_at', 'desc')
  302. ->get()
  303. ->map(function ($article ) use ($data) {
  304. $catid = $article->catid;
  305. $pinyin = '';
  306. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  307. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  308. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  309. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  310. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  311. }
  312. } else {
  313. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  314. }
  315. $article->pinyin = $pinyin;
  316. return $article;
  317. });
  318. if (empty($result)) {
  319. return Result::success([]);
  320. }
  321. return Result::success($result);
  322. } else {
  323. return Result::error("参数错误level=7,id不能为空", 0);
  324. }
  325. }
  326. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  327. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  328. var_dump($where, 'where-----------------');
  329. $result = Article::where($where)
  330. ->whereIn("catid", $category)
  331. // ->leftJoin('website_category', 'article.catid', 'website_category.category_id')
  332. // ->where('website_category.website_id', $data['website_id'])
  333. ->where(function ($query) use ($data) {
  334. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  335. ->orWhereNull("ignore_ids");
  336. })
  337. //$data['level'] == 4 || $data['level'] == 5 查询随机
  338. ->when($data['level'] == 5, function ($query) {
  339. $query->inRandomOrder()
  340. //updated_at最近三十天;
  341. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  342. })
  343. ->when($data['level'] == 4, function ($query) {
  344. $query->orderBy("article.updated_at", "desc");
  345. })
  346. ->when(!empty($data['level']), function ($query) use ($data) {
  347. if ($data['level'] != 4 && $data['level'] != 5) {
  348. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  349. ->orderBy("article.updated_at", "desc");
  350. }
  351. })
  352. ->select('article.*')
  353. ->offset($placeid)
  354. ->limit($data['pageSize'])
  355. ->get()
  356. ->map(function ($article ) use ($data) {
  357. $catid = $article->catid;
  358. $pinyin = '';
  359. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  360. if (!empty($category->aLIas_pinyin) && $category->pid != 0) {
  361. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  362. if ($childCategory && !empty($childCategory->aLIas_pinyin)) {
  363. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  364. }
  365. } else {
  366. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  367. }
  368. $article->pinyin = $pinyin;
  369. return $article;
  370. });
  371. if (empty($result)) {
  372. return Result::error("暂无头条新闻", 0);
  373. }
  374. return Result::success($result);
  375. } else {
  376. return Result::error("本网站下暂无相关栏目", 0);
  377. }
  378. }
  379. /**
  380. * 获取模块新闻
  381. * @param array $data
  382. * @return array
  383. */
  384. public function getWebsiteModelArticles(array $data): array
  385. {
  386. $catid = $data['catid'];
  387. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  388. $category = $category->toArray();
  389. if (!empty($category)) {
  390. $where = [
  391. 'status' => 1,
  392. 'catid' => $catid,
  393. ];
  394. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  395. // 1:文字新闻;2:轮播图;3:图文;
  396. // 级别:0:未分类
  397. // 3:推荐图片
  398. if ($data['level'] == 1) {
  399. $data['level'] = 0;
  400. }
  401. $result = Article::where($where)
  402. ->where(function ($query) use ($data) {
  403. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  404. ->orWhereNull("level")
  405. ->orWhereRaw("level = '[]'");
  406. })
  407. ->where(function ($query) use ($data) {
  408. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  409. ->orWhereNull("ignore_ids");
  410. })
  411. ->orderBy("updated_at", "desc")
  412. ->offset($placeid)
  413. ->limit($data['pagesize'])
  414. ->get();
  415. if (empty($result)) {
  416. return Result::error("此栏目暂无相关新闻", 0);
  417. }
  418. } else {
  419. return Result::error("此网站暂无此栏目", 0);
  420. }
  421. return Result::success($result);
  422. }
  423. /**
  424. *获取新闻列表
  425. * @param array $data
  426. * @return array
  427. */
  428. public function getWebsiteArticleList(array $data): array
  429. {
  430. // return Result::success($data);
  431. $where[] = ['status', '=', 1];
  432. if (isset($data['catid']) && !empty($data['catid'])) {
  433. if (is_array($data['catid'])) {
  434. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  435. array_push($where, ['catid', 'in', $data['catid']]);
  436. } else {
  437. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  438. array_push($where, ['catid', '=', $data['catid']]);
  439. }
  440. if (empty($category)) {
  441. return Result::error("此网站暂无此栏目", 0);
  442. }
  443. }
  444. // return Result::success($where);
  445. $rep = Article::where(function ($query) use ($where) {
  446. foreach ($where as $condition) {
  447. if ($condition[1] === 'in') {
  448. $query->whereIn($condition[0], $condition[2]);
  449. } else {
  450. $query->where($condition[0], $condition[1], $condition[2]);
  451. }
  452. }
  453. })
  454. ->where(function ($query) use ($data) {
  455. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  456. ->orWhereNull("ignore_ids");
  457. })
  458. ->select(
  459. 'article.id',
  460. 'article.title',
  461. 'article.imgurl',
  462. 'article.author',
  463. 'article.updated_at',
  464. 'article.introduce',
  465. 'article.islink',
  466. 'article.linkurl',
  467. 'article.copyfrom',
  468. 'article.catid')
  469. ->orderBy("updated_at", "desc")
  470. ->limit($data['pageSize'])
  471. ->offset(($data['page'] - 1) * $data['pageSize'])
  472. ->get()
  473. ->map(function ($article ) use ($data) {
  474. $catid = $article->catid ?? 0;
  475. $pinyin = '';
  476. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  477. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  478. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  479. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  480. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  481. }
  482. $article->pinyin = $pinyin;
  483. return $article;
  484. });
  485. $count = Article::where(function ($query) use ($where) {
  486. foreach ($where as $condition) {
  487. if ($condition[1] === 'in') {
  488. $query->whereIn($condition[0], $condition[2]);
  489. } else {
  490. $query->where($condition[0], $condition[1], $condition[2]);
  491. }
  492. }
  493. })->count();
  494. $data = [
  495. 'rows' => $rep->toArray(),
  496. 'count' => $count,
  497. ];
  498. if (empty($rep)) {
  499. return Result::error("没有信息数据");
  500. }
  501. return Result::success($data);
  502. }
  503. /**
  504. * 前端-获取新闻详情
  505. * @param array $data
  506. * @return array
  507. */
  508. public function selectWebsiteArticleInfo(array $data): array
  509. {
  510. $where = [
  511. 'article.id'=>$data['id'],
  512. 'article.status'=>1
  513. ];
  514. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  515. ->where(function ($query) use ($data) {
  516. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  517. ->orWhereNull("ignore_ids");
  518. })
  519. ->first();
  520. if(empty($result)){
  521. return Result::error("暂无此新闻!",0);
  522. }
  523. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  524. if(empty($category)){
  525. return Result::error("查询失败",0);
  526. }
  527. $result['category_id'] = $category['category_id'];
  528. $result['cat_name'] = $category['alias'];
  529. return Result::success($result);
  530. }
  531. /**
  532. * 前端-获取网站调查问卷
  533. * @param array $data
  534. * @return array
  535. */
  536. public function getWebsiteSurvey(array $data): array
  537. {
  538. if(isset($data['website_id']) && !empty($data['website_id'])){
  539. $website = Website::where('id',$data['website_id'])->first();
  540. if(empty($website)){
  541. return Result::error("暂无此网站",0);
  542. }
  543. }
  544. if(isset($data['art_id']) && !empty($data['art_id'])){
  545. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  546. if(empty($article)){
  547. return Result::error("暂无此文章",0);
  548. }
  549. // return Result::error($data,0);
  550. $where['art_id'] = $data['art_id'];
  551. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  552. }else{
  553. $survey = Article::where(function ($query) {
  554. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  555. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  556. })
  557. ->where('status',1)
  558. ->where('is_survey',1)
  559. ->select('survey_id')
  560. ->orderBy('updated_at','desc')
  561. ->first();
  562. if(empty($survey)){
  563. return Result::error("暂无调查问卷",0);
  564. }
  565. $where['sur_id'] = $survey['survey_id'];
  566. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  567. }
  568. // return Result::success($where);
  569. $result = ArticleSurvey::where($where)
  570. ->where(function ($query) {
  571. $query->where('is_other', 0)
  572. ->orWhere(function ($subQuery) {
  573. $subQuery->where('is_other', 1)
  574. ->where('other_id', 0);
  575. });
  576. })
  577. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  578. ->select('article_survey.*', 'article.survey_type')
  579. ->get()->all();
  580. if(empty($result)){
  581. return Result::error("此文章暂无调查问卷",0);
  582. }
  583. return Result::success($result);
  584. }
  585. /**
  586. * 前端-添加网站调查问卷选项
  587. * @param array $data
  588. * @return array
  589. */
  590. public function addWebsiteSurveyOption(array $data): array
  591. {
  592. if(isset($data['website_id']) && !empty($data['website_id'])){
  593. $website = Website::where('id',$data['website_id'])->first();
  594. if(empty($website)){
  595. return Result::error("暂无此网站",0);
  596. }
  597. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  598. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  599. if(empty($survey)){
  600. return Result::error("此调查问卷不可添加选项",0);
  601. }
  602. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  603. $choice = [
  604. 'art_id'=>$survey['art_id'],
  605. 'website_id'=>$data['website_id'],
  606. 'survey_name'=>$survey['survey_name'],
  607. 'choice_name'=>$data['choice_name'],
  608. 'sur_id'=>$survey['sur_id'],
  609. 'is_other'=>1,
  610. 'other_id'=>$survey['id'],
  611. ];
  612. $result = ArticleSurvey::insertGetId($choice);
  613. if(empty($result)){
  614. return Result::error("添加失败",0);
  615. }
  616. return Result::success($result);
  617. }
  618. }
  619. return Result::error("添加失败",0);
  620. }
  621. return Result::error("添加失败",0);
  622. }
  623. /**
  624. * 前端-调查问卷投票
  625. * @param array $data
  626. * @return array
  627. */
  628. public function addWebsiteSurveyVote(array $data): array
  629. {
  630. // return Result::success($data);
  631. if(isset($data['website_id']) && !empty($data['website_id'])){
  632. $website = Website::where('id',$data['website_id'])->first();
  633. if(empty($website)){
  634. return Result::error("暂无此网站",0);
  635. }
  636. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  637. $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
  638. // return Result::success($survey);
  639. if(empty($is_survey)){
  640. return Result::error("此调查问卷不存在",0);
  641. }
  642. // return Result::success($survey);
  643. // 调查问卷类型
  644. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  645. //多选 若是json型则转化成数组类型
  646. if (strpos($data['choice_id'], '[') === 0) {
  647. $data['choice_id'] = json_decode($data['choice_id'], true);
  648. } else {
  649. // 单选 也转换成数组
  650. $data['choice_id'] = [$data['choice_id']];
  651. }
  652. $data['choice_id'] = array_map('intval', $data['choice_id']);
  653. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  654. ->where('website_id',$data['website_id'])
  655. ->where('is_other',1)
  656. ->where('other_id',0)
  657. ->first();
  658. if(!empty($other)){
  659. return Result::error("请选择已有的选项!",0);
  660. }
  661. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  662. ->where('website_id',$data['website_id'])
  663. ->where('is_other',1)
  664. ->where('other_id','!=',0)
  665. ->first();
  666. // return Result::success($choice['other']);
  667. $choice_id = $data['choice_id'];
  668. if(!empty($choice['other'])){
  669. // array_push($data['choice_id'],$choice['other']['other_id']);
  670. if(!empty($choice_id)){
  671. $key = array_search($choice['other']['id'], $choice_id);
  672. if ($key!== false) {
  673. unset($choice_id[$key]);
  674. $choice_id = array_values($choice_id);
  675. }
  676. array_push($choice_id,$choice['other']['other_id']);
  677. }else{
  678. $choice_id[0] = $choice['other']['other_id'];
  679. }
  680. array_push($data['choice_id'],$choice['other']['other_id']);
  681. }
  682. // return Result::success($data);
  683. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  684. ->where('website_id',$data['website_id'])
  685. ->increment('results', 1);
  686. if(empty($choice)){
  687. return Result::error("请选择已有的选项!",0);
  688. }
  689. $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
  690. ->where('website_id',$data['website_id'])
  691. ->where('other_id', 0)
  692. ->get();
  693. $survey['choice'] = $choice_id;
  694. return Result::success($survey);
  695. }
  696. return Result::error("参数必填!");
  697. }
  698. return Result::error("此调查问卷不存在",0);
  699. }
  700. return Result::error("参数必填!");
  701. }
  702. /**
  703. * 后端-获取网站调查问卷列表
  704. * @param array $data
  705. * @return array
  706. */
  707. public function getSurveyList(array $data): array
  708. {
  709. $where = [];
  710. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  711. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  712. }
  713. if(isset($data['survey_type']) && $data['survey_type'] != null){
  714. array_push($where,['survey_type','=',$data['survey_type']]);
  715. }
  716. if(isset($data['is_survey']) && $data['is_survey'] != null){
  717. array_push($where,['is_survey','=',$data['is_survey']]);
  718. }
  719. // return Result::success($where);
  720. if (!empty($where)) {
  721. $query = Article::where($where)->where(function ($q) {
  722. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  723. });
  724. } else {
  725. $query = Article::where(function ($q) {
  726. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  727. });
  728. }
  729. $count = $query->count();
  730. $survey = $query->orderByDesc('id')
  731. ->limit($data['pageSize'])
  732. ->offset(($data['page']-1)*$data['pageSize'])
  733. ->get();
  734. if(empty($survey->toArray())){
  735. return Result::error("暂无调查问卷!",0);
  736. }
  737. $result = [
  738. 'rows'=>$survey,
  739. 'count'=>$count
  740. ];
  741. return Result::success($result);
  742. }
  743. /**
  744. * 后端-获取网站调查问卷详情
  745. * @param array $data
  746. * @return array
  747. */
  748. public function getSurveyInfo(array $data): array
  749. {
  750. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  751. $where = [ 'sur_id'=>$data['sur_id']];
  752. $choose = ArticleSurvey::where($where)->where('is_other',0)
  753. ->leftJoin('article','article_survey.art_id','article.id')
  754. ->select('article_survey.*','article.survey_type')
  755. ->get()->all();
  756. if(empty($choose)){
  757. return Result::error("此调查问卷不存在",0);
  758. }
  759. $resultsArray = array_column($choose, 'results');
  760. $total = array_sum($resultsArray);
  761. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  762. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  763. // $total = 0;
  764. if(!empty($other)){
  765. $total = $total + $other['results'];
  766. $other['choice_name'] ='(其他)';
  767. if(!empty($others)){
  768. $other['hasChildren'] = true;
  769. // array_push($other,['hasChildren','=',true]);
  770. $other['children'] = $others;
  771. $other_choices = [$other->toArray()];
  772. $mer_choice = array_merge($choose,$other_choices);
  773. $value_choice = array_values($mer_choice);
  774. }
  775. else{
  776. // return Result::error('1111');
  777. $other_choices = [$other->toArray()];
  778. $other_choices = array_merge($choose,$other_choices);
  779. $value_choice = array_values($other_choices);
  780. // return Result::success($result);
  781. }
  782. }else{
  783. $value_choice = $choose;
  784. }
  785. $result = [
  786. 'choose'=>$value_choice,
  787. 'total'=>$total
  788. ];
  789. }
  790. return Result::success($result);
  791. }
  792. /**
  793. * 前端-搜索新闻列表
  794. * @param array $data
  795. * @return array
  796. */
  797. public function selectWebsiteArticle(array $data): array
  798. {
  799. $where = [];
  800. // 初始化查询构造器
  801. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  802. $query = Article::where('status', 1)
  803. ->whereIn('catid', $category)
  804. ->where(function ($query) use ($data) {
  805. $query->where(function ($subQuery) use ($data) {
  806. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0");
  807. })->orWhereNull("ignore_ids");
  808. });
  809. // return Result::success($all_articles);
  810. // 检查是否存在 cityid 参数
  811. if (isset($data['cityid']) && !empty($data['cityid'])) {
  812. $query->whereRaw("JSON_CONTAINS(city_arr_id, '" . intval($data['cityid']) . "')");
  813. }
  814. // 检查是否存在 department_id 参数
  815. if (isset($data['department_id']) && !empty($data['department_id'])) {
  816. $query->whereRaw("JSON_CONTAINS(department_arr_id, '" . intval($data['department_id']) . "')");
  817. }
  818. // 检查是否存在 keyword 参数
  819. if (isset($data['keyword']) && !empty($data['keyword'])) {
  820. $query->where('title', 'like', '%' . $data['keyword'] . '%');
  821. }
  822. // 计算总数
  823. $count = $query->count();
  824. // 分页查询
  825. $articles = $query
  826. ->select(
  827. 'article.id',
  828. 'article.title',
  829. 'article.imgurl',
  830. 'article.author',
  831. 'article.updated_at',
  832. 'article.introduce',
  833. 'article.islink',
  834. 'article.linkurl',
  835. 'article.copyfrom',
  836. 'article.catid',
  837. 'article.department_arr_id',
  838. 'article.city_arr_id',)
  839. ->orderBy("updated_at", "desc")
  840. ->offset(($data['page'] - 1) * $data['pageSize'])
  841. ->limit($data['pageSize'])
  842. ->get()
  843. ->map(function ($article ) use ($data) {
  844. $catid = $article->catid ?? 0;
  845. $pinyin = '';
  846. $category = WebsiteCategory::where('category_id', $catid)->where('website_category.website_id', $data['website_id'])->first();
  847. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  848. if ($category->pid != 0 && !empty($category->aLIas_pinyin)) {
  849. $childCategory = WebsiteCategory::where('category_id', $category->pid)->where('website_category.website_id', $data['website_id'])->first();
  850. $pinyin = $childCategory->aLIas_pinyin ? $childCategory->aLIas_pinyin.'/'. $category->aLIas_pinyin : null;
  851. }
  852. $article->pinyin = $pinyin;
  853. return $article;
  854. });
  855. if (empty($articles)) {
  856. return Result::error("没有符合条件的资讯数据");
  857. }
  858. $data = [
  859. 'rows' => $articles,
  860. 'count' => $count,
  861. ];
  862. return Result::success($data);
  863. }
  864. /**
  865. * 模块新闻加强版
  866. * @param array $data
  867. * @return array
  868. */
  869. public function getWebsiteCatidArticle(array $data): array
  870. {
  871. // return Result::success($data);
  872. $where = [
  873. // 'category.status' => 1,
  874. 'website_category.category_id' => $data['catid'],
  875. 'website_category.website_id' => $data['website_id'],
  876. // 'article.status' => 1,
  877. ];
  878. // $category = WebsiteCategory::where($where);
  879. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  880. $child_catnum = $data['child_catnum']?? 1;
  881. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  882. $childCategoryIds = array_column($category['child'], 'category_id');
  883. if(empty($childCategoryIds)){
  884. return Result::error("暂无子栏目",0);
  885. }
  886. $imgArticles = [];
  887. $textArticles = [];
  888. // return Result::success($childCategoryIds);
  889. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  890. // 初始化子分类图片新闻和文字新闻数组
  891. // 查询所有子级栏目的图文新闻
  892. $imgArticles = Article::where('catid', $childCategoryIds[0])
  893. ->where(function ($query) use ($data) {
  894. $query->where(function ($subQuery) use ($data) {
  895. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  896. })->orWhereNull("ignore_ids");
  897. })
  898. ->where('status', 1)
  899. ->where('imgurl', '!=', '')
  900. ->select('*')
  901. ->orderBy('updated_at', 'desc')
  902. ->limit($data['child_imgnum'])
  903. ->get();
  904. }
  905. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  906. // 查询所有子级栏目的文字新闻
  907. $textArticles = Article::where('catid', $childCategoryIds[0])
  908. ->where('status', 1)
  909. ->where(function ($query) use ($data) {
  910. $query->where(function ($subQuery) use ($data) {
  911. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  912. })->orWhereNull("ignore_ids");
  913. })
  914. // ->where(function ($query) {
  915. // $query->whereNull('imgurl')
  916. // ->orWhere('imgurl', '');
  917. // })
  918. ->select('*')
  919. ->orderBy('updated_at', 'desc')
  920. ->limit($data['child_textnum'])
  921. ->get();
  922. }
  923. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  924. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  925. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  926. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  927. return $child;
  928. }, $category['child']);
  929. }
  930. // }
  931. if (isset($data['img_num']) && !empty($data['img_num'])) {
  932. $category['img'] = WebsiteCategory::where($where)
  933. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  934. ->where('article.status', 1)
  935. ->where('article.imgurl', '!=', '')
  936. ->select('article.*','website_category.category_id','website_category.alias')
  937. ->orderBy('article.updated_at', 'desc')
  938. ->limit($data['img_num'])
  939. ->get();
  940. }
  941. if (isset($data['text_num']) && !empty($data['text_num'])) {
  942. $category['text'] = WebsiteCategory::where($where)
  943. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  944. ->where('article.status', 1)
  945. // ->where(function ($query) {
  946. // $query->whereNull('article.imgurl')
  947. // ->orWhere('article.imgurl', '');
  948. // })
  949. ->select('article.*','website_category.category_id','website_category.alias')
  950. ->orderBy('article.updated_at', 'desc')
  951. ->limit($data['text_num'])
  952. ->get();
  953. }
  954. // $category = $category->get();
  955. if(empty($category)){
  956. return Result::error("查询失败", 0);
  957. }
  958. return Result::success($category);
  959. }
  960. /**
  961. * 模块新闻加强plus版
  962. * @param array $data
  963. * @return array
  964. */
  965. public function getWebsiteAllArticle(array $data): array
  966. {
  967. // 修正传入的字符串,将单引号替换为双引号
  968. $input['id'] = $data['id'];
  969. $input['website_id'] = $data['website_id'];
  970. // 将 JSON 字符串转换为 PHP 数组
  971. $data = json_decode($input['id'], true);
  972. // 使用 array_map 处理每个元素
  973. $result = array_map(function ($item) use ($input) {
  974. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  975. $website = [
  976. 'website_id' => $input['website_id'],
  977. ];
  978. // 查询栏目名称
  979. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  980. if(empty($category)){
  981. $imgArticles = [];
  982. $textArticles = [];
  983. // return Result::error("暂无此栏目",0);
  984. }else{
  985. $parent_alias = $category->alias ?? '';
  986. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  987. // 查询图片新闻
  988. $imgArticles = Article::where('catid', $parentCatId)
  989. ->where('status', 1)
  990. ->where(function ($query) use ($website) {
  991. $query->where(function ($subQuery) use ($website) {
  992. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  993. })->orWhereNull("ignore_ids");
  994. })
  995. ->where('imgurl', '!=', '')
  996. ->select(
  997. 'article.id',
  998. 'article.title',
  999. 'article.imgurl',
  1000. 'article.author',
  1001. 'article.updated_at',
  1002. 'article.introduce',
  1003. 'article.islink',
  1004. 'article.linkurl',
  1005. 'article.copyfrom',
  1006. DB::raw("'$parent_pinyin' as pinyin") // 添加 pinyin 字段
  1007. )
  1008. ->orderBy('updated_at', 'desc')
  1009. ->limit($parentImgNum)
  1010. ->get()->all();
  1011. // 查询文字新闻
  1012. $textArticles = Article::where('catid', $parentCatId)
  1013. ->where('status', 1)
  1014. ->where(function ($query) use ($website) {
  1015. $query->where(function ($subQuery) use ($website) {
  1016. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1017. })->orWhereNull("ignore_ids");
  1018. })
  1019. ->select(
  1020. 'article.id',
  1021. 'article.title',
  1022. 'article.author',
  1023. 'article.updated_at',
  1024. 'article.introduce',
  1025. 'article.islink',
  1026. 'article.linkurl',
  1027. 'article.copyfrom',
  1028. DB::raw("'$parent_pinyin' as pinyin"))
  1029. ->orderBy('updated_at', 'desc')
  1030. ->limit($parentTextNum)
  1031. ->get()->all();
  1032. }
  1033. $resultItem = [
  1034. 'alias' => $parent_alias,
  1035. 'category_id' => $parentCatId,
  1036. 'pinyin' => $parent_pinyin,
  1037. 'imgnum' => $imgArticles,
  1038. 'textnum' => $textArticles,
  1039. ];
  1040. if (!empty($item['child']) && $item['child'] != "") {
  1041. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1042. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1043. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1044. ->get()->all();
  1045. if ($childCategory) {
  1046. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1047. // 查询子栏目名称
  1048. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1049. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1050. ->first();
  1051. $child_pinyin = $childCategoryInfo->aLIas_pinyin ? $childCategoryInfo->aLIas_pinyin : null;
  1052. if(empty($childCategoryInfo)){
  1053. $childImgArticles = [];
  1054. $childTextArticles = [];
  1055. }else{
  1056. // 查询子栏目图片新闻
  1057. $childImgArticles = Article::where('catid', $childCatId)
  1058. ->where('status', 1)
  1059. ->where(function ($query) use ($website) {
  1060. $query->where(function ($subQuery) use ($website) {
  1061. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1062. })->orWhereNull("ignore_ids");
  1063. })
  1064. ->select(
  1065. 'article.id',
  1066. 'article.title',
  1067. 'article.imgurl',
  1068. 'article.author',
  1069. 'article.updated_at',
  1070. 'article.introduce',
  1071. 'article.islink',
  1072. 'article.linkurl',
  1073. 'article.copyfrom',
  1074. DB::raw("'$child_pinyin' as pinyin"))
  1075. ->where('imgurl', '!=', '')
  1076. ->orderBy('updated_at', 'desc')
  1077. ->limit($childImgNum)
  1078. ->get()->all();
  1079. // 查询子栏目文字新闻
  1080. $childTextArticles = Article::where('catid', $childCatId)
  1081. ->where('status', 1)
  1082. ->where(function ($query) use ($website) {
  1083. $query->where(function ($subQuery) use ($website) {
  1084. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1085. })->orWhereNull("ignore_ids");
  1086. })
  1087. ->select(
  1088. 'article.id',
  1089. 'article.title',
  1090. 'article.author',
  1091. 'article.updated_at',
  1092. 'article.introduce',
  1093. 'article.islink',
  1094. 'article.linkurl',
  1095. 'article.copyfrom',
  1096. DB::raw("'$child_pinyin' as pinyin"))
  1097. ->orderBy('updated_at', 'desc')
  1098. ->limit($childTextNum)
  1099. ->get()->all();
  1100. }
  1101. $resultItem['child'] = [
  1102. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1103. 'category_id' => $childCatId,
  1104. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1105. 'all_childcat' => $childCategory,
  1106. 'imgnum' => $childImgArticles,
  1107. 'textnum' => $childTextArticles,
  1108. ];
  1109. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1110. }
  1111. }
  1112. return $resultItem;
  1113. }, $data);
  1114. return Result::success($result);
  1115. // return Result::success($data);
  1116. }
  1117. /**
  1118. * 乡村网-获取特殊新闻模块
  1119. * @param array $data
  1120. * @return array
  1121. */
  1122. public function getWebsiteArticles(array $data): array
  1123. {
  1124. $input['id'] = $data['id'];
  1125. $input['website_id'] = $data['website_id'];
  1126. $data = json_decode($input['id'], true);
  1127. // 使用 array_map 处理每个元素
  1128. $result = array_map(function ($item) use ($input) {
  1129. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1130. $website = [
  1131. 'website_id' => $input['website_id']
  1132. ];
  1133. // 查询栏目名称
  1134. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1135. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1136. if(empty($category)){
  1137. $imgArticles = [];
  1138. $textArticles = [];
  1139. // return Result::error("暂无此栏目",0);
  1140. }else{
  1141. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1142. // return Result::success($website);
  1143. // 查询图片新闻
  1144. // 合并查询条件
  1145. $baseQuery = Article::where(function ($query) use ($website) {
  1146. $query->where(function ($subQuery) use ($website) {
  1147. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1148. })->orWhereNull("ignore_ids");
  1149. })
  1150. ->whereRaw("JSON_CONTAINS(category_arr_id, '" . intval($parentCatId) . "')")
  1151. ->leftJoin('website_category', 'website_category.category_id', 'article.catid')
  1152. ->where('website_category.website_id', $website['website_id'])
  1153. ->where('article.status', 1);
  1154. // 查询文字新闻
  1155. $textArticles = clone $baseQuery;
  1156. $textArticles = $textArticles
  1157. ->select(
  1158. 'article.id',
  1159. 'article.title',
  1160. // 'article.imgurl',
  1161. 'article.author',
  1162. 'article.updated_at',
  1163. 'article.introduce',
  1164. 'article.islink',
  1165. 'article.linkurl',
  1166. 'article.copyfrom',
  1167. 'website_category.category_id',
  1168. 'website_category.alias',
  1169. 'website_category.aLIas_pinyin'
  1170. )
  1171. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1172. ->orderBy('article.updated_at', 'desc')
  1173. ->limit($parentTextNum)
  1174. ->get()
  1175. ->all();
  1176. // 查询图片新闻
  1177. $imgArticles = clone $baseQuery;
  1178. $imgArticles = $imgArticles
  1179. ->select(
  1180. 'article.id',
  1181. 'article.title',
  1182. 'article.imgurl',
  1183. 'article.author',
  1184. 'article.updated_at',
  1185. 'article.introduce',
  1186. 'article.islink',
  1187. 'article.linkurl',
  1188. 'article.copyfrom',
  1189. 'website_category.category_id',
  1190. 'website_category.alias',
  1191. 'website_category.aLIas_pinyin'
  1192. )
  1193. ->selectRaw("CONCAT(?, aLIas_pinyin ) as aLIas_pinyin", [$parent_alias])
  1194. ->orderBy('article.updated_at', 'desc')
  1195. ->where('imgurl', '!=', '')
  1196. ->limit($parentImgNum)
  1197. ->get()
  1198. ->all();
  1199. }
  1200. $resultItem = [
  1201. 'alias' => $category ? $category->alias : null,
  1202. 'category_id' => $parentCatId,
  1203. 'pinyin' => $pinyin ? $pinyin : null,
  1204. 'imgnum' => $imgArticles ?? [],
  1205. 'textnum' => $textArticles ?? [],
  1206. ];
  1207. return $resultItem;
  1208. }, $data);
  1209. return Result::success($result);
  1210. }
  1211. /**
  1212. *获取头条类新闻模块-合集
  1213. * @param array $data
  1214. * @return array
  1215. */
  1216. public function getWebsiteAllArticlett(array $data): array
  1217. {
  1218. $input['id'] = $data['id'];
  1219. $input['website_id'] = $data['website_id'];
  1220. $data = json_decode($input['id'], true);
  1221. // 使用 array_map 处理每个元素
  1222. $result = array_map(function ($item) use ($input) {
  1223. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1224. $website = [
  1225. 'website_id' => $input['website_id']
  1226. ];
  1227. $category = WebsiteCategory::where('website_id', $input['website_id'])->pluck('category_id');
  1228. $category = array_values(array_unique($category->toArray()));
  1229. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  1230. $where = [
  1231. 'status' => 1,
  1232. ];
  1233. return $category;
  1234. }, $data); // 添加第二个参数 $data,确保 array_map 函数有两个参数
  1235. return Result::success($result);
  1236. }
  1237. }