This commit is contained in:
Pang 2021-06-16 10:20:20 +08:00
parent 080e805cfb
commit face5a943d
2 changed files with 227 additions and 40 deletions

View File

@ -4,23 +4,33 @@
<div class="calendar-head">
<div class="calendar-titbox">
<div class="calendar-title">
<h1>{{$L('日历')}}</h1>
<h1>{{viewDay}}</h1>
</div>
<ButtonGroup class="calendar-arrow" size="small">
<Button @click="preMonth"><Icon type="ios-arrow-back"></Icon></Button>
<Button @click="curMonth">{{$L('今天')}}</Button>
<Button @click="afterMonth"><Icon type="ios-arrow-forward"></Icon></Button>
</ButtonGroup>
</div>
</div>
<div class="calendar-box">
<ul>
<ul class="head">
<li>SUN</li>
<li>MON</li>
<li>TUE</li>
<li>WED</li>
<li>THU</li>
<li>FRI</li>
<li>SAT</li>
<li>SUN</li>
</ul>
{{days}}
<ul>
<li v-for="n in 5"></li>
<ul class="days">
<li v-for="row in days">
<ul>
<li v-for="(item, key) in row" :key="key" :class="item.place">
<div class="time"><em :class="{'cur-day': item.ymd == curDay}">{{item.day}}</em></div>
</li>
</ul>
</li>
</ul>
</div>
</div>
@ -32,53 +42,128 @@ import {mapState} from "vuex";
export default {
data() {
return {
days: []
viewDay: $A.formatDate("Y-m"),
curDay: $A.formatDate("Y-m-d"),
curInterval: null,
}
},
mounted() {
this.days = this.generateMonthCalendar('2020-09-19');
this.curInterval = setInterval(() => {
this.curDay = $A.formatDate("Y-m-d");
}, 60000)
},
destroyed() {
clearInterval(this.curInterval)
},
computed: {
...mapState(['userInfo']),
days() {
const days = this.getDateJson(new Date(this.viewDay));
let row = days[days.length - 1].day >= 7 ? 5 : 6
let array = [], tmp = [];
for (let i = 0; i < days.length; i++) {
let obj = days[i];
tmp.push(obj);
if ((i + 1) % 7 == 0) {
array.push(tmp);
tmp = [];
}
if (array.length >= row) {
break;
}
}
return array;
}
},
watch: {
},
methods: {
generateMonthCalendar(dates, line = 6) {
var date = new Date(dates); //
var y = date.getFullYear();
var m = date.getMonth();
var days = new Date(y, m + 1, 0).getDate(); //
var firstDayWeek = new Date(y, m, 1).getDay(); //
var arr = []; //
var n = []; //
var d = 1; //
//
// ''
for(let i = 0; i < firstDayWeek; i++) {
// new Date(2020, 8, 0) --> 90 === 83127 n.unshift(getDate(y, m, 0 - i).getDate());
preMonth() {
const date = new Date(this.viewDay);
date.setMonth(date.getMonth() - 1);
this.viewDay = $A.formatDate("Y-m", date)
},
curMonth() {
this.viewDay = $A.formatDate("Y-m");
},
afterMonth() {
const date = new Date(this.viewDay);
date.setMonth(date.getMonth() + 1);
this.viewDay = $A.formatDate("Y-m", date)
},
getDateJson(date){
const getMonths = (yy) => {
let months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let months2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let mrun = yy % 100 == 0 && yy % 400 == 0 ? true : (yy % 4 == 0);
return mrun ? months2 : months;
}
//
//
// 6
for (let j = 0; j < line; j++) {
// 7
// 7
for (let i = 0; i < 7; i++) {
if(d > days) {
//
// new Date(2020, 8, 31) --> 931 === 101       n.push(new Date(y, m, d++).getDate());
} else {
//        n.push(d++);
}51
if (n.length == 7) break; // 7
}
arr.push(n);
n = []; // ba
const zeroFill = (num) => {
if (num < 9) return '0' + num;
return '' + num;
}
return arr;
//
let yy = date.getFullYear();
let mm = date.getMonth();
//使
let month = getMonths(yy);
//1
let begin_date = new Date(yy, mm, 1);
//
let pre_num = begin_date.getDay();
//
let const_num = 7 * 6;
//
let cur_num = month[mm];
//
let after_num = const_num - cur_num - pre_num;
//
let preyy = yy;
let premm = mm;
//-10
if (premm == 0) {
preyy -= 1;
}
//
premm = premm - 1 < 0 ? 11 : (premm - 1);
let pre_max = month[premm];
//
let afteryy = yy;
let aftermm = mm;
if (aftermm == 11) {
afteryy += 1;
}
aftermm = aftermm + 1 > 11 ? 0 : (aftermm + 1);
//
let dateJson = [];
//
for (let i = pre_num; i > 0; i--) {
let obj = {year: preyy, month: premm + 1, day: (pre_max - i + 1), place: 'pre'};
obj.ymd = obj.year + '-' + zeroFill(obj.month) + '-' + zeroFill(obj.day)
dateJson.push(obj);
}
//
for (let i = 1; i <= cur_num; i++) {
let obj = {year: yy, month: mm + 1, day: i, place: 'cur'};
obj.ymd = obj.year + '-' + zeroFill(obj.month) + '-' + zeroFill(obj.day)
dateJson.push(obj);
}
//
for (let i = 1; i <= after_num; i++) {
let obj = {year: afteryy, month: aftermm + 1, day: i, place: 'after'};
obj.ymd = obj.year + '-' + zeroFill(obj.month) + '-' + zeroFill(obj.day)
dateJson.push(obj);
}
return dateJson;
}
}
}

View File

@ -8,6 +8,8 @@
border-bottom: 1px solid #F4F4F5;
.calendar-titbox {
flex: 1;
display: flex;
align-items: center;
margin-bottom: 16px;
.calendar-title {
display: flex;
@ -18,12 +20,112 @@
font-weight: 600;
}
}
.calendar-arrow {
margin-left: 36px;
> button {
color: #888888;
font-size: 12px;
&:focus {
box-shadow: none;
}
}
}
}
}
.calendar-box {
flex: 1;
height: 0;
display: flex;
margin-bottom: 16px;
flex-direction: column;
padding: 0 48px;
.head {
display: flex;
align-items: center;
border-bottom: 1px solid #f4f5f5;
> li {
flex: 1;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
padding: 12px;
position: relative;
&:after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 1px;
height: 100%;
background-color: #f4f5f5;
}
&:last-child {
&:after {
display: none;
}
}
}
}
.days {
flex: 1;
flex-shrink: 0;
display: flex;
flex-direction: column;
> li {
flex: 1;
flex-shrink: 0;
list-style: none;
display: flex;
flex-direction: column;
border-bottom: 1px solid #f4f5f5;
> ul {
flex: 1;
flex-shrink: 0;
display: flex;
> li {
flex: 1;
flex-shrink: 0;
list-style: none;
position: relative;
display: flex;
flex-direction: column;
.time {
padding: 10px 10px 0;
> em {
font-style: normal;
display: inline-block;
border-radius: 50%;
min-height: 24px;
min-width: 24px;
line-height: 24px;
text-align: center;
&.cur-day {
background-color: #2d8cf0;
color: #ffffff;
}
}
}
&:after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 1px;
height: 100%;
background-color: #f4f5f5;
}
&:last-child {
&:after {
display: none;
}
}
&.pre,
&.after {
color: #aaaaaa;
}
}
}
}
}
}
}