Browse Source

init org mng table structure

master
liujiangyong 2 years ago
parent
commit
a12409ff5b
  1. 140
      api/app/lib/controllers/auth/index.js
  2. 78
      api/app/lib/index.js
  3. 33
      api/app/lib/models/department.js
  4. 22
      api/app/lib/models/resource.js
  5. 84
      api/app/lib/models/user.js
  6. 52
      api/app/lib/models/user_resource.js
  7. 3
      api/app/lib/models/user_token.js
  8. 1
      script/1.0.0/data/1.admin_user.sql
  9. 75
      script/1.0.0/schema/1.init_inspection.sql

140
api/app/lib/controllers/auth/index.js

@ -6,113 +6,79 @@ const moment = require('moment');
const uuid = require('uuid'); const uuid = require('uuid');
async function login(ctx, next) { async function login(ctx, next) {
// const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const transaction = await ctx.fs.dc.orm.transaction();
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
const params = ctx.request.body; const params = ctx.request.body;
let password = Hex.stringify(MD5(params.password));
const userRes = await models.User.findOne({
where: {
username: params.username,
password: password,
delete: false,
enable: true
},
attributes: { exclude: ['password'] },
include: [{
attributes: ["resourceId"],
model: models.UserResource
}]
});
let userRes = null if (!userRes) {
if (params.username && params.password) { ctx.status = 400;
const password = Hex.stringify(MD5(params.password)); ctx.body = {
userRes = await models.User.findOne({ "message": "账号或密码错误"
attributes: { exclude: ['password'] },
where: {
userName: params.username,
password: password,
del: false,
},
include: [{
model: models.UserDepartment,
include: [{
model: models.Department,
attributes: ['id', 'name'],
include: [{
model: models.Company,
attributes: ['id', 'name'],
}]
}]
}, {
model: models.UserPost,
include: [{
model: models.Post,
attributes: ['id', 'name'],
}]
}, {
model: models.Role,
}]
});
} else if (params.phone && params.code) {
const record = await models.PhoneValidateCode.findOne({
where: {
phone: phone,
code: code
}
});
if (!record) {
throw '验证码错误'
} else if (record.expired < new Date()) {
throw '验证码已失效'
} }
userRes = await models.User.findOne({
attributes: { exclude: ['password'] },
where: {
tel: phone,
del: false,
},
include: [{
model: models.UserDepartment,
include: [{
model: models.Department,
attributes: ['id', 'name'],
include: [{
model: models.Company,
attributes: ['id', 'name'],
}]
}]
}, {
model: models.UserPost,
include: [{
model: models.Post,
attributes: ['id', 'name'],
}]
}, {
model: models.Role,
}]
});
} }
if (userRes) { if (userRes)
if (!userRes.state) { if (userRes && !userRes.enable) {
throw '用户已禁用' ctx.status = 400;
ctx.body = { message: "该用户已被禁用" }
} else { } else {
const token = uuid.v4(); const token = uuid.v4();
const userInfo = { let deptInfo = null;
if (userRes) {
const { departmentId } = userRes.dataValues;
deptInfo = await models.Department.findOne({
where: {
id: departmentId
}
})
}
if (!userRes) {
ctx.status = 400;
ctx.body = { message: "暂无登录权限,请联系管理员" }
return;
}
let userData = userRes.dataValues;
let userRslt = Object.assign(userData, {
authorized: true, authorized: true,
...userRes.dataValues, token: token,
token, userResources: userRes ? userRes.userResources.map(r => r.resourceId) : [],
}; type: deptInfo ? deptInfo.type : ''
// 记录token });
const expiredDay = 7;
const expired = moment().add(expiredDay, 'day').format('YYYY-MM-DD HH:mm:ss')
await models.UserToken.create({ await models.UserToken.create({
token: token, token: token,
userInfo: userInfo, userInfo: userRslt,
expired: expired expired: moment().add(30, 'days').format()
}); });
ctx.status = 200; ctx.status = 200;
ctx.body = userInfo; ctx.body = userRslt;
} }
} else { await transaction.commit();
throw '账号或密码错误'
}
// await transaction.commit();
} catch (error) { } catch (error) {
// await transaction.rollback(); await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: typeof error == 'string' ? error : '登录失败' "message": "登录失败"
} }
} }
} }

78
api/app/lib/index.js

