CollectorService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Amqp\Producer\GatherProducer;
  4. use App\Model\ArticleData;
  5. use App\Model\OldModel\Article as OldArticle;
  6. use App\Model\OldModel\ArticleData as OldArticleData;
  7. use App\Model\OldModel\Category;
  8. use App\Model\Article;
  9. use App\Model\Rule;
  10. use App\Model\Web;
  11. use Hyperf\Amqp\Producer;
  12. use Hyperf\Context\ApplicationContext as ContextApplicationContext;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\Di\Annotation\Inject;
  15. use Hyperf\RpcServer\Annotation\RpcService;
  16. use App\Tools\Result;
  17. use QL\QueryList;
  18. use Swoole\Coroutine;
  19. use App\Amqp\Producer\ImportProducer;
  20. use function Hyperf\Support\retry;
  21. #[RpcService(name: "CollectorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  22. class CollectorService implements CollectorServiceInterface
  23. {
  24. // #[Inject]
  25. // protected GatherQueueService $Gservice;
  26. /**
  27. * 添加网站
  28. * @param array $data
  29. * @return array|mixed
  30. */
  31. public function addWeb(array $data): array
  32. {
  33. $where = [
  34. 'name' => $data['name']
  35. ];
  36. $isweb = Web::where($where)->first();
  37. if(empty($isweb)){
  38. date_default_timezone_set('Asia/Shanghai');
  39. $web = Web::insert($data);
  40. }else{
  41. return Result::error('此网站已存在,不可重复添加!');
  42. }
  43. if(empty($web)){
  44. return Result::error('添加失败');
  45. }
  46. return Result::success('添加成功');
  47. }
  48. /**
  49. * 获取并搜索网站
  50. * @param array $data
  51. * @return array|mixed
  52. */
  53. public function getWeb(array $data): array
  54. {
  55. if(isset($data['keyWord'])){
  56. $where = [
  57. ['name','like','%'.$data['keyWord'].'%']
  58. ];
  59. $rep = Web::where($where)->limit($data['pageSize'])->orderBy("updated_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  60. $count = Web::where($where)->count();
  61. if($count==0){
  62. return Result::error('未查找到相关网站!');
  63. }
  64. }else{
  65. $rep = Web::limit($data['pageSize'])->orderBy("updated_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  66. $count = Web::count();
  67. }
  68. $data = [
  69. 'rep' => $rep->toArray(),
  70. 'count' => $count
  71. ];
  72. if(empty($rep)){
  73. return Result::error('您还未添加网站,请先去添加!');
  74. }
  75. return Result::success($data);
  76. }
  77. /**
  78. * 修改网站
  79. * @param array $data
  80. * @return array|mixed
  81. */
  82. public function upWeb(array $data): array
  83. {
  84. $web = Web::where('id',$data['id'])->first();
  85. if(empty($web)){
  86. return Result::error('请输入正确的网站id!');
  87. }else{
  88. date_default_timezone_set('Asia/Shanghai');
  89. $id = Web::where('id',$data['id'])->update($data);
  90. if(empty($id)){
  91. return Result::error('无法修改!');
  92. }
  93. }
  94. return Result::success($id);
  95. }
  96. /**
  97. * 删除网站
  98. * @param array $data
  99. * @return array|mixed
  100. */
  101. public function delWeb(array $data): array
  102. {
  103. $web = Web::where('id',$data['id'])->first();
  104. if(empty($web)){
  105. return Result::error('请输入正确的网站id!');
  106. }else{
  107. $where = [
  108. ['web_id','=',$data['id']]
  109. ];
  110. //判断此网站下是否规则u任务
  111. $rule = Rule::where($where)->get();
  112. if(empty($rle)){
  113. //若没有直接删除网站
  114. $result['web'] = Web::where('id',$data['id'])->delete();
  115. }else{
  116. //若有,判断规则任务是否有已执行的
  117. $rule = Rule::where($where)->where('status',2)->get();
  118. // return Result::success($rule);
  119. if(!empty($rule->toArray())){
  120. //若有已执行的任务规则,不可删除网站
  121. return Result::error('该网站已有成功执行的任务规则,不可删除!');
  122. }else{
  123. try {
  124. Db::beginTransaction();
  125. //若无已执行的任务规则,删除网站及相应的未执行的规则任务
  126. $result['web'] = Web::where('id',$data['id'])->delete();
  127. $result['rule'] = Rule::where($where)->delete();
  128. Db::commit();
  129. } catch(\Throwable $ex){
  130. Db::rollBack();
  131. var_dump($ex->getMessage());
  132. return Result::error("删除失败",0);
  133. }
  134. }
  135. }
  136. }
  137. return Result::success($result);
  138. }
  139. /**
  140. * 添加任务规则
  141. * @param array $data
  142. * @return array|mixed
  143. */
  144. public function addRule(array $data): array
  145. {
  146. $web = Web::where('id',$data['web_id'])->get();
  147. if(empty($web->toArray())){
  148. return Result::error('请输入正确的网站id!');
  149. }else{
  150. $rulename = Rule::where('name',$data['name'])->get();
  151. //查找是否存在规则名称重复的
  152. if(empty($rulename->toArray())){
  153. //(若是多类型参数一起传过来则根据类型,只获取对应类型需要的参数)
  154. switch($data['type']){
  155. case 1:
  156. $rule = [
  157. 'name' => $data['name'],
  158. 'web_id' => $data['web_id'],
  159. 'first_url' => $data['first_url'],
  160. 'second_start' => $data['second_start'],
  161. 'second_num' => $data['second_num'],
  162. 'second_end' => $data['second_end'],
  163. 'end_pagenum' => $data['end_pagenum'],
  164. 'start' => $data['start'],
  165. 'con_url' => $data['con_url'],
  166. 'title' => $data['title'],
  167. 'content' => $data['content']
  168. ];
  169. // var_dump("============1============");
  170. break;
  171. case 2:
  172. $rule = [
  173. 'name' => $data['name'],
  174. 'web_id' => $data['web_id'],
  175. 'first_url' => $data['first_url'],
  176. 'parameter' => $data['parameter'],
  177. 'start' => $data['start'],
  178. 'title' => $data['title'],
  179. 'content' => $data['content']
  180. ];
  181. // var_dump("============2============");
  182. break;
  183. default:
  184. $rule = [
  185. 'name' => $data['name'],
  186. 'web_id' => $data['web_id'],
  187. 'diy_rule' => $data['diy_rule']
  188. ];
  189. // var_dump("============3============");
  190. break;
  191. }
  192. if(!empty($data['con_start']) && $data['type'] == 1){
  193. $rule ['con_start'] = $data['con_start'];
  194. }
  195. if(!empty($data['source']) && $data['type'] != 3){
  196. $rule ['source'] = $data['source'];
  197. }
  198. if(isset($data['writer_class']) && $data['type'] != 3){
  199. $rule ['writer_class'] = $data['writer_class'];
  200. }
  201. if(isset($data['writer']) && $data['type'] != 3){
  202. $rule ['writer'] = $data['writer'];
  203. }
  204. date_default_timezone_set('Asia/Shanghai');
  205. //若不存在,根据网站类型添加到不行类型的规则表中
  206. $result = Rule::insertGetId($rule);
  207. }else{
  208. return Result::error('此任务已存在!');
  209. }
  210. }
  211. return Result::success($result);
  212. }
  213. /**
  214. * 获取并搜索规则任务
  215. * @param array $data
  216. * @return array|mixed
  217. */
  218. public function getRule(array $data): array
  219. {
  220. $where = [];
  221. if(isset($data['web_id'])){
  222. $web = Web::where('id',$data['web_id'])->get();
  223. if(empty($web->toArray())){
  224. return Result::error('请输入正确的网站id!');
  225. }else{
  226. //若是根据网站跳转到的规则任务则存到$where数组中
  227. $where = [
  228. ['web_id','=', $data['web_id']]
  229. ];
  230. }
  231. }
  232. if(isset($data['keyWord'])){
  233. //若存在搜索词,则存到条件数组$where中
  234. $where = [
  235. ['name','like','%'.$data['keyWord'].'%']
  236. ];
  237. }
  238. if(empty($where)){
  239. $rep = Rule::withCount(relations:'arts')->limit($data['pageSize'])->orderBy("updated_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  240. }else{
  241. $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("updated_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  242. }
  243. $count = Rule::where($where)->count();
  244. if($count==0){
  245. return Result::error('暂无相关规则任务!');
  246. }
  247. $data = [
  248. 'rep' => $rep->toArray(),
  249. 'count' => $count
  250. ];
  251. return Result::success($data);
  252. }
  253. /**
  254. * 获取某个任务规则
  255. * @param array $data
  256. * @return array|mixed
  257. */
  258. public function getOneRule(array $data): array
  259. {
  260. $result = Rule::where('id',$data['id'])->first();
  261. if(empty($result)){
  262. return Result::error('请输入正确的规则任务id!');
  263. }else{
  264. return Result::success($result);
  265. }
  266. }
  267. /**
  268. * 修改规则任务
  269. * @param array $data
  270. * @return array|mixed
  271. */
  272. public function upRule(array $data): array
  273. {
  274. $rule = Rule::where('id',$data['id'])->select('id')->first();
  275. unset($data['type']);
  276. if(empty($rule)){
  277. return Result::error('请输入正确的规则任务id!');
  278. }else{
  279. $rulename = Rule::where('id','!=',$rule['id'])->where('name',$data['name'])->select('name')->first();
  280. if(empty($rulename)){
  281. $result = Rule::where('id',$data['id'])->update($data);
  282. }else{
  283. return Result::error('已存在此任务规则名称!');
  284. }
  285. }
  286. return Result::success($result);
  287. }
  288. /**
  289. * 删除规则任务
  290. * @param array $data
  291. * @return array
  292. */
  293. public function delRule(array $data): array
  294. {
  295. $where = ['id' => $data['rule_id']];
  296. $rule = Rule::where($where)->first();
  297. if(empty($rule)){
  298. return Result::error('请输入正确的规则任务id!');
  299. }else{
  300. //查找是否存在已导入的文章
  301. $art_num = Article::where('rule_id',$data['rule_id'])->where('state',1)->count();
  302. if($art_num==0){
  303. //查找是否存在已采集但是未导入的文章
  304. $colart_num = Article::where('rule_id',$data['rule_id'])->where('state',0)->count();
  305. if($colart_num==0){
  306. $result['rule'] = Rule::where($where)->delete();
  307. }else{
  308. try {
  309. Db::beginTransaction();
  310. //若有已采集但未导入的文章,删除规则任务及相应的未导入的文章
  311. $result['rule'] = Rule::where($where)->delete();
  312. $result['art'] = Article::where('rule_id',$data['rule_id'])->delete();
  313. Db::commit();
  314. } catch(\Throwable $ex){
  315. Db::rollBack();
  316. var_dump($ex->getMessage());
  317. return Result::error("删除失败",0);
  318. }
  319. }
  320. }else{
  321. return Result::error('此规则任务下的文章已导入,不可删除!');
  322. }
  323. }
  324. return Result::success($result);
  325. }
  326. /**
  327. * 开始采集
  328. * @param array $data
  329. * @return array
  330. */
  331. public function sendCrawler(array $data): array
  332. {
  333. var_dump("接收到的数据:",$data);
  334. $message = new GatherProducer($data);
  335. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  336. $a = $producer->produce($message);
  337. var_dump("生产者:",$a);
  338. // $result = $this->Gservice->push($data,rand(5,20));
  339. return Result::success([]);
  340. }
  341. /**
  342. * @param array $data
  343. * @return array
  344. */
  345. public function goCrawler(array $data): array
  346. {
  347. //通过规则id 查询规则类型
  348. $where = [
  349. 'rule.id'=>$data['id']
  350. ];
  351. $info = Rule::where($where)->leftJoin('web','rule.web_id','web.id')
  352. ->select("rule.*","web.name as web_name","web.url as web_url","web.type as web_type")
  353. ->first();
  354. $info = $info->toArray();
  355. var_dump("规则信息:",$info);
  356. switch ($info['web_type']){
  357. case 1:
  358. var_dump("===========规则采集======",$info);
  359. try {
  360. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  361. //添加几个值
  362. $info['admin_user_id'] = $data['admin_user_id'];
  363. $info['rule_id'] = $data['id'];
  364. $info['copyfrom'] = $info['web_name'];
  365. $info['author'] = $info['writer'];
  366. // var_dump("++++++++++++++++++");
  367. $urlList = $this->addUrlArr($info);
  368. // var_dump("采集列表:",$urlList);
  369. if($urlList){
  370. foreach ($urlList as $val){
  371. $this->ruleCollection($val,$info);
  372. }
  373. }
  374. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  375. }catch (\Exception $e){
  376. var_dump("采集失败报错:",$e->getMessage());
  377. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  378. }
  379. break;
  380. case 2:
  381. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  382. $wecUrl = $info['first_url'];//'https://www.ndcpa.gov.cn/queryList';
  383. $parames = json_decode($info['parameter'],true);
  384. // var_dump($parames);die;
  385. $parames['webSiteCode'] = [trim($parames['webSiteCode'], "[]")]; //['jbkzzx'];//
  386. $parames['channelCode'] = [trim($parames['channelCode'], "[]")]; // ['c100008'];//
  387. $other = [
  388. 'web_url'=>$info['web_url'],
  389. 'copyfrom'=>$info['web_name'],
  390. 'admin_user_id'=>$data['admin_user_id'],
  391. 'rule_id'=>$data['id'],
  392. 'writer'=>$info['writer'],
  393. ];
  394. var_dump("=======开始接口采集====",$parames);
  395. // die;
  396. $this->foreachCurl($wecUrl,$parames,$other);
  397. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  398. }
  399. return Result::success([]);
  400. }
  401. /**
  402. * 把可采集的列表页连接 打包成一个大数组
  403. * @return void
  404. */
  405. public function addUrlArr($data)
  406. {
  407. $arrList = [];
  408. array_push($arrList,$data['first_url']);
  409. $exit = false;
  410. $i = 0;
  411. while(!$exit){
  412. $i++;
  413. $url = $data['second_start'].$i.$data['second_end'];
  414. $respon1 = Result::pageExists($url);
  415. // Coroutine::sleep(2);
  416. if ($i==intval($data['end_pagenum'])-1 || intval($data['end_pagenum'])-1==0) {
  417. $exit = true;
  418. // Coroutine::exit(); // 退出循环
  419. }else{
  420. array_push($arrList,$url);
  421. }
  422. }
  423. return $arrList;
  424. }
  425. /**
  426. * 按照规则采集数据
  427. * @return void
  428. */
  429. public function ruleCollection($url,$info)
  430. {
  431. // var_dump("采集参数:",$url,$info['start']);
  432. $list = QueryList::get($url);
  433. $dataList = $list->rules([
  434. 'title' => ['a:eq(0)', 'text'],
  435. 'link' => ['a:eq(0)', 'href'],
  436. ])->range($info['start'])->query()->getData();
  437. var_dump("采集的列表:",$dataList);
  438. $firstUrlArr = explode("/", $url);
  439. array_pop($firstUrlArr);
  440. $firstUrlArr = implode('/',$firstUrlArr);
  441. $dataList = $dataList->toArray();
  442. if($dataList){
  443. foreach ($dataList as $tiem){
  444. //检测采集的url是否存在网站域名 。存在就继续,不存在就检测是否是三方跳转
  445. $newUrlStr = $tiem['link'];
  446. if (strpos($tiem['link'], $info['web_url']) === false) {
  447. $array = ['http','https'];
  448. $link = $tiem['link'];
  449. $found = array_filter($array, function($item) use ($link) {
  450. return str_contains($link, $item);
  451. });
  452. if(count($found)>0){
  453. continue;
  454. }
  455. $newUrlStr = $info['con_url'].$tiem['link'];
  456. }
  457. var_dump("详情地址:",$newUrlStr);
  458. $detailContent = QueryList::get($newUrlStr);
  459. $rules = [];
  460. if($info['title']){
  461. $rules['title'] = [$info['title'],'text'];
  462. }
  463. if($info['content']){
  464. $rules['content'] = [$info['content'],'html'];
  465. }
  466. //详情页范围
  467. $detailRange = $info['con_start']??'';
  468. var_dump("打印规则:",$rules,"详情起始:", $info['con_start']);
  469. $detailData = $detailContent->rules($rules)->range($detailRange)->query()->getData();
  470. $detailData = $detailData->toArray();
  471. var_dump("内容详情:",$detailData,$newUrlStr);
  472. if($detailData){
  473. foreach ($detailData as $val){
  474. // var_dump("进没进foreach:",$newUrlStr,$val);
  475. $data = [];
  476. $data['fromurl'] = $newUrlStr;
  477. $data['title'] = $val['title'];
  478. $data['content'] = $val['content'];
  479. $data['newUrlStr'] = $newUrlStr;
  480. $data['introduce'] = $val['title']??'';
  481. $data['keyword'] = $val['title']??'';
  482. $data['copyfrom'] = $info['copyfrom'];
  483. $data['source'] = $info['source']??$info['copyfrom'];
  484. $data['admin_user_id'] = $info['admin_user_id']??'';
  485. $data['rule_id'] = $info['rule_id']??'';
  486. $data['author'] = $info['author']??'';
  487. $this->insertArticleData($data);
  488. }
  489. }
  490. }
  491. }
  492. }
  493. /**
  494. * 插入数据
  495. * @param $data
  496. * @return void
  497. */
  498. public function insertArticleData($data=[])
  499. {
  500. if($data){
  501. Db::beginTransaction();
  502. try{
  503. $articleInfo = Article::where(['title'=>$data['title']])->first();
  504. // var_dump("获取详情:",$articleInfo,$data);
  505. if(empty($articleInfo)){
  506. $insertData = [];
  507. $insertData['fromurl'] =$data['newUrlStr'];
  508. $insertData['oldtitle'] =$data['title'];
  509. $insertData['title'] = $data['title'];
  510. $insertData['copyfrom'] = $data['copyfrom'];
  511. $insertData['author'] = $data['author'];
  512. $insertData['introduce'] = $data['title'];
  513. $insertData['keyword'] = $data['title'];
  514. $insertData['source'] = isset($data['source']) && $data['source']!=''? $data['source']:$data['copyfrom'];
  515. $insertData['admin_user_id'] = $data['admin_user_id'];
  516. $insertData['rule_id'] = $data['rule_id'];
  517. // var_dump("插入Article:",$insertData);
  518. $article_id = Article::insertGetId($insertData);
  519. $insertDataDetail = [];
  520. $insertDataDetail['article_id'] = $article_id;
  521. $insertDataDetail['content'] = $data['content'];
  522. // var_dump("插入ArticleData:",$insertDataDetail);
  523. ArticleData::insertGetId($insertDataDetail);
  524. // Coroutine::sleep(2);
  525. // var_dump("插入成功一次:",$article_id,$insertDataDetail);
  526. }
  527. Db::commit();
  528. }catch (\Exception $e){
  529. Db::rollBack();
  530. var_dump("插入失败:",$e->getMessage());
  531. }
  532. }else{
  533. var_dump("没有数据可以插入:");
  534. }
  535. }
  536. /**
  537. * 分页采集
  538. * @return void
  539. */
  540. public function foreachCurl($wecUrl,$parames,$other,&$page=1)
  541. {
  542. $options = [
  543. CURLOPT_HEADER => true, // 如果想包含头部信息在响应中,可以设置为true
  544. CURLOPT_TIMEOUT => 30 // 设置请求超时时间为30秒
  545. ];
  546. $result = Result::http_post($wecUrl,$parames,$options);
  547. $result = json_decode($result['response'],true);
  548. // var_dump("获取数据:",$result);
  549. if($result['data'] && $result['data']['results']){
  550. $dataList = $result['data']['results'];
  551. // var_dump("取数据结构体:",$dataList);
  552. foreach ($dataList as $val){
  553. // var_dump("进入循环插入:",$val);
  554. $newUrlStr = json_decode($val['source']['urls'],true);
  555. $newUrlStr = $other['web_url'].$newUrlStr['common'];
  556. // var_dump("来源地址:",$newUrlStr);
  557. $insertData = [
  558. 'newUrlStr'=>$newUrlStr,
  559. 'title'=>$val['source']['title']??'',
  560. 'source'=>$val['source']['contentSource']??'',
  561. 'copyfrom'=>$other['copyfrom']??'',
  562. 'content'=>$val['source']['content']['content']??'',
  563. 'admin_user_id'=>$other['admin_user_id']??'',
  564. 'rule_id'=>$other['rule_id']??'',
  565. 'author'=>$other['writer']??''
  566. ];
  567. // var_dump("调用插入数据方法,组装数据:",$insertData);
  568. $this->insertArticleData($insertData);
  569. }
  570. }
  571. $pages = intval($parames['current']);
  572. $pages = $pages+1;
  573. $parames['current'] = $pages;
  574. $twoResult = Result::http_post($wecUrl,$parames,$options);
  575. if($result['data'] && $result['data']['results'] && count($result['data']['results'])>0){
  576. // var_dump("分页测试:",$parames,$parames['current']);
  577. $this->foreachCurl($wecUrl,$parames,$other,$pages);
  578. }
  579. // var_dump("正确的数据:",$result);
  580. }
  581. /**
  582. * 获取并搜索资讯
  583. * @param array $data
  584. * @return array
  585. */
  586. public function getInfo(array $data): array
  587. {
  588. $where = [
  589. ['rule_id','=',$data['rule_id']]
  590. ];
  591. //若存在条件参数都存到where数组
  592. if(isset($data['title']) && !empty($data['title'])){
  593. array_push($where,['title','like','%'.$data['title'].'%']);
  594. }
  595. if(isset($data['source']) && !empty($data['source'])){
  596. // $art_source = Article::where($where)->get();
  597. // if(!empty($art_source->toArray())){
  598. array_push($where,['copyfrom','like','%'.$data['source'].'%']);
  599. // }
  600. }
  601. if(isset($data['state']) && $data['state']!=''){
  602. array_push($where,['state',$data['state']]);
  603. }
  604. //跨库查询栏目导航及采集的新闻
  605. $info = Article::query()
  606. ->where($where)
  607. ->with('category')
  608. ->orderBy("article.updated_at","desc")
  609. ->limit($data['pageSize'])
  610. ->offset(($data['page']-1)*$data['pageSize'])->get();
  611. $count = Article::where($where)->count();
  612. if($count == 0){
  613. return Result::error('暂无资讯');
  614. }
  615. $result = [
  616. 'rep' => $info->toArray(),
  617. 'count' => $count
  618. ];
  619. return Result::success($result);
  620. }
  621. /**
  622. * 获取某个资讯
  623. * @param array $data
  624. * @return array
  625. */
  626. public function getOneInfo(array $data): array
  627. {
  628. $where = ['id' => $data['art_id']];
  629. $inf = Article::where($where)->first();
  630. if($inf==null){
  631. return Result::error('请输入正确的资讯id!');
  632. }
  633. $info = Article::where($where)
  634. ->leftJoin('article_data','article_id','id')
  635. ->select('article.*','article_data.content')
  636. ->first();
  637. if($inf['catid']!=null){
  638. $category = Category::where(['id'=>$info['catid']])->select('name')->first();
  639. $info['category'] = $category['name'];
  640. }
  641. return Result::success($info);
  642. }
  643. /**
  644. * 修改资讯
  645. * @param array $data
  646. * @return array
  647. */
  648. public function upInfo(array $data): array
  649. {
  650. $id = $data['art_id'];
  651. $content = $data['content'];
  652. unset($data['art_id']);
  653. //去掉此元素
  654. unset($data['content']);
  655. //去掉此元素
  656. $info = Article::where('id',$id)->first();
  657. if($info==null){
  658. return Result::error('请输入正确的文章id!');
  659. }
  660. if($info['state']==1){
  661. return Result::error('此文章已导入 ,不可编辑!');
  662. }else{
  663. Db::beginTransaction();
  664. try{
  665. $info = Article::where('id',$id)->update($data);
  666. $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);
  667. Db::commit();
  668. } catch(\Throwable $ex){
  669. Db::rollBack();
  670. var_dump($ex->getMessage());
  671. return Result::error("修改失败",0);
  672. }
  673. $data = [
  674. 'info' => $info,
  675. 'art_data' => $art_data
  676. ];
  677. return Result::success($data);
  678. }
  679. }
  680. /**
  681. * 删除资讯
  682. * @param array $data
  683. * @return array
  684. */
  685. public function delInfo(array $data): array
  686. {
  687. $id = $data['art_id'];
  688. $info = Article::where('id',$id)->first();
  689. if($info==null){
  690. return Result::error('请输入正确的文章id!');
  691. }
  692. if($info['state']==1){
  693. return Result::error('此文章已导入,不可删除!');
  694. }else{
  695. Db::beginTransaction();
  696. try{
  697. $delinfo = Article::where('id',$id)->delete();
  698. $deldata = ArticleData::where('article_id',$id)->delete();
  699. Db::commit();
  700. } catch(\Throwable $ex){
  701. Db::rollBack();
  702. var_dump($ex->getMessage());
  703. return Result::error("删除失败",0);
  704. }
  705. }
  706. $data = [
  707. 'delinfo' => $delinfo,
  708. 'deldata' => $deldata
  709. ];
  710. return Result::success($data);
  711. }
  712. /**
  713. * 关联导航池
  714. * @param array $data
  715. * @return array
  716. */
  717. public function addCatid(array $data): array
  718. {
  719. $id = $data['rule_id'];
  720. $art = Article::where('rule_id',$id)->select('id')->count();
  721. if($art==0){
  722. return Result::error('还未采集,请采集');
  723. }else{
  724. $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
  725. if(empty($info->toArray())){
  726. return Result::error('所有文章都已导入,不可修改关联的导航池!');
  727. }else{
  728. //查找此规则任务下的文章是否已经有导入的文章
  729. $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
  730. if(!empty($article->toArray())){
  731. //查询已导入的文章的导航id
  732. $catid = Article::whereIn('id',$article)->select('catid')->first();
  733. $cat_arr_id = Article::whereIn('id',$article)->select('cat_arr_id')->first();
  734. //查询未导入的文章id
  735. $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
  736. if($art_catid>0){
  737. $catid = isset($catid['catid'])?$catid['catid']:'';
  738. // var_dump("更新数据111:",$catid,$cat_arr_id);
  739. $result = Article::whereIn('id',$info)->update(['catid'=>$catid,'cat_arr_id'=>$cat_arr_id['cat_arr_id']]);
  740. // var_dump("更新数据111:",$result);
  741. }else{
  742. // var_dump("已全部关联导航池请勿重复关联");
  743. return Result::error('已全部关联导航池请勿重复关联');
  744. }
  745. }else{
  746. //若不存在已导入的文章则判断是否存在导航id
  747. if(isset($data['cat_arr_id'])){
  748. $catid = isset($data['cat_arr_id'])?end($data['cat_arr_id']):'';
  749. $cat_arr_id = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  750. //若存在直接使用此导航id
  751. $result = Article::whereIn('id',$info)->update(['catid'=>$catid,'cat_arr_id'=>$cat_arr_id]);
  752. var_dump("55555555555555555",$result);
  753. }else{
  754. //若不存在则返回所有导航栏目
  755. $result = Category::select('id','name')->get();
  756. if(!empty($result)){
  757. return Result::success($result);
  758. }else{
  759. return Result::error('暂无数据');
  760. }
  761. }
  762. }
  763. }
  764. }
  765. if(empty($result)){
  766. return Result::error('暂无数据');
  767. }else{
  768. return Result::success($result);
  769. }
  770. // return Result::success($result);
  771. }
  772. /**
  773. * 导入文章(生产者)
  774. * @param array $data
  775. * @return array
  776. */
  777. public function addArt(array $data): array
  778. {
  779. var_dump("接收到的数据:",$data);
  780. $message = new ImportProducer($data);
  781. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  782. $a = $producer->produce($message);
  783. var_dump("生产者:",$a);
  784. // $result = $this->Gservice->push($data,rand(5,20));
  785. return Result::success([]);
  786. }
  787. /**
  788. * 导入文章(消费者)
  789. * @param array $data
  790. * @return array
  791. */
  792. public function goAddArt(array $data): array
  793. {
  794. // var_dump('准备去消费------',$data);
  795. // var_dump("======@@@====");
  796. $where = [
  797. 'rule_id' => $data['rule_id'],
  798. 'state' => 0
  799. ];
  800. //获取某个规则任务下的已采集未导入的文章及文章详情
  801. $arts_id = Article::where($where)->wherenotNull('catid')->select('id')->orderBy('id')->get()->toArray();
  802. $arts = Article::where($where)->wherenotNull('catid')->select('title','catid','level','introduce','keyword','author','copyfrom','fromurl','hits','islink','imgurl','admin_user_id','is_original','cat_arr_id')->orderBy('id')->get()->toArray();
  803. // var_dump('=============:::',$arts_id);
  804. $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id','desc')->get()->toArray();
  805. // var_dump('=============',$arts);
  806. $data = [
  807. 'articles' => $arts,
  808. 'art_content' => $arts_data
  809. ];
  810. Db::beginTransaction();
  811. try{
  812. $oldart = OldArticle::insert($arts);
  813. $oldart_data = OldArticleData::insert($arts_data);
  814. $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
  815. Db::commit();
  816. } catch(\Throwable $ex){
  817. Db::rollBack();
  818. var_dump($ex->getMessage());
  819. return Result::error($ex->getMessage(),0);
  820. }
  821. return Result::success($data);
  822. }
  823. }