Autouserinfo.php 13.1 KB
<?php

use Action\WebAction;
use LibModels\Web\Passport\RegData;
use LibModels\Web\Passport\BindData;
use LibModels\Web\Home\UserData;
use WebPlugin\Images;
use Passport\PassportModel as PassportModel;
use WebPlugin\Helpers;

class AutouserinfoController extends WebAction
{

    /**
     * 第三方登录完善个人信息(旧)
     * 绑定手机号
     */
    public function indexAction()
    {
        $this->setTitle('联合登录补全信息');
        $this->setSimpleHeader();
//        $nickname = $this->get('nickname');
        $openId = trim($this->get('openId'));
        $sourceType = trim($this->get('sourceType'));

        //登录后跳转页面
        $refer = rawurldecode($this->getCookie('refer'));
        $isRedirectHome = PassportModel::redirectHome($refer);
        if (empty($refer) || $isRedirectHome) {
            $refer = SITE_MAIN;
        }

        $cover = PassportModel::getLeftBanner(PassportModel::AUTOUSERINFO_LEFT_BANNER_CODE);

        //整合
        $data = array(
            'thirdPage' => true,
            'passport' => array(
                'actionUrl' => Helpers::url('/passport/autouserinfo/bind'),
                'region' => RegData::getAreasData(),
                'location' => '+86',
                'captchaUrl' => Helpers::url('/passport/images?t=' . time()),
                'itemUrl' => Helpers::url('/help/?category_id=9'),
                'referUrl' => $refer,
                'skipUrl' => SITE_MAIN,
                'coverHref' => $cover['url'],
                'coverImg' => !empty($cover['img']) ? $cover['img'] : 'http://img12.static.yhbimg.com/yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
                'regBtnText' => '完善信息',
                'openId' => $openId,
                'sourceType' => $sourceType
            )
        );

        $this->_view->display('index', $data);
    }

    /**
     * 完善信息页面(邮箱登录,手机绑定流程中间页)
     */
    public function userInfoAction()
    {
        $cover = PassportModel::getLeftBanner(PassportModel::AUTOUSERINFO_LEFT_BANNER_CODE);
        //整合
        $data = array(
            'passport' => array(
                'coverHref' => $cover['url'],
                'coverImg' => !empty($cover['img']) ? $cover['img'] : 'http://img12.static.yhbimg.com/yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
                'goShoppingUrl' => SITE_MAIN,
                'completeInfoUrl' => Helpers::url('/passport/emailbind/index'),
            ),
        );

        $this->setSimpleHeader();
        $this->_view->display('userinfo', $data);
    }

    /**
     * 绑定前手机号校验
     * 若已注册,则查询用户头像,昵称信息
     * 
     */
    public function bindCheckAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            $mobile = trim($this->post('mobile'));
            $openId = trim($this->post('openId'));
            $area = trim($this->post('area', '86'));
            $sourceType = trim($this->post('sourceType'));

            if (!is_numeric($mobile) || !$openId || !$area || !$sourceType) {
                break;
            }

