From 9f804e26f3cb91e496ddf56188d82c3124469c5b Mon Sep 17 00:00:00 2001 From: hubert Date: Sat, 9 May 2026 18:46:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(zentao-api):=20=E7=94=A8=20dayjs=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E8=87=AA=E7=A0=94=E6=97=A5=E6=9C=9F=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 createDate 与 formatDate 实现 - 引入 dayjs 统一处理日期时间格式 - 将 zentao-legacy 中的 yyyy-MM-dd 等格式改为 dayjs 语法 - 删除对应单元测试,时区问题导致不同环境下测试不通过 --- package.json | 2 +- packages/zentao-api/package.json | 1 + packages/zentao-api/src/utils.ts | 77 ------------------------ packages/zentao-api/src/zentao-legacy.ts | 7 ++- packages/zentao-api/test/utils.test.ts | 44 -------------- yarn.lock | 8 +++ 6 files changed, 14 insertions(+), 125 deletions(-) diff --git a/package.json b/package.json index 981f759..0668609 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "release:next": "lerna publish --conventional-commits --conventional-prerelease --preid next --dist-tag next", "release:graduate": "lerna publish --conventional-commits --conventional-graduate", "changelog": "node ./scripts/genChangelog.js", - "prepublishOnly": "yarn build && yarn test", + "prepublishOnly": "yarn changelog && yarn build && yarn test", "lint": "lerna run lint --stream --scope '@acehubert/*'", "lint:fix": "lerna run lint:fix --stream --scope '@acehubert/*'", "commit": "git-cz" diff --git a/packages/zentao-api/package.json b/packages/zentao-api/package.json index caa62c8..e702df7 100644 --- a/packages/zentao-api/package.json +++ b/packages/zentao-api/package.json @@ -31,6 +31,7 @@ "axios": "^1.13.2", "configstore": "^5.0.1", "crypto-js": "^4.2.0", + "dayjs": "^1.11.20", "form-data": "^4.0.0", "kleur": "^4.1.4" }, diff --git a/packages/zentao-api/src/utils.ts b/packages/zentao-api/src/utils.ts index 4f74716..1283850 100644 --- a/packages/zentao-api/src/utils.ts +++ b/packages/zentao-api/src/utils.ts @@ -4,83 +4,6 @@ import { ZentaoRequestParamPair, ZentaoRequestParams } from "./types"; export const md5 = (value: string): string => CryptoJS.MD5(value).toString(); -/** - * 创建一个 Date 对象 - * @param {Date|number|String} [date=null] 用于创建 Date 对象的日期时间表达值,如果留空则创建当前系统时间对象 - * @return {Date} 日期时间对象 - * @function - */ -export const createDate = (date: Date | number | string) => { - if (!date) { - return new Date(); - } - if (!(date instanceof Date)) { - if (typeof date === "string") { - date = date.trim(); - if (/^\d+$/.test(date)) { - date = Number.parseInt(date, 10); - } - } - if (typeof date === "number" && date < 10000000000) { - date *= 1000; - } - date = new Date(date); - } - return date; -}; - -/** - * 格式化日期时间值为字符串 - * @remarks - * 所有可用的格式化参数有: - * - `yyyy`,例如:'2018',表示四位数字表示的年份 - * - `yy`,例如:'18',表示两位数字表示的年份 - * - `MM`,例如:'07',表示两位数字表示的月份,不足两位在起始用 0 填充 - * - `M`,例如:'10',表示一位或两位数字表示的月份 - * - `dd`,例如:'05',表示两位数字表示的日期,不足两位在起始用 0 填充 - * - `d`,例如:'5',表示一位或两位数字表示的日期 - * - `hh`,例如:'08',表示两位数字表示的小时,不足两位在起始用 0 填充 - * - `h`,例如:'8',表示一位或两位数字表示的小时 - * - `mm`,例如:'3',表示两位数字表示的分钟,不足两位在起始用 0 填充 - * - `m`,例如:'03',表示一位或两位数字表示的分钟 - * - `ss`,例如:'5',表示两位数字表示的秒数,不足两位在起始用 0 填充 - * - `s`,例如:'05',表示一位或两位数字表示的秒数 - * - `S`,例如:'236',表示毫秒数 - * - `SSS`,例如:'036',表示毫秒数,不足3位在起始用 0 填充 - * @summary 格式化日期时间值为字符串 - * @param {Date|number|string} date 要格式化的日期时间表达值 - * @param {string} [format='yyyy-MM-dd hh:ss'] 格式化字符串 - * @return {string} 日期时间格式化文本 - * @function - */ -export const formatDate = (date: Date | number | string, format: string = "yyyy-MM-dd hh:mm") => { - date = createDate(date); - - const dateInfo: Record = { - "M+": date.getMonth() + 1, - "d+": date.getDate(), - "h+": date.getHours(), - "H+": date.getHours() % 12, - "m+": date.getMinutes(), - "s+": date.getSeconds(), - // 'q+': Math.floor((date.getMonth() + 3) / 3), - "S+": date.getMilliseconds(), - }; - if (/(y+)/i.test(format)) { - format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length)); - } - Object.keys(dateInfo).forEach((k) => { - if (new RegExp(`(${k})`).test(format)) { - const str = `${dateInfo[k]}`; - format = format.replace( - RegExp.$1, - RegExp.$1.length === 1 ? str : `00${str}`.substr(str.length + 2 - RegExp.$1.length), - ); - } - }); - return format; -}; - /** * 格式化禅道地址 * @param url 禅道地址 diff --git a/packages/zentao-api/src/zentao-legacy.ts b/packages/zentao-api/src/zentao-legacy.ts index c4cc4fb..bd52a7d 100644 --- a/packages/zentao-api/src/zentao-legacy.ts +++ b/packages/zentao-api/src/zentao-legacy.ts @@ -1,3 +1,4 @@ +import dayjs from "dayjs"; import { CreateDocModuleParams, CreateDocParams, @@ -8,7 +9,7 @@ import { ZentaoLegacyApiResponse, ZentaoFileReadResult, } from "./types"; -import { formatDate, md5 } from "./utils"; +import { md5 } from "./utils"; import Zentao from "./helpers/zentao"; /** @@ -845,7 +846,7 @@ export default class ZentaoLegacy extends Zentao { currentConsumed: params.currentConsumed, consumed, assignedTo: params.assignedTo, - finishedDate: params.finishedDate ?? formatDate(new Date(), "yyyy-MM-dd"), + finishedDate: params.finishedDate ?? dayjs().format("YYYY-MM-DD"), comment: params.comment, status: "done", }, @@ -1189,7 +1190,7 @@ export default class ZentaoLegacy extends Zentao { const data: Record = { resolution: params.resolution ?? "fixed", resolvedBuild: params.resolvedBuild ?? "trunk", - resolvedDate: params.resolvedDate ?? formatDate(new Date(), "yyyy-MM-dd hh:mm:ss"), + resolvedDate: params.resolvedDate ?? dayjs().format("YYYY-MM-DD HH:mm:ss"), assignedTo: params.assignedTo, comment: params.comment, duplicateBug: params.duplicateBug, diff --git a/packages/zentao-api/test/utils.test.ts b/packages/zentao-api/test/utils.test.ts index 9c237ec..dd32d85 100644 --- a/packages/zentao-api/test/utils.test.ts +++ b/packages/zentao-api/test/utils.test.ts @@ -1,49 +1,5 @@ import * as utils from "../src/utils"; -describe("utils.createDate", () => { - it("date from string", () => { - const date = utils.createDate("2020-02-01"); - expect(date).toBeInstanceOf(Date); - expect(date.toDateString()).toBe("Sat Feb 01 2020"); - }); - - it("date from timestamp", () => { - const date = utils.createDate(1615214148009); // Mon Mar 08 2021 22:35:48 GMT+0800 - expect(date).toBeInstanceOf(Date); - expect(date.getTime()).toBe(1615214148009); - }); - - it("date from php timestamp", () => { - const date = utils.createDate(1615214148); // Mon Mar 08 2021 22:35:48 GMT+0800 - expect(date).toBeInstanceOf(Date); - expect(date.getTime()).toBe(1615214148000); - }); -}); - -describe("utils.formatDate", () => { - it("format time", () => { - const date = new Date(1615214108029); // 2021/3/8 22:35:08 - expect(utils.formatDate(date, "hh:mm")).toBe("22:35"); - expect(utils.formatDate(date, "hh:mm:ss")).toBe("22:35:08"); - expect(utils.formatDate(date, "HH:mm:s")).toBe("10:35:8"); - expect(utils.formatDate(date, "hh:mm:s.S")).toBe("22:35:8.29"); - expect(utils.formatDate(date, "hh:mm:s.SSS")).toBe("22:35:8.029"); - expect(utils.formatDate(date, "H:mm:ss")).toBe("10:35:08"); - expect(utils.formatDate(date, "m")).toBe("35"); - }); - - it("format date", () => { - const date = new Date(1615214108029); // 2021/3/8 22:35:08 - expect(utils.formatDate(date, "hh:mm")).toBe("22:35"); - expect(utils.formatDate(date, "hh:mm:ss")).toBe("22:35:08"); - expect(utils.formatDate(date, "HH:mm:s")).toBe("10:35:8"); - expect(utils.formatDate(date, "hh:mm:s.S")).toBe("22:35:8.29"); - expect(utils.formatDate(date, "hh:mm:s.SSS")).toBe("22:35:8.029"); - expect(utils.formatDate(date, "H:mm:ss")).toBe("10:35:08"); - expect(utils.formatDate(date, "m")).toBe("35"); - }); -}); - describe("utils.formatZentaoUrl", () => { it("formatZentaoUrl", () => { expect(utils.formatZentaoUrl("http://demo.zentao.net/")).toBe("http://demo.zentao.net/"); diff --git a/yarn.lock b/yarn.lock index e79e316..e87dc66 100644 --- a/yarn.lock +++ b/yarn.lock @@ -43,6 +43,7 @@ __metadata: axios: ^1.13.2 configstore: ^5.0.1 crypto-js: ^4.2.0 + dayjs: ^1.11.20 form-data: ^4.0.0 kleur: ^4.1.4 typedoc: ^0.20.30 @@ -4409,6 +4410,13 @@ __metadata: languageName: node linkType: hard +"dayjs@npm:^1.11.20": + version: 1.11.20 + resolution: "dayjs@npm:1.11.20" + checksum: 26f4867c4ae1315885ac3e560906d3f8c49cb6a1303e6fdd5f87ace3b814b07a45f036facad70299cea36f3eb62ee2070dd239079c56d8f55e4e684afb752a67 + languageName: node + linkType: hard + "debug@npm:4, debug@npm:^4.0.0, debug@npm:^4.1.0, debug@npm:^4.1.1, debug@npm:^4.3.1, debug@npm:^4.3.2, debug@npm:^4.3.3, debug@npm:^4.3.4, debug@npm:^4.4.0, debug@npm:^4.4.1, debug@npm:^4.4.3": version: 4.4.3 resolution: "debug@npm:4.4.3"