CollectorService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\ArticleData;
  4. use App\Model\OldModel\Article as OldArticle;
  5. use App\Model\OldModel\ArticleData as OldArticleData;
  6. use App\Model\OldModel\Category;
  7. use App\Model\Article;
  8. use App\Model\Rule;
  9. use App\Model\Web;
  10. use Hyperf\DbConnection\Db;
  11. use Hyperf\RpcServer\Annotation\RpcService;
  12. use App\Tools\Result;
  13. use QL\QueryList;
  14. use Swoole\Coroutine;
  15. use function Hyperf\Support\retry;
  16. #[RpcService(name: "CollectorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  17. class CollectorService implements CollectorServiceInterface
  18. {
  19. /**
  20. * 添加网站
  21. * @param array $data
  22. * @return array|mixed
  23. */
  24. public function addWeb(array $data): array
  25. {
  26. $where = [
  27. 'name' => $data['name']
  28. ];
  29. $isweb = Web::where($where)->first();
  30. if(empty($isweb)){
  31. date_default_timezone_set('Asia/Shanghai');
  32. $web = Web::insert($data);
  33. }else{
  34. return Result::error('此网站已存在,不可重复添加!');
  35. }
  36. if(empty($web)){
  37. return Result::error('添加失败');
  38. }
  39. return Result::success('添加成功');
  40. }
  41. /**
  42. * 获取并搜索网站
  43. * @param array $data
  44. * @return array|mixed
  45. */
  46. public function getWeb(array $data): array
  47. {
  48. if(isset($data['keyWord'])){
  49. $where = [
  50. ['name','like','%'.$data['keyWord'].'%']
  51. ];
  52. $rep = Web::where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  53. $count = Web::where($where)->count();
  54. if($count==0){
  55. return Result::error('未查找到相关网站!');
  56. }
  57. }else{
  58. $rep = Web::limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  59. $count = Web::count();
  60. }
  61. $data = [
  62. 'rep' => $rep->toArray(),
  63. 'count' => $count
  64. ];
  65. if(empty($rep)){
  66. return Result::error('您还未添加网站,请先去添加!');
  67. }
  68. return Result::success($data);
  69. }
  70. /**
  71. * 修改网站
  72. * @param array $data
  73. * @return array|mixed
  74. */
  75. public function upWeb(array $data): array
  76. {
  77. $web = Web::where('id',$data['id'])->first();
  78. if(empty($web)){
  79. return Result::error('请输入正确的网站id!');
  80. }else{
  81. date_default_timezone_set('Asia/Shanghai');
  82. $id = Web::where('id',$data['id'])->update($data);
  83. if(empty($id)){
  84. return Result::error('无法修改!');
  85. }
  86. }
  87. return Result::success($id);
  88. }
  89. /**
  90. * 删除网站
  91. * @param array $data
  92. * @return array|mixed
  93. */
  94. public function delWeb(array $data): array
  95. {
  96. $web = Web::where('id',$data['id'])->first();
  97. if(empty($web)){
  98. return Result::error('请输入正确的网站id!');
  99. }else{
  100. $where = [
  101. ['web_id','=',$data['id']]
  102. ];
  103. //判断此网站下是否规则任务
  104. $rule = Rule::where($where)->get();
  105. if(empty($rule)){
  106. //若没有直接删除网站
  107. $result['web'] = Web::where('id',$data['id'])->delete();
  108. }else{
  109. //若有,判断规则任务是否有已执行的
  110. $rule = Rule::where($where)->where('status',2)->get();
  111. // return Result::success($rule);
  112. if(!empty($rule->toArray())){
  113. //若有已执行的任务规则,不可删除网站
  114. return Result::error('该网站已有成功执行的任务规则,不可删除!');
  115. }else{
  116. try {
  117. Db::beginTransaction();
  118. //若无已执行的任务规则,删除网站及相应的未执行的规则任务
  119. $result['web'] = Web::where('id',$data['id'])->delete();
  120. $result['rule'] = Rule::where($where)->delete();
  121. Db::commit();
  122. } catch(\Throwable $ex){
  123. Db::rollBack();
  124. var_dump($ex->getMessage());
  125. return Result::error("删除失败",0);
  126. }
  127. }
  128. }
  129. }
  130. return Result::success($result);
  131. }
  132. /**
  133. * 添加任务规则
  134. * @param array $data
  135. * @return array|mixed
  136. */
  137. public function addRule(array $data): array
  138. {
  139. $web = Web::where('id',$data['web_id'])->get();
  140. if(empty($web->toArray())){
  141. return Result::error('请输入正确的网站id!');
  142. }else{
  143. $rulename = Rule::where('name',$data['name'])->get();
  144. //查找是否存在规则名称重复的
  145. if(empty($rulename->toArray())){
  146. //(若是多类型参数一起传过来则根据类型,只获取对应类型需要的参数)
  147. switch($data['type']){
  148. case 1:
  149. $rule = [
  150. 'name' => $data['name'],
  151. 'web_id' => $data['web_id'],
  152. 'first_url' => $data['first_url'],
  153. 'second_start' => $data['second_start'],
  154. 'second_num' => $data['second_num'],
  155. 'second_end' => $data['second_end'],
  156. 'end_pagenum' => $data['end_pagenum'],
  157. 'start' => $data['start'],
  158. 'title' => $data['title'],
  159. 'content' => $data['content']
  160. ];
  161. // var_dump("============1============");
  162. break;
  163. case 2:
  164. $rule = [
  165. 'name' => $data['name'],
  166. 'web_id' => $data['web_id'],
  167. 'first_url' => $data['first_url'],
  168. 'parameter' => $data['parameter'],
  169. 'start' => $data['start'],
  170. 'title' => $data['title'],
  171. 'content' => $data['content']
  172. ];
  173. // var_dump("============2============");
  174. break;
  175. default:
  176. $rule = [
  177. 'name' => $data['name'],
  178. 'web_id' => $data['web_id'],
  179. 'diy_rule' => $data['diy_rule']
  180. ];
  181. // var_dump("============3============");
  182. break;
  183. }
  184. if(isset($data['source']) && $data['type'] != 3){
  185. $rule ['source'] = $data['source'];
  186. }
  187. if(isset($data['writer_class']) && $data['type'] != 3){
  188. $rule ['writer_class'] = $data['writer_class'];
  189. }
  190. if(isset($data['writer']) && $data['type'] != 3){
  191. $rule ['writer'] = $data['writer'];
  192. }
  193. date_default_timezone_set('Asia/Shanghai');
  194. //若不存在,根据网站类型添加到不行类型的规则表中
  195. $result = Rule::insertGetId($rule);
  196. }else{
  197. return Result::error('此任务已存在!');
  198. }
  199. }
  200. return Result::success($result);
  201. }
  202. /**
  203. * 获取并搜索规则任务
  204. * @param array $data
  205. * @return array|mixed
  206. */
  207. public function getRule(array $data): array
  208. {
  209. if(isset($data['web_id'])){
  210. $web = Web::where('id',$data['web_id'])->get();
  211. if(empty($web->toArray())){
  212. return Result::error('请输入正确的网站id!');
  213. }else{
  214. $where = [
  215. ['web_id','=', $data['web_id']]
  216. ];
  217. }
  218. }
  219. if(isset($data['keyWord'])){
  220. //若存在搜索词,则存到条件数组$where中
  221. $where = [
  222. ['name','like','%'.$data['keyWord'].'%']
  223. ];
  224. }
  225. $rep = Rule::withCount(relations:'arts')->where($where)->limit($data['pageSize'])->orderBy("created_at","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  226. $count = Rule::where($where)->count();
  227. if($count==0){
  228. return Result::error('暂无相关规则任务!');
  229. }
  230. $data = [
  231. 'rep' => $rep->toArray(),
  232. 'count' => $count
  233. ];
  234. return Result::success($data);
  235. }
  236. /**
  237. * 获取某个任务规则
  238. * @param array $data
  239. * @return array|mixed
  240. */
  241. public function getOneRule(array $data): array
  242. {
  243. $result = Rule::where('id',$data['id'])->first();
  244. if(empty($result)){
  245. return Result::error('请输入正确的规则任务id!');
  246. }else{
  247. return Result::success($result);
  248. }
  249. }
  250. /**
  251. * 修改规则任务
  252. * @param array $data
  253. * @return array|mixed
  254. */
  255. public function upRule(array $data): array
  256. {
  257. $rule = Rule::where('id',$data['id'])->select('id')->first();
  258. unset($data['type']);
  259. if(empty($rule)){
  260. return Result::error('请输入正确的规则任务id!');
  261. }else{
  262. $rulename = Rule::where('id','!=',$rule['id'])->where('name',$data['name'])->select('name')->first();
  263. if(empty($rulename)){
  264. $result = Rule::where('id',$data['id'])->update($data);
  265. }else{
  266. return Result::error('已存在此任务规则名称!');
  267. }
  268. }
  269. return Result::success($result);
  270. }
  271. /**
  272. * 删除规则任务
  273. * @param array $data
  274. * @return array
  275. */
  276. public function delRule(array $data): array
  277. {
  278. $where = ['id' => $data['rule_id']];
  279. $rule = Rule::where($where)->first();
  280. if(empty($rule)){
  281. return Result::error('请输入正确的规则任务id!');
  282. }else{
  283. //查找是否存在已导入的文章
  284. $art_num = Article::where('rule_id',$data['rule_id'])->where('state',1)->count();
  285. if($art_num==0){
  286. //查找是否存在已采集但是未导入的文章
  287. $colart_num = Article::where('rule_id',$data['rule_id'])->where('state',0)->count();
  288. if($colart_num==0){
  289. $result['rule'] = Rule::where($where)->delete();
  290. }else{
  291. try {
  292. Db::beginTransaction();
  293. //若有已采集但未导入的文章,删除规则任务及相应的未导入的文章
  294. $result['rule'] = Rule::where($where)->delete();
  295. $result['art'] = Article::where('rule_id',$data['rule_id'])->delete();
  296. Db::commit();
  297. } catch(\Throwable $ex){
  298. Db::rollBack();
  299. var_dump($ex->getMessage());
  300. return Result::error("删除失败",0);
  301. }
  302. }
  303. }else{
  304. return Result::error('此规则任务下的文章已导入,不可删除!');
  305. }
  306. }
  307. return Result::success($result);
  308. }
  309. /**
  310. * 开始采集
  311. * @param array $data
  312. * @return array
  313. */
  314. public function sendCrawler(array $data): array
  315. {
  316. //通过规则id 查询规则类型
  317. $where = [
  318. 'rule.id'=>$data['id']
  319. ];
  320. $info = Rule::where($where)->leftJoin('web','rule.web_id','web.id')
  321. ->select("rule.*","web.name as web_name","web.url as web_url","web.type as web_type")
  322. ->first();
  323. $info = $info->toArray();
  324. switch ($info['web_type']){
  325. case 1:
  326. var_dump("wojinlailaile======",$info);
  327. Rule::where(['id'=>$data['id']])->update(['status'=>1]);
  328. $data['copyfrom'] = $info['web_name'];
  329. $data['author'] = '刘德华';
  330. $data['first_url'] = $info['first_url'];
  331. $data['second_start'] = $info['second_start'];
  332. $data['second_num'] = $info['second_num'];
  333. $data['second_end'] = $info['second_end'];
  334. $data['end_pagenum']= $info['end_pagenum'];
  335. $data['rule_id']= $data['id'];
  336. $data['admin_user_id']= $data['admin_user_id'];
  337. // $data['newUrlStr'] =
  338. $urlList = $this->addUrlArr($data);
  339. if($urlList){
  340. foreach ($urlList as $val){
  341. var_dump("单列表地址:",$val);
  342. $this->ruleCollection($val,$data);
  343. }
  344. }
  345. Rule::where(['id'=>$data['id']])->update(['status'=>2]);
  346. break;
  347. case 2:
  348. $wecUrl = $info['first_url'];//'https://www.ndcpa.gov.cn/queryList';
  349. $parames = json_decode($info['parameter'],true);
  350. $parames['webSiteCode'] = [trim($parames['webSiteCode'], "[]")]; //['jbkzzx'];//
  351. $parames['channelCode'] = [trim($parames['channelCode'], "[]")]; // ['c100008'];//
  352. $other = [
  353. 'web_url'=>$info['web_url'],
  354. 'copyfrom'=>$info['web_name'],
  355. 'admin_user_id'=>$data['admin_user_id'],
  356. 'rule_id'=>$data['id']
  357. ];
  358. var_dump("开始调用接口方法====",$parames);
  359. // die;
  360. $this->foreachCurl($wecUrl,$parames,$other);
  361. }
  362. return Result::success([]);
  363. }
  364. /**
  365. * 把可采集的列表页连接 打包成一个大数组
  366. * @return void
  367. */
  368. public function addUrlArr($data)
  369. {
  370. $arrList = [];
  371. array_push($arrList,$data['first_url']);
  372. $exit = false;
  373. $i = 0;
  374. while(!$exit){
  375. $i++;
  376. $url = $data['second_start'].$i.$data['second_end'];
  377. $respon1 = Result::pageExists($url);
  378. var_dump("采集地址:",$respon1,$url);
  379. // Coroutine::sleep(2);
  380. if ($i==intval($data['end_pagenum'])-1) {
  381. $exit = true;
  382. // Coroutine::exit(); // 退出循环
  383. }else{
  384. array_push($arrList,$url);
  385. }
  386. }
  387. return $arrList;
  388. }
  389. /**
  390. * 按照规则采集数据
  391. * @return void
  392. */
  393. public function ruleCollection($url,$data)
  394. {
  395. var_dump("采集参数:",$data);
  396. $list = QueryList::get($url);
  397. $dataList = $list->rules([
  398. 'title' => ['a', 'text'],
  399. 'link' => ['a', 'href'],
  400. ])->range('.list1 li')->query()->getData();
  401. var_dump("采集的内容:",$dataList);
  402. // var_dump("====",$dataList);die;
  403. $firstUrlArr = explode("/", $url);
  404. array_pop($firstUrlArr);
  405. $firstUrlArr = implode('/',$firstUrlArr);
  406. $dataList = $dataList->toArray();
  407. if($dataList){
  408. foreach ($dataList as $tiem){
  409. $newUrl = substr($tiem['link'], 1);
  410. $newUrlStr = $firstUrlArr.$newUrl;
  411. $detailContent = QueryList::get($newUrlStr);
  412. $detailData = $detailContent->rules([
  413. 'title'=>['h1','text'],
  414. 'content'=>['.TRS_UEDITOR','html'],
  415. ])->range(".news-details")->query()->getData();
  416. $detailData = $detailData->toArray();
  417. var_dump("内容详情:",$detailData,$newUrlStr);
  418. if($detailData){
  419. foreach ($detailData as $val){
  420. var_dump("进没进foreach:",$newUrlStr,$val);
  421. $data['fromurl'] = $newUrlStr;
  422. $data['title'] = $val['title'];
  423. $data['content'] = $val['content'];
  424. $data['newUrlStr'] = $newUrlStr;
  425. $data['source'] = '';
  426. $data['introduce'] = $val['title']??'';
  427. $data['keyword'] = $val['title']??'';
  428. $data['copyfrom'] = $data['copyfrom'];
  429. $data['source'] = $data['source']??$data['copyfrom'];
  430. $data['admin_user_id'] = $data['admin_user_id']??'';
  431. $data['rule_id'] = $data['rule_id']??'';
  432. // $data['copyfrom'] = $data['copyfrom'];
  433. var_dump("要插入的数据:",$data);
  434. $this->insertArticleData($data);
  435. }
  436. }
  437. }
  438. //
  439. }
  440. }
  441. /**
  442. * 插入数据
  443. * @param $data
  444. * @return void
  445. */
  446. public function insertArticleData($data=[])
  447. {
  448. if($data){
  449. Db::beginTransaction();
  450. try{
  451. $articleInfo = Article::where(['title'=>$data['title']])->first();
  452. var_dump("获取详情:",$articleInfo,$data);
  453. if(empty($articleInfo)){
  454. $insertData = [];
  455. $insertData['fromurl'] =$data['newUrlStr'];
  456. $insertData['oldtitle'] =$data['title'];
  457. $insertData['title'] = $data['title'];
  458. $insertData['copyfrom'] = $data['copyfrom'];
  459. $insertData['author'] = $data['author'];
  460. $insertData['introduce'] = $data['title'];
  461. $insertData['keyword'] = $data['title'];
  462. $insertData['source'] = isset($data['source']) && $data['source']!=''? $data['source']:$data['copyfrom'];
  463. $insertData['admin_user_id'] = $data['admin_user_id'];
  464. $insertData['rule_id'] = $data['rule_id'];
  465. // var_dump("插入Article:",$insertData);
  466. $article_id = Article::insertGetId($insertData);
  467. $insertDataDetail = [];
  468. $insertDataDetail['article_id'] = $article_id;
  469. $insertDataDetail['content'] = $data['content'];
  470. var_dump("插入ArticleData:",$insertDataDetail);
  471. ArticleData::insertGetId($insertDataDetail);
  472. // Coroutine::sleep(2);
  473. // var_dump("插入成功一次:",$article_id,$insertDataDetail);
  474. }
  475. Db::commit();
  476. }catch (\Exception $e){
  477. Db::rollBack();
  478. var_dump("插入失败:",$e->getMessage());
  479. }
  480. }else{
  481. var_dump("没有数据可以插入:");
  482. }
  483. }
  484. /**
  485. * 分页采集
  486. * @return void
  487. */
  488. public function foreachCurl($wecUrl,$parames,$other,&$page=1)
  489. {
  490. $options = [
  491. CURLOPT_HEADER => true, // 如果想包含头部信息在响应中,可以设置为true
  492. CURLOPT_TIMEOUT => 30 // 设置请求超时时间为30秒
  493. ];
  494. $result = Result::http_post($wecUrl,$parames,$options);
  495. $result = json_decode($result['response'],true);
  496. var_dump("获取数据:",$result);
  497. if($result['data'] && $result['data']['results']){
  498. $dataList = $result['data']['results'];
  499. // var_dump("取数据结构体:",$dataList);
  500. foreach ($dataList as $val){
  501. // var_dump("进入循环插入:",$val);
  502. $newUrlStr = json_decode($val['source']['urls'],true);
  503. $newUrlStr = $other['web_url'].$newUrlStr['common'];
  504. // var_dump("来源地址:",$newUrlStr);
  505. $insertData = [
  506. 'newUrlStr'=>$newUrlStr,
  507. 'title'=>$val['source']['title']??'',
  508. 'source'=>$val['source']['contentSource']??'',
  509. 'copyfrom'=>$other['copyfrom']??'',
  510. 'content'=>$val['source']['content']['content']??'',
  511. 'admin_user_id'=>$other['admin_user_id']??'',
  512. 'rule_id'=>$other['rule_id']??'',
  513. 'author'=>'冯蕊'
  514. ];
  515. // var_dump("调用插入数据方法,组装数据:",$insertData);
  516. $this->insertArticleData($insertData);
  517. }
  518. }
  519. $pages = intval($parames['current']);
  520. $pages = $pages+1;
  521. $parames['current'] = $pages;
  522. $twoResult = Result::http_post($wecUrl,$parames,$options);
  523. if($result['data'] && $result['data']['results'] && count($result['data']['results'])>0){
  524. var_dump("分页测试:",$parames,$parames['current']);
  525. $this->foreachCurl($wecUrl,$parames,$other,$pages);
  526. }
  527. // var_dump("正确的数据:",$result);
  528. }
  529. /**
  530. * 获取并搜索资讯
  531. * @param array $data
  532. * @return array
  533. */
  534. public function getInfo(array $data): array
  535. {
  536. $where = [
  537. ['rule_id','=',$data['rule_id']]
  538. ];
  539. //若存在条件参数都存到where数组
  540. if(isset($data['title'])){
  541. $where[] = ['title','like','%'.$data['title'].'%'];
  542. }
  543. if(isset($data['source'])){
  544. $art_source = Article::where($where)->get();
  545. if(!empty($art_source->toArray())){
  546. $where[] = ['source','=',$data['source']];
  547. }
  548. }
  549. if(isset($data['state'])){
  550. $where[] = ['state','=',$data['state']];
  551. }
  552. //跨库查询栏目导航及采集的新闻
  553. $info = Article::query()
  554. ->where($where)
  555. ->with(['category' => function ($query) {
  556. $query->select('name');
  557. }])
  558. ->orderBy("article.id","desc")
  559. ->limit($data['pageSize'])
  560. ->offset(($data['page']-1)*$data['pageSize'])->get();
  561. $count = Article::where($where)->count();
  562. if($count == 0){
  563. return Result::error('暂无资讯');
  564. }
  565. $data = [
  566. 'rep' => $info->toArray(),
  567. 'count' => $count
  568. ];
  569. return Result::success($data);
  570. }
  571. /**
  572. * 获取某个资讯
  573. * @param array $data
  574. * @return array
  575. */
  576. public function getOneInfo(array $data): array
  577. {
  578. $where = ['id' => $data['art_id']];
  579. $inf = Article::where($where)->first();
  580. if($inf==null){
  581. return Result::error('请输入正确的资讯id!');
  582. }
  583. $info = Article::where($where)
  584. ->leftJoin('article_data','article_id','id')
  585. ->select('article.*','article_data.content')
  586. ->first();
  587. if($inf['catid']!=null){
  588. $category = Category::where(['id'=>$info['catid']])->select('name')->first();
  589. $info['category'] = $category['name'];
  590. }
  591. return Result::success($info);
  592. }
  593. /**
  594. * 修改资讯
  595. * @param array $data
  596. * @return array
  597. */
  598. public function upInfo(array $data): array
  599. {
  600. $id = $data['art_id'];
  601. $content = $data['content'];
  602. unset($data['art_id']);
  603. //去掉此元素
  604. unset($data['content']);
  605. //去掉此元素
  606. $info = Article::where('id',$id)->first();
  607. if($info==null){
  608. return Result::error('请输入正确的文章id!');
  609. }
  610. if($info['state']==1){
  611. return Result::error('此文章已导入 ,不可编辑!');
  612. }else{
  613. Db::beginTransaction();
  614. try{
  615. $info = Article::where('id',$id)->update($data);
  616. $art_data = ArticleData::where('article_id',$id)->update(['content'=>$content]);
  617. Db::commit();
  618. } catch(\Throwable $ex){
  619. Db::rollBack();
  620. var_dump($ex->getMessage());
  621. return Result::error("修改失败",0);
  622. }
  623. $data = [
  624. 'info' => $info,
  625. 'art_data' => $art_data
  626. ];
  627. return Result::success($data);
  628. }
  629. }
  630. /**
  631. * 删除资讯
  632. * @param array $data
  633. * @return array
  634. */
  635. public function delInfo(array $data): array
  636. {
  637. $id = $data['art_id'];
  638. $info = Article::where('id',$id)->first();
  639. if($info==null){
  640. return Result::error('请输入正确的文章id!');
  641. }
  642. if($info['state']==1){
  643. return Result::error('此文章已导入,不可删除!');
  644. }else{
  645. Db::beginTransaction();
  646. try{
  647. $delinfo = Article::where('id',$id)->delete();
  648. $deldata = ArticleData::where('article_id',$id)->delete();
  649. Db::commit();
  650. } catch(\Throwable $ex){
  651. Db::rollBack();
  652. var_dump($ex->getMessage());
  653. return Result::error("删除失败",0);
  654. }
  655. }
  656. $data = [
  657. 'delinfo' => $delinfo,
  658. 'deldata' => $deldata
  659. ];
  660. return Result::success($data);
  661. }
  662. /**
  663. * 关联导航池
  664. * @param array $data
  665. * @return array
  666. */
  667. public function addCatid(array $data): array
  668. {
  669. $id = $data['rule_id'];
  670. $art = Article::where('rule_id',$id)->select('id')->count();
  671. if($art==0){
  672. return Result::error('还未采集,请采集');
  673. }else{
  674. $info = Article::where('rule_id',$id)->where('state',0)->select('id')->get();
  675. if(empty($info->toArray())){
  676. return Result::error('所有文章都已导入,不可修改关联的导航池!');
  677. }else{
  678. //查找此规则任务下的文章是否已经有导入的文章
  679. $article = Article::where('rule_id',$id)->where('state',1)->select('id')->get();
  680. if(!empty($article->toArray())){
  681. //若有已导入的文章则直接复制之前已导入的导航池
  682. $catid = Article::whereIn('id',$article)->select('catid')->first();
  683. //若未导入的文章已经复制之前的导航,则无需修改
  684. $art_catid = Article::whereIn('id',$info)->whereNull('catid')->count();
  685. if($art_catid>0){
  686. $result = Article::whereIn('id',$info)->update(['catid'=>$catid['catid']]);
  687. }else{
  688. $result = ['已全部关联导航,无需再次关联!'];
  689. }
  690. }else{
  691. //若不存在已导入的文章则判断是否存在导航id
  692. if(isset($data['catid'])){
  693. //若存在直接使用此导航id
  694. $result = Article::whereIn('id',$info)->update(['catid'=>$data['catid']]);
  695. }else{
  696. //若不存在则返回所有导航栏目
  697. $result = Category::select('id','name')->get();
  698. }
  699. }
  700. }
  701. }
  702. //查找此规则任务下的文章是否都已经导入
  703. return Result::success($result);
  704. }
  705. /**
  706. * 导入文章
  707. * @param array $data
  708. * @return array
  709. */
  710. public function addArt(array $data): array
  711. {
  712. // var_dump("======@@@====");
  713. $where = [
  714. 'rule_id' => $data['rule_id'],
  715. 'state' => 0
  716. ];
  717. //获取某个规则任务下的已采集未导入的文章及文章详情
  718. $arts_id = Article::where($where)->wherenotNull('catid')->orderBy('id')->select('id')->get();
  719. $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();
  720. $arts_data = ArticleData::whereIn('article_id',$arts_id)->select('content')->orderBy('article_id')->get()->toArray();
  721. // var_dump($article_data);
  722. $data = [
  723. 'articles' => $arts,
  724. 'art_content' => $arts_data
  725. ];
  726. Db::beginTransaction();
  727. try{
  728. $oldart = OldArticle::insert($arts);
  729. $oldart_data = OldArticleData::insert($arts_data);
  730. $upstate_art = Article::where($where)->wherenotNull('catid')->update(['state' => 1]);
  731. Db::commit();
  732. } catch(\Throwable $ex){
  733. Db::rollBack();
  734. var_dump($ex->getMessage());
  735. return Result::error($ex->getMessage(),0);
  736. }
  737. return Result::success($data);
  738. }
  739. }