validate.page.js 12.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
/**
 * 个人中心页-账号安全验证
 * @author:wsl<shuiling.wang@yoho.cn>
 * @date: 2016/02/23
 */
var $ = require('yoho-jquery'),
    Captcha = require('../plugins/captcha');

var dialog = require('../common/dialog');

var phoneRegx = require('../passport/mail-phone-regx');

var Alert = dialog.Alert;

var $checkUser = $('.check-user'),
    $checkInput = $checkUser.find('input').not('input[type=button],input[type=hidden],input[type=reset]'),
    canSend = true,
    canFlag = true,
    stime = 60;

var sInt,
    active;

var captcha = new Captcha('.captcha-safe-form-r').init();

captcha.refresh();

require('../common');
$.ajaxSetup({
    async: false
});

if (!Array.prototype.indexOf) {// eslint-disable-line
    Array.prototype.indexOf = function(obj, start) {// eslint-disable-line
        var i,
            j;

        for (i = (start || 0), j = this.length; i < j; i++) {
            if (this[i] === obj) {
                return i;
            }
        }

        return -1;
    };
}

function errorInfoAction(opt, txt) {
    canFlag = false;
    opt.$checkInfo.html('<div class="form-error">' + txt + '</div>');
    if (opt.dom.attr('name') !== 'code' && opt.dom.attr('name') !== 'verifyCode') {
        opt.dom.addClass('input-error');
    }
    return false;
}

function successInfoAction(opt) {
    canFlag = true;
    opt.$checkInfo.html('<div class="form-success">&nbsp;</div>');
    if (opt.dom.attr('name') !== 'code' && opt.dom.attr('name') !== 'verifyCode') {
        opt.dom.removeClass('input-error');
    }
    return true;
}

function checkFormAjax(ajaxData) {
    var res;

    $.post(ajaxData.url, ajaxData.data, function(data) {
        if (typeof data.code !== 'undefined' && data.code === 200) {
            res = successInfoAction(ajaxData.opt);
        } else {
            ajaxData.txt = ajaxData.txt !== '' && ajaxData.txt !== null ? ajaxData.txt : data.message;
            res = errorInfoAction(ajaxData.opt, ajaxData.txt);
        }
    });
    return res;
}

// 身份校验
function checkForm(dom) {
    var val = dom.val(),
        len = val.length,
        $domParent = dom.parent(),
        $checkInfo = $domParent.find('.check-info'),
        inputName = dom.attr('name'),
        regular = '',
        text = '',
        mobileValue = '',
        opt = {
            $checkInfo: $checkInfo,
            dom: dom
        },
        ajaxData = {
            opt: opt
        };

    $checkInfo.html('');

    if (inputName === 'password') {
        if (len === 0) {
            return errorInfoAction(opt, '密码不能为空!');
        } else {
            $.extend(ajaxData, {
                url: '/home/account/checkpassword',
                data: {
                    password: val
                },
                txt: '密码错误!'
            });
            return checkFormAjax(ajaxData);
        }
    } else if (inputName === 'email') {
        regular = phoneRegx.emailRegx;

        if (val.match(regular) === null) {
            return errorInfoAction(opt, '邮箱错误!');
        } else {
            $.extend(ajaxData, {
                url: '/home/account/checkemail',
                data: {
                    email: val
                },
                txt: ''
            });
            return checkFormAjax(ajaxData);
        }
    } else if (inputName === 'mobile') {
        regular = phoneRegx.phoneRegx;
        text = val.split('-');

        if (text.length === 1) {
            regular = val.match('^1[35847]{1}[0-9]{9}');
        } else {
            regular = regular['+' + text[0]].test(text[1]);
        }

        if (len === 0) {
            return errorInfoAction(opt, '手机号不能为空!');
        } else if (regular === null || !regular) {
            return errorInfoAction(opt, '手机号错误!');
        } else {
            $.extend(ajaxData, {
                url: '/home/account/checkmobile',
                data: {
                    mobile: val
                },
                txt: '手机号已经存在!'
            });
            return checkFormAjax(ajaxData);
        }
    } else if (inputName === 'newPwd') {
        regular = phoneRegx.pwdValidateRegx;

        if (!regular.test(val)) {
            return errorInfoAction(opt, '建议6~20个数字+字母组合!');
        } else {
            return successInfoAction(opt);
        }
    } else if (inputName === 'confirm_password') {
        if ($('#newPwd').val() !== val) {
            return errorInfoAction(opt, '两次密码不一致!');
        } else {
            return successInfoAction(opt);
        }

        // else if ($('#newPwd').val() !== '') {
        //     $('#newPwd').next().html('<div class="form-success">&nbsp;</div>');
        //     return successInfoAction(opt);
        // }
    } else if (inputName === 'code') {
        mobileValue = $('#realAccount').length > 0 ? $('#realAccount').val() : $('#mobilevalue').val();

        if (val !== '') {
            $.extend(ajaxData, {
                url: '/home/account/checkmobilemsg',
                data: {
                    mobile: mobileValue,
                    code: $('#inputcode').val()
                },
                txt: '验证码错误!'
            });
            return checkFormAjax(ajaxData);
        }
    } else {
        return true;
    }
}

