Browse Source

(*)定时刷新缓存数据

master
peng.peng 1 year ago
parent
commit
34d5ccb8c5
  1. 4
      api/app/lib/controllers/superScreen/water.js
  2. 33
      api/app/lib/schedule/refreshWater.js
  3. 16
      api/app/lib/utils/waterCache.js
  4. 2
      super-screen/client/src/sections/water-prevention/containers/homePage.js

4
api/app/lib/controllers/superScreen/water.js

@ -59,10 +59,10 @@ function waterReq(opts) {
try { try {
let ache = body.url == 'login' ? await opts.fs.utils.getAnxinyunToken() : let ache = body.url == 'login' ? await opts.fs.utils.getAnxinyunToken() :
await opts.fs.utils.getDataFromAxy(body.url, body.method || 'get', body.data) await opts.fs.utils.getDataFromAxy(body.url, body.method || 'get', body.data || {})
ctx.status = 200; ctx.status = 200;
ctx.body = ache.body ? ache.body : ache; ctx.body = ache && ache.body ? ache.body : ache;
} catch (error) { } catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;

33
api/app/lib/schedule/refreshWater.js

@ -0,0 +1,33 @@
const moment = require('moment')
const { refresh } = require('../utils/waterCache');
module.exports = function (app, opts) {
const { models } = app.fs.dc
const freshWater = app.fs.scheduleInit(
{
interval: '0 * * * * *',
// interval: '*/3 * * * *',
immediate: true,
// proRun: true,
},
async () => {
try {
const waterCache = app.fs.waterCache
if (waterCache) {
Object.keys(waterCache).forEach(key => {
key.indexOf('login') > -1 ? app.fs.utils.getAnxinyunToken()
:
key.indexOf('axy/alarm') > -1 ? null :
app.fs.utils.getDataFromAxy(key, 'get', {})
})
}
} catch (error) {
app.fs.logger.error(`sechedule: freshYingshiState, error: ${error}`)
}
});
return {
freshWater,
}
}

16
api/app/lib/utils/waterCache.js

@ -33,7 +33,8 @@ module.exports = function (app, opts) {
} }
} }
let rslt = {}; let rslt = {
};
async function getDataFromAxy(url, method, data) { async function getDataFromAxy(url, method, data) {
try { try {
if (rslt[url]) { if (rslt[url]) {
@ -50,20 +51,17 @@ module.exports = function (app, opts) {
async function getData(url, method = 'get', data) { async function getData(url, method = 'get', data) {
try { try {
let ache = await getAnxinyunToken() const res = await app.fs.waterReq[method](`${url}`, data ? { ...data } : {})
if (ache) {
const connector = url.indexOf('?') > -1 ? '&' : '?'
const res = await app.fs.waterReq[method](`${url}`, { ...data })
rslt[url] = res rslt[url] = res
return rslt[url] app.fs.waterCache = rslt
} return res
} catch (err) { } catch (err) {
app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`); app.fs.logger.error(`utils: error: ${err}`);
} }
} }
return { return {
getAnxinyunToken, getAnxinyunToken,
getDataFromAxy getDataFromAxy,
} }
} }

2
super-screen/client/src/sections/water-prevention/containers/homePage.js

@ -129,7 +129,7 @@ function homePage(props) {
setVideoList([videoTop].concat(list?.filter(v => v.name != '玉湖公园'))); setVideoList([videoTop].concat(list?.filter(v => v.name != '玉湖公园')));
}) })
dispatch(getYsAccessToken({ token })).then(resArr => { dispatch(getYsAccessToken({ token })).then(resArr => {
const at = resArr.payload.data.accessToken; const at = resArr.payload.data?.accessToken;
setAccessToken(at) setAccessToken(at)
}) })

Loading…
Cancel
Save