account-service.js 34.4 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
/**
 * 个人中心---账户安全
 * @author gaohongwei <hongwei.gao@yoho.cn>
 * @date: 2016/8/30
 */
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const helpers = global.yoho.helpers;
const logger = global.yoho.logger;
const _ = require('lodash');

const crypto = global.yoho.crypto;
const AccountApi = require('./account-api');
const CaptchaImgService = require('../../passport/models/captcha-img-service');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.captchaService = new CaptchaImgService(ctx);
    }

    // 时间转换为时间戳
    datetimeToUnix(datetime) {
        let tmpDatetime = datetime.replace(/:/g, '-');

        tmpDatetime = tmpDatetime.replace(/ /g, '-');
        let arr = tmpDatetime.split('-');

        let now = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4], arr[5]));

        return parseInt(now.getTime() / 1000, 10);
    }

    /**
     * 根据输入的mobile获取area
     * @param type $mobile
     * @return int
     */
    handleMobile(mobile) {
        let res = {};

        // 国际号
        if (mobile.indexOf('-') > 0) {
            let areaTmp = mobile.split('-');

            res.area = areaTmp[0];
            res.mobile = areaTmp[1];
        } else {
            res.area = 86;
            res.mobile = mobile;
        }
        return res;
    }

    /**
     * 获得标题文案
     * @param type ischeckMobile
     * @param type ischeckEmail
     * @param type checkType
     */
    getTitles(ischeckMobile, ischeckEmail, checkType) {
        let subTitle,
            enTitle,
            pageKey;

        if (checkType === 'mobile') {
            subTitle = ischeckMobile ? '修改手机' : '验证手机';
            enTitle = ischeckMobile ? 'CHANGE TELEPHONE' : 'VERIFICATION TELEPHONE';
            pageKey = 'mobile';
        } else if (checkType === 'userpwd') {
            subTitle = '修改密码';
            enTitle = 'CHANGE PASSWORD';
            pageKey = 'userpwd';
        } else {
            subTitle = ischeckEmail ? '修改邮箱' : '验证邮箱';
            enTitle = ischeckEmail ? 'CHANGE EMAIL' : 'VERIFICATION EMAIL';
            pageKey = 'email';
        }
        return {
            subTitle: subTitle,
            enTitle: enTitle,
            pageKey: pageKey
        };
    }


    /**
     * 第一部页面form结构-step1
     * @param type $data 用户验证相关信息
     * @param type $ischeckMobile
     * @param type $ischeckEmail
     * @param type $firstCheck
     * @return string
     */
    getFormDataStep1(data, ischeckMobile, ischeckEmail, firstCheck) {
        // 都没验证
        let formData1 = [
            {
                inputTxt: '请输入登录密码',
                key: 'password',
                type: 'password',
                name: 'password'
            }
        ];
        let formData2 = [
            {// 只验证手机号
                inputTxt: '已验证的手机号',
                isVerify: true,
                verifyAccount: data.mobile.slice(0, 3) + '****' + data.mobile.slice(7),
                realAccount: data.mobile
            }
        ];

        let formData3 = [
            {// 只验证邮箱
                inputTxt: '已验证邮箱',
                isVerify: true,
                verifyAccount: data.email.slice(0, 2) + '****' + data.email.slice(6),
                realAccount: data.email
            }
        ];

        let formData,
            verifyType,
            checkEmailFlag,
            checkMobileFlag;

        // 只验证手机号
        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 {
            formData: formData,
            verifyType: verifyType,
            checkEmailFlag: checkEmailFlag,
            checkMobileFlag: checkMobileFlag
        };
    }

    /**
     * 第二步-formData
     * @param type $ischeckEmail
     * @param type $ischeckMobile
     * @param type $checkType
     * @return array
     */
    getFormDataStep2(ischeckEmail, ischeckMobile, checkType) {
        let formData = [];

        switch (checkType) {
            case 'userpwd':
                formData = [
                    {
                        inputTxt: '输入新密码',
                        key: 'newPwd',
                        type: 'password',
                        name: 'newPwd'
                    },
                    {
                        inputTxt: '确认新密码',
                        key: 'confirm_password',
                        type: 'password',
                        name: 'confirm_password'
                    }
                ];
                break;
            case 'email':
                formData = [
                    {
                        inputTxt: ischeckEmail ? '新的邮箱' : '我的邮箱',
                        key: 'email',
                        type: 'text',
                        name: 'email'
                    }
                ];
                break;
            case 'mobile':
                formData = [
                    {
                        inputTxt: ischeckMobile ? '输入新的手机号码' : '请输入手机号码',
                        key: 'mobilevalue',
                        type: 'text',
                        name: 'mobile'
                    }
                ];
                break;
            default:
                formData = [];
                break;
        }

        return {formData: formData};
    }

    /**
     * 个人中心-判断身份验证状态
     * @param type uid
     * @param type checkType
     * @param type step page步骤
     * @return type
     */
    auditCheckStatus(uid, checkType, step) {
        let that = this;

        return co(function* () {
            let accountDataModel = new AccountApi(that.ctx);
            let res = yield accountDataModel.getVerifyInfo(uid),
                ret = {status: false};

            if (res.data) {
                let data = res.data,
                    ischeckMobile = (data.mobileVerify === 'N') ? false : true,
                    ischeckEmail = (data.emailVerify === 'N') ? false : true;

                let firstCheck = '', // 优先验证标识
                    titleInfo = that.getTitles(ischeckMobile, ischeckEmail, checkType),
                    formJson = {};

                if (ischeckMobile && ischeckEmail) {
                    firstCheck = (that.datetimeToUnix(data.mobileVerifyTime) <=
                        that.datetimeToUnix(data.emailVerifyTime)) ? 'mobile' : 'email';
                }

                let verifyType = 1,
                    checkEmailFlag = false,
                    checkMobileFlag = false;

                if (step === 1) {
                    formJson = that.getFormDataStep1(data, ischeckMobile, ischeckEmail, firstCheck);
                    verifyType = formJson.verifyType;
                    checkEmailFlag = formJson.checkEmailFlag;
                    checkMobileFlag = formJson.checkMobileFlag;
                    delete formJson.verifyType;
                    delete formJson.checkEmailFlag;
                    delete formJson.checkMobileFlag;
                } else if (step === 2) {
                    formJson = that.getFormDataStep2(ischeckEmail, ischeckMobile, checkType);
                }

                ret = {
                    status: true,
                    ischeckMobile: ischeckMobile,
                    ischeckEmail: ischeckEmail,
                    email: data.email,
                    mobile: data.mobile,
                    subTitle: titleInfo.subTitle,
                    enTitle: titleInfo.enTitle,
                    pageKey: titleInfo.pageKey,
                    formData: formJson.formData,
                    verifyType: verifyType,
                    checkEmailFlag: checkEmailFlag,
                    checkMobileFlag: checkMobileFlag
                };
            }

            return ret;
        })();
    }

    /**
     * 校验进入第二步
     * @param type $checkCode
     * @param type $uid
     * @param type MaxTime
     * @return boolean
     */
    checkCode(ckCode, uid, time) {
        time = parseInt(`0${time}`, 10) || 86400000;

        try {
            // checkCode里空格用+替换
            let code = decodeURIComponent(ckCode);
            let checkStr = crypto.decrypt('yoho9646abcdefgh', code);

            let checkInfo = checkStr.split('_'),
                checkUid = checkInfo[0],
                timeDiff = Date.parse(new Date()) - checkInfo[1]; // 时间差,秒 24h 86400000

            if (checkStr.indexOf('completeverify') > 0 && String(checkUid) === String(uid) && timeDiff <= time) {
                return true;
            } else {
                return false;
            }
        } catch (e) { // eslint-disable-line
            logger.error(`account checkCode decrypt error [checkCode]:${ckCode}`);

            return false;
        }
    }

    /**
     * 个人中心-账号安全-index
     *
     */
    getAccountInfo(uid) {
        let that = this;

        return co(function* () {

            let accountDataModel = new AccountApi(that.ctx);
            let resq = [{
                icon: 'ok',
                type: '登录密码',
                tip: '互联网帐号存在被盗风险,建议您定期更改密码以保护帐号安全。',
                red: 'true',
                url: helpers.urlFormat('/home/account/userpwd'),
                isValid: true
            }, {
                icon: 'warning',
                type: '邮箱验证',
                tip: '验证后,可用于找回登录密码。',
                url: helpers.urlFormat('/home/account/email')
            }, {
                icon: 'warning',
                type: '手机验证',
                tip: '验证后,可用于找回登录密码。',
                url: helpers.urlFormat('/home/account/mobile')
            }];

            let verifyResult = yield accountDataModel.getVerifyInfo(uid);

            if (verifyResult.data) {
                let verifyData = verifyResult.data;

                resq[1].icon = verifyData.emailVerify === 'N' ? 'warning' : 'ok';
                resq[1].tip = verifyData.emailVerify === 'N' ? '验证后,可用于找回登录密码。' :
                    '您验证的邮箱:' + verifyData.email.slice(0, 2) + '****' + verifyData.email.slice(6);
                resq[1].isValid = verifyData.emailVerify === 'N' ? false : true;
                resq[2].icon = verifyData.mobileVerify === 'N' ? 'warning' : 'ok';
                resq[2].isValid = verifyData.mobileVerify === 'N' ? false : true;
                resq[2].tip = verifyData.mobileVerify === 'N' ? '验证后,可用于找回登录密码。' :
                    '您验证的手机:' + verifyData.mobile.slice(0, 3) + '****' + verifyData.mobile.slice(7);
            }

            return resq;

        })();
    }

    /**
     * 个人中心-修改密码身份验证-page1/2/3
     */
    userPwd(params) {
        let that = this;

        return co(function* () {
            let step = params.step ? parseInt(params.step, 10) : 1,
                ckCode = params.checkCode || '',
                success = params.success || false,
                progress = 'progress' + step,
                uid = params.uid;

            // 第二步验证信息校验
            if (step === 2 && ckCode !== '') {
                let checkFlag = that.checkCode(ckCode, uid);

                if (!checkFlag) {
                    // res.redirect(helpers.urlFormat('/home/account/userpwd', {step: 1}));
                    return {
                        code: 400,
                        url: '/home/account/userpwd',
                        params: {step: 1}
                    };
                }
            }

            // 验证信息
            let verifyInfo = yield that.auditCheckStatus(uid, 'userpwd', step);

            if (!verifyInfo.status) {
                return {
                    meValidatePage: true
                };
            }

            let data = {
                subTitle: verifyInfo.subTitle,
                enTitle: verifyInfo.enTitle,
                verifyType: verifyInfo.verifyType, // verifyType 1:登录密码验证 2:邮箱验证 3:手机验证
                progressCur: progress,
                progress: [
                    {
                        progressName: '1.验证身份'
                    },
                    {
                        progressName: '2.修改登录密码'
                    },
                    {
                        progressName: '3.完成'
                    }
                ]
            };


            // form变化1-验证身份 2-修改 3-成功
            if (step === 1) {
                data.progress[0].iscur = true;
                data.returnInfo = false;
                data.formInfo = {
                    ischeckEmail: verifyInfo.checkEmailFlag,
                    formData: verifyInfo.formData,
                    mobileCode: verifyInfo.checkMobileFlag
                };
            } else if (step === 2) {
                data.progress[1].iscur = true;
                data.returnInfo = false;
                data.formInfo = {
                    formData: verifyInfo.formData
                };
            } else if (step === 3) {
                data.progress[2].iscur = true;
                data.returnInfo = true;
                data.resClass = success ? 'res-success' : 'res-error';
                data.complete = {
                    resInfo: '恭喜你,您已经成功修改了登录密码!'
                };
            }

            return {
                userpwd: data,
                meValidatePage: true
            };
        })();
    }

    /**
     * 个人中心-邮箱验证身份-page1/2/3
     */
    userEmail(params) {
        let that = this;

        return co(function* () {

            let step = params.step ? parseInt(params.step, 10) : 1,
                ckCode = params.checkCode || '',
                success = params.success || false,
                progress = 'progress' + step,
                uid = params.uid;

            // 第二步验证信息校验
            if (step === 2) {
                let checkFlag = that.checkCode(ckCode, uid);

                if (!checkFlag) {
                    // res.redirect(helpers.urlFormat('/home/account/email', {step: 1}));
                    return {
                        code: 400,
                        url: '/home/account/email',
                        params: {step: 1}
                    };
                }
            }

            // 验证信息
            let verifyInfo = yield that.auditCheckStatus(uid, 'email', step);

            if (!verifyInfo.status) {
                return {
                    meValidatePage: true
                };
            }
            let data = {
                subTitle: verifyInfo.subTitle,
                enTitle: verifyInfo.enTitle,
                verifyType: verifyInfo.verifyType, // verifyType 1:登录密码验证 2:邮箱验证 3:手机验证
                progressCur: progress,
                progress: [
                    {
                        progressName: '1.验证身份'
                    },
                    {
                        progressName: '2.' + verifyInfo.subTitle
                    },
                    {
                        progressName: '3.完成'
                    }
                ]
            };

            // form变化1-验证身份 2-修改 3-成功
            if (step === 1) {
                data.progress[0].iscur = true;
                data.returnInfo = false;
                data.formInfo = {
                    ischeckEmail: verifyInfo.checkEmailFlag,
                    formData: verifyInfo.formData,
                    mobileCode: verifyInfo.checkMobileFlag
                };
            } else if (step === 2) {
                data.progress[1].iscur = true;
                data.returnInfo = false;
                data.formInfo = {
                    formData: verifyInfo.formData,
                    mobileCode: false
                };
            } else if (step === 3) {
                data.progress[2].iscur = true;
                data.returnInfo = true;
                data.resClass = success === 'true' ? 'res-success' : 'res-error';
                data.complete = {
                    resInfo: success === 'true' ? '恭喜您,您已经成功' + verifyInfo.subTitle +
                        '!' : '抱歉,' + verifyInfo.subTitle + '失败'
                };
            }

            return {
                email: data,
                meValidatePage: true
            };
        })();
    }

    /**
     * 个人中心-手机验证身份-page1/2/3
     */
    userMobile(params) {
        let that = this;

        return co(function* () {
            let step = params.step ? parseInt(params.step, 10) : 1,
                ckCode = params.checkCode || '',
                success = params.success || false,
                progress = 'progress' + step,
                uid = params.uid;

            // 第二步验证信息校验
            if (step === 2) {
                let checkFlag = that.checkCode(ckCode, uid);

                if (!checkFlag) {
                    // res.redirect(helpers.urlFormat('/home/account/mobile', {step: 1}));
                    return {
                        code: 400,
                        url: '/home/account/mobile',
                        params: {step: 1}
                    };
                }
            }

            // 验证信息
            let verifyInfo = yield that.auditCheckStatus(uid, 'mobile', step);

            if (!verifyInfo.status) {
                return {
                    meValidatePage: true
                };
            }
            let data = {
                subTitle: verifyInfo.subTitle,
                enTitle: verifyInfo.enTitle,
                verifyType: verifyInfo.verifyType, // verifyType 1:登录密码验证 2:邮箱验证 3:手机验证
                progressCur: progress,
                progress: [
                    {
                        progressName: '1.验证身份'
                    },
                    {
                        progressName: '2.' + verifyInfo.subTitle
                    },
                    {
                        progressName: '3.完成'
                    }
                ]
            };


            // form变化1-验证身份 2-修改 3-成功
            if (step === 1) {
                data.progress[0].iscur = true;
                data.returnInfo = false;
                data.formInfo = {
                    ischeckEmail: verifyInfo.checkEmailFlag,
                    formData: verifyInfo.formData,
                    mobileCode: verifyInfo.checkMobileFlag
                };
            } else if (step === 2) {
                data.progress[1].iscur = true;
                data.returnInfo = false;
                data.formInfo = {
                    formData: verifyInfo.formData,
                    mobileCode: true
                };
            } else if (step === 3) {
                data.progress[2].iscur = true;
                data.returnInfo = true;
                data.resClass = success ? 'res-success' : 'res-error';
                data.complete = {
                    resInfo: '恭喜你,您已成功' + verifyInfo.subTitle +
                    '!今后您可以使用该手机号+密码进行登录'
                };
            }

            return {
                mobile: data,
                meValidatePage: true
            };
        })();
    }

    /**
     * 个人中心-邮箱验证身份-邮件发送成功过渡页
     */
    sendEmailSuccess(params) {
        let that = this;

        return co(function* () {
            let checkType = params.checkType || 'userpwd',
                uid = params.uid,
                email = params.email || '',
                emailDomain = '',
                type = params.email || 1;// 1:身份验证 2:修改邮箱

            // 验证信息
            let verifyInfo = yield that.auditCheckStatus(uid, checkType);

            if (!verifyInfo.status) {
                return {
                    meValidatePage: true
                };
            }
            emailDomain = 'http://' + ((email.split[1] === 'gmail.com') ?
                'mail.google.com' : 'mail.' + email.split('@')[1]);
            let data = {
                subTitle: verifyInfo.subTitle,
                enTitle: verifyInfo.enTitle,
                progressCur: (type === 1) ? 'progress1' : 'progress2',
                progress: [
                    {
                        progressName: '1.验证身份'
                    },
                    {
                        progressName: '2.' + verifyInfo.subTitle
                    },
                    {
                        progressName: '3.完成'
                    }
                ],
                returnInfo: true,
                sendEmail: {
                    emailInfo: email.slice(0, 2) + '****' + email.slice(6),
                    emailUrl: emailDomain
                }
            };

            if (type === 1) {
                data.progress[0].iscur = true;
            } else {
                data.progress[1].iscur = true;
            }

            let resqData = {meValidatePage: true};

            resqData.email = data;
            return resqData;

        })();
    }

    /**
     * 点击邮箱验证链接方法--修改验证邮箱
     */
    mailResult(params) {
        let that = this;

        return co(function* () {
            let code = params.code;
            let accountDataModel = new AccountApi(that.ctx);
            let check = yield accountDataModel.checkEmailCode(code);

            if (check.code === 200) {
                let data = yield accountDataModel.modifyVerifyEmail(code);

                if (data.code === 200) {
                    // res.redirect(helpers.urlFormat('/home/account/email',
                    //        {step: 3, success: true}));
                    return {
                        code: 400,
                        url: '/home/account/email',
                        params: {step: 3, success: true}
                    };
                }
            }

            // res.redirect(helpers.urlFormat('/home/account/email',
            //        {step: 3, success: false}));
            return {
                code: 400,
                url: '/home/account/email',
                params: {step: 3, success: false}
            };
        })();
    }

    /**
     * 身份验证-登录密码验证Ajax
     */
    verifyPassword(req) {
        let that = this;

        return co(function* () {
            let password = _.trim(req.body.password || ''),
                uid = req.user.uid,
                accountDataModel = new AccountApi(that.ctx);

            let resqData = yield accountDataModel.verifyPwd(uid, password);

            if (resqData.code === 200) {
                let ckCode = crypto.encryption('yoho9646abcdefgh', uid + '_' + Date.parse(new Date()) +
                    '_' + password + 'completeverify');

                resqData.data = encodeURIComponent(ckCode);
            }
            return resqData;
        })();
    }

    /**
     * 分-验证密码正确性-ajax
     */
    checkPassword(req) {
        let that = this;

        return co(function* () {
            let password = _.trim(req.body.password || ''),
                uid = req.user.uid,
                resqData = {code: 400},
                accountDataModel = new AccountApi(that.ctx);

            resqData = yield accountDataModel.verifyPwd(uid, password);

            return resqData;
        })();
    }

    /**
     * 分-验证图形验证码-ajax
     */
    checkVerifyCode(req) {
        let captchaCode = _.trim(req.body.verifyCode || '').toLowerCase(),
            resqData = {};

        if (captchaCode && captchaCode !== req.session.captcha) {
            resqData.code = 400;
            resqData.message = '图形验证码不正确';
        } else {
            resqData.code = 200;
            resqData.message = '';
        }

        return Promise.resolve(resqData);
    }

    /**
     * 手机身份验证-校验手机号
     */
    identityMobile(req) {
        let that = this;

        return co(function* () {
            let mobile = req.body.mobile || '',
                resqData = {code: 400},
                uid = req.user.uid,
                check = false,
                userId;

            let mobileInfo = that.handleMobile(mobile),
                accountDataModel = new AccountApi(that.ctx);

            let userInfo = yield accountDataModel.getUserInfoByMobile(mobileInfo.area, mobile);

            userId = 'uid' in userInfo.data ? userInfo.data.uid : 0;
            if (userId === uid) {
                check = true;
            }

            if (check) {
                resqData = {
                    code: 200,
                    message: '',
                    data: ''
                };
            } else {
                resqData = {
                    code: 400,
                    message: '手机号错误',
                    data: ''
                };
            }
            return resqData;
        })();
    }

    /**
     * 向验证手机号发送短信-ajax
     */
    async sendMobileMsg(req, uid) {
        let mobile = req.body.mobile || '',
            _code = req.body.checkCode,
            resqData = {code: 400};

        let accountDataModel = new AccountApi(this.ctx);
        let id = req.session.id;
        let captcha = req.body.verifyCode;

        // 发送验证码前置数据校验
        if (!_code) {
            let verifyInfo = await accountDataModel.getVerifyInfo(uid);

            mobile = _.get(verifyInfo, 'data.mobile', '');
            if (!mobile) {
                return Object.assign(resqData, {
                    message: '数据验证错误'
                });
            }
        } else if (!this.checkCode(_code, uid)) {
            return Object.assign(resqData, {
                message: '数据验证错误'
            });
        }

        let mobileInfo = this.handleMobile(mobile);

        resqData = await accountDataModel.sendMobileMsg(uid, mobileInfo.mobile, mobileInfo.area, id, captcha);

        let captchaNeeded = await this.captchaService.try();

        _.set(resqData, 'data.needCaptcha', captchaNeeded);

        return resqData;
    }

    /**
     * 校验短信验证码-ajax
     */
    checkMobileMsg(req, uid) {
        let that = this;

        return co(function* () {
            let mobile = req.body.mobile || '',
                code = req.body.code || '',
                _code = req.body.checkCode,
                resqData = {code: 400};

            let accountDataModel = new AccountApi(that.ctx);

            // 校验验证码前置数据校验
            if (!_code) {
                let verifyInfo = yield accountDataModel.getVerifyInfo(uid);

                mobile = _.get(verifyInfo, 'data.mobile', '');
                if (!mobile) {
                    return Object.assign(resqData, {
                        message: '数据验证错误'
                    });
                }
            } else if (!that.checkCode(_code, uid)) {
                return Object.assign(resqData, {
                    message: '数据验证错误'
                });
            }

            if (mobile === '') {
                Object.assign(resqData, {
                    message: '手机号为空',
                    data: ''
                });
                return resqData;
            }
            if (code === '') {
                Object.assign(resqData, {
                    message: '验证码为空',
                    data: ''
                });
                return resqData;
            }
            let mobileInfo = that.handleMobile(mobile);

            resqData = yield accountDataModel.checkVerifyMsg(mobileInfo.area, mobileInfo.mobile, code);
            if (resqData.code === 200) {
                let ckCode = crypto.encryption('yoho9646abcdefgh', uid + '_' + Date.parse(new Date()) + '_' +
                    mobileInfo.mobile + mobileInfo.area + 'completeverify');


                Object.assign(resqData, {
                    code: 200,
                    data: encodeURIComponent(ckCode)
                });
            }
            return resqData;
        })();
    }

    /**
     * 身份验证时,发送邮件-ajax
     */
    sendEmail(req) {
        let that = this;

        return co(function* () {
            let uid = req.user.uid,
                checkType = req.body.checkType || 'userpwd',
                email = req.body.email || '',
                resqData = {code: 400};

            let id = req.session.id;
            let captcha = req.body.verifyCode;

            let accountDataModel = new AccountApi(that.ctx),
                verifyInfo = yield accountDataModel.getVerifyInfo(uid);

            email = _.get(verifyInfo, 'data.email', '');
            if (!email) {
                return Object.assign(resqData, {
                    message: '数据验证错误'
                });
            }

            let ckCode = crypto.encryption('yoho9646abcdefgh', uid + '_' + Date.parse(new Date()) +
                '_' + email + checkType + 'completeverify');

            let callback = 'home/account/' + checkType + '?step=2&checkCode=' +
                encodeURIComponent(ckCode); // callback拼接于邮箱域名处;

            resqData = yield accountDataModel.sendVerifyEmailForNext(email, callback, id, captcha);
            return resqData;
        })();
    }

    /**
     * 分-修改邮箱前,校验邮箱-ajax
     */
    checkEmail(req) {
        let that = this;

        return co(function* () {
            let uid = req.user.uid,
                email = req.body.email || '',
                resqData = {code: 400};

            let accountDataModel = new AccountApi(that.ctx);

            resqData = yield accountDataModel.checkVerifyEmail(uid, email);
            return resqData;
        })();
    }

    /**
     * 修改密码
     */
    modifyPwd(req, params) {
        let that = this;

        return co(function* () {
            let uid = params.uid,
                newPwd = params.newPwd || '',
                _code = params.checkCode;

            let accountDataModel = new AccountApi(that.ctx);

            // 前置数据校验
            if (!_code || !that.checkCode(_code, uid, 600000)) {
                return {
                    code: 400,
                    message: '数据验证错误'
                };
            }

            let resqData = yield accountDataModel.modifyPwd(uid, newPwd);

            return resqData;
        })();
    }

    /**
     * 修改验证手机号
     */
    modifyMobile(req, uid) {
        let that = this;

        return co(function* () {
            let mobile = req.body.mobile || '',
                code = req.body.code || '',
                _code = req.body.checkCode,
                resqData = {code: 400};

            let accountDataModel = new AccountApi(that.ctx);

            // 校验验证码前置数据校验
            // 校验checkCode,有效时间10分钟(checkCode在调改接口前获取,考虑网络延时,服务器间的时间差,设置10分钟)
            if (!_code || !that.checkCode(_code, uid, 600000)) {
                return Object.assign(resqData, {
                    message: '数据验证错误'
                });
            }

            if (mobile === '') {
                resqData = {
                    code: 400,
                    message: '手机号为空',
                    data: ''
                };
                return resqData;
            }
            if (code === '') {
                resqData = {
                    code: 400,
                    message: '验证码为空',
                    data: ''
                };
                return resqData;
            }
            let mobileInfo = that.handleMobile(mobile);

            let checkFlag = yield accountDataModel.checkVerifyMobile(uid, mobileInfo.mobile, mobileInfo.area);

            if (checkFlag.code === 200) {
                resqData = yield accountDataModel.modifyVerifyMobile(uid, mobileInfo.area, mobileInfo.mobile);
            } else {
                resqData = {
                    code: checkFlag.data,
                    message: checkFlag.message,
                    data: ''
                };
            }
            return resqData;
        })();
    }

    /**
     * 分-检查手机号是否可修改-ajax
     */
    checkMobile(params, uid) {
        let that = this;

        return co(function* () {
            let mobile = params.mobile || '',
                resqData = {code: 400};

            let mobileInfo = that.handleMobile(mobile),
                accountDataModel = new AccountApi(that.ctx);

            resqData = yield accountDataModel.checkVerifyMobile(uid, mobileInfo.mobile, mobileInfo.area);
            return resqData;
        })();
    }

    /**
     * 修改验证邮箱校验并发送邮件-ajax
     *
     */
    modifyEmail(req) {
        let that = this;

        return co(function* () {
            let uid = req.user.uid,
                email = req.body.email || '',
                _code = req.body.checkCode,
                resqData = {code: 400};

            let accountDataModel = new AccountApi(that.ctx);

            // 前置数据校验
            if (!_code || !that.checkCode(_code, uid, 600000)) {
                return Object.assign(resqData, {
                    message: '数据验证错误'
                });
            }

            let check = yield accountDataModel.checkVerifyEmail(uid, email);

            if (check.code === 200) {
                resqData = yield accountDataModel.sendVerifyEmail(uid, email);
            }
            return resqData;
        })();
    }

};