// 校验表单
function checkAllForm() {
    var arr = [];

    $.each($checkInput, function(key, item) {
        arr[key] = checkForm($(item));
    });

    if (captcha.$container.length !== 0) {
        captcha.check().then(function() {
            arr.push(true);
        }).fail(function() {
            arr.push(false);
        });
    }

    if (arr.indexOf(false) >= 0) {
        return false;
    } else {
        return true;
    }
}

// 切换验证码
function changeCode() {
    var timestamp = (new Date()).getTime();

    $('#the-code-img').attr('src', '/passport/imagesNode?len=6&time=' + timestamp);
}

// 重新发送倒计时
function code() {
    var sstring = '';

    if (stime > 0) {
        sstring = '重新发送' + stime + '秒';
        $('#sendButton').text(sstring);
        stime = stime - 1;
    } else {
        stime = 60;
        $('#sendButton').text('发送验证码');
        clearInterval(sInt);
        canSend = true;
    }
}

// 发送手机验证码ajax请求
function sendMobileMsg(mobileV) {
    var $code = $('#inputcode'),
        $ccheckInfo = $code.parent().find('check-info');

    $.post('/home/account/sendmobilemsg', {
        mobile: mobileV
    }, function(data) {
        if (typeof data.code !== 'undefined' && data.code === 200) {
            canSend = false;
            sInt = setInterval(function() {
                code();
            }, 1000);
            $ccheckInfo.html('');
        } else {
            $ccheckInfo.html('<div class="form-error">验证码发送失败</div>');
        }
    });
}

// 发送手机验证码
function sendcode() {
    var $mobile = $('#mobilevalue');

    var $mcheckInfo,
        mobileV,
        mobileObj;

    if ($mobile.length > 0) {
        $mcheckInfo = $mobile.next();
        mobileV = $mobile.val();
        mobileObj = mobileV.split('-');

        if (mobileObj.length === 1) {
            mobileObj = mobileV.match('^1[35847]{1}[0-9]{9}');
        } else {
            mobileObj = phoneRegx.phoneRegx['+' + mobileObj[0]].test(mobileObj[1]);
        }
    } else {
        mobileV = $('#realAccount').val();
    }

    if (canSend) {
        if ($mobile.length > 0) {
            if (mobileObj === null || !mobileObj) {
                $mcheckInfo.html('<div class="form-error">手机号错误!</div>');
                $mobile.addClass('input-error');
                return false;
            }

            if ($mcheckInfo.find('.form-success').length > 0) {
                sendMobileMsg(mobileV);
            }
        }

        if ($('#realAccount').length > 0) {
            sendMobileMsg(mobileV);
        }
    } else {
        return false;
    }
}

// 验证完成后倒计时跳转
function toHome() {
    window.location.href = '/home/account';
}

// ajax公共处理模块
function ajaxAction(opt, flag) {
    $.post(opt.url, opt.data, function(data) {
        if (data.code === 200) {
            if (flag) {
                opt.hrefUrl += '&checkCode=' + data.data;
            }
            window.location.href = opt.hrefUrl;
        } else {
            canFlag = true;
            active = new Alert(data.message);
            active.show();
            return false;
        }
    }, 'json');
}

