tracexd/target/classes/static/js/app.js
2026-06-09 16:03:44 +08:00

63 lines
1.8 KiB
JavaScript

/**
* TraceXD 前端通用工具函数
*/
(function(global) {
/**
* 格式化时间显示
* 当天 → HH:mm
* 当年非当天 → M-d HH:mm
* 非当年 → yy-M-d HH
*
* @param {string} isoString ISO 日期时间字符串
* @returns {string} 格式化后的时间文本
*/
global.formatTime = function(isoString) {
if (!isoString) return '';
var d = new Date(isoString);
var pad = function(n) { return String(n).padStart(2, '0'); };
return (d.getMonth() + 1) + '-' + d.getDate() + ' ' +
pad(d.getHours()) + ':' + pad(d.getMinutes());
};
/**
* ??????? badge CSS ??
* ??????????????????????????
*/
global.badgeClass = function(category) {
if (!category) return 'badge-default';
var fixed = {
'餐饮': 'badge-c1',
'饮食': 'badge-c1',
'交通': 'badge-c2',
'出行': 'badge-c2',
'购物': 'badge-c3',
'娱乐': 'badge-c4',
'医疗': 'badge-c5',
'健康': 'badge-c5',
'教育': 'badge-c6',
'学习': 'badge-c6',
'养老': 'badge-c7',
'社交': 'badge-c8',
'旅游': 'badge-c9',
'居家': 'badge-c10'
};
if (fixed[category]) return fixed[category];
// hash-based fallback
var hash = 0;
for (var i = 0; i < category.length; i++) {
hash = ((hash << 5) - hash) + category.charCodeAt(i);
hash |= 0;
}
var idx = (Math.abs(hash) % 12) + 1;
return 'badge-c' + idx;
};
global.logout = function() {
fetch('/logout', { method: 'POST' }).then(function() {
window.location.href = '/login?logout';
});
};
})(window);