Passport.php 6.4 KB
<?php

namespace Passport;

use LibModels\Web\Home\IndexData;
use LibModels\Web\Home\UserData;
use Configs\WebCacheConfig;
use Configs\ChannelConfig;
use WebPlugin\Images;
use WebPlugin\Captcha;
use WebPlugin\Cache;
use WebPlugin\Helpers;

/**
 * web登录注册等相关数据构建
 *
 * @name PassportModel
 * @package models
 * @copyright yoho.inc
 * @version 1.0 (2015-12-29 14:38:00)
 * @author xiaowei <xiaowei.gong@yoho.cn>
 */
class PassportModel
{

    const REGISTER_LEFT_BANNER_CODE = 'c479ec90120cae7f96e52922b4917064'; //注册左边的banner
    const BACK_LFFT_BANNER_CODE = '3bbaf502c447a2ddad60879042e286d8'; //找回密码左边的banner
    const SIGNIN_LEFT_BANNER_CODE = 'db350894e01e90eac55cd3a13ad77331'; //登录页左边的banner
    const AUTOUSERINFO_LEFT_BANNER_CODE = 'c62d5da06d843b6ed78d8d27e87fa143'; //完善信息页左边的banner
    const BACK_FIND_SECRET_KEY = '_+@#$%^';

    /**
     * 获取左侧banner
     *
     * @param string $code
     * @return array
     */
    public static function getLeftBanner($code)
    {
        $ret = array();

        do {
            $key = WebCacheConfig::KEY_WEB_LOGIN_LEFT_BANNER . '_' . $code;
            $ret = Cache::get($key);
            if (!empty($ret)) {
                break;
            }

            $resource = IndexData::getResourceData($code);
            if (!isset($resource['data'][0])) {
                $ret = Cache::get($key, 'slave');
                if (empty($ret)) {
                    $ret = array('img' => '//img12.static.yhbimg.com/yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190', 'url' => '');
                }
                break;
            }

            if ($resource['data'][0]['template_name'] == 'single_image') {
                $val = current($resource['data'][0]['data']);
            }
            else if ($resource['data'][0]['template_name'] == 'single_name_image') {
                $val = $resource['data'][0]['data'];
            }
            $ret['img'] = Images::getImageUrl($val['src'], 252, 190);
            $ret['url'] =  Helpers::getUrlSafe($val['url']);

            // 设置缓存
            Cache::set($key, $ret);
        }
        while (false);

        return $ret;
    }

    /**
     * 校验验证码
     *
     * @param string $verifyCode
     * @return boolean
     */
    public static function verifyCode($verifyCode)
    {
        $verifyCode = strtolower($verifyCode);
        $ret = true;
        //检测验证码不正确
        if ($verifyCode != strtolower(Captcha::getFromSession('passport_istration'))) {
            $ret = false;
        }
        return $ret;
    }

    /**
     * 根据手机号获取用户信息
     *
     * @param string $area
     * @param string $mobile
     * @return array
     */
    public static function getUserInfoByMobile($area, $mobile)
    {
        $key = WebCacheConfig::KEY_PASSPORT_USER_DATA . '_' . $area . '_' . $mobile;
        $ret = Cache::get($key);
        if (empty($ret)) {
            $data = UserData::getUserInfoByMobile($area, $mobile);
            if ($data['code'] == 200) {
                if (!empty($data['data'])) {
                    $ret = $data['data'];
                    Cache::set($key, $ret, 600);
                }
            }
        }
        return $ret;
    }

    /**
     * 根据邮箱获取用户信息
     * 
     * @param string $email
     * @return array
     */
    public static function getUserInfoByEmail($email)
    {
        $key = WebCacheConfig::KEY_PASSPORT_USER_DATA . '_' . $email;
        $ret = Cache::get($key);
        if (empty($ret)) {
            $data = UserData::getUserInfoByEmail($email);
            if ($data['code'] == 200) {
                if (!empty($data['data'])) {
                    $ret = $data['data'];
                    Cache::set($key, $ret, 600);
                }
            }
        }
        return $ret;
    }

    /**
     * 确认邮箱登录是否绑定过手机号
     * @param type $email
     */
    public static function emailBindCheck($email)
    {
        $ret = false;
        $key = WebCacheConfig::KEY_PASSPORT_USER_DATA . '_bind_' . $email;
        $userInfo = Cache::get($key);
        if (empty($userInfo)) {
            $userInfo = UserData::getUserInfoByEmail($email);
            if ($userInfo['code'] == 200 && !empty($userInfo['data'])) {
                Cache::set($key, $userInfo, 600);
            }
        }
        $user = isset($userInfo['data']) ? $userInfo['data'] : '';
        if (isset($user['mobile']) && !$user['mobile']) {
            $ret = true;
        }
        return $ret;
    }

    /**
     * URL中是否包含关键字,需要跳转首页
     */
    public static function redirectHome($url)
    {
        $ret = false;
        $keyArr = ChannelConfig::$loginUrlKeys;
        foreach ($keyArr as $key) {
            if (strstr($url, $key)) {
                $ret = true;
                break;
            }
        }
        return $ret;
    }

    /**
     * 第三方登录 根据手机号获取用户相关信息
     */
    public static function getUserInfo($area, $mobile)
    {
        $userInfo = UserData::getUserInfoByMobile($area, $mobile);
        $user = array('username' => '', 'headImg' => '', 'bindLogin' => '');
        $userTmp = array();
        if (isset($userInfo['data'])) {
            $userTmp = $userInfo['data'];
        }
        if ($userTmp) {
            //*号临时处理
            $profileName = isset($userTmp['profile_name']) ? trim($userTmp['profile_name']) : '';
            if (!$profileName) {
                $profileName = $mobile;
            }
            if ((strlen($profileName) == 11 && !strpos('*', $profileName)) || (strpos($profileName, "-") && !strpos('*', $profileName))) {
                $profileName = substr_replace($profileName, '****', 3, 4);
            }
            $user = array(
                'username' => $profileName,
                'headImg' => (isset($userTmp['head_ico']) && !empty($userTmp['head_ico'])) ? Images::getImageUrl($userTmp['head_ico'], 100, 100, 2, 'yhb-head') : ChannelConfig::$headDefaultImgIco,
                'bindLogin' => Helpers::url('/signin.html', array('bindMobile' => $mobile, 'bindArea' => $area)),
            );
        }
        return $user;
    }

}