CollectorService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. 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("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  240. }else{
  241. $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("created_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. switch ($info['web_type']){
  356. case 1:
  357. var_dump("===========规则采集======",$info);
  358. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  359. $data['copyfrom'] = $info['web_name'];
  360. $data['author'] = $info['writer'];;
  361. $data['first_url'] = $info['first_url'];
  362. $data['second_start'] = $info['second_start'];
  363. $data['second_num'] = $info['second_num'];
  364. $data['second_end'] = $info['second_end'];
  365. $data['end_pagenum']= $info['end_pagenum'];
  366. $data['rule_id']= $data['id'];
  367. $data['admin_user_id']= $data['admin_user_id'];
  368. // $data['newUrlStr'] =
  369. $urlList = $this->addUrlArr($data);
  370. if($urlList){
  371. foreach ($urlList as $val){
  372. // var_dump("单列表地址:",$val);
  373. $this->ruleCollection($val,$data);
  374. }
  375. }
  376. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  377. break;
  378. case 2:
  379. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  380. $wecUrl = $info['first_url'];//'https://www.ndcpa.gov.cn/queryList';
  381. $parames = json_decode($info['parameter'],true);
  382. // var_dump($parames);die;
  383. $parames['webSiteCode'] = [trim($parames['webSiteCode'], "[]")]; //['jbkzzx'];//
  384. $parames['channelCode'] = [trim($parames['channelCode'], "[]")]; // ['c100008'];//
  385. $other = [
  386. 'web_url'=>$info['web_url'],
  387. 'copyfrom'=>$info['web_name'],
  388. 'admin_user_id'=>$data['admin_user_id'],
  389. 'rule_id'=>$data['id'],
  390. 'writer'=>$info['writer'],
  391. ];
  392. var_dump("=======开始接口采集====",$parames);
  393. // die;
  394. $this->foreachCurl($wecUrl,$parames,$other);
  395. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  396. }
  397. return Result::success([]);
  398. }
  399. /**
  400. * 把可采集的列表页连接 打包成一个大数组
  401. * @return void
  402. */
  403. public function addUrlArr($data)
  404. {
  405. $arrList = [];
  406. array_push($arrList,$data['first_url']);
  407. $exit = false;
  408. $i = 0;
  409. while(!$exit){
  410. $i++;
  411. $url = $data['second_start'].$i.$data['second_end'];
  412. $respon1 = Result::pageExists($url);
  413. // var_dump("采集地址:",$respon1,$url);
  414. // Coroutine::sleep(2);
  415. if ($i==intval($data['end_pagenum'])-1) {
  416. $exit = true;
  417. // Coroutine::exit(); // 退出循环
  418. }else{
  419. array_push($arrList,$url);
  420. }
  421. }
  422. return $arrList;
  423. }
  424. /**
  425. * 按照规则采集数据
  426. * @return void
  427. */
  428. public function ruleCollection($url,$data)
  429. {
  430. // var_dump("采集参数:",$data);
  431. $list = QueryList::get($url);
  432. $dataList = $list->rules([
  433. 'title' => ['a', 'text'],
  434. 'link' => ['a', 'href'],
  435. ])->range('.list1 li')->query()->getData();
  436. // var_dump("采集的内容:",$dataList);
  437. // var_dump("====",$dataList);die;
  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. $newUrl = substr($tiem['link'], 1);
  445. $newUrlStr = $firstUrlArr.$newUrl;
  446. $detailContent = QueryList::get($newUrlStr);
  447. $detailData = $detailContent->rules([
  448. 'title'=>['h1','text'],
  449. 'content'=>['.TRS_UEDITOR','html'],
  450. ])->range(".news-details")->query()->getData();
  451. $detailData = $detailData->toArray();
  452. // var_dump("内容详情:",$detailData,$newUrlStr);
  453. if($detailData){
  454. foreach ($detailData as $val){
  455. // var_dump("进没进foreach:",$newUrlStr,$val);
  456. $data['fromurl'] = $newUrlStr;
  457. $data['title'] = $val['title'];
  458. $data['content'] = $val['content'];
  459. $data['newUrlStr'] = $newUrlStr;
  460. $data['source'] = '';
  461. $data['introduce'] = $val['title']??'';
  462. $data['keyword'] = $val['title']??'';
  463. $data['copyfrom'] = $data['copyfrom'];
  464. $data['source'] = $data['source']??$data['copyfrom'];
  465. $data['admin_user_id'] = $data['admin_user_id']??'';
  466. $data['rule_id'] = $data['rule_id']??'';
  467. // $data['copyfrom'] = $data['copyfrom'];
  468. // var_dump("要插入的数据:",$data);
  469. $this->insertArticleData($data);
  470. }
  471. }
  472. }
  473. //
  474. }
  475. }
  476. /**
  477. * 插入数据
  478. * @param $data
  479. * @return void
  480. */
  481. public function insertArticleData($data=[])
  482. {
  483. if($data){
  484. Db::beginTransaction();
  485. try{
  486. $articleInfo = Article::where(['title'=>$data['title']])->first();
  487. // var_dump("获取详情:",$articleInfo,$data);
  488. if(empty($articleInfo)){
  489. $insertData = [];
  490. $insertData['fromurl'] =$data['newUrlStr'];
  491. $insertData['oldtitle'] =$data['title'];
  492. $insertData['title'] = $data['title'];
  493. $insertData['copyfrom'] = $data['copyfrom'];
  494. $insertData['author'] = $data['author'];
  495. $insertData['introduce'] = $data['title'];
  496. $insertData['keyword'] = $data['title'];
  497. $insertData['source'] = isset($data['source']) && $data['source']!=''? $data['source']:$data['copyfrom'];
  498. $insertData['admin_user_id'] = $data['admin_user_id'];
  499. $insertData['rule_id'] = $data['rule_id'];
  500. // var_dump("插入Article:",$insertData);
  501. $article_id = Article::insertGetId($insertData);
  502. $insertDataDetail = [];
  503. $insertDataDetail['article_id'] = $article_id;
  504. $insertDataDetail['content'] = $data['content'];
  505. // var_dump("插入ArticleData:",$insertDataDetail);
  506. ArticleData::insertGetId($insertDataDetail);
  507. // Coroutine::sleep(2);
  508. // var_dump("插入成功一次:",$article_id,$insertDataDetail);
  509. }
  510. Db::commit();
  511. }catch (\Exception $e){
  512. Db::rollBack();
  513. var_dump("插入失败:",$e->getMessage());
  514. }
  515. }else{
  516. var_dump("没有数据可以插入:");
  517. }
  518. }
  519. /**
  520. * 分页采集
  521. * @return void
  522. */
  523. public function foreachCurl($wecUrl,$parames,$other,&$page=1)
  524. {
  525. $options = [
  526. CURLOPT_HEADER => true, // 如果想包含头部信息在响应中,可以设置为true
  527. CURLOPT_TIMEOUT => 30 // 设置请求超时时间为30秒
  528. ];
  529. $result = Result::http_post($wecUrl,$parames,$options);
  530. $result = json_decode($result['response'],true);
  531. // var_dump("获取数据:",$result);
  532. if($result['data'] && $result['data']['results']){
  533. $dataList = $result['data']['results'];
  534. // var_dump("取数据结构体:",$dataList);
  535. foreach ($dataList as $val){
  536. // var_dump("进入循环插入:",$val);
  537. $newUrlStr = json_decode($val['source']['urls'],true);
  538. $newUrlStr = $other['web_url'].$newUrlStr['common'];
  539. // var_dump("来源地址:",$newUrlStr);
  540. $insertData = [
  541. 'newUrlStr'=>$newUrlStr,
  542. 'title'=>$val['source']['title']??'',
  543. 'source'=>$val['source']['contentSource']??'',
  544. 'copyfrom'=>$other['copyfrom']??'',
  545. 'content'=>$val['source']['content']['content']??'',
  546. 'admin_user_id'=>$other['admin_user_id']??'',
  547. 'rule_id'=>$other['rule_id']??'',
  548. 'author'=>$other['writer']??''
  549. ];
  550. // var_dump("调用插入数据方法,组装数据:",$insertData);
  551. $this->insertArticleData($insertData);
  552. }
  553. }
  554. $pages = intval($parames['current']);
  555. $pages = $pages+1;
  556. $parames['current'] = $pages;
  557. $twoResult = Result::http_post($wecUrl,$parames,$options);
  558. if($result['data'] && $result['data']['results'] && count($result['data']['results'])>0){
  559. // var_dump("分页测试:",$parames,$parames['current']);
  560. $this->foreachCurl($wecUrl,$parames,$other,$pages);
  561. }
  562. // var_dump("正确的数据:",$result);
  563. }
  564. /**
  565. * 获取并搜索资讯
  566. * @param array $data
  567. * @return array
  568. */
  569. public function getInfo(array $data): array
  570. {
  571. $where = [
  572. ['rule_id','=',$data['rule_id']]
  573. ];
  574. //若存在条件参数都存到where数组
  575. if(isset($data['title'])){
  576. $where[] = ['title','like','%'.$data['title'].'%'];
  577. }
  578. if(isset($data['source'])){
  579. $art_source = Article::where($where)->get();
  580. if(!empty($art_source->toArray())){
  581. $where[] = ['source','=',$data['source']];
  582. }
  583. }
  584. if(isset($data['state'])){
  585. $where[] = ['state','=',$data['state']];
  586. }
  587. //跨库查询栏目导航及采集的新闻
  588. $info = Article::query()
  589. ->where($where)
  590. ->with('category')
  591. ->orderBy("article.id","desc")
  592. ->limit($data['pageSize'])
  593. ->offset(($data['page']-1)*$data['pageSize'])->get();
  594. $count = Article::where($where)->count();
  595. if($count == 0){
  596. return Result::error('暂无资讯');
  597. }
  598. $data = [
  599. 'rep' => $info->toArray(),
  600. 'count' => $count
  601. ];
  602. return Result::success($data);
  603. }
  604. /**
  605. * 获取某个资讯
  606. * @param array $data
  607. * @return array
  608. */
  609. public function getOneInfo(array $data): array
  610. {
  611. $where = ['id' => $data['art_id']];
  612. $inf = Article::where($where)->first();
  613. if($inf==null){
  614. return Result::error('请输入正确的资讯id!');
  615. }
  616. $info = Article::where($where)
  617. ->leftJoin('article_data','article_id','id')
  618. ->select('article.*','article_data.content')
  619. ->first();
  620. if($inf['catid']!=null){
  621. $category = Category::where(['id'=>$info['catid']])->select('name')->first();
  622. $info['category'] = $category['name'];
  623. }
  624. return Result::success($info);
  625. }
  626. /**
  627. * 修改资讯
  628. * @param array $data
  629. * @return array
  630. */
  631. public function upInfo(array $data): array
  632. {
  633. $id = $data['art_id'];
  634. $content = $data['content'];
  635. unset($data['art_id']);
  636. //去掉此元素
  637. unset($data['content']);
  638. //去掉此元素
  639. $info = Article::where('id',$id)->first();
  640. if($info==null){
  641. return Result::error('请输入正确的文章id!');
  642. }
  643. if($info['state']==1){
  644. return Result::error('此文章已导入 ,不可编辑!');
  645. }else{
  646. Db::beginTransaction();
  647. try{
  648. $info = Article::where('id',$id)->update($data);
  649. $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);
  650. Db::commit();
  651. } catch(\Throwable $ex){
  652. Db::rollBack();
  653. var_dump($ex->getMessage());
  654. return Result::error("修改失败",0);
  655. }
  656. $data = [
  657. 'info' => $info,
  658. 'art_data' => $art_data
  659. ];
  660. return Result::success($data);
  661. }
  662. }
  663. /**
  664. * 删除资讯
  665. * @param array $data
  666. * @return array
  667. */
  668. public function delInfo(array $data): array
  669. {
  670. $id = $data['art_id'];
  671. $info = Article::where('id',$id)->first();
  672. if($info==null){
  673. return Result::error('请输入正确的文章id!');
  674. }
  675. if($info['state']==1){
  676. return Result::error('此文章已导入,不可删除!');
  677. }else{
  678. Db::beginTransaction();
  679. try{
  680. $delinfo = Article::where('id',$id)->delete();
  681. $deldata = ArticleData::where('article_id',$id)->delete();
  682. Db::commit();
  683. } catch(\Throwable $ex){
  684. Db::rollBack();
  685. var_dump($ex->getMessage());
  686. return Result::error("删除失败",0);
  687. }
  688. }
  689. $data = [
  690. 'delinfo' => $delinfo,
  691. 'deldata' => $deldata
  692. ];
  693. return Result::success($data);
  694. }
  695. /**
  696. * 关联导航池
  697. * @param array $data
  698. * @return array
  699. */
  700. public function addCatid(array $data): array
  701. {
  702. $id = $data['rule_id'];
  703. $art = Article::where('rule_id',$id)->select('id')->count();
  704. if($art==0){
  705. return Result::error('还未采集,请采集');
  706. }else{
  707. $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
  708. if(empty($info->toArray())){
  709. return Result::error('所有文章都已导入,不可修改关联的导航池!');
  710. }else{
  711. //查找此规则任务下的文章是否已经有导入的文章
  712. $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
  713. if(!empty($article->toArray())){
  714. //若有已导入的文章则直接复制之前已导入的导航池
  715. $catid = Article::whereIn('id',$article)->select('catid')->first();
  716. //若未导入的文章已经复制之前的导航,则无需修改
  717. $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
  718. if($art_catid>0){
  719. $result = Article::whereIn('id',$info)->update(['catid'=>$catid['catid']]);
  720. }else{
  721. $result = ['已全部关联导航,无需再次关联!'];
  722. }
  723. }else{
  724. //若不存在已导入的文章则判断是否存在导航id
  725. if(isset($data['catid'])){
  726. //若存在直接使用此导航id
  727. $result = Article::whereIn('id',$info)->update(['catid'=>$data['catid']]);
  728. }else{
  729. //若不存在则返回所有导航栏目
  730. $result = Category::select('id','name')->get();
  731. }
  732. }
  733. }
  734. }
  735. //查找此规则任务下的文章是否都已经导入
  736. return Result::success($result);
  737. }
  738. /**
  739. * 导入文章(生产者)
  740. * @param array $data
  741. * @return array
  742. */
  743. public function addArt(array $data): array
  744. {
  745. var_dump("接收到的数据:",$data);
  746. $message = new ImportProducer($data);
  747. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  748. $a = $producer->produce($message);
  749. var_dump("生产者:",$a);
  750. // $result = $this->Gservice->push($data,rand(5,20));
  751. return Result::success([]);
  752. }
  753. /**
  754. * 导入文章(消费者)
  755. * @param array $data
  756. * @return array
  757. */
  758. public function goAddArt(array $data): array
  759. {
  760. // var_dump('准备去消费------',$data);
  761. // var_dump("======@@@====");
  762. $where = [
  763. 'rule_id' => $data['rule_id'],
  764. 'state' => 0
  765. ];
  766. //获取某个规则任务下的已采集未导入的文章及文章详情
  767. $arts_id = Article::where($where)->wherenotNull('catid')->select('id')->orderBy('id')->get()->toArray();
  768. $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();
  769. // var_dump('=============:::',$arts_id);
  770. $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id','desc')->get()->toArray();
  771. // var_dump('=============',$arts);
  772. $data = [
  773. 'articles' => $arts,
  774. 'art_content' => $arts_data
  775. ];
  776. Db::beginTransaction();
  777. try{
  778. $oldart = OldArticle::insert($arts);
  779. $oldart_data = OldArticleData::insert($arts_data);
  780. $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
  781. Db::commit();
  782. } catch(\Throwable $ex){
  783. Db::rollBack();
  784. var_dump($ex->getMessage());
  785. return Result::error($ex->getMessage(),0);
  786. }
  787. return Result::success($data);
  788. }
  789. }