// 提交表单
function submitForm() {
    var step = $('.progress-bar .cur').index(),
        verifyType = $('#verifyType').val(),
        curType = '',
        opt = {};

    if ($('.email').length > 0) {
        curType = 'email';
    }

    if ($('.mobile').length > 0) {
        curType = 'mobile';
    }

    if ($('.userpwd').length > 0) {
        curType = 'userpwd';
    }

    /*
     * step 0: 验证身份 1:验证第二步骤 2:验证第三步骤
     * verifyType 验证身份的状态 1:登录密码验证 2:邮箱的身份验证 3:手机的身份验证
     */
    if (step === 0) {
        if (verifyType === '1') {
            opt = {
                url: '/home/account/verifypassword',
                data: $('#pwdform').serialize(),
                hrefUrl: '/home/account/' + curType + '?step=2'
            };

            ajaxAction(opt, 'step1');

            // if (!ajaxAction(opt)) {
            //     $("input[type=reset]").trigger("click");
            // }
        } else if (verifyType === '2') {
            opt = {
                url: '/home/account/sendemail',
                data: {
                    checkType: curType,
                    email: $('#realAccount').val()
                },
                hrefUrl: '/home/account/sendemailsuccess?email=' + $('#realAccount').val() +
                        '&type=1&checkType=' + curType
            };
            ajaxAction(opt);
        } else {
            opt = {
                url: '/home/account/checkmobilemsg',
                data: {
                    mobile: $('#realAccount').val(),
                    code: $('#inputcode').val()
                },
                hrefUrl: '/home/account/' + curType + '?step=2'
            };
            ajaxAction(opt, 'step1');
        }
    } else if (step === 1) {
        if (curType === 'userpwd') {
            opt = {
                url: '/home/account/modifypwd',
                data: $('#pwdform').serialize(),
                hrefUrl: '/home/account/userpwd?step=3&success=true'
            };
            ajaxAction(opt);
        } else if (curType === 'email') {
            opt = {
                url: '/home/account/modifyemail',
                data: {
                    email: $('#email').val()
                },
                hrefUrl: '/home/account/sendemailsuccess?email=' + $('#email').val() +
                        '&type=2&checkType=email'
            };
            ajaxAction(opt);
        } else {
            opt = {
                url: '/home/account/modifymobile',
                data: {
                    mobile: $('#mobilevalue').val(),
                    code: $('#inputcode').val()
                },
                hrefUrl: '/home/account/mobile?step=3&success=true'
            };
            ajaxAction(opt);
        }
    }
}

$(function() {

    if ($('.res-info').length > 0) {
        setTimeout(function() {
            toHome();
        }, 5000);
    }

    // changeCode();

    $('#pwdform').submit(function() {
        return false;
    });

    $checkInput.blur(function() {
        checkForm($(this));
    });

    $('.sub-btn').on('click', function() {
        if (canFlag === false) {
            return false;
        }

        if (checkAllForm()) {
            submitForm();
        } else {
            return false;
        }
    });

    $('input[name=verifyCode]').keydown(function(e) {
        if (e.keyCode === 13) {
            if (canFlag === false) {
                return false;
            }

            if (checkAllForm()) {
                submitForm();
            } else {
                return false;
            }
        }
    });

    $('.the-code').on('click', function() {
        changeCode();
    });

    $('#send-mobile-code').on('click', function() {
        captcha.check().then(function() {
            sendcode();
        });
    });

    if ($('#newPwd').length > 0) {
        $('#newPwd').on('input', function() {
            var $confirm = $('input[name=confirm_password]'),
                confirmV = $confirm.val(),
                newPwdV = $('#newPwd').val(),
                opt = {
                    dom: $confirm,
                    $checkInfo: $confirm.next()
                };

            if (confirmV.length > 0 && newPwdV !== confirmV) {
                errorInfoAction(opt, '两次密码不一致!');
            }
        });
    }
});