@ -53,77 +53,17 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq
require(`./models/${filename}`)(dc) require(`./models/${filename}`)(dc)
}); });
const { Camera,Company, Department, Post, RoleGroup, Role, RoleResource, User, UserDepartment, UserPost, Site, ProjectDisclosure, ProjectDisclosureFiles, Coordinate, ProblemReport, ProblemReportFile, Worker, WorkerAttendance, const { Camera, Company, Department, Post, RoleGroup, Role, RoleResource, User, UserDepartment, UserPost, Site, ProjectDisclosure, ProjectDisclosureFiles, Coordinate, ProblemReport, ProblemReportFile, Worker, WorkerAttendance,
RiskReport, Metting, HideDangerRectify, HideDangerRectifySites, HideDangerDispose RiskReport, Metting, HideDangerRectify, HideDangerRectifySites, HideDangerDispose, UserResource, Resource
} = dc.models; } = dc.models;
Metting.belongsTo(User, { foreignKey: 'submitUser', targetKey: 'id' }); UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(Metting, { foreignKey: 'submitUser', sourceKey: 'id' }); User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
Department.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
Company.hasMany(Department, { foreignKey: 'companyId', sourceKey: 'id' }); Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });
Post.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Company.hasMany(Post, { foreignKey: 'companyId', sourceKey: 'id' }); Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' });
Post.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Department.hasMany(Post, { foreignKey: 'departmentId', sourceKey: 'id' });
Role.belongsTo(RoleGroup, { foreignKey: 'roleGroupId', targetKey: 'id' });
RoleGroup.hasMany(Role, { foreignKey: 'roleGroupId', sourceKey: 'id' });
RoleResource.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' });
Role.hasMany(RoleResource, { foreignKey: 'roleId', sourceKey: 'id' });
UserDepartment.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserDepartment, { foreignKey: 'userId', sourceKey: 'id' });
UserDepartment.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Department.hasMany(UserDepartment, { foreignKey: 'departmentId', sourceKey: 'id' });
UserPost.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserPost, { foreignKey: 'userId', sourceKey: 'id' });
UserPost.belongsTo(Post, { foreignKey: 'postId', targetKey: 'id' });
Post.hasMany(UserPost, { foreignKey: 'postId', sourceKey: 'id' });
User.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' });
Role.hasMany(User, { foreignKey: 'roleId', sourceKey: 'id' });
ProjectDisclosure.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' });
Site.hasMany(ProjectDisclosure, { foreignKey: 'siteId', sourceKey: 'id' });
ProjectDisclosure.belongsTo(User, { foreignKey: 'submiter', targetKey: 'id' });
User.hasMany(ProjectDisclosure, { foreignKey: 'submiter', sourceKey: 'id' });
ProjectDisclosureFiles.belongsTo(ProjectDisclosure, { foreignKey: 'projectDisclosureId', targetKey: 'id' });
ProjectDisclosure.hasMany(ProjectDisclosureFiles, { foreignKey: 'projectDisclosureId', sourceKey: 'id' });
ProblemReport.belongsTo(User, { foreignKey: 'reporter', sourceKey: 'id' });
ProblemReport.hasMany(ProblemReportFile, { foreignKey: 'reportId', sourceKey: 'id' });
ProblemReport.belongsTo(Site, { foreignKey: 'siteId', sourceKey: 'id' });
Site.hasMany(ProblemReport, { foreignKey: 'siteId', sourceKey: 'id' });
Site.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' });
Company.hasMany(Site, { foreignKey: 'companyId', sourceKey: 'id' });
Coordinate.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' });
Site.hasMany(Coordinate, { foreignKey: 'siteId', sourceKey: 'id' });
WorkerAttendance.belongsTo(Worker, { foreignKey: 'workerId', targetKey: 'id' });
Worker.hasMany(WorkerAttendance, { foreignKey: 'workerId', sourceKey: 'id' });
RiskReport.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' });
Site.hasMany(RiskReport, { foreignKey: 'siteId', sourceKey: 'id' });
HideDangerRectifySites.belongsTo(HideDangerRectify, { foreignKey: 'rectifyId', targetKey: 'id' });
HideDangerRectify.hasMany(HideDangerRectifySites, { foreignKey: 'rectifyId', sourceKey: 'id' });
HideDangerRectifySites.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' });
Site.hasMany(HideDangerRectifySites, { foreignKey: 'siteId', sourceKey: 'id' });
HideDangerDispose.belongsTo(HideDangerRectifySites, { foreignKey: 'rectifySiteId', targetKey: 'id' });
HideDangerRectifySites.hasMany(HideDangerDispose, { foreignKey: 'rectifySiteId', sourceKey: 'id' });
HideDangerDispose.belongsTo(User, { foreignKey: 'disposeUser', targetKey: 'id' });
User.hasMany(HideDangerDispose, { foreignKey: 'disposeUser', sourceKey: 'id' });
Camera.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' });
Site.hasMany(Camera, { foreignKey: 'siteId', sourceKey: 'id' });
}; };

33
api/app/lib/models/department.js

