@kne/fastify-account 1.0.0-alpha.2 → 1.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/README.md +47 -2
- package/index.js +10 -4
- package/libs/controllers/account.js +7 -6
- package/libs/controllers/admin.js +16 -15
- package/libs/controllers/adminPermission.js +42 -35
- package/libs/controllers/adminRole.js +13 -12
- package/libs/controllers/adminTenant.js +39 -36
- package/libs/controllers/tenant.js +7 -6
- package/libs/controllers/user.js +4 -3
- package/libs/models/admin-role.js +4 -8
- package/libs/models/application.js +16 -10
- package/libs/models/login-log.js +4 -8
- package/libs/models/permission.js +7 -9
- package/libs/models/tenant-application.js +8 -10
- package/libs/models/tenant-org.js +5 -9
- package/libs/models/tenant-permission.js +7 -9
- package/libs/models/tenant-role-application.js +13 -13
- package/libs/models/tenant-role-permission.js +9 -14
- package/libs/models/tenant-role.js +5 -9
- package/libs/models/tenant-share-group-permission.js +5 -9
- package/libs/models/tenant-share-group.js +5 -9
- package/libs/models/tenant-source-user-share-group.js +5 -9
- package/libs/models/tenant-token.js +7 -9
- package/libs/models/tenant-user-org.js +11 -10
- package/libs/models/tenant-user-role.js +11 -10
- package/libs/models/tenant-user-share-group.js +6 -10
- package/libs/models/tenant-user.js +35 -16
- package/libs/models/tenant.js +17 -9
- package/libs/models/user-account.js +17 -9
- package/libs/models/user.js +27 -17
- package/libs/models/verification-code.js +4 -8
- package/libs/services/account.js +26 -16
- package/libs/services/admin.js +14 -116
- package/libs/services/application.js +151 -0
- package/libs/services/permission.js +47 -145
- package/libs/services/tenant-invite.js +62 -0
- package/libs/services/tenant-org.js +84 -0
- package/libs/services/tenant-role.js +108 -0
- package/libs/services/tenant-user.js +486 -0
- package/libs/services/tenant.js +68 -669
- package/libs/services/user.js +63 -33
- package/package.json +3 -3
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
5
|
+
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
8
7
|
},
|
|
9
8
|
permissionId: {
|
|
@@ -19,21 +18,17 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
19
18
|
defaultValue: 0 //0:开启 11:关闭
|
|
20
19
|
}
|
|
21
20
|
},
|
|
22
|
-
{
|
|
23
|
-
paranoid: true,
|
|
21
|
+
options: {
|
|
24
22
|
indexes: [
|
|
25
23
|
{
|
|
26
24
|
name: 'permission_key',
|
|
27
25
|
unique: true,
|
|
28
|
-
fields: ['
|
|
26
|
+
fields: ['tenant_id', 'permission_id', 'role_id', 'deleted_at']
|
|
29
27
|
}
|
|
30
28
|
]
|
|
29
|
+
},
|
|
30
|
+
associate: ({ tenantRolePermission, permission }) => {
|
|
31
|
+
tenantRolePermission.belongsTo(permission, { constraints: false });
|
|
31
32
|
}
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
tenantRolePermission.associate = ({ tenantRolePermission, permission }) => {
|
|
35
|
-
tenantRolePermission.belongsTo(permission);
|
|
36
33
|
};
|
|
37
|
-
|
|
38
|
-
return tenantRolePermission;
|
|
39
34
|
};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
5
|
+
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
8
7
|
},
|
|
9
8
|
name: {
|
|
@@ -19,9 +18,6 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
19
18
|
defaultValue: 0 //0:用户自定义,1:系统默认
|
|
20
19
|
},
|
|
21
20
|
description: DataTypes.TEXT
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
paranoid: true
|
|
25
21
|
}
|
|
26
|
-
|
|
22
|
+
};
|
|
27
23
|
};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
5
|
+
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
8
7
|
},
|
|
9
8
|
tenantShareGroupId: {
|
|
@@ -14,9 +13,6 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
14
13
|
type: DataTypes.INTEGER,
|
|
15
14
|
allowNull: false
|
|
16
15
|
}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
paranoid: true
|
|
20
16
|
}
|
|
21
|
-
|
|
17
|
+
};
|
|
22
18
|
};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
5
|
+
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
8
7
|
},
|
|
9
8
|
name: {
|
|
@@ -14,9 +13,6 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
14
13
|
type: DataTypes.INTEGER,
|
|
15
14
|
defaultValue: 0
|
|
16
15
|
}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
paranoid: true
|
|
20
16
|
}
|
|
21
|
-
|
|
17
|
+
};
|
|
22
18
|
};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
5
|
+
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
8
7
|
},
|
|
9
8
|
tenantUserId: {
|
|
@@ -14,9 +13,6 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
14
13
|
type: DataTypes.INTEGER,
|
|
15
14
|
allowNull: false
|
|
16
15
|
}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
paranoid: true
|
|
20
16
|
}
|
|
21
|
-
|
|
17
|
+
};
|
|
22
18
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
5
|
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
@@ -15,18 +14,17 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
15
14
|
type: DataTypes.INTEGER,
|
|
16
15
|
allowNull: false //10:邀请用户加入租户
|
|
17
16
|
},
|
|
18
|
-
|
|
17
|
+
createdTenantUserId: {
|
|
19
18
|
type: DataTypes.UUID
|
|
20
19
|
}
|
|
21
20
|
},
|
|
22
|
-
{
|
|
23
|
-
paranoid: true,
|
|
21
|
+
options: {
|
|
24
22
|
indexes: [
|
|
25
23
|
{
|
|
26
24
|
unique: true,
|
|
27
|
-
fields: ['
|
|
25
|
+
fields: ['tenant_id', 'token', 'type', 'deleted_at']
|
|
28
26
|
}
|
|
29
27
|
]
|
|
30
28
|
}
|
|
31
|
-
|
|
29
|
+
};
|
|
32
30
|
};
|
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
4
|
+
id: {
|
|
5
|
+
type: DataTypes.INTEGER,
|
|
6
|
+
autoIncrement: true,
|
|
7
|
+
primaryKey: true
|
|
8
|
+
},
|
|
5
9
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
10
|
+
type: DataTypes.UUID,
|
|
7
11
|
allowNull: false
|
|
8
12
|
},
|
|
9
13
|
tenantUserId: {
|
|
10
|
-
type: DataTypes.
|
|
14
|
+
type: DataTypes.UUID,
|
|
11
15
|
allowNull: false
|
|
12
16
|
},
|
|
13
17
|
tenantOrgId: {
|
|
14
18
|
type: DataTypes.INTEGER,
|
|
15
19
|
allowNull: false
|
|
16
20
|
}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
paranoid: true
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
+
};
|
|
22
23
|
};
|
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
4
|
+
id: {
|
|
5
|
+
type: DataTypes.INTEGER,
|
|
6
|
+
autoIncrement: true,
|
|
7
|
+
primaryKey: true
|
|
8
|
+
},
|
|
5
9
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
10
|
+
type: DataTypes.UUID,
|
|
7
11
|
allowNull: false
|
|
8
12
|
},
|
|
9
13
|
tenantUserId: {
|
|
10
|
-
type: DataTypes.
|
|
14
|
+
type: DataTypes.UUID,
|
|
11
15
|
allowNull: false
|
|
12
16
|
},
|
|
13
17
|
tenantRoleId: {
|
|
14
18
|
type: DataTypes.INTEGER,
|
|
15
19
|
allowNull: false
|
|
16
20
|
}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
paranoid: true
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
+
};
|
|
22
23
|
};
|
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
tenantId: {
|
|
6
|
-
type: DataTypes.
|
|
5
|
+
type: DataTypes.UUID,
|
|
7
6
|
allowNull: false
|
|
8
7
|
},
|
|
9
8
|
tenantUserId: {
|
|
10
|
-
type: DataTypes.
|
|
9
|
+
type: DataTypes.UUID,
|
|
11
10
|
allowNull: false
|
|
12
11
|
},
|
|
13
12
|
tenantShareGroupId: {
|
|
14
13
|
type: DataTypes.INTEGER,
|
|
15
14
|
allowNull: false
|
|
16
15
|
}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
paranoid: true
|
|
20
16
|
}
|
|
21
|
-
|
|
17
|
+
};
|
|
22
18
|
};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
id: {
|
|
6
|
-
type: DataTypes.
|
|
7
|
-
|
|
5
|
+
type: DataTypes.INTEGER,
|
|
6
|
+
autoIncrement: true,
|
|
8
7
|
primaryKey: true
|
|
9
8
|
},
|
|
9
|
+
uuid: {
|
|
10
|
+
type: DataTypes.UUID,
|
|
11
|
+
defaultValue: DataTypes.UUIDV4
|
|
12
|
+
},
|
|
10
13
|
userId: {
|
|
11
14
|
type: DataTypes.UUID,
|
|
12
15
|
allowNull: false
|
|
@@ -36,21 +39,37 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
36
39
|
defaultValue: 0 //0:正常,11:已禁用,12:已关闭,
|
|
37
40
|
}
|
|
38
41
|
},
|
|
39
|
-
{
|
|
40
|
-
paranoid: true,
|
|
42
|
+
options: {
|
|
41
43
|
indexes: [
|
|
42
44
|
{
|
|
43
45
|
unique: true,
|
|
44
|
-
fields: ['
|
|
46
|
+
fields: ['uuid', 'deleted_at']
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
unique: true,
|
|
50
|
+
fields: ['tenant_id', 'user_id', 'deleted_at']
|
|
45
51
|
}
|
|
46
52
|
]
|
|
53
|
+
},
|
|
54
|
+
associate: ({ user, tenantRole, tenantUser, tenantUserRole, tenantOrg, tenantUserOrg }) => {
|
|
55
|
+
tenantUser.belongsToMany(tenantRole, {
|
|
56
|
+
through: tenantUserRole,
|
|
57
|
+
foreignKey: 'tenantUserId',
|
|
58
|
+
otherKey: 'tenantRoleId',
|
|
59
|
+
sourceKey: 'uuid',
|
|
60
|
+
constraints: false
|
|
61
|
+
});
|
|
62
|
+
tenantUser.belongsToMany(tenantOrg, {
|
|
63
|
+
through: tenantUserOrg,
|
|
64
|
+
foreignKey: 'tenantUserId',
|
|
65
|
+
otherKey: 'tenantOrgId',
|
|
66
|
+
sourceKey: 'uuid',
|
|
67
|
+
constraints: false
|
|
68
|
+
});
|
|
69
|
+
tenantUser.belongsTo(user, {
|
|
70
|
+
targetKey: 'uuid',
|
|
71
|
+
constraints: false
|
|
72
|
+
});
|
|
47
73
|
}
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
tenantUser.associate = ({ user, tenantRole, tenantUser, tenantUserRole, tenantOrg, tenantUserOrg }) => {
|
|
51
|
-
tenantUser.belongsToMany(tenantRole, { through: tenantUserRole });
|
|
52
|
-
tenantUser.belongsToMany(tenantOrg, { through: tenantUserOrg });
|
|
53
|
-
tenantUser.belongsTo(user);
|
|
54
74
|
};
|
|
55
|
-
return tenantUser;
|
|
56
75
|
};
|
package/libs/models/tenant.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
id: {
|
|
6
|
-
type: DataTypes.
|
|
7
|
-
|
|
5
|
+
type: DataTypes.INTEGER,
|
|
6
|
+
autoIncrement: true,
|
|
8
7
|
primaryKey: true
|
|
9
8
|
},
|
|
9
|
+
uuid: {
|
|
10
|
+
type: DataTypes.UUID,
|
|
11
|
+
defaultValue: DataTypes.UUIDV4
|
|
12
|
+
},
|
|
10
13
|
name: {
|
|
11
14
|
type: DataTypes.STRING,
|
|
12
15
|
allowNull: false
|
|
@@ -31,8 +34,13 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
31
34
|
},
|
|
32
35
|
description: DataTypes.TEXT
|
|
33
36
|
},
|
|
34
|
-
{
|
|
35
|
-
|
|
37
|
+
options: {
|
|
38
|
+
indexes: [
|
|
39
|
+
{
|
|
40
|
+
unique: true,
|
|
41
|
+
fields: ['uuid', 'deleted_at']
|
|
42
|
+
}
|
|
43
|
+
]
|
|
36
44
|
}
|
|
37
|
-
|
|
45
|
+
};
|
|
38
46
|
};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
id: {
|
|
6
|
-
type: DataTypes.
|
|
7
|
-
|
|
5
|
+
type: DataTypes.INTEGER,
|
|
6
|
+
autoIncrement: true,
|
|
8
7
|
primaryKey: true
|
|
9
8
|
},
|
|
9
|
+
uuid: {
|
|
10
|
+
type: DataTypes.UUID,
|
|
11
|
+
defaultValue: DataTypes.UUIDV4
|
|
12
|
+
},
|
|
10
13
|
password: {
|
|
11
14
|
type: DataTypes.STRING,
|
|
12
15
|
allowNull: false
|
|
@@ -19,8 +22,13 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
19
22
|
type: DataTypes.UUID //用户修改密码时重新生成一条数据,该字段用于记录这个账号之前是属于哪个账号的
|
|
20
23
|
}
|
|
21
24
|
},
|
|
22
|
-
{
|
|
23
|
-
|
|
25
|
+
options: {
|
|
26
|
+
indexes: [
|
|
27
|
+
{
|
|
28
|
+
unique: true,
|
|
29
|
+
fields: ['uuid', 'deleted_at']
|
|
30
|
+
}
|
|
31
|
+
]
|
|
24
32
|
}
|
|
25
|
-
|
|
33
|
+
};
|
|
26
34
|
};
|
package/libs/models/user.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
id: {
|
|
6
|
-
type: DataTypes.
|
|
7
|
-
|
|
5
|
+
type: DataTypes.INTEGER,
|
|
6
|
+
autoIncrement: true,
|
|
8
7
|
primaryKey: true
|
|
9
8
|
},
|
|
9
|
+
uuid: {
|
|
10
|
+
type: DataTypes.UUID,
|
|
11
|
+
defaultValue: DataTypes.UUIDV4
|
|
12
|
+
},
|
|
10
13
|
nickname: DataTypes.STRING,
|
|
11
14
|
email: DataTypes.STRING,
|
|
12
15
|
phone: DataTypes.STRING,
|
|
@@ -26,25 +29,32 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
26
29
|
birthday: DataTypes.DATE,
|
|
27
30
|
description: DataTypes.TEXT
|
|
28
31
|
},
|
|
29
|
-
{
|
|
30
|
-
paranoid: true,
|
|
32
|
+
options: {
|
|
31
33
|
indexes: [
|
|
32
34
|
{
|
|
33
35
|
unique: true,
|
|
34
|
-
fields: ['
|
|
36
|
+
fields: ['uuid', 'deleted_at']
|
|
35
37
|
},
|
|
36
38
|
{
|
|
37
39
|
unique: true,
|
|
38
|
-
fields: ['
|
|
40
|
+
fields: ['email', 'deleted_at']
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
unique: true,
|
|
44
|
+
fields: ['phone', 'deleted_at']
|
|
39
45
|
}
|
|
40
46
|
]
|
|
47
|
+
},
|
|
48
|
+
associate: ({ adminRole, user, tenant, tenantUser }) => {
|
|
49
|
+
user.hasOne(adminRole, { foreignKey: 'userId', sourceKey: 'uuid', constraints: false });
|
|
50
|
+
user.belongsToMany(tenant, {
|
|
51
|
+
through: { model: tenantUser, unique: false },
|
|
52
|
+
otherKey: 'tenantId',
|
|
53
|
+
foreignKey: 'userId',
|
|
54
|
+
targetKey: 'uuid',
|
|
55
|
+
sourceKey: 'uuid',
|
|
56
|
+
constraints: false
|
|
57
|
+
});
|
|
41
58
|
}
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
user.associate = ({ adminRole, user, tenant, tenantUser }) => {
|
|
45
|
-
user.hasOne(adminRole, { foreignKey: 'userId' });
|
|
46
|
-
user.belongsToMany(tenant, { through: { model: tenantUser, unique: false } });
|
|
47
59
|
};
|
|
48
|
-
|
|
49
|
-
return user;
|
|
50
60
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
{
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
5
4
|
name: {
|
|
6
5
|
type: DataTypes.STRING,
|
|
7
6
|
allowNull: false
|
|
@@ -18,9 +17,6 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
18
17
|
type: DataTypes.INTEGER,
|
|
19
18
|
defaultValue: 0 //0:未验证,1:已验证,2:已过期
|
|
20
19
|
}
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
paranoid: true
|
|
24
20
|
}
|
|
25
|
-
|
|
21
|
+
};
|
|
26
22
|
};
|
package/libs/services/account.js
CHANGED
|
@@ -12,9 +12,10 @@ function userNameIsEmail(username) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
module.exports = fp(async (fastify, options) => {
|
|
15
|
+
const { models, services } = fastify.account;
|
|
15
16
|
const login = async ({ username, password, ip }) => {
|
|
16
17
|
const isEmail = userNameIsEmail(username);
|
|
17
|
-
const user = await
|
|
18
|
+
const user = await models.user.findOne({
|
|
18
19
|
where: Object.assign(
|
|
19
20
|
{},
|
|
20
21
|
isEmail
|
|
@@ -38,16 +39,23 @@ module.exports = fp(async (fastify, options) => {
|
|
|
38
39
|
|
|
39
40
|
await passwordAuthentication({ accountId: user.userAccountId, password });
|
|
40
41
|
|
|
41
|
-
await
|
|
42
|
-
userId: user.
|
|
42
|
+
await models.loginLog.create({
|
|
43
|
+
userId: user.uuid,
|
|
43
44
|
ip
|
|
44
45
|
});
|
|
45
46
|
|
|
46
|
-
return {
|
|
47
|
+
return {
|
|
48
|
+
token: fastify.jwt.sign({ payload: { id: user.uuid } }),
|
|
49
|
+
user: Object.assign({}, user.get({ plain: true }), { id: user.uuid })
|
|
50
|
+
};
|
|
47
51
|
};
|
|
48
52
|
|
|
49
53
|
const passwordAuthentication = async ({ accountId, password }) => {
|
|
50
|
-
const userAccount = await
|
|
54
|
+
const userAccount = await models.userAccount.findOne({
|
|
55
|
+
where: {
|
|
56
|
+
uuid: accountId
|
|
57
|
+
}
|
|
58
|
+
});
|
|
51
59
|
if (!userAccount) {
|
|
52
60
|
throw new Error('账号不存在');
|
|
53
61
|
}
|
|
@@ -69,22 +77,24 @@ module.exports = fp(async (fastify, options) => {
|
|
|
69
77
|
};
|
|
70
78
|
|
|
71
79
|
const resetPassword = async ({ userId, password }) => {
|
|
72
|
-
const userInfo = await
|
|
80
|
+
const userInfo = await models.user.findOne({
|
|
81
|
+
where: { uuid: userId }
|
|
82
|
+
});
|
|
73
83
|
if (!userInfo) {
|
|
74
84
|
throw new Error('用户不存在');
|
|
75
85
|
}
|
|
76
|
-
const account = await
|
|
86
|
+
const account = await models.userAccount.create(
|
|
77
87
|
Object.assign({}, await passwordEncryption(password), {
|
|
78
88
|
belongToUserId: userId
|
|
79
89
|
})
|
|
80
90
|
);
|
|
81
91
|
|
|
82
|
-
await userInfo.update({ userAccountId: account.
|
|
92
|
+
await userInfo.update({ userAccountId: account.uuid });
|
|
83
93
|
};
|
|
84
94
|
|
|
85
95
|
const register = async ({ avatar, nickname, gender, birthday, description, phone, email, code, password, status, invitationCode }) => {
|
|
86
96
|
const type = phone ? 0 : 1;
|
|
87
|
-
const verificationCode = await
|
|
97
|
+
const verificationCode = await models.verificationCode.findOne({
|
|
88
98
|
where: {
|
|
89
99
|
name: type === 0 ? phone : email,
|
|
90
100
|
type,
|
|
@@ -99,7 +109,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
99
109
|
verificationCode.status = 2;
|
|
100
110
|
await verificationCode.save();
|
|
101
111
|
|
|
102
|
-
return await
|
|
112
|
+
return await services.user.addUser({
|
|
103
113
|
avatar,
|
|
104
114
|
nickname,
|
|
105
115
|
gender,
|
|
@@ -117,7 +127,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
117
127
|
|
|
118
128
|
// 这里写发送逻辑
|
|
119
129
|
|
|
120
|
-
await
|
|
130
|
+
await models.verificationCode.update(
|
|
121
131
|
{
|
|
122
132
|
status: 2
|
|
123
133
|
},
|
|
@@ -130,7 +140,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
130
140
|
}
|
|
131
141
|
);
|
|
132
142
|
|
|
133
|
-
await
|
|
143
|
+
await models.verificationCode.create({
|
|
134
144
|
name: email,
|
|
135
145
|
type: 1,
|
|
136
146
|
code
|
|
@@ -140,7 +150,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
140
150
|
};
|
|
141
151
|
|
|
142
152
|
const verificationCodeValidate = async ({ name, type, code }) => {
|
|
143
|
-
const verificationCode = await
|
|
153
|
+
const verificationCode = await models.verificationCode.findOne({
|
|
144
154
|
where: {
|
|
145
155
|
name,
|
|
146
156
|
type,
|
|
@@ -165,7 +175,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
165
175
|
|
|
166
176
|
// 这里写发送逻辑
|
|
167
177
|
|
|
168
|
-
await
|
|
178
|
+
await models.verificationCode.update(
|
|
169
179
|
{
|
|
170
180
|
status: 2
|
|
171
181
|
},
|
|
@@ -178,7 +188,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
178
188
|
}
|
|
179
189
|
);
|
|
180
190
|
|
|
181
|
-
await
|
|
191
|
+
await models.verificationCode.create({
|
|
182
192
|
name: phone,
|
|
183
193
|
type: 0,
|
|
184
194
|
code
|
|
@@ -187,7 +197,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
187
197
|
return code;
|
|
188
198
|
};
|
|
189
199
|
|
|
190
|
-
|
|
200
|
+
services.account = {
|
|
191
201
|
login,
|
|
192
202
|
register,
|
|
193
203
|
sendEmailCode,
|