NewsService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. $city_id='';
  30. if(isset($data['city_id']) && $data['city_id']){
  31. $city_id = intval($data['city_id']);
  32. }
  33. $rep = Category::where($where)
  34. ->when($city_id, function ($query) use ($city_id) {
  35. if(isset($city_id) && $city_id) {
  36. $query->whereJsonContains("category.city_arr_id", $city_id);
  37. }
  38. })
  39. ->leftJoin('district','category.city_id','district.id')
  40. ->leftJoin('department','category.department_id','department.id')
  41. ->select("category.*","district.name as city_name","department.name as department_name")
  42. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  43. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  44. if(isset($city_id) && $city_id) {
  45. $query->whereJsonContains("category.city_arr_id", $city_id);
  46. }
  47. })->count();
  48. $data = [
  49. 'rows'=>$rep->toArray(),
  50. 'count'=>$count
  51. ];
  52. if(empty($rep->toArray())){
  53. return Result::error("没有导航池数据");
  54. }
  55. return Result::success($data);
  56. }
  57. /**
  58. * @param array $data
  59. * @return array
  60. */
  61. public function categoryList(array $data): array
  62. {
  63. $where[] = [
  64. 'pid','=',$data['pid']
  65. ];
  66. if(isset($data['name'])){
  67. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  68. }
  69. var_dump($where);
  70. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  71. if(empty($result)){
  72. return Result::error("没有栏目数据");
  73. }
  74. return Result::success($result);
  75. }
  76. /**
  77. * @param array $data
  78. * @return array
  79. */
  80. public function addCategory(array $data): array
  81. {
  82. $id = Category::insertGetId($data);
  83. if(empty($id)){
  84. return Result::error("添加失败");
  85. }
  86. return Result::success(['id'=>$id]);
  87. }
  88. /**
  89. * @param array $data
  90. * @return array
  91. */
  92. public function delCategory(array $data): array
  93. {
  94. $categoryList = Category::where(['pid'=>$data['id']])->get();
  95. var_dump("分类列表:",$data,$categoryList);
  96. if($categoryList->toArray()){
  97. return Result::error("分类下面有子分类不能删除");
  98. }
  99. $articleList = Article::where(['catid'=>$data['id']])->get();
  100. var_dump("文章列表:",$articleList);
  101. if($articleList->toArray()){
  102. return Result::error("分类下面有资讯不能删除");
  103. }
  104. $result = Category::where($data)->delete();
  105. if(!$result){
  106. return Result::error("删除失败");
  107. }
  108. return Result::success($result);
  109. }
  110. /**
  111. * @param array $data
  112. * @return array
  113. */
  114. public function updateCategory(array $data): array
  115. {
  116. $where = [
  117. 'id'=>$data['id']
  118. ];
  119. $result = Category::where($where)->update($data);
  120. if($result){
  121. return Result::success($result);
  122. }else{
  123. return Result::error("更新失败");
  124. }
  125. }
  126. /**
  127. * 获取导航池信息
  128. * @param array $data
  129. * @return array
  130. */
  131. public function getCategoryInfo(array $data): array
  132. {
  133. $where = [
  134. 'id'=>$data['id']
  135. ];
  136. $result = Category::where($where)->first();
  137. if($result){
  138. return Result::success($result);
  139. }else{
  140. return Result::error("更新失败");
  141. }
  142. }
  143. /**
  144. * @param array $data
  145. * @return array
  146. */
  147. public function getArticleList(array $data): array
  148. {
  149. $where= [];
  150. if(isset($data['title']) && $data['title']){
  151. array_push($where,['article.title','like','%'.$data['title'].'%']);
  152. }
  153. if(isset($data['category_name']) && $data['category_name']){
  154. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  155. }
  156. if(isset($data['author']) && $data['author']){
  157. array_push($where,['article.author','=',$data['author']]);
  158. }
  159. if(isset($data['islink']) && $data['islink']!==""){
  160. array_push($where,['article.islink','=',$data['islink']]);
  161. }
  162. if(isset($data['status']) && $data['status']!==""){
  163. array_push($where,['article.status','=',$data['status']]);
  164. }
  165. $rep = Article::where($where)
  166. ->whereNotIn('article.status',[404])
  167. ->leftJoin('category','article.catid','category.id')
  168. ->select("article.*","category.name as category_name")
  169. ->orderBy("article.id","desc")
  170. ->limit($data['pageSize'])
  171. ->offset(($data['page']-1)*$data['pageSize'])->get();
  172. $count = Article::where($where)->whereNotIn('article.status',[404])
  173. ->leftJoin('category','article.catid','category.id')->count();
  174. $data = [
  175. 'rows'=>$rep->toArray(),
  176. 'count'=>$count
  177. ];
  178. if(empty($rep)){
  179. return Result::error("没有信息数据");
  180. }
  181. return Result::success($data);
  182. }
  183. /**
  184. * @param array $data
  185. * @return array
  186. */
  187. public function addArticle(array $data): array
  188. {
  189. Db::beginTransaction();
  190. try{
  191. $articleData = $data;
  192. unset($articleData['content']);
  193. $id = Article::insertGetId($articleData);
  194. $articleDataContent = [
  195. 'article_id'=>$id,
  196. 'content'=>$data['content']
  197. ];
  198. ArticleData::insertGetId($articleDataContent);
  199. Db::commit();
  200. } catch(\Throwable $ex){
  201. Db::rollBack();
  202. var_dump($ex->getMessage());
  203. return Result::error("创建失败",0);
  204. }
  205. return Result::success(['id'=>$id]);
  206. }
  207. /**
  208. * @param array $data
  209. * @return array
  210. */
  211. public function delArticle(array $data): array
  212. {
  213. $result = Article::where($data)->delete();
  214. if(!$result){
  215. return Result::error("删除失败");
  216. }
  217. return Result::success($result);
  218. }
  219. /**
  220. * @param array $data
  221. * @return array
  222. */
  223. public function updateArticle(array $data): array
  224. {
  225. Db::beginTransaction();
  226. try{
  227. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  228. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  229. $articleData = $data;
  230. unset($articleData['content']);
  231. unset($articleData['status_name']);
  232. unset($articleData['name']);
  233. unset($articleData['content']);
  234. unset($articleData['pid_arr']);
  235. unset($articleData['pid']);
  236. $id = Article::where(['id'=>$data['id']])->update($articleData);
  237. $articleDataContent = [
  238. 'content'=>$data['content']
  239. ];
  240. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  241. } catch(\Throwable $ex){
  242. Db::rollBack();
  243. var_dump($ex->getMessage());
  244. return Result::error("更新失败",0);
  245. }
  246. return Result::success([]);
  247. }
  248. /**
  249. * 更新资讯状态
  250. * @param array $data
  251. * @return array
  252. */
  253. public function upArticleStatus(array $data):array
  254. {
  255. $result = Article::where(['id'=>$data['id']])->update($data);
  256. if($result){
  257. return Result::success();
  258. }else{
  259. return Result::error("更新状态失败",0);
  260. }
  261. }
  262. /**
  263. * 获取新闻详情
  264. * @param array $data
  265. * @return array
  266. */
  267. public function getArticleInfo(array $data): array
  268. {
  269. $where = [
  270. 'article.id'=>$data['id'],
  271. 'article.status'=>1
  272. ];
  273. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  274. if(empty($result)){
  275. return Result::error("查询失败",0);
  276. }
  277. return Result::success($result);
  278. }
  279. /**
  280. * 获取头条新闻
  281. * @param array $data
  282. * @return array
  283. */
  284. public function getWebsiteArticlett(array $data): array
  285. {
  286. $category = WebsiteCategory::where('website_id',$data['website_id'])->select('category_id')->get();
  287. $category = $category->toArray();
  288. $result= [];
  289. if($category){
  290. $category_ids = [];
  291. foreach($category as $val){
  292. array_push($category_ids,$val['category_id']);
  293. }
  294. if(isset($data['placeid'])){
  295. $placeid=$data['placeid']-1;
  296. $result=Article::where('status',1)->where('level',$data['level'])->whereIn("catid",$category_ids)->orderBy("created_at","desc")->offset($placeid)->limit($data['pageSize'])->get();
  297. }else{
  298. $result=Article::where('status',1)->where('level',$data['level'])->whereIn("catid",$category_ids)->orderBy("created_at","desc")->offset(0)->limit($data['pageSize'])->get();
  299. }
  300. if(empty($result)){
  301. return Result::error("暂无头条新闻",0);
  302. }
  303. return Result::success($result);
  304. }else{
  305. return Result::error("本网站下暂无相关栏目",0);
  306. }
  307. }
  308. /**
  309. * 获取模块新闻
  310. * @param array $data
  311. * @return array
  312. */
  313. public function getWebsiteModelArticles(array $data): array
  314. {
  315. $catid=$data['catid'];
  316. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$catid)->select('category_id')->get();
  317. $category = $category->toArray();
  318. if(!empty($category)){
  319. $where=[
  320. 'status' => 1,
  321. 'catid' => $catid
  322. ];
  323. if ($data['level'] == 1) {
  324. $level = [
  325. 0 => '1',
  326. 1 => '4',
  327. 2 => '5',
  328. 3 => '0',
  329. ];
  330. $result = Article::where($where)->whereIn('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
  331. }elseif($data['level']==2){
  332. $level='2';
  333. $result = Article::where($where)->where('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
  334. }else{
  335. $level='3';
  336. $result = Article::where($where)->where('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
  337. }
  338. $result= $result->toArray();
  339. if(!empty($result) && isset($data['placeid']) && !empty($data['placeid'])){
  340. $placeid=$data['placeid']-1;
  341. if($level==2 || $level==3){
  342. $where =[
  343. 'level' => $level
  344. ];
  345. $result = Article::where($where)
  346. ->orderBy("created_at","desc")
  347. ->offset($placeid)
  348. ->limit($data['pagesize'])->get();
  349. }else{
  350. $result = Article::where($where)
  351. ->whereIn('level',$level)
  352. ->offset($placeid)
  353. ->orderBy("created_at","desc")
  354. ->limit($data['pagesize'])->get();
  355. }
  356. }
  357. if(empty($result)){
  358. return Result::error("此栏目暂无相关新闻",0);
  359. }
  360. }else{
  361. return Result::error("此网站暂无此栏目",0);
  362. }
  363. return Result::success($result);
  364. }
  365. /**
  366. *获取新闻列表
  367. * @param array $data
  368. * @return array
  369. */
  370. public function getWebsiteArticleList(array $data): array
  371. {
  372. $where[] = ['status', '=', 1];
  373. if(isset($data['keyword']) && !empty($data['keyword'])){
  374. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  375. }
  376. if(isset($data['catid']) && !empty($data['catid'])){
  377. if(is_array($data['catid'])){
  378. $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
  379. array_push($where,['catid', 'in', $data['catid']]);
  380. }else{
  381. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
  382. array_push($where,['catid', '=', $data['catid']]);
  383. }
  384. if(empty($category)){
  385. return Result::error("此网站暂无此栏目",0);
  386. }
  387. }
  388. // return Result::success($where);
  389. $rep = Article::where(function ($query) use ($where) {
  390. foreach ($where as $condition) {
  391. if ($condition[1] === 'in') {
  392. $query->whereIn($condition[0], $condition[2]);
  393. } else {
  394. $query->where($condition[0], $condition[1], $condition[2]);
  395. }
  396. }
  397. })
  398. ->orderBy("created_at", "desc")
  399. ->limit($data['pageSize'])
  400. ->offset(($data['page'] - 1) * $data['pageSize'])
  401. ->get();
  402. $count = Article::where(function ($query) use ($where) {
  403. foreach ($where as $condition) {
  404. if ($condition[1] === 'in') {
  405. $query->whereIn($condition[0], $condition[2]);
  406. } else {
  407. $query->where($condition[0], $condition[1], $condition[2]);
  408. }
  409. }
  410. })->count();
  411. $data = [
  412. 'rows'=>$rep->toArray(),
  413. 'count'=>$count
  414. ];
  415. if(empty($rep)){
  416. return Result::error("没有信息数据");
  417. }
  418. return Result::success($data);
  419. }
  420. /**
  421. * 前端-获取新闻详情
  422. * @param array $data
  423. * @return array
  424. */
  425. public function selectWebsiteArticleInfo(array $data): array
  426. {
  427. $where = [
  428. 'article.id'=>$data['id'],
  429. 'article.status'=>1
  430. ];
  431. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  432. if(empty($result)){
  433. return Result::error("查询失败",0);
  434. }
  435. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  436. if(empty($category)){
  437. return Result::error("查询失败",0);
  438. }
  439. $result['category_id'] = $category['category_id'];
  440. $result['cat_name'] = $category['name'];
  441. return Result::success($result);
  442. }
  443. /**
  444. * 前端-获取网站调查问卷
  445. * @param array $data
  446. * @return array
  447. */
  448. public function getWebsiteSurvey(array $data): array
  449. {
  450. if(isset($data['survey_id']) && !empty($data['survey_id'])){
  451. $website = Website::where('id',$data['website_id'])->first();
  452. if(empty($website)){
  453. return Result::error("暂无此网站",0);
  454. }
  455. }
  456. if(isset($data['art_id']) && !empty($data['art_id'])){
  457. $article = Article::where('id',$data['art_id'])->first();
  458. if(empty($article)){
  459. return Result::error("暂无此文章",0);
  460. }
  461. // return Result::error($data,0);
  462. $where['art_id'] = $data['art_id'];
  463. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  464. }else{
  465. $survey = ArticleSurvey::where('website_id',$data['website_id'])->orderBy('created_at')->first();
  466. if(empty($survey)){
  467. return Result::error("暂无调查问卷",0);
  468. }
  469. $where['sur_id'] = $survey['sur_id'];
  470. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  471. }
  472. $result['survey'] = ArticleSurvey::where($where)->where('is_other',0)
  473. ->leftJoin('article','article_survey.art_id','article.id')
  474. ->select('article_survey.*','article.survey_type')
  475. ->get()->all();
  476. $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  477. $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
  478. if(empty($result)){
  479. return Result::error("此文章暂无调查问卷",0);
  480. }
  481. return Result::success($result);
  482. }
  483. /**
  484. * 前端-添加网站调查问卷选项
  485. * @param array $data
  486. * @return array
  487. */
  488. public function addWebsiteSurveyOption(array $data): array
  489. {
  490. if(isset($data['website_id']) && !empty($data['website_id'])){
  491. $website = Website::where('id',$data['website_id'])->first();
  492. if(empty($website)){
  493. return Result::error("暂无此网站",0);
  494. }
  495. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  496. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  497. if(empty($survey)){
  498. return Result::error("此调查问卷不可添加选项",0);
  499. }
  500. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  501. $choice = [
  502. 'art_id'=>$survey['art_id'],
  503. 'website_id'=>$data['website_id'],
  504. 'survey_name'=>$survey['survey_name'],
  505. 'choice_name'=>$data['choice_name'],
  506. 'sur_id'=>$survey['sur_id'],
  507. 'is_other'=>1,
  508. 'other_id'=>$survey['id'],
  509. ];
  510. $result = ArticleSurvey::insertGetId($choice);
  511. if(empty($result)){
  512. return Result::error("添加失败",0);
  513. }
  514. return Result::success($result);
  515. }
  516. }
  517. return Result::error("添加失败",0);
  518. }
  519. return Result::error("添加失败",0);
  520. }
  521. /**
  522. * 前端-调查问卷投票
  523. * @param array $data
  524. * @return array
  525. */
  526. public function addWebsiteSurveyVote(array $data): array
  527. {
  528. if(isset($data['website_id']) && !empty($data['website_id'])){
  529. $website = Website::where('id',$data['website_id'])->first();
  530. if(empty($website)){
  531. return Result::error("暂无此网站",0);
  532. }
  533. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  534. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->pluck('sur_id');
  535. if(empty($survey)){
  536. return Result::error("此调查问卷不存在",0);
  537. }
  538. // return Result::success($survey);
  539. // 调查问卷类型 0:单选 1:多选
  540. $type = Article::where('survey_id',$data['sur_id'])->pluck('survey_type');
  541. // return Result::success($type);
  542. if(empty($type) || ($type[0]!= 1 && $type[0]!= 0)){
  543. return Result::error("此调查问卷不可投票",0);
  544. }
  545. // return Result::success($type[0]);
  546. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  547. if($type[0] == 0){
  548. if(is_array($data['choice_id'])){
  549. return Result::error("请选择一个选项!",0);
  550. }
  551. $data['choice_id'] = [$data['choice_id']];
  552. }else{
  553. if(!is_array($data['choice_id'])){
  554. return Result::error("请传递数组!",0);
  555. }
  556. }
  557. // return Result::success($data['choice_id']);
  558. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  559. ->where('website_id',$data['website_id'])
  560. ->where('is_other',1)
  561. ->where('other_id','!=',0)
  562. ->first();
  563. // return Result::success($data);
  564. if(!empty($choice['other'])){
  565. array_push($data['choice_id'],$choice['other']['other_id']);
  566. // return Result::success($data['choice_id']);
  567. }
  568. // return Result::success($data);
  569. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  570. ->where('website_id',$data['website_id'])
  571. ->increment('results', 1);
  572. if(empty($choice)){
  573. return Result::error("请选择已有的选项!",0);
  574. }
  575. return Result::success($choice);
  576. }
  577. return Result::error("参数必填!");
  578. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  579. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  580. // }
  581. }
  582. return Result::error("此调查问卷不存在",0);
  583. }
  584. return Result::error("参数必填!");
  585. }
  586. }