·修改部分页面数据冗余

This commit is contained in:
lemon橪
2021-05-14 17:31:40 +08:00
parent 23804939eb
commit 7f4212755d
42 changed files with 1415 additions and 2987 deletions

View File

@@ -1,5 +1,6 @@
/**
* 一些常用的基础方法
* whetherNavigate 登录后跳转判断
* unixToDate 将unix时间戳转换为指定格式
* dateToUnix 将时间转unix时间戳
* deepClone 对一个对象进行深拷贝
@@ -8,6 +9,39 @@
* randomString 随机生成指定长度的字符串
*/
/**
* 登录后跳转判断
* 计算出当前router路径
* 1.如果跳转的链接为登录页面或跳转的链接为空页面。则会重新跳转到首页
* 2.都不满足返回跳转页面
*/
export function whetherNavigate() {
if (getCurrentPages().length > 1) {
if ((getCurrentPages().length - 2).route == "pages/passport/login") {
uni.switchTab({
url: "/pages/tabbar/home/index",
});
} else {
if (
!(getCurrentPages().length - 2).route ||
(getCurrentPages().length - 2).route == "undefined"
) {
uni.switchTab({
url: "/pages/tabbar/home/index",
});
} else {
uni.navigateBack({
delta: getCurrentPages().length - 2,
});
}
}
} else {
uni.switchTab({
url: "/pages/tabbar/home/index",
});
}
}
/**
* 将unix时间戳转换为指定格式
* @param unix 时间戳【秒】
@@ -15,23 +49,30 @@
* @returns {*|string}
*/
export function unixToDate(unix, format) {
if (!unix) return unix
let _format = format || 'yyyy-MM-dd hh:mm:ss'
const d = new Date(unix)
const o = {
'M+': d.getMonth() + 1,
'd+': d.getDate(),
'h+': d.getHours(),
'm+': d.getMinutes(),
's+': d.getSeconds(),
'q+': Math.floor((d.getMonth() + 3) / 3),
S: d.getMilliseconds()
}
if (/(y+)/.test(_format)) _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
for (const k in o)
if (new RegExp('(' + k + ')').test(_format)) _format = _format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) :
(('00' + o[k]).substr(('' + o[k]).length)))
return _format
if (!unix) return unix;
let _format = format || "yyyy-MM-dd hh:mm:ss";
const d = new Date(unix);
const o = {
"M+": d.getMonth() + 1,
"d+": d.getDate(),
"h+": d.getHours(),
"m+": d.getMinutes(),
"s+": d.getSeconds(),
"q+": Math.floor((d.getMonth() + 3) / 3),
S: d.getMilliseconds(),
};
if (/(y+)/.test(_format))
_format = _format.replace(
RegExp.$1,
(d.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (const k in o)
if (new RegExp("(" + k + ")").test(_format))
_format = _format.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
return _format;
}
/**
@@ -40,18 +81,20 @@ export function unixToDate(unix, format) {
* @returns {number} 【秒】
*/
export function dateToUnix(date) {
let newStr = date.replace(/:/g, '-')
newStr = newStr.replace(/ /g, '-')
const arr = newStr.split('-')
const datum = new Date(Date.UTC(
arr[0],
arr[1] - 1,
arr[2],
arr[3] - 8 || -8,
arr[4] || 0,
arr[5] || 0
))
return parseInt(datum.getTime() / 1000)
let newStr = date.replace(/:/g, "-");
newStr = newStr.replace(/ /g, "-");
const arr = newStr.split("-");
const datum = new Date(
Date.UTC(
arr[0],
arr[1] - 1,
arr[2],
arr[3] - 8 || -8,
arr[4] || 0,
arr[5] || 0
)
);
return parseInt(datum.getTime() / 1000);
}
/**
@@ -60,8 +103,8 @@ export function dateToUnix(date) {
* @returns {string}
*/
export function formatPrice(price) {
if (typeof price !== 'number') return price
return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
if (typeof price !== "number") return price;
return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/**
@@ -71,11 +114,11 @@ export function formatPrice(price) {
* @returns {*}
*/
export function secrecyMobile(mobile) {
mobile = String(mobile)
if (!/\d{11}/.test(mobile)) {
return mobile
}
return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3')
mobile = String(mobile);
if (!/\d{11}/.test(mobile)) {
return mobile;
}
return mobile.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");
}
/**
@@ -84,13 +127,14 @@ export function secrecyMobile(mobile) {
* @returns {string}
*/
export function randomString(length = 32) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const maxPos = chars.length
let _string = ''
for (let i = 0; i < length; i++) {
_string += chars.charAt(Math.floor(Math.random() * maxPos))
}
return _string
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const maxPos = chars.length;
let _string = "";
for (let i = 0; i < length; i++) {
_string += chars.charAt(Math.floor(Math.random() * maxPos));
}
return _string;
}
/**
@@ -100,16 +144,16 @@ export function randomString(length = 32) {
*/
export function countTimeDown(seconds) {
const leftTime = (time) => {
if (time < 10) time = '0' + time
return time + ''
}
return {
day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
hours: leftTime(parseInt(seconds / 60 / 60 % 24, 10)),
minutes: leftTime(parseInt(seconds / 60 % 60, 10)),
seconds: leftTime(parseInt(seconds % 60, 10))
}
const leftTime = (time) => {
if (time < 10) time = "0" + time;
return time + "";
};
return {
day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
hours: leftTime(parseInt((seconds / 60 / 60) % 24, 10)),
minutes: leftTime(parseInt((seconds / 60) % 60, 10)),
seconds: leftTime(parseInt(seconds % 60, 10)),
};
}
/**
@@ -117,16 +161,25 @@ export function countTimeDown(seconds) {
* @returns {number}
*/
export function theNextDayTime() {
const nowDate = new Date()
const time = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1, 0, 0, 0).getTime() - nowDate.getTime()
return parseInt(time / 1000)
const nowDate = new Date();
const time =
new Date(
nowDate.getFullYear(),
nowDate.getMonth(),
nowDate.getDate() + 1,
0,
0,
0
).getTime() - nowDate.getTime();
return parseInt(time / 1000);
}
module.exports = {
unixToDate,
dateToUnix,
formatPrice,
secrecyMobile,
randomString,
countTimeDown,
theNextDayTime
}
unixToDate,
dateToUnix,
formatPrice,
secrecyMobile,
randomString,
countTimeDown,
theNextDayTime,
whetherNavigate
};