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