Account.php 11 KB
<?php

namespace Home;

use LibModels\Web\Home\AccountData;
use LibModels\Web\Home\UserData;
use WebPlugin\Helpers;

/**
 * 个人中心-账号安全
 */
class AccountModel
{

    /**
     * 账户安全首页
     * @param type $uid
     * @return type
     */
    public static function getAccountInfo($uid)
    {
        $result = array(
            array(
                'icon' => 'ok',
                'type' => '登录密码',
                'tip' => '互联网帐号存在被盗风险,建议您定期更改密码以保护帐号安全。',
                'red' => 'true',
                'url' => Helpers::url('/home/account/userpwd'),
                'isValid' => true
            ),
            array(
                'icon' => 'warning',
                'type' => '邮箱验证',
                'tip' => '验证后,可用于找回登录密码。',
                'url' => Helpers::url('/home/account/email')
            ),
            array(
                'icon' => 'warning',
                'type' => '手机验证',
                'tip' => '验证后,可用于找回登录密码。',
                'url' => Helpers::url('/home/account/mobile')
            )
        );
        $res = AccountData::getVerifyInfo($uid);
        if (isset($res['data']) && $res['data']) {
            $data = $res['data'];
            $result[1]['icon'] = ($data['emailVerify'] == 'N') ? 'warning' : 'ok';
            $result[1]['tip'] = ($data['emailVerify'] == 'N') ? '验证后,可用于找回登录密码。' : '您验证的邮箱:' . substr_replace($data['email'], '****', 2, 4);
            $result[1]['isValid'] = ($data['emailVerify'] == 'N') ? false : true;
            $result[2]['icon'] = ($data['mobileVerify'] == 'N') ? 'warning' : 'ok';
            $result[2]['isValid'] = ($data['mobileVerify'] == 'N') ? false : true;
            $result[2]['tip'] = ($data['mobileVerify'] == 'N') ? '验证后,可用于找回登录密码。' : '您验证的手机:' . substr_replace($data['mobile'], '****', 3, 4);
        }
        return $result;
    }

    /**
     * 校验当前输入手机号是否为本人的
     * @param type $uid
     * @param type $area
     * @param type $mobile
     * @return boolean
     */
    public static function checkMobileStep1($uid, $area, $mobile)
    {
        $check = false;
        $userInfo = UserData::getUserInfoByMobile($area, $mobile);
        $userId = isset($userInfo['data']['uid']) ? $userInfo['data']['uid'] : 0;
        if ($uid == $userId) {
            $check = true;
        }
        return $check;
    }

    /**
     * 个人中心-判断身份验证状态
     * @param type $uid
     * @param type $checkType
     * @param type $step page步骤
     * @return type
     */
    public static function auditCheckStatus($uid, $checkType = 'userpwd', $step = 1)
    {
        $ret = array();
        $res = AccountData::getVerifyInfo($uid);
        if (isset($res['data']) && $res['data']) {
            $data = $res['data'];
            $ischeckMobile = ($data['mobileVerify'] == 'N') ? false : true;
            $ischeckEmail = ($data['emailVerify'] == 'N') ? false : true;
            //优先验证标识
            $firstCheck = '';
            if ($ischeckMobile && $ischeckEmail) {
                $firstCheck = (strtotime($data['mobileVerifyTime']) <= strtotime($data['emailVerifyTime'])) ? 'mobile' : 'email';
            }

            $titleInfo = self::getTitles($ischeckMobile, $ischeckEmail, $checkType);

            $formData = array();
            $verifyType = 1;
            $checkEmailFlag = false;
            $checkMobileFlag = false;
            if ($step == 1) {
                $formData = self::getFormDataStep1($data, $ischeckMobile, $ischeckEmail, $firstCheck);
                $verifyType = $formData['verifyType'];
                $checkEmailFlag = $formData['checkEmailFlag'];
                $checkMobileFlag = $formData['checkMobileFlag'];
                unset($formData['verifyType']);
                unset($formData['checkEmailFlag']);
                unset($formData['checkMobileFlag']);
            }
            else if ($step == 2) {
                $formData = self::getFormDataStep2($ischeckEmail, $ischeckMobile, $checkType);
            }

            $ret = array(
                'ischeckMobile' => $ischeckMobile,
                'ischeckEmail' => $ischeckEmail,
                'email' => $data['email'],
                'mobile' => $data['mobile'],
                'subTitle' => $titleInfo['subTitle'],
                'enTitle' => $titleInfo['enTitle'],
                'pageKey' => $titleInfo['pageKey'],
                'formData' => $formData,
                'verifyType' => $verifyType,
                'checkEmailFlag' => $checkEmailFlag,
                'checkMobileFlag' => $checkMobileFlag
            );
        }

        return $ret;
    }

