|
@@ -1,31 +1,18 @@
|
|
|
/**
|
|
|
- * 用户数据模型
|
|
|
+ * 用户业务模型
|
|
|
*
|
|
|
* @author CaiAoLin
|
|
|
* @date 2017/6/9
|
|
|
* @version
|
|
|
*/
|
|
|
let mongoose = require("mongoose");
|
|
|
-let dbm = require("../../../config/db/db_manager");
|
|
|
+let MongooseHelper = require("../libraries/database/mongoose_helper");
|
|
|
+let userSchema = require("./schema/user");
|
|
|
let Request = require("request");
|
|
|
|
|
|
class UserModel {
|
|
|
|
|
|
/**
|
|
|
- * 表名
|
|
|
- *
|
|
|
- * @var {string}
|
|
|
- */
|
|
|
- collectionName = 'user';
|
|
|
-
|
|
|
- /**
|
|
|
- * 模型
|
|
|
- *
|
|
|
- * @var {object}
|
|
|
- */
|
|
|
- model = null;
|
|
|
-
|
|
|
- /**
|
|
|
* db句柄
|
|
|
*
|
|
|
* @var {object}
|
|
@@ -33,13 +20,6 @@ class UserModel {
|
|
|
db = null;
|
|
|
|
|
|
/**
|
|
|
- * 最后选定的数据结构
|
|
|
- *
|
|
|
- * @var {object}
|
|
|
- */
|
|
|
- schema = null;
|
|
|
-
|
|
|
- /**
|
|
|
* 企业所在地区
|
|
|
*
|
|
|
* @var {object}
|
|
@@ -106,39 +86,13 @@ class UserModel {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * 保存用户详情结构
|
|
|
- *
|
|
|
- * @var {object}
|
|
|
- */
|
|
|
- saveInfoSchema = {
|
|
|
- real_name: {
|
|
|
- type: String,
|
|
|
- required: true
|
|
|
- },
|
|
|
- company: {
|
|
|
- type: String,
|
|
|
- required: true
|
|
|
- },
|
|
|
- province: {
|
|
|
- type: String,
|
|
|
- required: true
|
|
|
- },
|
|
|
- area: {
|
|
|
- type: Number,
|
|
|
- required: true
|
|
|
- },
|
|
|
- company_type: Number,
|
|
|
- company_scale: Number,
|
|
|
- };
|
|
|
-
|
|
|
- /**
|
|
|
* 构造函数
|
|
|
*
|
|
|
* @return {void}
|
|
|
*/
|
|
|
constructor() {
|
|
|
- this.db = dbm.getCfgConnection('scConstruct');
|
|
|
- this.schema = new mongoose.Schema(this.defaultSchema);
|
|
|
+ this.db = new MongooseHelper();
|
|
|
+ this.db.model = userSchema;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -181,17 +135,16 @@ class UserModel {
|
|
|
async markUser(userData) {
|
|
|
let userDataFromDb = await this.findDataByName(userData.username);
|
|
|
let result = false;
|
|
|
+
|
|
|
// 信息是否补全
|
|
|
let info = false;
|
|
|
if (userDataFromDb.length <= 0) {
|
|
|
// 不存在用户则入库
|
|
|
- this.setScene('login');
|
|
|
result = await this.addUser(userData);
|
|
|
} else {
|
|
|
// 存在则更新用户信息
|
|
|
let updateData = {last_login: new Date().getTime()};
|
|
|
let condition = {email: userData.email};
|
|
|
- this.setScene('updateTime');
|
|
|
result = await this.updateUser(condition, updateData);
|
|
|
|
|
|
userDataFromDb = userDataFromDb[0];
|
|
@@ -208,22 +161,14 @@ class UserModel {
|
|
|
* @param {string} scene
|
|
|
*/
|
|
|
setScene(scene = '') {
|
|
|
- let schema = {};
|
|
|
switch (scene) {
|
|
|
case 'saveInfo':
|
|
|
- schema = this.saveInfoSchema;
|
|
|
- break;
|
|
|
- case 'updateTime':
|
|
|
- schema = {last_login: Number, real_name: String};
|
|
|
- break;
|
|
|
- case 'login':
|
|
|
- schema = this.loginSchema;
|
|
|
- break;
|
|
|
- default:
|
|
|
- schema = this.defaultSchema;
|
|
|
+ this.db.model.schema.path('real_name').required(true);
|
|
|
+ this.db.model.schema.path('company').required(true);
|
|
|
+ this.db.model.schema.path('province').required(true);
|
|
|
+ this.db.model.schema.path('area').required(true);
|
|
|
break;
|
|
|
}
|
|
|
- this.schema = new mongoose.Schema(schema);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -233,16 +178,7 @@ class UserModel {
|
|
|
* @return {object}
|
|
|
*/
|
|
|
findDataByName(username) {
|
|
|
- let userModel = this.db.model(this.collectionName, this.schema);
|
|
|
- return new Promise(function (resolve, reject) {
|
|
|
- userModel.find({username: username}, function (error, data) {
|
|
|
- if (error) {
|
|
|
- reject(error);
|
|
|
- } else {
|
|
|
- resolve(data);
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
+ return this.db.find({username: username});
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -262,19 +198,10 @@ class UserModel {
|
|
|
company_type: -1,
|
|
|
company_scale: -1,
|
|
|
last_login: 0,
|
|
|
- create_time: new Date().getTime()
|
|
|
+ create_time: new Date().getTime(),
|
|
|
+ area: 0
|
|
|
};
|
|
|
- let userModel = this.db.model(insertData, this.schema);
|
|
|
- return new Promise(function (resolve, reject) {
|
|
|
- userModel.save(function (error) {
|
|
|
- if (error) {
|
|
|
- console.log(error);
|
|
|
- reject(false);
|
|
|
- } else {
|
|
|
- resolve(true);
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
+ return this.db.create(insertData);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -287,23 +214,7 @@ class UserModel {
|
|
|
if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
|
|
|
return null;
|
|
|
}
|
|
|
- let userModel = this.db.model('users', this.schema);
|
|
|
-
|
|
|
- return new Promise(function(resolve, reject) {
|
|
|
- // 验证数据
|
|
|
- let model = new userModel(updateData);
|
|
|
- let validationError = model.validateSync();
|
|
|
- if (validationError) {
|
|
|
- reject(validationError);
|
|
|
- }
|
|
|
- userModel.update(condition, {$set: updateData}, function(error, result) {
|
|
|
- if (error) {
|
|
|
- reject(error);
|
|
|
- } else {
|
|
|
- resolve(result);
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
+ return this.db.update(condition, updateData);
|
|
|
}
|
|
|
|
|
|
}
|