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

namespace Common\Model;
use Think\Model;
class SupplierLoginModel extends Model{
    protected $tableName = 'Suppliers';
	public function _initialize(){
		parent::_initialize ();
       	$forward=I('supplier_forward')?I('supplier_forward'):cookie('supplier_forward');
      	$this->jumpUrl = $forward? $forward: U('Supplier/Index/index');
    }
    /**登陆
     * @param $param 登陆所需参数数组
     * @return array 返回状态、信息
     */
    public function check_login($param){
        $return=array('status'=>0,'info'=>'');
        if(empty($param)){$return['info']='参数错误';return $return;}
		
        $username=$param['name'];
		$password=$param['password'];
		$verify=$param['verify'];
        if(empty($username) || empty($password)){$return['info']='请输入用户名或密码'; return $return;}
        if(empty($verify)){
			$return['info']='请输入验证码';return $return;
		}elseif(!check_verify($verify,'Member')){
            $return['info']=L('verify_error');return $return;
        }
        $map['username|phone']= array($username,$username,'_multi'=>true);
        $map['status'] = 1;
        $userinfo = M('Suppliers')->where($map)->field('password,id')->find();
        if($userinfo){
			$postpassword = md5($password);
			if($postpassword==$userinfo['password']){
				$s_map['id']=$userinfo['id'];
				$s_map['status']=1;
				$supplierinfo=M('Suppliers')->where($s_map)->field(true)->find();
				if(empty($supplierinfo)){$return['info']='此供货商已禁止使用';return $return;}
				$return['status']=1;
				$return['info']='登录成功';
				session('suppliers_info',$supplierinfo);
				$return['url']=$this->jumpUrl;
				if(cookie('supplier_forward')){cookie('supplier_forward',null);}
			}else{
				$return['info']='密码错误';return $return;
			}
        }else{
			$return['info']='用户不存在,或者被删除!';
        }
        if($return['status']==1){
            $this->loginupdate($supplierinfo['id']);
            action_log('supplier_login', 'Supplier', $supplierinfo['id'],$supplierinfo['id']);//供货商登录日志
        }
        return $return;
    }
	
	/**
     * 更新数据
     */
    public function loginupdate($shop_id){
        if(!$shop_id)return false;
        $savedata['login_count'] = array('exp', 'login_count+1');
        $savedata['last_login'] = NOW_TIME;
        $savedata['last_ip'] = get_client_ip(1);
        M('Shops')->where('id='.$shop_id)->save($savedata);
        $shopinfo=M('Shops')->where('id='.$shop_id)->field(true)->find();
        $shopinfo['shop_info_auth_sign'] = data_auth_sign($shopinfo);
        session('shop_info', $shopinfo);
    }
	/**
     * 退出登录
     */
    public function logout(){
        $shop_info=session('suppliers_info');
        $url=U('Supplier/Register/sup_login');
        if(isset($shop_info)){
            session('suppliers_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'=>'');
		$supplier_info=session('suppliers_info');
		
		if(empty($supplier_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('Suppliers')->where('id='.$supplier_info['id'])->field('password')->find();
		
		
		$old_psw=md5($data['password']);
		if($old_psw != $userinfo['password']){$return['info']='旧密码不正确!';return $return;}
		
		
		$save['password'] = md5($data['new_password']);
		$result=M('Suppliers')->where('id='.$supplier_info['id'])->save($save);
		if($result){
			session('suppliers_info',null);
			action_log('supplier_resetpwd', 'Supplier', $supplier_info['id'], $supplier_info['id']);//日志
			$return['status']=1;
			$return['info']='修改密码成功！请重新登录';
		}else{
			$return['info']='修改失败';
		}
		return $return;
	}
	/**
     * 找回密码获取店铺信息
     * @param array $postdata
     * @return array
     */
	 public function get_sup($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['phone']=$data['mobile'];
		$supplierinfo=M('Suppliers')->where($u_map)->field('id,status')->find();
		
		if(empty($supplierinfo) || $supplierinfo['status']==0){$return['info']='供货商不存在或已禁用';return $return;}
		
		$return['status']=1;
		$return['id'] =$supplierinfo['id'];
		return $return;
	}
	/**
     * 找回密码
     * @param array $postdata
     * @return array
     */
	 public function getpwd($data){
		$return=array('status'=>0,'info'=>'');
		
		if(!$data['id']){$return['info']='供货商信息错误';return $return;}
		if(!$data['password'] || !$data['repassword'] || $data['password']!=$data['repassword']){
			$return['info']='您输入的新密码为空或不一致,请重新输入!';return $return;
		}
		$save['password'] = md5($data['password']);
		$result=M('Suppliers')->where('id='.$data['id'])->save($save);
		if($result !== false){
			$return['status']=1;
			$return['info']='找回密码成功！';
			$supplier_id=idtoname($data['id'],'Suppliers','id');
			action_log('supplier_getpwd', 'Supplier', $supplier_id, $supplier_id);//日志
		}else{
			$return['info']='找回密码失败！';
		}
		return $return;
	}
    /**
     * 申请店铺
     * @param $postdata
     * @return mixed
     */
    public function register($postdata){
        /*$return=array('status'=>0,'info'=>'');
		if(empty($postdata['cate_ids'])){$return['info']='请选择经营类目';return $return;}
		
		$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']);
			$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();
		}else{
			$res=M('ShopsSubmit')->add();
		}
		if($res===false){$return['info']='提交失败';return $return;}
		
		$return['status']=1;
		$return['info']='提交成功，等待后台审核';
        return $return;*/
    }
}
?>