Browse Source

init org mng table structure

master
liujiangyong 2 years ago
parent
commit
a12409ff5b
  1. 132
      api/app/lib/controllers/auth/index.js
  2. 76
      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

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

@ -6,113 +6,79 @@ const moment = require('moment');
const uuid = require('uuid');
async function login(ctx, next) {
// const transaction = await ctx.fs.dc.orm.transaction();
try {
const transaction = await ctx.fs.dc.orm.transaction();
const models = ctx.fs.dc.models;
const params = ctx.request.body;
let userRes = null
if (params.username && params.password) {
const password = Hex.stringify(MD5(params.password));
userRes = await models.User.findOne({
attributes: { exclude: ['password'] },
let password = Hex.stringify(MD5(params.password));
const userRes = await models.User.findOne({
where: {
userName: params.username,
username: params.username,
password: password,
del: false,
delete: false,
enable: true
},
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,
attributes: ["resourceId"],
model: models.UserResource
}]
});
if (!userRes) {
ctx.status = 400;
ctx.body = {
"message": "账号或密码错误"
}
}
if (userRes) {
if (!userRes.state) {
throw '用户已禁用'
if (userRes)
if (userRes && !userRes.enable) {
ctx.status = 400;
ctx.body = { message: "该用户已被禁用" }
} else {
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,
...userRes.dataValues,
token,
};
// 记录token
const expiredDay = 7;
const expired = moment().add(expiredDay, 'day').format('YYYY-MM-DD HH:mm:ss')
token: token,
userResources: userRes ? userRes.userResources.map(r => r.resourceId) : [],
type: deptInfo ? deptInfo.type : ''
});
await models.UserToken.create({
token: token,
userInfo: userInfo,
expired: expired
userInfo: userRslt,
expired: moment().add(30, 'days').format()
});
ctx.status = 200;
ctx.body = userInfo;
ctx.body = userRslt;
}
} else {
throw '账号或密码错误'
}
// await transaction.commit();
await transaction.commit();
} catch (error) {
// await transaction.rollback();
await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
message: typeof error == 'string' ? error : '登录失败'
"message": "登录失败"
}
}
}

76
api/app/lib/index.js

@ -54,76 +54,16 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq
});
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;
Metting.belongsTo(User, { foreignKey: 'submitUser', targetKey: 'id' });
User.hasMany(Metting, { foreignKey: 'submitUser', sourceKey: 'id' });
UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
Department.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' });
Company.hasMany(Department, { foreignKey: 'companyId', sourceKey: 'id' });
UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });
Post.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' });
Company.hasMany(Post, { foreignKey: 'companyId', 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' });
User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' });
};

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

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

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

@ -5,24 +5,15 @@ module.exports = dc => {
const DataTypes = dc.ORM;
const sequelize = dc.orm;
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: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: null,
comment: null,
primaryKey: false,
primaryKey: true,
field: "code",
autoIncrement: false
autoIncrement: false,
unique: "resource_code_uindex"
},
name: {
type: DataTypes.STRING,
@ -31,15 +22,16 @@ module.exports = dc => {
comment: null,
primaryKey: false,
field: "name",
autoIncrement: false
autoIncrement: false,
unique: "resource_name_uindex"
},
parentCode: {
parentResource: {
type: DataTypes.STRING,
allowNull: true,
defaultValue: null,
comment: null,
primaryKey: false,
field: "parent_code",
field: "parent_resource",
autoIncrement: false
}
}, {

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

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