@ -24,35 +24,22 @@ module.exports = dc => {
field: "name", field: "name",
autoIncrement: false autoIncrement: false
}, },
companyId: { dependence: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "company_id",
autoIncrement: false,
references: {
key: "id",
model: "tCompany"
}
},
relateSites: {
type: DataTypes.ARRAY(DataTypes.INTEGER),
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: null, comment: "上级部门/从属",
primaryKey: false, primaryKey: false,
field: "relate_sites", field: "dependence",
autoIncrement: false autoIncrement: false
}, },
del: { type: {
type: DataTypes.BOOLEAN, type: DataTypes.INTEGER,
allowNull: true, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: "市1,区县2,乡镇3,村4",
primaryKey: false, primaryKey: false,
field: "del", field: "type",
autoIncrement: false autoIncrement: false
} }
}, { }, {
@ -60,6 +47,10 @@ module.exports = dc => {
comment: "", comment: "",
indexes: [] indexes: []
}); });
dc.models.Department = Department; dc.models.Department = Department;
return Department; return Department;
}; };

22
api/app/lib/models/resource.js

@ -5,24 +5,15 @@ module.exports = dc => {
const DataTypes = dc.ORM; const DataTypes = dc.ORM;
const sequelize = dc.orm; const sequelize = dc.orm;
const Resource = sequelize.define("resource", { const Resource = sequelize.define("resource", {
id: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: true,
field: "id",
autoIncrement: true,
unique: "resource_id_uindex"
},
code: { code: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: false, primaryKey: true,
field: "code", field: "code",
autoIncrement: false autoIncrement: false,
unique: "resource_code_uindex"
}, },
name: { name: {
type: DataTypes.STRING, type: DataTypes.STRING,
@ -31,15 +22,16 @@ module.exports = dc => {
comment: null, comment: null,
primaryKey: false, primaryKey: false,
field: "name", field: "name",
autoIncrement: false autoIncrement: false,
unique: "resource_name_uindex"
}, },
parentCode: { parentResource: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: false, primaryKey: false,
field: "parent_code", field: "parent_resource",
autoIncrement: false autoIncrement: false
} }
}, { }, {

84
api/app/lib/models/user.js

@ -15,49 +15,40 @@ module.exports = dc => {
autoIncrement: true, autoIncrement: true,
unique: "user_id_uindex" unique: "user_id_uindex"
}, },
photo: { name: {
type: DataTypes.TEXT,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "photo",
autoIncrement: false
},
userName: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: false, primaryKey: false,
field: "user_name", field: "name",
autoIncrement: false autoIncrement: false
}, },
password: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: "用户名 账号",
primaryKey: false, primaryKey: false,
field: "password", field: "username",
autoIncrement: false autoIncrement: false
}, },
displayName: { password: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: false, primaryKey: false,
field: "display_name", field: "password",
autoIncrement: false autoIncrement: false
}, },
tel: { departmentId: {
type: DataTypes.STRING, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: "部门id",
primaryKey: false, primaryKey: false,
field: "tel", field: "department_id",
autoIncrement: false autoIncrement: false
}, },
email: { email: {
@ -69,58 +60,40 @@ module.exports = dc => {
field: "email", field: "email",
autoIncrement: false autoIncrement: false
}, },
gender: { enable: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "gender",
autoIncrement: false
},
state: {
type: DataTypes.BOOLEAN, type: DataTypes.BOOLEAN,
allowNull: true, allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "state",
autoIncrement: false
},
roleId: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: null, defaultValue: null,
comment: null, comment: "启用状态",
primaryKey: false, primaryKey: false,
field: "role_id", field: "enable",
autoIncrement: false autoIncrement: false
}, },
relateSites: { delete: {
type: DataTypes.ARRAY(DataTypes.INTEGER), type: DataTypes.BOOLEAN,
allowNull: true, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: false, primaryKey: false,
field: "relate_sites", field: "delete",
autoIncrement: false autoIncrement: false
}, },
del: { phone: {
type: DataTypes.BOOLEAN, type: DataTypes.STRING,
allowNull: true, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: "手机号(小程序使用手机号登录)",
primaryKey: false, primaryKey: false,
field: "del", field: "phone",
autoIncrement: false autoIncrement: false
}, },
isSuper: { post: {
type: DataTypes.BOOLEAN, type: DataTypes.STRING,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: null, comment: "职位",
primaryKey: false, primaryKey: false,
field: "is_super", field: "post",
autoIncrement: false autoIncrement: false
} }
}, { }, {
@ -128,6 +101,7 @@ module.exports = dc => {
comment: "", comment: "",
indexes: [] indexes: []
}); });
dc.models.User = User; dc.models.User = User;
return User; return User;
}; };

52
api/app/lib/models/user_resource.js

