From c69ab08c2145a5437c3736aeabee0d4037090592 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Sat, 17 Sep 2022 16:29:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=85=B3=E7=B3=BB=E7=BB=91?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/alarm/app.js | 4 +- api/app/lib/controllers/project/bind.js | 194 ++++++++++++++++------- api/app/lib/controllers/project/index.js | 17 +- api/app/lib/controllers/push/config.js | 2 +- api/app/lib/middlewares/authenticator.js | 8 +- 5 files changed, 150 insertions(+), 75 deletions(-) diff --git a/api/app/lib/controllers/alarm/app.js b/api/app/lib/controllers/alarm/app.js index 3faa464..edbaf63 100644 --- a/api/app/lib/controllers/alarm/app.js +++ b/api/app/lib/controllers/alarm/app.js @@ -108,10 +108,10 @@ async function notedInspection (ctx) { try { const models = ctx.fs.dc.models; const { inspectionId } = ctx.request.body - const { userId } = ctx.fs.api + const { userId, pepUserId } = ctx.fs.api await models.AppInspection.update({ - notedPepUserId: userId, + notedPepUserId: pepUserId, notedTime: moment().format() }, { where: { diff --git a/api/app/lib/controllers/project/bind.js b/api/app/lib/controllers/project/bind.js index 5bed3bd..7985ee2 100644 --- a/api/app/lib/controllers/project/bind.js +++ b/api/app/lib/controllers/project/bind.js @@ -1,74 +1,148 @@ 'use strict'; +const moment = require('moment') async function bindAnxin2pep (ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { - // const models = ctx.fs.dc.models; - // const { clickHouse } = ctx.app.fs - // const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body + const models = ctx.fs.dc.models; + const { clickHouse } = ctx.app.fs + const { userId, pepUserId } = ctx.fs.api + const { bindId, name, pepProjectId, anxinProjectId = [], app = [] } = ctx.request.body - // let bindId_ = bindId - // const existRes = await models.ProjectCorrelation.findOne({ - // where: { - // pepProjectId: pepProjectId - // } - // }) + let bindId_ = bindId + const now = moment() + const existRes = await models.ProjectCorrelation.findOne({ + where: { + pepProjectId: pepProjectId + }, + include: { + model: models.ProjectApp + } + }) - // let storageData = { - // name, pepProjectId, anxinProjectId, - // } - // if (bindId_) { - // if (!existRes) { - // throw '尚无已绑定的项企项目' - // } - // // 修改 - // await models.ProjectCorrelation.update(storageData, { - // where: { - // pepProjectId: pepProjectId - // }, - // transaction - // }) - // } else { - // // 新增 - // if (existRes) { - // // 但是有之前的数据 - // if (existRes.del) { - // // 不过之前的删除了 + let storageData = { + name, pepProjectId, anxinProjectId, + del: false, + } + // 已经创建过的 app + let existApp = [] + // 新增的 + let createAppData = [] + // url 相同需要更新的 解除 lock 状态 + let updateAppData = [] + // 没有出现但存在的 叠加 lock 状态 + let lockAppData = [] + + if (bindId_) { + if (!existRes) { + throw '尚无已绑定的项企项目' + } + // 修改 + await models.ProjectCorrelation.update(storageData, { + where: { + pepProjectId: pepProjectId + }, + transaction + }) + existApp = existRes.projectApps + } else { + // 新增 + if (existRes) { + // 但是有之前的数据 + if (existRes.del) { + // 不过之前的删除了 + await models.ProjectCorrelation.update(storageData, { + where: { + pepProjectId: pepProjectId + }, + transaction + }) + existApp = existRes.projectApps + } else { + // 没有删除 重复添加 + throw '当前项企项目已绑定' + } + } else { + storageData.createUser = userId; + storageData.createTime = now.format() + const createRes = await models.ProjectCorrelation.create(storageData, { + transaction + }) + bindId_ = createRes.id + } + } + + app.forEach((a, i, arr) => { + if (!a.name || !a.url) { + throw `${a.name} ${a.url} 缺少必要参数` + } + let curUrlArr = a.url.split('://') + if (curUrlArr.length < 2) { + throw `${a.name} ${a.url} url 地址错误` + } + curUrlArr.shift() + let curUrl = curUrlArr.join('://') + // 先判断传过来的数据有没有重复 + for (let ii = i + 1; ii < arr.length; ii++) { + let curForUrlArr = arr[ii].url.split('://') + curForUrlArr.shift() + if (curUrl == curForUrlArr.join('://')) { + throw `${a.name} ${a.url} 与 ${arr[ii].name} ${arr[ii].url} 地址重复` + } + } - // } else { - // // 没有删除 重复添加 - // throw '当前项企项目已绑定' - // } - // } else { - // const createRes = await models.ProjectCorrelation.create(storageData, { - // transaction - // }) - // bindId_ = createRes.id + // 再判断和已有的有没有重复 + let existSameApp = existApp.find(ea => { + let curForUrlArr = ea.url.split('://') + curForUrlArr.shift() + return curUrl == curForUrlArr.join('://') + }) + if (existSameApp) { + updateAppData.push({ + id: existSameApp.id, + name: a.name, + url: a.url, + projectId: bindId_, + lock: false, + }) + existSameApp.addAgain = true + } else { + createAppData.push({ + name: a.name, + url: a.url, + projectId: bindId_, + lock: false, + }) + } + lockAppData = existApp.filter(esa => !esa.addAgain).map(esa => { + return { + id: esa.id, + // name: esa.name, + // url: esa.url, + // projectId: bindId_, + lock: true, + } + }) + }) - // await models.ProjectApp.bulkCreate(app.map((a, i, arr) => { - // if (!a.name || !a.url) { - // throw `${a.name} ${a.url} 缺少必要参数` - // } - // let curUrlArr = a.url.split('://') - // if(curUrlArr.length < 2){ - // throw `${a.name} ${a.url} url 地址错误` - // } - // for (let ii = i; ii < arr.length; ii++) { - // let - // } + createAppData.length ? + await models.ProjectApp.bulkCreate(createAppData, { + transaction + }) : null - // return { - // name: a.name, - // url: a.url, - // projectId: bindId_, - // lock: false, - // } - // })) - // } - // } + updateAppData.length || lockAppData.length ? + await Promise.all(updateAppData.concat(lockAppData).map(ud => { + return models.ProjectApp.update(ud, { + where: { + id: ud.id + }, + transaction + }) + })) + : null - // await transaction.commit(); - // ctx.status = 204; + await transaction.commit(); + ctx.status = 204; } catch (error) { await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); diff --git a/api/app/lib/controllers/project/index.js b/api/app/lib/controllers/project/index.js index 7f55f79..73df7cd 100644 --- a/api/app/lib/controllers/project/index.js +++ b/api/app/lib/controllers/project/index.js @@ -24,13 +24,14 @@ async function pomsProject (ctx) { try { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs - const { userId } = ctx.fs.api + const { userId, pepUserId } = ctx.fs.api const { limit, page } = ctx.query let findOption = { where: { del: false }, + distinct: true, include: { model: models.ProjectApp, where: { @@ -49,14 +50,11 @@ async function pomsProject (ctx) { findOption.offset = page * limit } - const proRes = await models.ProjectCorrelation.findAll(findOption) - delete findOption.limit - delete findOption.offset - const proCount = await models.ProjectCorrelation.count(findOption) + const proRes = await models.ProjectCorrelation.findAndCountAll(findOption) let pepProjectIds = new Set() let anxinProjectIds = new Set() - for (let p of proRes) { + for (let p of proRes.rows) { pepProjectIds.add(p.pepProjectId) for (let ap of p.anxinProjectId) { anxinProjectIds.add(ap) @@ -71,7 +69,7 @@ async function pomsProject (ctx) { [] - for (let p of proRes) { + for (let p of proRes.rows) { const corPro = pepProjectRes.find(pp => pp.id == p.pepProjectId) p.dataValues.pepProjectName = corPro.project_name let nextAnxinProject = anxinProjectRes.filter(ap => p.anxinProjectId.includes(ap.id)) @@ -79,10 +77,7 @@ async function pomsProject (ctx) { delete p.dataValues.anxinProjectId } ctx.status = 200; - ctx.body = { - count: proCount, - rows: proRes - } + ctx.body = proRes } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; diff --git a/api/app/lib/controllers/push/config.js b/api/app/lib/controllers/push/config.js index 3cbcdb0..f4cba4f 100644 --- a/api/app/lib/controllers/push/config.js +++ b/api/app/lib/controllers/push/config.js @@ -48,7 +48,7 @@ async function list (ctx) { async function edit (ctx) { try { const models = ctx.fs.dc.models; - const { userId } = ctx.fs.api + const { userId, pepUserId } = ctx.fs.api const { pushId, name, pepProjectId = [], alarmType = [], receiverPepUserId = [], timeType = [], disable } = ctx.request.body let storageData = { diff --git a/api/app/lib/middlewares/authenticator.js b/api/app/lib/middlewares/authenticator.js index ac08b3c..617f596 100644 --- a/api/app/lib/middlewares/authenticator.js +++ b/api/app/lib/middlewares/authenticator.js @@ -76,11 +76,17 @@ let authorizeToken = async function (ctx, token) { }) const { userInfo, expired } = authorizeRes; if (expired && moment().valueOf() <= moment(expired).valueOf()) { + const pomsUser = await ctx.app.fs.dc.models.User.findOne({ + where: { + pepUserId: userInfo.id + } + }) rslt = { 'authorized': userInfo.authorized, 'resources': (userInfo || {}).resources || [], }; - ctx.fs.api.userId = userInfo.id; + ctx.fs.api.userId = pomsUser.id; + ctx.fs.api.pepUserId = userInfo.id; ctx.fs.api.userInfo = userInfo; ctx.fs.api.token = token; }