Browse Source

本地缓存接口请求,解决页面响应较慢问题。

master
Julin 1 year ago
parent
commit
14197537e6
  1. 95
      web/routes/missionboard/index.js

95
web/routes/missionboard/index.js

@ -5,6 +5,9 @@ const Holiday = require('../holiday');
module.exports = {
entry(app, router, opts) {
app.fs = app.fs || {};
app.fs.cache = app.fs.cache || {};
const ZENTAO_DOMAIN = opts.zentaoDomain;
async function auth() {
@ -18,17 +21,9 @@ module.exports = {
.query({ [`${sessionName}`]: sessionID })
.send({ account: 'admin', password: 'Fashion123' });
app.fs = app.fs || {};
app.fs.session = { sessionName, sessionID };
}
(async function () {
await auth();
setInterval(async () => {
await auth();
}, 1200000); // 每 20分钟 刷新一次
}());
function isWorkday(date) {
let workday = false;
const dateString = moment(date).format('YYYY-MM-DD');
@ -131,8 +126,10 @@ module.exports = {
return executionStats;
}
async function getOngoingProjects(ctx, next) {
async function cacheOngoingProjects() {
try {
if (!app.fs.cache.ongoingProjects) app.fs.cache.ongoingProjectsError = '数据缓存尚未完成';
const executions = [];
// 执行(未开始)
@ -170,22 +167,17 @@ module.exports = {
});
}
ctx.status = 200;
ctx.body = {
total: executions.length,
projects: executions,
};
app.fs.cache.ongoingProjects = executions;
app.fs.cache.ongoingProjectsError = null;
} catch (e) {
ctx.status = 400;
ctx.body = {
name: 'FindError',
message: `版本计划获取失败${e}`,
};
app.fs.cache.ongoingProjectsError = e;
}
}
async function getOngoingPersons(ctx, next) {
async function cacheOngoingPersons() {
try {
if (!app.fs.cache.ongoingPersons) app.fs.cache.ongoingPersonsError = '数据缓存尚未完成';
const currentDate = moment();
// 获取当前周的第一天(周一)
const firstDayOfWeek = currentDate.clone().startOf('isoWeek');
@ -262,16 +254,71 @@ module.exports = {
return { ...initData, ...memberTasks };
});
ctx.status = 200;
app.fs.cache.ongoingPersons = members;
app.fs.cache.ongoingPersonsError = null;
} catch (e) {
app.fs.cache.ongoingPersonsError = e;
}
}
(async function () {
await auth();
cacheOngoingProjects();
cacheOngoingPersons();
setInterval(async () => {
await auth();
cacheOngoingProjects();
cacheOngoingPersons();
}, 1200000); // 每 20分钟 刷新一次
}());
async function getOngoingProjects(ctx, next) {
try {
const { ongoingProjects, ongoingProjectsError } = app.fs.cache;
if (ongoingProjectsError) {
ctx.status = 400;
ctx.body = {
name: 'FindError',
message: `版本计划获取失败:${ongoingProjectsError}`,
};
} else {
ctx.status = 200;
ctx.body = {
total: ongoingProjects.length,
projects: ongoingProjects,
};
}
} catch (e) {
ctx.status = 400;
ctx.body = {
total: members.length,
projects: members,
name: 'FindError',
message: `版本计划获取失败:${e}`,
};
}
}
async function getOngoingPersons(ctx, next) {
try {
const { ongoingPersons, ongoingPersonsError } = app.fs.cache;
if (ongoingPersonsError) {
ctx.status = 400;
ctx.body = {
name: 'FindError',
message: `人员计划获取失败:${ongoingPersonsError}`,
};
} else {
ctx.status = 200;
ctx.body = {
total: ongoingPersons.length,
projects: ongoingPersons,
};
}
} catch (e) {
ctx.status = 400;
ctx.body = {
name: 'FindError',
message: `人员计划获取失败${e}`,
message: `人员计划获取失败${e}`,
};
}
}

Loading…
Cancel
Save