future 1 年之前
父节点
当前提交
0e22618fec
共有 3 个文件被更改,包括 54 次插入12 次删除
  1. 50 8
      xs-app/framework/util.ts
  2. 3 3
      xs-app/pages/bookshelf/bookshelf-bookList.vue
  3. 1 1
      xs-app/stores/readHistoryManager.ts

+ 50 - 8
xs-app/framework/util.ts

@@ -159,13 +159,55 @@ export class util {
 		});
 		});
 	}
 	}
 	
 	
-	// 日期格式
-	public static formatDate(timestamp:number):string {
-		var date = new Date(timestamp); // 时间戳转换成Date对象
-	    let year = date.getFullYear();  // 获取年份
-	    let month = (date.getMonth() + 1).toString().padStart(2, '0');
-	    let day = date.getDate().toString().padStart(2, '0');
-	    return `${year}-${month}-${day}`;
+	/**
+	 * 格式化时间
+	 * dateTime(String|Number) 需要格式化的时间戳
+	 * formatStr(String) 格式化规则 yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合 默认yyyy-mm-dd
+	 */
+	public static timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') {
+		let date:any
+		// 若传入时间为假值,则取当前时间
+		if (!dateTime) {
+			date = new Date()
+		}
+		// 若为unix秒时间戳,则转为毫秒时间戳(逻辑有点奇怪,但不敢改,以保证历史兼容)
+		else if (/^\d{10}$/.test(dateTime?.toString().trim())) {
+			date = new Date(dateTime * 1000)
+		}
+		// 若用户传入字符串格式时间戳,new Date无法解析,需做兼容
+		else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) {
+			date = new Date(Number(dateTime))
+		}
+		// 处理平台性差异,在Safari/Webkit中,new Date仅支持/作为分割符的字符串时间
+		// 处理 '2022-07-10 01:02:03',跳过 '2022-07-10T01:02:03'
+		else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) {
+			date = new Date(dateTime.replace(/-/g, '/'))
+		}
+		// 其他都认为符合 RFC 2822 规范
+		else {
+			date = new Date(dateTime)
+		}
+	
+		const timeSource = {
+			'y': date.getFullYear().toString(), // 年
+			'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月
+			'd': date.getDate().toString().padStart(2, '0'), // 日
+			'h': date.getHours().toString().padStart(2, '0'), // 时
+			'M': date.getMinutes().toString().padStart(2, '0'), // 分
+			's': date.getSeconds().toString().padStart(2, '0') // 秒
+			// 有其他格式化字符需求可以继续添加,必须转化成字符串
+		}
+	
+		for (const key in timeSource) {
+			const [ret] = new RegExp(`${key}+`).exec(formatStr) || []
+			if (ret) {
+				// 年可能只需展示两位
+				const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0
+				formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex))
+			}
+		}
+	
+		return formatStr
 	}
 	}
 	
 	
 	// 转换时间(timeStamp:毫秒)
 	// 转换时间(timeStamp:毫秒)
@@ -188,7 +230,7 @@ export class util {
 		} else if (between >= 86400 * 2 && between < 86400 * 30) {
 		} else if (between >= 86400 * 2 && between < 86400 * 30) {
 		    return `${Math.round(between/86400)}天前`
 		    return `${Math.round(between/86400)}天前`
 		} else {
 		} else {
-		    return util.formatDate(timeStamp)
+		    return util.timeFormat(timeStamp)
 		}
 		}
 	}
 	}
 
 

+ 3 - 3
xs-app/pages/bookshelf/bookshelf-bookList.vue

@@ -66,9 +66,9 @@
 	const props = defineProps({
 	const props = defineProps({
 		book_list: Array<book_item_data>,
 		book_list: Array<book_item_data>,
 	})
 	})
-	watch(()=>props.book_list, (new_v,old_v)=>{
-	    console.log('book_list-观察 新值:',new_v,'旧值:',old_v)
-	},{immediate:true})
+	// watch(()=>props.book_list, (new_v,old_v)=>{
+	//     console.log('book_list-观察 新值:',new_v,'旧值:',old_v)
+	// },{immediate:true})
 	const emits = defineEmits(['clickBook','clickButtonStatus','clickDeleteBook'])
 	const emits = defineEmits(['clickBook','clickButtonStatus','clickDeleteBook'])
 	
 	
 	function clickBook(book_data:book_item_data, index:number) {
 	function clickBook(book_data:book_item_data, index:number) {

+ 1 - 1
xs-app/stores/readHistoryManager.ts

@@ -39,7 +39,7 @@ export class ReadHistoryManager {
 	
 	
 	// 添加书
 	// 添加书
 	public static async addBook(book_data:book_item_data, cb:Function) {
 	public static async addBook(book_data:book_item_data, cb:Function) {
-		let read_hisroty_time = util.formatDate(new Date().getTime())
+		let read_hisroty_time = util.timeFormat(new Date().getTime())
 		book_data.read_hisroty_time = read_hisroty_time
 		book_data.read_hisroty_time = read_hisroty_time
 		
 		
 		ReadHistoryManager.checkBookOnReadHistory(book_data.book_id, (index:number)=>{
 		ReadHistoryManager.checkBookOnReadHistory(book_data.book_id, (index:number)=>{