Authored by wenjiekong

‘账号安全接口都写了

@@ -15,6 +15,138 @@ const getVerifyInfo = uid => { @@ -15,6 +15,138 @@ const getVerifyInfo = uid => {
15 15
16 }; 16 };
17 17
  18 +const checkEmailCode = code => {
  19 + return api.get('', {
  20 + method: 'web.passport.checkCodeValid',
  21 + code: code
  22 + });
  23 +
  24 +};
  25 +
  26 +const modifyVerifyEmail = code => {
  27 + return api.get('', {
  28 + method: 'web.passport.changeVerifyEmail',
  29 + code: code
  30 + });
  31 +
  32 +};
  33 +
  34 +const verifyPwd = (uid, password) => {
  35 + return api.get('', {
  36 + method: 'web.passport.verifyUserPwd',
  37 + uid: uid,
  38 + password: password
  39 + });
  40 +
  41 +};
  42 +
  43 +const checkVerifyMsg = (area, mobile, code) => {
  44 + return api.get('', {
  45 + method: 'web.passport.checkcode',
  46 + area: area,
  47 + mobile: mobile,
  48 + code: code
  49 + });
  50 +
  51 +};
  52 +/**
  53 + * 邮箱身份验证--发送邮件
  54 + * @param type $email
  55 + * @param type $callback 成功后跳转链接
  56 + * @return type
  57 + */
  58 +const sendVerifyEmailForNext = (email, callback) => {
  59 + return api.get('', {
  60 + method: 'web.passport.sendVerifyEmailInfo',
  61 + email: email,
  62 + callback: callback
  63 + });
  64 +
  65 +};
  66 +/**
  67 + * 修改验证手机号
  68 + * @param type $uid
  69 + * @param type $area
  70 + * @param type $newMobile
  71 + * @return type
  72 + */
  73 +const modifyVerifyMobile = (uid, area, newMobile) => {
  74 + return api.get('', {
  75 + method: 'web.passport.modifyVerifyMobile',
  76 + uid: uid,
  77 + area: area,
  78 + newMobile: newMobile
  79 + });
  80 +
  81 +};
  82 +/**
  83 + * 修改邮箱前校验
  84 + * @param type $uid
  85 + * @param type $email
  86 + */
  87 +const checkVerifyEmail = (uid, email) => {
  88 + return api.get('', {
  89 + method: 'web.passport.checkVerifyEmail',
  90 + uid: uid,
  91 + email: email
  92 + });
  93 +
  94 +};
  95 +/**
  96 + * 验证邮箱--发送邮件
  97 + * @param type $uid
  98 + * @param type $email
  99 + * @return type
  100 + */
  101 + const sendVerifyEmail = (uid, email) => {
  102 + return api.get('', {
  103 + method: 'web.passport.verifyEmail',
  104 + uid: uid,
  105 + email: email
  106 + });
  107 +
  108 +};
  109 +
  110 + /**
  111 + * 修改手机号前校验
  112 + * @param type $mobile
  113 + * @param type $area
  114 + * @return type
  115 + */
  116 +const checkVerifyMobile = (uid, mobile, area) => {
  117 + return api.get('', {
  118 + method: 'web.passport.checkVerifyMobile',
  119 + uid: uid,
  120 + mobile: mobile,
  121 + area: area
  122 + });
  123 +
  124 +};
  125 +
  126 +/**
  127 + * 修改密码
  128 + * @param type $uid
  129 + * @param type $newPwd
  130 + * @return type
  131 + */
  132 +const modifyPwd = (uid, newPwd) => {
  133 + return api.get('', {
  134 + method: 'web.passport.changePwd',
  135 + uid: uid,
  136 + newPwd: newPwd
  137 + });
  138 +
  139 +};
18 module.exports = { 140 module.exports = {
19 - getVerifyInfo 141 + getVerifyInfo,
  142 + checkEmailCode,
  143 + modifyVerifyEmail,
  144 + verifyPwd,
  145 + checkVerifyMsg,
  146 + sendVerifyEmailForNext,
  147 + checkVerifyEmail,
  148 + checkVerifyMobile,
  149 + sendVerifyEmail,
  150 + modifyVerifyMobile,
  151 + modifyPwd
20 }; 152 };
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 const _ = require('lodash'); 10 const _ = require('lodash');
11 const crypto = global.yoho.crypto; 11 const crypto = global.yoho.crypto;
12 const accountApi = require('./account-api'); 12 const accountApi = require('./account-api');
  13 + const userApi = require('./user-api');
