<?php
// +----------------------------------------------------------------------
// | 首页控制器
// +----------------------------------------------------------------------
// | ThinkOAO （Online And Offline）
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.hanchao9999.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xiaogg <xiaogg@sina.cn>
// +----------------------------------------------------------------------
namespace Mobile\Controller;
class LoginController extends BaseController {
	public function index() {
		if($this->member_info['id']){$this->redirect('Mobile/Member/index');exit;}
		$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('page_title', $title);
		$this->display();
	}
	public function shop_index() {
        if(!checksiteauth('MenDian')){ exit;}
		if($this->shops_info['id']){$this->redirect('Mobile/ShopCenter/index');exit;}
		$title='门店登录';

        $this->assign('tplseo_title', $title . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', $title);
		$this->display();
	}
	 /**
     * 获取验证码
     */
    public function verify(){
        $config = array(
            'imageW' => '120',
            'imageH' => '40',
        	'fontSize'=>'18',
            'seKey' => 'chanchao' . C('HANCHAO_KEY'),
            'length' => 4,
        );
        $verify = new \Common\Vendor\Verify($config);
        $verify->entry('Member');
    }
    /**
	 * 门店登陆
	 */
	public function doshoplogin(){
        if(!checksiteauth('MenDian')){ exit;}
		$param=I('param.');
		$username=I('login_name');
		$password=I('password');
		if(!$username || !$password) {$data['status']=2;$data['info']='请输入用户名/手机号或密码！';$this->ajaxReturn($data);exit;}
		$model=D("Common/ShopLogin");
		$result=$model->check_login($param);
		$url=U('Shop/Index/index');//手机和电脑用着一个登录model,这里区分一下手机默认的跳转
		// if($result['url']==$url){
			$result['url']=U('Mobile/ShopCenter/index');
		// }
		$this->ajaxReturn($result);
	}
	/**
	 * 登陆
	 */
	public function dologin(){
		$param=I('param.');
		//$username=I('username');
        //$password=I('password');
        //if(!$username || !$password) {$data['status']=2;$data['info']='请输入用户名/手机号或密码！';$this->ajaxReturn($data);exit;}
        //$result=$model->check_login($param);
        $mobile = I('mobile');
        $verify = I('verify');
		if(!$mobile || !$verify) {$data['status']=2;$data['info']='请输入手机号或短信验证码！';$this->ajaxReturn($data);exit;}
		$model=D("Common/Login");
		$result=$model->mobilecheck_login($param);
		$url=U('Member/Index/index');//手机和电脑用着一个登录model,这里区分一下手机默认的跳转
		if($result['url']==$url){$result['url']=U('Mobile/Member/index');}
		$this->ajaxReturn($result);
	}
    public function register() {
		if($this->member_info['id']){$this->redirect('Mobile/Member/index');exit;}
		$eid =I('get.eid');
        //$card_data = M('CardData')->cache(true)->field(true)->getField('option',true);
       // echo "<pre>";
        //print_r($card_data);exit;
       // $this->assign('card_data',$card_data);
		$ref_mobile=idtoname($eid,'Member','mobile');
    	$is_reffer = C('IS_FILL_RECOMMEND');
		$this->assign('ref_mobile',$ref_mobile);
    	$this->assign('is_reffer',$is_reffer);
        $this->assign('tplseo_title', '新用户注册' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '新用户注册');
    	$this->display();
	}
	public function doregister(){
		$param=I('param.');
		$model=D("Common/Login");
		$result=$model->do_register($param);
		$this->ajaxReturn($result);
	}
	public function getpassword(){
		if($this->member_info['id']){$this->redirect('Mobile/Member/index');exit;}
        $this->assign('tplseo_title', '找回密码' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '找回密码');
    	$this->display();
	}
	public function get_pay_password(){
		$mid = $this->member_info['id'];
        $this->assign('mid',$mid);
        $this->assign('tplseo_title', '找回支付密码' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '找回支付密码');
    	$this->display();
	}
    public function resetpwd(){
		$param = I('param.');
		$result = D('Common/Login')->resetpwd($param);
		$this->ajaxReturn($result);
	}
	public function perfect(){
		//$card_data = M('CardData')->getField('option',true);
        //$this->assign('card_data',$card_data);
		$memberdata = session('member_info');
		$wxperfectuid = $memberdata['id'];
        if(empty($wxperfectuid)){$this->redirect('Mobile/Member/index');exit;}
        $memberinfo=M('Member')->where('id='.$wxperfectuid)->field('code,mobile,card_sn')->find();
        if($memberinfo['code'] && $memberinfo['mobile'] && empty($memberinfo['card_sn'])){
            $this->success('您已经领取过会员卡了,前往激活',U('Mobile/Member/wx_phone'));return;
        }
        if($memberinfo['code'] && $memberinfo['mobile'] && $memberinfo['card_sn']){
            $this->success('您已经领取过会员卡了',U('Mobile/Index/index'));return;
        }
        if($memberinfo['mobile']){
            $this->redirect('Mobile/Member/index');return;
        }
		$parent_id = $memberdata['parent_id'];
        if($parent_id){
            $parent_info=M('Member')->where('id='.$parent_id)->field('id,mobile,recom_code')->find();
    		$this->assign('parent_id',$parent_info['id']);
    		$this->assign('ref_mobile',$parent_info['mobile']);
    		$this->assign('recom_code',$parent_info['recom_code']);
        }
		$is_reffer = C('IS_FILL_RECOMMEND');
    	$this->assign('is_reffer',$is_reffer);
    	$register_open = C('REGISTER_OPEN');
    	$this->assign('register_open',$register_open);
		$this->assign('tplseo_title', '完善资料' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '完善资料');
		$this->display();
	}
    //有手机号 直接发卡
	public function autoperfect(){
	    $result=array('status'=>0,'url'=>U('Mobile/Index/index'));
		$memberdata = session('member_info');
		$wxperfectuid = $memberdata['id'];
        if(empty($wxperfectuid)){$this->redirect('Mobile/Member/index');exit;}
        $memberinfo=M('Member')->where('id='.$wxperfectuid)->field('code,mobile,card_sn')->find();
        if(empty($memberinfo['mobile'])){
            $result['info']='请先完善资料再领卡';
            $result['url']=U('Mobile/Member/perfect');$this->ajaxReturn($result);
        }
        if($memberinfo['code'] && empty($memberinfo['card_sn'])){
            $result['info']='您已经领取过会员卡了,前往激活';
            $result['url']=U('Mobile/Member/wx_phone');$this->ajaxReturn($result);
        }
        if($memberinfo['code'] && $memberinfo['card_sn']){
            $result['info']='您已经领取过会员卡了';
            $this->ajaxReturn($result);
        }
        $memberdata['mobile'] = $memberdata['mobilecode'] =$memberinfo['mobile'];
		$model=D("Common/Login");
		$result=$model->get_card($memberdata,1);
		if(session('format_url')){
			$result['url']=session('format_url');session('format_url',null);
		}
        if($result['status']){
            if(empty($result['info']))$result['info']='尊敬的会员您好！恭喜您领取成功!';
        }else{
            if(empty($result['info']))$result['info']='领取失败!';
        }
        $this->ajaxReturn($result);
	}
	public function get_card(){
        $this->assign('page_title', '领取会员卡');
		$this->display();
	}
	public function doperfect(){
		$param=I('param.');
		$memberdata = session('member_info');
		$wxperfectuid = $memberdata['id'];
        if(empty($wxperfectuid)){$this->redirect('Mobile/Member/index');exit;}
		$param['id'] = $wxperfectuid;
		$param['wxid'] = $memberdata['wxid'];
		$model=D("Common/Login");
		$result=$model->get_card($param);
		if(session('format_url')){
			$result['url']=session('format_url');session('format_url',null);
		}else{
			$result['url']=U('Mobile/Member/index');
		}
		$this->ajaxReturn($result);
	}
	public function do_get_card(){
		$param=I('param.');
		$memberdata = session('member_info');
		$wxperfectuid = $memberdata['id'];
        if(empty($wxperfectuid)){$this->redirect('Mobile/Member/index');exit;}
		$param['id'] = $wxperfectuid;
		$res = M('Config')->field('value')->where("name='WX_CARD'")->find();
		if($res){
			$val = unserialize($res['value']);
			$wxids[] = $memberdata['wxid'];
			$wxids[] = $memberdata['wxid'];
			$card_id = $val['card_id'];
			$Weixin = new \Common\Vendor\WeixinCoupon();
			$wxcard = $Weixin->wxcard($wxids,$card_id);
			if($wxcard['errcode'] == 0 || $wxcard['errcode'] == 45065){
				$id = M('Member')->save($param);
			}else{
				$root['status'] = 0;
				$root['info'] = '微信会员卡发放失败';
			}
		}else{
			$root['status'] = 0;
			$root['info'] = '微信会员卡发放失败';
		}
		$this->ajaxReturn($root);
	}
}