<?php
/** 店铺登录
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */

namespace Common\Model;
use Think\Model;
class ShopLoginModel extends Model{
    protected $tableName = 'shops';
	public function _initialize(){
		parent::_initialize ();
       	$forward=I('shop_forward')?I('shop_forward'):cookie('shop_forward');
      	$this->jumpUrl = $forward? $forward: U('Shop/Index/index');
    }
    /**登陆
     * @param $param 登陆所需参数数组
     * @return array 返回状态、信息
     */
    public function check_login($param){
        $return=array('status'=>0,'info'=>'');
        if(empty($param)){$return['info']='参数错误';return $return;}
        $login_name=$param['login_name'];
		$password=$param['password'];
		$verify=$param['verify'];
        if(empty($login_name) || empty($password)){$return['info']='请输入用户名或密码'; return $return;}
 
        if(!isset($param['unverify'])){
            if(empty($verify)){
            	$return['info']='请输入验证码';return $return;
            }
            if (!check_verify($verify,'Member')){
            	$return['info']=L('verify_error');return $return;
            }
        }
        /*if(str_exists($login_name,'@')){
            $login_arr=explode('@',$login_name);
            if(empty($login_arr[1])){$return['info']='商家账号不存在'; return $return;}
            $map_shop=array();
            $map_shop['pid']=0;
            $map_shop['status']=1;
            $map_shop['is_delete']=0;
            $map_shop['login_name']=$login_arr[1];
            $shop_login_name=M('ShopsAccount')->where($map_shop)->field('id,shop_id')->find();
            if(empty($shop_login_name)){$return['info']='商家账号不存在'; return $return;}
            $map['pid']=$shop_login_name['id'];
            $map['shop_id']=$shop_login_name['shop_id'];
            $map['login_name|mobile']= array($login_arr[0],$login_arr[0],'_multi'=>true);
        }else{
            $map['login_name|mobile']= array($login_name,$login_name,'_multi'=>true);
            $map['pid']=0;
        }*/
		$map = array();
		$map['login_name|mobile']= array($login_name,$login_name,'_multi'=>true);
        $map['status'] = 1;
		$map['shop_id'] = array('gt',0);
		$map['is_delete'] = 0;
		
        $account_info = M('ShopsAccount')->where($map)->field('id,salt,password,shop_id')->find();
        if($account_info){
			$postpassword = creatpwd($password, 'HANCHAO'.$account_info['salt']);
			if($postpassword==$account_info['password']){
				$s_map=array();
				$s_map['id']=$account_info['shop_id'];
				$s_map['status']=1;
				$shopinfo=M('Shops')->where($s_map)->count();
				if(empty($shopinfo)){$return['info']='此店铺已禁止使用';return $return;}
				$return['status']=1;
				$return['info']='登录成功';
				$return['url']=$this->jumpUrl;
				if(cookie('shop_forward')){cookie('shop_forward',null);}
				if(cookie('is_shop_login')){
					$url = cookie('is_shop_login');
					cookie('is_shop_login',null);
					$return['url']=$url;
				}
			}else{
				$return['info']='密码错误';return $return;
			}
        }else{
			$return['info']='用户不存在,或者被禁用!';
        }
        if($return['status']==1){
            $this->loginupdate($account_info['id']);
            action_log('shop_login', 'Shop', $account_info['id'],$account_info['id'],'',$account_info['shop_id']);//店铺登录日志
        }
        return $return;
    }
	
