From 7d5a105818d6f627705e1a5000f35fddcca47421 Mon Sep 17 00:00:00 2001 From: wf <2547096351@qq.com> Date: Tue, 8 Oct 2024 15:41:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=EF=BC=9Apx2px=E3=80=81?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E8=BD=AC=E6=97=B6=E9=97=B4=E3=80=81?= =?UTF-8?q?formData=E8=87=AA=E5=8A=A8=E5=8A=A0=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/index.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/px2px.ts | 15 ++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/utils/px2px.ts diff --git a/src/utils/index.ts b/src/utils/index.ts index d6c7fa6..4a99f5a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -103,3 +103,61 @@ export function webDefaultLanguage() { return "enUS"; } } + +/** + * 时间戳转 年月日时分秒 + * 若timestamp不传则取当前时间 + * 若type不传则取 年月日时分秒 + * @param { number } timestamp 时间戳 + * @return 返回年月日时分秒字符串 + */ +export const getTimestamp = (timestamp: string | number | null, type: string) => { + let date = null; + if (timestamp) { + date = new Date(timestamp); + } else { + date = new Date(); + } + let Year = String(date.getFullYear()); + let Moth = String(date.getMonth() + 1).padStart(2, "0"); + let Day = String(date.getDate()).padStart(2, "0"); + let Hour = String(date.getHours()).padStart(2, "0"); + let Minute = String(date.getMinutes()).padStart(2, "0"); + let Seconds = String(date.getSeconds()).padStart(2, "0"); + if (type === "yyyy") { + return `${Year}`; + } + if (type === "yyyy-MM") { + return `${Year}-${Moth}`; + } + if (type === "yyyy-MM-dd") { + return `${Year}-${Moth}-${Day}`; + } + return `${Year}-${Moth}-${Day} ${Hour}:${Minute}:${Seconds}`; +}; + +/** + * 给formData循环添加参数,过滤null、undefined、空字符串、NaN + * 示例:let data = appendFormData(your-object); + * @param { object } obj 参数对象 + * @return 返回formData对象 + */ +export const appendFormData = (obj: any) => { + let formData = new FormData(); + function deepAppendFormData(formData: any, data: any, parentKey = "") { + if (Array.isArray(data) || (typeof data === "object" && data !== null)) { + // 如果数据是数组或对象,序列化为 JSON 字符串 + formData.append(parentKey, JSON.stringify(data)); + } else if (data !== null && data !== undefined && !Number.isNaN(data) && data !== "") { + // 如果数据是基本类型,直接添加 + formData.append(parentKey, data); + } + } + for (let key in obj) { + if (obj.hasOwnProperty(key)) { + deepAppendFormData(formData, obj[key], key); + } + } + deepAppendFormData(formData, obj); + return formData; +}; diff --git a/src/utils/px2px.ts b/src/utils/px2px.ts new file mode 100644 index 0000000..bb71b17 --- /dev/null +++ b/src/utils/px2px.ts @@ -0,0 +1,15 @@ +/** + * px转响应式px + * 示例:$px2px(20, 1920),在1920px的设计稿上元素大小为20px,若在其它窗口大小下,会根据窗口大小自动缩放 + * @param {number} px 设计稿元素大小 + * @param {number} viewport 设计稿窗口大小 + * @returns {number} 响应式px + */ +export const px2px = (px: number, viewport: number): number => { + const viewportWidth: number = (window.innerWidth || document.documentElement.clientWidth) as number; + let originalWidth = px; + let originalViewport = viewport; + // 计算新视口下的宽度 + let newWidth = (originalWidth / originalViewport) * viewportWidth; + return parseInt(String(newWidth)); +};