an_xiao/police_uniapp/utils/index.js

175 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-07-25 16:03:08 +08:00
/**
* 提示框
* @property {opt} Object 选项
* @argument 标题 - title
* @argument 持续时间 - duration
* @argument 图标 - icon
*/
export function customTotast(opt = {}) {
let timer = null;
return () => {
const options = Object.assign({
title: '',
duration: 3000,
icon: 'none'
}, opt)
if (timer) {
clearTimeout(timer)
timer = null;
uni.hideToast();
};
uni.showToast(options);
timer = setTimeout(() => {
uni.hideToast();
clearTimeout(timer)
timer = null;
}, options.duration)
}
}
/**
* 判断是否为空
* @param {Object} str 字符串
*/
export function isEmpty(str) {
if (!str) return true;
return str.trim().length == 0
}
/**
* 是否是手机号
* @param {Object} str 字符串
*/
export function isPhone(str) {
return /^1[3-9][0-9]{9}$/.test(str)
}
/**
* 获取合理时间
* @param {Object} time 时间字符串
*/
export function getTime(time) {
const s = (new Date().getTime() - new Date(time).getTime()) / 1000;
if (s <= 660) {
return "10分钟之前"
} else if (s > 660 && s <= 3600) {
return "1小时以内"
} else if (s > 3600 && s <= 10800) {
return "3小时以内"
} else if (s > 10800 && s <= 86400) {
return "24小时以内"
} else if (s > 86400 && s <= 259200) {
return "3天以内"
} else if (s > 259200 && s <= 604800) {
return "一周以内"
} else if (s > 604800 && s <= 2592000) {
return "一个月以内"
} else if (s > 2592000 && s <= 31536000) {
return "一年之前"
} else if (s > 31536000 && s <= 94608000) {
return "三年之前"
} else {
return "超出范围"
}
}
/**
* @param {Object} url url链接
*/
export function copyUrl(url) {
uni.setClipboardData({
data: url,
success: function() {
uni.showToast({
title: '复制成功',
duration: 2000,
icon: 'none'
});
},
fail: function(err) {
uni.showToast({
title: '复制失败',
duration: 2000,
icon: 'none'
});
}
});
}
//秒装换成时间
export function timestampFormatTime() {
var currentTime = new Date();
var year = currentTime.getFullYear();
var getMonth = (currentTime.getMonth()) + 1;
var getDate = currentTime.getDate();
var getHours = currentTime.getHours();
var getMinutes = currentTime.getMinutes();
var getSeconds = currentTime.getSeconds();
if (getMonth < 10) {
getMonth = "0" + getMonth;
}
if (getDate < 10) {
getDate = "0" + getDate;
}
if (getHours < 10) {
getHours = "0" + getHours;
}
if (getMinutes < 10) {
getMinutes = "0" + getMinutes;
}
if (getSeconds < 10) {
getSeconds = "0" + getSeconds;
}
return year + "-" + getMonth + "-" + getDate + " " + getHours + ":" + getMinutes + ":" + getSeconds;
}
export function Time() {
let dataTime = new Date();
let years = dataTime.getFullYear();
let getMonths = (dataTime.getMonth()) + 1 ;
var schoolTimes = ''
if(getMonths > 2 && getMonths < 9){
schoolTimes = "上半年"
}else{
schoolTimes = "下半年"
}
return years + "年" + schoolTimes
}
//秒装换成时间
export function timeFormat() {
var currentTime = new Date();
var year = currentTime.getFullYear();
var getMonth = (currentTime.getMonth()) + 1;
var getDate = currentTime.getDate();
if (getMonth < 10) {
getMonth = "0" + getMonth;
}
if (getDate < 10) {
getDate = "0" + getDate;
}
return year + "-" + getMonth + "-" + getDate ;
}
//秒装换成时间
export function timeSecurity() {
var currentTime = new Date();
var year = currentTime.getFullYear();
var getMonth = (currentTime.getMonth()) + 1;
var getDate = currentTime.getDate();
if (getMonth < 10) {
getMonth = "0" + getMonth;
}
if (getDate < 10) {
getDate = "0" + getDate;
}
return year + "年" + getMonth + "月" + getDate + '日' ;
}