维基小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

124 lines
3.1 KiB

<template>
<view class="card radius shadow-warp bg-white">
<view @tap="$refs.calendar.open()" class="card-head solid-bottom" hover-class="cc-active">
<view class="card-head-avatar bg-orange">
<view class="cuIcon-calendar"></view>
</view>
<view class="card-head-title">选择时间</view>
<view class="card-head-action" style="letter-spacing: 0.6">{{ date }}</view>
</view>
<view class="menus-wrap">
<button
:class="[ menu === item ? 'bg-purple': 'line-gray' ]"
:key="index"
@tap="handleClickMenu(item)"
class="cu-btn round"
v-for="(item, index) in menus"
>{{ item }}</button>
</view>
<uni-calendar
:insert="false"
:range="true"
:show-month="true"
@confirm="handleChange"
ref="calendar"
/>
</view>
</template>
<script>
const menus = ['昨天', '今天', '近7天', '近30天'];
export default {
name: 'DateSelector',
data() {
const start = this.$moment().format('YYYY-MM-DD');
const end = this.$moment().format('YYYY-MM-DD');
return {
start,
end,
menu: '今天',
menus,
};
},
computed: {
date() {
const { start, end } = this;
return start === end ? start : `${start} - ${end}`;
},
},
methods: {
/**
* 日历确认选择了时间段
* @param {object} value 日历返回对象
*/
handleChange(value) {
const { before, after } = value.range;
// 开始时间或者结束时间只有一个的时候
// 就当选择了某一天
if (before && !after) {
this.start = before;
this.end = before;
} else if (!before && after) {
this.end = after;
this.start = after;
} else if (before && after) {
this.start = before;
this.end = after;
}
this.menu = '';
this.$emit('change', this.start, this.end);
},
/**
* 点选了快捷菜单
* @param {string} menu 快捷时间菜单字符串
*/
handleClickMenu(menu) {
this.menu = menu;
let start = this.$moment().format('YYYY-MM-DD');
let end = this.$moment().format('YYYY-MM-DD');
switch (menu) {
case '昨天':
start = this.$moment()
.subtract(1, 'days')
.format('YYYY-MM-DD');
end = this.$moment()
.subtract(1, 'days')
.format('YYYY-MM-DD');
break;
case '近7天':
start = this.$moment()
.subtract(6, 'days')
.format('YYYY-MM-DD');
end = this.$moment().format('YYYY-MM-DD');
break;
case '近30天':
start = this.$moment()
.subtract(29, 'days')
.format('YYYY-MM-DD');
end = this.$moment().format('YYYY-MM-DD');
break;
default:
break;
}
this.start = start;
this.end = end;
this.$emit('change', start, end);
},
},
};
</script>
<style lang="scss" scoped>
.menus-wrap {
display: flex;
align-items: center;
justify-content: space-around;
padding: 40rpx;
}
</style>