From dad65bad50fe19d11627205e919d7514c55d433c Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Wed, 28 Sep 2022 15:32:52 +0800 Subject: [PATCH 1/3] =?UTF-8?q?token=20=E9=89=B4=E6=9D=83=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=BF=9D=E5=AD=98=E8=87=B3=20redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/auth/index.js | 9 ++++++- api/app/lib/middlewares/authenticator.js | 30 ++++++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/api/app/lib/controllers/auth/index.js b/api/app/lib/controllers/auth/index.js index bd67e02..555caf8 100644 --- a/api/app/lib/controllers/auth/index.js +++ b/api/app/lib/controllers/auth/index.js @@ -33,6 +33,8 @@ async function login (ctx, next) { ) { throw '当前账号已禁用' } + emisLoginRes.authorized = true + emisLoginRes.expired = moment().add(1, 'day') emisLoginRes.pomsUserInfo = pomsRegisterRes.dataValues let userUpdateData = { @@ -60,13 +62,17 @@ async function login (ctx, next) { ctx.fs.logger.error(`IP GET, error: ${error}`); } - await models.User.update(userUpdateData, { where: { id: emisLoginRes.id } }) + await ctx.redis.hmset(emisLoginRes.token, { + expired: moment().add(1, 'day'), + userInfo:JSON.stringify(emisLoginRes) + }); + ctx.status = 200; ctx.body = emisLoginRes; } @@ -95,6 +101,7 @@ async function logout (ctx) { await ctx.app.fs.emisRequest.put('logout', { data: params }) + await ctx.redisTools.hdelall(token); ctx.status = 204; } catch (error) { diff --git a/api/app/lib/middlewares/authenticator.js b/api/app/lib/middlewares/authenticator.js index 1bc7416..52e6a09 100644 --- a/api/app/lib/middlewares/authenticator.js +++ b/api/app/lib/middlewares/authenticator.js @@ -72,24 +72,34 @@ let authorizeToken = async function (ctx, token) { if (token && tokenFormatRegexp.test(token)) { try { console.log(`DurationCalc: auth 1 用时 ${moment().diff(startTime, 'milliseconds')}`); - const authorizeRes = await ctx.app.fs.emisRequest.get('authorize', { - query: { token } - }) + + const expired = await ctx.redis.hget(token, 'expired'); + + // const authorizeRes = await ctx.app.fs.emisRequest.get('authorize', { + // query: { token } + // }) + // const { userInfo, expired } = authorizeRes; + console.log(`DurationCalc: auth 2 用时 ${moment().diff(startTime, 'milliseconds')}`); - const { userInfo, expired } = authorizeRes; + + // TODO 从项企 clickhouse 数据库中查 token 并更新 if (expired && moment().valueOf() <= moment(expired).valueOf()) { - const pomsUser = await ctx.app.fs.dc.models.User.findOne({ - where: { - pepUserId: userInfo.id - } - }) || {} + const userInfo = JSON.parse(await ctx.redis.hmget(token, 'userInfo')); + const { pomsUserInfo: pomsUser } = userInfo + + // const pomsUser = await ctx.app.fs.dc.models.User.findOne({ + // where: { + // pepUserId: userInfo.id + // } + // }) || {} + console.log(`DurationCalc: auth 3 用时 ${moment().diff(startTime, 'milliseconds')}`); rslt = { 'authorized': userInfo.authorized, 'resources': (userInfo || {}).resources || [], }; ctx.fs.api.userId = pomsUser.id; - ctx.fs.api.userInfo = pomsUser.dataValues; + ctx.fs.api.userInfo = pomsUser; ctx.fs.api.pepUserId = userInfo.id; ctx.fs.api.pepUserInfo = userInfo; ctx.fs.api.token = token; From d03c8cb7afc48343c82dd7154a88078a30d5434e Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Wed, 28 Sep 2022 15:46:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AD=9B=E9=80=89=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E5=BA=94=E8=BF=94=E5=9B=9E=20pomsProject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/utils/dataRange.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/api/app/lib/utils/dataRange.js b/api/app/lib/utils/dataRange.js index b542b4e..71dbd21 100644 --- a/api/app/lib/utils/dataRange.js +++ b/api/app/lib/utils/dataRange.js @@ -127,15 +127,21 @@ module.exports = function (app, opts) { let undelStruc = [] for (let s of undelStrucRes) { if (!undelStruc.some(us => us.strucId == s.strucId)) { + let pomsProject = [] + for (let br of bindRes) { + if (br.anxinProjectId.some(braId => braId == s.projectId)) { + let corPepProject = pepProjectRes.find(pp => pp.id == br.pepProjectId) + pomsProject.push({ + ...br, + pepProject: corPepProject + }) + } + } undelStruc.push({ strucId: s.strucId, strucName: s.strucName, // projectId: s.projectId, - pepProject: pepProjectRes.filter(pp => { - return bindRes.some(br => { - return br.pepProjectId == pp.id && br.anxinProjectId.some(braId => braId == s.projectId) - }) - }) + pomsProject: pomsProject }) } } From dd44ebbde5a875ade826df59973b1ec8912d4cd8 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Wed, 28 Sep 2022 15:49:33 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AD=9B=E9=80=89=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E5=BA=94=E8=BF=94=E5=9B=9E=20pomsProject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/alarm/data.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/api/app/lib/controllers/alarm/data.js b/api/app/lib/controllers/alarm/data.js index 8a1742e..2a92164 100644 --- a/api/app/lib/controllers/alarm/data.js +++ b/api/app/lib/controllers/alarm/data.js @@ -155,19 +155,12 @@ async function list (ctx) { alarmRes.forEach(ar => { - ar.pepProject = (anxinStruc.find(as => as.strucId == ar.StructureId) || + ar.pomsProject = (anxinStruc.find(as => as.strucId == ar.StructureId) || { - pepProject: [ + pomsProject: [ // TODO: 开发临时添加 - // { - // id: 999, - // projectName: '这是假的开发の数据,看到请拨打110', - // isdelete: 0, - // constructionStatusId: 1, - // constructionStatus: '建设中', - // } ] - }).pepProject + }).pomsProject // 最新告警详情 - 确认信息 let corConfirmedData = (confirmedAlarmDetailMax.find(cdm => cdm.AlarmId == ar.AlarmId) || {});