<?php

namespace app\controller\api\article;

use app\Request;
use think\facade\Db;
use crmeb\basic\BaseController;

class Juhe extends BaseController
{
    public static $key_tt = "4aea8d10a534112d7ae880b321d05268";
    public static $key_sp = "a92b764c1e53abddda3a99ccc91cf22c";
    /**
     * 新闻那头条1
     * author 爱代码的糖糖
     * time  2021 08 17 16:00:26
     */
    public function toutiao()
    {

        $url='http://v.juhe.cn/toutiao/index';
        for ($i=1;$i<=9;$i++){

            if($i==1 or $i==2){
                $type='guonei';
            }elseif($i==3){
                $type='guowai';
            }elseif($i==4){
                $type='yule';
            }elseif($i==6 or $i==7){
                $type='caijing';
            }elseif($i==8 or $i==9){
                $type='jiankang';
            }elseif($i==5){
                $type='keji';
            }
            $param=[
                'type'=>$type,
                'key'=>self::$key_tt,
                'is_filter'=>1,
                'page'=>$i,
                'page_size'=>10
            ];
            $data= json_decode($this->curl($url,$param,'get'),true);
            if(empty($data['result']['data'])){
                break;
            }
            $result=$data['result']['data'];
            foreach ($result as $k=>$v){
                $insertData=[
                    'cid'=>23,
                    'admin_id'=>1,
                    'title'=>$v['title'],
                    'author'=>$v['author_name'],
                    'image_input'=>$v['thumbnail_pic_s'],
                    'create_time'=>$v['date'],
                    'status'=>1
                ];

                $urldetail='http://v.juhe.cn/toutiao/content';
                $paramdetail=[
                    'key'=>self::$key_tt,
                    'uniquekey'=>$v['uniquekey']
                ];
                $resultdetail=json_decode($this->curl($urldetail,$paramdetail,'post'),true);
                if(empty($resultdetail['result']['content'])){
                    continue;
                }
                $id=Db::name('article')->insertGetId($insertData);
                $contentdata=[
                    'article_content_id'=>$id,
                    'content'=>$resultdetail['result']['content']
                ];
                Db::name('article_content')->insert($contentdata);
            }
        }
        return;
        
    }
    /**
     * 热门视频
     * author 爱代码的糖糖
     * time  2021 08 17 16:15:58
     */
    public function Getvideo()
    {
        $url='http://apis.juhe.cn/fapig/douyin/billboard';
        $param=[
            'key'=>self::$key_sp,
            'type'=>'hot_video',
            'size'=>50,
        ];
        $data=json_decode($this->curl($url,$param,'post'),true);
        foreach ($data['result'] as $k=>$v){
            $insertData=[
                'title'=>$v['title'],
                'share_url'=>$v['share_url'],
                'author'=>$v['author'],
                'item_cover'=>$v['item_cover'],
                'digg_count'=>$v['digg_count'],
                'play_count'=>$v['play_count'],
                'hot_value'=>$v['hot_value'],
                'status'=>1,
                'admin_id'=>1,
            ];
            Db::name('video')->insert($insertData);
        }
return ;
    }
    //curl 请求
    public function curl($url , $data=array(),$option,$header=''){

        $ch = curl_init();//
        curl_setopt($ch, CURLOPT_URL, $url);//

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        // POST数据
        if($option=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);

        curl_close($ch);

        return $output;

    }
    /**
     * APP获取新闻资讯
     * author 爱代码的糖糖
     * time  2021 08 17 17:00:15
     * 废弃
     */
//    public function getarticle()
//    {
//        $data=Db::name('article')
//            ->alias('a')
//            ->leftJoin('article_content ac','a.article_id=ac.article_content_id')
//            ->field('a.title,a.author,a.image_input,ac.content')
//            ->select()
//            ->toArray();
//        return app('json') -> success($data);
//
//    }
    /**
     * APP获取热门视频
     * author 爱代码的糖糖
     * time  2021 08 17 17:04:19
     * param start 开始位置 默认0  limit 条数 默认10
     */
    public function getvideos()
    {
        try {
            [$page, $limit] = $this->getPage();
            $data=Db::name('video')
                ->withoutField('status,admin_id,mer_id')
                ->page($page, $limit)
                ->order("create_time desc")
                ->select()
                ->toArray();
            return app('json') -> success($data);
        }catch (\Throwable $e) {
            return app('json')->fail($e->getMessage());
        }

    }
    
}