123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- <template>
- <view>
- <view class="uni-calendar__box">
- <view class="uni-calendar__wrapper">
- <view class="uni-calenda__content">
- <view class="uni-calendar__panel">
- <view class="uni-calendar__date-befor" @tap="dataBefor('0', 'month')">
- <text class="iconfont icon-jiantou" />
- </view>
- <view class="uni-calendar__panel-box">
- <view>{{ canlender.year }}年</view>
- <view>{{ canlender.month }}月</view>
- </view>
- <view class="uni-calendar__date-after uni-calendar__rollback" @tap="dataBefor('1', 'month')">
- <text class="iconfont icon-jiantou " />
- </view>
- <view class="uni-calendar__backtoday" @tap="backtoday">回到今天</view>
- </view>
- <view v-if="lunar" class="uni-calendar__day-detail">
- <view>
- {{
- canlender.year +
- '年' +
- canlender.month +
- '月' +
- canlender.date +
- '日 (' +
- canlender.lunar.astro +
- ')'
- }}
- </view>
- <view>
- {{
- canlender.lunar.gzYear +
- '年' +
- canlender.lunar.gzMonth +
- '月' +
- canlender.lunar.gzDay +
- '日 (' +
- canlender.lunar.Animal +
- '年)'
- }}
- {{ canlender.lunar.IMonthCn + canlender.lunar.IDayCn }}
- {{ canlender.lunar.isTerm ? canlender.lunar.Term : '' }}
- </view>
- </view>
- <view class="uni-calendar__header">
- <view class="uni-calendar__week">日</view>
- <view class="uni-calendar__week">一</view>
- <view class="uni-calendar__week">二</view>
- <view class="uni-calendar__week">三</view>
- <view class="uni-calendar__week">四</view>
- <view class="uni-calendar__week">五</view>
- <view class="uni-calendar__week">六</view>
- </view>
- <uni-calendar-item v-if="slide === 'none'" :canlender="canlender" :lunar="lunar" @selectDays="selectDays" />
- <swiper v-else :style="{ height: domHeihgt + 'px' }" :current="currentIndex" :vertical="slide === 'vertical' ? true : false" :duration="duration" class="uni-calendar__body" circular skip-hidden-item-layout @animationfinish="animationfinish" @change="change">
- <swiper-item v-for="(item, itemindx) in swiperData" :key="itemindx">
- <view :class="elClass">
- <uni-calendar-item :canlender="item" :lunar="lunar" @selectDays="selectDays" />
- </view>
- </swiper-item>
- </swiper>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import CALENDAR from './calendar.js'
- import uniCalendarItem from './uni-calendar-item'
- export default {
- name: 'UniCalendar',
- components: {
- uniCalendarItem
- },
- props: {
-
- date: {
- type: String,
- default: ''
- },
-
- selected: {
- type: Array,
- default () {
- return []
- }
- },
-
- lunar: {
- type: Boolean,
- default: false
- },
-
- disableBefore: {
- type: Boolean,
- default: false
- },
-
- startDate: {
- type: String,
- default: ''
- },
-
- endDate: {
- type: String,
- default: ''
- },
-
- slide: {
- type: String,
- default: 'horizontal'
- },
- fixedHeihgt: {
- type: Boolean,
- default: true
- }
- },
- data() {
-
- const elClass = this.__call_hook ? 'uni_canlender' : `Uni_${Math.ceil(Math.random() * 10e5).toString(36)}`
- return {
- dateShow: false,
- selectDay: '',
- canlender: {
- weeks: []
- },
- domHeihgt: 254,
- swiperData: [],
- currentIndex: 0,
- currentSelDate: '',
- duration: 200,
- hold: false,
- isSilde: false,
- isClick: false,
- elClass: elClass
- }
- },
- watch: {
- selected(newVal) {
- let canlender = this.canlender
-
- this.isSilde = true
- this.getMonthAll(
- this.currentIndex,
- canlender.year + '-' + canlender.month + '-' + canlender.date
- )
- }
- },
- created() {
-
- this.isSilde = true
- if (this.slide === 'none') {
- this.getMonthAll(0, this.date, true)
- } else {
- this.getMonthAll(1, this.date, true)
- this.getQueryDom(1)
- }
- },
- methods: {
-
- animationfinish(e) {
- let index = e.detail.current
- let date =
- this.swiperData[index].year +
- '-' +
- this.swiperData[index].month +
- '-' +
- this.swiperData[index].date
- this.currentIndex = index
- this.getMonthAll(index, date)
- clearTimeout(this.timer)
- this.duration = 200
- },
- change(e) {
- let index = e.detail.current
-
- this.isSilde = true
- this.getQueryDom(index)
- },
-
- getMonthAll(index, date, first) {
- if (date === '') {
- date = new Date()
- }
- let canlender = this.getWeek(date)
- this.currentSelDate = canlender.date
- let tempyear = canlender.year + '-' + canlender.month + '-1'
- let tempbefore = this.getWeek(this.getDate(tempyear, -1, 'month'))
- let tempafter = this.getWeek(this.getDate(tempyear, +1, 'month'))
- let beforeyear =
- canlender.year +
- '-' +
- canlender.month +
- '-' +
- (canlender.date > tempbefore.lastDate ? tempbefore.lastDate : canlender.date)
- let afteryear =
- canlender.year +
- '-' +
- canlender.month +
- '-' +
- (canlender.date > tempafter.lastDate ? tempafter.lastDate : canlender.date)
- let before = this.getWeek(this.getDate(beforeyear, -1, 'month'))
- let after = this.getWeek(this.getDate(afteryear, +1, 'month'))
- this.selectDay = canlender.month + '月' + canlender.date + '日'
- this.canlender = canlender
- if (this.slide === 'none') {
-
- this.duration = 0
- this.currentIndex = 0
- if (first && index === 0) {
-
- this.canlender = canlender
- } else {
- if (index === 0) {
- this.canlender = canlender
- }
- if (index === -1) {
- this.canlender = before
- }
- if (index === 1) {
- this.canlender = after
- }
- }
- this.selectDay = canlender.month + '月' + canlender.date + '日'
- this.hold = false
-
- this.setEmit(this.canlender)
- return
- }
- this.setEmit(canlender)
- this.currentIndex = index
- if (first && index === 1) {
- this.swiperData.push(before)
- this.swiperData.push(canlender)
- this.swiperData.push(after)
- return
- }
- if (index === 0) {
- this.swiperData[0] = canlender
- this.swiperData[1] = after
- this.swiperData[2] = before
- return
- }
- if (index === 1) {
- this.swiperData[0] = before
- this.swiperData[1] = canlender
- this.swiperData[2] = after
- return
- }
- if (index === 2) {
- this.swiperData[0] = after
- this.swiperData[1] = before
- this.swiperData[2] = canlender
- }
- },
- setEmit(canlender) {
- if (this.isSilde) {
- let isClick = ''
- if (this.isClick) {
- isClick = 'to-click'
- this.isClick = false
- } else {
- isClick = 'change'
- }
- this.$emit(isClick, {
- year: canlender.year,
- month: canlender.month,
- date: canlender.date,
- lunar: canlender.lunar,
- clockinfo: canlender.clockinfo || {},
- fulldate: canlender.year + '-' + canlender.month + '-' + canlender.date
- })
- this.isSilde = false
- }
- },
-
- getlunar(year, month, date) {
- return CALENDAR.solar2lunar(year, month, date).IDayCn
- },
-
- isDisableBefore(year, month, date) {
- let nowDate = this.date ? this.date : new Date()
- let time = year + '-' + month + '-' + date
- let startDate = false
- let endDate = false
- if (this.startDate) {
- startDate = this.dateCompare(this.startDate, time)
- }
- if (this.endDate) {
- endDate = this.dateCompare(this.getDate(this.endDate, 1), time)
- }
- if (this.disableBefore) {
- if (!this.startDate) {
- if (!this.endDate) {
- return !this.dateCompare(this.getDate(nowDate, 0), time)
- } else {
- return !this.dateCompare(this.getDate(nowDate, 0), time) || endDate
- }
- } else {
- return (
- !this.dateCompare(this.getDate(nowDate, 0), time) || !startDate || endDate
- )
- }
- } else {
- if (!this.startDate) {
- if (!this.endDate) {
- return false
- } else {
- return endDate
- }
- } else {
- return !startDate || endDate
- }
-
- }
- },
-
- backtoday() {
- if (this.hold) {
- return
- }
- this.hold = true
- this.duration = 0
- clearTimeout(this.indexTimer)
- this.indexTimer = setTimeout(() => {
-
- this.isSilde = true
- if (this.slide === 'none') {
- this.getMonthAll(0, this.date)
- } else {
- this.getMonthAll(1, this.date)
- }
- this.hold = false
- }, 200)
- },
-
- dataBefor(id, types) {
-
- if (this.hold) {
- return
- }
- this.hold = true
-
- this.isSilde = true
- if (this.slide === 'none') {
- let num = 0
- if (id === '0') {
- num = -1
- } else {
- num = 1
- }
- let year =
- this.canlender.year + '-' + this.canlender.month + '-' + this.canlender.date
- this.getMonthAll(num, year)
- return
- }
- let index = this.currentIndex
- this.duration = 0
- if (id === '0') {
- index--
- if (index < 0) index = 2
- } else {
- index++
- if (index > 2) index = 0
- }
- clearTimeout(this.indexTimer)
- this.indexTimer = setTimeout(() => {
- this.currentIndex = index
- this.hold = false
- }, 200)
- },
-
- selectDays(params) {
- let {
- week,
- index,
- ischeck,
- isDay
- } = params
- let canlender = null
- if (this.slide === 'none') {
- canlender = this.canlender
- } else {
- canlender = this.swiperData[this.currentIndex]
- }
- if (!ischeck) return false
- if (isDay) return false
-
- let month =
- canlender.weeks[week][index].month < 10 ? '0' + canlender.weeks[week][index].month : canlender.weeks[week][index].month
- let date = canlender.weeks[week][index].date < 10 ? '0' +
- canlender.weeks[week][index].date :
- canlender.weeks[week][index].date
-
- let indexNum = 0
- if (this.slide !== 'none') {
- indexNum = this.currentIndex
- }
-
- this.isSilde = true
- this.isClick = true
- this.getMonthAll(indexNum, canlender.year + '-' + month + '-' + date)
- },
-
- getWeek(dateData) {
- let selected = this.selected
- let nowDate = this.getDate(this.date)
-
- if (typeof dateData !== 'object') {
- dateData = dateData.replace(/-/g, '/')
- }
- let _date = new Date(dateData)
- let year = _date.getFullYear()
- let month = _date.getMonth() + 1
- let date = _date.getDate()
- let day = _date.getDay()
- let canlender = []
-
- let dates = {
- firstDay: new Date(year, month - 1, 1).getDay(),
- lastMonthDays: [],
- currentMonthDys: [],
- nextMonthDays: [],
- endDay: new Date(year, month, 0).getDay(),
- weeks: []
- }
-
- for (let i = dates.firstDay; i > 0; i--) {
- let beforeDate = new Date(year, month - 1, -i + 1).getDate() + ''
- dates.lastMonthDays.push({
- date: beforeDate,
- month: month - 1,
- disable: this.isDisableBefore(year, month - 1, beforeDate),
- lunar: this.getlunar(year, month - 1, beforeDate),
- isDay: false
- })
- }
- let clockinfo = {
- have: false
- }
-
- for (let i = 1; i <= new Date(year, month, 0).getDate(); i++) {
- let have = false
- let clockinfoTemp = {}
-
- for (let j = 0; j < selected.length; j++) {
- let selDate = selected[j].date.split('-')
- if (
- Number(year) === Number(selDate[0]) &&
- Number(month) === Number(selDate[1]) &&
- Number(i) === Number(selDate[2])
- ) {
- have = true
- clockinfoTemp.have = true
- clockinfoTemp.date = selected[j].date
- if (selected[j].info) {
- clockinfoTemp.info = selected[j].info
- }
- if (
- JSON.stringify(selected[j].data) === '{}' ||
- selected[j].data !== undefined
- ) {
- clockinfoTemp.data = selected[j].data
- }
- if (
- Number(year) === Number(selDate[0]) &&
- Number(month) === Number(selDate[1]) &&
- Number(date) === Number(selDate[2])
- ) {
- clockinfo = clockinfoTemp
- }
- }
- }
- dates.currentMonthDys.push({
- date: i + '',
- month: month,
- have,
- clockinfo: clockinfoTemp,
- disable: this.isDisableBefore(year, month, i + ''),
- lunar: this.getlunar(year, month, i + ''),
- isDay: nowDate ===
- year +
- '-' +
- (month < 10 ? '0' + month : month) +
- '-' +
- (i < 10 ? '0' + i : i)
- })
- }
- let surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)
- if (!this.fixedHeihgt) {
- surplus = 6 - dates.endDay
- }
-
- for (let i = 1; i < surplus + 1; i++) {
- dates.nextMonthDays.push({
- date: i + '',
- month: month + 1,
- disable: this.isDisableBefore(year, month + 1, i + ''),
- lunar: this.getlunar(year, month + 1, i + ''),
- isDay: false
- })
- }
- canlender = canlender.concat(
- dates.lastMonthDays,
- dates.currentMonthDys,
- dates.nextMonthDays
- )
-
- for (let i = 0; i < canlender.length; i++) {
- if (i % 7 === 0) {
- dates.weeks[parseInt(i / 7)] = new Array(7)
- }
- dates.weeks[parseInt(i / 7)][i % 7] = canlender[i]
- }
- return {
- weeks: dates.weeks,
- month: month,
- date: date,
- day: day,
- year: year,
- clockinfo,
- lunar: CALENDAR.solar2lunar(year, month, date),
- lastDate: dates.currentMonthDys[dates.currentMonthDys.length - 1].date
- }
- },
-
- getDate(date, AddDayCount = 0, str = 'day') {
- if (typeof date !== 'object') {
- date = date.replace(/-/g, '/')
- }
- let dd = new Date(date)
- switch (str) {
- case 'day':
- dd.setDate(dd.getDate() + AddDayCount)
- break
- case 'month':
- dd.setMonth(dd.getMonth() + AddDayCount)
- break
- case 'year':
- dd.setFullYear(dd.getFullYear() + AddDayCount)
- break
- }
- let y = dd.getFullYear()
- let m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1
- let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()
- return y + '-' + m + '-' + d
- },
-
- dateCompare(startDate, endDate) {
-
- startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
-
- endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
- if (startDate <= endDate) {
- return true
- } else {
- return false
- }
- },
- getQueryDom(index) {
- let dom = uni.createSelectorQuery().in(this).selectAll(`.${this.elClass}`)
- dom.boundingClientRect(rect => {}).exec(e => {
- if (!e[0][index]) {
- setTimeout(() => this.getQueryDom(1), 50)
- return
- }
-
- if (e[0][index]) {
- this.domHeihgt = e[0][index].height
- }
- })
- }
- }
- }
- </script>
- <style>
- @charset "UTF-8";
- @font-face {
- font-family: iconfont;
- src: url(//at.alicdn.com/t/font_989023_qdgy7euvg4.eot?t=1545961121132);
- src: url(//at.alicdn.com/t/font_989023_qdgy7euvg4.eot?t=1545961121132#iefix) format("embedded-opentype"), url("data:application/x-font-woff;charset=utf-8;base64,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Ps10PnIhVZk6oJqzpJjMqt2erQBRvn8lGvF4kehCblWGP+tsYCjnEFhSUOjDFCGGSIyujoO1Vm9K+xQ8Jee1Y9zed0WxTU/3OFAQL0z1xTurLSeTpPgT1fG1J1dCtuy56UNJFezUkSskJe1rZUQuoBNmVXjhF6XNGJPyhnSP8ACVpuyAAAAHicY2BigAAuBuyAiZGJkZmBIyszMa8kv9SEgQEAGD0DTAAAAA==") format("woff"), url(//at.alicdn.com/t/font_989023_qdgy7euvg4.ttf?t=1545961121132) format("truetype"), url(//at.alicdn.com/t/font_989023_qdgy7euvg4.svg?t=1545961121132#iconfont) format("svg")
- }
- .iconfont {
- font-family: iconfont !important;
- font-size: 32upx;
- font-style: normal;
- -webkit-font-smoothing: antialiased;
- -moz-osx-font-smoothing: grayscale
- }
- .icon-jiantou:before {
- content: '\e606'
- }
- .header {
- display: flex;
- justify-content: center;
- align-items: center;
- position: relative;
- height: 100upx;
- background: #fff;
- z-index: 10000;
- font-size: 32upx
- }
- .uni-calendar__box {
- width: 100%;
- box-sizing: border-box;
- transition: all .3s
- }
- .uni-calendar__wrapper {
- width: 100%;
- box-sizing: border-box;
- font-size: 26rpx;
- background: #fff;
- transition: all .3s
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel {
- position: relative;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 28rpx;
- height: 80rpx
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel .uni-calendar__date-after,
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel .uni-calendar__date-befor {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 80rpx;
- width: 80rpx;
- text-align: center;
- line-height: 80rpx
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel .uni-calendar__date-after.uni-calendar__rollback,
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel .uni-calendar__date-befor.uni-calendar__rollback {
- transform: rotate(180deg)
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel .uni-calendar__panel-box {
- display: flex;
- justify-content: center;
- align-items: center;
- width: 200upx
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__panel .uni-calendar__backtoday {
- position: absolute;
- right: 0;
- top: 15rpx;
- padding: 0 10rpx;
- padding-left: 20rpx;
- height: 50rpx;
- line-height: 50rpx;
- border: 1px rgba(253, 46, 50, .5) solid;
- border-right: none;
- font-size: 24rpx;
- border-top-left-radius: 50rpx;
- border-bottom-left-radius: 50rpx;
- color: rgba(253, 46, 50, .7);
- background: rgba(241, 233, 233, .4)
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__day-detail {
- padding: 20upx;
- padding-left: 30upx;
- border-top: 1px #f5f5f5 solid
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__header {
- display: flex;
- font-size: 28upx;
- border-top: 1px #f5f5f5 solid
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__header .uni-calendar__week {
- width: 100%;
- text-align: center;
- line-height: 80rpx;
- color: #333;
- font-weight: 700
- }
- .uni-calendar__wrapper .uni-calenda__content .uni-calendar__body {
- display: flex;
- flex-wrap: wrap;
- font-size: 28upx
- }
- </style>
|