From 33372daa62e8c2a22ca2bd4e31bac0efd32bdc40 Mon Sep 17 00:00:00 2001 From: "peng.peng" Date: Wed, 15 Nov 2023 16:13:13 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BC=88*=EF=BC=89=E6=B0=B4=E5=88=A9=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/.vscode/launch.json | 2 +- api/app/lib/controllers/superScreen/water.js | 23 ++++++- api/app/lib/middlewares/authenticator.js | 2 + api/app/lib/middlewares/water.js | 38 ++++++++++ api/app/lib/routes/superScreen/water.js | 4 ++ api/app/lib/utils/waterCache.js | 69 +++++++++++++++++++ api/app/lib/utils/waterReq.js | 33 +++++++++ api/config.js | 7 ++ .../actions/waterconservancy.js | 52 +++++++++----- .../components/right-bottom.js | 5 +- .../water-prevention/containers/gis.js | 33 ++++++--- .../water-prevention/containers/homePage.js | 28 +++++--- super-screen/client/src/utils/webapi.js | 15 ++-- 13 files changed, 260 insertions(+), 51 deletions(-) create mode 100644 api/app/lib/middlewares/water.js create mode 100644 api/app/lib/utils/waterCache.js create mode 100644 api/app/lib/utils/waterReq.js diff --git a/api/.vscode/launch.json b/api/.vscode/launch.json index ae38046..ae1553b 100644 --- a/api/.vscode/launch.json +++ b/api/.vscode/launch.json @@ -20,7 +20,7 @@ "-b http://10.8.30.161:31420", "-s http://10.8.30.161:32258", "-d postgres/example/10.8.30.160/30432", - "-w https://smartwater.anxinyun.cn", + "-w https://smartwater.anxinyun.cn/api", "-a https://smartworksafety.anxinyun.cn", "--yingshiKey 5d16a667e1c2423d9d0d634f781810b4", "--yingshiSecret 0cc4e1ec4e6a53ea3dabeb09cd5f468b", diff --git a/api/app/lib/controllers/superScreen/water.js b/api/app/lib/controllers/superScreen/water.js index 5641d4d..7cd1676 100644 --- a/api/app/lib/controllers/superScreen/water.js +++ b/api/app/lib/controllers/superScreen/water.js @@ -51,8 +51,29 @@ function getEmergencyList(opts) { } } +function waterReq(opts) { + return async function (ctx, next) { + + let errMsg = { message: '获取水务平台数据失败' } + const body = ctx.request.body; + try { + + let ache = body.url == 'login' ? await opts.fs.utils.getAnxinyunToken() : + await opts.fs.utils.getDataFromAxy(body.url, body.method || 'get', body.data) + + ctx.status = 200; + ctx.body = ache.body ? ache.body : ache; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = errMsg + } + } +} + module.exports = { getRealStateData, - getEmergencyList + getEmergencyList, + waterReq } \ No newline at end of file diff --git a/api/app/lib/middlewares/authenticator.js b/api/app/lib/middlewares/authenticator.js index b2ac0ca..e7d57db 100644 --- a/api/app/lib/middlewares/authenticator.js +++ b/api/app/lib/middlewares/authenticator.js @@ -75,6 +75,8 @@ let isPathExcluded = function (opts, path, method) { excludeOpts.push({ p: '/community/info', o: 'GET' }); excludeOpts.push({ p: '/community/fgjv/notice', o: 'GET' }); excludeOpts.push({ p: '/affordable/housing/add', o: 'POST' }); + excludeOpts.push({ p: '/waterReq', o: 'POST' }); + excludes = new ExcludesUrls(excludeOpts); } diff --git a/api/app/lib/middlewares/water.js b/api/app/lib/middlewares/water.js new file mode 100644 index 0000000..6615898 --- /dev/null +++ b/api/app/lib/middlewares/water.js @@ -0,0 +1,38 @@ +/** + * Created by PengLing on 2017/7/3. + */ +'use strict'; + +const request = require('superagent'); + +module.exports = { + entry: (app, router, opts) => { + const apiRoot = `${opts.host}${opts.match}`; + const req = { + get: (url) => { + return request.get(`${apiRoot}${url}`); + }, + + post: (url, data) => { + return request + .post(`${apiRoot}${url}`) + .set('Content-Type', 'application/json') + .send(data); + }, + + put: (url, data) => { + return request + .put(`${apiRoot}${url}`) + .set('Content-Type', 'application/json') + .send(data); + }, + + delete: (url) => { + return request.del(`${apiRoot}${url}`); + } + }; + + app.iota = app.iota || {}; + app.iota.request = req; + } +}; diff --git a/api/app/lib/routes/superScreen/water.js b/api/app/lib/routes/superScreen/water.js index b6cd67b..9d7d32c 100644 --- a/api/app/lib/routes/superScreen/water.js +++ b/api/app/lib/routes/superScreen/water.js @@ -11,4 +11,8 @@ module.exports = function (app, router, opts, AuthCode) { //获取水利应急抢险数据 app.fs.api.logAttr['GET/water/emergency'] = { content: '获取水利应急抢险数据', visible: true }; router.get('/water/emergency', water.getEmergencyList(opts)); + + //获取水利应急抢险数据 + app.fs.api.logAttr['GET/waterReq'] = { content: '获取智慧水务平台数据', visible: true }; + router.post('/waterReq', water.waterReq(app)); }; diff --git a/api/app/lib/utils/waterCache.js b/api/app/lib/utils/waterCache.js new file mode 100644 index 0000000..404d81e --- /dev/null +++ b/api/app/lib/utils/waterCache.js @@ -0,0 +1,69 @@ +'use strict'; +const moment = require('moment') + +module.exports = function (app, opts) { + + let axyTokenCache = { + userId: null, + orgId: null, + token: null, + expireTime: null //过期时间 + } + + let getAnxinyunToken = async function () { + try { + if (!axyTokenCache.token || moment() > moment(axyTokenCache.expireTime)) { + const dataToAxy = { + pcode: 'fce4afe2-5b6a-408a-ab18-a2afa7fa027c', + username: '123456', + password: '123456', + } + const axyResponse = await app.fs.waterReq.post('/login', dataToAxy) + const axyData = axyResponse.body + if (axyData.authorized) { + axyTokenCache.userId = axyData.id //放进缓存 + axyTokenCache.orgId = axyData.orgId //放进缓存 + axyTokenCache.token = axyData.token //放进缓存 + axyTokenCache.expireTime = moment().add(23, 'hour').format('YYYY-MM-DD HH:mm:ss') + } + } + return axyTokenCache + } catch (err) { + app.fs.logger.error(`utils: fetch anxinyun token, error: ${err}`); + } + } + + let rslt = {}; + async function getDataFromAxy(url, method, data) { + try { + if (rslt[url]) { + getData(url, method, data) + return rslt[url] + } else { + let res = await getData(url, method, data) + return res + } + } catch (err) { + app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`); + } + } + + async function getData(url, method = 'get', data) { + try { + let ache = await getAnxinyunToken() + if (ache) { + const connector = url.indexOf('?') > -1 ? '&' : '?' + const res = await app.fs.waterReq[method](`${url}`, { ...data }) + rslt[url] = res + return rslt[url] + } + } catch (err) { + app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`); + } + } + + return { + getAnxinyunToken, + getDataFromAxy + } +} \ No newline at end of file diff --git a/api/app/lib/utils/waterReq.js b/api/app/lib/utils/waterReq.js new file mode 100644 index 0000000..ce002cb --- /dev/null +++ b/api/app/lib/utils/waterReq.js @@ -0,0 +1,33 @@ +/** + * Created by PengLing on 2017/7/3. + */ +'use strict' + +const request = require('superagent'); + +class AxyReq { + constructor(root) { + this.apiRoot = root + } + + get(url) { + return request.get(`${this.apiRoot}${url}`); + } + + post(url, data) { + return request.post(`${this.apiRoot}${url}`).set('Content-Type', 'application/json').send(data) + } + + put(url, data) { + return request.put(`${this.apiRoot}${url}`).set('Content-Type', 'application/json').send(data) + } + + delete(url) { + return request.del(`${this.apiRoot}${url}`) + } +} + +module.exports = function (app, opts) { + const req = new AxyReq(opts.waterUrl) + app.fs.waterReq = req +} \ No newline at end of file diff --git a/api/config.js b/api/config.js index 6cce684..6980c37 100644 --- a/api/config.js +++ b/api/config.js @@ -66,6 +66,12 @@ const product = { port: flags.port || 8080, staticDirs: ['static'], mws: [ + { + entry: require('./app/lib/utils/waterReq').entry, + opts: { + host: WATER_URL, + } + }, { entry: require('@fs/attachment').entry, opts: { @@ -110,6 +116,7 @@ const product = { tfApi: THIRD_FIRECONTROL, axyPumpProject: AXY_PUMP_PROJECT, statistics: AXY_STATISTICS, + waterUrl: WATER_URL, pssaRequest: [{// name 会作为一个 request 出现在 ctx.app.fs name: 'anxinyun', root: AXY_API_URL diff --git a/super-screen/client/src/sections/water-prevention/actions/waterconservancy.js b/super-screen/client/src/sections/water-prevention/actions/waterconservancy.js index 904d70d..3186eab 100644 --- a/super-screen/client/src/sections/water-prevention/actions/waterconservancy.js +++ b/super-screen/client/src/sections/water-prevention/actions/waterconservancy.js @@ -16,10 +16,10 @@ export function getPcode(orgId) { export function waterLogin(data) { return dispatch => basicAction({ type: 'post', - data, + data: { url: 'login', method: 'post' }, dispatch: dispatch, actionType: 'POST_WATER_LOGIN', - url: `${ApiTable.waterLogin}`, + url: `${ApiTable.getSmartWaterData}`, msg: { error: '登录水环境失败' }, // reducer: { name: 'members' } }); @@ -27,44 +27,52 @@ export function waterLogin(data) { export function getWaterLevelTrend(query) { return dispatch => basicAction({ - type: 'get', - query, + type: 'post', + data: { + url: `${ApiTable.getWaterLevelTrend}?token=${query?.token}&orgId=${query?.orgId}&keyword=`, + }, dispatch: dispatch, actionType: 'GET_WATER_LEVEL_TREND', - url: `${ApiTable.getWaterLevelTrend}`, + url: `${ApiTable.getSmartWaterData}`, msg: { error: '获取水位趋势失败' }, }); } export function getWaterStructures(query) { return dispatch => basicAction({ - type: 'get', - query, + type: 'post', + data: { + url: `${ApiTable.getWaterStructures}?token=${query?.token}`, + }, dispatch: dispatch, actionType: 'GET_WATER_STRUCTURES', - url: `${ApiTable.getWaterStructures}`, + url: `${ApiTable.getSmartWaterData}`, msg: { error: '获取水务结构物失败' }, }); } export function getWaterVideoList(struIds, query) { return dispatch => basicAction({ - type: 'get', - query, + type: 'post', + data: { + url: `${ApiTable.getWaterVideoList.replace('{struIds}', struIds)}&token=${query?.token}`, + }, dispatch: dispatch, actionType: 'GET_WATER_VIDEO_LIST', - url: `${ApiTable.getWaterVideoList.replace('{struIds}', struIds)}`, + url: `${ApiTable.getSmartWaterData.replace('{struIds}', struIds)}`, msg: { error: '获取水务视频失败' }, }); } export function getYsAccessToken(query) { return dispatch => basicAction({ - type: 'get', - query, + type: 'post', + data: { + url: `${ApiTable.getYsAccessToken}?token=${query?.token}`, + }, dispatch: dispatch, actionType: 'GET_YS_ACCESS_TOKEN', - url: `${ApiTable.getYsAccessToken}`, + url: `${ApiTable.getSmartWaterData}`, msg: { error: '获取萤石授权失败' }, }); } @@ -72,22 +80,28 @@ export function getYsAccessToken(query) { export function getWaterAlarms(query) { return dispatch => basicAction({ type: 'post', - query, - data: { "keywords": "", "status": "new", "levels": [1, 2, 3], "orderBy": "endTime", "orderDirection": "desc", "limit": 10, "offset": 0, "structures": null, "types": [1] }, + data: { + url: `${ApiTable.getWaterAlarms}&token=${query?.token}`, + method: 'post', + data: { "keywords": "", "status": "new", "levels": [1, 2, 3], "orderBy": "endTime", "orderDirection": "desc", "limit": 10, "offset": 0, "structures": null, "types": [1] } + }, // data: { "structures": null, "orderBy": "endTime", "endTime": "2022-06-30 23:59:59", "keywords": "", "levels": [1, 2, 3], "startTime": "2022-06-01 00:00:00", "orderDirection": "desc", "status": "history", "offset": 0, "types": [1], "limit": 10 }, dispatch: dispatch, actionType: 'GET_WATER_ALARMS', - url: `${ApiTable.getWaterAlarms}`, + url: `${ApiTable.getSmartWaterData}`, msg: { error: '获取告警失败' }, }); } export function getPumpStatus(structId) { return dispatch => basicAction({ - type: 'get', + type: 'post', + data: { + url: `${ApiTable.getPumpStatus.replace('{structId}', structId)}`, + }, dispatch: dispatch, actionType: 'GET_PUMP_STATUS', - url: `${ApiTable.getPumpStatus.replace('{structId}', structId)}`, + url: `${ApiTable.getSmartWaterData}`, msg: { error: '获取水泵状态失败' }, }); } \ No newline at end of file diff --git a/super-screen/client/src/sections/water-prevention/components/right-bottom.js b/super-screen/client/src/sections/water-prevention/components/right-bottom.js index 4105917..c120a2c 100644 --- a/super-screen/client/src/sections/water-prevention/components/right-bottom.js +++ b/super-screen/client/src/sections/water-prevention/components/right-bottom.js @@ -10,13 +10,12 @@ function DataTop5(props) { const [showVideoList, setShowVideoList] = useState(); useEffect(() => { - if (videoList.length && structures.length) { - setShowVideoList(videoList.filter(v => v.structId === structures[0].id)); + if (videoList.length && structures.length && accessToken) { if (!showVideoList) { setShowVideoList(recUrlToLiveUrl(videoList[0]?.url)) } } - }, [videoList]) + }, [videoList, accessToken]) const renderSubtitle = () => ( videoList?.length > 0 &&