8 changed files with 142 additions and 25 deletions
@ -0,0 +1,68 @@ |
|||
'use strict'; |
|||
const moment = require('moment') |
|||
|
|||
async function edit (ctx, next) { |
|||
const transaction = await ctx.fs.dc.orm.transaction(); |
|||
try { |
|||
const models = ctx.fs.dc.models; |
|||
const { userId } = ctx.fs.api |
|||
const data = ctx.request.body; |
|||
|
|||
// 或取其他服务信息
|
|||
const nvrData = { |
|||
channelCount: 8, |
|||
port: 8080, |
|||
} |
|||
|
|||
if (data.id) { |
|||
// 修改
|
|||
const storageData = Object.assign({}, data, nvrData) |
|||
await models.Nvr.update(storageData, { |
|||
where: { |
|||
id: data.id |
|||
}, |
|||
transaction |
|||
}) |
|||
} else { |
|||
// 添加
|
|||
const storageData = Object.assign({}, data, nvrData, { |
|||
createTime: moment().format(), |
|||
createUserId: userId, |
|||
delete: false, |
|||
}) |
|||
await models.Nvr.create(storageData, { transaction }) |
|||
} |
|||
|
|||
await transaction.commit(); |
|||
ctx.status = 204; |
|||
} catch (error) { |
|||
await transaction.rollback(); |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = {} |
|||
} |
|||
} |
|||
|
|||
async function del (ctx, next) { |
|||
try { |
|||
const models = ctx.fs.dc.models; |
|||
const { nvrId } = ctx.params |
|||
|
|||
await models.Nvr.destroy({ |
|||
where: { |
|||
id: nvrId |
|||
} |
|||
}) |
|||
|
|||
ctx.status = 204; |
|||
} catch (error) { |
|||
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|||
ctx.status = 400; |
|||
ctx.body = {} |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
edit, |
|||
del, |
|||
}; |
@ -0,0 +1,11 @@ |
|||
'use strict'; |
|||
|
|||
const nvr = require('../../controllers/nvr'); |
|||
|
|||
module.exports = function (app, router, opts) { |
|||
app.fs.api.logAttr['POST/nvr'] = { content: '添加/修改nvr', visible: false }; |
|||
router.post('/nvr', nvr.edit); |
|||
|
|||
app.fs.api.logAttr['DEL/nvr'] = { content: '删除nvr', visible: false }; |
|||
router.del('/nvr/:nvrId', nvr.del); |
|||
}; |
@ -0,0 +1,29 @@ |
|||
'use strict'; |
|||
const redis = require("ioredis") |
|||
const moment = require('moment') |
|||
|
|||
module.exports = async function factory (app, opts) { |
|||
let client = new redis(opts.redis.port, opts.redis.host); |
|||
|
|||
client.on("error", function (err) { |
|||
app.fs.logger.error('info', '[FS-AUTH-REDIS]', 'redis connect error.'); |
|||
console.error("Error :", err); |
|||
process.exit(-1); |
|||
}); |
|||
|
|||
client.on('connect', function () { |
|||
console.log(`redis connect success ${opts.redis.host + ':' + opts.redis.port}`); |
|||
}) |
|||
|
|||
// 自定义方法
|
|||
async function hdelall (key) { |
|||
const obj = await client.hgetall(key); |
|||
const hkeys = Object.keys(obj) |
|||
await client.hdel(key, hkeys) |
|||
} |
|||
|
|||
app.redis = client |
|||
app.redisTools = { |
|||
hdelall, |
|||
} |
|||
} |
Loading…
Reference in new issue