	/**
     * 更新数据
     */
    public function loginupdate($account_id){
        if(!$account_id)return false;
        $savedata['login_count'] = array('exp', 'login_count+1');
        $savedata['last_login'] = NOW_TIME;
        $savedata['last_ip'] = get_client_ip(1);
        M('ShopsAccount')->where('id='.$account_id)->save($savedata);
        $shopsinfo=M('ShopsAccount')->where('id='.$account_id)->field(true)->find();
        if($shopsinfo['pid'] == 0){
	        $shopsinfo['service_switch']=M('Shops')->where('id ='.$shopsinfo['shop_id'])->getField('service_switch');
	        $shopsinfo['package_switch']=M('Shops')->where('id ='.$shopsinfo['shop_id'])->getField('package_switch');
	        $shopsinfo['shop_type']=M('Shops')->where('id ='.$shopsinfo['shop_id'])->getField('shop_type');
        }else{
        	$shopsinfo['shop_type']=M('Shops')->where('id ='.$shopsinfo['pid'])->getField('shop_type');
        }
        $shopsinfo['shops_info_auth_sign'] = data_auth_sign($shopsinfo);
        session('shops_info', $shopsinfo);
		
		return $shopsinfo;
    }
	/**
     * 退出登录
     */
    public function logout(){
        $shops_info=session('shops_info');
        $url=C('Cashier/login');
        if(isset($shops_info)){
            session('shops_info',null);
            $return['status']=1;
            $return['info']='退出成功';
            $return['url']=$url;
        }else{
            $return['status']=0;
            $return['info']='已经退出登录！';
            $return['url']=$url;
        }
        return $return;
    }
	/**
     * 修改密码
     * @param array $postdata
     * @return array
     */
	public function resetpwd($data){
		$return=array('status'=>0,'info'=>'');
		$shops_info=session('shops_info');
		$shop_id = $shops_info['shop_id'];
		
		if(empty($shops_info)){$return['info']='登录超时，请重新登录!';return $return;}
		if(empty($data['password'])){$return['info']='请输入原密码!';return $return;}
		if(!$data['new_password'] || !$data['yes_password'] || $data['new_password']!=$data['yes_password']){
			$return['info']='您输入的新密码为空或不一致,请重新输入!';return $return;
		}
		$userinfo=M('ShopsAccount')->where('shop_id='.$shop_id)->field('password,salt')->find();
		
		
		$old_psw=creatpwd($data['password'], 'HANCHAO'.$userinfo['salt']);
		if($old_psw != $userinfo['password']){$return['info']='原密码不正确!';return $return;}
		
		//修改密码
		$save['salt'] = randStr();
		$save['password'] = creatpwd($data['new_password'], 'HANCHAO'.$save['salt']);
		$result=M('ShopsAccount')->where('shop_id='.$shop_id)->save($save);
		$result=M('Shops')->where('id='.$shop_id)->save($save);
		
		if($result){
			session('shops_info',null);
			action_log('shop_resetpwd', 'Shop', $shops_info['id'], $shops_info['id'],'',$shops_info['shop_id']);//日志
			$return['status']=1;
			$return['info']='修改密码成功！请重新登录';
		}else{
			$return['info']='修改失败';
		}
		return $return;
	}
	/**
     * 找回密码获取店铺信息
     * @param array $postdata
     * @return array
     */
	 public function get_shop($data){
		$return=array('status'=>0,'info'=>'');
		
		$is_true = check_verify_code($data['mobile'],$data['mobilecode'],0);
		if($is_true == false){$return['info']='短信验证码错误!';return $return;}
		
		$u_map['mobile']=$data['mobile'];
		$userinfo=M('Member')->where($u_map)->field('id,status,shop_id')->find();
		if(empty($userinfo) || $userinfo['status']==0){$return['info']='会员不存在或已禁用';return $return;}
		
		$shops_info=M('Shops')->where('id='.$userinfo['shop_id'])->field('id,status')->find();
		if(empty($shops_info) || $shops_info['status']==0){$return['info']='店铺不存在或已禁用';return $return;}
		
		$return['status']=1;
		$return['uid'] =$userinfo['id'];
		return $return;
	}
	/**
     * 找回密码
     * @param array $postdata
     * @return array
     */
	 public function getpwd($data){
		$return=array('status'=>0,'info'=>'');
		
		if(!$data['uid']){$return['info']='店铺信息错误';return $return;}
		if(!$data['password'] || !$data['repassword'] || $data['password']!=$data['repassword']){
			$return['info']='您输入的新密码为空或不一致,请重新输入!';return $return;
		}
		$save['salt'] = randStr();
		$save['password'] = creatpwd($data['password'], 'HANCHAO'.$save['salt']);
		$result=M('Member')->where('id='.$data['uid'])->save($save);
		if($result !== false){
			$return['status']=1;
			$return['info']='找回密码成功！';
			$shop_id=idtoname($data['uid'],'Member','shop_id');
			action_log('shop_getpwd', 'Shop', $shop_id, $shop_id);//日志
		}else{
			$return['info']='找回密码失败！';
		}
		return $return;
	}
    /**
     * 申请店铺
     * @param $postdata
     * @return mixed
     */
    // public function register($postdata){
        /*$return=array('status'=>0,'info'=>'');
		//验证账号
		$map_name['login_name']=$postdata['login_name'];
		$name_count=M('ShopsAccount')->where($map_name)->count();
		if($name_count){$return['info']='账户名已存在';return $return;}
		
		$map_mobile['mobile']=$postdata['account_mobile'];
		$mobile_count=M('ShopsAccount')->where($map_mobile)->count();
		if($mobile_count){$return['info']='手机号已存在';return $return;}
		//会员信息
		$memberinfo=checkuserauthtype();
		$uid=$memberinfo['id'];
		if(!$uid){//注册会员
			$user_data['username']=$postdata['username'];unset($postdata['username']);
			$user_data['mobile']=$postdata['user_mobile'];unset($postdata['user_mobile']);
			$user_data['password']=$postdata['u_password'];unset($postdata['u_password']);
			$user_data['repassword']=$postdata['u_repassword'];unset($postdata['u_repassword']);
			$user_data['mobilecode']=$postdata['mobilecode'];unset($postdata['mobilecode']);
			$result=D('Common/Login')->do_register($user_data);
			if($result['status']!=1){$return['info']=$result['info'];return $return;}
			$uid=$result['data']['id'];
		}
		if(!$uid){$return['info']='会员信息不存在';return $return;}
		
		//添加商户信息
		if(!empty($postdata['cate_ids'])){$postdata['cate_ids']=implode(',',$postdata['cate_ids']);}
		if(!empty($postdata['shop_cates'])){$postdata['shop_cates']=implode(',',$postdata['shop_cates']);}
		if(!empty($postdata['takeout_ids'])){$postdata['takeout_ids']=implode(',',$postdata['takeout_ids']);}
		if(!empty($postdata['quan_id'])){$postdata['quan_id']=implode(',',$postdata['quan_id']);}
		$postdata['uid']=$uid;
		$postdata['site_id'] = $postdata['site_id'];
		$postdata['publish_wait'] = 0;
		$postdata['addtime'] = NOW_TIME;
		M('ShopsSubmit')->create($postdata);
		
		if($postdata['id']){
			$res=M('ShopsSubmit')->where('id='.$postdata['id'])->save();
		}else{
			$res=M('ShopsSubmit')->add();
		}
		if($res===false){$return['info']='提交失败';return $return;}
		
		$return['status']=1;
		$return['info']='提交成功，等待后台审核'; 
        return $return;*/
    // }

