CollectorService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\OldModel\Article as OldArticle;
  4. use App\Model\OldModel\ArticleData as OldArticleData;
  5. use App\Model\OldModel\Category;
  6. use App\Model\Article;
  7. use App\Model\Web;
  8. use App\Model\Rule;
  9. use App\Model\ArticleData;
  10. use Hyperf\DbConnection\Db;
  11. use Hyperf\RpcServer\Annotation\RpcService;
  12. use App\Tools\Result;
  13. use function Hyperf\Support\retry;
  14. #[RpcService(name: "CollectorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  15. class CollectorService implements CollectorServiceInterface
  16. {
  17. /**
  18. * 添加网站
  19. * @param array $data
  20. * @return array|mixed
  21. */
  22. public function addWeb(array $data): array
  23. {
  24. $where = [
  25. 'name' => $data['name']
  26. ];
  27. $isweb = Web::where($where)->first();
  28. if(empty($isweb)){
  29. date_default_timezone_set('Asia/Shanghai');
  30. $web = Web::insert($data);
  31. }else{
  32. return Result::error('此网站已存在,不可重复添加!');
  33. }
  34. if(empty($web)){
  35. return Result::error('添加失败');
  36. }
  37. return Result::success('添加成功');
  38. }
  39. /**
  40. * 获取并搜索网站
  41. * @param array $data
  42. * @return array|mixed
  43. */
  44. public function getWeb(array $data): array
  45. {
  46. if(isset($data['keyWord'])){
  47. $where = [
  48. ['name','like','%'.$data['keyWord'].'%']
  49. ];
  50. $rep = Web::where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  51. $count = Web::where($where)->count();
  52. if($count==0){
  53. return Result::error('未查找到相关网站!');
  54. }
  55. }else{
  56. $rep = Web::limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  57. $count = Web::count();
  58. }
  59. $data = [
  60. 'rep' => $rep->toArray(),
  61. 'count' => $count
  62. ];
  63. if(empty($rep)){
  64. return Result::error('您还未添加网站,请先去添加!');
  65. }
  66. return Result::success($data);
  67. }
  68. /**
  69. * 修改网站
  70. * @param array $data
  71. * @return array|mixed
  72. */
  73. public function upWeb(array $data): array
  74. {
  75. $web = Web::where('id',$data['id'])->first();
  76. if(empty($web)){
  77. return Result::error('请输入正确的网站id!');
  78. }else{
  79. date_default_timezone_set('Asia/Shanghai');
  80. $id = Web::where('id',$data['id'])->update($data);
  81. if(empty($id)){
  82. return Result::error('无法修改!');
  83. }
  84. }
  85. return Result::success($id);
  86. }
  87. /**
  88. * 删除网站
  89. * @param array $data
  90. * @return array|mixed
  91. */
  92. public function delWeb(array $data): array
  93. {
  94. $web = Web::where('id',$data['id'])->first();
  95. if(empty($web)){
  96. return Result::error('请输入正确的网站id!');
  97. }else{
  98. $where = [
  99. ['web_id','=',$data['id']]
  100. ];
  101. //判断此网站下是否规则任务
  102. $rule = Rule::where($where)->get();
  103. if(empty($rule)){
  104. //若没有直接删除网站
  105. $result['web'] = Web::where('id',$data['id'])->delete();
  106. }else{
  107. //若有,判断规则任务是否有已执行的
  108. $rule = Rule::where($where)->where('status',2)->get();
  109. // return Result::success($rule);
  110. if(!empty($rule->toArray())){
  111. //若有已执行的任务规则,不可删除网站
  112. return Result::error('该网站已有成功执行的任务规则,不可删除!');
  113. }else{
  114. try {
  115. Db::beginTransaction();
  116. //若无已执行的任务规则,删除网站及相应的未执行的规则任务
  117. $result['web'] = Web::where('id',$data['id'])->delete();
  118. $result['rule'] = Rule::where($where)->delete();
  119. Db::commit();
  120. } catch(\Throwable $ex){
  121. Db::rollBack();
  122. var_dump($ex->getMessage());
  123. return Result::error("删除失败",0);
  124. }
  125. }
  126. }
  127. }
  128. return Result::success($result);
  129. }
  130. /**
  131. * 添加任务规则
  132. * @param array $data
  133. * @return array|mixed
  134. */
  135. public function addRule(array $data): array
  136. {
  137. $web = Web::where('id',$data['web_id'])->get();
  138. if(empty($web->toArray())){
  139. return Result::error('请输入正确的网站id!');
  140. }else{
  141. $rulename = Rule::where('name',$data['name'])->get();
  142. //查找是否存在规则名称重复的
  143. if(empty($rulename->toArray())){
  144. //(若是多类型参数一起传过来则根据类型,只获取对应类型需要的参数)
  145. switch($data['type']){
  146. case 1:
  147. $rule = [
  148. 'name' => $data['name'],
  149. 'web_id' => $data['web_id'],
  150. 'first_url' => $data['first_url'],
  151. 'second_start' => $data['second_start'],
  152. 'second_num' => $data['second_num'],
  153. 'second_end' => $data['second_end'],
  154. 'end_pagenum' => $data['end_pagenum'],
  155. 'start' => $data['start'],
  156. 'title' => $data['title'],
  157. 'content' => $data['content']
  158. ];
  159. // var_dump("============1============");
  160. break;
  161. case 2:
  162. $rule = [
  163. 'name' => $data['name'],
  164. 'web_id' => $data['web_id'],
  165. 'first_url' => $data['first_url'],
  166. 'parameter' => $data['parameter'],
  167. 'start' => $data['start'],
  168. 'title' => $data['title'],
  169. 'content' => $data['content']
  170. ];
  171. // var_dump("============2============");
  172. break;
  173. default:
  174. $rule = [
  175. 'name' => $data['name'],
  176. 'web_id' => $data['web_id'],
  177. 'diy_rule' => $data['diy_rule']
  178. ];
  179. // var_dump("============3============");
  180. break;
  181. }
  182. if(isset($data['source']) && $data['type'] != 3){
  183. $rule ['source'] = $data['source'];
  184. }
  185. if(isset($data['writer_class']) && $data['type'] != 3){
  186. $rule ['writer_class'] = $data['writer_class'];
  187. }
  188. if(isset($data['writer']) && $data['type'] != 3){
  189. $rule ['writer'] = $data['writer'];
  190. }
  191. date_default_timezone_set('Asia/Shanghai');
  192. //若不存在,根据网站类型添加到不行类型的规则表中
  193. $result = Rule::insertGetId($rule);
  194. }else{
  195. return Result::error('此任务已存在!');
  196. }
  197. }
  198. return Result::success($result);
  199. }
  200. /**
  201. * 获取并搜索规则任务
  202. * @param array $data
  203. * @return array|mixed
  204. */
  205. public function getRule(array $data): array
  206. {
  207. if(isset($data['web_id'])){
  208. $web = Web::where('id',$data['web_id'])->get();
  209. if(empty($web->toArray())){
  210. return Result::error('请输入正确的网站id!');
  211. }else{
  212. $where = [
  213. ['web_id','=', $data['web_id']]
  214. ];
  215. }
  216. }
  217. if(isset($data['keyWord'])){
  218. //若存在搜索词,则存到条件数组$where中
  219. $where = [
  220. ['name','like','%'.$data['keyWord'].'%']
  221. ];
  222. }
  223. $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  224. $count = Rule::where($where)->count();
  225. if($count==0){
  226. return Result::error('暂无相关规则任务!');
  227. }
  228. $data = [
  229. 'rep' => $rep->toArray(),
  230. 'count' => $count
  231. ];
  232. return Result::success($data);
  233. }
  234. /**
  235. * 获取某个任务规则
  236. * @param array $data
  237. * @return array|mixed
  238. */
  239. public function getOneRule(array $data): array
  240. {
  241. $result = Rule::where('id',$data['id'])->first();
  242. if(empty($result)){
  243. return Result::error('请输入正确的规则任务id!');
  244. }else{
  245. return Result::success($result);
  246. }
  247. }
  248. /**
  249. * 修改规则任务
  250. * @param array $data
  251. * @return array|mixed
  252. */
  253. public function upRule(array $data): array
  254. {
  255. $rule = Rule::where('id',$data['id'])->select('id')->first();
  256. unset($data['type']);
  257. if(empty($rule)){
  258. return Result::error('请输入正确的规则任务id!');
  259. }else{
  260. $rulename = Rule::where('id','!=',$rule['id'])->where('name',$data['name'])->select('name')->first();
  261. if(empty($rulename)){
  262. $result = Rule::where('id',$data['id'])->update($data);
  263. }else{
  264. return Result::error('已存在此任务规则名称!');
  265. }
  266. }
  267. return Result::success($result);
  268. }
  269. /**
  270. * 删除规则任务
  271. * @param array $data
  272. * @return array
  273. */
  274. public function delRule(array $data): array
  275. {
  276. $where = ['id' => $data['rule_id']];
  277. $rule = Rule::where($where)->first();
  278. if(empty($rule)){
  279. return Result::error('请输入正确的规则任务id!');
  280. }else{
  281. //查找是否存在已导入的文章
  282. $art_num = Article::where('rule_id',$data['rule_id'])->where('state',1)->count();
  283. if($art_num==0){
  284. //查找是否存在已采集但是未导入的文章
  285. $colart_num = Article::where('rule_id',$data['rule_id'])->where('state',0)->count();
  286. if($colart_num==0){
  287. $result['rule'] = Rule::where($where)->delete();
  288. }else{
  289. try {
  290. Db::beginTransaction();
  291. //若有已采集但未导入的文章,删除规则任务及相应的未导入的文章
  292. $result['rule'] = Rule::where($where)->delete();
  293. $result['art'] = Article::where('rule_id',$data['rule_id'])->delete();
  294. Db::commit();
  295. } catch(\Throwable $ex){
  296. Db::rollBack();
  297. var_dump($ex->getMessage());
  298. return Result::error("删除失败",0);
  299. }
  300. }
  301. }else{
  302. return Result::error('此规则任务下的文章已导入,不可删除!');
  303. }
  304. }
  305. return Result::success($result);
  306. }
  307. /**
  308. * 开始采集
  309. * @param array $data
  310. * @return array
  311. */
  312. public function sendCrawler(array $data): array
  313. {
  314. $result = Article::get();
  315. $b = OldArticle::get();
  316. $a = [
  317. 'old'=>$b,
  318. 'new'=>$result
  319. ];
  320. return Result::success($a);
  321. }
  322. /**
  323. * 获取并搜索资讯
  324. * @param array $data
  325. * @return array
  326. */
  327. public function getInfo(array $data): array
  328. {
  329. $where = [
  330. ['rule_id','=',$data['rule_id']]
  331. ];
  332. //若存在条件参数都存到where数组
  333. if(isset($data['title'])){
  334. $where[] = ['title','like','%'.$data['title'].'%'];
  335. }
  336. if(isset($data['source'])){
  337. $art_source = Article::where($where)->get();
  338. if(!empty($art_source->toArray())){
  339. $where[] = ['source','=',$data['source']];
  340. }
  341. }
  342. if(isset($data['state'])){
  343. $where[] = ['state','=',$data['state']];
  344. }
  345. //跨库查询栏目导航及采集的新闻
  346. $info = Article::query()
  347. ->where($where)
  348. ->with(['category' => function ($query) {
  349. $query->select('name');
  350. }])
  351. ->orderBy("article.id","desc")
  352. ->limit($data['pageSize'])
  353. ->offset(($data['page']-1)*$data['pageSize'])->get();
  354. $count = Article::where($where)->count();
  355. if($count == 0){
  356. return Result::error('暂无资讯');
  357. }
  358. $data = [
  359. 'rep' => $info->toArray(),
  360. 'count' => $count
  361. ];
  362. return Result::success($data);
  363. }
  364. /**
  365. * 获取某个资讯
  366. * @param array $data
  367. * @return array
  368. */
  369. public function getOneInfo(array $data): array
  370. {
  371. $where = ['id' => $data['art_id']];
  372. $inf = Article::where($where)->first();
  373. if($inf==null){
  374. return Result::error('请输入正确的资讯id!');
  375. }
  376. $info = Article::where($where)
  377. ->leftJoin('article_data','article_id','id')
  378. ->select('article.*','article_data.content')
  379. ->first();
  380. if($inf['catid']!=null){
  381. $category = Category::where(['id'=>$info['catid']])->select('name')->first();
  382. $info['category'] = $category['name'];
  383. }
  384. return Result::success($info);
  385. }
  386. /**
  387. * 修改资讯
  388. * @param array $data
  389. * @return array
  390. */
  391. public function upInfo(array $data): array
  392. {
  393. $id = $data['art_id'];
  394. $content = $data['content'];
  395. unset($data['art_id']);
  396. //去掉此元素
  397. unset($data['content']);
  398. //去掉此元素
  399. $info = Article::where('id',$id)->first();
  400. if($info==null){
  401. return Result::error('请输入正确的文章id!');
  402. }
  403. if($info['state']==1){
  404. return Result::error('此文章已导入 ,不可编辑!');
  405. }else{
  406. Db::beginTransaction();
  407. try{
  408. $info = Article::where('id',$id)->update($data);
  409. $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);
  410. Db::commit();
  411. } catch(\Throwable $ex){
  412. Db::rollBack();
  413. var_dump($ex->getMessage());
  414. return Result::error("修改失败",0);
  415. }
  416. $data = [
  417. 'info' => $info,
  418. 'art_data' => $art_data
  419. ];
  420. return Result::success($data);
  421. }
  422. }
  423. /**
  424. * 删除资讯
  425. * @param array $data
  426. * @return array
  427. */
  428. public function delInfo(array $data): array
  429. {
  430. $id = $data['art_id'];
  431. $info = Article::where('id',$id)->first();
  432. if($info==null){
  433. return Result::error('请输入正确的文章id!');
  434. }
  435. if($info['state']==1){
  436. return Result::error('此文章已导入,不可删除!');
  437. }else{
  438. Db::beginTransaction();
  439. try{
  440. $delinfo = Article::where('id',$id)->delete();
  441. $deldata = ArticleData::where('article_id',$id)->delete();
  442. Db::commit();
  443. } catch(\Throwable $ex){
  444. Db::rollBack();
  445. var_dump($ex->getMessage());
  446. return Result::error("删除失败",0);
  447. }
  448. }
  449. $data = [
  450. 'delinfo' => $delinfo,
  451. 'deldata' => $deldata
  452. ];
  453. return Result::success($data);
  454. }
  455. /**
  456. * 关联导航池
  457. * @param array $data
  458. * @return array
  459. */
  460. public function addCatid(array $data): array
  461. {
  462. $id = $data['rule_id'];
  463. //查找此规则任务下的文章是否都已经导入
  464. $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
  465. if(empty($info->toArray())){
  466. return Result::error('所有文章都已导入,不可修改关联的导航池!');
  467. }else{
  468. //查找此规则任务下的文章是否已经有导入的文章
  469. $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
  470. if(!empty($article->toArray())){
  471. //若有已导入的文章则直接复制之前已导入的导航池
  472. $catid = Article::whereIn('id',$article)->select('catid')->first();
  473. //若未导入的文章已经复制之前的导航,则无需修改
  474. $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
  475. if($art_catid>0){
  476. $result = Article::whereIn('id',$info)->update(['catid'=>$catid['catid']]);
  477. }else{
  478. $result = ['已全部关联导航,无需再次关联!'];
  479. }
  480. }else{
  481. //若不存在已导入的文章则判断是否存在导航id
  482. if(isset($data['catid'])){
  483. //若存在直接使用此导航id
  484. $result = Article::whereIn('id',$info)->update(['catid'=>$data['catid']]);
  485. }else{
  486. //若不存在则返回所有导航栏目
  487. $result = Category::select('id','name')->get();
  488. }
  489. }
  490. }
  491. return Result::success($result);
  492. }
  493. /**
  494. * 导入文章
  495. * @param array $data
  496. * @return array
  497. */
  498. public function addArt(array $data): array
  499. {
  500. // var_dump("======@@@====");
  501. $where = [
  502. 'rule_id' => $data['rule_id'],
  503. 'state' => 0
  504. ];
  505. //获取某个规则任务下的已采集未导入的文章及文章详情
  506. $arts_id = Article::where($where)->wherenotNull('catid')->orderBy('id')->select('id')->get();
  507. $arts = Article::where($where)->wherenotNull('catid')->select('title','catid','level','introduce','keyword','author','copyfrom','fromurl','hits','islink','imgurl','admin_user_id','is_original')->orderBy('id')->get()->toArray();
  508. $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id')->get()->toArray();
  509. // var_dump($article_data);
  510. $data = [
  511. 'articles' => $arts,
  512. 'art_content' => $arts_data
  513. ];
  514. Db::beginTransaction();
  515. try{
  516. $oldart = OldArticle::insert($arts);
  517. $oldart_data = OldArticleData::insert($arts_data);
  518. $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
  519. Db::commit();
  520. } catch(\Throwable $ex){
  521. Db::rollBack();
  522. var_dump($ex->getMessage());
  523. return Result::error($ex->getMessage(),0);
  524. }
  525. return Result::success($data);
  526. }
  527. }