NewsService.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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('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. // 检查parent元素是否存在且不是undefined
  975. if (isset($item['parent']) && $item['parent'] != 'undefined' && $item['parent']!= "") {
  976. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  977. $website = [
  978. 'website_id' => $input['website_id'],
  979. ];
  980. // 查询栏目名称
  981. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  982. if(empty($category)){
  983. $parent_alias = '';
  984. $parent_pinyin = null;
  985. $imgArticles = [];
  986. $textArticles = [];
  987. }else{
  988. $parent_alias = $category->alias ?? '';
  989. $parent_pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  990. // 查找子分类ID数组
  991. $childCategoryIds = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  992. array_push($childCategoryIds,$parentCatId);
  993. $childCategoryIds = json_encode(array_values(array_unique($childCategoryIds)));
  994. if($parentImgNum!=0){
  995. // 查询图片新闻
  996. $imgArticles = Article::where('article.status', 1)
  997. ->where(function($query) use ($parentCatId) {
  998. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  999. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1000. })
  1001. ->where(function ($query) use ($website) {
  1002. $query->where(function ($subQuery) use ($website) {
  1003. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1004. ->orWhereNull("ignore_ids");
  1005. });
  1006. })
  1007. ->where('imgurl', '!=', '')
  1008. ->leftJoin('website_category', function($join) use ($website) {
  1009. $join->on('article.catid', '=', 'website_category.category_id')
  1010. ->where('website_category.website_id', '=', $website['website_id']);
  1011. })
  1012. ->select(
  1013. 'article.id',
  1014. 'article.title',
  1015. 'article.imgurl',
  1016. 'article.author',
  1017. 'article.updated_at',
  1018. 'article.introduce',
  1019. 'article.islink',
  1020. 'article.linkurl',
  1021. 'article.copyfrom',
  1022. 'article.catid',
  1023. 'website_category.alias as category_name',
  1024. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1025. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1026. )
  1027. ->orderBy('updated_at', 'desc')
  1028. ->limit($parentImgNum)
  1029. ->get()->all();
  1030. // 查询文字新闻
  1031. }
  1032. if($parentTextNum!=0){
  1033. $textArticles = [];
  1034. $textArticles = Article::where('article.status', 1)
  1035. ->where(function($query) use ($parentCatId) {
  1036. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '\"$parentCatId\"')")
  1037. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '$parentCatId')");
  1038. })
  1039. ->where(function ($query) use ($website) {
  1040. $query->where(function ($subQuery) use ($website) {
  1041. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0")
  1042. ->orWhereNull("ignore_ids");
  1043. });
  1044. })
  1045. ->leftJoin('website_category', function($join) use ($website) {
  1046. $join->on('article.catid', '=', 'website_category.category_id')
  1047. ->where('website_category.website_id', '=', $website['website_id']);
  1048. })
  1049. ->select(
  1050. 'article.id',
  1051. 'article.title',
  1052. 'article.imgurl',
  1053. 'article.author',
  1054. 'article.updated_at',
  1055. 'article.introduce',
  1056. 'article.islink',
  1057. 'article.linkurl',
  1058. 'article.copyfrom',
  1059. 'article.catid',
  1060. 'website_category.alias as category_name',
  1061. DB::raw("CASE WHEN article.catid = $parentCatId THEN '$parent_pinyin'
  1062. ELSE CONCAT('$parent_pinyin', '/', website_category.aLIas_pinyin) END as pinyin")
  1063. )
  1064. ->orderBy('updated_at', 'desc')
  1065. ->limit($parentTextNum)
  1066. ->get()->all();
  1067. }
  1068. }
  1069. }
  1070. $resultItem = [
  1071. 'alias' => $parent_alias ?? '',
  1072. 'category_id' => $parentCatId ?? 0,
  1073. 'pinyin' => $parent_pinyin ?? null,
  1074. 'imgnum' => $imgArticles ?? [],
  1075. 'textnum' => $textArticles ?? [],
  1076. ];
  1077. if(isset($item['child']) && $item['child'] != 'undefined' && $item['child'] != ""){
  1078. $parent_pinyin_str = is_string($parent_pinyin) ? $parent_pinyin.'/' : '';
  1079. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)
  1080. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1081. ->get()->all();
  1082. if (!empty($childCategory)) {
  1083. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  1084. // 查询子栏目名称
  1085. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)
  1086. ->selectRaw("category_id, alias, CONCAT( ?, aLIas_pinyin) as aLIas_pinyin", [$parent_pinyin_str])
  1087. ->first();
  1088. if(empty($childCategoryInfo) || ($childImgNum==0 && $childTextNum==0)){
  1089. $childImgArticles = [];
  1090. $childTextArticles = [];
  1091. $resultItem['child'] = [];
  1092. }else{
  1093. $child_pinyin = $childCategoryInfo->aLIas_pinyin? $childCategoryInfo->aLIas_pinyin : null;
  1094. // 查询子栏目图片新闻
  1095. $childImgArticles = Article::where('catid', $childCatId)
  1096. ->where('status', 1)
  1097. ->where(function ($query) use ($website) {
  1098. $query->where(function ($subQuery) use ($website) {
  1099. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1100. })->orWhereNull("ignore_ids");
  1101. })
  1102. ->select(
  1103. 'article.id',
  1104. 'article.title',
  1105. 'article.imgurl',
  1106. 'article.author',
  1107. 'article.updated_at',
  1108. 'article.introduce',
  1109. 'article.islink',
  1110. 'article.linkurl',
  1111. 'article.copyfrom',
  1112. DB::raw("'$child_pinyin' as pinyin"))
  1113. ->where('imgurl', '!=', '')
  1114. ->orderBy('updated_at', 'desc')
  1115. ->limit($childImgNum)
  1116. ->get()->all();
  1117. // 查询子栏目文字新闻
  1118. $childTextArticles = Article::where('catid', $childCatId)
  1119. ->where('status', 1)
  1120. ->where(function ($query) use ($website) {
  1121. $query->where(function ($subQuery) use ($website) {
  1122. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($website['website_id'])."') = 0");
  1123. })->orWhereNull("ignore_ids");
  1124. })
  1125. ->select(
  1126. 'article.id',
  1127. 'article.title',
  1128. 'article.author',
  1129. 'article.updated_at',
  1130. 'article.introduce',
  1131. 'article.islink',
  1132. 'article.linkurl',
  1133. 'article.copyfrom',
  1134. DB::raw("'$child_pinyin' as pinyin"))
  1135. ->orderBy('updated_at', 'desc')
  1136. ->limit($childTextNum)
  1137. ->get()->all();
  1138. $resultItem['child'] = [
  1139. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  1140. 'category_id' => $childCatId,
  1141. 'pinyin' => $childCategoryInfo->aLIas_pinyin ?? '',
  1142. 'all_childcat' => $childCategory,
  1143. 'imgnum' => $childImgArticles,
  1144. 'textnum' => $childTextArticles,
  1145. ];
  1146. }
  1147. // $resultItem['pinyin'] = $childCategoryInfo->aLIas_pinyin ?? '';
  1148. }
  1149. }else{
  1150. $resultItem['child'] = [];
  1151. }
  1152. return $resultItem;
  1153. }, $data);
  1154. return Result::success($result);
  1155. // return Result::success($data);
  1156. }
  1157. /**
  1158. * 乡村网-获取特殊新闻模块
  1159. * @param array $data
  1160. * @return array
  1161. */
  1162. public function getWebsiteArticles(array $data): array
  1163. {
  1164. $input['id'] = $data['id'];
  1165. $input['website_id'] = $data['website_id'];
  1166. $data = json_decode($input['id'], true);
  1167. // 使用 array_map 处理每个元素
  1168. $result = array_map(function ($item) use ($input) {
  1169. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1170. $website = [
  1171. 'website_id' => $input['website_id']
  1172. ];
  1173. // 查询栏目名称
  1174. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id','aLIas_pinyin']);
  1175. $pinyin = $category->aLIas_pinyin ? $category->aLIas_pinyin : null;
  1176. if(empty($category)){
  1177. $imgArticles = [];
  1178. $textArticles = [];
  1179. // return Result::error("暂无此栏目",0);
  1180. }else{
  1181. $child_category = WebsiteCategory::where('pid', $parentCatId)->where($website)->pluck('category_id')->toArray();
  1182. $parent_alias = $category->aLIas_pinyin ? $category->aLIas_pinyin.'/' : null;
  1183. // return Result::success($website);
  1184. // 查询图片新闻
  1185. // 合并查询条件
  1186. $baseQuery = Article::where(function ($query) use ($website) {
  1187. $query->where(function ($subQuery) use ($website) {
  1188. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($website['website_id']) . "') = 0");
  1189. })->orWhereNull("ignore_ids");
  1190. })
  1191. ->where('website_category.website_id', $website['website_id'])
  1192. ->where('article.status', 1)
  1193. ->leftJoin('website_category', 'website_category.category_id', 'article.catid');
  1194. // 查询文字新闻
  1195. $textArticles = clone $baseQuery;
  1196. $textArticles = $textArticles->whereIn('catid',$child_category)
  1197. ->select(
  1198. 'article.id',
  1199. 'article.title',
  1200. // 'article.imgurl',
  1201. 'article.author',
  1202. 'article.updated_at',
  1203. 'article.introduce',
  1204. 'article.islink',
  1205. 'article.linkurl',
  1206. 'article.copyfrom',
  1207. 'website_category.category_id',
  1208. 'website_category.alias',
  1209. 'website_category.aLIas_pinyin'
  1210. )
  1211. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1212. ->orderBy('article.updated_at', 'desc')
  1213. ->limit($parentTextNum)
  1214. ->get()
  1215. ->all();
  1216. if(empty($textArticles)){
  1217. $textArticles = clone $baseQuery;
  1218. $textArticles = $textArticles->where('catid',$parentCatId)
  1219. ->select(
  1220. 'article.id',
  1221. 'article.title',
  1222. // 'article.imgurl',
  1223. 'article.author',
  1224. 'article.updated_at',
  1225. 'article.introduce',
  1226. 'article.islink',
  1227. 'article.linkurl',
  1228. 'article.copyfrom',
  1229. 'website_category.category_id',
  1230. 'website_category.alias',
  1231. 'website_category.aLIas_pinyin'
  1232. )
  1233. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1234. ->orderBy('article.updated_at', 'desc')
  1235. ->limit($parentTextNum)
  1236. ->get()
  1237. ->all();
  1238. }
  1239. // 查询图片新闻
  1240. $imgArticles = clone $baseQuery;
  1241. $imgArticles = $imgArticles->where('imgurl', '!=', '')->whereIn('catid',$child_category)
  1242. ->select(
  1243. 'article.id',
  1244. 'article.title',
  1245. 'article.imgurl',
  1246. 'article.author',
  1247. 'article.updated_at',
  1248. 'article.introduce',
  1249. 'article.islink',
  1250. 'article.linkurl',
  1251. 'article.copyfrom',
  1252. 'website_category.category_id',
  1253. 'website_category.alias',
  1254. 'website_category.aLIas_pinyin'
  1255. )
  1256. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1257. ->orderBy('article.updated_at', 'desc')
  1258. ->limit($parentImgNum)
  1259. ->get()
  1260. ->all();
  1261. if(empty($imgArticles)){
  1262. $imgArticles = clone $baseQuery;
  1263. $imgArticles = $imgArticles->where('imgurl', '!=', '')->where('catid',$parentCatId)
  1264. ->select(
  1265. 'article.id',
  1266. 'article.title',
  1267. 'article.imgurl',
  1268. 'article.author',
  1269. 'article.updated_at',
  1270. 'article.introduce',
  1271. 'article.islink',
  1272. 'article.linkurl',
  1273. 'article.copyfrom',
  1274. 'website_category.category_id',
  1275. 'website_category.alias',
  1276. 'website_category.aLIas_pinyin'
  1277. )
  1278. ->selectRaw("CONCAT(?, aLIas_pinyin) as aLIas_pinyin", [$parent_alias])
  1279. ->orderBy('article.updated_at', 'desc')
  1280. ->limit($parentImgNum)
  1281. ->get()
  1282. ->all();
  1283. }
  1284. }
  1285. $resultItem = [
  1286. 'alias' => $category ? $category->alias : null,
  1287. 'category_id' => $parentCatId,
  1288. 'pinyin' => $pinyin ? $pinyin : null,
  1289. 'imgnum' => $imgArticles ?? [],
  1290. 'textnum' => $textArticles ?? [],
  1291. ];
  1292. return $resultItem;
  1293. }, $data);
  1294. return Result::success($result);
  1295. }
  1296. /**
  1297. *获取头条类新闻模块-合集(暂时用不到)
  1298. * @param array $data
  1299. * @return array
  1300. */
  1301. public function getWebsiteAllArticlett(array $data): array
  1302. {
  1303. $input['id'] = $data['id'];
  1304. $input['website_id'] = $data['website_id'];
  1305. $data = json_decode($input['id'], true);
  1306. // 使用 array_map 处理每个元素
  1307. $result = array_map(function ($item) use ($input) {
  1308. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  1309. $website = [
  1310. 'website_id' => $input['website_id']
  1311. ];
  1312. $category = WebsiteCategory::where('website_id', $input['website_id'])->pluck('category_id');
  1313. $category = array_values(array_unique($category->toArray()));
  1314. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  1315. $where = [
  1316. 'status' => 1,
  1317. ];
  1318. return $category;
  1319. }, $data); // 添加第二个参数 $data,确保 array_map 函数有两个参数
  1320. return Result::success($result);
  1321. }
  1322. }