NewsService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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'])->select('category_id')->get();
  277. $category = $category->toArray();
  278. $result= [];
  279. if($category){
  280. $category_ids = [];
  281. foreach($category as $val){
  282. array_push($category_ids,$val['category_id']);
  283. }
  284. if(isset($data['placeid'])){
  285. $placeid=$data['placeid']-1;
  286. $result=Article::where('status',1)->where('level',$data['level'])->whereIn("catid",$category_ids)->orderBy("created_at","desc")->offset($placeid)->limit($data['pageSize'])->get();
  287. }else{
  288. $result=Article::where('status',1)->where('level',$data['level'])->whereIn("catid",$category_ids)->orderBy("created_at","desc")->offset(0)->limit($data['pageSize'])->get();
  289. }
  290. if(empty($result)){
  291. return Result::error("暂无头条新闻",0);
  292. }
  293. return Result::success($result);
  294. }else{
  295. return Result::error("本网站下暂无相关栏目",0);
  296. }
  297. }
  298. /**
  299. * 获取模块新闻
  300. * @param array $data
  301. * @return array
  302. */
  303. public function getWebsiteModelArticles(array $data): array
  304. {
  305. $catid=$data['catid'];
  306. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$catid)->select('category_id')->get();
  307. $category = $category->toArray();
  308. if(!empty($category)){
  309. $where=[
  310. 'status' => 1,
  311. 'catid' => $catid
  312. ];
  313. if ($data['level'] == 1) {
  314. $level = [
  315. 0 => '1',
  316. 1 => '4',
  317. 2 => '5',
  318. 3 => '0',
  319. ];
  320. $result = Article::where($where)->whereIn('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
  321. }elseif($data['level']==2){
  322. $level='2';
  323. $result = Article::where($where)->where('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
  324. }else{
  325. $level='3';
  326. $result = Article::where($where)->where('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
  327. }
  328. $result= $result->toArray();
  329. if(!empty($result) && isset($data['placeid']) && !empty($data['placeid'])){
  330. $placeid=$data['placeid']-1;
  331. if($level==2 || $level==3){
  332. $where =[
  333. 'level' => $level
  334. ];
  335. $result = Article::where($where)
  336. ->orderBy("created_at","desc")
  337. ->offset($placeid)
  338. ->limit($data['pagesize'])->get();
  339. }else{
  340. $result = Article::where($where)
  341. ->whereIn('level',$level)
  342. ->offset($placeid)
  343. ->orderBy("created_at","desc")
  344. ->limit($data['pagesize'])->get();
  345. }
  346. }
  347. if(empty($result)){
  348. return Result::error("此栏目暂无相关新闻",0);
  349. }
  350. }else{
  351. return Result::error("此网站暂无此栏目",0);
  352. }
  353. return Result::success($result);
  354. }
  355. /**
  356. *获取新闻列表
  357. * @param array $data
  358. * @return array
  359. */
  360. public function getWebsiteArticleList(array $data): array
  361. {
  362. $where[] = ['status', '=', 1];
  363. if(isset($data['keyword']) && !empty($data['keyword'])){
  364. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  365. }
  366. if(isset($data['catid']) && !empty($data['catid'])){
  367. if(is_array($data['catid'])){
  368. $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
  369. $where[] = ['catid', 'in', $data['catid']];
  370. }else{
  371. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
  372. $where[] = ['catid', '=', $data['catid']];
  373. }
  374. if(empty($category)){
  375. return Result::error("此网站暂无此栏目",0);
  376. }
  377. }
  378. // return Result::success($category);
  379. $rep = Article::where(function ($query) use ($where) {
  380. foreach ($where as $condition) {
  381. if ($condition[1] === 'in') {
  382. $query->whereIn($condition[0], $condition[2]);
  383. } else {
  384. $query->where($condition[0], $condition[1], $condition[2]);
  385. }
  386. }
  387. })
  388. ->orderBy("created_at", "desc")
  389. ->limit($data['pageSize'])
  390. ->offset(($data['page'] - 1) * $data['pageSize'])
  391. ->get();
  392. $count = Article::where(function ($query) use ($where) {
  393. foreach ($where as $condition) {
  394. if ($condition[1] === 'in') {
  395. $query->whereIn($condition[0], $condition[2]);
  396. } else {
  397. $query->where($condition[0], $condition[1], $condition[2]);
  398. }
  399. }
  400. })->count();
  401. $data = [
  402. 'rows'=>$rep->toArray(),
  403. 'count'=>$count
  404. ];
  405. if(empty($rep)){
  406. return Result::error("没有信息数据");
  407. }
  408. return Result::success($data);
  409. }
  410. /**
  411. * 前端-获取新闻详情
  412. * @param array $data
  413. * @return array
  414. */
  415. public function selectWebsiteArticleInfo(array $data): array
  416. {
  417. $where = [
  418. 'article.id'=>$data['id'],
  419. 'article.status'=>1
  420. ];
  421. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  422. if(empty($result)){
  423. return Result::error("查询失败",0);
  424. }
  425. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  426. if(empty($category)){
  427. return Result::error("查询失败",0);
  428. }
  429. $result['category_id'] = $category['category_id'];
  430. $result['cat_name'] = $category['name'];
  431. return Result::success($result);
  432. }
  433. /**
  434. * 前端-获取网站调查问卷
  435. * @param array $data
  436. * @return array
  437. */
  438. public function getWebsiteSurvey(array $data): array
  439. {
  440. if(isset($data['survey_id']) && !empty($data['survey_id'])){
  441. $website = Website::where('id',$data['website_id'])->first();
  442. if(empty($website)){
  443. return Result::error("暂无此网站",0);
  444. }
  445. }
  446. if(isset($data['art_id']) && !empty($data['art_id'])){
  447. $article = Article::where('id',$data['art_id'])->first();
  448. if(empty($article)){
  449. return Result::error("暂无此文章",0);
  450. }
  451. // return Result::error($data,0);
  452. $where['art_id'] = $data['art_id'];
  453. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  454. }else{
  455. $survey = ArticleSurvey::where('website_id',$data['website_id'])->orderBy('created_at')->first();
  456. if(empty($survey)){
  457. return Result::error("暂无调查问卷",0);
  458. }
  459. $where['sur_id'] = $survey['sur_id'];
  460. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  461. }
  462. $result['survey'] = ArticleSurvey::where($where)->where('is_other',0)
  463. ->leftJoin('article','article_survey.art_id','article.id')
  464. ->select('article_survey.*','article.survey_type')
  465. ->get()->all();
  466. $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  467. $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
  468. if(empty($result)){
  469. return Result::error("此文章暂无调查问卷",0);
  470. }
  471. return Result::success($result);
  472. }
  473. /**
  474. * 前端-添加网站调查问卷选项
  475. * @param array $data
  476. * @return array
  477. */
  478. public function addWebsiteSurveyOption(array $data): array
  479. {
  480. if(isset($data['website_id']) && !empty($data['website_id'])){
  481. $website = Website::where('id',$data['website_id'])->first();
  482. if(empty($website)){
  483. return Result::error("暂无此网站",0);
  484. }
  485. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  486. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  487. if(empty($survey)){
  488. return Result::error("此调查问卷不可添加选项",0);
  489. }
  490. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  491. $choice = [
  492. 'art_id'=>$survey['art_id'],
  493. 'website_id'=>$data['website_id'],
  494. 'survey_name'=>$survey['survey_name'],
  495. 'choice_name'=>$data['choice_name'],
  496. 'sur_id'=>$survey['sur_id'],
  497. 'is_other'=>1,
  498. 'other_id'=>$survey['id'],
  499. ];
  500. $result = ArticleSurvey::insertGetId($choice);
  501. if(empty($result)){
  502. return Result::error("添加失败",0);
  503. }
  504. return Result::success($result);
  505. }
  506. }
  507. return Result::error("添加失败",0);
  508. }
  509. return Result::error("添加失败",0);
  510. }
  511. /**
  512. * 前端-调查问卷投票 --------未完待续-------
  513. * @param array $data
  514. * @return array
  515. */
  516. public function addWebsiteSurveyVote(array $data): array
  517. {
  518. if(isset($data['website_id']) &&!empty($data['website_id'])){
  519. $website = Website::where('id',$data['website_id'])->first();
  520. if(empty($website)){
  521. return Result::error("暂无此网站",0);
  522. }
  523. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  524. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->pluck('sur_id');
  525. if(empty($survey)){
  526. return Result::error("此调查问卷不存在",0);
  527. }
  528. // 调查问卷类型 0:单选 1:多选
  529. $type = Article::where('survey_id',$data['sur_id'])->pluck('survey_type');
  530. // return Result::success($type);
  531. if(empty($type) || (!in_array($type, [0, 1], true))){
  532. return Result::error("此调查问卷不可投票",0);
  533. }
  534. return Result::success($type[0]);
  535. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  536. if($type[0] == 0){
  537. $choice = ArticleSurvey::where('id',$data['choice_id'])->where('website_id',$data['website_id'])->first();
  538. }else{
  539. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->get()->all();
  540. }
  541. if(empty($choice)){
  542. return Result::error("请选择已有的选项!",0);
  543. }
  544. return Result::success($choice);
  545. $where = [
  546. 'website_id'=>$data['website_id'],
  547. 'sur_id'=>$data['sur_id'],
  548. 'choice_id'=>$data['choice_id']
  549. ];
  550. }
  551. return Result::success("111");
  552. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  553. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  554. // }
  555. }
  556. return Result::error("此调查问卷不存在",0);
  557. }
  558. return Result::success("2222");
  559. }
  560. }