13 const headerModel = require('../../../doraemon/models/header'); 14 const headerModel = require('../../../doraemon/models/header');
14 15
15 // 时间转换为时间戳 16 // 时间转换为时间戳
@@ -23,6 +24,26 @@ @@ -23,6 +24,26 @@
23 24
24 return parseInt(now.getTime() / 1000, 10); 25 return parseInt(now.getTime() / 1000, 10);
25 } 26 }
  27 + /**
  28 + * 根据输入的mobile获取area
  29 + * @param type $mobile
  30 + * @return int
  31 + */
  32 + function handleMobile(mobile) {
  33 + let res = {};
  34 +
  35 + //国际号
  36 + if (mobile.indexOf("-") > 0) {
  37 + let areaTmp = mobile.split("-");
  38 +
  39 + res.area = areaTmp[0];
  40 + res.mobile = areaTmp[1];
  41 + }else {
  42 + res.area = 86;
  43 + res.mobile = mobile;
  44 + }
  45 + return res;
  46 + }
26 47
27 /** 48 /**
28 * 获得标题文案 49 * 获得标题文案
@@ -73,7 +94,7 @@ @@ -73,7 +94,7 @@
73 }], formData2 = [{// 只验证手机号 94 }], formData2 = [{// 只验证手机号
74 inputTxt: '已验证的手机号', 95 inputTxt: '已验证的手机号',
75 isVerify: true, 96 isVerify: true,
76 - verifyAccount: data.mobile.slice(0, 2) + '****' + data.email.slice(6), 97 + verifyAccount: data.mobile.slice(0, 3) + '****' + data.email.slice(7),
77 realAccount: data.mobile 98 realAccount: data.mobile
78 }], formData3 = [{// 只验证邮箱 99 }], formData3 = [{// 只验证邮箱
79 inputTxt: '已验证邮箱', 100 inputTxt: '已验证邮箱',
@@ -559,37 +580,374 @@ @@ -559,37 +580,374 @@
559 /** 580 /**
560 * 个人中心-邮箱验证身份-邮件发送成功过渡页 581 * 个人中心-邮箱验证身份-邮件发送成功过渡页
561 */ 582 */
562 - // const sendEmailSuccess = (req, res, channel) => { 583 + const sendEmailSuccess = (req, res, channel) => {
  584 + return co(function*() {
  585 + let checkType = req.body.checkType || 'userpwd',
  586 + email = req.body.email || '',
  587 + emailDomain = '',
  588 + type = req.body.email || 1;//1:身份验证 2:修改邮箱
  589 +
  590 + let headerData = yield headerModel.requestHeaderData(channel);
  591 + // 验证信息
  592 + let verifyInfo = yield auditCheckStatus(uid, 'mobile', step);
  593 +
  594 + if (!verifyInfo.status) {
  595 + return {
  596 + headerData: headerData,
  597 + meValidatePage: true
  598 + };
  599 + }
  600 + emailDomain = 'http://' + ((email.split[1] === 'gmail.com') ?
  601 + 'mail.google.com' : 'mail.' + email.split[1]);
  602 + let data = {
  603 + subTitle: verifyInfo.subTitle,
  604 + enTitle: verifyInfo.enTitle,
  605 + progressCur: (type === 1) ? 'progress1' : 'progress2',
  606 + progress: [
  607 + {
  608 + progressName: '1.验证身份'
  609 + },
  610 + {
  611 + progressName: '2.' + verifyInfo.subTitle
  612 + },
  613 + {
  614 + progressName: '3.完成'
  615 + }
  616 + ],
  617 + returnInfo: true,
  618 + sendEmail: {
  619 + emailInfo: email.slice(0, 2) + '****' + email.slice(6),
  620 + emailUrl: emailDomain
  621 + }
  622 + };
  623 +
  624 + if (type === 1) {
  625 + data.progress[0].iscur = true;
  626 + }
  627 + else {
  628 + data.progress[1].iscur = true;
  629 + }
563 630
564 - // }; 631 + let resqData = {meValidatePage: true};
565 632
  633 + resqData[verifyInfo.pageKey] = data;
  634 + return resqData;
566 635
  636 + })();
  637 + };
  638 +/**
  639 + * 点击邮箱验证链接方法--修改验证邮箱
  640 + */
  641 + const mailResult = (req, res) => {
  642 + return co(function*() {
  643 + let code = req.body.code;
  644 +
  645 + let check = yield accountApi.checkEmailCode(code);
  646 +
  647 + if (check.code === 200) {
  648 + let data = yield accountApi.modifyVerifyEmail(code);
  649 +
  650 + if (data.code === 200) {
  651 + res.redirect(helpers.urlFormat('/home/account/email',
  652 + {step: 3,success: true}));
  653 + }
  654 + }
  655 +
  656 + res.redirect(helpers.urlFormat('/home/account/email',
  657 + {step: 3,success: false}));
  658 +
  659 + })();
  660 +};
  661 +/**
  662 + * 身份验证-登录密码验证Ajax
  663 + */
  664 +const verifyPassword = (req) => {
  665 + return co(function*() {
  666 + let password = _.trim(req.body.password || ''),
  667 + uid = req.uid,
  668 + captchaCode = _.trim(req.body.verifyCode || '').toLowerCase();
  669 +
  670 + if (captchaCode && captchaCode !== req.session.captcha) {
  671 + return {
  672 + code: 400,
  673 + message:'图形验证码不正确'
  674 + }
  675 + }
  676 + let data = yield accountApi.verifyPwd(uid, password);
  677 +
  678 + if (data.code == 200) {
  679 + let checkCode = crypto.encrypt('yoho9646abcdefgh', uid + '_' + Date.parse(new Date()) +
  680 + '_' + password + captchaCode + 'completeverify');
  681 +
  682 + data.data = checkCode;
  683 + }
  684 + return data;
  685 + })();
  686 +};
  687 +/**
  688 + * 分-验证密码正确性-ajax
  689 + */
  690 + const checkPassword = (req) => {
  691 + return co(function*() {
  692 + let password = _.trim(req.body.password || ''),
  693 + uid = req.uid,
  694 + resqData = {code: 400};
  695 +
  696 + resqData = yield accountApi.verifyPwd(uid, password);
  697 +
  698 + return resqData;
  699 + })();
  700 + };
