YZProjectCloud/yanzhu-ui-app/miniprogram/utils/format.wxs

140 lines
3.2 KiB
Plaintext
Raw Normal View History

2025-10-31 14:18:44 +08:00
// 引入 WXS 配置文件
var config = require("./config.wxs");
2024-10-13 11:24:45 +08:00
/**
* 序号格式化
2025-10-31 14:18:44 +08:00
* @param num
2024-10-13 11:24:45 +08:00
*/
function indexNumFormat(num) {
2025-08-02 15:44:20 +08:00
switch (num) {
case 0:
num = "";
break;
case 1:
num = "Ⅱ";
break;
case 2:
num = "Ⅲ";
break;
case 3:
num = "Ⅳ";
break;
case 4:
num = "";
break;
case 5:
num = "Ⅵ";
break;
case 6:
num = "Ⅶ";
break;
case 7:
num = "Ⅷ";
break;
case 8:
num = "Ⅸ";
break;
}
return num;
2024-10-13 11:24:45 +08:00
}
/**
* 计算时长
2025-10-31 14:18:44 +08:00
* @param val
2024-10-13 11:24:45 +08:00
*/
function findDurationDate(val) {
2025-08-02 15:44:20 +08:00
// 计算出相差天数
2025-10-31 14:18:44 +08:00
let days = Math.floor(val / (24 * 3600 * 1000));
2025-08-02 15:44:20 +08:00
// 计算出小时数
2025-10-31 14:18:44 +08:00
let leave1 = val % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
let hours = Math.floor(leave1 / (3600 * 1000));
2025-08-02 15:44:20 +08:00
// 计算相差分钟数
2025-10-31 14:18:44 +08:00
let leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
let minutes = Math.floor(leave2 / (60 * 1000));
2025-08-02 15:44:20 +08:00
// 计算相差秒数
2025-10-31 14:18:44 +08:00
let leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
let seconds = Math.round(leave3 / 1000);
2025-08-02 15:44:20 +08:00
if (days > 0) {
if (days < 10) days = "0" + days;
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
2025-10-31 14:18:44 +08:00
return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
2025-08-02 15:44:20 +08:00
}
if (hours > 0) {
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
2025-10-31 14:18:44 +08:00
return hours + "小时" + minutes + "分钟" + seconds + "秒";
2025-08-02 15:44:20 +08:00
}
if (minutes > 0) {
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
2025-10-31 14:18:44 +08:00
return minutes + "分钟" + seconds + "秒";
2025-08-02 15:44:20 +08:00
}
if (seconds > 0) {
if (seconds < 10) seconds = "0" + seconds;
2025-10-31 14:18:44 +08:00
return seconds + "秒";
2025-08-02 15:44:20 +08:00
}
2024-10-13 11:24:45 +08:00
}
module.exports = {
2025-08-02 15:44:20 +08:00
indexNumFormat: indexNumFormat,
findDurationDate: findDurationDate,
split: function (str, sign) {
return str.split(sign);
},
parseStr: function (str) {
return JSON.parse(str);
},
evalStr: function (str) {
return eval(str);
},
dateStrEv: function (startDate, endDate) {
if (startDate) {
return startDate.split(" ")[0];
} else if (endDate) {
return endDate.split(" ")[0];
} else {
return "未找到记录";
}
},
dateStr: function (str) {
if (str) {
return str.split(" ")[0];
} else {
return "";
}
},
timeStr: function (str) {
return str.split(" ")[1];
},
isHttpImg: function (str) {
if (str.indexOf("http:") > -1 || str.indexOf("https:") > -1) {
return true;
} else {
return false;
}
},
httpImg: function (str) {
if (str.indexOf("https:") > -1) {
return str;
} else {
2025-10-31 14:18:44 +08:00
// 使用 WXS 配置文件中的 baseImgUrl确保与 JS 配置同步
return config.baseImgUrl + str;
2025-08-02 15:44:20 +08:00
}
},
birthDate: function (timestamp) {
var date = getDate(parseInt(timestamp));
var mm = date.getMonth() + 1;
if (mm < 10) {
2025-10-31 14:18:44 +08:00
mm = "0" + mm;
2025-08-02 15:44:20 +08:00
}
var dd = date.getDate();
if (dd < 10) {
2025-10-31 14:18:44 +08:00
dd = "0" + dd;
2025-08-02 15:44:20 +08:00
}
return date.getFullYear() + "-" + mm + "-" + dd;
2025-10-31 14:18:44 +08:00
},
};