Userinfo.class.php 11.2 KB
<?php
/**
 * 用户信息
 */
class Controller_Userinfo extends Controller_Abstract
{
    /**
     * 首页
     */
    public function indexAction()
    {
    	return 'Run';
    }
    
    
    /**
     * 获取用户信息接口
     */
    public function getUserinfoAction()
    {
    	if ($this->_request->isPost())
    	{
    		$account  = trim($this->_request->account) ;
    		$uid	  = trim($this->_request->uid) ;
    		$fields   = trim($this->_request->fields) ;
			$area     = isset($this->_request->area) ? trim($this->_request->area) : '';
    		
    		//两个参数不能同时为空
    		if (!$uid && !$account)
    		{
    			return $this->returnJson(Config_Code::$error['missing_parameter']['code'],'',Config_Code::$error['missing_parameter']['message']);
    		}
    		
    		//如果传递了Account并且uid为空,则根据Account获取口令信息
    		if (!$uid && $account) 
    		{
    			$authInfo = Facade_Auth::getAuthByAccount($account, '', $area);
    			if (!$authInfo)
    			{
    				return $this->returnJson(Config_Code::$error['user_not_exist']['code'],'',Config_Code::$error['user_not_exist']['message']);
    			}
    			$uid = $authInfo['uid'];
    		}
    		
    		//查询用户
    		$user = Facade_Userinfo::getUserinfoByUid($uid);
    		if (!$user) 
    		{
    			return $this->returnJson(Config_Code::$error['user_not_exist']['code'],'',Config_Code::$error['user_not_exist']['message']);
    		}
    		
    		//获取用户状态
    		if ( $user['state'] == Facade_Userinfo::USER_STATE_DISABLED )
    		{
    			return $this->returnJson(Config_Code::$error['user_disabled']['code'],'',Config_Code::$error['user_disabled']['message']);
    		}
    		$data = Facade_Userinfo::getUserinfoForApp($uid, $fields, $this->_appScope);
    		//返回密码
    		if (in_array($this->_appId, array('1392109210','1392109259','1392109260')))
    		{
    		    $data['password'] = Facade_Auth::getPassword($uid);
    		}
    		return $this->returnJson(Config_Code::$success['system']['code'], $data, Config_Code::$success['system']['message']);
    	}
    }
    
    /**
     * 更新用户信息
     */
    public function updateAction()
    {
    	if ($this->_request->isPost())
    	{
    		$uid	  = trim($this->_request->uid) ;
    		do 
    		{
    			//参数不能同时为空
    			if (!$uid)
    			{
    				$ret = $this->returnJson(Config_Code::$error['missing_parameter']['code'],'',Config_Code::$error['missing_parameter']['message']);
    				break ;
    			}
    			 
    			//查询用户
    			$user = Facade_Userinfo::getUserinfoByUid($uid);
    			if (!$user)
    			{
    				$ret =  $this->returnJson(Config_Code::$error['user_not_exist']['code'],'',Config_Code::$error['user_not_exist']['message']);
    				break ;
    			}
    			 
    			//获取用户状态
    			if ( $user['state'] == Facade_Userinfo::USER_STATE_DISABLED )
    			{
    				$ret = $this->returnJson(Config_Code::$error['user_disabled']['code'],'',Config_Code::$error['user_disabled']['message']);
    				break ;
    			}
    			//@TODO 获取该应用可以更新的信息
    			$allow_fields = explode(',',$this->_appScope) ;
    			$updateDate = array() ;
                        $value = null;
    			foreach ($allow_fields as $column)
    			{
    				$value =  trim($this->_request->query($column , '*&^%$#@!@#$%^&'));
                                // 2014/03/26 hf: 修复平台来源被更新的问题
    				if ($column != 'app_id' && $value != '*&^%$#@!@#$%^&') 
    				{
                                    $updateDate[$column] = $value;
    				}
    			}
    			if ($updateDate) 
    			{
    				Facade_Userinfo::updateUserinfo($uid, $updateDate);
    				$ret = $this->returnJson(Config_Code::$success['userinfo_set']['code'],'',Config_Code::$success['userinfo_set']['message']);
    				break ;
    			}
    			$ret = $this->returnJson(Config_Code::$error['userinfo_set']['code'],'',Config_Code::$error['userinfo_set']['message']);
    			break ;
    		}while (false);
    		return $ret ;
    	}
    }
    
