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.
 

151 lines
4.2 KiB

// pages/component/myCalendar/index.js
import util from "../../../utils/util"
let app = getApp()
Component({
/**
* 组件的属性列表
*/
options: {
styleIsolation: 'apply-shared',
addGlobalClass: true
},
properties: {
datelist:{
type:Array,
value:[]
},
activeDay:{
type:String,
value:''
}
},
/**
* 组件的初始数据
*/
data: {
year: 0,
month: 0,
date: ['日', '一', '二', '三', '四', '五', '六'],
dateArr: [],
isToday: 0,
isTodayWeek: false,
todayIndex: 0,
isKj:null,
isGp:null
},
lifetimes: {
attached: function() {
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
this.dateInit();
this.setData({
year: year,
month: month,
isToday: '' + year + month + now.getDate(),
isKj:app.globalData.kjId,
isGp:app.globalData.gp_id || app.globalData.team_id
})
let dates={}
this.properties.datelist.map(item=>{
dates[item.date] = item
})
console.log(dates)
this.setData({
dates:dates
})
}
},
/**
* 组件的方法列表
*/
methods: {
dateInit: function (setYear, setMonth) {
//全部时间的月份都是按0~11基准,显示月份才+1
let dateArr = []; //需要遍历的日历数组数据
let arrLen = 0; //dateArr的数组长度
let now = setYear ? new Date(setYear, setMonth) : new Date();
let year = setYear || now.getFullYear();
let nextYear = 0;
let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期
let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
let obj = {};
let num = 0;
if (month + 1 > 11) {
nextYear = year + 1;
dayNums = new Date(nextYear, nextMonth, 0).getDate();
}
arrLen = startWeek + dayNums;
for (let i = 0; i < arrLen; i++) {
if (i >= startWeek) {
num = i - startWeek + 1;
let date =util.formatDate(new Date(year +'/' + (month + 1) +'/' + num));
obj = {
isToday: '' + year + (month + 1) + num,
dateNum: num,
weight: 5,
date:date
}
} else {
obj = {};
}
dateArr[i] = obj;
}
dateArr.map(monthDate=>{
console.log(monthDate)
})
this.setData({
dateArr: dateArr
})
let nowDate = new Date();
let nowYear = nowDate.getFullYear();
let nowMonth = nowDate.getMonth() + 1;
let nowWeek = nowDate.getDay();
let getYear = setYear || nowYear;
let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
if (nowYear == getYear && nowMonth == getMonth) {
this.setData({
isTodayWeek: true,
todayIndex: nowWeek
})
} else {
this.setData({
isTodayWeek: false,
todayIndex: -1
})
}
},
lastMonth: function () {
//全部时间的月份都是按0~11基准,显示月份才+1
let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
this.setData({
year: year,
month: (month + 1)
})
this.dateInit(year, month);
},
nextMonth: function () {
//全部时间的月份都是按0~11基准,显示月份才+1
let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
let month = this.data.month > 11 ? 0 : this.data.month;
this.setData({
year: year,
month: (month + 1)
})
this.dateInit(year, month);
},
onTapDay:function(e){
let date = e.currentTarget.dataset.date;
console.log(date)
if(!date || date.price===null || date.stock==null || date.stock==0) return;
this.triggerEvent("onTapDay",date)
},
}
})