Utils.php 4.64 KB
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 14-3-2
 * Time: 16:31
 */
class YHMAuth_User_Utils
{

    static $smsAuthPrivateKey = '05a9371223f4eab96818b7c4cabce1e7';

    static $defaultPassword = 'yohobuy9646abcdeedcba';

    /**
     * 检测第三方登录信息
     * @param $source
     * @param $uid
     * @param $nickName
     * @param null $email
     * @return array
     */
    static function check($source, $uid, $nickName, $email = null, array $package = array())
    {
        $trueEmail = $email;
        if (empty($email)) {
            $trueEmail = $email = $source . '__' . $uid . '@yohoinc.com';
        }
        $oldFormatEmail = $source . '__' . $uid . '@yohoe.cn';
        $_profile = QINPassport_Models_Profile_Client::getProfileByEmail($email);
        if (empty($_profile)) {
            $_profile = QINPassport_Models_Profile_Client::getProfileByEmail($oldFormatEmail);
            if (!empty($_profile)) {
                $trueEmail = $oldFormatEmail;
            }
        }
        $result = array(
            'email' => $trueEmail,
            'source' => $source,
            'nickname' => $nickName
        );
        return array_merge($result, $package);
    }

    /**
     * reg | repassword
     * @param $mobile
     * @param $package
     * @param int $sleep
     * @param string $project
     * @return array
     */
    static function sendSms($mobile, $package, $sleep = 86400, $project = 'reg')
    {
//         $ip = Q_Utils_Request::getClientIp();
//         $mc = QINLib_Utils::Mc('home', 'mobile');
//         $exists = $mc->get($ip);
//         if (empty($exists)) {
//             $mc->set($ip, 0, 86400);
//         }
//         $result = $mc->increment($ip, 1);
//         if ($result > 4) {
//             return array();
//         }
        
        if (empty($package['code']) || empty($package['token_key'])) {
            return array();
        }
        if (Q_Version::VERSION == 'QF.1.0') {
            $mc = Q_Cache::factory()->setPrefix('passport_space.timeout_put');
        } else {
            $mc = Q_Cache::factory(array(
                'domain' => 'passport_space',
                'class' => 'timeout_put'
            ));
        }
        $key = 'register_timeout_put_' . $mobile . '_' . $sleep . '_' . $project;
        $putSmsData = $mc->get($key);
        if (!empty($putSmsData)) {
            return $putSmsData;
        }
        $mc->set($key, $package, $sleep);
        $package['private_key'] = self::$smsAuthPrivateKey;
        $tokenKey = $package['token_key'];
        unset($package['token_key']);
        $verify = Q_Utils_AuthCode::verifySign(Q_Utils_AuthCode::getSign($package), $tokenKey);
        if (empty($mobile) && $verify == false) {
            return array();
        }
        $message = array('code' => $package['code']);
        QINSms_Utils_Message::Sms($project, $mobile, $message);
        return $package;
    }


    /**
     * 获得注册用户的来源
     * Enter description here ...
     */
    static function getChannel()
    {
        $yh_union_key = !isset($_COOKIE['_QYH_UNION']) || empty($_COOKIE['_QYH_UNION']) ? NULL : $_COOKIE['_QYH_UNION'];
        if (empty($yh_union_key)) {
            return 0;
        }
        $yh_union_info = Q_Utils_AuthCode::decode($yh_union_key, 'q_union_yohobuy');
        if (empty($yh_union_info)) {
            return 0;
        }
        $yh_union_info = json_decode($yh_union_info, true);
        if (empty($yh_union_info['client_id'])) {
            return 0;
        }
        return $yh_union_info['client_id'];
    }

    /**
     * 显示 iframe 登录
     * @param $uid
     * @param $channel
     * @param string $mode
     * @return string
     */
    static function getSyncStatusLink($uid, $channel, $mode = 'local')
    {
        $auth = YHMAuth_Factory::profile($channel, $mode);
        $session = $auth->getSession();
        $state = 2;
        if (!empty($session)) {
            $state = 1;
        }
        $ssoUid = 0;
        $_ssoProfile = QINPassport_Models_Sso_Client::getSsoRelationByUid($uid);
        if (!empty($_ssoProfile)) {
            $ssoUid = $_ssoProfile['sso_uid'];
        }
        $link = $auth->getSyncStatusLink($ssoUid, $state);
        #return '<iframe name="newssoproxy" src="' . $link . '" width="0" height="0" style="display:none"></iframe>';
        return '<script type="text/javascript" src="' . $link . '" async="async"></script>';
    }

    /**
     * @param $channel
     * @param string $mode
     * @return string
     */
    static function getCurrentLoginUidLink($channel, $mode = 'local')
    {
        $auth = YHMAuth_Factory::profile($channel, $mode);
        return $auth->getCurrentLoginUidLink();
    }
}