Local.php 7.75 KB
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 14-2-17
 * Time: 23:59
 */
class YHMAuth_User_Local extends YHMAuth_User_Abstract
{
	
	
	public function isExistAuth($profile) {
		//手机号
		$isMobile = Q_Utils_Validate_Mobile::isValid($profile);
		if ($isMobile) {
			$exist = YHMPassport_Models_Auth_Client::getProfileByMobile($profile);
			if (!empty($exist)) {
				return true;
			}
		}
		return false;
	}
	
    /**
     * 登录
     * @param $profile
     * @param $inputPassword
     * @param string $profileType
     * @param string $persistent
     * @throws Exception
     */
    public function signin($profile, $inputPassword = '', $persistent = 'Y', $append=array())
    {
        ######################  登录开始触发 ######################
        $this->signinPreDispatch($profile, $inputPassword);
        ######################  登录开始触发 ######################
        $isEmail = Q_Utils_Validate_EmailAddress::isValid($profile);
       	if($isEmail) {
       		$authInfo = YHMPassport_Models_Auth_Client::getProfileByEmail($profile);
       	} else {
       		$authInfo = YHMPassport_Models_Auth_Client::getProfileByMobile($profile);
       	}
       	if(empty($authInfo)) {
       		throw new Exception('没有找到该用户', 422);
       	}
        //判断是否删除账户
        $user_status = YHMPassport_Models_Profile_Client::getInfoByStatus($authInfo['uid'], 0);
        if(!empty($user_status)){
            throw new Exception('您输入的帐号无法登录,如有疑问请咨询客服', 427);
        }
       	$uid = $authInfo['uid'];
       	$profile = YHMPassport_Models_Profile_Client::getByUid($uid);
       	$authPass = Q_Utils_AuthCode::authPassword($inputPassword, $profile['password']);
        if(empty($authPass)) {
        	throw new Exception('账户或密码错误', 422);
        }
        $this->signinEndDispatch($uid, $append);
        #################### 登录结束触发 ####################
        $this->_uid = $uid;
        return $uid;
    }

    /**
     * 注册
     * @param $profile
     * @param $password
     * @param string $profileType
     * @param array $append
     * @throws Exception
     */
    public function register($profile, $password, array $append = array())
    {
        ######################  注册开始触发 ######################
        $this->registerPreDispatch($profile, $password, $append);
        
        $nick_name = $append['nick_name'];
        $password = Q_Utils_AuthCode::makePass($password);
        $isEmail = Q_Utils_Validate_EmailAddress::isValid($profile);
        if($isEmail) {
        	//email
        	$exist = YHMPassport_Models_Auth_Client::getProfileByEmail($profile);
        	if (!empty($exist)) {
        		throw new Exception('该Email已经注册');
        	}
        	$uid = YHMPassport_Models_Profile_Client::addUserProfile($password, $nick_name);
        	if (empty($uid)) {
        		throw new Exception('添加用户失败');
        	}
        	$ret = YHMPassport_Models_Auth_Client::addAuth($uid, $profile, YHMConfig_Passport::PROFILE_TYPE_MAIL, $this->_channel, $this->_channelMap['channel_code']);
        	if(empty($ret)) {
        		throw new Exception('添加用户认证信息失败');
        	}
        } else {
        	//mobile
        	$exist = YHMPassport_Models_Auth_Client::getProfileByMobile($profile);
        	if(!empty($exist)) {
        		throw new Exception('该手机号已经注册');
        	}
        	$uid = YHMPassport_Models_Profile_Client::addUserProfile($password, $nick_name);
        	$ret = YHMPassport_Models_Auth_Client::addAuth($uid, $profile, YHMConfig_Passport::PROFILE_TYPE_MOBILE, $this->_channel, $this->_channelMap['channel_code']);
        	if(empty($ret)) {
        		throw new Exception('添加用户认证信息失败');
        	}
        }
        $this->registerEndDispatch($uid, $append);
        $this->_uid = $uid;
        return $uid;
    }