            $res = BindData::bindCheck($mobile, $openId, $sourceType, $area);
            if (!isset($res['code'])) {
                break;
            }
            //未注册 可绑定
            if ($res['code'] == 200 && $res['data']['is_register'] == 0) {
                $next = Helpers::url('/passport/thirdlogin/noregist', array(
                    'openId' => $openId,
                    'sourceType' => $sourceType,
                    'area' => $area,
                    'mobile' => $mobile,
                ));
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('next' => $next));
            }
            //已注册 绑定过其他第三方 可绑定
            elseif ($res['code'] == 200 && $res['data']['is_register'] == 1) {
                $user = self::getUserInfo($area, $mobile);
                $data = array('code' => 506, 'message' => $res['message'], 'data' => array('user' => $user));
            }
            //506 已注册 绑定过同一种第三方 / 505 - 已注册 已绑定 --统一处理为code=505
            elseif ($res['code'] == 506 || $res['code'] == 505) {
                $user = self::getUserInfo($area, $mobile);
                $data = array('code' => 505, 'message' => $res['message'], 'data' => array('user' => $user));
            } else {
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => isset($res['data']) ? $res['data'] : '');
            }
        } while (false);

        $this->echoJson($data);
    }

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

    /**
     * 发送验证码
     */
    public function sendBindMsgAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            $mobile = trim($this->post('mobile'));
            $area = trim($this->post('area'));
            //校验手机号格式
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['code'] = 400;
                $data['message'] = '手机号码格式不正确';
                break;
            }

            $data = BindData::sendBindMsg($area, $mobile);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 校验短信验证码
     */
    public function checkBindMsgAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            $mobile = trim($this->post('mobile'));
            $area = trim($this->post('area'));
            $code = trim($this->post('code')); //短信验证码
            //校验手机号格式
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['code'] = 400;
                $data['message'] = '手机号码格式不正确';
                break;
            }

            $data = BindData::checkBindCode($area, $mobile, $code);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 绑定手机号
     */
    public function bindMobileAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            $mobile = trim($this->post('mobile'));
            $area = trim($this->post('area', '86'));
            $openId = trim($this->post('openId'));
            $sourceType = trim($this->post('sourceType'));
            $code = trim($this->post('code')); //短信验证码
            $password = trim($this->post('password'));
            $password = empty($password) ? '' : $password;

            //校验手机号格式
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['code'] = 400;
                $data['message'] = '手机号码格式不正确';
                break;
            }
            //校验手机验证码
            $bindMsgFlag = BindData::checkBindCode($area, $mobile, $code);
            if (isset($bindMsgFlag['code']) && $bindMsgFlag['code'] != 200) {
                $data['code'] = 400;
                $data['message'] = '短信验证码不正确';
                break;
            }
            //绑定手机号
            $res = BindData::bindMobile($openId, $sourceType, $mobile, $area, $password);
            if (!isset($res['code'])) {
                break;
            }
            //绑定成功,跳转页面
            if (isset($res['code']) && $res['code'] == 200 && !empty($res['data']['uid'])) {
                $token = Helpers::makeToken($res['data']['uid']);
                $this->setSession('_TOKEN', $token);
                $this->setSession('_LOGIN_UID', $res['data']['uid']);
                $this->setCookie('_TOKEN', $token);
                $refer = Helpers::syncUserSession($res['data']['uid'], Helpers::url('/passport/thirdlogin/bindsuccess', array('sourceType' => $sourceType)));
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('refer' => $refer));
            } else {
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('refer' => ''));
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 邮箱登录手机号绑定流程,检查手机号是否可以绑定
     */
    public function changeCheckAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            $mobile = $this->post('mobile');
            $area = $this->post('area');

            if (!is_numeric($mobile) || !$area) {
                break;
            }

            $res = BindData::changeCheck($mobile, $area);
            if (!isset($res['code'])) {
                break;
            }
            //code=200 继续绑定  
            if ($res['code'] == 200) {
                $next = Helpers::url('/passport/emailbind/noregist', array(
                        'area' => $area,
                        'mobile' => $mobile,
                ));
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('next' => $next));
            }
            //506-弹出换手机弹窗
            elseif ($res['code'] == 506) {
                $user = self::getUserInfo($area, $mobile);
                $data = array('code' => 506, 'message' => $res['message'], 'data' => array('user' => $user));
            } else {
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => isset($res['data']) ? $res['data'] : '');
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 邮箱登录手机号绑定流程
     */
    public function changeMobileAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            $uid = $this->getUid(true);
            $mobile = $this->post('mobile');
            $area = $this->post('area');
            $code = $this->post('code');

            if (!is_numeric($mobile) || !$area || !$code || !$uid) {
                break;
            }

            $res = BindData::changeMobile($uid, $mobile, $area, $code);
            if (!isset($res['code'])) {
                break;
            }
            //绑定成功,跳转页面
            if (isset($res['code']) && $res['code'] == 200 && !empty($res['data']['uid'])) {
                $refer = Helpers::url('/passport/emailbind/bindsuccess');
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('refer' => $refer));
            } else {
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('refer' => ''));
            }
        } while (false);

        $this->echoJson($data);
    }

}