修改功能,现已测试注册登录忘记密码

This commit is contained in:
rain
2026-06-12 11:42:49 +08:00
parent 0bf2f573ed
commit cfc64c69e5
26 changed files with 2238 additions and 898 deletions
+77 -104
View File
@@ -1,114 +1,87 @@
/**
* 日期时间工具类
*/
class DateUtil {
/**
* 将日期字符串转换为"月日"格式,例如:"5月2日"
* @param {string|Date} dateStr - 日期字符串或Date对象,如 "2026-05-02" 或 Date对象
* @returns {string} 返回格式化的日期字符串,如 "5月2日"
*/
static formatDateToMonthDay(dateStr) {
try {
const date = typeof dateStr === 'string' ? new Date(dateStr) : dateStr;
static formatDateToMonthDay(dateStr) {
const date = DateUtil._parseDate(dateStr);
if (!DateUtil._isValidDate(date)) return '';
return `${date.getMonth() + 1}${date.getDate()}`;
}
if (isNaN(date.getTime())) {
console.error('Invalid date:', dateStr);
return '';
}
static formatDateToYearMonthDay(dateStr) {
const date = DateUtil._parseDate(dateStr);
if (!DateUtil._isValidDate(date)) return '';
return `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`;
}
const month = date.getMonth() + 1; // getMonth()返回0-11,需要加1
const day = date.getDate();
return `${month}${day}`;
} catch (error) {
console.error('Error formatting date:', error);
return '';
}
static formatDateToYMD(dateStr) {
const date = DateUtil._parseDate(dateStr);
if (!DateUtil._isValidDate(date)) return '';
return [
date.getFullYear(),
String(date.getMonth() + 1).padStart(2, '0'),
String(date.getDate()).padStart(2, '0'),
].join('-');
}
static formatDateTimeToYMDHMS(dateStr) {
const date = DateUtil._parseDate(dateStr);
if (!DateUtil._isValidDate(date)) return '';
return `${DateUtil.formatDateToYMD(date)} ${DateUtil._timeParts(date).join(':')}`;
}
static formatDateTimeToYMDHM(dateStr) {
const date = DateUtil._parseDate(dateStr);
if (!DateUtil._isValidDate(date)) return '';
const [hour, minute] = DateUtil._timeParts(date);
return `${DateUtil.formatDateToYMD(date)} ${hour}:${minute}`;
}
static formatTimeToHMS(dateStr) {
const date = typeof dateStr === 'number' ? new Date(dateStr) : DateUtil._parseDate(dateStr);
if (!DateUtil._isValidDate(date)) return '';
return DateUtil._timeParts(date).join(':');
}
static getCurrentDate() {
return new Date();
}
static isSameDay(date1, date2) {
const d1 = DateUtil._parseDate(date1);
const d2 = DateUtil._parseDate(date2);
if (!DateUtil._isValidDate(d1) || !DateUtil._isValidDate(d2)) return false;
return (
d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate()
);
}
static _parseDate(dateStr) {
if (dateStr instanceof Date) return dateStr;
if (typeof dateStr === 'number') return new Date(dateStr);
if (typeof dateStr !== 'string') return null;
const value = dateStr.trim();
if (!value) return null;
if (/^\d{4}-\d{2}-\d{2}$/.test(value)) {
return new Date(value.replace(/-/g, '/'));
}
return new Date(value);
}
/**
* 将日期字符串转换为"年月日"格式,例如:"2026年5月2日"
* @param {string|Date} dateStr - 日期字符串或Date对象
* @returns {string} 返回格式化的日期字符串,如 "2026年5月2日"
*/
static formatDateToYearMonthDay(dateStr) {
try {
const date = typeof dateStr === 'string' ? new Date(dateStr) : dateStr;
static _isValidDate(date) {
return date instanceof Date && !Number.isNaN(date.getTime());
}
if (isNaN(date.getTime())) {
console.error('Invalid date:', dateStr);
return '';
}
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${year}${month}${day}`;
} catch (error) {
console.error('Error formatting date:', error);
return '';
}
}
/**
* 将日期字符串转换为"YYYY-MM-DD"格式
* @param {string|Date} dateStr - 日期字符串或Date对象
* @returns {string} 返回格式化的日期字符串,如 "2026-05-02"
*/
static formatDateToYMD(dateStr) {
try {
const date = typeof dateStr === 'string' ? new Date(dateStr) : dateStr;
if (isNaN(date.getTime())) {
console.error('Invalid date:', dateStr);
return '';
}
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
} catch (error) {
console.error('Error formatting date:', error);
return '';
}
}
/**
* 获取当前日期
* @returns {Date} 返回当前日期的Date对象
*/
static getCurrentDate() {
return new Date();
}
/**
* 比较两个日期是否为同一天
* @param {string|Date} date1 - 第一个日期
* @param {string|Date} date2 - 第二个日期
* @returns {boolean} 如果是同一天返回true,否则返回false
*/
static isSameDay(date1, date2) {
try {
const d1 = typeof date1 === 'string' ? new Date(date1) : date1;
const d2 = typeof date2 === 'string' ? new Date(date2) : date2;
if (isNaN(d1.getTime()) || isNaN(d2.getTime())) {
return false;
}
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate();
} catch (error) {
console.error('Error comparing dates:', error);
return false;
}
}
static _timeParts(date) {
return [
String(date.getHours()).padStart(2, '0'),
String(date.getMinutes()).padStart(2, '0'),
String(date.getSeconds()).padStart(2, '0'),
];
}
}
// 导出模块,兼容不同引入方式
if (typeof module !== 'undefined' && module.exports) {
module.exports = DateUtil;
module.exports = DateUtil;
} else if (typeof window !== 'undefined') {
window.DateUtil = DateUtil;
}
window.DateUtil = DateUtil;
}