<?php
class Default_LoginController extends Ec_Controller_DefaultAction
{
	// --------------------------------------------------------------------
	public function preDispatch()
    {
        $this->tplDirectory	= "default/views/login/";
    }

    // --------------------------------------------------------------------
    /**
     * 登陆首页
     *
     */
    public function indexAction()
    {
		$loginInfo	= Service_Login::getLoginInfo();
 		$hostName = $this->_request->getHttpHost();
        $arr = array(
            'name'=>'招海oms系统',
            'icp'=>'粤ICP备2021069532号-1',
        );
        $this->view->display = $arr;
    	if (empty($loginInfo['id'])) {
            // 备案成功sku
            $products = Service_Product::getByCondition(array('product_status'=>1), 'count(*)');
            $customers = Service_Customer::getByCondition(array('customer_status'=>2), 'count(*)');
            $this->view->products = $products+300000;
            $this->view->customers = $customers;
            // 审核通过企业
    		echo $this->view->render($this->tplDirectory . 'index.tpl');
    	} else {
    		$this->_redirect('/merchant');
    	}
    }

    /**
     * [loginAction description]
     *
     * @return [type] [description]
     */
    public function apiLoginAction(){
        $loginInfo  = Service_Login::getLoginInfo();
        if (empty($loginInfo['id'])) {
            echo $this->view->render($this->tplDirectory . 'api_login.tpl');
        } else {
            $url = $this->getRequest()->getParam('url');
            $this->_redirect($url);
            //die('Login Success.');
        }
    }

    // --------------------------------------------------------------------
    /**
     * 登陆检测
     *
     */
    public function checkAction()
    {
        $verify	= new Common_Verifycode();
        $apiLogin = 0;
        $callback = '';
        if( isset( $_REQUEST ['apiLogin'] ) ) {
            $apiLogin = trim($_REQUEST ['apiLogin']);
        }
        if( isset( $_REQUEST ['callback'] ) ) {
            $callback = htmlspecialchars($_REQUEST ['callback']);
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
        header("Cache-Control: no-store, must-revalidate"); 
        header("Pragma: no-cache"); 
        if(!$apiLogin){
    		$value	= $this->getRequest()->getParam('verify');
            $state	= $verify->is_true($value);
            $name	= $this->getRequest()->getParam('username');
            $pwd	= $this->getRequest()->getParam('password');
            if($name==''){
                header("location:/login");
                exit;
            }
            $result = array();
    		if ($state) {
                $time	= date('Y-m-d H:i:s');
                $result	= Service_Login::check($name, md5($pwd));
                /*die("<script>alert('" . $msg . "');window.history.back();</script>");*/
    		} else {
                $result['ask'] = "0";
                $result['errorMsg'] = "验证码错误，请重新填写！";
    			/*die("<script>alert('验证码错误，请重新填写！');window.history.back();</script>"); */
    		}
            die(json_encode($result));
        //接口登陆
        }else {
            header('Access-Control-Allow-Origin: *');
            header('P3P: CP=CAO PSA OUR');
			$plante_code = $_REQUEST ['plantecode'];
			if($plante_code=='lightrade'){
				//光亚登陆
				$name = $_REQUEST ['usercode'];
				$pwd = $_REQUEST ['password'];
				$url = $_REQUEST ['url'];
				$sign = $_REQUEST ['sign'];

				$key = 'globex12';

				$secu = new Common_SecretUtilTools();

				$deco_name = $secu->decryptForDES($name,$key);
				$deco_pass = $secu->decryptForDES($pwd,$key);
				$deco_sign = $secu->decryptForDES($sign,$sign);
				$loginInfo	= Service_Login::getLoginInfo();
				if (empty($loginInfo['id'])) {
					$result = Service_Login::check($deco_name, md5($deco_pass),$plante_code);
				}else{
					$result = array("ask"=>'1',"message"=>"你已经登陆");
				}
			}else{
				require 'JWT.php';
				$name = $_REQUEST ['username'];
				$pwd = $_REQUEST ['password'];
				$url = $_REQUEST ['url'];
				if(!empty($name) && !empty($pwd)){
					$name = JWT::decode($name, 'gsglobex');
					$pwd = JWT::decode($pwd, 'gsglobex');
					$result = Service_Login::check($name, md5($pwd));
					/*
					if($result['ask'] == 1){
						$this->_redirect($url);
					}
					*/
				}else {
					die('Parameter error');
				}
			
			}

            //输出jsonp格式的数据
            die($callback . "(" . json_encode($result) . ")");
        }
	}

	// --------------------------------------------------------------------
	/**
	 * 注销登陆
	 *
	 */
	public function outAction()
	{
		Service_Login::outLogin();
	}
	
	// --------------------------------------------------------------------
	/**
	 * 验证码
	 *
	 */
    public function verifyCodeAction()
    {
    	$verify	= new Common_Verifycode();    	
    	$verify->set_img_size(60,23);
    	echo $verify->render();
    }

}
/* End of file LoginController.php */
/* Location: /application/modules/default/controllers/LoginController.php */