NewsService.php 43 KB

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