Commit 31232f8c authored by leon's avatar leon

feat: common add weekly and diffOfNow function

parent 9b466372
......@@ -130,6 +130,9 @@ const install = (Vue, vm) => {
if (paramStr.includes('@month')) {
paramStr = paramStr.replace(/@month/g, monthly())
}
if (paramStr.includes('@weekly')) {
paramStr = paramStr.replace(/@weekly/g, mweekly())
}
if (paramStr.includes('@day')) {
paramStr = paramStr.replace(/@day/g, day())
}
......@@ -140,7 +143,23 @@ const install = (Vue, vm) => {
if (paramStr.includes('@yesterday')) {
paramStr = paramStr.replace(/@yesterday/g, yesterday())
}
// 匹配以@开头,空格 或者 " 结尾的的字符串
const pattern = /((?<=@).*?(?=\s))|((?<=@).*?(?="))/g
const match = paramStr.match(pattern)
if (match?.length) {
match.forEach((item) => {
if (item.includes('diffOfNow')) {
// 取出()里面的内容
const regex = /(?<=\()\S+(?=\))/g
const value = item.match(regex)
if (value?.length) {
diffOfNow(parseInt(value[0]))
const str = '@' + item
paramStr = paramStr.replace(str, diffOfNow(parseInt(value[0])))
}
}
})
}
return JSON.parse(paramStr)
}
......@@ -156,6 +175,17 @@ const install = (Vue, vm) => {
return month
}
const weekly = () => {
let d1 = new Date();
let d2 = new Date();
d2.setMonth(0);
d2.setDate(3);//3 周一为本周第一天;2 周日为本周第一天;1 周六为本周第一天
let rq = d1 - d2;
let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
let weekly = Math.ceil(days / 7);
return weekly + 1;
}
const day = () => {
const date = new Date();
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
......@@ -173,8 +203,12 @@ const install = (Vue, vm) => {
}
const yesterday = () => {
return diffOfNow(-1)
}
const diffOfNow = (diff) => {
const date = new Date()
const timestamp = date.getTime() - 1000 * 24 * 60 * 60
const timestamp = date.getTime() + diff * 1000 * 24 * 60 * 60
date.setTime(timestamp)
const year = date.getFullYear();
// 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一 如 09:11:05
......@@ -182,6 +216,7 @@ const install = (Vue, vm) => {
const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
// 拼接
return year + "-" + month + "-" + day;
}
vm.$u.common = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment