NewsService.php 54 KB

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