From 5b287b5fc88fc12f239a1c6e419595037eedbc03 Mon Sep 17 00:00:00 2001 From: hd-huangchao Date: Wed, 11 Sep 2024 16:22:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20ts=20=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/build.json | 2 +- src/api/modules/common.js | 2 +- src/plugins/http/cancel.js | 47 ++++++++++++++++++-------------------- src/plugins/utils/log.js | 11 ++------- 4 files changed, 26 insertions(+), 36 deletions(-) diff --git a/public/build.json b/public/build.json index 83206d75..93354bd3 100644 --- a/public/build.json +++ b/public/build.json @@ -1 +1 @@ -{"version":"1726039227052"} \ No newline at end of file +{"version":"1726042780905"} \ No newline at end of file diff --git a/src/api/modules/common.js b/src/api/modules/common.js index 9026d09e..b1a8c411 100644 --- a/src/api/modules/common.js +++ b/src/api/modules/common.js @@ -32,7 +32,7 @@ export default { data, }); }, - queryAddressByIp: (data = { ip: "" }) => { + queryAddressByIp: (data = {}) => { return Axios({ url: `${AxiosConfig.ipUrl}/index?ip=${data.ip}&type=0`, method: "GET", diff --git a/src/plugins/http/cancel.js b/src/plugins/http/cancel.js index b63f59a4..0f3c3559 100644 --- a/src/plugins/http/cancel.js +++ b/src/plugins/http/cancel.js @@ -1,10 +1,22 @@ +const axiosCancel = {}; const cancelMap = new Map(); function getPending(config) { return [config.url, config.method].join("&"); } -function removeCancer(config) { +axiosCancel.addCancer = (config) => { + // 防止重复请求 + axiosCancel.removeCancer(config); + // 取消请求 + const controller = new AbortController(); + const key = getPending(config); + config.signal = controller.signal; + if (!cancelMap.has(key)) { + cancelMap.set(key, controller); + } +}; +axiosCancel.removeCancer = (config) => { const key = getPending(config); if (cancelMap.has(key)) { const cancel = cancelMap.get(key); @@ -13,29 +25,14 @@ function removeCancer(config) { } cancelMap.delete(key); } -} - -export default { - addCancer: (config) => { - // 防止重复请求 - removeCancer(config); - // 取消请求 - const controller = new AbortController(); - const key = getPending(config); - config.signal = controller.signal; - if (!cancelMap.has(key)) { - cancelMap.set(key, controller); +}; +axiosCancel.removeAllCancer = () => { + cancelMap.forEach((cancel) => { + if (cancel) { + cancel.abort(); } - }, - removeCancer: (config) => { - removeCancer(config); - }, - removeAllCancer: () => { - cancelMap.forEach((cancel) => { - if (cancel) { - cancel.abort(); - } - }); - cancelMap.clear(); - }, + }); + cancelMap.clear(); }; + +export default axiosCancel; diff --git a/src/plugins/utils/log.js b/src/plugins/utils/log.js index f53bbd47..8a1e156d 100644 --- a/src/plugins/utils/log.js +++ b/src/plugins/utils/log.js @@ -1,11 +1,4 @@ -const log = { - capsule: () => {}, - default: () => {}, - primary: () => {}, - success: () => {}, - warning: () => {}, - danger: () => {}, -}; +const log = {}; function typeColor(type = "default") { let color = ""; @@ -48,7 +41,7 @@ function colorful(textArr) { ); } -log.capsule = (title = "", info = "", type = "primary") => { +log.capsule = (title, info, type = "primary") => { console.log(`%c ${title} %c ${info} %c`, "background:#35495E; padding: 1px; border-radius: 3px 0 0 3px; color: #fff;", `background:${typeColor(type)}; padding: 1px; border-radius: 0 3px 3px 0; color: #fff;`, "background:transparent"); };