perf: 优化仪表盘任务更新规则

This commit is contained in:
kuaifan 2025-01-13 10:49:01 +08:00
parent c16e37023c
commit 46a623b430
3 changed files with 31 additions and 11 deletions

View File

@ -1643,7 +1643,7 @@ const timezone = require("dayjs/plugin/timezone");
contentType: 'application/x-www-form-urlencoded', contentType: 'application/x-www-form-urlencoded',
timeout: 0 timeout: 0
}; };
let callbacks = ['beforeSend', 'error', 'complete', 'success', 'statusCode']; const callbacks = ['beforeSend', 'error', 'complete', 'success', 'statusCode'];
//For jQuery guys //For jQuery guys
@ -1749,7 +1749,10 @@ const timezone = require("dayjs/plugin/timezone");
} }
// Create XHR // Create XHR
let xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
// 添加请求开始时间记录
const requestStartTime = Date.now();
// Save Request URL // Save Request URL
xhr.requestUrl = options.url; xhr.requestUrl = options.url;
@ -1827,6 +1830,22 @@ const timezone = require("dayjs/plugin/timezone");
xhr.onload = function (e) { xhr.onload = function (e) {
if (xhrTimeout) clearTimeout(xhrTimeout); if (xhrTimeout) clearTimeout(xhrTimeout);
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 0) { if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 0) {
// 计算请求耗时
const requestDuration = Date.now() - requestStartTime;
// 获取响应标头时间信息
const serverDate = xhr.getResponseHeader('Date');
const lastModified = xhr.getResponseHeader('Last-Modified');
const age = xhr.getResponseHeader('Age');
// 将时间信息添加到响应对象中
xhr.timeData = {
serverDate: serverDate,
lastModified: lastModified,
age: age,
duration: requestDuration
};
let responseData; let responseData;
if (options.dataType === 'json') { if (options.dataType === 'json') {
try { try {

View File

@ -273,7 +273,7 @@ export default {
return return
} }
if (ret === 1) { if (ret === 1) {
resolve({data, msg}) resolve({data, msg, xhr})
} else { } else {
reject({ret, data, msg: msg || $A.L('未知错误')}) reject({ret, data, msg: msg || $A.L('未知错误')})
// //
@ -1354,9 +1354,9 @@ export default {
dispatch("call", { dispatch("call", {
url: 'project/lists', url: 'project/lists',
data: callData.get() data: callData.get()
}).then(({data}) => { }).then(({data, xhr}) => {
dispatch("saveProject", data.data); dispatch("saveProject", data.data);
callData.save(data).then(ids => dispatch("forgetProject", ids)) callData.save(data, xhr.timeData.serverDate).then(ids => dispatch("forgetProject", ids))
state.projectTotal = data.total_all; state.projectTotal = data.total_all;
// //
resolve(data) resolve(data)
@ -1853,12 +1853,12 @@ export default {
dispatch("call", { dispatch("call", {
url: 'project/task/lists', url: 'project/task/lists',
data: callData.get() data: callData.get()
}).then(({data}) => { }).then(({data, xhr}) => {
if (requestData.project_id) { if (requestData.project_id) {
state.projectLoad--; state.projectLoad--;
} }
dispatch("saveTask", data.data); dispatch("saveTask", data.data);
callData.save(data).then(ids => dispatch("forgetTask", ids)) callData.save(data, xhr.timeData.serverDate).then(ids => dispatch("forgetTask", ids))
// //
if (data.next_page_url) { if (data.next_page_url) {
requestData.page = data.current_page + 1 requestData.page = data.current_page + 1
@ -2757,9 +2757,9 @@ export default {
dispatch("call", { dispatch("call", {
url: 'dialog/lists', url: 'dialog/lists',
data: callData.get() data: callData.get()
}).then(({data}) => { }).then(({data, xhr}) => {
dispatch("saveDialog", data.data); dispatch("saveDialog", data.data);
callData.save(data).then(ids => dispatch("forgetDialog", ids)) callData.save(data, xhr.timeData.serverDate).then(ids => dispatch("forgetDialog", ids))
// //
if (data.current_page === 1) { if (data.current_page === 1) {
dispatch("getDialogLatestMsgs", data.data.map(({id}) => id)) dispatch("getDialogLatestMsgs", data.data.map(({id}) => id))

View File

@ -34,15 +34,16 @@ function __callData(key, requestData, state) {
* @param total * @param total
* @param current_page * @param current_page
* @param deleted_id * @param deleted_id
* @param serverDate
* @returns {Promise<unknown>} * @returns {Promise<unknown>}
*/ */
this.save = ({total, current_page, deleted_id}) => { this.save = ({total, current_page, deleted_id}, serverDate = undefined) => {
return new Promise(async resolve => { return new Promise(async resolve => {
if (current_page !== 1) { if (current_page !== 1) {
return return
} }
let hasUpdate = false let hasUpdate = false
const time = $A.dayjs().unix() const time = $A.dayjs(serverDate).unix()
if (total > 0) { if (total > 0) {
callData.updated = time callData.updated = time
hasUpdate = true hasUpdate = true