CollectorService.php 19 KB

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