CollectorService.php 32 KB

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