CollectorService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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("created_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("created_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. '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['parameter']) && $data['type'] == 1){
  193. $rule ['parameter'] = $data['parameter'];
  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. var_dump($data);
  205. date_default_timezone_set('Asia/Shanghai');
  206. //若不存在,根据网站类型添加到不行类型的规则表中
  207. $result = Rule::insertGetId($rule);
  208. }else{
  209. return Result::error('此任务已存在!');
  210. }
  211. }
  212. return Result::success($result);
  213. }
  214. /**
  215. * 获取并搜索规则任务
  216. * @param array $data
  217. * @return array|mixed
  218. */
  219. public function getRule(array $data): array
  220. {
  221. $where = [];
  222. if(isset($data['web_id'])){
  223. $web = Web::where('id',$data['web_id'])->get();
  224. if(empty($web->toArray())){
  225. return Result::error('请输入正确的网站id!');
  226. }else{
  227. //若是根据网站跳转到的规则任务则存到$where数组中
  228. $where = [
  229. ['web_id','=', $data['web_id']]
  230. ];
  231. }
  232. }
  233. if(isset($data['keyWord'])){
  234. //若存在搜索词,则存到条件数组$where中
  235. $where = [
  236. ['name','like','%'.$data['keyWord'].'%']
  237. ];
  238. }
  239. if(empty($where)){
  240. $rep = Rule::withCount(relations:'arts')->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  241. }else{
  242. $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  243. }
  244. $count = Rule::where($where)->count();
  245. if($count==0){
  246. return Result::error('暂无相关规则任务!');
  247. }
  248. $data = [
  249. 'rep' => $rep->toArray(),
  250. 'count' => $count
  251. ];
  252. return Result::success($data);
  253. }
  254. /**
  255. * 获取某个任务规则
  256. * @param array $data
  257. * @return array|mixed
  258. */
  259. public function getOneRule(array $data): array
  260. {
  261. $result = Rule::where('id',$data['id'])->first();
  262. if(empty($result)){
  263. return Result::error('请输入正确的规则任务id!');
  264. }else{
  265. return Result::success($result);
  266. }
  267. }
  268. /**
  269. * 修改规则任务
  270. * @param array $data
  271. * @return array|mixed
  272. */
  273. public function upRule(array $data): array
  274. {
  275. $rule = Rule::where('id',$data['id'])->select('id')->first();
  276. unset($data['type']);
  277. if(empty($rule)){
  278. return Result::error('请输入正确的规则任务id!');
  279. }else{
  280. $rulename = Rule::where('id','!=',$rule['id'])->where('name',$data['name'])->select('name')->first();
  281. if(empty($rulename)){
  282. $result = Rule::where('id',$data['id'])->update($data);
  283. }else{
  284. return Result::error('已存在此任务规则名称!');
  285. }
  286. }
  287. return Result::success($result);
  288. }
  289. /**
  290. * 删除规则任务
  291. * @param array $data
  292. * @return array
  293. */
  294. public function delRule(array $data): array
  295. {
  296. $where = ['id' => $data['rule_id']];
  297. $rule = Rule::where($where)->first();
  298. if(empty($rule)){
  299. return Result::error('请输入正确的规则任务id!');
  300. }else{
  301. //查找是否存在已导入的文章
  302. $art_num = Article::where('rule_id',$data['rule_id'])->where('state',1)->count();
  303. if($art_num==0){
  304. //查找是否存在已采集但是未导入的文章
  305. $colart_num = Article::where('rule_id',$data['rule_id'])->where('state',0)->count();
  306. if($colart_num==0){
  307. $result['rule'] = Rule::where($where)->delete();
  308. }else{
  309. try {
  310. Db::beginTransaction();
  311. //若有已采集但未导入的文章,删除规则任务及相应的未导入的文章
  312. $result['rule'] = Rule::where($where)->delete();
  313. $result['art'] = Article::where('rule_id',$data['rule_id'])->delete();
  314. Db::commit();
  315. } catch(\Throwable $ex){
  316. Db::rollBack();
  317. var_dump($ex->getMessage());
  318. return Result::error("删除失败",0);
  319. }
  320. }
  321. }else{
  322. return Result::error('此规则任务下的文章已导入,不可删除!');
  323. }
  324. }
  325. return Result::success($result);
  326. }
  327. /**
  328. * 开始采集
  329. * @param array $data
  330. * @return array
  331. */
  332. public function sendCrawler(array $data): array
  333. {
  334. var_dump("接收到的数据:",$data);
  335. $message = new GatherProducer($data);
  336. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  337. $a = $producer->produce($message);
  338. var_dump("生产者:",$a);
  339. // $result = $this->Gservice->push($data,rand(5,20));
  340. return Result::success([]);
  341. }
  342. /**
  343. * @param array $data
  344. * @return array
  345. */
  346. public function goCrawler(array $data): array
  347. {
  348. //通过规则id 查询规则类型
  349. $where = [
  350. 'rule.id'=>$data['id']
  351. ];
  352. $info = Rule::where($where)->leftJoin('web','rule.web_id','web.id')
  353. ->select("rule.*","web.name as web_name","web.url as web_url","web.type as web_type")
  354. ->first();
  355. $info = $info->toArray();
  356. switch ($info['web_type']){
  357. case 1:
  358. var_dump("===========规则采集======",$info);
  359. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  360. $data['copyfrom'] = $info['web_name'];
  361. $data['author'] = $info['writer'];;
  362. $data['first_url'] = $info['first_url'];
  363. $data['second_start'] = $info['second_start'];
  364. $data['second_num'] = $info['second_num'];
  365. $data['second_end'] = $info['second_end'];
  366. $data['end_pagenum']= $info['end_pagenum'];
  367. $data['rule_id']= $data['id'];
  368. $data['admin_user_id']= $data['admin_user_id'];
  369. // $data['newUrlStr'] =
  370. $urlList = $this->addUrlArr($data);
  371. if($urlList){
  372. foreach ($urlList as $val){
  373. // var_dump("单列表地址:",$val);
  374. $this->ruleCollection($val,$data);
  375. }
  376. }
  377. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  378. break;
  379. case 2:
  380. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  381. $wecUrl = $info['first_url'];//'https://www.ndcpa.gov.cn/queryList';
  382. $parames = json_decode($info['parameter'],true);
  383. // var_dump($parames);die;
  384. $parames['webSiteCode'] = [trim($parames['webSiteCode'], "[]")]; //['jbkzzx'];//
  385. $parames['channelCode'] = [trim($parames['channelCode'], "[]")]; // ['c100008'];//
  386. $other = [
  387. 'web_url'=>$info['web_url'],
  388. 'copyfrom'=>$info['web_name'],
  389. 'admin_user_id'=>$data['admin_user_id'],
  390. 'rule_id'=>$data['id'],
  391. 'writer'=>$info['writer'],
  392. ];
  393. var_dump("=======开始接口采集====",$parames);
  394. // die;
  395. $this->foreachCurl($wecUrl,$parames,$other);
  396. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  397. }
  398. return Result::success([]);
  399. }
  400. /**
  401. * 把可采集的列表页连接 打包成一个大数组
  402. * @return void
  403. */
  404. public function addUrlArr($data)
  405. {
  406. $arrList = [];
  407. array_push($arrList,$data['first_url']);
  408. $exit = false;
  409. $i = 0;
  410. while(!$exit){
  411. $i++;
  412. $url = $data['second_start'].$i.$data['second_end'];
  413. $respon1 = Result::pageExists($url);
  414. // var_dump("采集地址:",$respon1,$url);
  415. // Coroutine::sleep(2);
  416. if ($i==intval($data['end_pagenum'])-1) {
  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,$data)
  430. {
  431. // var_dump("采集参数:",$data);
  432. $list = QueryList::get($url);
  433. $dataList = $list->rules([
  434. 'title' => ['a', 'text'],
  435. 'link' => ['a', 'href'],
  436. ])->range('.list1 li')->query()->getData();
  437. // var_dump("采集的内容:",$dataList);
  438. // var_dump("====",$dataList);die;
  439. $firstUrlArr = explode("/", $url);
  440. array_pop($firstUrlArr);
  441. $firstUrlArr = implode('/',$firstUrlArr);
  442. $dataList = $dataList->toArray();
  443. if($dataList){
  444. foreach ($dataList as $tiem){
  445. $newUrl = substr($tiem['link'], 1);
  446. $newUrlStr = $firstUrlArr.$newUrl;
  447. $detailContent = QueryList::get($newUrlStr);
  448. $detailData = $detailContent->rules([
  449. 'title'=>['h1','text'],
  450. 'content'=>['.TRS_UEDITOR','html'],
  451. ])->range(".news-details")->query()->getData();
  452. $detailData = $detailData->toArray();
  453. // var_dump("内容详情:",$detailData,$newUrlStr);
  454. if($detailData){
  455. foreach ($detailData as $val){
  456. // var_dump("进没进foreach:",$newUrlStr,$val);
  457. $data['fromurl'] = $newUrlStr;
  458. $data['title'] = $val['title'];
  459. $data['content'] = $val['content'];
  460. $data['newUrlStr'] = $newUrlStr;
  461. $data['source'] = '';
  462. $data['introduce'] = $val['title']??'';
  463. $data['keyword'] = $val['title']??'';
  464. $data['copyfrom'] = $data['copyfrom'];
  465. $data['source'] = $data['source']??$data['copyfrom'];
  466. $data['admin_user_id'] = $data['admin_user_id']??'';
  467. $data['rule_id'] = $data['rule_id']??'';
  468. // $data['copyfrom'] = $data['copyfrom'];
  469. // var_dump("要插入的数据:",$data);
  470. $this->insertArticleData($data);
  471. }
  472. }
  473. }
  474. //
  475. }
  476. }
  477. /**
  478. * 插入数据
  479. * @param $data
  480. * @return void
  481. */
  482. public function insertArticleData($data=[])
  483. {
  484. if($data){
  485. Db::beginTransaction();
  486. try{
  487. $articleInfo = Article::where(['title'=>$data['title']])->first();
  488. // var_dump("获取详情:",$articleInfo,$data);
  489. if(empty($articleInfo)){
  490. $insertData = [];
  491. $insertData['fromurl'] =$data['newUrlStr'];
  492. $insertData['oldtitle'] =$data['title'];
  493. $insertData['title'] = $data['title'];
  494. $insertData['copyfrom'] = $data['copyfrom'];
  495. $insertData['author'] = $data['author'];
  496. $insertData['introduce'] = $data['title'];
  497. $insertData['keyword'] = $data['title'];
  498. $insertData['source'] = isset($data['source']) && $data['source']!=''? $data['source']:$data['copyfrom'];
  499. $insertData['admin_user_id'] = $data['admin_user_id'];
  500. $insertData['rule_id'] = $data['rule_id'];
  501. // var_dump("插入Article:",$insertData);
  502. $article_id = Article::insertGetId($insertData);
  503. $insertDataDetail = [];
  504. $insertDataDetail['article_id'] = $article_id;
  505. $insertDataDetail['content'] = $data['content'];
  506. // var_dump("插入ArticleData:",$insertDataDetail);
  507. ArticleData::insertGetId($insertDataDetail);
  508. // Coroutine::sleep(2);
  509. // var_dump("插入成功一次:",$article_id,$insertDataDetail);
  510. }
  511. Db::commit();
  512. }catch (\Exception $e){
  513. Db::rollBack();
  514. var_dump("插入失败:",$e->getMessage());
  515. }
  516. }else{
  517. var_dump("没有数据可以插入:");
  518. }
  519. }
  520. /**
  521. * 分页采集
  522. * @return void
  523. */
  524. public function foreachCurl($wecUrl,$parames,$other,&$page=1)
  525. {
  526. $options = [
  527. CURLOPT_HEADER => true, // 如果想包含头部信息在响应中,可以设置为true
  528. CURLOPT_TIMEOUT => 30 // 设置请求超时时间为30秒
  529. ];
  530. $result = Result::http_post($wecUrl,$parames,$options);
  531. $result = json_decode($result['response'],true);
  532. // var_dump("获取数据:",$result);
  533. if($result['data'] && $result['data']['results']){
  534. $dataList = $result['data']['results'];
  535. // var_dump("取数据结构体:",$dataList);
  536. foreach ($dataList as $val){
  537. // var_dump("进入循环插入:",$val);
  538. $newUrlStr = json_decode($val['source']['urls'],true);
  539. $newUrlStr = $other['web_url'].$newUrlStr['common'];
  540. // var_dump("来源地址:",$newUrlStr);
  541. $insertData = [
  542. 'newUrlStr'=>$newUrlStr,
  543. 'title'=>$val['source']['title']??'',
  544. 'source'=>$val['source']['contentSource']??'',
  545. 'copyfrom'=>$other['copyfrom']??'',
  546. 'content'=>$val['source']['content']['content']??'',
  547. 'admin_user_id'=>$other['admin_user_id']??'',
  548. 'rule_id'=>$other['rule_id']??'',
  549. 'author'=>$other['writer']??''
  550. ];
  551. // var_dump("调用插入数据方法,组装数据:",$insertData);
  552. $this->insertArticleData($insertData);
  553. }
  554. }
  555. $pages = intval($parames['current']);
  556. $pages = $pages+1;
  557. $parames['current'] = $pages;
  558. $twoResult = Result::http_post($wecUrl,$parames,$options);
  559. if($result['data'] && $result['data']['results'] && count($result['data']['results'])>0){
  560. // var_dump("分页测试:",$parames,$parames['current']);
  561. $this->foreachCurl($wecUrl,$parames,$other,$pages);
  562. }
  563. // var_dump("正确的数据:",$result);
  564. }
  565. /**
  566. * 获取并搜索资讯
  567. * @param array $data
  568. * @return array
  569. */
  570. public function getInfo(array $data): array
  571. {
  572. $where = [
  573. ['rule_id','=',$data['rule_id']]
  574. ];
  575. //若存在条件参数都存到where数组
  576. if(isset($data['title'])){
  577. $where[] = ['title','like','%'.$data['title'].'%'];
  578. }
  579. if(isset($data['source'])){
  580. $art_source = Article::where($where)->get();
  581. if(!empty($art_source->toArray())){
  582. $where[] = ['source','=',$data['source']];
  583. }
  584. }
  585. if(isset($data['state'])){
  586. $where[] = ['state','=',$data['state']];
  587. }
  588. //跨库查询栏目导航及采集的新闻
  589. $info = Article::query()
  590. ->where($where)
  591. ->with('category')
  592. ->orderBy("article.id","desc")
  593. ->limit($data['pageSize'])
  594. ->offset(($data['page']-1)*$data['pageSize'])->get();
  595. $count = Article::where($where)->count();
  596. if($count == 0){
  597. return Result::error('暂无资讯');
  598. }
  599. $data = [
  600. 'rep' => $info->toArray(),
  601. 'count' => $count
  602. ];
  603. return Result::success($data);
  604. }
  605. /**
  606. * 获取某个资讯
  607. * @param array $data
  608. * @return array
  609. */
  610. public function getOneInfo(array $data): array
  611. {
  612. $where = ['id' => $data['art_id']];
  613. $inf = Article::where($where)->first();
  614. if($inf==null){
  615. return Result::error('请输入正确的资讯id!');
  616. }
  617. $info = Article::where($where)
  618. ->leftJoin('article_data','article_id','id')
  619. ->select('article.*','article_data.content')
  620. ->first();
  621. if($inf['catid']!=null){
  622. $category = Category::where(['id'=>$info['catid']])->select('name')->first();
  623. $info['category'] = $category['name'];
  624. }
  625. return Result::success($info);
  626. }
  627. /**
  628. * 修改资讯
  629. * @param array $data
  630. * @return array
  631. */
  632. public function upInfo(array $data): array
  633. {
  634. $id = $data['art_id'];
  635. $content = $data['content'];
  636. unset($data['art_id']);
  637. //去掉此元素
  638. unset($data['content']);
  639. //去掉此元素
  640. $info = Article::where('id',$id)->first();
  641. if($info==null){
  642. return Result::error('请输入正确的文章id!');
  643. }
  644. if($info['state']==1){
  645. return Result::error('此文章已导入 ,不可编辑!');
  646. }else{
  647. Db::beginTransaction();
  648. try{
  649. $info = Article::where('id',$id)->update($data);
  650. $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);
  651. Db::commit();
  652. } catch(\Throwable $ex){
  653. Db::rollBack();
  654. var_dump($ex->getMessage());
  655. return Result::error("修改失败",0);
  656. }
  657. $data = [
  658. 'info' => $info,
  659. 'art_data' => $art_data
  660. ];
  661. return Result::success($data);
  662. }
  663. }
  664. /**
  665. * 删除资讯
  666. * @param array $data
  667. * @return array
  668. */
  669. public function delInfo(array $data): array
  670. {
  671. $id = $data['art_id'];
  672. $info = Article::where('id',$id)->first();
  673. if($info==null){
  674. return Result::error('请输入正确的文章id!');
  675. }
  676. if($info['state']==1){
  677. return Result::error('此文章已导入,不可删除!');
  678. }else{
  679. Db::beginTransaction();
  680. try{
  681. $delinfo = Article::where('id',$id)->delete();
  682. $deldata = ArticleData::where('article_id',$id)->delete();
  683. Db::commit();
  684. } catch(\Throwable $ex){
  685. Db::rollBack();
  686. var_dump($ex->getMessage());
  687. return Result::error("删除失败",0);
  688. }
  689. }
  690. $data = [
  691. 'delinfo' => $delinfo,
  692. 'deldata' => $deldata
  693. ];
  694. return Result::success($data);
  695. }
  696. /**
  697. * 关联导航池
  698. * @param array $data
  699. * @return array
  700. */
  701. public function addCatid(array $data): array
  702. {
  703. $id = $data['rule_id'];
  704. $art = Article::where('rule_id',$id)->select('id')->count();
  705. if($art==0){
  706. return Result::error('还未采集,请采集');
  707. }else{
  708. $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
  709. if(empty($info->toArray())){
  710. return Result::error('所有文章都已导入,不可修改关联的导航池!');
  711. }else{
  712. //查找此规则任务下的文章是否已经有导入的文章
  713. $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
  714. if(!empty($article->toArray())){
  715. //若有已导入的文章则直接复制之前已导入的导航池
  716. $catid = Article::whereIn('id',$article)->select('catid')->first();
  717. //若未导入的文章已经复制之前的导航,则无需修改
  718. $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
  719. if($art_catid>0){
  720. $result = Article::whereIn('id',$info)->update(['catid'=>$catid['catid']]);
  721. }else{
  722. $result = ['已全部关联导航,无需再次关联!'];
  723. }
  724. }else{
  725. //若不存在已导入的文章则判断是否存在导航id
  726. if(isset($data['catid'])){
  727. //若存在直接使用此导航id
  728. $result = Article::whereIn('id',$info)->update(['catid'=>$data['catid']]);
  729. }else{
  730. //若不存在则返回所有导航栏目
  731. $result = Category::select('id','name')->get();
  732. }
  733. }
  734. }
  735. }
  736. //查找此规则任务下的文章是否都已经导入
  737. return Result::success($result);
  738. }
  739. /**
  740. * 导入文章(生产者)
  741. * @param array $data
  742. * @return array
  743. */
  744. public function addArt(array $data): array
  745. {
  746. var_dump("接收到的数据:",$data);
  747. $message = new ImportProducer($data);
  748. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  749. $a = $producer->produce($message);
  750. var_dump("生产者:",$a);
  751. // $result = $this->Gservice->push($data,rand(5,20));
  752. return Result::success([]);
  753. }
  754. /**
  755. * 导入文章(消费者)
  756. * @param array $data
  757. * @return array
  758. */
  759. public function goAddArt(array $data): array
  760. {
  761. // var_dump('准备去消费------',$data);
  762. // var_dump("======@@@====");
  763. $where = [
  764. 'rule_id' => $data['rule_id'],
  765. 'state' => 0
  766. ];
  767. //获取某个规则任务下的已采集未导入的文章及文章详情
  768. $arts_id = Article::where($where)->wherenotNull('catid')->select('id')->orderBy('id')->get()->toArray();
  769. $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();
  770. // var_dump('=============:::',$arts_id);
  771. $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id','desc')->get()->toArray();
  772. // var_dump('=============',$arts);
  773. $data = [
  774. 'articles' => $arts,
  775. 'art_content' => $arts_data
  776. ];
  777. Db::beginTransaction();
  778. try{
  779. $oldart = OldArticle::insert($arts);
  780. $oldart_data = OldArticleData::insert($arts_data);
  781. $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
  782. Db::commit();
  783. } catch(\Throwable $ex){
  784. Db::rollBack();
  785. var_dump($ex->getMessage());
  786. return Result::error($ex->getMessage(),0);
  787. }
  788. return Result::success($data);
  789. }
  790. }