CollectorService.php 31 KB

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