CollectorService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. public function addRule(array $data): array
  131. {
  132. $web = Web::where('id',$data['web_id'])->get();
  133. if(empty($web->toArray())){
  134. return Result::error('请输入正确的网站id!');
  135. }else{
  136. $rulename = Rule::where('name',$data['name'])->get();
  137. //查找是否存在规则名称重复的
  138. if(empty($rulename->toArray())){
  139. //(若是多类型参数一起传过来则根据类型,只获取对应类型需要的参数)
  140. switch($data['type']){
  141. case 1:
  142. $rule = [
  143. 'name' => $data['name'],
  144. 'web_id' => $data['web_id'],
  145. 'first_url' => $data['first_url'],
  146. 'second_start' => $data['second_start'],
  147. 'second_num' => $data['second_num'],
  148. 'second_end' => $data['second_end'],
  149. 'end_pagenum' => $data['end_pagenum'],
  150. 'start' => $data['start'],
  151. 'title' => $data['title'],
  152. 'content' => $data['content']
  153. ];
  154. // var_dump("============1============");
  155. break;
  156. case 2:
  157. $rule = [
  158. 'name' => $data['name'],
  159. 'web_id' => $data['web_id'],
  160. 'first_url' => $data['first_url'],
  161. 'parameter' => $data['parameter'],
  162. 'start' => $data['start'],
  163. 'title' => $data['title'],
  164. 'content' => $data['content']
  165. ];
  166. // var_dump("============2============");
  167. break;
  168. default:
  169. $rule = [
  170. 'name' => $data['name'],
  171. 'web_id' => $data['web_id'],
  172. 'diy_rule' => $data['diy_rule']
  173. ];
  174. // var_dump("============3============");
  175. break;
  176. }
  177. if(isset($data['source']) && $data['type'] != 3){
  178. $rule ['source'] = $data['source'];
  179. }
  180. if(isset($data['writer_class']) && $data['type'] != 3){
  181. $rule ['writer_class'] = $data['writer_class'];
  182. }
  183. if(isset($data['writer']) && $data['type'] != 3){
  184. $rule ['writer'] = $data['writer'];
  185. }
  186. date_default_timezone_set('Asia/Shanghai');
  187. //若不存在,根据网站类型添加到不行类型的规则表中
  188. $result = Rule::insertGetId($rule);
  189. }else{
  190. return Result::error('此任务已存在!');
  191. }
  192. }
  193. return Result::success($result);
  194. }
  195. /**
  196. * 获取并搜索规则任务
  197. * @param array $data
  198. * @return array|mixed
  199. */
  200. public function getRule(array $data): array
  201. {
  202. $web = Web::where('id',$data['web_id'])->get();
  203. if(empty($web->toArray())){
  204. return Result::error('请输入正确的网站id!');
  205. }else{
  206. $where = [
  207. ['web_id','=', $data['web_id']]
  208. ];
  209. if(isset($data['keyWord'])){
  210. //若存在搜索词,则存到条件数组$where中
  211. $where = [
  212. ['name','like','%'.$data['keyWord'].'%']
  213. ];
  214. }
  215. $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  216. $count = Rule::where($where)->count();
  217. if($count==0){
  218. return Result::error('暂无相关规则任务!');
  219. }
  220. }
  221. $data = [
  222. 'rep' => $rep->toArray(),
  223. 'count' => $count
  224. ];
  225. return Result::success($data);
  226. }
  227. /**
  228. * 获取某个任务规则
  229. * @param array $data
  230. * @return array|mixed
  231. */
  232. public function getOneRule(array $data): array
  233. {
  234. $result = Rule::where('id',$data['id'])->first();
  235. if(empty($result)){
  236. return Result::error('请输入正确的规则任务id!');
  237. }else{
  238. return Result::success($result);
  239. }
  240. }
  241. /**
  242. * 修改规则任务
  243. * @param array $data
  244. * @return array|mixed
  245. */
  246. public function upRule(array $data): array
  247. {
  248. $rule = Rule::where('id',$data['id'])->select('id')->first();
  249. unset($data['type']);
  250. if(empty($rule)){
  251. return Result::error('请输入正确的规则任务id!');
  252. }else{
  253. $rulename = Rule::where('id','!=',$rule['id'])->where('name',$data['name'])->select('name')->first();
  254. if(empty($rulename)){
  255. $result = Rule::where('id',$data['id'])->update($data);
  256. }else{
  257. return Result::error('已存在此任务规则名称!');
  258. }
  259. }
  260. return Result::success($result);
  261. }
  262. /**
  263. * 删除规则任务
  264. * @param array $data
  265. * @return array
  266. */
  267. public function delRule(array $data): array
  268. {
  269. $where = ['id' => $data['rule_id']];
  270. $rule = Rule::where($where)->first();
  271. if(empty($rule)){
  272. return Result::error('请输入正确的规则任务id!');
  273. }else{
  274. //查找是否存在已导入的文章
  275. $art_num = Article::where('rule_id',$data['rule_id'])->where('state',1)->count();
  276. if($art_num==0){
  277. //查找是否存在已采集但是未导入的文章
  278. $colart_num = Article::where('rule_id',$data['rule_id'])->where('state',0)->count();
  279. if($colart_num==0){
  280. $result['rule'] = Rule::where($where)->delete();
  281. }else{
  282. try {
  283. Db::beginTransaction();
  284. //若有已采集但未导入的文章,删除规则任务及相应的未导入的文章
  285. $result['rule'] = Rule::where($where)->delete();
  286. $result['art'] = Article::where('rule_id',$data['rule_id'])->delete();
  287. Db::commit();
  288. } catch(\Throwable $ex){
  289. Db::rollBack();
  290. var_dump($ex->getMessage());
  291. return Result::error("删除失败",0);
  292. }
  293. }
  294. }else{
  295. return Result::error('此规则任务下的文章已导入,不可删除!');
  296. }
  297. }
  298. return Result::success($result);
  299. }
  300. /**
  301. * 开始采集
  302. * @param array $data
  303. * @return array
  304. */
  305. public function sendCrawler(array $data): array
  306. {
  307. $result = Article::get();
  308. $b = OldArticle::get();
  309. $a = [
  310. 'old'=>$b,
  311. 'new'=>$result
  312. ];
  313. return Result::success($a);
  314. }
  315. /**
  316. * 获取并搜索资讯
  317. * @param array $data
  318. * @return array
  319. */
  320. public function getInfo(array $data): array
  321. {
  322. $where = [
  323. ['rule_id','=',$data['rule_id']]
  324. ];
  325. //若存在条件参数都存到where数组
  326. if(isset($data['title'])){
  327. $where[] = ['title','like','%'.$data['title'].'%'];
  328. }
  329. if(isset($data['source'])){
  330. $art_source = Article::where($where)->get();
  331. if(!empty($art_source->toArray())){
  332. $where[] = ['source','=',$data['source']];
  333. }
  334. }
  335. if(isset($data['state'])){
  336. $where[] = ['state','=',$data['state']];
  337. }
  338. //跨库查询栏目导航及采集的新闻
  339. $info = Article::query()
  340. ->where($where)
  341. ->with(['category' => function ($query) {
  342. $query->select('name');
  343. }])
  344. ->orderBy("article.id","desc")
  345. ->limit($data['pageSize'])
  346. ->offset(($data['page']-1)*$data['pageSize'])->get();
  347. $count = Article::where($where)->count();
  348. if($count == 0){
  349. return Result::error('暂无资讯');
  350. }
  351. $data = [
  352. 'rep' => $info->toArray(),
  353. 'count' => $count
  354. ];
  355. return Result::success($data);
  356. }
  357. /**
  358. * 获取某个资讯
  359. * @param array $data
  360. * @return array
  361. */
  362. public function getOneInfo(array $data): array
  363. {
  364. $where = ['id' => $data['art_id']];
  365. $inf = Article::where($where)->first();
  366. if($inf==null){
  367. return Result::error('请输入正确的资讯id!');
  368. }
  369. $info = Article::where($where)
  370. ->leftJoin('article_data','article_id','id')
  371. ->select('article.*','article_data.content')
  372. ->first();
  373. if($inf['catid']!=null){
  374. $category = Category::where(['id'=>$info['catid']])->select('name')->first();
  375. $info['category'] = $category['name'];
  376. }
  377. return Result::success($info);
  378. }
  379. /**
  380. * 修改资讯
  381. * @param array $data
  382. * @return array
  383. */
  384. public function upInfo(array $data): array
  385. {
  386. $id = $data['art_id'];
  387. $content = $data['content'];
  388. unset($data['art_id']);
  389. //去掉此元素
  390. unset($data['content']);
  391. //去掉此元素
  392. $info = Article::where('id',$id)->first();
  393. if($info==null){
  394. return Result::error('请输入正确的文章id!');
  395. }
  396. if($info['state']==1){
  397. return Result::error('此文章已导入 ,不可编辑!');
  398. }else{
  399. Db::beginTransaction();
  400. try{
  401. $info = Article::where('id',$id)->update($data);
  402. $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);
  403. Db::commit();
  404. } catch(\Throwable $ex){
  405. Db::rollBack();
  406. var_dump($ex->getMessage());
  407. return Result::error("修改失败",0);
  408. }
  409. $data = [
  410. 'info' => $info,
  411. 'art_data' => $art_data
  412. ];
  413. return Result::success($data);
  414. }
  415. }
  416. /**
  417. * 删除资讯
  418. * @param array $data
  419. * @return array
  420. */
  421. public function delInfo(array $data): array
  422. {
  423. $id = $data['art_id'];
  424. $info = Article::where('id',$id)->first();
  425. if($info==null){
  426. return Result::error('请输入正确的文章id!');
  427. }
  428. if($info['state']==1){
  429. return Result::error('此文章已导入,不可删除!');
  430. }else{
  431. Db::beginTransaction();
  432. try{
  433. $delinfo = Article::where('id',$id)->delete();
  434. $deldata = ArticleData::where('article_id',$id)->delete();
  435. Db::commit();
  436. } catch(\Throwable $ex){
  437. Db::rollBack();
  438. var_dump($ex->getMessage());
  439. return Result::error("删除失败",0);
  440. }
  441. }
  442. $data = [
  443. 'delinfo' => $delinfo,
  444. 'deldata' => $deldata
  445. ];
  446. return Result::success($data);
  447. }
  448. /**
  449. * 关联导航池
  450. * @param array $data
  451. * @return array
  452. */
  453. public function addCatid(array $data): array
  454. {
  455. $id = $data['rule_id'];
  456. //查找此规则任务下的文章是否都已经导入
  457. $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
  458. if(empty($info->toArray())){
  459. return Result::error('所有文章都已导入,不可修改关联的导航池!');
  460. }else{
  461. //查找此规则任务下的文章是否已经有导入的文章
  462. $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
  463. if(!empty($article->toArray())){
  464. //若有已导入的文章则直接复制之前已导入的导航池
  465. $catid = Article::whereIn('id',$article)->select('catid')->first();
  466. //若未导入的文章已经复制之前的导航,则无需修改
  467. $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
  468. if($art_catid>0){
  469. $result = Article::whereIn('id',$info)->update(['catid'=>$catid['catid']]);
  470. }else{
  471. $result = ['已全部关联导航,无需再次关联!'];
  472. }
  473. }else{
  474. //若不存在已导入的文章则判断是否存在导航id
  475. if(isset($data['catid'])){
  476. //若存在直接使用此导航id
  477. $result = Article::whereIn('id',$info)->update(['catid'=>$data['catid']]);
  478. }else{
  479. //若不存在则返回所有导航栏目
  480. $result = Category::select('id','name')->get();
  481. }
  482. }
  483. }
  484. return Result::success($result);
  485. }
  486. /**
  487. * 导入文章
  488. * @param array $data
  489. * @return array
  490. */
  491. public function addArt(array $data): array
  492. {
  493. // var_dump("======@@@====");
  494. $where = [
  495. 'rule_id' => $data['rule_id'],
  496. 'state' => 0
  497. ];
  498. //获取某个规则任务下的已采集未导入的文章及文章详情
  499. $arts_id = Article::where($where)->wherenotNull('catid')->orderBy('id')->select('id')->get();
  500. $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();
  501. $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id')->get()->toArray();
  502. // var_dump($article_data);
  503. $data = [
  504. 'articles' => $arts,
  505. 'art_content' => $arts_data
  506. ];
  507. Db::beginTransaction();
  508. try{
  509. $oldart = OldArticle::insert($arts);
  510. $oldart_data = OldArticleData::insert($arts_data);
  511. $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
  512. Db::commit();
  513. } catch(\Throwable $ex){
  514. Db::rollBack();
  515. var_dump($ex->getMessage());
  516. return Result::error($ex->getMessage(),0);
  517. }
  518. return Result::success($data);
  519. }
  520. }