From 5c9610aeb5967d397e7ee7dd8d245f93b40eedf8 Mon Sep 17 00:00:00 2001 From: chuzhichao Date: Mon, 26 Jun 2023 17:07:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E5=91=98=E4=BF=A1=E6=81=AF=E7=95=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 4 +- src/utils/DateUtil.js | 221 +++++++++++++++++++++++ src/utils/ruoyi.js | 15 +- src/views/ums/member/index.vue | 312 ++++++--------------------------- 4 files changed, 293 insertions(+), 259 deletions(-) create mode 100644 src/utils/DateUtil.js diff --git a/src/main.js b/src/main.js index 3f45547..c21d655 100644 --- a/src/main.js +++ b/src/main.js @@ -19,7 +19,7 @@ import './assets/icons' // icon import './permission' // permission control import { getDicts } from "@/api/system/dict/data"; import { getConfigKey } from "@/api/system/config"; -import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi"; +import { parseTime, resetForm, addDateRange, addDateRange2, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi"; // 分页组件 import Pagination from "@/components/Pagination"; // 自定义表格工具组件 @@ -54,6 +54,8 @@ Vue.prototype.selectDictLabel = selectDictLabel Vue.prototype.selectDictLabels = selectDictLabels Vue.prototype.download = download Vue.prototype.handleTree = handleTree +Vue.prototype.addDateRange2 = addDateRange2 + // 全局组件挂载 Vue.component('DictTag', DictTag) diff --git a/src/utils/DateUtil.js b/src/utils/DateUtil.js new file mode 100644 index 0000000..a4a6c73 --- /dev/null +++ b/src/utils/DateUtil.js @@ -0,0 +1,221 @@ +/** + * Created by lcx47996 on 2017/12/25. + */ + +export function dateFormat(date, format) { + if (!date || date === 0) { + return '' + } + if (!(date instanceof Date)) { + date = new Date(date) + if (date.toString() === 'Invalid Date') { + return '无效时间戳' + } + } + format = format || 'yyyy-MM-dd hh:mm:ss' + const o = { + 'M+': date.getMonth() + 1, // 月份 + 'd+': date.getDate(), // 日 + 'h+': date.getHours(), // 小时 + 'm+': date.getMinutes(), // 分 + 's+': date.getSeconds(), // 秒 + 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度 + 'S': date.getMilliseconds() // 毫秒 + }; + if (/(y+)/.test(format)) { + format = format.replace(RegExp.$1, (date.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; +} + +function getAfterDate(after) { + var dd = new Date(); + dd.setDate(dd.getDate() + after);// 获取AddDayCount天后的日期 + var y = dd.getFullYear(); + var m = dd.getMonth() + 1;// 获取当前月份的日期 + var d = dd.getDate(); + return y + '-' + m + '-' + d; +} + +function getAfterDateReturnDate(datetime, after) { + if (!(datetime.type instanceof Date)) { + return datetime + } + var r = new Date(datetime.getTime()) + r.setDate(datetime.getDate() + after);// 获取AddDayCount天后的日期 + return r; +} + +function getAfterHourReturnDate(datetime, after) { + if (!(datetime.type instanceof Date)) { + return datetime + } + var r = new Date(datetime.getTime() + after * 60 * 60 * 1000) + return r; +} + +function getAfterMounth(date, after) { + var dd = new Date(); + dd.setMonth(dd.getMonth() + after);// 获取AddDayCount天后的日期 + var y = dd.getFullYear(); + var m = dd.getMonth() + 1;// 获取当前月份的日期 + var d = dd.getDate(); + return y + '-' + m + '-' + d; +} + +/** + * 计算2个日期相差的天数,不包含今天,如:2016-12-13到2016-12-15,相差2天 + */ +function dateDiff(startDate, endDate) { + return parseInt((endDate - startDate) / 1000 / 60 / 60 / 24);// 把相差的毫秒数转换为天数 +} + +/** + * 计算2个日期相差的天数,包含今天,如:2016-12-13到2016-12-15,相差3天 + */ +function dateDiffIncludeToday(startDate, endDate) { + return parseInt((endDate - startDate) / 1000 / 60 / 60 / 24) + 1;// 把相差的毫秒数转换为天数 +} + +/** + * 获取凌晨时间 + * @param day + */ +export function getMorningTime(day = 0) { + if (day == null) { + return null; + } + const timeStamp = new Date(new Date().setHours(0, 0, 0, 0)).getTime(); + return new Date(timeStamp + day * 86400000); +} + +export function getStartEnd(length) { + const end = getMorningTime(0); + const start = getMorningTime(0); + start.setTime(start.getTime() - 3600 * 1000 * 24 * length); + return {end, start}; +} + +function getTimeShort() { + const timeShort = [ + { + text: '今天', + onClick(picker) { + const {end, start} = getStartEnd(0); + picker.$emit('pick', [start, end]); + } + }, + { + text: '昨天', + onClick(picker) { + const time = getMorningTime(0); + time.setTime(time.getTime() - 3600 * 1000 * 24); + picker.$emit('pick', [time, time]); + } + }, + { + text: '最近一周', + onClick(picker) { + const {end, start} = getStartEnd(7); + picker.$emit('pick', [start, end]); + } + }, { + text: '最近一个月', + onClick(picker) { + const {end, start} = getStartEnd(30); + picker.$emit('pick', [start, end]); + } + }, { + text: '最近三个月', + onClick(picker) { + const {end, start} = getStartEnd(90); + picker.$emit('pick', [start, end]); + } + } + ]; + return timeShort; +} + +function getTimeShort2(){ + const timeShort = [ + { + text: '今天', + onClick(picker) { + const temp = new Date(); + picker.$emit('pick', [new Date(temp.setHours(0, 0, 0, 0)), new Date(temp.setHours(23, 59, 59, 0))]); + } + }, + { + text: '昨天', + onClick(picker) { + const temp = new Date(); + temp.setTime(temp.getTime() - 3600 * 1000 * 24); + picker.$emit('pick', [new Date(temp.setHours(0, 0, 0, 0)), new Date(temp.setHours(23, 59, 59, 0))]); + } + }, + { + text: '前一周', + onClick(picker) { + const start = new Date(); + const end = new Date(); + start.setTime(end.getTime() - 3600 * 1000 * 24 * 6); + picker.$emit('pick', [new Date(start.setHours(0, 0, 0, 0)), new Date(end.setHours(23, 59, 59, 0))]); + } + }, + { + text: '这个月', + onClick(picker) { + const end = getCurrentMonthLast(); + const start = getCurrentMonthFirst(); + picker.$emit('pick', [new Date(start.setHours(0, 0, 0, 0)), new Date(end.setHours(23, 59, 59, 0))]); + + function getCurrentMonthFirst() { + let date = new Date(); + date.setDate(1); + return date; + } + + // 获取当前月的最后一天 + function getCurrentMonthLast() { + let date = new Date(); + let currentMonth = date.getMonth(); + let nextMonth = ++currentMonth; + let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1); + let oneDay = 1000 * 60 * 60 * 24; + + return new Date(nextMonthFirstDay - oneDay); + } + } + }, + { + text: '前一个月', + onClick(picker) { + const start = new Date(); + const end = new Date(); + start.setTime(end.getTime() - 3600 * 1000 * 24 * 29); + picker.$emit('pick', [new Date(start.setHours(0, 0, 0, 0)), new Date(end.setHours(23, 59, 59, 0))]); + } + } + ] + return timeShort +} + +export default { + 'getAfterDate': getAfterDate, + 'getAfterMounth': getAfterMounth, + 'dateDiff': dateDiff, + 'dateDiffIncludeToday': dateDiffIncludeToday, + 'getAfterDateReturnDate': getAfterDateReturnDate, + 'getAfterHourReturnDate': getAfterHourReturnDate, + getTimeShort, + getTimeShort2, + dateFormat, + getStartEnd, + getMorningTime +} + diff --git a/src/utils/ruoyi.js b/src/utils/ruoyi.js index d2cd2a0..f5fbd75 100644 --- a/src/utils/ruoyi.js +++ b/src/utils/ruoyi.js @@ -1,4 +1,4 @@ - +import moment from "moment/moment"; /** * 通用js方法封装处理 @@ -68,6 +68,19 @@ export function addDateRange(params, dateRange, propName) { return search; } +export function addDateRange2(params, dateRange, addDay=true) { + let search = params; + dateRange = Array.isArray(dateRange) ? dateRange : []; + if (addDay) { + search['beginTime'] = dateRange[0]; + search['endTime'] = moment(dateRange[1]).add(1,"days").format('yyyy-MM-DD'); + } else { + search['beginTime'] = dateRange[0]; + search['endTime'] = dateRange[1]; + } + return search; +} + // 回显数据字典 export function selectDictLabel(datas, value) { if (value === undefined) { diff --git a/src/views/ums/member/index.vue b/src/views/ums/member/index.vue index ce3c106..ac56755 100644 --- a/src/views/ums/member/index.vue +++ b/src/views/ums/member/index.vue @@ -1,208 +1,79 @@