back-service.js
5.56 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
/**
* Created by TaoHuang on 2016/6/14.
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const moment = require('moment');
const helpers = global.yoho.helpers;
const api = require('./back-api');
const userService = require('./user-service');
const passportHelper = require('./passport-helper');
const backHelper = require('./back-helper');
const BACK_LEFT_BANNER_CODE = '3bbaf502c447a2ddad60879042e286d8'; // 找回密码左边的banner
/**
* 验证手机和邮箱输入正确性
*/
const validateEmailOrMobileAsync = (userInput, areaCode) => {
return new Promise(function(resolve, rejected) {
let result = {type: 'email', area: '', phone: ''};
if (passportHelper.validator.isEmail(userInput)) {
result.type = 'email';
result.area = '';
result.phone = userInput;
resolve(result);
} else if (passportHelper.validator.isMobile(userInput)) {
result.type = 'mobile';
result.area = areaCode;
result.phone = userInput;
resolve(result);
} else {
rejected('输入信息出错!');
}
});
};
/**
* 查找用户
*/
const findUserAsync = (type, phone, area) => {
return co(function * () {
const MESSAGE = {
mobile: '您输入的手机号码尚未注册!',
email: '您输入的邮件账户尚未注册!',
ok: '验证成功'
};
const findBy = {
email: userService.findByEmailAsync,
mobile: _.rearg(userService.findByMobileAsync, [1, 0]) // 交换参数
};
const OK = {code: 200, message: MESSAGE.ok};
const user = yield findBy[type](phone, area);
if (_.isEmpty(user)) {
return {
code: 402,
message: MESSAGE[type]
};
}
return OK;
})();
};
/**
* 发送验证码到用户
*/
const sendCodeToUserAsync = (type, mobile, areaCode) => {
let sendTo = {
email: api.sendCodeToEmailAsync,
mobile: api.sendCodeToMobileAsync
};
return sendTo[type](mobile, areaCode);
};
/**
* 发送找回手机号短信
*/
const sendCodeToMobileAsync = (areaCode, mobile) => {
return api.sendCodeToMobileAsync(mobile, areaCode);
};
/**
* 获得首页的数据
*/
const indexPageDataAsync = () => {
return co(function *() {
let banner = yield passportHelper.getLeftBannerAsync(BACK_LEFT_BANNER_CODE);
let countryList = passportHelper.getCountry();
return {
back: {
coverHref: banner.url,
coverImg: banner.img,
countryCode: 86,
countryName: '中国',
captchaUrl: helpers.urlFormat('/passport/imagesNode', {t: moment().unix()}),
countryList: countryList
}
};
})();
};
/**
* 验证手机验证码
*/
const verifyCodyByMobileAsync = (area, mobile, mobileCode) => {
const ERR = {
code: 400,
message: '验证码错误!',
data: helpers.urlFormat('/passport/back/index')
};
return api.validateMobileCodeAsync(mobile, mobileCode, area)
.then(result => {
if (!(result.code && result.code === 200)) {
return ERR;
}
let data = {
mobile: mobile,
area: area,
token: result.data.token,
createdAt: moment().unix()
};
data.code = new Buffer(backHelper.makeToken(data)).toString('base64');
return {
code: 200,
message: '验证成功',
data: helpers.urlFormat('/passport/back/backcode', data)
};
});
};
/**
* 手机 token 合法性验证
*/
const authRequest = (data, token) => {
if (!backHelper.validateToken(data, token)) {
return {};
}
let existTime = moment.duration(60, 'minutes').asSeconds();
let isExpired = (moment().unix() - data.createdAt) > existTime;
if (isExpired) {
return {};
} else {
return data;
}
};
/**
* 更新密码接口
*/
const updatePwdAsync = (emailToken, mobileToken, newPassword) => {
return co(function * () {
let result = {type: 'mobile', status: false};
const ERR = {type: 'unknown', status: false};
if (!_.isEmpty(mobileToken)) {
if (!mobileToken.mobile || mobileToken.uid) {
return ERR;
}
let mobile = mobileToken.mobile;
let area = mobileToken.area;
let token = mobileToken.token;
let modifyStatus = yield api.modifyPasswordByMobileAsyncAes(mobile, token, newPassword, area);
if (!modifyStatus.code || modifyStatus.code !== 200) {
return ERR;
}
result.type = 'mobile';
result.status = true;
} else {
let modifyStatus = yield api.modifyPasswordByEmailCodeAsyncAes(emailToken, newPassword);
if (!modifyStatus.code || modifyStatus.code !== 200) {
return ERR;
}
result.type = 'email';
result.status = true;
}
return result;
})();
};
/**
* 验证邮件验证码
*/
const checkEmailCodeAsync = api.checkEmailCodeAsync;
module.exports = {
validateEmailOrMobileAsync,
findUserAsync,
sendCodeToUserAsync,
sendCodeToMobileAsync,
indexPageDataAsync,
verifyCodyByMobileAsync,
authRequest,
updatePwdAsync,
checkEmailCodeAsync
};