<template>
<u-popup
:maskCloseAble="maskCloseAble"
mode="bottom"
:popup="false"
v-model="value"
length="auto"
:safeAreaInsetBottom="safeAreaInsetBottom"
@close="close"
:z-index="uZIndex"
class="u-popup"
>
<!-- 多加一个if判断,避免微信小程序第二次打开后,视图没有重新渲染,而导致数据混乱 -->
<view class="u-datetime-picker" @tap.stop v-if="value">
<view class="u-picker-header u-navbar-inner" @touchmove.stop.prevent>
<view
class="u-btn-picker u-btn-picker--tips btn-gray100"
:style="{ color: cancelColor }"
@tap="getResult('cancel')"
>{{$i18nMsg.base.cancel}}</view>
<view class="u-picker__title">{{ title }}</view>
<view
class="u-btn-picker u-btn-picker--primary btn-blue100"
:style="{ color: moving ? cancelColor : confirmColor }"
@touchmove.stop
@tap.stop="getResult('confirm')"
>{{$i18nMsg.base.ensure}}</view>
</view>
<view class="u-picker-body">
<picker-view
v-if="mode == 'region'"
:value="valueArr"
@change="change"
class="u-picker-view"
@pickstart="pickstart"
@pickend="pickend"
>
<picker-view-column v-if="params.province">
<view class="u-column-item" v-for="(item, index) in provinces" :key="index">
<view class="u-line-1">{{ item.label }}</view>
</view>
</picker-view-column>
<picker-view-column v-if="params.city">
<view class="u-column-item" v-for="(item, index) in citys" :key="index">
<view class="u-line-1">{{ item.label }}</view>
</view>
</picker-view-column>
<picker-view-column v-if="params.area">
<view class="u-column-item" v-for="(item, index) in areas" :key="index">
<view class="u-line-1">{{ item.label }}</view>
</view>
</picker-view-column>
</picker-view>
<picker-view
v-else-if="mode == 'time'"
:value="valueArr"
@change="change"
class="u-picker-view"
@pickstart="pickstart"
@pickend="pickend"
>
<picker-view-column v-if="!reset && params.year">
<view class="u-column-item" v-for="(item, index) in years" :key="index">
{{ item }}
<text class="u-text" v-if="showTimeTag">年</text>
</view>
</picker-view-column>
<picker-view-column v-if="!reset && params.month">
<view class="u-column-item" v-for="(item, index) in months" :key="index">
{{ formatNumber(item) }}
<text class="u-text" v-if="showTimeTag">月</text>
</view>
</picker-view-column>
<picker-view-column v-if="!reset && params.day">
<view class="u-column-item" v-for="(item, index) in days" :key="index">
{{ formatNumber(item) }}
<text class="u-text" v-if="showTimeTag">日</text>
</view>
</picker-view-column>
<picker-view-column v-if="!reset && params.hour">
<view class="u-column-item" v-for="(item, index) in hours" :key="index">
{{ formatNumber(item) }}
<text class="u-text" v-if="showTimeTag">时</text>
</view>
</picker-view-column>
<picker-view-column v-if="!reset && params.minute">
<view class="u-column-item" v-for="(item, index) in minutes" :key="index">
{{ formatNumber(item) }}
<text class="u-text" v-if="showTimeTag">分</text>
</view>
</picker-view-column>
<picker-view-column v-if="!reset && params.second">
<view class="u-column-item" v-for="(item, index) in seconds" :key="index">
{{ formatNumber(item) }}
<text class="u-text" v-if="showTimeTag">秒</text>
</view>
</picker-view-column>
</picker-view>
<picker-view
v-else-if="mode == 'selector'"
:value="defaultSelector"
@change="change"
class="u-picker-view"
@pickstart="pickstart"
@pickend="pickend"
>
<picker-view-column>
<view class="u-column-item" v-for="(item, index) in range" :key="index" :class="{
'u-column-item-active':valueArr[0]==index,
'u-column-item-pre-1':valueArr[0]==index+1,
'u-column-item-pre-2':valueArr[0]==index+2,
'u-column-item-pre-3':valueArr[0]==index+3,
'u-column-item-pre-4':valueArr[0]==index+4,
'u-column-item-pre-5':valueArr[0]==index+5,
'u-column-item-next-1':valueArr[0]==index-1,
'u-column-item-next-2':valueArr[0]==index-2,
'u-column-item-next-3':valueArr[0]==index-3,
'u-column-item-next-4':valueArr[0]==index-4,
'u-column-item-next-5':valueArr[0]==index-5
}">
<view class="u-line-1"
:class="{
'active':valueArr[0]==index,
}"
>{{ getItemValue(item, 'selector') }}</view>
</view>
</picker-view-column>
</picker-view>
<picker-view
v-else-if="mode == 'multiSelector'"
:value="defaultSelector"
@change="change"
class="u-picker-view"
@pickstart="pickstart"
@pickend="pickend"
>
<picker-view-column v-for="(item, index) in range" :key="index">
<view class="u-column-item" v-for="(item1, index1) in item" :key="index1" :class="{
'u-column-item-active':valueArr[index]==index1,
'u-column-item-pre-1':valueArr[index]==index1+1,
'u-column-item-pre-2':valueArr[index]==index1+2,
'u-column-item-pre-3':valueArr[index]==index1+3,
'u-column-item-pre-4':valueArr[index]==index1+4,
'u-column-item-pre-5':valueArr[index]==index1+5,
'u-column-item-next-1':valueArr[index]==index1-1,
'u-column-item-next-2':valueArr[index]==index1-2,
'u-column-item-next-3':valueArr[index]==index1-3,
'u-column-item-next-4':valueArr[index]==index1-4,
'u-column-item-next-5':valueArr[index]==index1-5
}">
<view class="u-line-1"
:class="{'active':valueArr[index]==index1}"
>{{ getItemValue(item1, 'multiSelector') }}</view>
</view>
</picker-view-column>
</picker-view>
<view class="mask-top">
</view>
<view class="mask-bottom">
</view>
</view>
</view>
</u-popup>
</template>
<script>
import provinces from '../../libs/util/province.js'
import citys from '../../libs/util/city.js'
import areas from '../../libs/util/area.js'
export default {
name: 'u-picker',
props: {
params: {
type: Object,
default() {
return {
year: true,
month: true,
day: true,
hour: false,
minute: false,
second: false,
province: true,
city: true,
area: true,
timestamp: true
}
}
},
range: {
type: Array,
default() {
return []
}
},
defaultSelector: {
type: Array,
default() {
return [0]
}
},
rangeKey: {
type: String,
default: ''
},
mode: {
type: String,
default: 'time'
},
startYear: {
type: [String, Number],
default: 1950
},
endYear: {
type: [String, Number],
default: 2050
},
cancelColor: {
type: String,
default: '#606266'
},
confirmColor: {
type: String,
default: '#2979ff'
},
defaultTime: {
type: String,
default: ''
},
defaultRegion: {
type: Array,
default() {
return []
}
},
showTimeTag: {
type: Boolean,
default: true
},
areaCode: {
type: Array,
default() {
return []
}
},
safeAreaInsetBottom: {
type: Boolean,
default: false
},
maskCloseAble: {
type: Boolean,
default: true
},
value: {
type: Boolean,
default: false
},
zIndex: {
type: [String, Number],
default: 0
},
title: {
type: String,
default: ''
}
},
data() {
return {
years: [],
months: [],
days: [],
hours: [],
minutes: [],
seconds: [],
year: 0,
month: 0,
day: 0,
hour: 0,
minute: 0,
second: 0,
startDate: '',
endDate: '',
valueArr: [],
reset: false,
provinces: provinces,
citys: citys[0],
areas: areas[0][0],
province: 0,
city: 0,
area: 0,
moving: false
}
},
mounted() {
this.init()
},
computed: {
propsChange() {
return `${this.mode}-${this.defaultTime}-${this.startYear}-${this.endYear}-${this.defaultRegion}-${this.areaCode}`
},
regionChange() {
return `${this.province}-${this.city}`
},
yearAndMonth() {
return `${this.year}-${this.month}`
},
uZIndex() {
return this.zIndex ? this.zIndex : this.$u.zIndex.popup
}
},
watch: {
propsChange() {
this.reset = true
setTimeout(() => this.init(), 10)
},
regionChange(val) {
this.citys = citys[this.province]
this.areas = areas[this.province][this.city]
},
yearAndMonth(val) {
if (this.params.year) this.setDays()
},
value(n) {
if (n) {
this.reset = true
setTimeout(() => this.init(), 10)
}
}
},
methods: {
pickstart() {
this.moving = true
},
pickend() {
this.moving = false
},
getItemValue(item, mode) {
if (this.mode == mode) {
return typeof item == 'object' ? item[this.rangeKey] : item
}
},
formatNumber(num) {
return +num < 10 ? '0' + num : String(num)
},
generateArray: function(start, end) {
end = end > start ? end : start
return [...Array(end + 1).keys()].slice(start)
},
getIndex: function(arr, val) {
let index = arr.indexOf(val)
return ~index ? index : 0
},
initTimeValue() {
let fdate = this.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()
this.year = time.getFullYear()
this.month = Number(time.getMonth()) + 1
this.day = time.getDate()
this.hour = time.getHours()
this.minute = time.getMinutes()
this.second = time.getSeconds()
},
init() {
this.valueArr = []
this.reset = false
if (this.mode == 'time') {
this.initTimeValue()
if (this.params.year) {
this.valueArr.push(0)
this.setYears()
}
if (this.params.month) {
this.valueArr.push(0)
this.setMonths()
}
if (this.params.day) {
this.valueArr.push(0)
this.setDays()
}
if (this.params.hour) {
this.valueArr.push(0)
this.setHours()
}
if (this.params.minute) {
this.valueArr.push(0)
this.setMinutes()
}
if (this.params.second) {
this.valueArr.push(0)
this.setSeconds()
}
} else if (this.mode == 'region') {
if (this.params.province) {
this.valueArr.push(0)
this.setProvinces()
}
if (this.params.city) {
this.valueArr.push(0)
this.setCitys()
}
if (this.params.area) {
this.valueArr.push(0)
this.setAreas()
}
} else if (this.mode == 'selector') {
this.valueArr = this.defaultSelector
} else if (this.mode == 'multiSelector') {
this.valueArr = this.defaultSelector
this.multiSelectorValue = this.defaultSelector
}
this.$forceUpdate()
},
setYears() {
this.years = this.generateArray(this.startYear, this.endYear)
this.valueArr.splice(
this.valueArr.length - 1,
1,
this.getIndex(this.years, this.year)
)
},
setMonths() {
this.months = this.generateArray(1, 12)
this.valueArr.splice(
this.valueArr.length - 1,
1,
this.getIndex(this.months, this.month)
)
},
setDays() {
let totalDays = new Date(this.year, this.month, 0).getDate()
this.days = this.generateArray(1, totalDays)
let index = 0
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
this.valueArr.splice(index, 1, this.getIndex(this.days, this.day))
},
setHours() {
this.hours = this.generateArray(0, 23)
this.valueArr.splice(
this.valueArr.length - 1,
1,
this.getIndex(this.hours, this.hour)
)
},
setMinutes() {
this.minutes = this.generateArray(0, 59)
this.valueArr.splice(
this.valueArr.length - 1,
1,
this.getIndex(this.minutes, this.minute)
)
},
setSeconds() {
this.seconds = this.generateArray(0, 59)
this.valueArr.splice(
this.valueArr.length - 1,
1,
this.getIndex(this.seconds, this.second)
)
},
setProvinces() {
if (!this.params.province) return
let tmp = ''
let useCode = false
if (this.areaCode.length) {
tmp = this.areaCode[0]
useCode = true
} else if (this.defaultRegion.length) tmp = this.defaultRegion[0]
else tmp = 0
provinces.map((v, k) => {
if (useCode ? v.value == tmp : v.label == tmp) {
tmp = k
}
})
this.province = tmp
this.provinces = provinces
this.valueArr.splice(0, 1, this.province)
},
setCitys() {
if (!this.params.city) return
let tmp = ''
let useCode = false
if (this.areaCode.length) {
tmp = this.areaCode[1]
useCode = true
} else if (this.defaultRegion.length) tmp = this.defaultRegion[1]
else tmp = 0
citys[this.province].map((v, k) => {
if (useCode ? v.value == tmp : v.label == tmp) {
tmp = k
}
})
this.city = tmp
this.citys = citys[this.province]
this.valueArr.splice(1, 1, this.city)
},
setAreas() {
if (!this.params.area) return
let tmp = ''
let useCode = false
if (this.areaCode.length) {
tmp = this.areaCode[2]
useCode = true
} else if (this.defaultRegion.length) tmp = this.defaultRegion[2]
else tmp = 0
areas[this.province][this.city].map((v, k) => {
if (useCode ? v.value == tmp : v.label == tmp) {
tmp = k
}
})
this.area = tmp
this.areas = areas[this.province][this.city]
this.valueArr.splice(2, 1, this.area)
},
close() {
this.$emit('input', false)
},
change(e) {
this.valueArr = e.detail.value
let i = 0
if (this.mode == 'time') {
if (this.params.year) this.year = this.years[this.valueArr[i++]]
if (this.params.month) this.month = this.months[this.valueArr[i++]]
if (this.params.day) this.day = this.days[this.valueArr[i++]]
if (this.params.hour) this.hour = this.hours[this.valueArr[i++]]
if (this.params.minute) this.minute = this.minutes[this.valueArr[i++]]
if (this.params.second) this.second = this.seconds[this.valueArr[i++]]
} else if (this.mode == 'region') {
if (this.params.province) this.province = this.valueArr[i++]
if (this.params.city) this.city = this.valueArr[i++]
if (this.params.area) this.area = this.valueArr[i++]
} else if (this.mode == 'multiSelector') {
let index = null
this.defaultSelector.map((val, idx) => {
if (val != e.detail.value[idx]) index = idx
})
if (index != null) {
this.$emit('columnchange', {
column: index,
index: e.detail.value[index]
})
}
}
},
getResult(event = null) {
if (this.moving) return
let result = {}
if (this.mode == 'time') {
if (this.params.year) result.year = this.formatNumber(this.year || 0)
if (this.params.month) result.month = this.formatNumber(this.month || 0)
if (this.params.day) result.day = this.formatNumber(this.day || 0)
if (this.params.hour) result.hour = this.formatNumber(this.hour || 0)
if (this.params.minute)
result.minute = this.formatNumber(this.minute || 0)
if (this.params.second)
result.second = this.formatNumber(this.second || 0)
if (this.params.timestamp) result.timestamp = this.getTimestamp()
} else if (this.mode == 'region') {
if (this.params.province) result.province = provinces[this.province]
if (this.params.city) result.city = citys[this.province][this.city]
if (this.params.area)
result.area = areas[this.province][this.city][this.area]
} else if (this.mode == 'selector') {
result = this.valueArr
} else if (this.mode == 'multiSelector') {
result = this.valueArr
}
if (event) this.$emit(event, result)
this.close()
},
getTimestamp() {
let time =
this.year +
'-' +
this.month +
'-' +
this.day +
' ' +
this.hour +
':' +
this.minute +
':' +
this.second
return new Date(time).getTime() / 1000
}
}
}
</script>
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
.u-datetime-picker {
position: relative;
z-index: 999;
}
.u-picker-view {
height: 100%;
box-sizing: border-box;
}
.u-picker-header {
width: 100%;
height: rpx(140);
padding: 0 rpx(60);
display: flex;
justify-content: space-between;
align-items: center;
box-sizing: border-box;
font-size:rpx(46);
position: relative;
border-radius:rpx(16) rpx(16) 0px 0px;
}
.u-picker__title {
font-size:rpx(52);
font-family:SourceHanSansCN-Medium,SourceHanSansCN;
font-weight:bold;
color:rgba(0,0,0,1);
}
/deep/ .u-drawer-content{
border-radius:rpx(16) rpx(16) 0px 0px;
}
/deep/ .uni-scroll-view{
border-radius:rpx(16) rpx(16) 0px 0px;
}
/deep/ .uni-picker-view-mask {
background:none;
}
/deep/ .uni-picker-view-content {
z-index: 999;
}
/deep/ .uni-picker-view-indicator {
background: #ffffff;;
box-shadow:0px 0 0px 0px rgba(170,170,170,1),0px rpx(1) 0px 0px rgba(170,170,170,1);
height: rpx(160);
}
/deep/ .uni-picker-view-indicator:before{
border-top: 0;
}
/deep/ .uni-picker-view-indicator:after{
border-bottom: 0;
}
.u-picker-body {
width: 100%;
height: rpx(802);
overflow: hidden;
position: relative;
}
.mask-top{
position: absolute;
left: 0;
top: 0;
right: 0;
height:rpx(322);
background: url("/static/base-module/common/select-top.png") no-repeat center center;
}
.mask-bottom{
position: absolute;
left: 0;
bottom: 0;
right: 0;
height:rpx(318);
background: url("/static/base-module/common/select-bottom.png") no-repeat center center;
}
.u-column-item {
display: flex;
align-items: center;
justify-content: center;
font-size: rpx(46);
color:rgba(0,0,0,1);
padding: 0 4rpx;
.active{
font-weight: bold;
color: #000000;
font-size: rpx(52);
}
}
.u-text {
font-size: 12rpx;
padding-left: 4rpx;
}
.u-btn-picker {
width: rpx(264);
height: rpx(100);
}
.u-opacity {
opacity: 0.5;
}
.u-btn-picker--primary {
color:rgba(0,0,0,1);
font-size:rpx(46);
font-weight:500;
}
.u-btn-picker--tips {
color:rgba(255,255,255,1);
font-size:rpx(46);
font-weight:500;
}
.u-column-item-pre-1,.u-column-item-next-1{
.u-line-1{
color: #444444;
}
opacity: 0.8;
}
.u-column-item-pre-2,.u-column-item-next-2{
opacity: 0.5;
.u-line-1{
color: #444444;
}
}
.u-column-item-pre-3,.u-column-item-next-3{
opacity: 0.4;
.u-line-1{
color: #444444;
}
}
.u-column-item-pre-4,.u-column-item-next-4{
opacity: 0.3;
.u-line-1{
color: #444444;
}
}
</style>