    /**
     * 获得标题文案
     * @param type $ischeckMobile
     * @param type $ischeckEmail
     * @param type $checkType
     */
    public static function getTitles($ischeckMobile, $ischeckEmail, $checkType = 'userpwd')
    {
        if ($checkType == 'mobile') {
            $subTitle = $ischeckMobile ? '修改手机' : '验证手机';
            $enTitle = $ischeckMobile ? 'CHANGE TELEPHONE' : 'VERIFICATION TELEPHONE';
            $pageKey = 'mobile';
        }
        elseif ($checkType == 'userpwd') {
            $subTitle = '修改密码';
            $enTitle = 'CHANGE PASSWORD';
            $pageKey = 'userpwd';
        }
        else {
            $subTitle = $ischeckEmail ? '修改邮箱' : '验证邮箱';
            $enTitle = $ischeckEmail ? 'CHANGE EMAIL' : 'VERIFICATION EMAIL';
            $pageKey = 'email';
        }
        return array('subTitle' => $subTitle, 'enTitle' => $enTitle, 'pageKey' => $pageKey);
    }

    /**
     * 第一部页面form结构-step1
     * @param type $data 用户验证相关信息
     * @param type $ischeckMobile
     * @param type $ischeckEmail
     * @param type $firstCheck
     * @return string
     */
    protected static function getFormDataStep1($data, $ischeckMobile, $ischeckEmail, $firstCheck)
    {
        //都没验证
        $formData1 = array(
            'inputTxt' => '请输入登录密码',
            'key' => 'password',
            'type' => 'password',
            'name' => 'password'
        );
        //只验证手机号
        $formData2 = array(
            'inputTxt' => '已验证的手机号',
            'isVerify' => true,
            'verifyAccount' => substr_replace($data['mobile'], '****', 3, 4),
            'realAccount' => $data['mobile']
        );
        //只验证邮箱
        $formData3 = array(
            'inputTxt' => '已验证邮箱',
            'isVerify' => true,
            'verifyAccount' => substr_replace($data['email'], '****', 2, 4),
            'realAccount' => $data['email']
        );

        //只验证手机号
        if ($ischeckMobile && !$ischeckEmail) {
            $formData = $formData2;
            $verifyType = 3;
            $checkEmailFlag = false;
            $checkMobileFlag = true;
        }
        //只验证邮箱
        else if ($ischeckEmail && !$ischeckMobile) {
            $formData = $formData3;
            $verifyType = 2;
            $checkEmailFlag = true;
            $checkMobileFlag = false;
        }
        //都验证
        else if ($ischeckMobile && $ischeckEmail) {
            $formData = ($firstCheck == 'mobile') ? $formData2 : $formData3;
            $verifyType = ($firstCheck == 'mobile') ? 3 : 2;
            $checkEmailFlag = ($firstCheck == 'mobile') ? false : true;
            $checkMobileFlag = ($firstCheck == 'mobile') ? true : false;
        }
        //没有验证
        else {
            $formData = $formData1;
            $verifyType = 1;
            $checkEmailFlag = false;
            $checkMobileFlag = false;
        }
        return array($formData, 'verifyType' => $verifyType, 'checkEmailFlag' => $checkEmailFlag, 'checkMobileFlag' => $checkMobileFlag);
    }

    /**
     * 第二步-formData
     * @param type $ischeckEmail
     * @param type $ischeckMobile
     * @param type $checkType
     * @return array
     */
    protected static function getFormDataStep2($ischeckEmail, $ischeckMobile, $checkType)
    {
        switch ($checkType) {
            case 'userpwd':
                $formData = array(
                    array(
                        'inputTxt' => '输入新密码',
                        'key' => 'newPwd',
                        'type' => 'password',
                        'name' => 'newPwd'
                    ),
                    array(
                        'inputTxt' => '确认新密码',
                        'key' => 'confirm_password',
                        'type' => 'password',
                        'name' => 'confirm_password'
                    )
                );
                break;
            case 'email':
                $formData = array(
                    array(
                        'inputTxt' => $ischeckEmail ? '新的邮箱' : '我的邮箱',
                        'key' => 'email',
                        'type' => 'text',
                        'name' => 'email'
                    )
                );
                break;
            case 'mobile':
                $formData = array(
                    array(
                        'inputTxt' => $ischeckMobile ? '输入新的手机号码' : '请输入手机号码',
                        'key' => 'mobilevalue',
                        'type' => 'text',
                        'name' => 'mobile'
                    )
                );
                break;
            default:
                $formData = array();
                break;
        }
        return $formData;
    }

    /**
     * 加密
     * @param type $data
     * @param type $key
     * @return type
     */
    public static function encrypt($data, $key = 'yoho520')
    {
        $prepCode = serialize($data);
        $block = mcrypt_get_block_size('des', 'ecb');
        if (($pad = $block - (strlen($prepCode) % $block)) < $block) {
            $prepCode .= str_repeat(chr($pad), $pad);
        }
        $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prepCode, MCRYPT_MODE_ECB);
        return base64_encode($encrypt);
    }

    /**
     * 解密
     * @param type $str
     * @param type $key
     * @return type
     */
    public static function decrypt($data, $key = 'yoho520')
    {
        $strCode = base64_decode($data);
        $str = mcrypt_decrypt(MCRYPT_DES, $key, $strCode, MCRYPT_MODE_ECB);
        $block = mcrypt_get_block_size('des', 'ecb');
        $pad = ord($str[($len = strlen($str)) - 1]);
        if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
            $str = substr($str, 0, strlen($str) - $pad);
        }
        return unserialize($str);
    }

}