back-service.js
3.47 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
/**
* Created by TaoHuang on 2016/6/14.
*/
'use strict';
const helpers = require(`${library}/helpers`);
const api = require('./back-api');
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const userService = require('./user-service');
const sessionService = require('./session-service');
const api = require('./back-api');
module.exports.identifyUserByEmailOrMobileAsync = (userInput, areaCode) => {
return co(function * () {
const MESSAGE = {
errPhone: '您输入的手机号码尚未注册!',
errEmail: '您输入的邮件账户尚未注册!',
errUnknown: '服务器错误',
ok: '验证成功'
};
const OK = {code: 200, message: MESSAGE.ok};
let status = {code: 400, message: MESSAGE.errUnknown};
if (helpers.verifyEmail(userInput)) {
const user = yield userService.findByEmailAsync(userInput);
if (_.isEmpty(user)) {
status.code = 402;
status.message = MESSAGE.errEmail;
return status;
}
} else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
const user = yield userService.findByMobileAsync(userInput, areaCode);
if (_.isEmpty(user)) {
status.code = 402;
status.message = MESSAGE.errPhone;
return status;
}
} else {
return status;
}
return OK;
})();
};
function sendCodeToEmailAsync(input) {
return api.sendCodeToEmailAsync(input).then(result => {
if (!result.code || result.code !== 200) {
return Promise.reject('request error!');
}
return result.data;
}).catch(() => {
return {};
});
}
function sendCodeToMobileAsync(areaCode, userInput) {
return api.sendCodeToMobileAsync(areaCode, userInput).then(result => {
if (!result.code || result.code !== 200) {
return Promise.reject('request error');
}
return result.data;
}).catch(() => {
return {};
});
}
module.exports.sendCodeToUserAsync = (userInput, areaCode) => {
return co(function * () {
if (helpers.verifyEmail(userInput)) {
const result = yield sendCodeToEmailAsync(userInput);
if (!_.isEmpty(result)) {
sessionService.set('email', userInput);
return 'email;';
}
return 'unknown';
} else if (helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, userInput))) {
const result = yield sendCodeToMobileAsync(userInput, areaCode);
if (!_.isEmpty(result)) {
sessionService.set('mobile', userInput);
sessionService.set('area', areaCode);
return 'mobile';
}
return 'unknown';
} else {
return 'unknown';
}
})();
};
/**
* 发送找回手机号短信
*/
module.exports.sendCodeToMobileAsync = (areaCode, mobile) => {
const ERR = {code: 400, message: '验证失败'};
if (!helpers.verifyAreaMobile(helpers.makeAreaMobile(areaCode, mobile))) {
return ERR;
}
return api.sendCodeToMobileAsync(mobile, areaCode);
};
module.exports.sendCodeToEmailAsync = (email) => {
};
/**
* 获得首页的数据
*/
// TODO:
module.exports.indexPageDataAsync = () => {
};
module.exports.getVerifyCodeByMobilePageDataAsync = () => {
};