    /**
     * 获取所有口令
     * @return json
     */
    public function getAllAccountAction()
    {
        if ($this->_request->isPost() || true)
        {
            $uid	= trim($this->_request->uid) ;
            if (!$uid) 
            {
            	return $this->returnJson(Config_Code::$success['system']['code'], array(), Config_Code::$success['system']['message']);
            }
            $result = array
            (
                'password' => Facade_Auth::getPassword($uid),
                'list' => Facade_Auth::getAllAuth($uid),
                'userinfo' => Facade_Userinfo::getUserinfoForApp($uid),
            );
            return $this->returnJson(Config_Code::$success['system']['code'], $result, Config_Code::$success['system']['message']);
        }
    }
    
    /**
     * 设置/更新 auth 账号
     */
    public function setauthAction()
    {
    	if ($this->_request->isPost()) 
    	{
    		$uid	= trim($this->_request->uid) ;
    		$type 	= intval($this->_request->type) ;
    		$auth   = trim($this->_request->auth);
    		$result = Facade_Auth::setAuth($uid, $type, $auth);
    		if (is_array($result)) 
    		{
    			return $this->returnJson($result['code'], '', $result['message']);
    		}
    		return $this->returnJson(Config_Code::$success['system']['code'], '', Config_Code::$success['system']['message']);
    	}
    }
    
    /**
     * 解除账号绑定
     * 
     * @return json
     */
    public function delauthAction()
    {
        if ($this->_request->isPost())
        {
            $uid	= trim($this->_request->uid) ;
            $auth_type 	= intval($this->_request->auth_type) ;
            $auth_id   = trim($this->_request->auth_id);
            
            $result = Facade_Auth::delAuth($uid, $auth_type, $auth_id);
            if (is_array($result))
            {
                return $this->returnJson($result['code'], '', $result['message']);
            }
            return $this->returnJson(Config_Code::$success['system']['code'], '', Config_Code::$success['system']['message']);
        }
    }
    /**
     * 修改密码
     */
    public function changepwdAction()
    {
    	if ($this->_request->isPost())
    	{
    		$uid	= trim($this->_request->uid) ;
    		$password 	= $this->_request->password ;
    		$result = Facade_Auth::setPassword($uid, $password,true);
    		if ($result !== true)
    		{
    			return $this->returnJson($result['code'], '', $result['message']);
    		}
    		return $this->returnJson(Config_Code::$success['system']['code'], '', Config_Code::$success['system']['message']);
    	}
    }
    
    /**
     * 获取登录账号的相关信息
     * 
     * @return json
     */
    public function getauthAction()
    {
        $authId = $this->_request->query('auth_id', '');
        $authType = $this->_request->query('auth_type', '');
        
        $result = array();
        
        if ($authId && is_numeric($authType))
        {
            $result = Facade_Auth::getAuthByTypeAndAid($authType, $authId);
        }
        
        return $this->returnJson(Config_Code::$success['system']['code'], $result, Config_Code::$success['system']['message']);
    }
    