@ -0,0 +1,52 @@
/* eslint-disable*/
'use strict';
module.exports = dc => {
const DataTypes = dc.ORM;
const sequelize = dc.orm;
const UserResource = sequelize.define("userResource", {
id: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: true,
field: "id",
autoIncrement: true,
unique: "post_resource_id_uindex"
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "user_id",
autoIncrement: false,
references: {
key: "id",
model: "post"
}
},
resourceId: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
field: "resource",
autoIncrement: false,
references: {
key: "code",
model: "resource"
}
}
}, {
tableName: "user_resource",
comment: "",
indexes: []
});
dc.models.UserResource = UserResource;
return UserResource;
};

3
api/app/lib/models/user_token.js

@ -1,5 +1,4 @@
/* eslint-disable*/ /* eslint-disable*/
'use strict'; 'use strict';
module.exports = dc => { module.exports = dc => {
@ -7,7 +6,7 @@ module.exports = dc => {
const sequelize = dc.orm; const sequelize = dc.orm;
const UserToken = sequelize.define("userToken", { const UserToken = sequelize.define("userToken", {
token: { token: {
type: DataTypes.STRING, type: DataTypes.UUIDV4,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,

1
script/1.0.0/data/1.admin_user.sql

@ -0,0 +1 @@
INSERT INTO "public"."user" VALUES (1, '管理员', 'SuperAdmin', 'e10adc3949ba59abbe56e057f20f883e', 1, NULL, 't', 'f', '123456789', NULL);

75
script/1.0.0/schema/1.init_inspection.sql

@ -0,0 +1,75 @@
DROP SEQUENCE if EXISTS "public"."user_id_seq";
CREATE SEQUENCE "public"."user_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
DROP TABLE IF EXISTS "public"."user";
CREATE TABLE "public"."user" (
"id" int4 NOT NULL DEFAULT nextval('user_id_seq'::regclass),
"name" varchar(64) COLLATE "pg_catalog"."default" NOT NULL,
"username" varchar(64) COLLATE "pg_catalog"."default" NOT NULL,
"password" varchar(512) COLLATE "pg_catalog"."default" NOT NULL,
"department_id" int4 NOT NULL,
"email" varchar(128) COLLATE "pg_catalog"."default",
"enable" bool NOT NULL DEFAULT true,
"delete" bool NOT NULL DEFAULT false,
"phone" varchar(20) COLLATE "pg_catalog"."default" NOT NULL,
"post" varchar(64) COLLATE "pg_catalog"."default"
);
COMMENT ON COLUMN "public"."user"."username" IS '用户名 账号';
COMMENT ON COLUMN "public"."user"."department_id" IS '部门id';
COMMENT ON COLUMN "public"."user"."enable" IS '启用状态';
COMMENT ON COLUMN "public"."user"."phone" IS '手机号(小程序使用手机号登录)';
COMMENT ON COLUMN "public"."user"."post" IS '职位';
DROP TABLE IF EXISTS "public"."resource";
CREATE TABLE "public"."resource" (
"code" varchar(128) COLLATE "pg_catalog"."default" NOT NULL,
"name" varchar(128) COLLATE "pg_catalog"."default" NOT NULL,
"parent_resource" varchar(128) COLLATE "pg_catalog"."default"
);
COMMENT ON TABLE "public"."resource" IS '权限字典';
DROP SEQUENCE if EXISTS "public"."user_resource_id_seq";
CREATE SEQUENCE "public"."user_resource_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
DROP TABLE IF EXISTS "public"."user_resource";
CREATE TABLE "public"."user_resource" (
"id" int4 NOT NULL DEFAULT nextval('user_resource_id_seq'::regclass),
"user_id" int4 NOT NULL,
"resource" varchar(128) COLLATE "pg_catalog"."default" NOT NULL
);
DROP TABLE IF EXISTS "public"."user_token";
CREATE TABLE "public"."user_token" (
"token" uuid NOT NULL,
"user_info" jsonb NOT NULL,
"expired" timestamptz(6) NOT NULL
);
DROP SEQUENCE if EXISTS "public"."department_id_seq";
CREATE SEQUENCE "public"."department_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
DROP TABLE IF EXISTS "public"."department";
CREATE TABLE "public"."department" (
"id" int4 NOT NULL DEFAULT nextval('department_id_seq'::regclass),
"name" varchar(128) COLLATE "pg_catalog"."default" NOT NULL,
"dependence" int4,
"type" int4 NOT NULL
);
COMMENT ON COLUMN "public"."department"."dependence" IS '上级部门/从属';
-- COMMENT ON COLUMN "public"."department"."type" IS '市1,区县2,乡镇3,村4';
Loading…
Cancel
Save