CollectorService.php 32 KB

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