<?php
/**
 * 商品列表
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author shixiaoli <1094186793@qq.com>
 */
namespace Home\Controller;
use Think\Controller;
class LoginController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
		
    }
    public function index(){
        $param=I('param.');
		if($this->member_info['id']){$this->redirect('Member/Index/index');exit;}
		$parme=array();
        $result=D("Common/Login")->index();
		session('oauthmemberlogin')?$title='账号绑定_':$title='登录_';
		$this->assign('oauth',session('oauthmemberlogin'));
		$this->assign('list',$result);
        $this->assign('tplseo_title',$title.C('WEB_SITE_TITLE'));
        $this->assign($param);
        $this->display();
    }

    /**
     * 获取验证码
     */
    public function verify(){
		$width=I('width')?I('width'):170;
		$width=I('height')?I('height'):55;
        $config = array(
            'imageW' => '170',
            'imageH' => '50',
            'seKey' => 'chanchao' . C('HANCHAO_KEY'),
            'length' => 4,
        );
        $verify = new \Common\Vendor\Verify($config);
        $verify->entry('Member');
    }
    /**
     * 登陆
     */
    public function dologin(){
        $parme=I('post.');
        $username=I('username');
        $password=I('password');
        if (!$username || !$password){$data['status']=2;$data['info']='请输入用户名/手机号或密码！';$this->ajaxReturn($data);exit;}
        $model=D("Common/Login");
        $result=$model->check_login($parme);
        if($parme['is_from_shop']==1){
            if($result['status']==1){
                $result['url']=geturl('Shop/Register/index',array('is_from_shop'=>1));
            }
        }

        $this->ajaxReturn($result);
    }
    public function register(){//注册页面
		if($this->member_info['id']){$this->redirect('Member/Index/index');exit;}
        $eid =I('get.eid');
		$ref_mobile=idtoname($eid,'Member','mobile');
        $tui_time = time();
        //是否填写推荐人
        $is_fill_recommend=C('IS_FILL_RECOMMEND');
        $this->assign('is_fill_recommend',$is_fill_recommend);
        $this->assign('ref_mobile',$ref_mobile);
        $this->assign('tui_time',$tui_time);
        $this->assign('before_title','新用户注册');
        $this->assign('tplseo_title','新用户注册_'.C('WEB_SITE_TITLE'));
        $this->display();
    }
    //注册验证
    public function to_register(){
        if(!C('MEMBER_ALLOW_REGISTER'))$this->error('网站暂时禁止了注册功能!',U('Home/Index/index'));
        $postdata=I('post.');
        $model=D("Common/Login");
        $result=$model->do_register($postdata);
        $this->ajaxReturn($result);
    }
	//顶部登陆条
    public function smalllogin(){
        $str='';
        $member_info=session('member_info');
        if($member_info){ 
            $map['id']=$member_info['id'];
            $user_info=M('Member')->where($map)->field('username,nickname,rank_id')->find(); 
            $member_rank=M('MemberRank')->where('id='.intval($user_info['rank_id']))->field('rank_img,name')->find(); 
            $str=" <font color='#ef0000'>".($user_info['nickname']?$user_info['nickname']:$user_info['username']);
            if($member_rank['rank_img']){
                $str.="<img class='top-grade-img' src=".$member_rank['rank_img']." title=".$member_rank['name']." />";
            }
            $str.="</font>  <a class='ju' href=\"".U('Member/Index/index')."\">会员中心</a><a class='ju' href=\"".U('Home/Login/logout')."\">".L('logout')."</a>";
        }else{
            $str="<a class='ju' href=\"".U('Home/Login/index')."\" title=\"".L('user_login')."\">登录</a><a class='ju' title=\"".L('user_regist')."\" href=\"".U('Home/Login/register')."\">免费注册</a>";
        }
        unset($member_info);
        echo format_js($str);
        echo "var uid=".intval($member_info['id']).';';
    }
	//顶部登陆条
    public function psmalllogin(){
        $str='';
        $shops_info=session('shops_info');
        if($shops_info){ 
            $map['id']=$shops_info['shop_id'];
            $user_info=M('Shops')->where($map)->field('name,login_name')->find();
            $str=" <font color='#ef0000'>".($user_info['name']?$user_info['name']:$user_info['login_name']);
            $str.="</font>  <a class='ju' href=\"".U('Shop/Index/index')."\">门店中心</a><a class='ju' href=\"".U('Home/Login/plog_out')."\">".L('logout')."</a>";
        }else{
            $str="<a class='ju' href=\"".U('Shop/Register/shop_login')."\" title=\"".L('SHOPS_LOGIN')."\">登录</a>";
        }
        unset($shops_info);
        echo format_js($str);
        echo "var uid=".intval($shops_info['shop_id']).';';
    }
	
    public function logout(){
        $data=D('Common/Login')->logout();
        if($data['status']==1){
            $this->success($data['info'],$data['url']);
        }else{
            $this->error($data['info'],$data['url']);
        }
    }
	/**
     * 门店退出登录
     */
	public function plog_out(){
        $return=D("Common/ShopLogin")->logout();
        if($return['status']==1){
            $this->success($return['info'],$return['url']);
        }else{
            $this->error($return['info'],$return['url']);
        }
	}
	public function getpassword(){
		if($this->member_info['id']){$this->redirect('Member/Index/index');exit;}
		$this->assign('before_title','找回密码');
        $this->assign('tplseo_title','找回密码_'.C('WEB_SITE_TITLE'));
        $this->display();
	}
	public function resetpwd(){
		if($this->member_info['id']){$this->redirect('Member/Index/index');exit;}
		$param=I('param.');
		$result=D('Common/Login')->resetpwd($param);
		$this->ajaxReturn($result);
	}
}
?>