You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

/*
* 公共方法
*
*/
12 months ago
import moment from 'moment';
import { ROOTPATH } from "@/common/config.js"
const base = {
toast : (msg, time, callback) => {
uni.showToast({
icon: "none",
title: msg,
duration: time || 2000,
success: function(res) {
if (callback && typeof(callback) == 'function') {
console.log(callback)
callback()
}
}
})
},
// 验证是否为空
isNull : (p) => {
return p == '' || p == undefined || p == null || p == 'undefined' || p == 'null';
},
11 months ago
getAgeFromId(idNumber) {
if (!idNumber || idNumber.length !== 18) {
return ''
12 months ago
}
11 months ago
const today = new Date();
const birthDate = new Date(idNumber.substring(6, 10), idNumber.substring(10, 12) - 1, idNumber.substring(12, 14));
let age = today.getFullYear() - birthDate.getFullYear();
const m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
12 months ago
}
11 months ago
return age + '岁';
},
// 格式化中国日期兼容iOS
formatChinaDate : (dateStr) => {
if (!dateStr) return '';
// 处理iOS不支持的日期格式
let processedDateStr = dateStr;
if (typeof dateStr === 'string') {
// 将 "2025-06-25 01:31:10" 格式转换为 "2025-06-25T01:31:10"
if (dateStr.includes('-') && dateStr.includes(' ') && dateStr.includes(':')) {
processedDateStr = dateStr.replace(' ', 'T');
}
}
const date = new Date(processedDateStr);
// 检查日期是否有效
if (isNaN(date.getTime())) {
console.warn('Invalid date format:', dateStr);
return '';
}
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
return `${y}-${m}-${d}`;
12 months ago
}
}
export {
base
12 months ago
}