Files
GX.O3.WMS/APK/APP_O3/components/opsoft-date-picker/opsoft-date-picker.vue
T

310 lines
11 KiB
Vue
Executable File

<template>
<view class="opsoft-date-picker">
<view class="date-mask" @click.stop="hide"></view>
<view class="date-container" @click.stop="handleEvent">
<view class="date-title" v-if='isShowtitleBar.showBar'>
<view class="cancel-btn" v-if='isShowtitleBar.showLeftBtn' @click.stop="cancelClick">
<text>{{titleBarContext.leftText}}</text>
</view>
<view class="title-text" v-if='isShowtitleBar.showText'>
<text>{{titleBarContext.centerText}}</text>
</view>
<view class="confirm-btn" v-if='isShowtitleBar.showRightBtn' @click.stop="confirmClick">
<text>{{titleBarContext.rightText}}</text>
</view>
</view>
<picker-view :indicator-style="indicatorStyle" :value="dateTime" @change="bindChange"
class="picker-view">
<picker-view-column v-if="showDateType.showYears">
<view class="item" v-for="(item,index) in years" :key="index" @click.stop="columnClick(item,index,'year')">{{item}}年</view>
</picker-view-column>
<picker-view-column v-if="showDateType.showMonths">
<view class="item" v-for="(item,index) in months" :key="index" @click.stop="columnClick(item,index,'month')">{{parseInt(item) < 10 ? '0' + item : item}}月</view>
</picker-view-column>
<picker-view-column v-if="showDateType.showDays">
<view class="item" v-for="(item,index) in days" :key="index" @click.stop="columnClick(item,index,'day')">{{parseInt(item) < 10 ? '0' + item : item}}日</view>
</picker-view-column>
<picker-view-column v-if="showDateType.showHours">
<view class="item" v-for="(item,index) in hours" :key="index" @click.stop="columnClick(item,index,'hour')">{{parseInt(item) < 10 ? '0' + item : item}}时</view>
</picker-view-column>
<picker-view-column v-if="showDateType.showMins">
<view class="item" v-for="(item,index) in minutes" :key="index" @click.stop="columnClick(item,index,'min')">{{parseInt(item) < 10 ? '0' + item : item}}分</view>
</picker-view-column>
<picker-view-column v-if="showDateType.showSeconds">
<view class="item" v-for="(item,index) in seconds" :key="index" @click.stop="columnClick(item,index,'sec')">{{parseInt(item) < 10 ? '0' + item : item}}秒</view>
</picker-view-column>
</picker-view>
</view>
</view>
</template>
<script>
import {reactive,ref,computed,onMounted} from "vue";
import {getTimeArrayIndex,getDays,addZero} from './uitls/util.js'
export default {
name:"opsoft-date-picker",
props:{
isShowtitleBar: {
type: Object,
default: {showBar:true,showLeftBtn:true,showText:true,showRightBtn:true}
},
titleBarContext: {
type: Object,
default: {
leftText: '取消',centerText: '请选择',rightText: '确认'
}
},
columType: {//date-all:年月日时分秒;Y-M:年月;Y-M-D:年月日;Y-M-D-H-M:年月日时分;H-M:时分
type:String,
default:'date-all'
},
dateInfo: {
type:Object,
default:{
startYear:2000,
endYear:2030
}
}
},
data(){
return {
indicatorStyle:'',
dateTime: [],
showDateType: {
showYears:true,showMonths:true,showDays:true,
showHours:true,showMins:true,showSeconds:true
},
days: [],
};
},
computed: {
years() {
return this.getPickerViewColumn(this.dateInfo.endYear, this.dateInfo.startYear);
},
months() {
return this.getPickerViewColumn(12, 1);
},
hours() {
return this.getPickerViewColumn(23, 0);
},
minutes() {
return this.getPickerViewColumn(59, 0);
},
seconds() {
return this.getPickerViewColumn(59, 0);
}
},
mounted() {
this.init();
},
methods: {
init(){
let date = new Date();
this.dateTime = getTimeArrayIndex(date,this.dateInfo.startYear);
this.dateTime.forEach(e=>{parseInt(e)});
let daysLength = getDays(this.years[ this.dateTime[0] ],this.months[ this.dateTime[1]]);
this.days = this.getPickerViewColumn(parseInt(daysLength),1);
// if(this.columType == 'date-all'){
// this.showDateType.showYears = true;
// this.showDateType.showMonths = true;
// this.showDateType.showDays = true;
// this.showDateType.showHours = true;
// this.showDateType.showMins = true;
// this.showDateType.showSeconds = true;
// }
// if(this.columType == 'Y-M'){
// this.showDateType.showYears = true;
// this.showDateType.showMonths = true;
// this.showDateType.showDays = false;
// this.showDateType.showHours = false;
// this.showDateType.showMins = false;
// this.showDateType.showSeconds = false;
// }
// if(this.columType == 'Y-M-D'){
// this.showDateType.showYears = true;
// this.showDateType.showMonths = true;
// this.showDateType.showDays = true;
// this.showDateType.showHours = false;
// this.showDateType.showMins = false;
// this.showDateType.showSeconds = false;
// }
// if(this.columType == 'Y-M-D-H-M'){
// this.showDateType.showYears = true;
// this.showDateType.showMonths = true;
// this.showDateType.showDays = true;
// this.showDateType.showHours = true;
// this.showDateType.showMins = true;
// this.showDateType.showSeconds = false;
// }
// if(this.columType == 'H-M'){
// this.showDateType.showYears = false;
// this.showDateType.showMonths = false;
// this.showDateType.showDays = false;
// this.showDateType.showHours = true;
// this.showDateType.showMins = true;
// this.showDateType.showSeconds = false;
// }
},
getPickerViewColumn(end, start) {
let columArray = [];
while (start <= end) {
columArray.push(start);
start++;
}
return columArray;
},
bindChange(e){
this.dateTime = e.target.value;
this.dateTime.forEach(e=>{parseInt(e)});
let daysLength = getDays(this.years[ this.dateTime[0] ],this.months[ this.dateTime[1]]);
this.days = this.getPickerViewColumn(parseInt(daysLength),1);
},
hide(){
let visiable = false;
this.$emit('hide',visiable)
},
cancelClick(){
let visiable = false;
this.$emit('cancelClick',visiable)
},
confirmClick(){
let visiable = false;
let currDate = '';
let years = this.years;
let months = this.months;
let days = this.days;
let hours = this.hours;
let mins = this.minutes;
let seconds = this.seconds;
let fullDate = years[this.dateTime[0]] + '-' + addZero(months[this.dateTime[1]]) + '-' + addZero(days[this.dateTime[2]]) + ' ' +
addZero(hours[this.dateTime[3]]) + ':' + addZero(mins[this.dateTime[4]]) + ':' + addZero(seconds[this.dateTime[5]]);
if(this.columType=='date-all'){
currDate = years[this.dateTime[0]] + '-' + addZero(months[this.dateTime[1]]) + '-' + addZero(days[this.dateTime[2]]) + ' ' +
addZero(hours[this.dateTime[3]]) + ':' + addZero(mins[this.dateTime[4]]) + ':' + addZero(seconds[this.dateTime[5]])
}
if(this.columType=='Y-M-D-H-M'){
currDate = years[this.dateTime[0]] + '-' + addZero(months[this.dateTime[1]]) + '-' + addZero(days[this.dateTime[2]]) + ' ' +
addZero(hours[this.dateTime[3]]) + ':' + addZero(mins[this.dateTime[4]])
}
if(this.columType=='Y-M-D'){
currDate = years[this.dateTime[0]] + '-' + addZero(months[this.dateTime[1]]) + '-' + addZero(days[this.dateTime[2]])
}
if(this.columType=='Y-M'){
currDate = years[this.dateTime[0]] + '-' + addZero(months[this.dateTime[1]])
}
if(this.columType=='H-M'){
currDate = addZero(hours[this.dateTime[3]]) + ':' + addZero(mins[this.dateTime[4]])
}
console.log('confirmClickCurrDate'+fullDate)
this.$emit('confirmClick',visiable,currDate,fullDate)
},
columnClick(item,index,type){
let visiable = false;
let years = this.years;
let months = this.months;
let days = this.days;
let hours = this.hours;
let mins = this.minutes;
let seconds = this.seconds;
let curYear = years[this.dateTime[0]]
let curMonth = months[this.dateTime[1]]
let curDay = days[this.dateTime[2]]
let curHour = hours[this.dateTime[3]]
let curMin = mins[this.dateTime[4]]
let curSec = seconds[this.dateTime[5]]
let currDate = ''
if(type=='year'){
curYear = item
}
if(type=='month'){
curMonth = item
}
if(type=='day'){
curDay = item
}
if(type=='hour'){
curHour = item
}
if(type=='min'){
curMin = item
}
if(type=='sec'){
curSec = item
}
let fullDate = curYear+'-'+addZero(curMonth)+'-'+addZero(curDay)+' '+addZero(curHour)+':'+addZero(curMin)+':'+addZero(curSec)
if(this.columType=='date-all'){
currDate = curYear+'-'+addZero(curMonth)+'-'+addZero(curDay)+' '+addZero(curHour)+':'+addZero(curMin)+':'+addZero(curSec)
}
if(this.columType=='Y-M-D-H-M'){
currDate = curYear+'-'+addZero(curMonth)+'-'+addZero(curDay)+' '+addZero(curHour)+':'+addZero(curMin)
}
if(this.columType=='Y-M-D'){
currDate = curYear+'-'+addZero(curMonth)+'-'+addZero(curDay)
}
if(this.columType=='Y-M'){
currDate = curYear+'-'+addZero(curMonth)
}
if(this.columType=='H-M'){
currDate = addZero(curHour)+':'+addZero(curMin)
}
console.log('columnClickCurrDate'+fullDate)
this.$emit('columnClick',visiable,currDate,fullDate)
}
}
}
</script>
<style lang='scss' scoped>
.opsoft-date-picker{
.date-mask{
position: fixed;top: 0;bottom: 0;left: 0;right: 0;
background-color: rgba($color: #000000, $alpha: .5);z-index: 998;
}
.date-container{
position: fixed;bottom: 0;right: 0;left: 0;height: 45%;
display: flex;flex-direction: column;
background: #f6f6f6;z-index: 1000;
border-top-left-radius: 20rpx;
border-top-right-radius: 20rpx;
.date-title{
display: flex;align-items: center;justify-content: space-between;
padding: 0 12px;height: 90rpx;background-color: #FFFFFF;
border-bottom: solid 1px rgba(239, 239, 239, 1);
border-top-left-radius: 20rpx;
border-top-right-radius: 20rpx;
.cancel-btn {
padding: 5px 10px;box-sizing: border-box;
color: rgba(153, 153, 153, 1);
}
.title-text {
max-width: 100%;overflow: hidden;flex-grow: 1;
white-space: nowrap;text-overflow: ellipsis;
font-weight: bolder;font-size: 18px;text-align: center;
color: rgba(51, 51, 51, 1);
}
.confirm-btn {
padding: 5px 10px;box-sizing: border-box;
color: rgba(35, 132, 235, 1);
border-radius: 5px;
}
}
.picker-view{
width: 100%;flex: 1;
.item {
display: flex;align-items: center;justify-content: center;
font-size: 30rpx;color: rgba(51, 51, 51, 1);
}
}
}
}
picker-view {
width: 100%;
height: 460rpx;
}
</style>