You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

200 lines
5.0 KiB

'use strict';
const fs = require('fs');
const moment = require('moment')
const uuid = require('uuid');
async function check (ctx) {
try {
const { models } = ctx.fs.dc;
const { authorization } = ctx.headers;
const { utils: { oauthParseAuthHeader, oauthParseBody } } = ctx.app.fs
const keySplit = await oauthParseAuthHeader(authorization);
const existRes = await models.Application.findOne({
where: {
appKey: keySplit[0],
appSecret: keySplit[1],
}
})
if (!existRes) {
throw new Error('应用不存在');
} else if (existRes.forbidden) {
throw new Error('应用已被禁用');
}
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = error
}
}
async function edit (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
const { models } = ctx.fs.dc;
const { userId } = ctx.fs.api
const data = ctx.request.body;
let findOption = { where: { name: data.name } }
if (data.appId) {
findOption.where.id = { $ne: data.appId }
}
const applicationRes = await models.Application.findOne(findOption)
if (applicationRes) {
throw '已有相同应用名称'
}
if (data.appId) {
// 修改
const storageData = Object.assign({}, data,)
await models.Application.update(storageData, {
where: {
id: data.appId
},
transaction
})
} else {
// 添加
const storageData = Object.assign({}, data, {
appKey: uuid.v4(),
appSecret: uuid.v4(),
createUserId: userId,
createTime: moment().format(),
forbidden: true
})
await models.Application.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 = {
message: typeof error == 'string' ? error : undefined
}
}
}
async function get (ctx) {
try {
const models = ctx.fs.dc.models;
const { userId, token } = ctx.fs.api
const { limit, page, orderBy, orderDirection } = ctx.query
let findOption = {
where: {
// createUserId: userId,
},
order: [
[orderBy || 'id', orderDirection || 'DESC'] //查询排序
],
}
if (limit) {
findOption.limit = limit
}
if (page && limit) {
findOption.offset = page * limit
}
const applicationRes = await models.Application.findAndCountAll(findOption)
let createUserIds = new Set()
let cameraIds = []
for (let c of applicationRes.rows) {
cameraIds.push(c.id)
createUserIds.add(c.createUserId)
}
// 查用户信息
const createUserRes = await ctx.app.fs.authRequest.get(`user/${[...createUserIds].join(',') || -1}/message`, { query: { token } })
for (let { dataValues: n } of applicationRes.rows) {
const corCreateUser = createUserRes.find(u => u.id == n.createUserId)
n.createUser = {
name: corCreateUser ? corCreateUser.username : ''
}
}
ctx.status = 200;
ctx.body = {
total: applicationRes.count,
data: applicationRes.rows
}
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {}
}
}
async function put (ctx) {
try {
const { models } = ctx.fs.dc;
const data = ctx.request.body;
// TODO 向视频服务发送通知
// 库记录
await models.Application.update({
forbidden: data.forbidden
}, {
where: {
id: data.appId
}
})
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {}
}
}
async function del (ctx, next) {
const transaction = await ctx.fs.dc.orm.transaction();
try {
const models = ctx.fs.dc.models;
const { token } = ctx.fs.api
const { appId } = ctx.params
const { appKey } = await models.Application.findOne({
where: {
id: appId
},
}) || {}
await models.Application.destroy({
where: {
id: appId
},
transaction
})
await ctx.app.fs.authRequest.delete(`oauth2/token/invalidate_all`, {
query: { token, appKey }
})
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 = {}
}
}
module.exports = {
edit,
get,
put,
del,
check,
};