    /**
     * 移动有货账号关联
     */
    public function removeYohobuyAccountAction()
    {
        $uid = $this->_request->query('uid', '');
        $sso_uid = $this->_request->query('sso_uid', '');
        if ($uid) 
        {
            if (!$sso_uid) 
            {
            	$find = Facade_Userinfo::getYohobuyAccountByShowId($uid) ;
            	$sso_uid = $find ? $find['sso_uid']:0;
            }
            if (!$sso_uid) 
            {
                //用户不存在
            	return $this->returnJson(Config_Code::$error['system']['code'], array(),Config_Code::$error['user_not_exist']['message']);
            }
        	$ret = Facade_Userinfo::removeYohobuyAccount($uid,$sso_uid);
        	if ($ret['code'] == 200)
        	{
        		//成功移除
        		return $this->returnJson(Config_Code::$success['system']['code'], array(), Config_Code::$success['system']['message']);
        	}else
        	{
        	    return $this->returnJson(Config_Code::$error['system']['code'], array(), $ret['message']);
        	}
        }
        return $this->returnJson(Config_Code::$error['system']['code'], array(), Config_Code::$error['system']['message']);
    }
    
    public function relevanceYohobuyAccountAction()
    {
        $uid = $this->_request->query('uid', '');
        $yohobuy_name = $this->_request->query('yohobuy_name', '');
        $yohobuy_pwd = $this->_request->query('yohobuy_pwd', '');
        if ($uid && $yohobuy_name && $yohobuy_pwd)
        {
            $ret = Facade_Userinfo::relevanceYohobuyAccount($uid,$yohobuy_name,$yohobuy_pwd);
            if ($ret['code'] == 200)
            {
                $sso_uid = $ret['sso_uid'];
                $name = $this->getYohobuyAccount($sso_uid);
                //成功
                return $this->returnJson(Config_Code::$success['system']['code'], array('account' => $name), Config_Code::$success['system']['message']);
            }else
            {
                return $this->returnJson(Config_Code::$error['system']['code'], array(), $ret['message']);
            }
        }
        return $this->returnJson(Config_Code::$error['system']['code'], array(), Config_Code::$error['system']['message']);
    }
    
    /**
     * 获取关联的有货账号
     */
    public function getYohobuyAccountAction()
    {
    	$uid = $this->_request->query('uid', '');
    	if ($uid) 
    	{
    		$info = Facade_Userinfo::getYohobuyAccountByShowId($uid);
    		if ($info) 
    		{
    		    $name = $this->getYohobuyAccount($info['sso_uid']);
    			return $this->returnJson(Config_Code::$success['system']['code'], array('account' => $name,'sso_uid' => $info['sso_uid'],'list' => Facade_Auth::getAllAuth($info['sso_uid'])), Config_Code::$success['system']['message']); 
    		}
    		return $this->returnJson(Config_Code::$success['system']['code'], array(), Config_Code::$success['system']['message']);
    	}
    	return $this->returnJson(Config_Code::$error['system']['code'], array(), Config_Code::$error['system']['message']);
    }
    
    private function getYohobuyAccount($sso_uid)
    {
        $name = '' ;
        $accounts = Facade_Auth::getAllAuth($sso_uid);
        $email = isset($accounts[Facade_Auth::AUTH_TYPE_EMAIL]) ? $accounts[Facade_Auth::AUTH_TYPE_EMAIL]['auth_id']:'';
        $mobile = isset($accounts[Facade_Auth::AUTH_TYPE_MOBILE]) ? $accounts[Facade_Auth::AUTH_TYPE_MOBILE]['auth_id']:'';
        if ($email) 
        {
            $name = $email ;
        	if ($mobile && strstr($email,'@yohoinc'))
        	{
        	    $name = $mobile ;
        	}
        }elseif ($mobile)
        {
            $name = $mobile ;
        }
        return $name ;
    }
    
    /**
     * 通过SSO_id获取要SHOW_uid 
     */
    public function getShowUidAction()
    {
    	$sso_uid  =  $this->_request->sso_uid ;
    	if ($sso_uid)
    	{
    		$find = Facade_Userinfo::getShowUidBySsoId($sso_uid);
    		if ($find)
    		{
    			return $this->returnJson(Config_Code::$success['system']['code'], $find, Config_Code::$success['system']['message']);
    		}
    	}
    	return $this->returnJson(Config_Code::$error['system']['code'], array(), '暂无关联用户');
    }
}