<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Esf_center_api extends MY_Controller {

    function __construct()
    {
        parent::__construct();

        $this->load->model("user_model");
        $this->load->model("ask_model");
        $this->load->model("house_model");
        $this->load->library("user_class");
        $this->load->helper("template_helper");
        $this->load->library("apply_class");
        $this->load->model("user_statistic_model");
        $this->load->model("push_model");
        $this->load->model('rent_coupon_model');
        $this->city = '';
        $this->user_id = 0;
    }


    /**
     * 验证验证码接口
     * @author cxf
     */
    public function api_yzcode()
    {
        $validcode = intval($this->input->get('validcode'));
        $telno = intval($this->input->get('telno'));
        $kind = intval($this->input->get('kind'));

        //验证验证码
        $valid_isuse = $this->user_model->yzusersms($validcode,$telno,$kind);

        if($valid_isuse)
        {
            //验证成功
            //$this->user_model->updatesms($validcode,$telno);
            echo 1;
        }
        else
        {
            echo 0;
        }

        exit;
    }


    /**
     * 关注房源操作接口
     * @author cxf
     */
    public function api_savehouse()
    {
        $uid = $this->input->get('uid');
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $tbl = $kind == 3 ? 'sell' : 'rent';
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';

        $result = $this->user_model->getusercollectid($uid,$rowid,$city);

        //已关注此房源
        if( $result > 0 )
        {
            echo "2";
            exit;
        }

        if($tbl == 'sell')
        {
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
        }
        else
        {
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
        }


        //判断经纪人房源多查一次经纪人姓名
        if( $houseinfo[0]['infofrom'] == '5' && $houseinfo[0]['uid'] > 0 && $houseinfo[0]['contactor'] == '')
        {
            $broker_data = $this->house_model->get_data(array('form_name'=>'broker','where'=>array('uid'=>$houseinfo[0]['uid']),'select'=>array('uid','truename'),'limit'=>1),'esfhousebak');

            //查询经纪人姓名
            $houseinfo[0]['contactor'] = $broker_data[0]['truename'];
        }

        $searchcond = serialize($houseinfo[0]);
        $this->user_model->addcollect( $uid , $rowid , $kind , $searchcond,$city,1);

        echo "1";
        exit;
    }





    /**
     * 关注房源操作接口，移动专用
     * @author cxf
     */
    public function api_savehousemobile()
    {
        $return ='';
        $uid = $this->input->get('uid');
        if(empty($uid))
        {
            $telno = $this->input->get('telno');
            $uid = $this->user_model->yztelno($telno);
        }
        $flag = $this->input->get('flag');
        if(empty($flag))
        {
            $flag = 2;
        }
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';
        $tbl=$kind==3?'sell':'rent';
        $result = $this->user_model->getusercollectid($uid,$rowid,$city);
        //已关注此房源
        if($result['id']>0)
        {
            $return = 'N_COL';
        }
        if($tbl == 'sell'){
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infotype','infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','averprice'),'limit'=>1),'esfhousebak');
        }else{
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infotype','infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
        }
        if(empty($houseinfo[0]) && $city=='nj')
        {
            $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
                $searchcond = serialize($houseinfo[0]);
            }else{
                $searchcond = '';
            }
            $return = $this->user_model->addcollect($uid,$rowid,$kind,$searchcond,$city,$flag);
        }
        
        if($return == 'N_COL'){
            $result = 2;
            $msg = '已收藏此房源';
        }elseif($return == 'N_HOU'){
            $result = 3;
            $msg = '无此房源';
        }elseif($return == 'N_USER'){
            $result = 4;
            $msg = '无此用户';
        }elseif($return > 0){
            $house_type = $houseinfo[0]['room']."室".$houseinfo[0]['hall']."厅";
            if($kind==3) {
                $url = "http://newrent.house365.com/server-task/agent-crm-insert-record?phone={$telno}&city={$city}&activefrom=485&my_floor={$houseinfo[0]['floor']}&total_floor={$houseinfo[0]['totalfloor']}&block_name={$houseinfo[0]['blockshowname']}&area={$houseinfo[0]['buildarea']}&forward={$houseinfo[0]['forward']}&buy_old_property={$houseinfo[0]['infotype']}&house_type={$house_type}";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                $rs = curl_exec($ch);
                curl_close($ch);
            }


            $result = 1;
            $msg = '收藏成功';
            //验证不在白名单，不在推送库，不在推送历史库
            $result2 = $this->push_model->yzpush($telno);
            if($result2<=0  && $city=='nj'){
                //加入备选库
                //$this->push_model->addalternatives($uid,$telno);
            }
        }elseif($return == 0){
            $result = 0;
            $msg = '收藏失败';
        }

        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }



    /**
     * 关注房源操作接口，触屏专用
     * @author cxf
     */
    public function api_savehousetouch()
    {
        $return ='';
        $passport_uid = $this->input->get('passport_uid');

        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);

        $flag = $this->input->get('flag');
        if(empty($flag))
        {
            $flag = 2;
        }
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $phone =  $this->input->get('phone');
        $city = $city?$city:'nj';
        $tbl=$kind==3?'sell':'rent';
        $result = $this->user_model->getusercollectid($uid,$rowid,$city);
        //已关注此房源
        if($result['id']>0)
        {
            $return = 'N_COL';
        }
        if($tbl == 'sell'){
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infotype','infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
        }else{
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infotype','infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
        }
        if(empty($houseinfo[0]) && $city=='nj')
        {
            $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
                $searchcond = serialize($houseinfo[0]);
            }else{
                $searchcond = '';
            }
            $return = $this->user_model->addcollect($uid,$rowid,$kind,$searchcond,$city,$flag,$passport_uid);
        }


        if($return == 'N_COL'){
            $result = 2;
            $msg = '已收藏此房源';
        }elseif($return == 'N_HOU'){
            $result = 3;
            $msg = '无此房源';
        }elseif($return == 'N_USER'){
            $result = 4;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '收藏成功';
            $house_type = $houseinfo[0]['room']."室".$houseinfo[0]['hall']."厅";

            $houseinfo[0]['floor'] = iconv('gbk','utf-8',$houseinfo[0]['floor']);
            $houseinfo[0]['blockshowname'] = iconv('gbk','utf-8',$houseinfo[0]['blockshowname']);
            $houseinfo[0]['forward'] = iconv('gbk','utf-8',$houseinfo[0]['forward']);
            $house_type = iconv('gbk','utf-8',$house_type);
            if($kind==3){
                $url ="http://crm.house365.com/index.php/Simulate/sea_utf8?mobile={$phone}&city={$city}&activefrom=485&my_floor={$houseinfo[0]['floor']}&total_floor={$houseinfo[0]['totalfloor']}&block_name={$houseinfo[0]['blockshowname']}&area={$houseinfo[0]['buildarea']}&forward={$houseinfo[0]['forward']}&buy_old_property={$houseinfo[0]['infotype']}&house_type={$house_type}";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                $rs = curl_exec($ch);
                curl_close($ch);
            }
            
            //验证不在白名单，不在推送库，不在推送历史库
            $result2 = $this->push_model->yzpush($telno);
            if($result2<=0  && $city=='nj'){
                //加入备选库
                //$this->push_model->addalternatives($uid,$telno);
            }
        }elseif($return == 0){
            $result = 0;
            $msg = '收藏失败';
        }

        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }

    /**
     * 关注房源操作接口，base_id版本
     * @author fly
     */
    public function save_house_focus_by_baseId()
    {
        $return ='';
        $passport_uid = $this->input->get('passport_uid');
        $uid=0;
        $flag = $this->input->get('flag');

        if($passport_uid){
            //根据passport_uid获取uid
            $uid = $this->user_model->getUidByPassportId($passport_uid);


            if(empty($flag))
            {
                $flag = 2;
            }
            if($uid==0)
            {
                $return = 'N_USER';
            }
        }


        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $phone =  $this->input->get('phone');
        $base_id =  $this->input->get('base_id');
        $city = $city?$city:'nj';
        $tbl=$kind==3?'sell':'rent';
        $result = $this->user_model->getCollectHouseIdByBaseID($base_id,$kind,$rowid,$city);
        //已关注此房源
        if($result['id']>0)
        {
            $return = 'N_COL';
        }
        if($tbl == 'sell'){
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infotype','infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
        }else{
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infotype','infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
        }
        if(empty($houseinfo[0]) && $city=='nj')
        {
            $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
                $searchcond = serialize($houseinfo[0]);
            }else{
                $searchcond = '';
            }
            $return = $this->user_model->addcollectByBaseID($base_id,$rowid,$kind,$searchcond,$city,$flag,$passport_uid,$uid);
        }


        if($return == 'N_COL'){
            $result = 2;
            $msg = '已收藏此房源';
        }elseif($return == 'N_HOU'){
            $result = 3;
            $msg = '无此房源';
        }elseif($return == 'N_USER'){
            $result = 4;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '收藏成功';
            $house_type = $houseinfo[0]['room']."室".$houseinfo[0]['hall']."厅";

            $houseinfo[0]['floor'] = iconv('gbk','utf-8',$houseinfo[0]['floor']);
            $houseinfo[0]['blockshowname'] = iconv('gbk','utf-8',$houseinfo[0]['blockshowname']);
            $houseinfo[0]['forward'] = iconv('gbk','utf-8',$houseinfo[0]['forward']);
            $house_type = iconv('gbk','utf-8',$house_type);
            if($kind==3){
                $url ="http://crm.house365.com/index.php/Simulate/sea_utf8?mobile={$phone}&city={$city}&activefrom=485&my_floor={$houseinfo[0]['floor']}&total_floor={$houseinfo[0]['totalfloor']}&block_name={$houseinfo[0]['blockshowname']}&area={$houseinfo[0]['buildarea']}&forward={$houseinfo[0]['forward']}&buy_old_property={$houseinfo[0]['infotype']}&house_type={$house_type}";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                $rs = curl_exec($ch);
                curl_close($ch);
            }

            //验证不在白名单，不在推送库，不在推送历史库
            $result2 = $this->push_model->yzpush($telno);
            if($result2<=0  && $city=='nj'){
                //加入备选库
                //$this->push_model->addalternatives($uid,$telno);
            }
        }elseif($return == 0){
            $result = 0;
            $msg = '收藏失败';
        }

        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }




    /**
     * 关注房源操作接口，加盟站专用
     * @author cxf
     */
    public function api_savehouse_jmz()
    {



        $return ='';
        $uid = $this->input->get('uid');
        if(empty($uid))
        {
            $telno = $this->input->get('telno');
            $uid = $this->user_model->yztelno($telno);
        }
        $flag = $this->input->get('flag');
        if(empty($flag))
        {
            $flag = 2;
        }
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';
        $tbl=$kind==3?'sell':'rent';
        $result = $this->user_model->getusercollectid($uid,$rowid,$city);
        //已关注此房源
        if($result['id']>0)
        {
            $return = 'N_COL';
        }
        if($tbl == 'sell'){
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
        }else{
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
        }
        if(empty($houseinfo[0]) && $city=='nj')
        {
            $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
                $searchcond = serialize($houseinfo[0]);
            }else{
                $searchcond = '';
            }

            //获取淘房用户的用户中心ID passport_uid
            $passport_uid = getJiaUserID($uid);

            $return = $this->user_model->addcollect($uid,$rowid,$kind,$searchcond,$city,$flag,$passport_uid);
        }


        if($return == 'N_COL'){
            $result = 2;
            $msg = '已收藏此房源';
        }elseif($return == 'N_HOU'){
            $result = 3;
            $msg = '无此房源';
        }elseif($return == 'N_USER'){
            $result = 4;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '收藏成功';
            //验证不在白名单，不在推送库，不在推送历史库
            $result2 = $this->push_model->yzpush($telno);
            if($result2<=0  && $city=='nj'){
                //加入备选库
                //$this->push_model->addalternatives($uid,$telno);
            }
        }elseif($return == 0){
            $result = 0;
            $msg = '收藏失败';
        }

        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }


    /**
     * 关注房源操作接口，加盟站专用
     * @author cxf
     */
    public function api_savehouse_jmz_new()
    {



        $return ='';
        $passport_uid = $this->input->get('uid');

        $flag = $this->input->get('flag');
        if(empty($flag))
        {
            $flag = 2;
        }
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';
        $tbl=$kind==3?'sell':'rent';
        $esfuid = $this->user_model->getUidByPassportId($passport_uid);
        $result = $this->user_model->getusercollectid($esfuid,$rowid,$city);
        //已关注此房源
        if($result['id']>0)
        {
            $return = 'N_COL';
        }
        if($tbl == 'sell'){
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid'),'limit'=>1),'esfhousebak');
        }else{
            $houseinfo =  $this->house_model->get_data(array('form_name' => $tbl,'where'=>array('id'=>$rowid,'esta'=>'1'),'select'=>array('infofrom','id','district','address','streetid','blockshowname','room','hall','buildarea','forward','fitment','buildyear','floor','totalfloor','subfloor','price','uid','telno','pic1','contactor','uid','priceunit','renttype'),'limit'=>1),'esfhousebak');
        }
        if(empty($houseinfo[0]) && $city=='nj')
        {
            $return = 'N_HOU';
        }elseif($return != 'N_COL'){
            if(!empty($houseinfo[0]))
            {
                $searchcond = serialize($houseinfo[0]);
            }else{
                $searchcond = '';
            }



            $return = $this->user_model->addcollect($esfuid,$rowid,$kind,$searchcond,$city,$flag,$passport_uid);
        }


        if($return == 'N_COL'){
            $result = 2;
            $msg = '已收藏此房源';
        }elseif($return == 'N_HOU'){
            $result = 3;
            $msg = '无此房源';
        }elseif($return == 'N_USER'){
            $result = 4;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '收藏成功';
            //验证不在白名单，不在推送库，不在推送历史库
            $result2 = $this->push_model->yzpush($telno);
            if($result2<=0  && $city=='nj'){
                //加入备选库
                //$this->push_model->addalternatives($uid,$telno);
            }
        }elseif($return == 0){
            $result = 0;
            $msg = '收藏失败';
        }

        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }





    /**
     * 查询关注房源操作接口
     * @author cxf
     */
    public function api_yzsavehouse()
    {
        $return ='';
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';

        if($uid){
            $result = $this->user_model->getusercollectid($uid,$rowid,$city,$kind);
        }else{
            $result['id']=0;
        }

        echo $result['id']>0?'1':'0';
    }


    /**
     * 查询关注房源操作接口
     * @author cxf
     */
    public function api_yzsavehouse_uid()
    {
        $return ='';

        $passport_uid = $this->input->get('passport_uid');
        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';

        if($uid){
            $result = $this->user_model->getusercollectid($uid,$rowid);
        }else{
            $result['id']=0;
        }

        echo $result['id']>0?'1':'0';
    }

    /**
     * 查询我的已经关注房源uid接口
     * @author bsw
     */
    public function getCollectHouseId(){
        $return ='';
        $uid = $this->input->get('uid');
        if(empty($uid))
        {
            $telno = $this->input->get('telno');
            $uid = $this->user_model->yztelno($telno);
        }
        if($uid==0){
            return json_encode(array());;
        }
        $kind = $this->input->get('kind');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';
        $result = $this->user_model->getCollectHouseId($uid,$kind,$city);
        echo json_encode($result);
    }

    //入CRM数据
    public function get_api($t_url)
    {

        $ch = curl_init();
        $t_url = $t_url ? $t_url : "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        curl_setopt($ch, CURLOPT_URL, $t_url);
        curl_setopt($ch, CURLOPT_REFERER, $t_url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $str = curl_exec($ch);
        curl_close($ch);
        return $str;



    }

    //登陆成功需要把sso_token以cookie形式种到浏览器中
    protected function register_tf_ucenter_cookie($passport_uid){
        setcookie('PHPSESSID','',time()-1,'/','house365.com');
//        $url = "http://api.house365.com/passport/passport_gettoken_byuid.php?passport_uid=".$passport_uid;
//        if(isset($_COOKIE['utm_source']) && isset($_COOKIE['utm_medium']) && isset($_COOKIE['utm_campaign'])){
//            $url.= "&utm_source={$_COOKIE['utm_source']}&utm_medium={$_COOKIE['utm_medium']}&utm_campaign={$_COOKIE['utm_campaign']}";
//        }
//        if(isset($_COOKIE['crm_scene'])){
//            $url.= "&register_client=PC&register_application={$_COOKIE['crm_scene']}";
//        }
//        $api_res = json_decode($this->get_api($url), true);
        $api_res = $this->get_api(GB_GET_USER_INFO_BY_PASSPORT_ID_URL . '?passport_uid=' . $passport_uid );
        if(isset($api_res['result']) && $api_res['result']==1){
            setcookie('sso_token',$api_res['data']['sso_token'],time()+3600*24*180,'/','house365.com');
        }
    }


    /**
     * 查询关注房源操作接口
     * @author cxf
     */
    public function api_user_login()
    {
        $uid = $this->input->get('uid');
        $code = $this->input->get('code');
        $md5_code = md5(md5('myhouse365'.$uid.date("Y-m-d",time()).$_COOKIE['interface_msg']));

        if( $code == $md5_code )
        {
            $user_array = $this->user_model->getuserdetail($uid);

            if(is_array($user_array) && !empty($user_array) )
            {
                $this->register_tf_ucenter_cookie($user_array['passport_uid']);
                $this->user_class->user_login($uid,$user_array['password'],$user_array['telno'],$user_array['username']);
                jumpto(USER_CENTER.'user');
            }
            else
            {
                echo '用户不存在或已失效';
            }
        }
        else
        {
            echo '来源不正确或请再试一次!';
        }
    }

    /**
     * 取消关注房源操作接口
     * @author cxf
     */
    public function api_delsavehouse()
    {
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $tbl=$kind==3?'sell':'rent';
        $id = $this->user_model->getcollectdetail($kind,$uid,$rowid,$city);

        if($id==0)
        {
            $return = 'N_COL';
        }else{
            $return = $this->user_model->delcollect($id);

        }
        if($return == 'N_COL'){
            //$result = 2;
            //$msg = '无此收藏';
            $result = 1;
            $msg = '删除成功';
        }elseif($return == 'N_USER'){
            $result = 3;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '删除成功';
        }elseif($return == 0){
            $result = 0;
            $msg = '取消失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }



    /**
     * 取消关注小区操作接口
     * @author cxf
     */
    public function api_delsaveblock()
    {

        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');

        $id = $this->user_model->getcollectdetail($kind,$uid,$rowid,$city);

        if($id==0)
        {
            $return = 'N_COL';
        }
        else
        {
            $return = $this->user_model->delcollect($id);
        }
        if($return == 'N_COL')
        {
            $result = 1;
            $msg = '取消收藏成功';
        }elseif($return == 'N_USER')
        {
            $result = 3;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '取消收藏成功';
        }elseif($return == 0){
            $result = 0;
            $msg = '取消失败';
        }
        //取消收藏成功，调用接口刷新
        $url = 'http://newrent.house365.com/api/tf-app/refresh-block-list?uid='.$uid.'&city='.$city.'&id='.$rowid;
        curl_get_contents($url);
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }




    /**
     * 根据用户中心id取消关注房源操作接口
     * @author cxf
     */
    public function api_delsavehouse_uid()
    {
        $passport_uid = $this->input->get('passport_uid');


        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);

        if($uid==0)
        {
            $return = 'N_USER';
        }
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $tbl=$kind==3?'sell':'rent';
        $id = $this->user_model->getcollectdetail($kind,$uid,$rowid,$city);

        if($id==0)
        {
            $return = 'N_COL';
        }else{
            $return = $this->user_model->delcollect($id);

        }
        if($return == 'N_COL'){
            //$result = 2;
            //$msg = '无此收藏';
            $result = 1;
            $msg = '删除成功';
        }elseif($return == 'N_USER'){
            $result = 3;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '删除成功';
        }elseif($return == 0){
            $result = 0;
            $msg = '取消失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }

    /**
     * 根据用户中心base_id取消关注房源操作接口
     * @author cxf
     */
    public function api_cancel_focus_by_baseId()
    {
        $base_id = $this->input->get('base_id');
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $tbl=$kind==3?'sell':'rent';
        $id = $this->user_model->getCollectHouseIdByBaseID($base_id,$kind,$rowid,$city,1);

        if(empty($id))
        {
            $return = 'N_COL';
        }else{
            $return = $this->user_model->delcollect($id);

        }
        $result = 0;
        $msg = '取消失败';
        if($return == 'N_COL'){
            //$result = 2;
            //$msg = '无此收藏';
            $result = 1;
            $msg = '删除成功';
        }elseif($return == 'N_USER'){
            $result = 3;
            $msg = '无此用户';
        }elseif($return > 0){
            $result = 1;
            $msg = '删除成功';
        }elseif($return == 0){
            $result = 0;
            $msg = '取消失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }

    /**
     * 根据base_id获取收藏状态
     * @author fly
     */
    public function get_focus_status_by_baseId()
    {
        $base_id =  $this->input->get('base_id');
        $kind = $this->input->get('kind');
        $rowid = $this->input->get('rowid');
        $city = $this->input->get('city');
        $city = $city?$city:'nj';
        $result = $this->user_model->getCollectHouseIdByBaseID($base_id,$kind,$rowid,$city);
        $return_array = array('result'=>0,'msg'=>'未收藏');
        if(!empty($result))
        {
            $return_array = array('result'=>1,'msg'=>'已收藏');
        }
        echo serialize($return_array);
    }


    /**
     * 获取用户关注房源操作接口
     * @author cxf
     */
    public function api_getsavehouse()
    {
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);

        if($uid==0)
        {
            echo serialize(array());exit;
        }

        $kind = $this->input->get('kind');
        $page = $this->input->get('page');
        $pagesize = $this->input->get('pagesize');
        $city = $this->input->get('city');
        $page = $page ? $page:1;
        $pagesize = $pagesize?$pagesize : 20;
        $offset = $pagesize * ($page-1);

        $collect_array = $this->user_model->getcollect($kind,$uid,$offset,$pagesize,$city);
        if(!empty($collect_array) && is_array($collect_array))
        {
            foreach($collect_array as $key=>$value)
            {
                $arr[$key]['id'] = $value['collect_id'];
            }
        }
        else
        {
            $arr=array();
        }

        echo serialize($arr);
    }

    /**
     * 获取用户关注小区操作接口
     * @author cxf
     */
    public function api_getsaveblock()
    {
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);

        if($uid==0)
        {
            echo serialize(array());exit;
        }

        $kind = $this->input->get('kind');
        $page = $this->input->get('page');
        $pagesize = $this->input->get('pagesize');
        $city = $this->input->get('city');
        $page = $page ? $page:1;
        $pagesize = $pagesize?$pagesize : 20;
        $offset = $pagesize * ($page-1);

        $collect_array = $this->user_model->getcollect($kind,$uid,$offset,$pagesize,$city);
        if(!empty($collect_array) && is_array($collect_array))
        {
            foreach($collect_array as $key=>$value)
            {
                $arr[$key]['id'] = $value['collect_id'];
            }
        }
        else
        {
            $arr=array();
        }

        echo serialize($arr);
    }


    /**
     * 根据用户中心获取用户关注房源操作接口
     * @author cxf
     */
    public function api_getsavehouse_uid()
    {
        $passport_uid = $this->input->get('passport_uid');

        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);
        //var_dump($uid);exit();
        if($uid==0)
        {
            echo serialize(array());exit;
        }

        $kind = $this->input->get('kind');
        $page = $this->input->get('page');
        $pagesize = $this->input->get('pagesize');
        $city = $this->input->get('city');
        $page = $page ? $page:1;
        $pagesize = $pagesize?$pagesize : 20;
        $offset = $pagesize * ($page-1);

        $collect_array = $this->user_model->getcollect($kind,$uid,$offset,$pagesize,$city);
        if(!empty($collect_array) && is_array($collect_array))
        {
            foreach($collect_array as $key=>$value)
            {
                $arr[$key]['id'] = $value['collect_id'];
                $collect_value = unserialize($value['collect_value']);
                $collect_value['collect_time'] = $value['collect_time'];
                $arr[$key]['value'] = serialize($collect_value);
            }
        }
        else
        {
            $arr=array();
        }

        echo serialize($arr);
    }


    /**
     * 获取用户关注房源操作接口
     * @author cxf
     */
    public function api_getsavehouse_data()
    {

        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);

        if($uid==0)
        {
            echo serialize(array());exit;
        }

        $kind = $this->input->get('kind');
        $page = $this->input->get('page');
        $pagesize = $this->input->get('pagesize');
        $city = $this->input->get('city');
        $page = $page ? $page:1;
        $pagesize = $pagesize?$pagesize : 20;
        $offset = $pagesize * ($page-1);

        $this->load->model("userTest_model");

        $collect_array = $this->user_model->getcollect2($kind,$uid,$offset,$pagesize,$city);


        if(!empty($collect_array) && is_array($collect_array))
        {
            foreach($collect_array as $key=>$value)
            {
                $collect_value = unserialize($value['collect_value']);
                $collect_value['collect_time'] = $value['collect_time'];
                $arr[$key]['value'] = serialize($collect_value);
            }
        }
        else
        {
            $arr=array();
        }

        echo serialize($arr);
    }

    /**
     * 获取用户关注房源操作接口
     * @author cxf
     */
    public function get_focus_house_list()
    {

        $base_id = $this->input->get('base_id');
        if(!$base_id)
        {
            echo serialize(array());exit;
        }

        $kind = $this->input->get('kind');
        $page = $this->input->get('page');
        $pagesize = $this->input->get('pagesize');
        $city = $this->input->get('city');
        $page = $page ? $page:1;
        $pagesize = $pagesize?$pagesize : 20;
        $offset = $pagesize * ($page-1);

        $this->load->model("userTest_model");

        $collect_array = $this->user_model->getcollectByBaseId($kind,$base_id,$offset,$pagesize,$city);


        if(!empty($collect_array) && is_array($collect_array))
        {
            foreach($collect_array as $key=>$value)
            {
                $collect_value = unserialize($value['collect_value']);
                $collect_value['collect_time'] = $value['collect_time'];
                $arr[$key]['value'] = serialize($collect_value);
            }
        }
        else
        {
            $arr=array();
        }

        echo serialize($arr);
    }


    /**
     * 获得用户id接口
     * @author cxf
     */
//    public function api_getuidbytelno()
//    {
//        $telno = $this->input->get('telno');
//        $userinfo =  $this->user_model->get_data(array('form_name' => 'personal_user','where'=>array('telno'=>$telno,'is_use'=>'1'),'select'=>array('id'),'limit'=>1),'esfbak');
//        echo isset($userinfo[0]['id']) ? $userinfo[0]['id'] : 0 ;
//        exit;
//    }
    public function api_getuidbytelno()
    {
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
        echo $uid;
        exit;
    }

    /**
     * 获得用户username接口
     * @author cxf
     */
//    public function api_getusernamebytelno()
//    {
//        $telno = $this->input->get('telno');
//        $userinfo =  $this->user_model->get_data(array('form_name' => 'personal_user','where'=>array('telno'=>$telno,'is_use'=>'1'),'select'=>array('username'),'limit'=>1),'esfbak');
//        echo isset($userinfo[0]['username']) ? $userinfo[0]['username'] : 0 ;
//        exit;
//    }
    public function api_getusernamebytelno()
    {
        $telno = $this->input->get('telno');
        $userinfo = $this->user_model->getUserOInfoByPhone($telno);
        echo isset($userinfo['username']) ? $userinfo['username'] : 0;
        exit;
    }

    /**
     * 获得用户id接口
     * @author cxf
     */
//    public function api_get_userinfo_by_telno()
//    {
//        $telno = $this->input->get('telno');
//        $userinfo =  $this->user_model->get_data(array('form_name' => 'personal_user','where'=>array('telno'=>$telno,'is_use'=>'1'),'select'=>array('id','username','cityname','infofrom'),'limit'=>1),'esfbak');
//        //print_r($userinfo[0]);
//        echo isset($userinfo[0]) ? serialize($userinfo[0]) : serialize(array());
//        exit;
//    }
    public function api_get_userinfo_by_telno()
    {
        $telno = $this->input->get('telno');
        $userinfo = $this->user_model->getUserOInfoByPhone($telno);
        echo isset($userinfo['id']) && $userinfo['id'] > 0 ? serialize($userinfo) : serialize(array());
        exit;
    }


    /**
     * 关注小区操作接口
     * @author cxf
     */
    public function api_saveblock()
    {
        $uid = $this->input->get('uid');
        $kind = $this->input->get('kind');
        $blockid = $this->input->get('id');
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';
        $result = $this->user_model->getusercollectid($uid,$blockid,$city);

        //已关注此房源
        if( $result > 0 )
        {
            echo "2";
            exit;
        }

        $blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
        $searchcond = serialize($blockinfo[0]);
        $this->user_model->addcollect($uid,$blockid,$kind,$searchcond,$city);

        echo "1";
        exit;
    }

    /**
     * app关注小区操作接口
     * @author cxf
     */
    public function api_app_saveblock()
    {
        $uid = $this->input->get('uid');
        $kind = $this->input->get('kind');
        $blockid = $this->input->get('id');
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';
        $result = $this->user_model->getusercollectid($uid,$blockid,$city);
        
        //已关注此房源
        if( $result > 0 )
        {
            $msg="已经收藏此小区";
            $return_array = array('result'=>'0','data'=>'2','msg'=>$msg);
            echo serialize($return_array);
        }else{
            $url ="http://newrent.house365.com/server-task/agent-crm-insert-record?phone={$_GET['telno']}&city={$city}&activefrom=487&block_name=$blockid";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $rs = curl_exec($ch);
            curl_close($ch);
        }

        $blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
        $searchcond = serialize($blockinfo[0]);
        $this->user_model->addcollect($uid,$blockid,$kind,$searchcond,$city,2);

        $msg="收藏成功";

        //收藏成功，调用接口刷新
        $url = 'http://newrent.house365.com/api/tf-app/refresh-block-list?uid='.$uid.'&city='.$city.'&id='.$blockid;
        curl_get_contents($url);
        $return_array = array('result'=>'1','data'=>'1','msg'=>$msg);
        echo serialize($return_array);
    }

    /**
     * 关注小区操作接口加盟站专用
     * @author xue
     */
    public function api_saveblock_jmz()
    {
        $uid = $this->input->get('uid');
        $kind = $this->input->get('kind');
        $blockid = $this->input->get('id');
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';
        $result = $this->user_model->getusercollectid($uid,$blockid,$city);

        //已关注此房源
        if( $result > 0 )
        {
            echo "2";
            exit;
        }

        $blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
        $searchcond = serialize($blockinfo[0]);
        //获取淘房用户的用户中心ID passport_uid
        $passport_uid = getJiaUserID($uid);
        $this->user_model->addcollect($uid,$blockid,$kind,$searchcond,$city,1,$passport_uid);

        echo "1";
        exit;
    }


    /**
     * 关注小区操作接口淘房专用
     * @author xue
     *
     */
    public function api_saveblock_tf()
    {
        $passport_uid = $this->input->get('uid');
        $kind = $this->input->get('kind');
        $blockid = $this->input->get('id');
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';
        $kind = $kind ? $kind : 2;
        //找出二手房id
        $esfUid = $this->user_model->getUidByPassportId($passport_uid);

        //查看是否保存
        $result = $this->user_model->getusercollectid($esfUid,$blockid,$city,$kind);

        //已关注此房源
        if( $result > 0 )
        {
            echo "2";
            exit;
        }
        //获取小区信息
        $blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');


        if(is_array($blockinfo[0])&&!empty($blockinfo[0])){
            $searchcond = serialize($blockinfo[0]);
            //获取淘房用户的用户中心ID passport_uid
            //$passport_uid = getJiaUserID($uid);
            $this->user_model->addcollect($esfUid,$blockid,$kind,$searchcond,$city,1,$passport_uid);
            echo "1";
            exit;

        }else{
            //无此小区
            echo "3";
            exit();
        }

    }



    /**
     * 关注小区操作接口(手机端提交)
     * @author bsw
     */
    public function apiAddCollectBlock()
    {
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';
        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
        if(!$uid)
        {
            echo 3;
            die;
        }
        $blockid = $this->input->get('id');
        $result = $this->user_model->getusercollectid($uid,$blockid);

        //已关注此房源
        if($result>0)
        {
            echo "2";
            exit;
        }
        $blockinfo =  $this->user_model->get_data(array('form_name' => 'block','where'=>array('id'=>$blockid,'esta'=>'2'),'limit'=>1),'esfhousebak');
        $searchcond = serialize($blockinfo[0]);
        $this->user_model->addcollect($uid,$blockid,2,$searchcond,$city,2);
        //验证不在白名单，不在推送库，不在推送历史库
        $result = $this->push_model->yzpush($uid);

        if($result<=0)
        {
            //加入备选库
            //$result = $this->push_model->addalternatives($uid,$telno);
        }

        echo "1";
        exit;
    }


    /**
     * 关注学校操作接口
     * @author xue
     */
    public function api_save_school()
    {
        $uid = $this->input->get('uid');
        $kind = $this->input->get('kind');
        $schoolid = $this->input->get('id');
        $city = $this->input->get('city');
        $city = $city ? $city : 'nj';
        $result = $this->user_model->getusercollectid($uid,$schoolid,$city,$kind);
        //var_dump($result);exit;
        //已关注此房源
        if($result>0)
        {
            echo "2";
            exit;
        }

        $schoolinfo =  $this->user_model->get_data(array('form_name' => 'school','where'=>array('id'=>$schoolid),'select'=>array('id','schoolname','district','streetid','schooltype','blockid','features','address','schooltype','telno','ispublic','rank','coverphoto'),'limit'=>1),'esfhousebak');
        $this->load->model("block_model");
        $schoolinfo[0]['streetname'] = $this->block_model->getstreetname($schoolinfo[0]['streetid']);
        $searchcond = serialize($schoolinfo[0]);

        $this->user_model->addcollect($uid,$schoolid,$kind,$searchcond,$city);
        echo "1";exit;
    }



    /**
     * 获取验证码
     * @author cxf
     */
    public function api_getyzm()
    {
        //die("11111");
        //验证是否已经通过验证
        $validcode = random(6);
        $tt = array();
        $tt['validcode'] = $validcode;
        $tt['telno'] = $this->input->get('telno');
        $tt['kind'] = $this->input->get('kind');
        $tt['dateline'] = time();

        $is_send_validcode = $this->user_model->yzsms($tt['telno'],$tt['kind']);

        if($is_send_validcode!=0){
            $tt['validcode'] = $is_send_validcode;
        }

        //if($_GET['test']==1){
        //   var_dump($tt);exit();
        //}

        //验证此号码55秒内是否已经发送过验证码,防止刷短信
        $result=$this->user_model->yzusersmstime(90,$tt['telno'],$tt['kind']);
        if($result>0)
        {
            echo '7';
            exit;
        }
        //设置表
        $this->user_model->set_table('personal_sms');
        //过滤表结构
        $tt = $this->user_model->create($tt,'db_esf');
        $result = $this->user_model->add($tt,'db_esf');
        if($result) {
            $msg   = '尊敬的365网友，您操作的验证码为:'.$tt['validcode'].'';
            // sendsms($tt['telno'],$msg,'sx');
            sendsms($tt['telno'],$msg,'sx',$tt['validcode']);
            echo '1';
        }else{
            echo '2';//发送失败
        }

    }

    /**
     * 问答有回复提醒
     * @author cxf
     */
    public function api_ask_sms()
    {
        $telno = $this->input->get('telno');
        $msg   = '你提的问题有新答案，请登录365个人会员中心进行查看。';
        sendsms($telno,$msg,'tz');
    }


    /**
     * 发送站内信接口
     * @author cxf
     */
    public function api_sendmessage()
    {
        $uid = $this->input->get('uid');
        $msg = $this->input->get('msg');
        $subject = $this->input->get('subject');
        $city = $this->input->get('city');
        $kind = $this->input->get('kind');
        $kind = !empty($kind)?$kind:'1';
        $msg = urldecode($msg);
        $subject = urldecode($subject);

        if(empty($city))
        {
            $city = GB_CITY;
        }

        $result = $this->apply_class->send_systemmsg($uid,$subject,$msg,$kind,$city);
        echo $result;
    }


    /**
     * 用户添加接口
     * @author cxf
     */
    public function api_adduser()
    {


        $telno = $this->input->get('telno');
        $paasword = $this->input->get('paasword');
        $city = $this->input->get('city');
        $infofrom = $this->input->get('infofrom');

        if (!preg_match('/^[_0-9a-z]{6,16}$/i',$paasword))
        {
            //密码格式不对
            $return = 'N_PASS';
        }


        $url = "api.house365.com/esf/web/api_4001.php?&stc=".urlencode($telno);
        //ECHO $url;
        curl_get_contents($url);
        //验证此号码是否已经注册
        $result = $this->user_model->yztelno($telno);

        //记录日志
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s').var_export($telno,true)." \n";
        $log = dirname(__FILE__)."/esf_center_api_api_adduser.log";
        file_put_contents($log,$msg,FILE_APPEND );


        if( $result > 0 )
        {
            //已经注册
            $return = 'N_REG';
        }
        else if($return != 'N_PASS')
        {
            $result = $this->user_model->adduser($telno,$paasword,$city,$infofrom);
            $return = $result;
        }

        //密码格式不对
        if($return == 'N_PASS')
        {
            $result = 2;
            $msg = '密码格式不对';
        }
        else if($return == 'N_REG')
        {
            $result = 3;
            $msg = '号码已被注册';
        }
        else if($return > 0)
        {
            $result = 1;
            $msg = '注册成功';
        }
        else if($return == 0)
        {
            $result = 0;
            $msg = '注册失败';
        }

        $return_array = array('result'=>$result , 'msg'=>$msg);

        echo serialize($return_array);
    }




    /**
     * 用户添加接口APP专用
     * @author xue
     */
    public function api_adduser_mobile()
    {
        $result = 1;
        $msg = '停用';
        $return_array = array('result'=>$result , 'msg'=>$msg);
        echo serialize($return_array);die();

        $telno = $this->input->get('telno');
        $paasword = $this->input->get('paasword');
        $city = $this->input->get('city');
        $infofrom = $this->input->get('infofrom');
        $passport_uid = $this->input->get('passport_uid');

        $is_md5 = $this->input->get('is_md5');
        $register_from = $this->input->get('register_from');

        if($register_from=="" or $register_from=0){
            $register_from  = 4;
        }

        $url = "api.house365.com/esf/web/api_4002.php?&stc=".urlencode($telno);
        //ECHO $url;
        curl_get_contents($url);

        //if (!preg_match('/^[_0-9a-z]{6,16}$/i',$paasword))
        //{
        //密码格式不对
        //	$return = 'N_PASS';
        //}

        //记录日志
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s').var_export($telno,true)." \n";
        $log = dirname(__FILE__)."/esf_center_api_api_adduser_mobile.log";
        file_put_contents($log,$msg,FILE_APPEND );


        //验证此号码是否已经注册
        $result = $this->user_model->yztelno($telno);
        if( $result > 0 )
        {   //修改用户中心ID
            $this->user_model->updateuser($result,array('passport_uid'=>$passport_uid));
            //已经注册
            $return = 'N_REG';
        }
        else if($return != 'N_PASS')
        {
            $result = $this->user_model->adduser($telno,$paasword,$city,$infofrom,$passport_uid,$is_md5,$register_from);
            $return = $result;
        }

        //密码格式不对
        if($return == 'N_PASS')
        {
            $result = 2;
            $msg = '密码格式不对';
        }
        else if($return == 'N_REG')
        {
            $result = 3;
            $msg = '号码已被注册';
        }
        else if($return > 0)
        {
            $result = 1;
            $msg = '注册成功';
        }
        else if($return == 0)
        {
            $result = 0;
            $msg = '注册失败';
        }

        $return_array = array('result'=>$result , 'msg'=>$msg);

        echo serialize($return_array);
    }

    /**
     * 同步telno,password 根据直租
     * @author xue
     */
    public function sysTelnoPasswordZhiZu()
    {
        $telno = $this->input->get('telno');
        $password = $this->input->get('password');
        $passport_uid = $this->input->get('passport_uid');
        $code         = $this->input->get('code');

        if($code != md5($telno.'house365'))
        {
            $return_array = array('result'=>1 , 'msg'=>'fail');

            echo json_encode($return_array);die;
        }


        if($telno)
        {
            $result['telno'] =$telno;
        }
        if($password)
        {
            $result['password'] =$password;
        }

        $this->user_model->updateuserbypassworduid($passport_uid,$result);

        $msg = '更新成功';

        $return_array = array('result'=>1 , 'msg'=>$msg);

        echo serialize($return_array);
    }

    /**
     * 用户验证接口
     * @author cxf
     */
    public function api_yztelno()
    {
        $telno = $this->input->get('telno');
        $result = $this->user_model->yztelno($telno);
        echo $result;
    }

    /**
     * 验证用户是否可以登陆接口
     * @author cxf
     */
    public function api_yzlogin()
    {
        $telno = $this->input->get('telno');
        $password = $this->input->get('password');
        $result = $this->user_model->yzlogintelno($telno,$password);

        echo $result;
    }

    /**
     * 验证用户是否可以登陆接口移动专用
     * @author cxf
     */
    public function api_yzloginmobile()
    {
        $telno = $this->input->get('telno');
        $password = $this->input->get('password');
        $return = $this->user_model->yzlogintelno($telno,md5($password));

        if($return > 0){
            $result = 1;
            $msg = '登陆成功';
        }elseif($return == 0){
            $result = 0;
            $msg = '登陆失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }

    /**
     * 提交登陆信息
     * @author cxf
     */
    public function api_sub_login_log_mobile()
    {
        $device_array = $this->user_statistic_model->get_device();

        $telno = $this->input->get('telno');
        $uid = $this->user_model->yztelno($telno);
        $city = $this->input->get('city');
        $device = $this->input->get('device');
        //初始化用户信息
        $this->user_statistic_model->set_uid($uid);
        $this->user_statistic_model->set_city($city);
        //提交登录信息
        $result = $this->user_statistic_model->sub_login_log('mobile',$device_array[$device]);
        echo serialize($result);
    }

    /**
     * 提交登陆操作接口
     *
     * @access public
     * @param  无
     * @return void
     */
    function api_sub_login_log()
    {
        $this->load->model('user_statistic_model');
        $login_from = $this->input->get('login_from');
        $device = $this->input->get('device');
        $uid = $this->input->get('uid');
        $code = $this->input->get('code');
        $city = $this->input->get('city');
        if( $code != md5('my365house'.$uid) )
        {
            exit;
        }

        $this->user_statistic_model->set_city($city);
        $this->user_statistic_model->set_uid($uid);
        $result = $this->user_statistic_model->sub_login_log($login_from,$device);
        echo 1;
    }

    /**
     * 验证用户是否可以登陆接口
     * @author cxf
     */
    public function api_editpassword()
    {
        $telno = $this->input->get('telno');
        $password = $this->input->get('password');
        $return = '';
        if (!preg_match('/^[_0-9a-z]{6,16}$/i',$password)){
            //密码格式不对
            $return = 'N_PASS';
        }

        $result = $this->user_model->yztelno($telno);
        if($result==0)
        {
            //无此用户
            $return = 'N_REG';
        }elseif($return != 'N_PASS'){
            $result = $this->user_model->updatepassowrd($telno,$password);
            $return = $result;
        }

        //密码格式不对
        if($return == 'N_PASS')
        {
            $result = 2;
            $msg = '密码格式不正确';
        }elseif($return == 'N_REG')
        {
            $result = 3;
            $msg = '号码未被注册';
        }elseif($return > 0){
            $result = 1;
            $msg = '修改密码成功';
        }elseif($return == 0){
            $result = 0;
            $msg = '修改密码失败';
        }
        $return_array = array('result'=>$result,'msg'=>$msg);
        echo serialize($return_array);
    }

    //判断房源标签类型
    public function getState($istag, $isreal, $nofee)
    {
        if ($istag == 1) {
            $state = 1;
        } else if ($isreal == 1) {
            $state = 2;
        } else if ($nofee == 1) {
            $state = 3;
        } else {
            $state = 0;
        }
        return $state;
    }

    /**
     * 提交房源操作接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function api_sub_operate_log()
    {
        $this->load->model('house_statistic_model');
        $tbl = $this->input->get('tbl');
        $rowid = $this->input->get('rowid');
        $operate_type = $this->input->get('operate_type');
        $uid = $this->input->get('uid');
        $city = $this->input->get('city');
        $device = $this->input->get('device');
        $code = $this->input->get('code');

        //验证
        if( $code != md5('my365house'.$uid) )
        {
            $result = array ('is_ok' => 1 , 'msg' => '参数不正确');
        }
        else
        {
            $this->house_statistic_model->set_city($city);
            $this->house_statistic_model->set_uid( $uid );

            $result = $this->house_statistic_model->sub_operate_log( $tbl , $rowid , $operate_type , $device );
        }

        echo serialize($result);
    }


    /**
     * 提交房源点击量接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function api_sub_house_click()
    {
        $result = array();
        //GET参数
        $city	= $this->input->get('city');
        $uid	= $this->input->get('uid');
        $tbl	= $this->input->get('tbl');
        $rowid	= $this->input->get('rowid');
        $district	= $this->input->get('district');
        $blockid	= $this->input->get('blockid');
        $streetid	= $this->input->get('streetid');
        $room		= $this->input->get('room');
        $price		= $this->input->get('price');
        $buildarea	= $this->input->get('buildarea');
        $client_ip	= $this->input->get('client_ip');
        $device = $this->input->get('device');
        $device = isset($device) ? $device : '1' ;
        $sub_key = $this->input->get('sub_key');

        if(  in_array($tbl,array('sell','rent')) && $rowid > 0 && $uid > 0 )
        {
            //验证秘钥
            if( $sub_key != md5('house_click_'.$uid.'_'.$tbl.'_'.$rowid) )
            {
                $result = array ('is_ok' => 1 , 'msg' => '参数不正确');
            }
            else
            {
                $this->load->model('house_statistic_model');
                $this->house_statistic_model->set_city( $city );
                $this->house_statistic_model->set_uid( $uid );
                $this->house_statistic_model->set_ip( $client_ip );

                $result = $this->house_statistic_model->sub_house_click( $tbl , $rowid , $district , $blockid , $streetid , $room , $price , $buildarea , $device );
            }
        }
        else
        {
            $result = array ('is_ok' => 1 , 'msg' => '参数不正确');
        }

        echo serialize($result);
    }


    /**
     * 提交房源点击量接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function api_sub_house_refresh()
    {
        $result = array();

        //GET参数
        $city	    = $this->input->get('city');
        $tbl	    = $this->input->get('tbl');
        $rowid	    = $this->input->get('rowid');
        $price   	= $this->input->get('price');
        $order_id	= $this->input->get('order_id');
        $sub_key    = $this->input->get('sub_key');



        if(  in_array($tbl,array('sell','rent')) && $rowid > 0 )
        {
            //验证秘钥
            if( $sub_key != md5('house_refresh_'.'_'.$tbl.'_'.$rowid) )
            {
                $result = array ('is_ok' => 1 , 'msg' => 'sub_key failed');
            }
            else
            {
                $this->load->model('house_statistic_model');
                $this->house_statistic_model->set_city( $city );
                $result = $this->house_statistic_model->sub_house_refresh( $tbl , $rowid , $price , $order_id );
            }
        }
        else
        {
            $result = array ('is_ok' => 1 , 'msg' => 'failed');
        }

        echo json_encode($result);
    }

    /**
     * 获取消息数接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function api_getmessagenumbyuid()
    {
        //GET参数
        $city	= $this->input->get('city');
        $uid	= $this->input->get('uid');

        $return = $this->user_model->getmessagenumbyuidwd($uid,$city);

        echo $return;
    }

    /**
     * 获取未读匹配数接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function api_getmatchnumbyuid()
    {
        //GET参数
        $uid	= $this->input->get('uid');

        $return = $this->user_model->get_match_num_byuid($uid);

        echo $return;
    }


    /**
     * 获取未读回答数接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function api_getnewanswerbyuid()
    {
        //GET参数
        $uid	= $this->input->get('uid');

        $return = $this->ask_model->getnewanswerbyuid($uid);

        echo $return;
    }


    /**
     * 提交可刷新次数接口
     *
     * @access public
     * @param  无
     * @return void
     */
    function api_add_refresh_num()
    {
        $this->load->model('house_model');
        $uid = $this->input->get("uid");
        $id = $this->input->get("id");
        $tbl = $this->input->get("tbl");
        $num = $this->input->get("num");
        $city_name = $this->input->get("city");
        $code = $this->input->get("code");
        $flag = $this->input->get("flag");
        if(empty($flag)){
            $flag=1;
        }
        if($code != md5('my365house'.$uid))
        {
            exit;
        }

        $this->house_model->addrefrshnum($uid,$id,$tbl,$num,$city_name,$flag);
        echo 1;
        exit;
    }


    /**
     * 获取个人出售&出租房源总刷新次数&已经刷新次数
     * @author bsw
     */
    function getRefreshHousePerson($type,$id)
    {
        //验证刷新可用次数
        $num = $this->house_model->yzrefrshnum($this->user_id,$id,$type);

        //获取今日已刷新次数
        $this->house_statistic_model->set_uid($this->user_id);
        $this->house_statistic_model->set_city($this->city);
        $this->house_statistic_model->set_nocache(1);
        $arr = $this->house_statistic_model->get_operate_list(2,$id,date("Y-m-d"));
        return array('total'=>$num,'haveRef'=>count($arr));
    }

    /**
     * 批量获取个人出售&出租房源总刷新次数&已经刷新次数
     * @author bsw
     */
    function getRefreshHousePersonBatch(){
        $type = $this->input->get_post('type');
        $this->user_id = $this->input->get_post('uid');
        $this->city = $this->input->get_post('city');
        $tmp = $arr_ids = $this->input->get_post('arr_ids');
        $arr_ids = json_decode($arr_ids,1);

        $result = array();
        if(is_array($arr_ids) && $arr_ids){
            foreach($arr_ids as $val){
                $result[$val] = $this->getRefreshHousePerson($type,$val);
            }
        }

        echo json_encode($result);
    }



    /**
     * 刷新个人出售房源
     * @author bsw
     */
    function refreshSellPerson(){
        $id = $this->input->get_post('id');
        $telno = $this->input->get_post('telno');
        $version = $this->input->get_post('version');

        $this->user_id = $this->user_model->yztelno($telno);
        $this->city = $this->input->get_post('city');

        $device = $this->input->get_post('device');

        if(!$id || !$this->city || !$this->user_id){
            //参数错误
            $arr1['res'] = 0;
            $arr1['num'] = 0;
            echo json_encode($arr1);exit;
        }

        //获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('sell',$id);
        $num = $refreshInfo['total'] - $refreshInfo['haveRef'];
        if($num>9)
        {
            $num =9;
        }
        //echo $num;die;
        if($num>0){
            //更新房源刷新时间
            $result = $this->house_model->updatehouse($id,'sell',array('updatetime'=>time()));
            //记录刷新操作
            $this->house_statistic_model->set_uid($this->user_id);
            $this->house_statistic_model->set_city($this->city);
            $result = $this->house_statistic_model->sub_operate_log('sell' , $id , 2 ,$device);
            if($result['is_ok'] == 1){
                //发送消息
                $subject='您的房源刷新成功';
                $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
                $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
                //刷新成功
                $arr1['res'] = 1;
                // echo $num;die;
                $arr1['num'] = $num-1;
                echo json_encode($arr1);exit;
            }else{
                //刷新失败
                $arr1['res'] = 2;
                $arr1['num'] = $num;
                echo json_encode($arr1);exit;
            }
        }else{
            //刷新次数不足
            $arr1['res'] = 3;
            $arr1['num'] = $num;
            echo json_encode($arr1);exit;
        }
    }

    /**
     * 刷新个人出租房源
     * @author bsw
     */
    function refreshRentPerson(){
        $id = $this->input->get_post('id');
        $telno = $this->input->get_post('telno');
        $version = $this->input->get_post('version');
        $this->user_id = $this->user_model->yztelno($telno);
        $this->city = $this->input->get_post('city');

        $device = $this->input->get_post('device');

        if(!$id || !$this->city || !$this->user_id){
            //参数错误
            $arr1['res'] = 0;
            $arr1['num'] = 0;
            echo json_encode($arr1);exit;
        }

        //获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('rent',$id);
        $num = $refreshInfo['total'] - $refreshInfo['haveRef'];
        if($num>9)
        {
            $num=9;
        }
        if($num>0){
            //更新房源刷新时间
            $result = $this->house_model->updatehouse($id,'rent',array('updatetime'=>time()));
            //记录刷新操作
            $this->house_statistic_model->set_uid($this->user_id);
            $this->house_statistic_model->set_city($this->city);
            $result = $this->house_statistic_model->sub_operate_log('rent' , $id , 2 ,$device);
            if($result['is_ok'] == 1){
                //发送消息
                $subject='您的房源刷新成功';
                $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
                $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
                //刷新成功
                $arr1['res'] = 1;
                $arr1['num'] = $num-1;
                echo json_encode($arr1);exit;
            }else{
                //刷新失败
                $arr1['res'] = 2;
                $arr1['num'] = $num;
                echo json_encode($arr1);exit;
            }
        }else{
            //刷新次数不足
            $arr1['res'] = 3;
            $arr1['num'] = $num;
            echo json_encode($arr1);exit;
        }
    }

    public function get_operate_list_api() {
        $id = $this->input->get_post('id');
        $telno = intval($this->input->get_post('telno'));

        $city = $this->input->get('city');
        $this->load->model('house_statistic_model');
        $this->house_statistic_model->set_uid($uid);
        $this->house_statistic_model->set_city($city);

        echo serialize($this->house_statistic_model->get_operate_list(5 , $id,date('Y-m-d',time()))) ;
    }


    /**
     * 添加个人的推送信息接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function addpush() {

        //记录日志
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s')." \n";
        $log = dirname(__FILE__)."/addpush.log";
        file_put_contents($log,$msg,FILE_APPEND );


        $telno = $this->input->get_post('telno');
        $uid = $this->user_model->yztelno($telno);
        $city = $this->input->get_post('city');
        $city = isset($city)?$city:'nj';
        $code = $this->input->get_post('code');
        if($code != md5('my365house'.$telno) || empty($telno))
        {
            echo 'N_CHECK';exit;
        }
        //$msg = "*********************************************** \n";
        //$msg .= date('y-m-d H:i:s')."::telno-".$telno." \n";
        //$log = 'log/addpushlog'.date('Y-m-d');
        //@file_put_contents($log,$msg,FILE_APPEND );

        //验证不在白名单，不在推送库，不在推送历史库
        $result = $this->push_model->yzpush($telno);

        //added by bianshiwu 临时日志
        //$data['sendtime'] = date('Y-m-d H:i:s');
        //$data['telno'] = $telno;
        //$data['desc'] = $result;
        //$this->push_model->setPushLogTemp($data);

        //$msg = date('y-m-d H:i:s')."::result-".$result." \n";
        //$log = 'log/addpushlog'.date('Y-m-d');
        //@file_put_contents($log,$msg,FILE_APPEND );

        if($result>0){
            echo 'N_CHECK_Y';exit;
        }
        //如果有推送历史，只推送上次推送之后一个月的行为
        $sendtime = $this->push_model->getpushlogbytelno($telno);
        $sendtime = 86400*30;

        //读取求购
        $result = $this->push_model->addbuy($uid,$city,$sendtime);
        if($result>0){
            //加入成功，删除备选库
            $this->push_model->delalternatives($telno);
            echo 'Y';exit;
        }
        //读取求租
        $result = $this->push_model->addrentwanted($uid,$city,$sendtime);
        if($result>0){
            //加入成功，删除备选库
            $this->push_model->delalternatives($telno);
            echo 'Y';exit;
        }
        //预约看房
        /*
        $result = $this->push_model->addseehouse($uid,$telno,$sendtime);
        if($result>0){
         //加入成功，删除备选库
         $this->push_model->delalternatives($telno);
         echo 'Y';exit;
        }


        //关注房源
        $result = $this->push_model->addcollecthouse($uid,$telno,$sendtime);
        if($result>0){
         //加入成功，删除备选库
         $this->push_model->delalternatives($telno);
         echo 'Y';exit;
        }


        //发送到手机
        $result = $this->push_model->addsmshouse($uid,$telno,$sendtime);
        if($result>0){
         //加入成功，删除备选库
         $this->push_model->delalternatives($telno);
         echo 'Y';exit;
        }

        //关注小区
        $result = $this->push_model->addcollectblock($uid,$telno,$sendtime);
        if($result>0){
         //加入成功，删除备选库
         $this->push_model->delalternatives($telno);
         echo 'Y';exit;
        }
        */

        echo 'N';exit;
    }


    /**
     * 添加个人的进入备选库接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function addalternatives() {
        $telno = $this->input->get_post('telno');
        $sendkind = $this->input->get_post('sendkind');
        $tbl = $this->input->get_post('tbl');
        $uid = $this->user_model->yztelno($telno);
        $code = $this->input->get_post('code');
        $city = $this->input->get_post('city');
        $city = !empty($city)?$city:GB_CITY;
        if($code != md5('my365house'.$telno) or empty($uid) or empty($telno))
        {
            echo 'N_CHECK';exit;
        }
        //验证不在白名单，不在推送库，不在推送历史库最近一个月
        $result = $this->push_model->yzpush($telno);
        if($result>0){
            echo 'N_CHECK_Y';exit;
        }
        //加入备选库
        $result = $this->push_model->addalternatives($uid,$telno,$city,$sendkind,$tbl);
        if($result>0){
            echo 'Y';exit;
        }

        echo 'N';exit;
    }

    //临时-添加数据
//    public function Temppushadd()
//    {
//        echo '添加数据：<br><br>';
//        $res = $this->push_model->t_add();
//
//        echo '<br><br>-------------';
//        die;
//    }


    //测试中间件返回数据
    public function test_push()
    {
        //$result = $this->push_model->bidhouse();
        echo '---- over -----';
        exit;
        //$this->push_model->test_push();
//        error_reporting(E_ALL);
//        ini_set("display_errors",TRUE);
        $result = $this->push_model->getpusharraynew();
    }

    /**
     * 获取推送信息接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function Getpush()
    {
        //记录日志
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s')." \n";
        $log = dirname(__FILE__)."/addpush.log";
        file_put_contents($log,$msg,FILE_APPEND );

        //当前方法执行会锁定该方法，防止重复执行
        $key  = "Job::Getpush";
        $check = $this->memcached_class->get($key);
        if($check !== false){
            //echo json_encode('N_LOCKED');exit;
        }
        $this->memcached_class->add($key,true,1200);

        $time1=strtotime(date("Y-m-d "."8:30",time()));
        $time2=strtotime(date("Y-m-d "."21:30",time()));
        if(time()<$time1 || time()>$time2){
            //echo json_encode('N_TIMEOUT');exit;
        }
        $code = $this->input->get_post('code');
        if($code != md5("my365house::ADMINPUSH")){
            //echo json_encode('N_CHECK');exit;
        }
        $remainNum = $this->input->get_post('remainNum');
        if(intval($remainNum) <= 0){
            //echo json_encode('N_REMAINNUM');exit;
        }
        $result = $this->push_model->getpusharraynew();
        //$result = $this->push_model->getpusharray($remainNum);
        echo json_encode($result);
    }


    /**
     * 分站获取推送信息接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function Getpushcity()
    {

        $city = $this->input->get_post('city');
        //记录日志
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s')." \n";
        $log = dirname(__FILE__)."/addpush_".$city."log";
        file_put_contents($log,$msg,FILE_APPEND );

        //当前方法执行会锁定该方法，防止重复执行
        $key  = "Job::Getpush".$city;
        $check = $this->memcached_class->get($key);
        if($check !== false){
            //echo json_encode('N_LOCKED');exit;
        }
        $this->memcached_class->add($key,true,1200);

        $time1=strtotime(date("Y-m-d "."8:30",time()));
        $time2=strtotime(date("Y-m-d "."21:30",time()));
        if(time()<$time1 || time()>$time2){
            //echo json_encode('N_TIMEOUT');exit;
        }
        $code = $this->input->get_post('code');
        if($code != md5("my365house::ADMINPUSH")){
            //echo json_encode('N_CHECK');exit;
        }
        $remainNum = $this->input->get_post('remainNum');
        if(intval($remainNum) <= 0){
            //echo json_encode('N_REMAINNUM');exit;
        }

        $result = $this->push_model->getpusharraycity($remainNum,$city);
        echo json_encode($result);
    }



    /**
     * 获取推送信息接口
     *
     * @access public
     * @param  无
     * @return void
     */
    public function Getpushtest()
    {

        //记录日志
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s')." \n";
        $log = dirname(__FILE__)."/addpushtest.log";
        file_put_contents($log,$msg,FILE_APPEND );

        //当前方法执行会锁定该方法，防止重复执行
        $key  = "Job::Getpush";
        $check = $this->memcached_class->get($key);
        if($check !== false){
            //echo json_encode('N_LOCKED');exit;
        }
        $this->memcached_class->add($key,true,1200);

        $time1=strtotime(date("Y-m-d "."8:30",time()));
        $time2=strtotime(date("Y-m-d "."21:30",time()));
        if(time()<$time1 || time()>$time2){
            echo json_encode('N_TIMEOUT');exit;
        }
        $code = $this->input->get_post('code');
        if($code != md5("my365house::ADMINPUSH")){
            //echo json_encode('N_CHECK');exit;
        }
        $remainNum = $this->input->get_post('remainNum');
        if(intval($remainNum) <= 0){
            //echo json_encode('N_REMAINNUM');exit;
        }
        $result = $this->push_model->getpusharraytest($remainNum);
        echo json_encode($result);
    }

    /**
     * 根据经纪人id获取未抢完用户推送列表
     *
     * @param  ctime 开始时间，$is_buy是否购买
     * @access public
     * @return void
     */
    public function Getpushbybrokerid()
    {
        $brokerid = $this->input->get_post('brokerid');
        if(empty($brokerid)){echo json_encode(0);exit;}
        $ctime = $this->input->get_post('ctime');
        $ctime = $ctime>(time()-60*60*24*30)?$ctime:(time()-60*60*24*30);
        $is_buy = $this->input->get_post('is_buy');
        $is_buy= !empty($is_buy)?$is_buy:0;
        $page = $this->input->get_post('page');
        $pagesize = $this->input->get_post('pagesize');
        $code = $this->input->get_post('code');
        if($code != md5('my365house'.$brokerid))
        {
            echo 'N_CHECK';exit;
        }
        $result = $this->push_model->Getpushnumbybrokerid($brokerid,$ctime,$is_buy,$page,$pagesize);
        echo json_encode($result);
    }

    /**
     * 根据经纪人id获取已经推送的数量
     *
     * @param  ctime 开始时间，$is_buy是否购买
     * @access public
     * @return void
     */
    public function Getpushnumbybrokerid()
    {
        $brokerid = $this->input->get_post('brokerid');
        if(empty($brokerid)){echo json_encode(0);exit;}
        $ctime = $this->input->get_post('ctime');
        $ctime = $ctime>(time()-60*60*24*30)?$ctime:(time()-60*60*24*30);
        $is_buy = $this->input->get_post('is_buy');
        $is_buy= !empty($is_buy)?$is_buy:0;
        $code = $this->input->get_post('code');
        if($code != md5('my365house'.$brokerid))
        {
            echo 'N_CHECK';exit;
        }

        $result = $this->push_model->Getpushnumbybroker($brokerid,$ctime,$is_buy);
        echo json_encode($result);
    }

    /**
     * 获取推送信息的详细信息
     *
     * @access public
     * @param  telno
     * @return void
     */
    public function Getsellnumbypush()
    {
        $telno = $this->input->get_post('telno');
        $sendid = $this->input->get_post('sendid');
        $sendkind = $this->input->get_post('sendkind');
        if(empty($telno)){echo json_encode(0);exit;}
        $code = $this->input->get_post('code');

        if($code != md5('my365house'.$telno))
        {
            echo 'N_CHECK';exit;
        }

        $result = $this->push_model->Getsellnumbypush($telno,$sendid,$sendkind);
        echo json_encode($result);
    }

    /**
     * 修改经纪人出售或者查看
     *
     * @access public
     * @param  $type 1,更新是否电话，2更新是否购买
     * @return void
     */
    public function Updatebrokerpush()
    {
        $brokerid = $this->input->get_post('brokerid');
        $pushid = $this->input->get_post('pushid');
        if(empty($brokerid)){echo json_encode(0);exit;}
        $type = $this->input->get_post('type');
        $type= !empty($type)?$type:1;
        $code = $this->input->get_post('code');
        $code_yz = $type==1?md5('my365house'.'::'.$brokerid):md5('my365house'.'::'.$brokerid.'::'.$pushid);
        if($code != $code_yz)
        {
            echo 'N_CHECK';exit;
        }
        $result = $this->push_model->Updatebrokerpush($brokerid,$type,$pushid);
        echo json_encode($result);
    }


    /**
     * 根据用户行为修改用户的城市
     *
     * @access public
     * @param  $city,$telno,$uid
     * @return void
     */
    public function Updateusercity()
    {
        $uid = $this->input->get_post('uid');

        if(empty($uid))
        {
            $telno = $this->input->get_post('telno');
            $uid = $this->user_model->yztelno($telno);
        }

        if(empty($uid))
        {
            return 0;
        }

        $city = $this->input->get_post('city');
        $city = !empty($city)?$city:GB_CITY;
        $result = $this->user_model->Updateusercity($uid,$city);

        echo json_encode($result);
    }

    //获取微信二维码
    function getWxPic(){
        $house = intval($_GET['id']);
        $tbl = $_GET['tbl'];
        if($tbl=='sell'){
            $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://nj.sell.house365.com/s_".$house.".html";
        }else{
            $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://nj.rent.house365.com/r_".$house.".html";
        }

        echo curl_get_contents($wxapi);
    }

    //获取合肥微信二维码
    function getHfWxPic(){
        $house = intval($_GET['id']);
        $tbl = $_GET['tbl'];
        if($tbl=='sell'){
            $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://hf.sell.house365.com/s_".$house.".html";
        }else{
            $wxapi = "http://api.house365.com/esf/web/ewm/?url=http://hf.rent.house365.com/r_".$house.".html";
        }

        echo curl_get_contents($wxapi);
    }


    function get_house_clicknum_by_id()
    {
        $this->load->model('house_statistic_model');
        $rowid = $this->input->get('rowid');
        $tbl = $this->input->get('tbl');
        $uid = $this->input->get('uid');
        $city = $this->input->get('city');

        $this->house_statistic_model->set_uid( $uid );
        $click_num = $this->house_statistic_model->get_house_clicknum_by_id($rowid,$tbl,"",$city);

        echo $click_num;
        exit();

    }


    function sms_check_state()
    {
        $this->load->model('house_statistic_model');
        $telno = $this->input->get('telno');
        $status = $this->house_statistic_model->get_telno_state($telno);
        echo $status;exit;

    }

    function sms_check_state_app()
    {
        $this->load->model('house_statistic_model');
        $telno = $this->input->get('telno');
        $status = $this->house_statistic_model->get_telno_state_app($telno);
        echo $status;exit;

    }

    function check_validcode_state()
    {
        $this->load->model('house_statistic_model');
        $telno = $this->input->get('telno');
        $validcode = $this->input->get('validcode');
        $status = $this->house_statistic_model->check_validcode_state($telno,$validcode);

        echo $status;exit;
    }

    function check_validcode_state_app()
    {
        $this->load->model('house_statistic_model');
        $telno = $this->input->get('telno');
        $validcode = $this->input->get('validcode');
        $status = $this->house_statistic_model->check_validcode_state_app($telno,$validcode);

        echo $status;exit;
    }

    function update_validcode_state()
    {
        $this->load->model('house_statistic_model');
        $telno = $this->input->get('telno');
        $validcode = $this->input->get('validcode');
        $status = $this->house_statistic_model->update_validcode_state($telno,$validcode);
        echo $status;exit;
    }

    function update_validcode_state_app()
    {
        $this->load->model('house_statistic_model');
        $telno = $this->input->get('telno');
        $validcode = $this->input->get('validcode');
        $status = $this->house_statistic_model->update_validcode_state_app($telno,$validcode);
        echo $status;exit;
    }


    /**
     * 获取智能自动刷新套餐使用详情 by loukai
     * 线上由159服务器调用   在housePersonNew/autoRefreshDetail
     * 测试地址：http://user.house365.com/esf_center_api/autoRefreshDetail
     */
    function autoRefreshDetail(){

        $this->load->model('house_statistic_model');
        $houseid = $this->input->get('id');
        $tbl = $this->input->get('tbl'); //出租还是出售
        //测试数据
//        $houseid = 169674671;
//        $tbl = 1;
        //end
        if(!$houseid || !$tbl){
            //参数错误
            $result['result'] = 0;
            $result['msg'] = iconv('gbk','utf-8',"参数错误");
            echo json_encode($result);exit;
        }

        //获取最新一次购买的套餐
        $promote_id = $this->house_statistic_model->getLastAutoRefreshPackageOrder($houseid,$tbl);
        //套餐开始时间
        $result['package_starttime'] = $this->house_statistic_model->getAutoRefreshPackageStartTime($houseid,$tbl,$promote_id);
        //已刷新次数
        $result['used_nums'] = $this->house_statistic_model->getAutoRefreshPackageUsedNums($houseid,$tbl,$promote_id);

        //总刷新次数
        $result['total_nums'] = $this->house_statistic_model->getAutoRefreshPackageTotalNums($promote_id);

        //预计最后一次刷新时间
        $result['guess_last_refresh_time'] = $this->house_statistic_model->getGuessLastRefreshTime($houseid,$tbl,$promote_id);

        //刷新详情
        $details = $this->house_statistic_model->getAutoRefreshDetail($houseid,$tbl,$promote_id);
        $result['total_details_nums'] = $details['total'];
        $result['details'] = $details['data'];
        $res['data'] = $result;
        $res['result'] = 1;
        $res['msg'] = 'success';
        echo json_encode($res);
        exit;
    }
    /**
     * refreshSellPerson
     * 刷新APP个人房源（出售）
     */
    function refreshSellPersonApp()
    {
        $id = $this->input->get_post('id');
        $telno = $this->input->get_post('telno');
        $version = $this->input->get_post('version');

        $this->user_id = $this->user_model->yztelno($telno);
        $this->city = $this->input->get_post('city');

        $device = $this->input->get_post('device');

        if(!$id || !$this->city || !$this->user_id){
            //参数错误
            $arr1['res'] = 0;
            $arr1['num'] = 0;
            echo json_encode($arr1);exit;
        }

        //获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('sell',$id);
        if($version>='6.0.4')
        {
            if($refreshInfo['total']<9)
            {
                $num = $refreshInfo['total'] - $refreshInfo['haveRef']+1;
            }
            else
            {
                $num = $refreshInfo['total'] - $refreshInfo['haveRef'];
            }
            //$num = $refreshInfo['total'] - $refreshInfo['haveRef'];
        }
        else
        {
            $num = $refreshInfo['total'] - $refreshInfo['haveRef'];
        }
        if($num>9)
        {
            $num =9;
        }
        if($num>0){
            //更新房源刷新时间
            $result = $this->house_model->updatehouse($id,'sell',array('updatetime'=>time()));
            //记录刷新操作
            $this->house_statistic_model->set_uid($this->user_id);
            $this->house_statistic_model->set_city($this->city);
            $result = $this->house_statistic_model->sub_operate_log('sell' , $id , 2 ,$device);
            if($result['is_ok'] == 1){
                //发送消息
                $subject='您的房源刷新成功';
                $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
                $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
                //刷新成功
                $arr1['res'] = 1;
                // echo $num;die;
                $arr1['num'] = $num-1;
                echo json_encode($arr1);exit;
            }else{
                //刷新失败
                $arr1['res'] = 2;
                $arr1['num'] = $num;
                echo json_encode($arr1);exit;
            }
        }else{
            //刷新次数不足
            $arr1['res'] = 3;
            $arr1['num'] = $num;
            echo json_encode($arr1);exit;
        }

    }

    /**
     * 刷新APP端个人出租房源
     * @author bsw
     */
    function refreshRentPersonApp(){

        $id = $this->input->get_post('id');
        $telno = $this->input->get_post('telno');
        $version = $this->input->get_post('version');
        $this->user_id = $this->user_model->yztelno($telno);
        $this->city = $this->input->get_post('city');

        $device = $this->input->get_post('device');

        if(!$id || !$this->city || !$this->user_id){
            //参数错误
            $arr1['res'] = 0;
            $arr1['num'] = 0;
            echo json_encode($arr1);exit;
        }

        //获得实际可刷新次数
        $refreshInfo = $this->getRefreshHousePerson('rent',$id);
        if($version>='6.0.4')
        {
            if($refreshInfo['total']<9)
            {
                $num = $refreshInfo['total'] - $refreshInfo['haveRef']+1;
            }
            else
            {
                $num = $refreshInfo['total'] - $refreshInfo['haveRef'];
            }
        }
        else
        {
            $num = $refreshInfo['total'] - $refreshInfo['haveRef'];
        }
        if($num>9)
        {
            $num=9;
        }

        if($num>0){
            //更新房源刷新时间
            $result = $this->house_model->updatehouse($id,'rent',array('updatetime'=>time()));
            //记录刷新操作
            $this->house_statistic_model->set_uid($this->user_id);
            $this->house_statistic_model->set_city($this->city);
            $result = $this->house_statistic_model->sub_operate_log('rent' , $id , 2 ,$device);
            if($result['is_ok'] == 1){
                //发送消息
                $subject='您的房源刷新成功';
                $msg='您编号为"'.$id.'"房源刷新成功。如有疑问，请联系客服。';
                $this->apply_class->send_systemmsg($this->user_id,$subject,$msg,1,GB_CITY);
                //刷新成功
                $arr1['res'] = 1;
                $arr1['num'] = $num-1;
                echo json_encode($arr1);exit;
            }else{
                //刷新失败
                $arr1['res'] = 2;
                $arr1['num'] = $num;
                echo json_encode($arr1);exit;
            }
        }else{
            //刷新次数不足
            $arr1['res'] = 3;
            $arr1['num'] = $num;
            echo json_encode($arr1);exit;
        }


    }


    //通过全站用户中心passport_uid获取二手房uid
    function getEsfUidByPassportUid(){
        $passport_uid = $this->input->get('passport_uid');
        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);
        if($uid>0){
            $arr1['res'] = 1;
            $arr1['msg'] = iconv('gbk','utf-8',"获取用户uid成功");
            $arr1['data'] = array('uid'=>$uid);

        }else{
            $arr1['res'] = 0;
            $arr1['msg'] = iconv('gbk','utf-8',"获取用户uid失败");
            $arr1['data'] = array();
        }
        echo json_encode($arr1);exit;


    }

    //获取求购求租匹配数据
    public function get_want_match(){
        //error_reporting(E_ALL);
        //  ini_set("display_errors",TRUE);

        global $house;
        //获取表单数据
        $want_id = $this->input->get('want_id',TRUE);
        $want_type = strip_tags($this->input->get('want_type',TRUE));
        //$page = $this->input->get('page',TRUE);
        $data['priceunit'] = $house['priceunit'];

        $this->_limit = 20;

        if( $want_id > 0 && in_array( $want_type , array('buy','rentwant') ))
        {
            //加载房源操作统计类
            $this->load->model('want_house_model');

            $want_arr = array ('buy'=> array('house_tbl'=>'sell','match_tbl'=>'buy_match'),'rentwant'=> array('house_tbl'=>'rent','match_tbl'=>'rentwanted_match'));

            //设置房源表
            $this->want_house_model->set_tbl_house($want_arr[$want_type]['house_tbl']);

            //设置匹配表
            $this->want_house_model->set_tbl_match($want_arr[$want_type]['match_tbl']);
            if($want_type=='buy')
            {
                $fields_base = array('id','district','streetid','blockshowname','infotype','infofrom','buildarea','price','room','hall','fitment','floor','subfloor','totalfloor','address');
            }else{
                $fields_base = array('id','district','streetid','blockshowname','infotype','infofrom','buildarea','price','room','hall','fitment','floor','subfloor','totalfloor','priceunit','address');
            }
            $fields_t['buy'] = array('buildyear');
            $fields_t['rentwant'] = array('renttype');
            $fields = array_merge( $fields_base , $fields_t[$want_type]);
            $this->want_house_model->set_house_fields($fields);

            //符合条件的总行数
            $this->_total_count = $this->want_house_model->get_matched_num_by_id( $want_id );
            //计算总页数
            $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / 6 ) : 0;

            if($page>$pages)
            {
                $page=$pages;
            }

            //获取数据源

            $statistic_data = $this->want_house_model->get_matched_by_id( $want_id , $this->_offset , $this->_limit );

            if(is_array($statistic_data)&&!empty($statistic_data)){
                foreach($statistic_data as $key=>$value){
                    $statistic_data[$key]['url'] = $want_type == 'buy' ? get_sellinfo_url($value['id']) : get_rentinfo_url($value['id']);
                    $statistic_data[$key]['price'] = $want_type == 'buy' ? $value['price']."万元" : $value['price'].$house['priceunit'][$value['priceunit']];
                    $statistic_data[$key]['title'] = $value['address'];
                    unset($statistic_data[$key]['address']);
                }

            }
        }
        else
        {
            $this->_total_count = 0;
            $pages  = 0;
            $statistic_data = array();
        }


        $return_arr = array('total'=>$this->_total_count,'value'=>strIconv($statistic_data));
        echo json_encode(array('result'=>'1','msg'=>'success','data'=>$return_arr));




    }




    //获取求购求租匹配数据
    public function get_want_match_num(){
        //error_reporting(E_ALL);
        //  ini_set("display_errors",TRUE);


        //获取表单数据
        $want_id = $this->input->get('want_id',TRUE);
        $want_type = strip_tags($this->input->get('want_type',TRUE));


        if( $want_id > 0 && in_array( $want_type , array('buy','rentwant') ))
        {
            //加载房源操作统计类
            $this->load->model('want_house_model');

            $want_arr = array ('buy'=> array('house_tbl'=>'sell','match_tbl'=>'buy_match'),'rentwant'=> array('house_tbl'=>'rent','match_tbl'=>'rentwanted_match'));
            //设置房源表
            $this->want_house_model->set_tbl_house($want_arr[$want_type]['house_tbl']);
            //设置匹配表
            $this->want_house_model->set_tbl_match($want_arr[$want_type]['match_tbl']);

            //符合条件的总行数
            $this->_total_count = $this->want_house_model->get_matched_num_by_id( $want_id );
        }
        else
        {
            $this->_total_count = 0;
            $pages  = 0;
            $statistic_data = array();
        }


        $return_arr = array('total'=>$this->_total_count);
        echo json_encode(array('result'=>'1','msg'=>'success','data'=>$return_arr));




    }

    function get_refresh_num()
    {
        $passport_uid = $this->input->get('passport_uid');
        //根据passport_uid获取uid
        $uid = $this->user_model->getUidByPassportId($passport_uid);

        //$this->load->model("user_zhou_model");
        $result =array();
        $result['result'] ='0';
        $result['msg'] ='error';
        $result['data'] =array();
        if(isset($_GET['houseid']))
        {
            $houseid =$_GET['houseid'];
            $houseid_arr =explode(",",$houseid);

            $value =array();
            foreach ($houseid_arr as $key =>$rs)
            {
                $id =$rs;

                $refresh_num = $this->user_model->getRefreshByidByZhiZu('rent',$id,$uid);

                $value[$rs] =$refresh_num;

            }

            $result['result'] ='1';
            $result['msg'] ='success';
            $result['data'] =$value;
            echo  json_encode($result);die;

        }
        else
        {

            echo  json_encode($result);die;
        }


    }

    public function notify(){
        $data = $_POST;
        $this->user_model->addLog("微信支付回调参数数据2：", json_encode( $data ) );
//        $msg = "************************************************ \n";
//        $msg .= date('Y-m-d H:i:s').var_export($_POST,true)." \n";
//        $log = dirname(__FILE__)."/notify";
//        file_put_contents($log,$msg,FILE_APPEND );
//        $log2 = dirname(__FILE__ ) . "/notifylog2.txt";
//        file_put_contents( $log2, $msg,FILE_APPEND );
        $result['status'] = 'FAIL';
        if(GB_CITY != 'nj' && GB_CITY != 'wh' ){
            $result['msg'] = 'city error';
            echo json_encode($result);exit;
        }
        if($data['status'] != 'SUCCESS'){
            $result['msg'] = 'status error';
            echo json_encode($result);exit;
        }
        $this->load->model("package_model");
        $sign = $this->package_model->makeSign($data);
        if($sign != $data['sign']){
            $result['msg'] = 'sign error';
            echo json_encode($result);exit;
        }
        $transactionId = $data['tradeNo'];
        $payResult = json_decode( urldecode($data['payResult']),true);
        //获取订单信息
        $orderInfo = $this->package_model->getOrderInfoByTransactionId($transactionId);
        //判断订单状态
        if($orderInfo['order_status'] != 1){
            $result['msg'] = 'order close';
            echo json_encode($result);exit;
        }
        if($orderInfo['pay_status'] != 0){
            $result['msg'] = 'order paid';
            echo json_encode($result);exit;
        }

        //支付宝
        if ($orderInfo['pay_type'] == 'alipay') {
            if (isset($payResult['total_amount'])) {
                $payResult['total_fee'] = $payResult['total_amount'] * 100;
            }
            if (isset($payResult['trade_no'])) {
                $payResult['transaction_id'] = $payResult['trade_no'];
            }
        }

        if($orderInfo['price'] * 100 != $payResult['total_fee']){
            $result['msg'] = 'total fee error';
            echo json_encode($result);exit;
        }

        // 检查是否使用8折优惠券
        // 获取passport_uid
        // 优惠券南京站外报错！！！！！，联系张俊
        if (GB_CITY == 'nj') {
            $userInfoData = $this->user_model->getuserdetail($orderInfo['user_id']);
            $passport_uid = $userInfoData ? $userInfoData['passport_uid'] : 0;
            if ($this->rent_coupon_model->checkCheckBox($passport_uid)) {
                $this->rent_coupon_model->useCoupon($passport_uid);
            }
        }

        //支付成功后的操作
        $result = $this->package_model->payFinish($orderInfo,$payResult);
        if($result){
            if ( GB_CITY == 'nj' ) {
                $this->sendMsg( $orderInfo );
            }

            echo 'SUCCESS';
        }
    }

    /**
     * 0元支付回调
     */
    public function zeroNotify() {
        $result['status'] = 0;
        $data = $_POST;
        $transactionId = isset( $data['transaction_id'] ) ? $data['transaction_id'] : '';

        // 订单数据
        $orderInfo = $this->package_model->getOrderInfoByTransactionId( $transactionId );
        if ( empty( $orderInfo ) ) {
            $result['msg'] = '订单数据不存在';
            echo json_encode($result);exit;
        }

        //判断订单状态
        if($orderInfo['order_status'] != 1){
            $result['msg'] = '订单已关闭';
            echo json_encode($result);exit;
        }

        if($orderInfo['pay_status'] != 0){
            $result['msg'] = '订单已失效';
            echo json_encode($result);exit;
        }

        $payTransactionId = isset( $data['pay_transaction_id'] ) ? $data['pay_transaction_id'] : '';
        if ( empty( $payTransactionId ) ) {
            $result['msg'] = '支付凭证不能为空';
            echo json_encode($result);exit;
        }

        //支付成功后的操作
        $res = $this->package_model->payFinish( $orderInfo, array( 'transaction_id' => $payTransactionId ) );
        if( !$res ) {
            $result['msg'] = '生成推广相关数据失败';
            echo json_encode($result);exit;
        }

        // 发送短信
        if ( GB_CITY == 'nj' ) {
            $this->sendMsg( $orderInfo );
        }

        $result['status'] = 1;
        $result['msg'] = '成功';
        echo json_encode($result);exit;
    }


    // 支付回调修复数据 20200605
    public function notify_modify_data(){
        $data = $_POST;
        echo 'mod';
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s').var_export($_POST,true)." \n";
        $log = dirname(__FILE__)."/notify";
        file_put_contents($log,$msg,FILE_APPEND );
        $result['status'] = 'FAIL';
//        if(GB_CITY != 'nj' && GB_CITY != 'wh' ){
//            $result['msg'] = 'city error';
//            echo json_encode($result);exit;
//        }
//        if($data['status'] != 'SUCCESS'){
//            $result['msg'] = 'status error';
//            echo json_encode($result);exit;
//        }
        $this->load->model("package_model");
//        $sign = $this->package_model->makeSign($data);
//        if($sign != $data['sign']){
//            $result['msg'] = 'sign error';
//            echo json_encode($result);exit;
//        }
        $transactionId = $data['tradeNo'];
//        $payResult = json_decode( urldecode($data['payResult']),true);

        $payResult = array('transaction_id'=>$data['payment_order_id']);
        //获取订单信息
        $orderInfo = $this->package_model->getOrderInfoByTransactionId($transactionId);
        //判断订单状态
        if($orderInfo['order_status'] != 1){
            $result['msg'] = 'order close';
            echo json_encode($result);exit;
        }
//        if($orderInfo['pay_status'] != 0){
//            $result['msg'] = 'order paid';
//            echo json_encode($result);exit;
//        }

//        //支付宝
//        if ($orderInfo['pay_type'] == 'alipay') {
//            if (isset($payResult['total_amount'])) {
//                $payResult['total_fee'] = $payResult['total_amount'] * 100;
//            }
//            if (isset($payResult['trade_no'])) {
//                $payResult['transaction_id'] = $payResult['trade_no'];
//            }
//        }

//        if($orderInfo['price'] * 100 != $payResult['total_fee']){
//            $result['msg'] = 'total fee error';
//            echo json_encode($result);exit;
//        }
        // 检查是否使用8折优惠券
        // 获取passport_uid
//        $userInfoData = $this->user_model->getuserdetail($orderInfo['user_id']);
//        $passport_uid = $userInfoData ? $userInfoData['passport_uid'] : 0;
//        if($this->rent_coupon_model->checkCheckBox($passport_uid)){
//            $this->rent_coupon_model->useCoupon($passport_uid);
//        }

        //支付成功后的操作
//        $result = $this->package_model->payFinish($orderInfo,$payResult);
//        if($result){
//            echo 'SUCCESS';
//        }
    }



    public function notify_test(){
        $data = $_POST;
        $msg = "************************************************ \n";
        $msg .= date('Y-m-d H:i:s').var_export($_POST,true)." \n";
        $log = dirname(__FILE__)."/notify_test";
        file_put_contents($log,$msg,FILE_APPEND );
        $result['status'] = 'FAIL';
        if(GB_CITY != 'nj'){
            $result['msg'] = 'city error';
            echo json_encode($result);exit;
        }
        if($data['status'] != 'SUCCESS'){
            $result['msg'] = 'status error';
            echo json_encode($result);exit;
        }
        $this->load->model("package_model");
        $sign = $this->package_model->makeSign($data);
        if($sign != $data['sign']){
            $result['msg'] = 'sign error';
            echo json_encode($result);exit;
        }
        $transactionId = $data['tradeNo'];
        $payResult = json_decode( urldecode($data['payResult']),true);
        //获取订单信息
        $orderInfo = $this->package_model->getOrderInfoByTransactionId($transactionId);
        //判断订单状态
        if($orderInfo['order_status'] != 1){
            $result['msg'] = 'order close';
            echo json_encode($result);exit;
        }
        if($orderInfo['pay_status'] != 0){
            $result['msg'] = 'order paid';
            echo json_encode($result);exit;
        }

        if($orderInfo['price'] * 100 != $payResult['total_fee']){
            //$result['msg'] = 'total fee error';
            //echo json_encode($result);exit;
        }

        // 检查是否使用8折优惠券
        $userInfoData = $this->user_model->getuserdetail($orderInfo['user_id']);
        $passport_uid = $userInfoData ? $userInfoData['passport_uid'] : 0;
        if($this->rent_coupon_model->checkCheckBox($passport_uid)){
            $this->rent_coupon_model->useCoupon($passport_uid);
        }
        //支付成功后的操作
        $result = $this->package_model->payFinish($orderInfo,$payResult);
        if($result){
            echo 'SUCCESS';
        }
    }


    public function getOrderInvoice(){
        $contractNo = addslashes($this->input->get('contractNo',true));
        $city = addslashes($this->input->get('city',true));
        $city = empty($city) ? 'nj' : $city;
        $result['status'] = '0';
        $result['msg'] = '';
        $result['data'] = array();
        $contractArr = explode(',',$contractNo);
        if(empty($contractNo) || empty($contractArr) || !is_array($contractArr)){
            $result['msg'] = 'data error';
            echo json_encode($result);exit;
        }
        //重组数组
        $contractNo = '"'.implode('","',$contractArr).'"';
        if(GB_CITY != 'nj' || $city != 'nj'){
            $result['msg'] = 'city error';
            echo json_encode($result);exit;
        }
        //根据合同编号获取发票信息
        $this->load->model("package_model");
        $data = $this->package_model->getInvoiceByContractNo($contractNo);
        foreach($data as $key => $value){
            foreach($value as $k => $v){
                if($k == 'id' || $k == 'order_id' || $k == 'status'){
                    unset($data[$key][$k]);
                }else{
                    $data[$key][$k] = iconv('gbk','utf8',$v);
                }
            }
        }
        $result['status'] = '1';
        $result['data'] = $data;
        echo json_encode($result);exit;
    }

    /**
     * Notes:手动对支付成功但是回调失败的 刷新套餐修复数据
     * User: zhangkg
     */
    public function orderRepair(){
        $this->load->model("package_model");
        $this->package_model->getHouseOrders();

    }

    /**
     * 发送消息
     * @author zj
     * @date 2020-09-08
     *
     * @param array $orderInfo 订单数据
     * @return bool
     */
    public function sendMsg( $orderInfo ) {
        $message = '';
        if ( $orderInfo['package_type'] == 1 ) {
            $message = "您已成功购买{$orderInfo['package_days']}天的首页推荐套餐，购买的套餐可以在个人中心-我的订单进行查看。";
        } else if ( $orderInfo['package_type'] == 2 ) {
            $message = "您已成功购买{$orderInfo['package_days']}天的列表页推荐套餐，购买的套餐可以在个人中心-我的订单进行查看。";
        } else if ( $orderInfo['package_type'] == 3 ) {
            $message = "您已成功购买{$orderInfo['package_days']}天的智能刷新套餐，购买的套餐可以在个人中心-我的订单进行查看。";
        } else if ( $orderInfo['package_type'] == 6 ) {
            $message = "您已成功购买24小时{$orderInfo['refresh_num']}次刷新，您可以选择手动去刷新或者设置时间预约刷新，购买的套餐可以在个人中心-我的订单进行查看哦";
        }

        if ( !$message ) {
            return false;
        }

        //发送用户中心消息
        $subject = '购买套餐成功。';
        $this->apply_class->send_systemmsg( $orderInfo['user_id'], $subject, $message, 7, GB_CITY );

        //发送短信
        $telno = isset( $orderInfo['house_tel'] ) ? $orderInfo['house_tel'] : '';
        if ( $telno ) {
            sendsms( $telno, $message, 'sx' );
        }

        return true;
    }
}