diff --git a/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js b/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js index f9c4937..1b14045 100644 --- a/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/controllers/camera/index.js @@ -1,12 +1,10 @@ 'use strict'; -async function get (ctx, next) { +async function getCameraProject (ctx, next) { try { const models = ctx.fs.dc.models; const { limit, page, orderBy, orderDirection } = ctx.query - const { userId } = ctx.fs.api - - const aa = await ctx.app.fs.axyRequest.get('structures/types', { query: { token: 'b11b7e6d-032e-4e3e-8973-931c2c5ada5a' } }) + const { userId, token } = ctx.fs.api let findOption = { attributes: { exclude: ['delete', 'recycleTime',] }, @@ -32,6 +30,55 @@ async function get (ctx, next) { } const cameraRes = await models.Camera.findAll(findOption) + const total = await models.Camera.count({ + where: findOption.where + }) + + // 查在安心云绑定的数据 + const cameraIds = cameraRes.map(c => { + return c.dataValues.id + }) + const axbindCameraRes = await ctx.app.fs.axyRequest.get('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } }) + + for (let { dataValues: camera } of cameraRes) { + const corBindCamera = axbindCameraRes.find(b => b.cameraId == camera.id) + if (corBindCamera) { + camera.station = corBindCamera.stations + } else { + camera.station = [] + } + } + + ctx.status = 200; + ctx.body = { + total: total, + data: cameraRes + } + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = {} + } +} + +async function getCamera (ctx) { + try { + const { models } = ctx.fs.dc; + const { cameraId } = ctx.query + + const cameraRes = await models.Camera.findAll({ + attributes: { exclude: ['delete', 'recycleTime',] }, + where: { + id: { $in: cameraId.split(',') } + }, + include: [{ + model: models.CameraAbility + }, { + model: models.CameraKind + }, { + model: models.Vender + }] + }) ctx.status = 200; ctx.body = cameraRes @@ -42,6 +89,55 @@ async function get (ctx, next) { } } +async function banned (ctx) { + try { + const { models } = ctx.fs.dc; + const data = ctx.request.body; + + // 向视频服务发送通知 + + // 库记录 + await models.Camera.update({ + forbidden: data.forbidden + }, { + where: { + id: data.cameraId + } + }) + + ctx.status = 204; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = {} + } +} + +async function del (ctx) { + try { + const { models } = ctx.fs.dc; + const { cameraId } = ctx.query + const { token } = ctx.fs.api + + await models.cameraId.destroy({ + where: { + id: cameraId + } + }) + + await ctx.app.fs.axyRequest.delete('vcmp/camera/project', { query: { token, cameraId: cameraId.join(',') } }) + + ctx.status = 204; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = {} + } +} + module.exports = { - get, + getCameraProject, + getCamera, + banned, + del, }; \ No newline at end of file diff --git a/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js b/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js index 56b58ba..b2902c5 100644 --- a/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js @@ -2,103 +2,128 @@ 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 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, - } + // 或取其他服务信息 + 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 }) - } + 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 = {} - } + 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 get (ctx) { - const models = ctx.fs.dc.models; - try { - const { limit, page, orderBy, orderDirection } = ctx.query - let findOption = { - attributes: { exclude: ['delete'] }, - where: { - delete: false, - }, - order: [ - [orderBy || 'id', orderDirection || 'DESC'] - ] - } - if (limit) { - findOption.limit = limit - } - if (page && limit) { - findOption.offset = page * limit - } + const models = ctx.fs.dc.models; + try { + const { userId, token } = ctx.fs.api + const { limit, page, orderBy, orderDirection } = ctx.query + let findOption = { + attributes: { exclude: ['delete'] }, + where: { + createUserId: userId, + delete: false, + }, + order: [ + [orderBy || 'id', orderDirection || 'DESC'] + ] + } + if (limit) { + findOption.limit = limit + } + if (page && limit) { + findOption.offset = page * limit + } - const res = await models.Nvr.findAll(findOption) - const total = await models.Nvr.count() + const res = await models.Nvr.findAll(findOption) + const total = await models.Nvr.count({ + where: findOption.where + }) - ctx.status = 200; - ctx.body = { - total: total, - data: res - } - } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); - ctx.status = 400; - ctx.body = {} - } + ctx.status = 200; + ctx.body = { + total: total, + data: res + } + } catch (error) { + 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 + const transaction = await ctx.fs.dc.orm.transaction(); + try { + const models = ctx.fs.dc.models; + const { userId, token } = ctx.fs.api + const { nvrId } = ctx.params - await models.Nvr.destroy({ - where: { - id: nvrId - } - }) + await models.Nvr.destroy({ + where: { + id: nvrId + }, + transaction + }) + + const cameraRes = await models.Camera.findAll({ + where: { + nvrId + } + }) + + const cameraIds = cameraRes.map(c => c.id) + + await models.Camera.destroy({ + where: { + nvrId, + } + }) + + await ctx.app.fs.axyRequest.delete('vcmp/camera/project', { query: { token, cameraId: cameraIds.join(',') } }) - ctx.status = 204; - } catch (error) { - ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); - ctx.status = 400; - ctx.body = {} - } + 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, - del, + edit, + get, + del, }; \ No newline at end of file diff --git a/code/VideoAccess-VCMP/api/app/lib/middlewares/paasRequest.js b/code/VideoAccess-VCMP/api/app/lib/middlewares/paasRequest.js deleted file mode 100644 index adde1cc..0000000 --- a/code/VideoAccess-VCMP/api/app/lib/middlewares/paasRequest.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; -const request = require('../service/request') - -function factory (app, opts) { - if (opts.pssaRequest) { - try { - for (let r of opts.pssaRequest) { - if (r.name && r.root) { - app.fs[r.name] = new request(r.root) - } else { - throw 'opts.pssaRequest 参数错误!' - } - } - } catch (error) { - console.error(error) - process.exit(-1); - } - } -} - -module.exports = factory; diff --git a/code/VideoAccess-VCMP/api/app/lib/models/vender.js b/code/VideoAccess-VCMP/api/app/lib/models/vender.js index 92e5474..f74bc23 100644 --- a/code/VideoAccess-VCMP/api/app/lib/models/vender.js +++ b/code/VideoAccess-VCMP/api/app/lib/models/vender.js @@ -29,6 +29,12 @@ module.exports = dc => { comment: "", indexes: [] }); + + const Camera = dc.models.Camera; + Camera.belongsTo(Vender, { foreignKey: 'venderId', targetKey: 'id' }); + Vender.hasMany(Camera, { foreignKey: 'venderId', sourceKey: 'id' }); + dc.models.Vender = Vender; + return Vender; }; \ No newline at end of file diff --git a/code/VideoAccess-VCMP/api/app/lib/routes/camera/index.js b/code/VideoAccess-VCMP/api/app/lib/routes/camera/index.js index fa97a76..931e683 100644 --- a/code/VideoAccess-VCMP/api/app/lib/routes/camera/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/routes/camera/index.js @@ -3,6 +3,15 @@ const camera = require('../../controllers/camera'); module.exports = function (app, router, opts) { - app.fs.api.logAttr['GET/camera'] = { content: '获取摄像头列表', visible: false }; - router.get('/camera', camera.get); + app.fs.api.logAttr['GET/camera/project'] = { content: '获取摄像头列表及项目绑定信息', visible: false }; + router.get('/camera/project', camera.getCameraProject); + + app.fs.api.logAttr['GET/camera'] = { content: '获取摄像头信息', visible: false }; + router.get('/camera', camera.getCamera); + + app.fs.api.logAttr['PUT/camera/banned'] = { content: '禁用摄像头', visible: false }; + router.put('/camera/banned', camera.banned); + + app.fs.api.logAttr['DEL/camera'] = { content: '删除摄像头', visible: false }; + router.delete('/camera', camera.del); }; diff --git a/code/VideoAccess-VCMP/api/app/lib/service/paasRequest.js b/code/VideoAccess-VCMP/api/app/lib/service/paasRequest.js index 7f91a9c..4d5edcb 100644 --- a/code/VideoAccess-VCMP/api/app/lib/service/paasRequest.js +++ b/code/VideoAccess-VCMP/api/app/lib/service/paasRequest.js @@ -20,27 +20,27 @@ class paasRequest { }; } - get = (url, { query, header = {} }) => { + get = (url, { query = {}, header = {} } = {}) => { return new Promise((resolve, reject) => { request.get(this.#buildUrl(url)).set(header).query(query).end(this.#resultHandler(resolve, reject)); }) } - post = (url, data, query) => { + post = (url, { data = {}, query = {}, header = {} } = {}) => { return new Promise((resolve, reject) => { - request.post(this.#buildUrl(url, this.root)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject)); + request.post(this.#buildUrl(url)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject)); }) } - put = (url, data) => { + put = (url, { data = {}, header = {}, query = {}, } = {}) => { return new Promise((resolve, reject) => { - request.put(this.#buildUrl(url)).set(setHeader()).send(data).end(this.#resultHandler(resolve, reject)); + request.put(this.#buildUrl(url)).set(header).query(query).send(data).end(this.#resultHandler(resolve, reject)); }) } - delete = (url) => { + delete = (url, { header = {} } = {}) => { return new Promise((resolve, reject) => { - request.delete(this.#buildUrl(url)).set(setHeader()).end(this.#resultHandler(resolve, reject)); + request.delete(this.#buildUrl(url)).set(header).end(this.#resultHandler(resolve, reject)); }) } }