IndexController.php 2.8 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()
    {
        return $this->fetch(':index');
    }

    public function caiji(){
        $param=$this->request->param('url');
        dump($param);
        $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){
                $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());
        $id=Db::name('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');
    }
}