    public function register($postdata){
        $return=array('status'=>0,'info'=>'');
		// if(empty($postdata['cate_ids'])){$return['info']='请选择经营类目';return $return;}//ggw12/15
		
		$memberinfo=checkuserauthtype();
		$uid=$memberinfo['id'];
		
		// if(!$uid){//注册会员
		// 	$user_data['username']=$postdata['username'];unset($postdata['username']);
		// 	$user_data['mobile']=$postdata['mobile'];unset($postdata['mobile']);
		// 	$user_data['password']=$postdata['u_password'];unset($postdata['u_password']);
		// 	$user_data['repassword']=$postdata['u_repassword'];unset($postdata['u_repassword']);
		// 	$user_data['mobilecode']=$postdata['mobilecode'];unset($postdata['mobilecode']);
		// 	$user_data['verify']=$postdata['verify'];unset($postdata['verify']);
		// 	$result=D('Common/Login')->do_register($user_data);
		// 	if($result['status']!=1){$return['info']=$result['info'];return $return;}
		// 	$uid=$result['data']['id'];
		// }
		
		if(!$uid){$return['info']='会员信息不存在';return $return;}
		
		//检测名称重复
		$name_map['name']=$postdata['name'];
		$name_map['publish_wait']=array('neq',2);
		$name_map['uid']=array('neq',$uid);
		$is_shop=M('ShopsSubmit')->where($name_map)->find();
		if($is_shop){$return['info']='公司名称已被申请，请换个其他名称';return $return;}
		
		//店铺地址
		$str='';
		if($postdata['province'])$str.=$postdata['province'];
		if($postdata['city_id'])$str.=','.$postdata['city_id'];
		if($postdata['district'])$str.=','.$postdata['district'];
		
		//经营品类
		if(!empty($postdata['cate_ids'])){$postdata['cate_ids']=implode(',',$postdata['cate_ids']);}
		
		$postdata['uid']=$uid;
		$postdata['publish_wait']=0;
		$postdata['updatetime']=NOW_TIME;
		$postdata['licence_start']=strtotime($postdata['licence_start']);
		$postdata['licence_end']=strtotime($postdata['licence_end']);
		$postdata['postion']=$str;
		// M('ShopsSubmit')->create($postdata);
		if($postdata['id']){
			$res=M('ShopsSubmit')->where('id='.$postdata['id'])->save($postdata);
		}else{
			$res=M('ShopsSubmit')->add($postdata);
		}
		if($res===false){$return['info']='提交失败';return $return;}
		
		$return['status']=1;
		$return['info']='提交成功，等待后台审核';
        return $return;
    }
}
?>