<template>
|
<view>
|
<uni-popup ref="popup">
|
<view class="popup-box">
|
<view class="title">
|
<view class="tabs-box" v-if="mode=='all'">
|
<view v-for="(item,index) in tabs" :key="index" class="tabs-item" @click="tabChange(index)"
|
:class="{'tabs-check':index==tabIndex}">
|
<text class="item-text">{{item}}</text>
|
</view>
|
</view>
|
<view class="title-text" v-else>
|
{{mode=='multiple'?'日期筛选':'月份选择'}}
|
</view>
|
<uni-icons type="closeempty" class="close" size="16" @click="close"></uni-icons>
|
</view>
|
|
|
<view class="options-box" v-if="tabIndex">
|
<view class="flex row-between">
|
<view v-for="(item,index) in options" :key="index" class="option" @click="optionSelect(index)"
|
:class="{'option-check':opIndex==index}">
|
<text class="option-text">{{item}}</text>
|
</view>
|
</view>
|
<view class="flex row-between" style="margin-top: 40rpx;">
|
<view class="option" :class="{'option-check':op1Index==0}"
|
style="width: 320rpx;padding-left: 0;padding-right: 0;" @click="optionChange(0)">
|
<text class="option-text">{{timeFormat(getTimestamp(start_time),fmt)}}</text>
|
</view>
|
<view style="width: 18rpx;height: 2rpx;background: #7B8686;"></view>
|
<view class="option" :class="{'option-check':op1Index==1}"
|
style="width: 320rpx;padding-left: 0;padding-right: 0;" @click="optionChange(1)">
|
<text class="option-text">{{timeFormat(getTimestamp(end_time),fmt)}}</text>
|
</view>
|
</view>
|
</view>
|
<view class="picker-body">
|
<picker-view class="picker-view"
|
indicator-style="height: 76rpx;background: rgba(0,0,0,.03);border:none;" :value="valueArr"
|
@change="change" @touchend="touchend">
|
<picker-view-column v-if="params.year">
|
<view class="column-item" v-for="(item,index) in years" :key="index">
|
<text class="column-item-text">{{item}}年</text>
|
</view>
|
</picker-view-column>
|
<picker-view-column v-if="params.month">
|
<view class="column-item" v-for="(item,index) in months" :key="index">
|
<text class="column-item-text">{{formatNumber(item)}}月</text>
|
</view>
|
</picker-view-column>
|
<picker-view-column v-if="params.day">
|
<view class="column-item" v-for="(item,index) in days" :key="index">
|
<text class="column-item-text">{{formatNumber(item)}}日</text>
|
</view>
|
</picker-view-column>
|
<picker-view-column v-if="params.hour">
|
<view class="column-item" v-for="(item,index) in hours" :key="index">
|
<text class="column-item-text">{{formatNumber(item)}}时</text>
|
</view>
|
</picker-view-column>
|
<picker-view-column v-if="params.minute">
|
<view class="column-item" v-for="(item,index) in minutes" :key="index">
|
<text class="column-item-text">{{formatNumber(item)}}分</text>
|
</view>
|
</picker-view-column>
|
<picker-view-column v-if="params.second">
|
<view class="column-item" v-for="(item,index) in seconds" :key="index">
|
<text class="column-item-text">{{formatNumber(item)}}秒</text>
|
</view>
|
</picker-view-column>
|
</picker-view>
|
</view>
|
<view class="bottom">
|
<view class="h-btn h-btn-line" @click="reset">
|
<text style="color: #333;">重置</text>
|
</view>
|
<view class="h-btn" @click="getResult">
|
<text style="color: #fff;">确定</text>
|
</view>
|
</view>
|
</view>
|
</uni-popup>
|
</view>
|
</template>
|
|
<script>
|
import timeFormat from "../../js/timeFormat.js"
|
export default {
|
props: {
|
// picker中需要显示的参数
|
params: {
|
type: Object,
|
default () {
|
return {
|
year: true,
|
month: true,
|
day: true,
|
hour: true,
|
minute: true,
|
second: false,
|
timestamp: true,
|
};
|
}
|
},
|
// 默认显示的时间,2025-07-02 || 2025-07-02 13:01:00 || 2025/07/02
|
defaultTime: {
|
type: String,
|
default: ''
|
},
|
// 年份开始时间
|
startYear: {
|
type: [String, Number],
|
default: 1950
|
},
|
// 年份结束时间
|
endYear: {
|
type: [String, Number],
|
default: 2050
|
},
|
// 月份结束时间
|
endMonth: {
|
type: [String, Number],
|
default: 12
|
},
|
// 自定义时间选项模式month:月 day:天
|
optionMode: {
|
type: String,
|
default: 'month'
|
},
|
//选择器模式 single:单月 multiple:多月 all:都包括
|
mode: {
|
type: String,
|
default: 'all'
|
},
|
startTime: {
|
type: String,
|
default: ''
|
},
|
endTime: {
|
type: String,
|
default: ''
|
}
|
},
|
data() {
|
return {
|
timeFormat,
|
years: [],
|
months: [],
|
days: [],
|
hours: [],
|
minutes: [],
|
seconds: [],
|
valueArr: [],
|
yearKey: 0,
|
monthKey: 0,
|
dayKey: 0,
|
hourKey: 0,
|
minuteKey: 0,
|
secondy: 0,
|
time: {},
|
start_time: {},
|
end_time: {},
|
tabs: ['月份选择', '自定义时间'],
|
tabIndex: 0,
|
opIndex: -1,
|
op1Index: 0,
|
options: ['近三月', '近半年', '近一年'],
|
itemIndex: 0,
|
fmt: 'yyyy-mm-dd',
|
isClick: false
|
}
|
},
|
mounted() {
|
this.init();
|
},
|
watch: {
|
'time.year'(val) {
|
if(this.params.day){
|
this.setDays(this.time)
|
}
|
},
|
'time.month'(val) {
|
if(this.params.day){
|
this.setDays(this.time)
|
}
|
},
|
'start_time.year'(val) {
|
this.setDays(this.start_time)
|
},
|
'start_time.month'(val) {
|
this.setDays(this.start_time)
|
},
|
'end_time.year'(val) {
|
this.setDays(this.end_time)
|
},
|
'end_time.month'(val) {
|
this.setDays(this.end_time)
|
},
|
},
|
methods: {
|
open() {
|
this.$refs.popup.open('bottom')
|
},
|
close() {
|
this.$refs.popup.close()
|
},
|
//点击重置
|
reset() {
|
this.close();
|
this.$emit('reset');
|
},
|
// 点击确定
|
getResult() {
|
let result = {};
|
if (this.tabIndex) {
|
let start_time = {}
|
let end_time = {}
|
if (this.params.year) {
|
start_time.year = this.formatNumber(this.start_time.year || 0);
|
end_time.year = this.formatNumber(this.end_time.year || 0);
|
}
|
if (this.params.month) {
|
start_time.month = this.formatNumber(this.start_time.month || 0);
|
end_time.month = this.formatNumber(this.end_time.month || 0);
|
}
|
if (this.params.day) {
|
start_time.day = this.formatNumber(this.start_time.day || 0);
|
end_time.day = this.formatNumber(this.end_time.day || 0);
|
}
|
if (this.params.hour) {
|
start_time.hour = this.formatNumber(this.start_time.hour || 0);
|
end_time.hour = this.formatNumber(this.end_time.hour || 0);
|
}
|
if (this.params.minute) {
|
start_time.minute = this.formatNumber(this.start_time.minute || 0);
|
end_time.minute = this.formatNumber(this.end_time.minute || 0);
|
}
|
if (this.params.second) {
|
start_time.second = this.formatNumber(this.start_time.second || 0);
|
end_time.second = this.formatNumber(this.end_time.second || 0);
|
}
|
if (this.params.timestamp) {
|
start_time.timestamp = this.getTimestamp(this.start_time);
|
end_time.timestamp = this.getTimestamp(this.end_time);
|
}
|
start_time.time = this.timeFormat(this.getTimestamp(this.start_time), this.fmt);
|
end_time.time = this.timeFormat(this.getTimestamp(this.end_time), this.fmt);
|
result = {
|
start_time,
|
end_time
|
}
|
} else {
|
if (this.params.year) result.year = this.formatNumber(this.time.year || 0);
|
if (this.params.month) result.month = this.formatNumber(this.time.month || 0);
|
if (this.params.day) result.day = this.formatNumber(this.time.day || 0);
|
if (this.params.hour) result.hour = this.formatNumber(this.time.hour || 0);
|
if (this.params.minute) result.minute = this.formatNumber(this.time.minute || 0);
|
if (this.params.second) result.second = this.formatNumber(this.time.second || 0);
|
if (this.params.timestamp) result.timestamp = this.getTimestamp(this.time);
|
result.time = this.timeFormat(this.getTimestamp(this.time), this.fmt);
|
}
|
this.$emit('confirm', result);
|
this.close();
|
},
|
init() {
|
this.valueArr = [];
|
this.tabIndex = this.mode == 'multiple' ? 1 : 0
|
//初始化时间
|
if (this.startTime) {
|
this.start_time = this.time = this.initTimeValue(this.startTime);
|
} else {
|
this.start_time = this.time = this.initTimeValue(this.defaultTime);
|
}
|
if (this.endTime) {
|
this.end_time = this.initTimeValue(this.endTime);
|
} else {
|
this.end_time = this.initTimeValue(this.defaultTime);
|
}
|
let fmt = ''
|
if (this.optionMode == 'day') {
|
this.options = ['近3天', '近7天', '近15天']
|
}
|
if (this.params.year) {
|
this.valueArr.push(0);
|
this.yearKey = this.valueArr.length - 1
|
this.setYears(this.time);
|
fmt = 'yyyy'
|
}
|
if (this.params.month) {
|
this.valueArr.push(0);
|
this.monthKey = this.valueArr.length - 1
|
this.setMonths(this.time);
|
fmt += '-mm'
|
}
|
if (this.params.day) {
|
this.valueArr.push(0);
|
this.dayKey = this.valueArr.length - 1
|
this.setDays(this.time);
|
fmt += '-dd'
|
}
|
if (this.params.hour) {
|
this.valueArr.push(0);
|
this.hourKey = this.valueArr.length - 1
|
this.setHours(this.time);
|
fmt += ' hh'
|
}
|
if (this.params.minute) {
|
this.valueArr.push(0);
|
this.minuteKey = this.valueArr.length - 1
|
this.setMinutes(this.time);
|
fmt += ':MM'
|
}
|
if (this.params.second) {
|
this.valueArr.push(0);
|
this.secondKey = this.valueArr.length - 1
|
this.setSeconds(this.time);
|
fmt += ':ss'
|
}
|
this.fmt = fmt
|
this.$forceUpdate();
|
},
|
// 设置picker的某一列值
|
setYears(date) {
|
// 获取年份集合
|
this.years = this.generateArray(this.startYear, this.endYear);
|
// 设置this.valueArr某一项的值,是为了让picker预选中某一个值
|
this.valueArr.splice(this.yearKey, 1, this.getIndex(this.years, date.year));
|
},
|
setMonths(date) {
|
if (this.endYear == this.year) {
|
this.months = this.generateArray(1, this.endMonth);
|
} else {
|
this.months = this.generateArray(1, 12);
|
}
|
this.valueArr.splice(this.monthKey, 1, this.getIndex(this.months, date.month));
|
},
|
setDays(date) {
|
let totalDays = new Date(date.year, date.month, 0).getDate();
|
this.days = this.generateArray(1, totalDays);
|
// let index = 0;
|
// 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
|
// 因为this.month和this.year变化时,会触发watch中的this.setDays(),导致this.valueArr.length计算有误
|
// if (this.params.year && this.params.month) index = 2;
|
// else if (this.params.month) index = 1;
|
// else if (this.params.year) index = 1;
|
// else index = 0;
|
// 当月份变化时,会导致日期的天数也会变化,如果原来选的天数大于变化后的天数,则重置为变化后的最大值
|
// 比如原来选中3月31日,调整为2月后,日期变为最大29,这时如果day值继续为31显然不合理,于是将其置为29(picker-column从1开始)
|
if (this.day > this.days.length) this.day = this.days.length;
|
this.valueArr.splice(this.dayKey, 1, this.getIndex(this.days, date.day));
|
},
|
setHours() {
|
this.hours = this.generateArray(0, 23);
|
this.valueArr.splice(this.hourKey, 1, this.getIndex(this.hours, this.time.hour));
|
},
|
setMinutes() {
|
this.minutes = this.generateArray(0, 59);
|
this.valueArr.splice(this.minuteKey, 1, this.getIndex(this.minutes, this.time.minute));
|
},
|
setSeconds() {
|
this.seconds = this.generateArray(0, 59);
|
this.valueArr.splice(this.secondKey, 1, this.getIndex(this.seconds, this.time.second));
|
},
|
// 生成递进的数组
|
generateArray: function(start, end) {
|
// 转为数值格式,否则用户给end-year等传递字符串值时,下面的end+1会导致字符串拼接,而不是相加
|
start = Number(start);
|
end = Number(end);
|
end = end > start ? end : start;
|
// 生成数组,获取其中的索引,并剪出来
|
return [...Array(end + 1).keys()].slice(start);
|
},
|
getIndex: function(arr, val) {
|
let index = arr.indexOf(val);
|
// 如果index为-1(即找不到index值),~(-1)=-(-1)-1=0,导致条件不成立
|
return ~index ? index : 0;
|
},
|
//日期时间处理
|
initTimeValue(defaultTime) {
|
// 格式化时间,在IE浏览器(uni不存在此情况),无法识别日期间的"-"间隔符号
|
let fdate = defaultTime.replace(/\-/g, '/');
|
fdate = fdate && fdate.indexOf('/') == -1 ? `2020/01/01 ${fdate}` : fdate;
|
let time = null;
|
if (fdate) time = new Date(fdate);
|
else time = new Date();
|
// 获取年日月时分秒
|
return {
|
year: time.getFullYear(),
|
month: Number(time.getMonth()) + 1,
|
day: time.getDate(),
|
hour: time.getHours(),
|
minute: time.getMinutes(),
|
second: time.getSeconds(),
|
}
|
},
|
// 小于10前面补0,用于月份,日期,时分秒等
|
formatNumber(num) {
|
return +num < 10 ? '0' + num : String(num);
|
},
|
// 获取时间戳
|
getTimestamp(date) {
|
// yyyy-mm-dd为安卓写法,不支持iOS,需要使用"/"分隔,才能二者兼容
|
let time = date.year + '/' + date.month + '/' + date.day + ' ' + date.hour + ':' + date.minute + ':' + date
|
.second;
|
return new Date(time).getTime();
|
},
|
touchend(e) {
|
this.opIndex = -1
|
},
|
// 用户更改picker的列选项
|
change(e) {
|
this.itemIndex = e.detail.value[0]
|
this.valueArr = e.detail.value;
|
let i = 0;
|
let time = {
|
year: 0,
|
month: 0,
|
day: 0,
|
hour: 0,
|
minute: 0,
|
second: 0,
|
}
|
if (this.params.year) time.year = this.years[this.valueArr[i++]];
|
if (this.params.month) time.month = this.months[this.valueArr[i++]];
|
if (this.params.day) time.day = this.days[this.valueArr[i++]];
|
if (this.params.hour) time.hour = this.hours[this.valueArr[i++]];
|
if (this.params.minute) time.minute = this.minutes[this.valueArr[i++]];
|
if (this.params.second) time.second = this.seconds[this.valueArr[i++]];
|
//自定义时间段
|
if (this.tabIndex) {
|
if (this.op1Index) {
|
this.end_time = time
|
} else {
|
this.start_time = time
|
}
|
} else { //月份选择
|
this.time = time
|
}
|
},
|
tabChange(i) {
|
this.tabIndex = i
|
if (!i) {
|
this.setYears(this.time)
|
this.setMonths(this.time)
|
this.setDays(this.time)
|
} else {
|
this.optionChange(this.op1Index)
|
}
|
},
|
//选择快捷时间段
|
optionSelect(i) {
|
this.opIndex = i;
|
let end_time = new Date()
|
let date;
|
switch (i) {
|
case 0: //近三月,近三天
|
if (this.optionMode == 'day') {
|
date = end_time.setDate(new Date(end_time).getDate() - 3)
|
} else {
|
date = end_time.setMonth(new Date(end_time).getMonth() - 3)
|
}
|
break;
|
case 1: //近半年,近七天
|
if (this.optionMode == 'day') {
|
date = end_time.setDate(new Date(end_time).getDate() - 7)
|
} else {
|
date = end_time.setMonth(new Date(end_time).getMonth() - 6)
|
}
|
break;
|
case 2: //近一年,近十五天
|
if (this.optionMode == 'day') {
|
date = end_time.setDate(new Date(end_time).getDate() - 15)
|
} else {
|
date = end_time.setMonth(new Date(end_time).getMonth() - 12)
|
}
|
break;
|
}
|
this.start_time = this.initTimeValue(this.timeFormat(date, this.fmt))
|
this.end_time = this.initTimeValue(this.timeFormat(new Date(), this.fmt))
|
if (this.op1Index) {
|
this.setYears(this.end_time)
|
this.setMonths(this.end_time)
|
this.setDays(this.end_time)
|
} else {
|
this.setYears(this.start_time)
|
this.setMonths(this.start_time)
|
this.setDays(this.start_time)
|
}
|
|
},
|
//起始时间、截至时间切换
|
optionChange(i) {
|
this.op1Index = i
|
let date = i ? this.end_time : this.start_time
|
this.setYears(date)
|
this.setMonths(date)
|
this.setDays(date)
|
}
|
}
|
}
|
</script>
|
|
<style lang="scss">
|
.flex {
|
display: flex;
|
flex-direction: row;
|
align-items: center;
|
}
|
|
.row-between {
|
justify-content: space-between;
|
}
|
|
.popup-box {
|
background-color: #fff;
|
border-radius: 24rpx 24rpx 0 0;
|
|
}
|
|
.picker-body {
|
overflow: hidden;
|
padding: 32rpx 40rpx;
|
}
|
|
.picker-view {
|
height: 380rpx;
|
width: 670rpx;
|
}
|
|
.column-item {
|
display: flex;
|
align-items: center;
|
justify-content: center;
|
font-size: 32rpx;
|
color: #888;
|
height: 76rpx;
|
|
}
|
|
.column-item-text {
|
// line-height: 100rpx;
|
font-size: 32rpx;
|
}
|
|
.title {
|
display: flex;
|
align-items: center;
|
justify-content: space-between;
|
flex-direction: row;
|
position: relative;
|
|
.close {
|
position: absolute;
|
right: 50rpx;
|
}
|
|
.title-text {
|
width: 100%;
|
text-align: center;
|
padding: 24rpx 0;
|
font-size: 32rpx;
|
font-weight: 600;
|
}
|
}
|
|
.tabs-box {
|
width: 100%;
|
display: flex;
|
flex-direction: row;
|
padding: 0 50rpx 0 32rpx;
|
align-items: center;
|
border-bottom: 2rpx solid rgba(225, 225, 225, 0.5);
|
|
.tabs-item {
|
padding: 24rpx 0 22rpx 0;
|
margin-right: 84rpx;
|
border-bottom: 4rpx solid #fff;
|
}
|
}
|
|
.tabs-check {
|
border-bottom: 4rpx solid #1DAA9F !important;
|
|
.item-text {
|
color: #1DAA9F;
|
}
|
}
|
|
.bottom {
|
padding: 0 32rpx 32rpx;
|
display: flex;
|
align-items: center;
|
flex-direction: row;
|
|
.h-btn {
|
flex: 1;
|
height: 80rpx;
|
background: #0BC7B9;
|
border-radius: 16rpx;
|
display: flex;
|
align-items: center;
|
border: 2rpx solid #0BC7B9;
|
justify-content: center;
|
}
|
|
.h-btn-line {
|
background: #fff;
|
border: 2rpx solid #E1E7EC;
|
margin-right: 42rpx;
|
}
|
}
|
|
.option {
|
padding: 14rpx 58rpx;
|
font-size: 28rpx;
|
border-radius: 16rpx;
|
background: #F4F7F8;
|
border: 3rpx solid #F4F7F8;
|
display: flex;
|
justify-content: center;
|
flex-direction: row;
|
|
.option-text {
|
font-size: 28rpx;
|
font-weight: 500;
|
color: #333333;
|
line-height: 40rpx;
|
|
}
|
}
|
|
.option-check {
|
background: #F5FFFF;
|
border-radius: 16rpx;
|
border: 3rpx solid #1DAA9F;
|
|
.option-text {
|
color: #1DAA9F;
|
}
|
}
|
|
.options-box {
|
padding: 32rpx;
|
transition-property: height;
|
transition-duration: .4s;
|
}
|
</style>
|