    /**
     * 退出
     * @param $token
     * @param string $sessionKey
     * @param null $domain
     * @return bool
     */
    public function signOut($token, $sessionKey = '_UID', $domain = null)
    {
        if (!empty($token) && $token == $this->getSession('token_session_key')) {
            $this->signOutPreDispatch($this->getSession());
            $this->setCookieID('', null, '_UID', -1);
            $this->unsetSession();
            $this->signOutEndDispatch($this->getSession());
            return true;
        }
        return false;
    }

    /*
     * 第三方登录
     */
    public function associate($profile, array $append = array())
    {
        ######################  登录开始触发 ######################
        $this->associatePreDispatch($profile, $append);
        ######################  登录开始触发 ######################
        
        #### 检查是否关联过 #####
        $auth = YHMPassport_Models_Auth_Client::getAuthByOpenID($profile, $append['profile_type']);
        if (!empty($auth)) {
        	$uid = $auth['uid'];
        } else {
        	$uid = YHMPassport_Models_Profile_Client::addUserProfile('', $append['nick_name'], $append['head_ico'], $append['birthday'], $append['gender']);
        	if (empty($uid)) {
        		return self::result(500, '创建用户失败.', $auth);
        	}
        	YHMPassport_Models_Auth_Client::addAuth($uid, $userProfile->profile, $append['nick_name'], $append['profile_type'], $this->_channel, $append['channel']);
        }
        
        #################### 结束时触发 ####################
        $this->associateEndDispatch($uid, $append);

        $this->_uid = $uid;
        return $uid;
    }
    
    /**
     * (non-PHPdoc)
     * @see YHMAuth_User_Interface::updatePassword()
     */
    public function updatePassword($uid, $inputPassword) {
    	if(intval($uid)<1 || empty($inputPassword)) {
    		return false;
    	}
    	$password = Q_Utils_AuthCode::makePass($inputPassword);
    	return YHMPassport_Models_Profile_Client::updatePassword($uid, $password);
    }
    
    /**
     * (non-PHPdoc)
     * @see YHMAuth_User_Interface::updateInfo()
     */
    public function updateInfo($uid, $userInfo, $profile='', $profile_type=null){
    	$nick_name = !empty($userInfo['nickname']) ? $userInfo['nickname'] : '';
    	$gender = $userInfo['gender'];
    	$code_address = !empty($userInfo['area_code']) ? $userInfo['area_code'] : '';
        //如果修改昵称进行后台审核
        $user_info = YHMPassport_Models_Profile_Client::getByUid($uid);
        if($user_info['nick_name']!=$nick_name){
            YHMPassport_Models_Profile_Client::updateStatus($uid, 2);
        }
    	return YHMPassport_Models_Profile_Client::setUserInfoById($uid, $nick_name, $gender, $code_address);
    }
    
    /**
     * 添加一种认证方式
     * @param integer $uid
     * @param string $profile
     * @param int $profile_type
     */
    public function addAuth($uid, $profile, $profile_type, $channel, $password='', $account_name=''){
    	$profileInfo = YHMPassport_Models_Profile_Client::getByUid($uid);
    	if(empty($profileInfo)) {
    		throw new Exception('没有找到该用户', 421);
    	}
    	$existAuth = YHMPassport_Models_Auth_Client::checkBindByOpenID($profile, $profile_type);
    	if(!empty($existAuth)) {
    		throw new Exception($profile . '已经被绑定', 425);
    	}
    	if(in_array($profile_type, array(1,2))) {
    		//绑定的是手机号或者邮箱,必须要设置密码
    		if(strlen($profileInfo['password'])<32 && empty($password)) {
    			throw new Exception('必须设置一个密码才能完成绑定', 422);
    		}
    		$_password = Q_Utils_AuthCode::makePass($password);
    		YHMPassport_Models_Profile_Client::updatePassword($uid, $_password);
    	}
    	return YHMPassport_Models_Auth_Client::addAuth($uid, $profile, $account_name, $profile_type, $this->_channel, $channel);
    }
}