init
This commit is contained in:
90
src/modules/system/system.controller.js
Normal file
90
src/modules/system/system.controller.js
Normal file
@@ -0,0 +1,90 @@
|
||||
const System = require('./system.model');
|
||||
const _cache = {};
|
||||
const provider = {
|
||||
getValue: async function (key) {
|
||||
if (!_cache[key]) {
|
||||
await System.findOne({ key }).then(data => {
|
||||
_cache[key] = data.value;
|
||||
}).catch(() => {
|
||||
mylog.error('unexpected key', key);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
return _cache[key];
|
||||
}
|
||||
};
|
||||
module.exports = {
|
||||
provider,
|
||||
getConfig: function (req, res) {
|
||||
const { key } = req.query;
|
||||
if (!_cache[key]) {
|
||||
System.findOne({ key }).then(data => {
|
||||
_cache[key] = data.value;
|
||||
res.json({
|
||||
code: 0,
|
||||
data
|
||||
});
|
||||
}).catch(err => {
|
||||
res.json({
|
||||
code: 1,
|
||||
err
|
||||
});
|
||||
});
|
||||
} else res.json({
|
||||
code: 0,
|
||||
data: _cache[key]
|
||||
});
|
||||
},
|
||||
publishConfig: async function (req, res) {
|
||||
const { key, value, option } = req.body;
|
||||
const isExist = await System.exists({ key });
|
||||
if (!isExist) {
|
||||
new System({ key, value, option }).save()
|
||||
.then(data => {
|
||||
_cache[key] = data.value;
|
||||
res.json({
|
||||
code: 0,
|
||||
msg: 'success',
|
||||
data
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
res.json({
|
||||
code: 2,
|
||||
err
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.json({
|
||||
code: 1,
|
||||
msg: 'key already existed'
|
||||
});
|
||||
}
|
||||
},
|
||||
updateConfig: async function (req, res) {
|
||||
const { key, value, option } = req.body;
|
||||
const isExist = await System.exists({ key });
|
||||
if (isExist) {
|
||||
System.findOneAndUpdate({ key }, { value, option }, { new: true })
|
||||
.then(data => {
|
||||
_cache[key] = data.value;
|
||||
res.json({
|
||||
code: 0,
|
||||
msg: 'success',
|
||||
data
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
res.json({
|
||||
code: 2,
|
||||
err
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.json({
|
||||
code: 1,
|
||||
err: "key doesn't existed"
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
26
src/modules/system/system.model.js
Normal file
26
src/modules/system/system.model.js
Normal file
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
const SystemSchema = new mongoose.Schema({
|
||||
key: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
value: {
|
||||
type: {},
|
||||
required: true
|
||||
},
|
||||
option: {
|
||||
type: {}
|
||||
},
|
||||
}, {
|
||||
timestamps: {
|
||||
createdAt: 'createdAt',
|
||||
updatedAt: 'updatedAt'
|
||||
}
|
||||
});
|
||||
|
||||
const systemSchema = mongoose.model('System', SystemSchema);
|
||||
|
||||
module.exports = systemSchema;
|
||||
25
src/modules/system/system.service.js
Normal file
25
src/modules/system/system.service.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const indexRoute = '/system';
|
||||
const controller = require('./system.controller');
|
||||
const services = [
|
||||
{
|
||||
url: '/get',
|
||||
method: 'GET',
|
||||
controller: controller.getConfig
|
||||
},
|
||||
{
|
||||
url: '/publish',
|
||||
method: 'POST',
|
||||
controller: controller.publishConfig
|
||||
},
|
||||
{
|
||||
url: '/update',
|
||||
method: 'POST',
|
||||
controller: controller.updateConfig
|
||||
},
|
||||
];
|
||||
|
||||
module.exports = services.map(service => {
|
||||
// 可以在此处对要导出的服务map进行统一处理
|
||||
service.url = indexRoute + service.url;
|
||||
return service;
|
||||
});
|
||||
Reference in New Issue
Block a user