irc_dicom_service/init.js

156 lines
3.6 KiB
JavaScript

const mongoose = require('mongoose');
const bcrypt = require("bcryptjs");
// let mongodbconfig = {
// client: {
// url: `mongodb://rayplus_admin:rayplus1234@127.0.0.1:27017/rayplus_cms`,
// options: {
// autoReconnect: true,
// socketTimeoutMS: 0,
// keepAlive: true,
// useNewUrlParser: true,
// useCreateIndex: true,
// reconnectTries: 30
// }
// }
// }
let mongodbconfig = {
client: {
// url: `mongodb://rayplus_admin:rayplus1234@192.168.1.50:27017/rayplus`,
// url: `mongodb://mongouser:mongo123456@192.168.3.69:27017/rayplus`,
url: `mongodb://mongouser:mongo123456@192.168.3.68:27017/?authSource=admin`,
// url: `mongodb://rayplus_admin:rayplus1234@192.168.1.25:27017/rayplus`,
// url: `mongodb://rayplus_admin:rayplus1234@127.0.0.1:27017/rayplus`,
options: {
socketTimeoutMS: 0,
keepAlive: true,
useNewUrlParser: true,
}
}
};
var Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String,
lowercase: true,
trim: true,
index: true
},
password: {
type: String,
require: true
}, // 密码
name: String, //用户姓名
phone: String, //手机号
email: String, // 邮箱
nickname: String, //别名
sex: {
type: String,
enum: ['0', '1', '2'],
default: '0'
}, //微信用户性别 0未知 1男性 2女性
role: {
type: String,
enum: ["0", "1", "2", "6", "9"],
default: "2"
}, //角色 0保留 1普通医生 2管理员医生 6医院最高管理员 9项目最高管理员
status: {
type: String,
enum: ["0", "1", "2", "4", "9"],
default: "2"
}, // 用户账号状态 0保留 1未激活 2已激活 4 9已删除
loginAttempts: {
type: Number,
default: 0
}, // 登录错误试图次数
lockUntil: {
type: Number
}, // 锁定账号的截至时间
meta: {
createdAt: {
type: Date,
default: new Date()
},
updatedAt: {
type: Date,
default: new Date()
}
}
});
UserSchema.pre("save", function (next) {
const user = this;
if (!user.isModified("password")) return next();
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(user.password, salt);
user.password = hash;
next();
});
UserSchema.pre("save", function (next) {
if (this.isNew) {
// 创建时间
this.meta.createdAt = this.meta.updatedAt = new Date();
} else {
this.meta.updatedAt = new Date();
}
next();
});
const User = mongoose.model('User', UserSchema)
// const Title = mongoose.model('Title', TitleSchema)
class Init {
constructor() {
this.connection = null;
this.User = User;
this.start()
}
async start() {
await this.connect(); //建立连接
try {
await this.resetDB();
await this.addAdmin()
console.log('congratulations!');
} catch (error) {
console.error(error)
}
process.exit();
}
async connect() {
try {
this.connection = await mongoose.connect(mongodbconfig.client.url, mongodbconfig.client.options);
} catch (e) {
console.console.error('db connect error')
}
}
async addAdmin() {
try {
let user = new User({
username: 'rayplus_cms',
password: 'rjkjCms',
role: '9'
});
await user.save();
} catch (e) {
console.error('mongodb save error', e)
}
console.log('add the user completes');
}
async resetDB() {
//第一步删除所有表除了users表
try {
await mongoose.connection.db.dropDatabase();
console.log('database drop success');
} catch (e) {
console.error('mongodb error:', e)
}
}
}
new Init()