IndexController.php 5.3 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use QL\QueryList;
use think\Db;

class IndexController extends HomeBaseController
{
    protected $abc;

    public function index()
    {
        $map['id']=['in','86,72'];
        $top=DB::connect('rds1')->name('arctype')->field('id,typename')->where($map)->select();
        $this->assign('top',$top);
        return $this->fetch(':index');
    }

    public function getMiddle(){
     $id=input('topid');
     $map['reid']=$id;
        $middle=DB::connect('rds1')->name('arctype')->field('id,typename')->where($map)->select();
        $this->success('','',$middle);
    }
    public function getThird(){
     $id=input('middleid');
     $map['reid']=$id;
        $middle=DB::connect('rds1')->name('arctype')->field('id,typename')->where($map)->select();
        $this->success('','',$middle);
    }

    public function getSecond(){

    }



    public function caiji(){
        $param=$this->request->param('url');
        if (empty($param)){
            $this->error('请输入网址!');
        }
        $rules1 = array(
            //采集标题
            'header' => array(':header','text'),
            //采集文章主体内容
            'content' => ['#js_content','html']
        );
        //获取网页链接
        $param= htmlspecialchars_decode($param);
        $html=file_get_contents("$param");
        $html =str_replace("data-src","src",$html);
//        echo $html;
        //抓取网页信息
        $image = QueryList::html($html)
            ->rules($rules1)
            ->query()
            ->getData(function ($item){

                if (empty($item['content'])){
                    $this->error('无法获取相关内容,请检查链接可用性。','/','','10');
                }
                $content = QueryList::html($item['content']);
                $content->find('section:last')->remove();
                $content->find('section:last')->remove();
                $content->find('section:last')->remove();
                $content->find('section:last')->remove();
                $content->find('section:last')->remove();
                $content->find('section:last')->remove();
                $content->find('img')->map(function($img){
                    $src = $img->src;
                    $localSrc = 'upload/image/'.md5($src).'.jpg';
                    $imageUrl = 'image/'.md5($src).'.jpg';
                    $stream = file_get_contents($src);
                    file_put_contents($localSrc,$stream);
                    $img->attr('width','100%');
                    $img->attr('src',cmf_get_image_preview_url($imageUrl));
                });
                $item['content'] = $content->find('')->html();
                return $item;
            });
//        var_dump($image->all());

        //按照织梦规则进行存储

        //判断要存储的栏目位置
        $top_id=input('top_id');
        $middle_id=input('middle_id');
        $third_id=input('third_id');
        if (empty($middle_id)||empty($middle_id)){
            $this->error('请输入您的目标栏目!');
        }
        if (empty($third_id)){
            $type_id=$middle_id;
        }
       $insert['typeid']=$type_id;
       $insert['channel']=1;
        $insert['senddate']=time();
       $insert['sortrank']=time();
       $insert['mid']=1;



        DB::connect('rds1')->name('arctiny')->insert($insert);
        $max_id=DB::connect('rds1')->name('archives')->max('id');
        $insert_arc['id']=$max_id+1;
        $insert_arc['typeid']=$type_id;
        $insert_arc['ismake']=1;
        $insert_arc['channel']=1;
        $insert_arc['click']=rand(50,100);
        $insert_arc['title']=$image->all()[0]['header'];
        $insert_arc['writer']='admin';
        $insert_arc['source']='微信文章';
        $insert_arc['pubdate']=time();
        $insert_arc['senddate']=time();
        $insert_arc['mid']=1;
        $insert_arc['keywords']='天津协和干细胞';
        $insert_arc['description']=$image->all()[0]['header'];
        $arc_id=DB::connect('rds1')->name('archives')->insertGetId($insert_arc);


        $insert_info['aid']=$insert_arc['id'];
        $insert_info['typeid']=$type_id;
        $insert_info['body']=$image->all()[0]['content'];
        $insert_info['userip']=get_client_ip();
        $result=DB::connect('rds1')->name('addonarticle')->insert($insert_info);
        dump($result);
        $id=db('web')->insertGetId(['url'=>$param,'title'=>$image->all()[0]['header'],'create_time'=>time(),'more'=>$image->all()[0]['content']]);
        $this->redirect('portal/index/web',['id'=>$id]);
    }

    public function web(){
        $id=$this->request->param('id',0,'intval');
        $data=Db::name('web')->where('id',$id)->find();
        $this->assign('list',$data);
        return $this->fetch(':web');
    }
}