CollectorService.php 33 KB

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