NewsService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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\WebsiteCategory;
  7. use Hyperf\DbConnection\Db;
  8. use Hyperf\RpcServer\Annotation\RpcService;
  9. use App\Tools\Result;
  10. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  11. class NewsService implements NewsServiceInterface
  12. {
  13. /**
  14. * 获取导航池列表
  15. * @param array $data
  16. * @return array
  17. */
  18. public function getCategoryList(array $data): array
  19. {
  20. $where = [];
  21. if (isset($data['name']) && $data['name']) {
  22. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  23. }
  24. if (isset($data['department_id']) && $data['department_id']) {
  25. array_push($where, ['category.department_id', '=', $data['department_id']]);
  26. }
  27. $city_id = '';
  28. if (isset($data['city_id']) && $data['city_id']) {
  29. $city_id = intval($data['city_id']);
  30. }
  31. $rep = Category::where($where)
  32. ->when($city_id, function ($query) use ($city_id) {
  33. if (isset($city_id) && $city_id) {
  34. $query->whereJsonContains("category.city_arr_id", $city_id);
  35. }
  36. })
  37. ->leftJoin('district', 'category.city_id', 'district.id')
  38. ->leftJoin('department', 'category.department_id', 'department.id')
  39. ->select("category.*", "district.name as city_name", "department.name as department_name")
  40. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page'] - 1) * $data['pageSize'])->get();
  41. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  42. if (isset($city_id) && $city_id) {
  43. $query->whereJsonContains("category.city_arr_id", $city_id);
  44. }
  45. })->count();
  46. $data = [
  47. 'rows' => $rep->toArray(),
  48. 'count' => $count,
  49. ];
  50. if (empty($rep->toArray())) {
  51. return Result::error("没有导航池数据");
  52. }
  53. return Result::success($data);
  54. }
  55. public function myCategoryList(array $data): array
  56. {
  57. $sszq = $data['sszq'] ?? '';
  58. unset($data['sszq']);
  59. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  60. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  61. $where[] = [
  62. 'pid', '=', $data['pid'],
  63. ];
  64. if (isset($data['name'])) {
  65. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  66. }
  67. var_dump($where);
  68. //根据分类id,从category表中取出分类信息
  69. $result = Category::where($where)
  70. ->whereIn('category.id', $catorytids)
  71. ->select('category.*', 'category.id as category_id')->get();
  72. if (empty($result)) {
  73. return Result::error("没有栏目数据");
  74. }
  75. return Result::success($result);
  76. }
  77. /**
  78. * @param array $data
  79. * @return array
  80. */
  81. public function categoryList(array $data): array
  82. {
  83. $where[] = [
  84. 'pid', '=', $data['pid'],
  85. ];
  86. if (isset($data['name'])) {
  87. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  88. }
  89. var_dump($where);
  90. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  91. if (empty($result)) {
  92. return Result::error("没有栏目数据");
  93. }
  94. return Result::success($result);
  95. }
  96. /**
  97. * @param array $data
  98. * @return array
  99. */
  100. public function addCategory(array $data): array
  101. {
  102. $id = Category::insertGetId($data);
  103. if (empty($id)) {
  104. return Result::error("添加失败");
  105. }
  106. return Result::success(['id' => $id]);
  107. }
  108. /**
  109. * @param array $data
  110. * @return array
  111. */
  112. public function delCategory(array $data): array
  113. {
  114. $categoryList = Category::where(['pid' => $data['id']])->get();
  115. var_dump("分类列表:", $data, $categoryList);
  116. if ($categoryList->toArray()) {
  117. return Result::error("分类下面有子分类不能删除");
  118. }
  119. $articleList = Article::where(['catid' => $data['id']])->get();
  120. var_dump("文章列表:", $articleList);
  121. if ($articleList->toArray()) {
  122. return Result::error("分类下面有资讯不能删除");
  123. }
  124. $result = Category::where($data)->delete();
  125. if (!$result) {
  126. return Result::error("删除失败");
  127. }
  128. return Result::success($result);
  129. }
  130. /**
  131. * @param array $data
  132. * @return array
  133. */
  134. public function updateCategory(array $data): array
  135. {
  136. $where = [
  137. 'id' => $data['id'],
  138. ];
  139. $result = Category::where($where)->update($data);
  140. if ($result) {
  141. return Result::success($result);
  142. } else {
  143. return Result::error("更新失败");
  144. }
  145. }
  146. /**
  147. * 获取导航池信息
  148. * @param array $data
  149. * @return array
  150. */
  151. public function getCategoryInfo(array $data): array
  152. {
  153. $where = [
  154. 'id' => $data['id'],
  155. ];
  156. $result = Category::where($where)->first();
  157. if ($result) {
  158. return Result::success($result);
  159. } else {
  160. return Result::error("更新失败");
  161. }
  162. }
  163. /**
  164. * @param array $data
  165. * @return array
  166. */
  167. public function getArticleList(array $data): array
  168. {
  169. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  170. $type_id = $data['type_id'];
  171. unset($data['type_id']);
  172. $user_id = $data['user_id'];
  173. unset($data['user_id']);
  174. $where = [];
  175. if (isset($data['title']) && $data['title']) {
  176. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  177. }
  178. if (isset($data['category_name']) && $data['category_name']) {
  179. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  180. }
  181. if (isset($data['author']) && $data['author']) {
  182. array_push($where, ['article.author', '=', $data['author']]);
  183. }
  184. if (isset($data['islink']) && $data['islink'] !== "") {
  185. array_push($where, ['article.islink', '=', $data['islink']]);
  186. }
  187. if (isset($data['status']) && $data['status'] !== "") {
  188. array_push($where, ['article.status', '=', $data['status']]);
  189. }
  190. //不是管理员展示个人数据;
  191. if ($type_id != 10000) {
  192. $where[] = ['article.admin_user_id', '=', $user_id];
  193. }
  194. $rep = Article::where($where)
  195. ->whereNotIn('article.status', [404])
  196. ->leftJoin('category', 'article.catid', 'category.id')
  197. ->select("article.*", "category.name as category_name")
  198. ->orderBy("article.id", "desc")
  199. ->limit($data['pageSize'])
  200. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  201. $count = Article::where($where)->whereNotIn('article.status', [404])
  202. ->leftJoin('category', 'article.catid', 'category.id')->count();
  203. $data = [
  204. 'rows' => $rep->toArray(),
  205. 'count' => $count,
  206. ];
  207. if (empty($rep)) {
  208. return Result::error("没有信息数据");
  209. }
  210. return Result::success($data);
  211. }
  212. /**
  213. * @param array $data
  214. * @return array
  215. */
  216. public function addArticle(array $data): array
  217. {
  218. Db::beginTransaction();
  219. try {
  220. $articleData = $data;
  221. unset($articleData['content']);
  222. $id = Article::insertGetId($articleData);
  223. $articleDataContent = [
  224. 'article_id' => $id,
  225. 'content' => $data['content'],
  226. ];
  227. ArticleData::insertGetId($articleDataContent);
  228. Db::commit();
  229. } catch (\Throwable $ex) {
  230. Db::rollBack();
  231. var_dump($ex->getMessage());
  232. return Result::error("创建失败", 0);
  233. }
  234. return Result::success(['id' => $id]);
  235. }
  236. /**
  237. * @param array $data
  238. * @return array
  239. */
  240. public function delArticle(array $data): array
  241. {
  242. $result = Article::where($data)->delete();
  243. if (!$result) {
  244. return Result::error("删除失败");
  245. }
  246. return Result::success($result);
  247. }
  248. /**
  249. * @param array $data
  250. * @return array
  251. */
  252. public function updateArticle(array $data): array
  253. {
  254. Db::beginTransaction();
  255. try {
  256. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  257. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  258. $articleData = $data;
  259. unset($articleData['content']);
  260. unset($articleData['status_name']);
  261. unset($articleData['name']);
  262. unset($articleData['content']);
  263. unset($articleData['pid_arr']);
  264. unset($articleData['pid']);
  265. $id = Article::where(['id' => $data['id']])->update($articleData);
  266. $articleDataContent = [
  267. 'content' => $data['content'],
  268. ];
  269. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  270. } catch (\Throwable $ex) {
  271. Db::rollBack();
  272. var_dump($ex->getMessage());
  273. return Result::error("更新失败", 0);
  274. }
  275. return Result::success([]);
  276. }
  277. /**
  278. * 更新资讯状态
  279. * @param array $data
  280. * @return array
  281. */
  282. public function upArticleStatus(array $data): array
  283. {
  284. $result = Article::where(['id' => $data['id']])->update($data);
  285. if ($result) {
  286. return Result::success();
  287. } else {
  288. return Result::error("更新状态失败", 0);
  289. }
  290. }
  291. /**
  292. * @param array $data
  293. * @return array
  294. */
  295. public function getArticleInfo(array $data): array
  296. {
  297. $where = [
  298. 'article.id' => $data['id'],
  299. ];
  300. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  301. if ($result) {
  302. return Result::success($result->toArray());
  303. } else {
  304. return Result::error("查询失败", 0);
  305. }
  306. }
  307. /**
  308. * 获取新闻
  309. * @param array $data
  310. * @return array
  311. */
  312. public function getWebsiteArticlett(array $data): array
  313. {
  314. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  315. $category = $category->toArray();
  316. $result = [];
  317. if ($category) {
  318. $category_ids = [];
  319. foreach ($category as $val) {
  320. array_push($category_ids, $val['category_id']);
  321. }
  322. if (isset($data['placeid'])) {
  323. $placeid = $data['placeid'] - 1;
  324. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  325. } else {
  326. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  327. }
  328. if (empty($result)) {
  329. return Result::error("暂无头条新闻", 0);
  330. }
  331. return Result::success($result);
  332. } else {
  333. return Result::error("本网站下暂无相关栏目", 0);
  334. }
  335. }
  336. /**
  337. * 获取模块新闻
  338. * @param array $data
  339. * @return array
  340. */
  341. public function getWebsiteModelArticles(array $data): array
  342. {
  343. $catid = $data['catid'];
  344. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  345. $category = $category->toArray();
  346. if (!empty($category)) {
  347. $where = [
  348. 'status' => 1,
  349. 'catid' => $catid,
  350. ];
  351. if ($data['level'] == 1) {
  352. $level = [
  353. 0 => '1',
  354. 1 => '4',
  355. 2 => '5',
  356. 3 => '0',
  357. ];
  358. $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  359. } elseif ($data['level'] == 2) {
  360. $level = '2';
  361. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  362. } else {
  363. $level = '3';
  364. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  365. }
  366. $result = $result->toArray();
  367. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  368. $placeid = $data['placeid'] - 1;
  369. if ($level == 2 || $level == 3) {
  370. $where = [
  371. 'level' => $level,
  372. ];
  373. $result = Article::where($where)
  374. ->orderBy("created_at", "desc")
  375. ->offset($placeid)
  376. ->limit($data['pagesize'])->get();
  377. } else {
  378. $result = Article::where($where)
  379. ->whereIn('level', $level)
  380. ->offset($placeid)
  381. ->orderBy("created_at", "desc")
  382. ->limit($data['pagesize'])->get();
  383. }
  384. }
  385. if (empty($result)) {
  386. return Result::error("此栏目暂无相关新闻", 0);
  387. }
  388. } else {
  389. return Result::error("此网站暂无此栏目", 0);
  390. }
  391. return Result::success($result);
  392. }
  393. /**
  394. * 获取新闻列表
  395. * @param array $data
  396. * @return array
  397. */
  398. public function getWebsiteArticleList(array $data): array
  399. {
  400. $where[] = ['status', '=', 1];
  401. if(isset($data['keyword']) && !empty($data['keyword'])){
  402. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  403. }
  404. if(isset($data['catid']) && !empty($data['catid'])){
  405. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->select('category_id')->first();
  406. if(empty($category)){
  407. return Result::error("此网站暂无此栏目",0);
  408. }
  409. array_push($where,['catid',$data['catid']]);
  410. }
  411. $rep = Article::where($where)
  412. ->orderBy("created_at","desc")
  413. ->limit($data['pageSize'])
  414. ->offset(($data['page']-1)*$data['pageSize'])->get();
  415. $count = Article::where($where)->count();
  416. $data = [
  417. 'rows'=>$rep->toArray(),
  418. 'count'=>$count
  419. ];
  420. if(empty($rep)){
  421. return Result::error("没有信息数据");
  422. }
  423. return Result::success($data);
  424. }
  425. /**
  426. * 验证导航名称是否重复
  427. * @return void
  428. */
  429. public function checkCategoryName(array $data): array
  430. {
  431. $result = Category::when($data, function ($query) use ($data) {
  432. if(isset($data['name']) && $data['name']) {
  433. $query->where("name", $data['name']);
  434. }
  435. if(isset($data['id']) && $data['id']) {
  436. $query->where("id","!=" ,$data['id']);
  437. }
  438. })->first();
  439. if($result){
  440. return Result::error("已存在");
  441. }else{
  442. return Result::success();
  443. }
  444. }
  445. }