|
@@ -6,11 +6,42 @@
|
|
* @version
|
|
* @version
|
|
*/
|
|
*/
|
|
import BaseModel from "../../common/base/base_model";
|
|
import BaseModel from "../../common/base/base_model";
|
|
|
|
+import SettingType from "../../common/const/setting_type_const";
|
|
import SettingSchema from "./schema/setting";
|
|
import SettingSchema from "./schema/setting";
|
|
|
|
|
|
class SettingModel extends BaseModel {
|
|
class SettingModel extends BaseModel {
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 偏好设置默认
|
|
|
|
+ *
|
|
|
|
+ * @var {Object}
|
|
|
|
+ */
|
|
|
|
+ preferenceDefault = {
|
|
|
|
+ login_ask: 1,
|
|
|
|
+ select_version: -1
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置场景
|
|
|
|
+ *
|
|
|
|
+ * @param {string} scene
|
|
|
|
+ * @return {void}
|
|
|
|
+ */
|
|
|
|
+ setScene(scene = '') {
|
|
|
|
+ switch (scene) {
|
|
|
|
+ // 新增数据的验证规则
|
|
|
|
+ case 'add':
|
|
|
|
+ this.model.schema.path('type').required(true);
|
|
|
|
+ this.model.schema.path('data').required(true);
|
|
|
|
+ this.model.schema.path('user_id').required(true);
|
|
|
|
+ break;
|
|
|
|
+ case 'update':
|
|
|
|
+ this.model.schema.path('data').required(true);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 构造函数
|
|
* 构造函数
|
|
*/
|
|
*/
|
|
constructor() {
|
|
constructor() {
|
|
@@ -24,7 +55,7 @@ class SettingModel extends BaseModel {
|
|
*
|
|
*
|
|
* @param {String} userId
|
|
* @param {String} userId
|
|
* @param {Number} type
|
|
* @param {Number} type
|
|
- * @return Promise
|
|
|
|
|
|
+ * @return {Promise}
|
|
*/
|
|
*/
|
|
async getSetting(userId, type) {
|
|
async getSetting(userId, type) {
|
|
let condition = {
|
|
let condition = {
|
|
@@ -33,7 +64,69 @@ class SettingModel extends BaseModel {
|
|
};
|
|
};
|
|
|
|
|
|
let settingData = await this.findDataByCondition(condition);
|
|
let settingData = await this.findDataByCondition(condition);
|
|
- return settingData.length > 0 ? settingData : [];
|
|
|
|
|
|
+ return settingData !== null && Object.keys(settingData).length > 0 ?
|
|
|
|
+ settingData.data : {};
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保存设置
|
|
|
|
+ *
|
|
|
|
+ * @param {String} userId
|
|
|
|
+ * @param {Number} type
|
|
|
|
+ * @param {Object} data
|
|
|
|
+ * @return {Promise}
|
|
|
|
+ */
|
|
|
|
+ async save(userId, type, data) {
|
|
|
|
+ let result = false;
|
|
|
|
+ if (typeof data === 'string' && data === '') {
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ if (typeof data === 'object' && Object.keys(data).length <= 0) {
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 查找是否有对应数据
|
|
|
|
+ let condition = {
|
|
|
|
+ user_id: userId,
|
|
|
|
+ type: type
|
|
|
|
+ };
|
|
|
|
+ let counter = await this.db.count(condition);
|
|
|
|
+ let saveData = {};
|
|
|
|
+
|
|
|
|
+ if (counter > 0) {
|
|
|
|
+ // 存在则更新
|
|
|
|
+ saveData = {
|
|
|
|
+ data: data
|
|
|
|
+ };
|
|
|
|
+ this.setScene('add');
|
|
|
|
+ let updateResult = await this.db.update(condition, saveData);
|
|
|
|
+ result = updateResult.ok === 1;
|
|
|
|
+ } else {
|
|
|
|
+ saveData = {
|
|
|
|
+ user_id: userId,
|
|
|
|
+ type: type,
|
|
|
|
+ data: data,
|
|
|
|
+ create_time: new Date().getTime()
|
|
|
|
+ };
|
|
|
|
+ this.setScene('update');
|
|
|
|
+ let insertResult = await this.db.create(saveData);
|
|
|
|
+ result = Object.keys(insertResult).length > 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取个人偏好设置
|
|
|
|
+ *
|
|
|
|
+ * @param {String} userId
|
|
|
|
+ * @return {Promise}
|
|
|
|
+ */
|
|
|
|
+ async getPreferenceSetting(userId) {
|
|
|
|
+ let setting = await this.getSetting(userId, SettingType.PREFERENCE);
|
|
|
|
+ setting = Object.keys(setting).length <= 0 ? this.preferenceDefault : setting;
|
|
|
|
+
|
|
|
|
+ return setting;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|