567 /** 701 /**
568 * 分-验证图形验证码-ajax 702 * 分-验证图形验证码-ajax
569 */ 703 */
570 const checkVerifyCode = (req) => { 704 const checkVerifyCode = (req) => {
571 - let code = _.trim(req.body.verifyCode || ''), 705 + let captchaCode = _.trim(req.body.verifyCode || '').toLowerCase(),
572 resqData = {}; 706 resqData = {};
573 707
574 - // captchaCode = code.toLowerCase(),  
575 -  
576 - // if (!PassportModel::verifyCode($captchaCode)) {  
577 - // resqData.code = 400;  
578 - // resqData.message = '图形验证码不正确';  
579 - // }  
580 - // else { 708 + if (captchaCode && captchaCode !== req.session.captcha) {
  709 + resqData.code = 400;
  710 + resqData.message = '图形验证码不正确';
  711 + }else {
581 resqData.code = code; 712 resqData.code = code;
582 resqData.message = ''; 713 resqData.message = '';
  714 + }
  715 +
  716 + return resqData;
  717 + };
  718 + /**
  719 + * 手机身份验证-校验手机号
  720 + */
  721 + const identityMobile = (req) => {
  722 + return co(function*() {
  723 + let mobile = req.body.mobile || '',
  724 + resqData = {code: 400},
  725 + uid = req.uid,
  726 + check = false,
  727 + userId;
  728 +
  729 + let mobileInfo = handleMobile(mobile);
  730 +
  731 + let userInfo = yield userApi.getUserInfoByMobile(area, mobile);
583 732
584 - // } 733 + userId = 'uid' in userInfo.data ? userInfo.data.uid : 0;
  734 + if (userId === uid) {
  735 + check = true;
  736 + }
  737 +
  738 + if (check) {
  739 + resqData = {
  740 + code: 200,
  741 + message: '',
  742 + data: ''
  743 + }
  744 + }else {
  745 + resqData = {
  746 + code: 400,
  747 + message: '手机号错误',
  748 + data: ''
  749 + }
  750 + }
  751 + return resqData;
  752 + })();
  753 + };
  754 +/**
  755 + * 向验证手机号发送短信-ajax
  756 + */
  757 + const sendMobileMsg = (req) => {
  758 + return co(function*() {
  759 + let mobile = req.body.mobile || '',
  760 + resqData = {code: 400},
  761 + uid = req.uid;
  762 +
  763 + let mobileInfo = handleMobile(mobile);
  764 +
  765 + resqData = yield accountApi.sendMobileMsg(uid, mobileInfo.mobile, mobileInfo.area);
585 766
586 return resqData; 767 return resqData;
  768 + })();
587 }; 769 };
  770 +/**
  771 + * 校验短信验证码-ajax
  772 + */
  773 +const checkMobileMsg = (req) => {
  774 + return co(function*() {
  775 + let mobile = req.body.mobile || '',
  776 + code = req.body.code || '',
  777 + uid = req.uid,
  778 + resqData;
  779 +
  780 + if (mobile === '') {
  781 + resqData = {
  782 + code: 400,
  783 + message: '手机号为空',
  784 + data: ''
  785 + }
  786 + return resqData;
  787 + }
  788 + if (code === '') {
  789 + resqData = {
  790 + code: 400,
  791 + message: '验证码为空',
  792 + data: ''
  793 + }
  794 + return resqData;
  795 + }
  796 + let mobileInfo = handleMobile(mobile);
  797 +
  798 + resqData = yield accountApi.checkVerifyMsg(uid, mobileInfo.mobile, mobileInfo.area);
  799 +
  800 + if (resqData.code === 200) {
  801 + let checkCode = crypto.encrypt('yoho9646abcdefgh', uid + '_' + Date.parse(new Date()) + '_' +
  802 + mobileInfo.mobile + mobileInfo.area + 'completeverify');
  803 +
  804 + resqData.data = checkCode;
  805 + }
  806 + return resqData;
  807 + })();
  808 +};
  809 +/**
  810 + * 身份验证时,发送邮件-ajax
  811 + */
  812 +const sendEmail = (req) => {
  813 + return co(function*() {
  814 + let uid = req.body.uid,
  815 + checkType = req.body.checkType || 'userpwd',
  816 + email = req.body.email || '',
  817 + resqData = {code: 400};
  818 +
  819 + let checkCode = crypto.encrypt('yoho9646abcdefgh', uid + '_' + Date.parse(new Date()) +
  820 + '_' + email + checkType + 'completeverify'),
  821 + callback = 'home/account/' + checkType + '?step=2&checkCode=' + checkCode; //callback拼接于邮箱域名处;
  822 +
  823 + resqData = yield accountApi.sendVerifyEmailForNext(email, callback);
  824 + return resqData;
  825 + })();
  826 +};
  827 +/**
  828 + * 分-修改邮箱前,校验邮箱-ajax
  829 + */
  830 +const checkEmail = (req) => {
  831 + return co(function*() {
  832 + let uid = req.body.uid,
  833 + email = req.body.email || '',
  834 + resqData = {code: 400};
588 835
  836 + resqData = yield accountApi.checkVerifyEmail(uid, email);
  837 + return resqData;
  838 + })();
  839 +};
  840 +/**
  841 + * 修改密码
  842 + */
  843 + const modifyPwd = (req) => {
  844 + return co(function*() {
  845 + let uid = req.body.uid,
  846 + newPwd = req.body.newPwd || '',
  847 + captchaCode = _.trim(req.body.verifyCode || '').toLowerCase();
  848 + resqData = {code: 400};
  849 +
  850 + if (captchaCode && captchaCode !== req.session.captcha) {
  851 + resqData.code = 400;
  852 + resqData.message = '图形验证码不正确';
  853 + }else {
  854 + resqData = yield accountApi.modifyPwd(uid, newPwd);
  855 + }
  856 + return resqData;
  857 + })();
  858 +};
  859 +/**
  860 + * 修改验证手机号
  861 + */
  862 + const modifyMobile = (req) => {
  863 + return co(function*() {
  864 + let mobile = req.body.mobile || '',
  865 + code = req.body.code || '',
  866 + uid = req.uid,
  867 + resqData = {code: 400};
  868 +
  869 + if (mobile === '') {
  870 + resqData = {
  871 + code: 400,
  872 + message: '手机号为空',
  873 + data: ''
  874 + }
  875 + return resqData;
  876 + }
  877 + if (code === '') {
  878 + resqData = {
  879 + code: 400,
  880 + message: '验证码为空',
  881 + data: ''
  882 + }
  883 + return resqData;
  884 + }
  885 + let mobileInfo = handleMobile(mobile);
  886 +
  887 + let checkFlag = accountApi.checkVerifyMobile(uid, mobileInfo.mobile, mobileInfo.area);
  888 +
  889 + if (checkFlag.code === 200) {
  890 + resqData = accountApi.modifyVerifyMobile(uid, mobileInfo.area, mobileInfo.mobile);
  891 + }else {
  892 + resqData = {
  893 + code: checkFlag.data,
  894 + message: checkFlag.message,
  895 + data: ''
  896 + }
  897 + }
  898 + return resqData;
  899 + })();
  900 +};
  901 +/**
  902 + * 分-检查手机号是否可修改-ajax
  903 + */
  904 +const checkMobile = (req) => {
  905 + return co(function*() {
  906 + let uid = req.body.uid,
  907 + mobile = req.body.mobile || '',
  908 + resqData = {code: 400};
  909 +
  910 + let mobileInfo = handleMobile(mobile);
  911 +
  912 + resqData = yield accountApi.checkVerifyMobile(uid, mobileInfo.mobile, mobileInfo.area);
  913 + return resqData;
  914 + })();
  915 +};
  916 +/**
  917 + * 修改验证邮箱校验并发送邮件-ajax
  918 + *
  919 + */
  920 + const modifyEmail = (req) => {
  921 + return co(function*() {
  922 + let uid = req.body.uid,
  923 + email = req.body.email || '',
  924 + resqData = {code: 400};
  925 +
  926 + let check = yield accountApi.checkVerifyEmail(uid, email);
  927 +
  928 + if (check.code === 200) {
  929 + resqData = accountApi.sendVerifyEmail(uid, email);
  930 + }
  931 + return resqData;
  932 + })();
  933 +};
