<?php
/** wap
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Mobile\Controller;
use Common\Controller\HanchaoController;
class BaseController extends HanchaoController {
	public function _initialize(){
		parent::_initialize ();
		GLOBAL $GoodsCategory;
        GLOBAL $PurchaseCategory;
        if(empty($GoodsCategory))$GoodsCategory=getcache('GoodsCategory');
        if(empty($PurchaseCategory))$PurchaseCategory=getcache('PurchaseCategory');
		$shops_info=$this->shops_info=session('site_info');
        define('MID',is_login(0));
		if(MID > 0){$this->assign('MID',MID);}else{$this->assign('MID',0);};
 		define('PAGE_CONFIG','%UP_PAGE% %DOWN_PAGE%');
		$autologin = cookie('autologin');
    	$this->TEMPLATE_PATH=__ROOT__.substr(APP_PATH,1).'Mobile/'.C('DEFAULT_V_LAYER').'/'.C('DEFAULT_THEME').'/';
		$this->member_info=session('member_info');$this->baseheaderjs='';
        //关联微信
		if(!in_array(CONTROLLER_NAME,array('App','Pay','Weixin','Oauth','Ajax','Qrcode','Poster'))){
			$wxopenid=$this->getopenid();//获取当前微信id
		}
        
        
        $memberdata = GetmdataBywx($wxopenid);
        if($memberdata['reg_type'] == 1 && $memberdata['parent_id'])TooKeenAward('',1,'bean',$memberdata['parent_id']);//新增粉丝给推荐人奖励
        session('memberdata',$memberdata);
		if($memberdata)$this->autologinupdate($memberdata);
        // session('main_shop_id',1);
		$this->m_info = idtoname(MID,'Member','is_tmp,mobile','id',false);
	  	$result = D('Common/Area')->init();//初始化地区信息
        $siteinfo=session('siteinfo')?session('siteinfo'):cookie('siteinfo');
        $this->assign('siteinfo',$siteinfo);
    }
	private function getopenid(){
        if(!is_weixin())return false;
        if(I('get.noautosignwx')){session('noautosignwx','1');return ;}
        return get_openid();
    }
	private function locktowx($sessionwxuname=''){
        $autosignwx=I('get.autosignwx');
        if(empty($sessionwxuname) || $sessionwxuname=='kong' || empty($this->member_info['id']) || !empty($this->member_info['wxid']) || empty($autosignwx))return false;
        $where=array('id'=>$this->member_info['id']);
        M('Member')->where($where)->save(array('wxid'=>$sessionwxuname));
        $member_info=$this->member_info;
        $member_info['wxid']=$sessionwxuname;
        $member_info['member_info_auth_sign'] = data_auth_sign($member_info);
        session('member_info',$member_info);        
    }
    private function getwxuserinfo($openid=''){
        if(empty($openid))$openid=session('wxopenid');
        if(empty($openid))return false;
        $memberinfo=M('Member')->where("wxid='".$openid."'")->field(true)->find();
        if(empty($memberinfo)){
            $memberid=M('OauthMember')->where("openid='".$openid."' and _from='weixin'")->getField('lock_to_id');
            if($memberid)$memberinfo=idtoname($memberid,'Member','*');
        }return $memberinfo;
    }
    protected function autowxlogin($sessionwxuname,$id=''){
		if(empty($sessionwxuname))return false;//微信自动登录
        $OMember = M('OauthMember');$ipid=get_client_ip(1);
		if(empty($id))$id=$this->member_info['id'];
        $res=$OMember->where("`_name`='".$sessionwxuname."'")->field('login_times')->find();
        $login_times=$res['login_times'];
        if($login_times){
            $data = array(
					'last_login_time'	 => NOW_TIME,
					'last_login_ip'		 => $ipid,
					'login_times'		 => $login_times + 1,
			);
			$OMember->where("_from='weixin' and (_name='{$sessionwxuname}' || openid='{$sessionwxuname}')")->save($data);
        }else{
            $data = array(
					'_from'				 => 'weixin',
					'_name'				 => $sessionwxuname,
					'head_img'			 => '',
					'create_time'		 => NOW_TIME,
					'lock_to_id'		 => $id?$id:MID,
					'last_login_time'	 => NOW_TIME,
					'last_login_ip'		 => $ipid,
					'login_times'		 => 1,
					'status'			 => 1,
					'access_token'		 => 0,
					'expires_date'		 => 0,
					'openid'			 => $sessionwxuname,
				);
			$OMember->add($data);
        }
    }
    protected function autologinupdate($memberinfo){
		if(empty($memberinfo))return false;//自动登录
		// if(CONTROLLER_NAME!='Login' &&(empty($memberinfo['mobile']) || $memberinfo['is_temp'] == 1)){
			// $this->redirect('Mobile/login/perfect');
		// }
        $memberinfo['member_info_auth_sign'] = data_auth_sign($memberinfo);
        session('member_info', $memberinfo);$userdata=array();
		//$siteinfo = session('siteinfo');
		//if($siteinfo['tj_type'] != 1){
//			$ma_map['uid'] = $memberinfo['id'];
//			$ma_map['status'] = 1;
//			$ma_info = M('MemberAddress')->where($ma_map)->field('xpoint,ypoint,address,city')->find();
//			$siteinfo['temp_lng'] = $ma_info['xpoint'];
//			$siteinfo['temp_lat'] = $ma_info['ypoint'];
//			$siteinfo['temp_name'] = $ma_info['address'];
//			$siteinfo['tj_type'] = 1;
//			$siteinfo['city'] = idtoname($ma_info['city'],'Area','name');
//			if($ma_info)session('siteinfo',$siteinfo);
//		}
		
		
        $userdata['id'] = $memberinfo['id'];
        $userdata['login_count'] = array('exp', 'login_count+1');
        $userdata['last_login'] = NOW_TIME;
        $userdata['last_ip'] = get_client_ip(1);
        M('Member')->save($userdata);                    
        if(C('MEMBER_UC')){//uc同步登录
            $uc = new \Ucenter\Client\Client();
            $re = $uc->uc_get_user($autostr[2]);
            if($re[0]>0){echo $uc->uc_user_synlogin($re[0]);}
       }
    }
    public function judge(){
        $main_shop_id = session('main_shop_id');
        $loc = session('loc');
        $siteinfo = session('siteinfo');
        if(MID){
            if(!$siteinfo['temp_lng'] && !$siteinfo['temp_lat']){
                $map = array();
                $map['uid'] = MID;
                $map['status'] = 1;
                $memberaddress_info = M('MemberAddress')->where($map)->field(true)->find();
                if($memberaddress_info){
                    $member_info['province'] = $memberaddress_info['province'];
                    $member_info['city_id'] = $memberaddress_info['city_id'];
                    $member_info['district'] = $memberaddress_info['district'];
                    $member_info['temp_lng'] = $memberaddress_info['xpoint'];
        			$member_info['temp_lat'] = $memberaddress_info['ypoint'];
        			$member_info['temp_name'] = $memberaddress_info['address'];
        			$member_info['city'] = idtoname($memberaddress_info['city'],'Area','name');
                    session('siteinfo',$member_info);
                    cookie('siteinfo',$member_info);
                    session('loc',1);
                    
                }
                save_session();
            }
        }
//        if(!$main_shop_id && $loc !=1 && !$siteinfo['temp_lng'] && !$siteinfo['temp_lat']){
//            $this->baseheaderjs="<script>relocation();</script>";
//
//        }
    }
}
?>