TravelController.php 7.8 KB
<?php
/**
 * 商旅服务
 * Author : xiaojie
 * DateTime: 2018/12/06 11:46
 */
namespace app\portal\controller;

use app\portal\model\PageModel;
use app\portal\model\PortalCategoryModel;
use app\portal\model\CarWxModel;
use app\portal\model\ShowModel;
use cmf\controller\HomeBaseController;
use think\Db;

class TravelController extends HomeBaseController
{
    private $appkey = 'a31656a82e1147b189246f88ddac2adf';
    private $url = 'http://v.juhe.cn/xianxing/index';
    /**
     * 交通信息
     * portal/travel/traffic
     */
    public function traffic()
    {
        $cate_id = 42;
        $pageModel = new PageModel();
        $info = $pageModel->where('cate_id',$cate_id)->find();

        //所在位置
        $location = [
            [
                'location' => '商旅服务',
                'url' => 'javascript:;'
            ],
            [
                'location' => '交通信息',
                'url' => url('portal/travel/traffic')
            ]
        ];
        $this->assign('info',$info);
        $this->assign('location',$location);
        return $this->fetch();
    }

    /**
     * 酒店住宿,周边美食,天津旅游(43,44,45)
     * portal/travel/news
     */
    public function news()
    {
        $cate_id = $this->request->param('cate_id');
        $showModel = new ShowModel();
        $list = $showModel->getList($cate_id);
        if($cate_id == 43){
            $location = '酒店住宿';
            $menu_id = '49';
        }elseif ($cate_id == 44){
            $location = '周边美食';
            $menu_id = '50';
        }elseif ($cate_id == 45){
            $location = '天津旅游';
            $menu_id = '51';
        }else{
            return false;
        }
        //分页样式
        $page = $this->getPageStyle($list);
        //所在位置
        $location = [
            [
                'location' => '商旅服务',
                'url' => 'javascript:;'
            ],
            [
                'location' => $location,
                'url' => url('portal/travel/news',array('cate_id'=>$cate_id))
            ]
        ];
        $this->assign('list',$list);
        $this->assign('location',$location);
        $this->assign('page',$page);
        $this->assign('menu_id',$menu_id);
        return $this->fetch(':list');
    }

    //移动端交通信息
    public function trafficMobile(){
        //交通信息数据
        $cateId = 42;
        $pageModel = new PageModel();
        $info = $pageModel->where('cate_id',$cateId)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    //移动端酒店住宿
    public function hotelMobile(){
        return $this->fetch();
    }

    //移动度周边美食
    public function foodMobile(){
        return $this->fetch();
    }

    //移动端商旅服务列表数据
    public function get_list(){
        $cate_id = $this->request->param('cate_id');
        $page = $this->request->param('page');
        $data = $this->dataList($cate_id,$page);
        $week = ['日','一','二','三','四','五','六'];
        foreach($data['list'] as &$value){
            $value['menu_id'] = $data['menu_id'];
            $value['thumb'] = cmf_get_image_url($value['thumb']);
            $value['m'] = date('m',$value['update_time']);
            $value['d'] = date('d',$value['update_time']);
            $value['w'] = $week[date('w',$value['update_time'])];
        }
        echo json_encode($data['list']);
    }

    //移动端天津旅游
    public function travelMobile(){
        return $this->fetch();
    }

    //移动端商旅服务数据
    public function dataList($cate_id,$page){
        $showModel = new ShowModel();
        $list = $showModel->getMobileList($cate_id,$page);
        if($cate_id == 43){
            $menu_id = '49';
        }elseif ($cate_id == 44){
            $menu_id = '50';
        }elseif ($cate_id == 45){
            $menu_id = '51';
        }elseif ($cate_id == 36){
            $menu_id = '42';
        }else{
            return false;
        }
        //分页样式
        $data = [];
        $data['list'] = $list;
        $data['menu_id'] = $menu_id;
        return $data;
    }

    //商旅服务详情页
    public function detail(){
        $id = $this->request->param('id','','intval');
        $showModel = new ShowModel();
        $info = $showModel->getDetail($id);
        $this->setShowNum($info['id']);
        $info['thumb'] = cmf_get_image_url($info['thumb']);
        $this->assign('info',$info);
        return $this->fetch();
    }

    //车辆限号
    public function carWx(){
        $carModel = new CarWxModel();
        $info = $carModel->select();
        echo json_encode($info);
    }

    //移动端限号数据
    public function limitRow(){
        $url = $this->url;
        $params = array(
            "key" => $this->appkey,//应用APPKEY(应用详细页查询)
            "dtype" => "",//返回数据的格式,xml或json,默认json
            "city" => "tianjin",//城市代码,在城市列表接口获取
            "type" => "6",//类型,1:今日 2:明天 3:后天 4:第4天 5:第5天 6:第6天 默认1
        );
        $paramstring = http_build_query($params);
        $content = $this->http($url,$paramstring);
        $res_decode = json_decode($content,true);
        if($res_decode){
            if($res_decode['error_code']=='0'){
                $date = strtotime($res_decode['result']['date']);
                $week = $res_decode['result']['week'];
                if($res_decode['result']['xxweihao']){
                    $number = $res_decode['result']['xxweihao'][0].'/'.$res_decode['result']['xxweihao'][1];
                }else{
                    $number = '不限行';
                }
                $result['date'] = date('n',$date).'月'.date('j',$date).'日';
                $result['week'] = '周'.mb_substr($week,2,1,"UTF-8");
                $result['number'] = $number;
                $result['create_time'] = time();
                $wxModel = new CarWxModel();
                $car = Db::name('car_wx')->field('id')->find();
                $wxModel->where('id',$car['id'])->delete();
                $wxModel::create($result);
                echo '请求成功';
            }else{
                echo $res_decode['error_code'].":".$res_decode['reason'];
            }
        }else{
            echo "请求失败";
        }
    }

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    private function http($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
}