@kne/fastify-account 2.0.0-alpha.3 → 2.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/libs/controllers/account.js +228 -227
- package/package.json +1 -1
|
@@ -1,263 +1,264 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
|
|
3
3
|
const accountController = fp(async (fastify, options) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}, response: {
|
|
12
|
-
200: {
|
|
13
|
-
content: {
|
|
14
|
-
'application/json': {
|
|
15
|
-
schema: {
|
|
16
|
-
type: 'object', properties: {
|
|
17
|
-
code: {type: 'string', description: '验证码'}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
4
|
+
const { services } = fastify[options.name];
|
|
5
|
+
fastify.post(`${options.prefix}/account/sendEmailCode`, {
|
|
6
|
+
schema: {
|
|
7
|
+
tags: ['账号'], summary: '发送登录邮箱验证码', body: {
|
|
8
|
+
type: 'object', required: ['email'], properties: {
|
|
9
|
+
email: { type: 'string', description: '邮箱' },
|
|
10
|
+
type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码', default: 0 }
|
|
24
11
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
schema: {
|
|
33
|
-
tags: ['账号'], summary: '发送登录短信验证码', body: {
|
|
34
|
-
type: 'object', required: ['phone'], properties: {
|
|
35
|
-
phone: {type: 'string', description: '电话'}
|
|
36
|
-
}
|
|
37
|
-
}, response: {
|
|
38
|
-
200: {
|
|
39
|
-
content: {
|
|
40
|
-
'application/json': {
|
|
41
|
-
schema: {
|
|
42
|
-
type: 'object', properties: {
|
|
43
|
-
code: {type: 'string', description: '验证码'}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
12
|
+
}, response: {
|
|
13
|
+
200: {
|
|
14
|
+
content: {
|
|
15
|
+
'application/json': {
|
|
16
|
+
schema: {
|
|
17
|
+
type: 'object', properties: {
|
|
18
|
+
code: { type: 'string', description: '验证码' }
|
|
48
19
|
}
|
|
20
|
+
}
|
|
49
21
|
}
|
|
22
|
+
}
|
|
50
23
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}, async request => {
|
|
27
|
+
const { email, type } = request.body;
|
|
28
|
+
const code = await services.account.sendVerificationCode({ name: email, type });
|
|
29
|
+
return options.isTest ? { code } : {};
|
|
30
|
+
});
|
|
56
31
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
type: {type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码'},
|
|
63
|
-
code: {type: 'string', description: '接受到的验证码'}
|
|
64
|
-
}
|
|
65
|
-
}, response: {
|
|
66
|
-
200: {
|
|
67
|
-
content: {
|
|
68
|
-
'application/json': {
|
|
69
|
-
schema: {}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}, async request => {
|
|
76
|
-
const {name, type, code} = request.body;
|
|
77
|
-
const isPass = await services.account.verificationCodeValidate({
|
|
78
|
-
name, type, code
|
|
79
|
-
});
|
|
80
|
-
if (!isPass) {
|
|
81
|
-
throw new Error('验证码错误');
|
|
32
|
+
fastify.post(`${options.prefix}/account/sendSMSCode`, {
|
|
33
|
+
schema: {
|
|
34
|
+
tags: ['账号'], summary: '发送登录短信验证码', body: {
|
|
35
|
+
type: 'object', required: ['phone'], properties: {
|
|
36
|
+
phone: { type: 'string', description: '电话' }
|
|
82
37
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
type: 'object', required: ['phone'], properties: {
|
|
91
|
-
phone: {type: 'string', description: '电话'}
|
|
92
|
-
}
|
|
93
|
-
}, {
|
|
94
|
-
type: 'object', required: ['email'], properties: {
|
|
95
|
-
email: {type: 'string', description: '邮箱'}
|
|
96
|
-
}
|
|
97
|
-
}]
|
|
98
|
-
}, response: {
|
|
99
|
-
200: {
|
|
100
|
-
content: {
|
|
101
|
-
'application/json': {
|
|
102
|
-
schema: {
|
|
103
|
-
type: 'object', properties: {
|
|
104
|
-
isExists: {type: 'boolean', description: 'true已存在,false不存在'}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
38
|
+
}, response: {
|
|
39
|
+
200: {
|
|
40
|
+
content: {
|
|
41
|
+
'application/json': {
|
|
42
|
+
schema: {
|
|
43
|
+
type: 'object', properties: {
|
|
44
|
+
code: { type: 'string', description: '验证码' }
|
|
109
45
|
}
|
|
46
|
+
}
|
|
110
47
|
}
|
|
48
|
+
}
|
|
111
49
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}, async request => {
|
|
53
|
+
const { phone } = request.body;
|
|
54
|
+
const code = await services.account.sendVerificationCode({ name: phone, type: 0 });
|
|
55
|
+
return options.isTest ? { code } : {};
|
|
56
|
+
});
|
|
116
57
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
description: {type: 'string', description: '个人简介'}
|
|
131
|
-
}
|
|
132
|
-
}, {
|
|
133
|
-
type: 'object', required: ['email', 'password', 'code'], properties: {
|
|
134
|
-
avatar: {type: 'string', description: '头像图片id'},
|
|
135
|
-
email: {type: 'string', description: '邮箱'},
|
|
136
|
-
code: {type: 'string', description: '验证码'},
|
|
137
|
-
password: {type: 'string', description: '密码(需要md5加密)'},
|
|
138
|
-
invitationCode: {type: 'string', description: '邀请码,用来默认加入租户'},
|
|
139
|
-
nickname: {type: 'string', description: '昵称'},
|
|
140
|
-
gender: {type: 'string', description: '性别'},
|
|
141
|
-
birthday: {type: 'string', format: 'date', description: '出生日期'},
|
|
142
|
-
description: {type: 'string', description: '个人简介'}
|
|
143
|
-
}
|
|
144
|
-
}]
|
|
145
|
-
}, response: {
|
|
146
|
-
200: {
|
|
147
|
-
content: {
|
|
148
|
-
'application/json': {
|
|
149
|
-
schema: {}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
58
|
+
fastify.post(`${options.prefix}/account/validateCode`, {
|
|
59
|
+
schema: {
|
|
60
|
+
tags: ['账号'], summary: '验证码验证', body: {
|
|
61
|
+
type: 'object', required: ['name', 'type', 'code'], properties: {
|
|
62
|
+
name: { type: 'string', description: '被验证的账号,手机或邮箱' },
|
|
63
|
+
type: { type: 'number', description: '0:注册,2:登录,4:验证租户管理员,5:忘记密码' },
|
|
64
|
+
code: { type: 'string', description: '接受到的验证码' }
|
|
65
|
+
}
|
|
66
|
+
}, response: {
|
|
67
|
+
200: {
|
|
68
|
+
content: {
|
|
69
|
+
'application/json': {
|
|
70
|
+
schema: {}
|
|
153
71
|
}
|
|
72
|
+
}
|
|
154
73
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, async request => {
|
|
77
|
+
const { name, type, code } = request.body;
|
|
78
|
+
const isPass = await services.account.verificationCodeValidate({
|
|
79
|
+
name, type, code
|
|
158
80
|
});
|
|
81
|
+
if (!isPass) {
|
|
82
|
+
throw new Error('验证码错误');
|
|
83
|
+
}
|
|
84
|
+
return {};
|
|
85
|
+
});
|
|
159
86
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
}
|
|
87
|
+
fastify.post(`${options.prefix}/account/accountIsExists`, {
|
|
88
|
+
schema: {
|
|
89
|
+
tags: ['账号'], summary: '账号是否已存在', body: {
|
|
90
|
+
oneOf: [{
|
|
91
|
+
type: 'object', required: ['phone'], properties: {
|
|
92
|
+
phone: { type: 'string', description: '电话' }
|
|
93
|
+
}
|
|
94
|
+
}, {
|
|
95
|
+
type: 'object', required: ['email'], properties: {
|
|
96
|
+
email: { type: 'string', description: '邮箱' }
|
|
97
|
+
}
|
|
98
|
+
}]
|
|
99
|
+
}, response: {
|
|
100
|
+
200: {
|
|
101
|
+
content: {
|
|
102
|
+
'application/json': {
|
|
103
|
+
schema: {
|
|
104
|
+
type: 'object', properties: {
|
|
105
|
+
isExists: { type: 'boolean', description: 'true已存在,false不存在' }
|
|
181
106
|
}
|
|
107
|
+
}
|
|
182
108
|
}
|
|
109
|
+
}
|
|
183
110
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}, async request => {
|
|
114
|
+
const { phone, email } = request.body;
|
|
115
|
+
return { isExists: await services.user.accountIsExists({ phone, email }) };
|
|
116
|
+
});
|
|
189
117
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
118
|
+
fastify.post(`${options.prefix}/account/register`, {
|
|
119
|
+
schema: {
|
|
120
|
+
tags: ['账号'], summary: '注册账号', body: {
|
|
121
|
+
oneOf: [{
|
|
122
|
+
type: 'object', required: ['phone', 'password', 'code'], properties: {
|
|
123
|
+
avatar: { type: 'string', description: '头像图片id' },
|
|
124
|
+
phone: { type: 'string', description: '电话' },
|
|
125
|
+
code: { type: 'string', description: '验证码' },
|
|
126
|
+
password: { type: 'string', description: '密码(需要md5加密)' },
|
|
127
|
+
invitationCode: { type: 'string', description: '邀请码,用来默认加入租户' },
|
|
128
|
+
nickname: { type: 'string', description: '昵称' },
|
|
129
|
+
gender: { type: 'string', description: '性别' },
|
|
130
|
+
birthday: { type: 'string', format: 'date', description: '出生日期' },
|
|
131
|
+
description: { type: 'string', description: '个人简介' }
|
|
132
|
+
}
|
|
133
|
+
}, {
|
|
134
|
+
type: 'object', required: ['email', 'password', 'code'], properties: {
|
|
135
|
+
avatar: { type: 'string', description: '头像图片id' },
|
|
136
|
+
email: { type: 'string', description: '邮箱' },
|
|
137
|
+
code: { type: 'string', description: '验证码' },
|
|
138
|
+
password: { type: 'string', description: '密码(需要md5加密)' },
|
|
139
|
+
invitationCode: { type: 'string', description: '邀请码,用来默认加入租户' },
|
|
140
|
+
nickname: { type: 'string', description: '昵称' },
|
|
141
|
+
gender: { type: 'string', description: '性别' },
|
|
142
|
+
birthday: { type: 'string', format: 'date', description: '出生日期' },
|
|
143
|
+
description: { type: 'string', description: '个人简介' }
|
|
144
|
+
}
|
|
145
|
+
}]
|
|
146
|
+
}, response: {
|
|
147
|
+
200: {
|
|
148
|
+
content: {
|
|
149
|
+
'application/json': {
|
|
150
|
+
schema: {}
|
|
206
151
|
}
|
|
152
|
+
}
|
|
207
153
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}, async request => {
|
|
157
|
+
const account = request.body;
|
|
158
|
+
return await services.account.register(account);
|
|
159
|
+
});
|
|
212
160
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
161
|
+
fastify.post(`${options.prefix}/account/login`, {
|
|
162
|
+
schema: {
|
|
163
|
+
tags: ['账号'], summary: '登录', body: {
|
|
164
|
+
type: 'object', required: ['password'], properties: {
|
|
165
|
+
type: { type: 'string', default: 'email', description: '登录类型' },
|
|
166
|
+
email: { type: 'string', description: '邮箱' },
|
|
167
|
+
phone: { type: 'string', description: '手机号' },
|
|
168
|
+
password: { type: 'string', description: '密码' }
|
|
169
|
+
}
|
|
170
|
+
}, response: {
|
|
171
|
+
200: {
|
|
172
|
+
content: {
|
|
173
|
+
'application/json': {
|
|
174
|
+
schema: {
|
|
175
|
+
type: 'object', properties: {
|
|
176
|
+
token: { type: 'string', description: '用户token' },
|
|
177
|
+
currentTenantId: { type: 'string', description: '当前租户id' }
|
|
218
178
|
}
|
|
179
|
+
}
|
|
219
180
|
}
|
|
181
|
+
}
|
|
220
182
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}, async request => {
|
|
186
|
+
const appName = request.headers['x-app-name'];
|
|
187
|
+
const { token, user } = await services.account.login(Object.assign({}, request.body, { appName }));
|
|
188
|
+
return { token, currentTenantId: user.currentTenantId };
|
|
189
|
+
});
|
|
225
190
|
|
|
226
|
-
|
|
227
|
-
|
|
191
|
+
fastify.post(`${options.prefix}/account/modifyPassword`, {
|
|
192
|
+
schema: {
|
|
193
|
+
tags: ['账号'], summary: '新用户重置新密码', body: {
|
|
194
|
+
oneOf: [{
|
|
195
|
+
type: 'object', required: ['email', 'newPwd', 'oldPwd'], properties: {
|
|
196
|
+
email: { type: 'string', description: '邮箱' },
|
|
197
|
+
newPwd: { type: 'string', description: '新密码' },
|
|
198
|
+
oldPwd: { type: 'string', description: '原密码' }
|
|
199
|
+
}
|
|
200
|
+
}, {
|
|
201
|
+
type: 'object', required: ['phone', 'newPwd', 'oldPwd'], properties: {
|
|
202
|
+
phone: { type: 'string', description: '手机号' },
|
|
203
|
+
newPwd: { type: 'string', description: '新密码' },
|
|
204
|
+
oldPwd: { type: 'string', description: '原密码' }
|
|
205
|
+
}
|
|
206
|
+
}]
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}, async request => {
|
|
210
|
+
await services.account.modifyPassword(request.body);
|
|
211
|
+
return {};
|
|
212
|
+
});
|
|
228
213
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
email: {type: 'string', description: '邮箱'}
|
|
235
|
-
}
|
|
236
|
-
}, {
|
|
237
|
-
type: 'object', required: ['phone'], properties: {
|
|
238
|
-
phone: {type: 'string', description: '手机号'}
|
|
239
|
-
}
|
|
240
|
-
}]
|
|
241
|
-
}
|
|
214
|
+
fastify.post(`${options.prefix}/account/resetPassword`, {
|
|
215
|
+
schema: {
|
|
216
|
+
tags: ['账号'], summary: '用户重置密码', body: {
|
|
217
|
+
type: 'object', required: ['newPwd', 'token'], properties: {
|
|
218
|
+
newPwd: { type: 'string', description: '新密码' }, token: { type: 'string', description: '验证token' }
|
|
242
219
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}, async request => {
|
|
223
|
+
await services.account.resetPasswordByToken({
|
|
224
|
+
password: request.body.newPwd, token: request.body.token
|
|
247
225
|
});
|
|
248
226
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
227
|
+
return {};
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
fastify.post(`${options.prefix}/account/forgetPwd`, {
|
|
231
|
+
schema: {
|
|
232
|
+
tags: ['账号'], summary: '忘记密码', body: {
|
|
233
|
+
oneOf: [{
|
|
234
|
+
type: 'object', required: ['email'], properties: {
|
|
235
|
+
email: { type: 'string', description: '邮箱' }
|
|
236
|
+
}
|
|
237
|
+
}, {
|
|
238
|
+
type: 'object', required: ['phone'], properties: {
|
|
239
|
+
phone: { type: 'string', description: '手机号' }
|
|
240
|
+
}
|
|
241
|
+
}]
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}, async request => {
|
|
245
|
+
const name = request.body.email || request.body.phone;
|
|
246
|
+
const token = await services.account.sendJWTVerificationCode({ name, type: 5 });
|
|
247
|
+
return options.isTest ? { token } : {};
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
fastify.post(`${options.prefix}/account/parseResetToken`, {
|
|
251
|
+
schema: {
|
|
252
|
+
tags: ['账号'], summary: '通过token获取name', body: {
|
|
253
|
+
type: 'object', required: ['token'], properties: {
|
|
254
|
+
token: { type: 'string' }
|
|
256
255
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}, async request => {
|
|
259
|
+
const { name } = await services.account.verificationJWTCodeValidate(request.body);
|
|
260
|
+
return { name };
|
|
261
|
+
});
|
|
261
262
|
});
|
|
262
263
|
|
|
263
264
|
module.exports = accountController;
|