@kne/fastify-account 1.0.0-alpha.6 → 1.0.0-alpha.7
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/README.md +2 -2
- package/libs/controllers/admin.js +1 -1
- package/libs/services/account.js +8 -0
- package/libs/services/tenant-user.js +69 -5
- package/libs/services/user.js +16 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ npm i --save @kne/fastify-account
|
|
|
22
22
|
### API
|
|
23
23
|
|
|
24
24
|
---
|
|
25
|
-
title: "@kne/fastify-account v1.0.0-alpha.
|
|
25
|
+
title: "@kne/fastify-account v1.0.0-alpha.6"
|
|
26
26
|
language_tabs:
|
|
27
27
|
- shell: Shell
|
|
28
28
|
- http: HTTP
|
|
@@ -42,7 +42,7 @@ headingLevel: 2
|
|
|
42
42
|
|
|
43
43
|
<!-- Generator: Widdershins v4.0.1 -->
|
|
44
44
|
|
|
45
|
-
<h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.
|
|
45
|
+
<h1 id="-kne-fastify-account">@kne/fastify-account v1.0.0-alpha.6</h1>
|
|
46
46
|
|
|
47
47
|
> Scroll down for code samples, example requests and responses. Select a language for code samples from the tabs above or the mobile navigation menu.
|
|
48
48
|
|
|
@@ -34,7 +34,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
34
34
|
},
|
|
35
35
|
async request => {
|
|
36
36
|
const userInfo = request.body;
|
|
37
|
-
await services.admin.addUser(Object.assign({}, userInfo, { password: options.defaultPassword }));
|
|
37
|
+
await services.admin.addUser(Object.assign({}, userInfo, { password: services.account.md5(options.defaultPassword) }));
|
|
38
38
|
return {};
|
|
39
39
|
}
|
|
40
40
|
);
|
package/libs/services/account.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
const dayjs = require('dayjs');
|
|
3
3
|
const bcrypt = require('bcryptjs');
|
|
4
|
+
const crypto = require('crypto');
|
|
4
5
|
|
|
5
6
|
function generateRandom6DigitNumber() {
|
|
6
7
|
const randomNumber = Math.random() * 1000000;
|
|
@@ -76,6 +77,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
76
77
|
};
|
|
77
78
|
};
|
|
78
79
|
|
|
80
|
+
const md5 = value => {
|
|
81
|
+
const hash = crypto.createHash('md5');
|
|
82
|
+
hash.update(value);
|
|
83
|
+
return hash.digest('hex');
|
|
84
|
+
};
|
|
85
|
+
|
|
79
86
|
const resetPassword = async ({ userId, password }) => {
|
|
80
87
|
const userInfo = await models.user.findOne({
|
|
81
88
|
where: { uuid: userId }
|
|
@@ -198,6 +205,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
198
205
|
};
|
|
199
206
|
|
|
200
207
|
services.account = {
|
|
208
|
+
md5,
|
|
201
209
|
login,
|
|
202
210
|
register,
|
|
203
211
|
sendEmailCode,
|
|
@@ -257,7 +257,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
257
257
|
}
|
|
258
258
|
};
|
|
259
259
|
|
|
260
|
-
const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }) => {
|
|
260
|
+
const addTenantUser = async ({ tenantId, roleIds, orgIds, userId, ...tenantUser }, transaction) => {
|
|
261
261
|
const tenant = await services.tenant.getTenant({ id: tenantId });
|
|
262
262
|
|
|
263
263
|
const currentAccountNumber = await models.tenantUser.count({
|
|
@@ -270,7 +270,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
270
270
|
|
|
271
271
|
await checkTenantUserInfoValidate({ tenantId, roleIds, orgIds, userId });
|
|
272
272
|
|
|
273
|
-
const t = await fastify.sequelize.instance.transaction();
|
|
273
|
+
const t = transaction || (await fastify.sequelize.instance.transaction());
|
|
274
274
|
|
|
275
275
|
if (
|
|
276
276
|
(await models.tenantUser.count({
|
|
@@ -319,9 +319,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
319
319
|
{ transaction: t }
|
|
320
320
|
);
|
|
321
321
|
|
|
322
|
-
await t.commit();
|
|
322
|
+
!transaction && (await t.commit());
|
|
323
323
|
} catch (e) {
|
|
324
|
-
await t.rollback();
|
|
324
|
+
!transaction && (await t.rollback());
|
|
325
325
|
throw e;
|
|
326
326
|
}
|
|
327
327
|
};
|
|
@@ -466,6 +466,69 @@ module.exports = fp(async (fastify, options) => {
|
|
|
466
466
|
};
|
|
467
467
|
};
|
|
468
468
|
|
|
469
|
+
const includeTenantUserBatch = async ({ tenantId, list }) => {
|
|
470
|
+
await services.tenant.getTenant({ id: tenantId });
|
|
471
|
+
const errors = [],
|
|
472
|
+
successes = [];
|
|
473
|
+
for (let current of list) {
|
|
474
|
+
if (!(current.phone || current.email)) {
|
|
475
|
+
errors.push({ item: current, msg: '电话和邮箱不能同时为空' });
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
const currentQuery = [];
|
|
479
|
+
if (current.phone) {
|
|
480
|
+
currentQuery.push({ phone: current.phone });
|
|
481
|
+
}
|
|
482
|
+
if (current.email) {
|
|
483
|
+
currentQuery.push({ email: current.email });
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (
|
|
487
|
+
(await models.tenantUser.count({
|
|
488
|
+
where: {
|
|
489
|
+
[Op.or]: currentQuery
|
|
490
|
+
}
|
|
491
|
+
})) > 0
|
|
492
|
+
) {
|
|
493
|
+
errors.push({ item: current, msg: '租户用户已经存在,或手机邮箱和已有租户用户重复' });
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
const t = await fastify.sequelize.instance.transaction();
|
|
497
|
+
try {
|
|
498
|
+
if (await services.user.accountIsExists(current, {})) {
|
|
499
|
+
errors.push({ item: current, msg: '用户已经存在,已发送加入租户邀请等待对方同意' });
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const user = await services.user.addUser(
|
|
504
|
+
{
|
|
505
|
+
nickname: current.name,
|
|
506
|
+
phone: current.phone,
|
|
507
|
+
email: current.email,
|
|
508
|
+
password: services.account.md5(current.password || options.defaultPassword),
|
|
509
|
+
status: 1
|
|
510
|
+
},
|
|
511
|
+
{ transaction: t }
|
|
512
|
+
);
|
|
513
|
+
await services.tenantUser.addTenantUser(
|
|
514
|
+
{
|
|
515
|
+
tenantId,
|
|
516
|
+
userId: user.id,
|
|
517
|
+
...current
|
|
518
|
+
},
|
|
519
|
+
{ transaction: t }
|
|
520
|
+
);
|
|
521
|
+
successes.push({ item: current });
|
|
522
|
+
await t.commit();
|
|
523
|
+
} catch (e) {
|
|
524
|
+
await t.rollback();
|
|
525
|
+
errors.push({ item: current, msg: e.message });
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return { errors, successes };
|
|
530
|
+
};
|
|
531
|
+
|
|
469
532
|
services.tenantUser = {
|
|
470
533
|
getUserTenant,
|
|
471
534
|
getTenantUserPermissionList,
|
|
@@ -477,6 +540,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
477
540
|
deleteTenantUser,
|
|
478
541
|
closeTenantUser,
|
|
479
542
|
openTenantUser,
|
|
480
|
-
getTenantUserList
|
|
543
|
+
getTenantUserList,
|
|
544
|
+
includeTenantUserBatch
|
|
481
545
|
};
|
|
482
546
|
});
|
package/libs/services/user.js
CHANGED
|
@@ -54,7 +54,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
54
54
|
);
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }) => {
|
|
57
|
+
const addUser = async ({ avatar, nickname, gender, birthday, description, phone, email, password, status }, transaction) => {
|
|
58
58
|
if ((await accountIsExists({ phone, email })) > 0) {
|
|
59
59
|
throw new Error('手机号或者邮箱都不能重复');
|
|
60
60
|
}
|
|
@@ -62,18 +62,21 @@ module.exports = fp(async (fastify, options) => {
|
|
|
62
62
|
throw new Error('密码不能为空');
|
|
63
63
|
}
|
|
64
64
|
const account = await models.userAccount.create(await services.account.passwordEncryption(password));
|
|
65
|
-
const user = await models.user.create(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
65
|
+
const user = await models.user.create(
|
|
66
|
+
{
|
|
67
|
+
avatar,
|
|
68
|
+
nickname,
|
|
69
|
+
gender,
|
|
70
|
+
birthday,
|
|
71
|
+
description,
|
|
72
|
+
phone,
|
|
73
|
+
email,
|
|
74
|
+
status,
|
|
75
|
+
userAccountId: account.uuid
|
|
76
|
+
},
|
|
77
|
+
{ transaction }
|
|
78
|
+
);
|
|
79
|
+
await account.update({ belongToUserId: user.uuid }, { transaction });
|
|
77
80
|
|
|
78
81
|
return Object.assign({}, user.get({ pain: true }), { id: user.uuid });
|
|
79
82
|
};
|