<?php
use Business\User\LoginV2Model;
use Business\Message\MessageServiceModel;
class IndexController extends \Our\Controller_Abstract  {
    public $req;
    public $memberId;
    public $messageService;
    public function init(){

    }
    public function is_https() {
        if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
            return true;
        } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        }elseif($_SERVER['SERVER_PORT'] == 89){
            return true;
        }

        return false;
    }
    public function getHtmlAction(){
        $this->req=$this->getRequest()->getQuery();
        $postData=$this->req;
        $postData=base64_decode($postData[\Our\NameConst::sessionKey]);
        $params=explode(\Our\PathConst::cutVertical,$postData);
        $this->key=$params[0];
        session_id($this->key);
        $this->sess=\Yaf\Session::getInstance();
        $this->sess->start();
        if($this->sess['key']!=$this->key){
            echo 'error auth!';
            exit;
            // ErrorModel::throwException(CodeConfigModel::authExpire);
        }
        $this->memberDb0Redis=\Redis\Db0\MemberRedisModel::getInstance();
        $paramsStr= $this->memberDb0Redis->tablelpop($this->key);
        $paramsStr=json_decode($paramsStr,true);
        $this->_view->assign("params", $paramsStr);
        if($paramsStr['type']==\Our\ApiConst::sharePlat){
            $this->_view->display('index/plat.phtml');
        }
        if($paramsStr['type']==\Our\ApiConst::shareStore){
            $this->_view->display('index/store.phtml');
        }
        if($paramsStr['type']==\Our\ApiConst::shareGoods){
            $this->_view->display('index/goods.phtml');
        }
        exit;
    }

    public function indexAction() {
        var_dump($_SERVER);exit;
       // echo $_SERVER['USER']);exit;
        $elasticConfig=\Our\Common::getConfig('elastic.master');
        $configArray=array(
            $elasticConfig->host.':'.$elasticConfig->port,
        );
        $client=\Elasticsearch\ClientBuilder::create()->setHosts($configArray)->build();
        $res=array(
            'id'=>'nignx-log*',
            'index'=>'.kibana',
            'type'=>'index-pattern'
        );
        $res=$client->getSource($res);
        echo json_encode($res);
        exit;
    }

    public function check(){
        $this->req=$this->getRequest()->getPost();
        $this->checkEncrypt();
        if(empty($this->req['data']['key'])){
            \Error\ErrorModel::throwException(CodeConfigModel::illegalAccess);
        }
        session_id($this->req['data']['key']);
        ini_set('session.gc_maxlifetime', \Our\ApiConst::tenDaySecond);
        $sess=\Yaf\Session::getInstance();
        $sess->start();
        $this->memberId=$sess->get('member_id');
        if(empty($this->memberId)){
            \Error\ErrorModel::throwException(\Error\CodeConfigModel::noLogin);
        }
        //\Yaf\Dispatcher::getInstance()->disableView();
    }

    public function chatRoomAction(){
        $this->check();
        $this->messageService=MessageServiceModel::getInstance();
        $pageIndex=isset($this->req['data']['pageIndex'])?$this->req['data']['pageIndex']:0;
        $pageSize=isset($this->req['data']['pageSize'])?$this->req['data']['pageSize']:20;
        if(!empty($this->req['data']['toType'])){
            $returnMessage=\Our\RedisHelper::cachedFunction(\Redis\Db15\MessageRedisModel::getInstance(),array(&$this->messageService, 'getMemberList'),array($this->memberId,$this->req['data']['fromType'],$this->req['data']['toId'],$this->req['data']['toType'],'*',$pageIndex,$pageSize),3600,array($this->memberId));
        }else{
            $returnMessage=\Our\RedisHelper::cachedFunction(\Redis\Db15\MessageRedisModel::getInstance(),array(&$this->messageService, 'getList'),array($this->memberId,$pageIndex,$pageSize),1,array($this->memberId));
        }
        $push=  \Yaf\Registry::get('config')->get('push');
        $pushUrl=$push['clientHost'];
        $this->_view->assign("key",$this->req['data']['key']);
        $this->_view->assign('users',$returnMessage['list']);
        $this->_view->assign('memberId',$this->memberId);
        $this->_view->assign('pushUrl',$pushUrl);
        $this->_view->assign('totalCount',$returnMessage['totalCount']);
    }

    public function goodsDetailAction() {
        $goodsCommonId=(int)$this->getRequest()->getQuery()['goodsCommonId'];
        if($goodsCommonId > 0) {
            $commonGoodsInstance = \DAO\GoodsCommonModel::getInstance();
            $commonInfo = \Our\RedisHelper::cachedFunction(\Redis\Db4\GoodsCommonRedisModel::getInstance(),array(&$commonGoodsInstance, 'getOneById'),array($goodsCommonId),0,array($goodsCommonId));
        }
        $this->_view->assign('commonInfo',$commonInfo);
    }

}