589 module.exports = { 934 module.exports = {
590 getAccountInfo, 935 getAccountInfo,
591 userPwd, 936 userPwd,
592 userEmail, 937 userEmail,
593 userMobile, 938 userMobile,
594 - checkVerifyCode 939 + sendEmailSuccess,
  940 + mailResult,
  941 + verifyPassword,
  942 + checkPassword,
  943 + checkVerifyCode,
  944 + identityMobile,
  945 + sendMobileMsg,
  946 + checkMobileMsg,
  947 + sendEmail,
  948 + checkEmail,
  949 + checkMobile,
  950 + modifyEmail,
  951 + modifyMobile,
  952 + modifyPwd
595 }; 953 };
@@ -102,6 +102,20 @@ const editUserLikeBrand = (uid, brand) => { @@ -102,6 +102,20 @@ const editUserLikeBrand = (uid, brand) => {
102 }); 102 });
103 }; 103 };
104 104
  105 +/**
  106 + * 根据手机号获取用户信息[TODO +cache]
  107 + * @param string $area
  108 + * @param string $mobile
  109 + * @return array
  110 + */
  111 +const getUserInfoByMobile = (area, mobile) => {
  112 + return api.get('', {
  113 + method: 'app.passport.getProfileByMobile',
  114 + area: area,
  115 + mobile: mobile
  116 + });
  117 +};
  118 +
105 module.exports = { 119 module.exports = {
106 getUserInfo, 120 getUserInfo,
107 editUserInfo, 121 editUserInfo,
@@ -112,5 +126,6 @@ module.exports = { @@ -112,5 +126,6 @@ module.exports = {
112 editUserHabitsInfo, 126 editUserHabitsInfo,
113 getUserLikeBrand, 127 getUserLikeBrand,
114 editUserLikeBrand, 128 editUserLikeBrand,
115 - getTipConfig 129 + getTipConfig,
  130 + getUserInfoByMobile
116 }; 131 };