From 590ba7e7b7c5edcd390f33b37ad87a2e161a8172 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Wed, 14 Sep 2022 16:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/organization/index.js | 51 ++++++++++++++++++- api/app/lib/controllers/project/bind.js | 23 +++++++++ api/app/lib/controllers/project/index.js | 3 +- api/app/lib/index.js | 6 ++- api/app/lib/routes/organization/index.js | 3 ++ api/app/lib/routes/project/index.js | 5 ++ api/app/lib/service/clickHouseClient.js | 19 +++++-- 7 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 api/app/lib/controllers/project/bind.js diff --git a/api/app/lib/controllers/organization/index.js b/api/app/lib/controllers/organization/index.js index 9284f47..d246b15 100644 --- a/api/app/lib/controllers/organization/index.js +++ b/api/app/lib/controllers/organization/index.js @@ -33,6 +33,10 @@ async function editUser (ctx) { pepUserId } }) + if (existUserRes && !existUserRes.deleted && !pepUserId) { + // 新增已存在未删除 + throw '人员账号已添加' + } let storageData = { pepUserId, @@ -81,8 +85,8 @@ async function putUser (ctx) { } }) - if (existUserRes && existUserRes.role.includes('admin')) { - throw '已是管理员,请先解除管理员权限' + if (existUserRes && existUserRes.role.includes('admin') && disabled) { + throw '成员不能既是管理员又是普通成员' } const updateData = { @@ -110,9 +114,43 @@ async function putUser (ctx) { } } +async function delAdmin (ctx) { + try { + const models = ctx.fs.dc.models; + const { pomsUserId } = ctx.params + + const existUserRes = await models.User.findOne({ + where: { + id: pomsUserId + } + }) + if (existUserRes) { + let updateValues = existUserRes.dataValues + let adminIndex = updateValues.role.findIndex(r => r == 'admin') + if (adminIndex > -1) { + updateValues.role.splice(adminIndex, 1) + } + await models.User.update(updateValues, { + where: { + id: pomsUserId + } + }) + } + + ctx.status = 204; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + async function user (ctx) { try { const models = ctx.fs.dc.models; + const { clickHouse } = ctx.app.fs const { role, limit, page, } = ctx.query const excludeField = ['lastInTime', 'inTimes', 'onlineDuration', 'lastInAddress', 'deleted', 'updateTime'] @@ -151,6 +189,14 @@ async function user (ctx) { order: [['updateTime', 'DESC']] }) + // let userIds = new Set() + // for (let u of userRes.rows.concat(adminRes)) { + // userIds.add(u.pepUserId) + // } + // let userPepRes = await clickHouse.pepEmis.query(`SELECT "user"."name" FROM department_user LEFT JOIN user ON department_user.user=user.id `).toPromise() + + // const userDepRes = await clickHouse.pepEmis.query(`SELECT * FROM department`).toPromise() + ctx.status = 200 ctx.body = { admin: adminRes, @@ -169,5 +215,6 @@ module.exports = { allDeps, editUser, putUser, + delAdmin, user, }; \ No newline at end of file diff --git a/api/app/lib/controllers/project/bind.js b/api/app/lib/controllers/project/bind.js new file mode 100644 index 0000000..f11651e --- /dev/null +++ b/api/app/lib/controllers/project/bind.js @@ -0,0 +1,23 @@ +'use strict'; + +async function bindAnxin2pep (ctx) { + try { + const models = ctx.fs.dc.models; + const { clickHouse } = ctx.app.fs + const { name } = ctx.request.body + + + ctx.status = 20; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: error`); + ctx.status = 400; + ctx.body = { + message: typeof error == 'string' ? error : undefined + } + } +} + + +module.exports = { + bindAnxin2pep +}; \ No newline at end of file diff --git a/api/app/lib/controllers/project/index.js b/api/app/lib/controllers/project/index.js index 744b3b7..df0008f 100644 --- a/api/app/lib/controllers/project/index.js +++ b/api/app/lib/controllers/project/index.js @@ -20,6 +20,7 @@ async function appList (ctx) { } } + module.exports = { - appList + appList, }; \ No newline at end of file diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 34059d5..2862fce 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -9,6 +9,7 @@ const socketConect = require('./service/socket') const mqttVideoServer = require('./service/mqttServer') const paasRequest = require('./service/paasRequest'); const authenticator = require('./middlewares/authenticator'); +const clickHouseClient = require('./service/clickHouseClient') const schedule = require('./schedule') // const apiLog = require('./middlewares/api-log'); @@ -28,6 +29,9 @@ module.exports.entry = function (app, router, opts) { // 实例其他平台请求方法 paasRequest(app, opts) + // clickHouse 数据库 client + clickHouseClient(app, opts) + // 工具类函数 utils(app, opts) @@ -51,7 +55,7 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq }); const { - + } = dc.models; }; diff --git a/api/app/lib/routes/organization/index.js b/api/app/lib/routes/organization/index.js index d7010d9..48e17e6 100644 --- a/api/app/lib/routes/organization/index.js +++ b/api/app/lib/routes/organization/index.js @@ -12,6 +12,9 @@ module.exports = function (app, router, opts) { app.fs.api.logAttr['PUT/organization/user/:pomsUserId'] = { content: '修改成员状态', visible: true }; router.put('/organization/user/:pomsUserId', organization.putUser); + app.fs.api.logAttr['DEL/organization/admin/:pomsUserId'] = { content: '删除管理员', visible: true }; + router.delete('/organization/admin/:pomsUserId', organization.delAdmin); + app.fs.api.logAttr['GET/organization/user'] = { content: '获取成员列表', visible: true }; router.get('/organization/user', organization.user); }; diff --git a/api/app/lib/routes/project/index.js b/api/app/lib/routes/project/index.js index a3dbe3e..8f980de 100644 --- a/api/app/lib/routes/project/index.js +++ b/api/app/lib/routes/project/index.js @@ -1,8 +1,13 @@ 'use strict'; const project = require('../../controllers/project'); +const projectBind = require('../../controllers/project/bind') module.exports = function (app, router, opts) { app.fs.api.logAttr['GET/project/app_list'] = { content: '获取应用列表', visible: true }; router.get('/project/app_list', project.appList); + + app.fs.api.logAttr['POST/project/bind'] = { content: '绑定安心云、项目管理项目', visible: true }; + router.post('/project/bind', projectBind.bindAnxin2pep); + }; \ No newline at end of file diff --git a/api/app/lib/service/clickHouseClient.js b/api/app/lib/service/clickHouseClient.js index fdf6490..e23d894 100644 --- a/api/app/lib/service/clickHouseClient.js +++ b/api/app/lib/service/clickHouseClient.js @@ -1,13 +1,24 @@ 'use strict'; +const { ClickHouse } = require('clickhouse'); function factory (app, opts) { if (opts.clickHouse) { try { - for (let r of opts.clickHouse) { - if (r.name && r.root) { - app.fs[r.name] = new paasRequest(r.root, { ...(r.params || {}) }, { dataWord: r.dataWord || 'body' }) + app.fs.clickHouse = {} + const { url, port, db = [] } = opts.clickHouse + for (let d of db) { + if (d.name && d.db) { + app.fs.clickHouse[d.name] = new ClickHouse({ + url: url, + port: port, + debug: true, + format: "json", + config: { + database: d.db, + }, + }) } else { - throw 'opts.pssaRequest 参数错误!' + throw 'opts.clickHouse 参数错误!' } } } catch (error) {