NewsService.php 18 KB

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