export default class utils { // 格式化为json字符串 static formatJson(jsonString: string) { try { return JSON.stringify(JSON.parse(jsonString), null, 2); // 对JSON字符串进行格式化处理 } catch (error) { return 'Invalid JSON' } } // 格式化为json static formatJsonObj(jsonString: string) { try { return JSON.parse(jsonString) // 对JSON字符串进行格式化处理 } catch (error) { return JSON.parse('{"error": "Invalid JSON"}') } } // 生成年月日时分秒毫秒字符串 static genDateTimeStr() { const now = new Date(); const year = now.getFullYear(); const month = (now.getMonth() + 1).toString().padStart(2, '0'); const day = now.getDate().toString().padStart(2, '0'); const hours = now.getHours().toString().padStart(2, '0'); const minutes = now.getMinutes().toString().padStart(2, '0'); const seconds = now.getSeconds().toString().padStart(2, '0'); const milliseconds = now.getMilliseconds().toString().padStart(3, '0'); const formattedDateTime = `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`; // console.log(formattedDateTime); // 输出类似:20221231120530123 return formattedDateTime } }