date-picker.js 10.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
import api from '@/api/api'
import {
	httpGet,
	httpPost
} from '@/api/fetch-api.js'

import Date from '@/utils/date.js';

export default {
	inheritAttrs: false,
	props: {
		// 周末是否可选
		value: {
			type: String,
			default: ""
		},
		// 周末是否可选
		weekend: {
			type: Boolean,
			default: false
		},
		// 过滤类型,含这些模型
		// history : 历史不可选择
		// future : 未来不可选择
		// weekend : 周末不可选择
		// today : 当日不可选择
		filtModel: {
			type: Array,
			default () {
				return ["history", "weekend", "today"]
			}
		},
		// 需要过滤的日期列表 格式 ["yyyy-MM-dd","yyyy-MM-dd"]
		filterDates: {
			type: Array,
			default () {
				return [] // ["2019-11-26", "2019-11-28"]
			}
		},
		/**
		 * 占位符
		 * 1.默认空格字符串 " " 即视觉上无内容
		 * 2.空字符串 ""  使用默认国际化
		 * 3.自定义传值
		 */
		placeholder: {
			type: String,
			default: " "
		},
		/**
		 * input框 是否只读
		 * ture为输入框不可编辑
		 */
		readonly: {
			type: Boolean,
			default: false
		},
		// 校验日期是否合法
		check: {
			type: Function,
			default: null
		},
	},
	data() {
		return {
			dateValue: "", // 日期value yyyy-MM-dd
			dateType: 1, // 选择显示类型 1.日 2.月 3.年
			year: 1970,
			month: 1, // (1~12)
			date: 1, // (1~31)
			day: 0, // (0~6)
			yearPage: 1,
			yearList: [],
			yearRange: "", // 年份范围
			// 用户渲染的数据
			fortmatMonthData: [],
			visible: false,
		}
	},
	components: {},
	computed: {
		locale() {
			return this.$i18n.locale || 'tc';
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		},
		getDayList() {
			if (this.locale == 'en') {
				return ["Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat"]
			} else {
				return ["日", "一", "二", "三", "四", "五", "六"]
			}
		},
		getMonthList() {
			if (this.locale == 'en') {
				return ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
			} else {
				return ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
			}
		},
		// 年月
		getDateType1() {
			if (this.locale == 'en') {
				let date = Date.parse(`${this.month}/${this.date}/${this.year}`);
				return date.toString('MMM , yyyy');
			} else {
				return `${this.year}${this.month}月`
			}
		},
		// 年
		getDateType2() {
			if (this.locale == 'en') {
				return `${this.year}`
			} else {
				return `${this.year}年`
			}
		}
	},
	methods: {
		activity(item) {
			let ymd = item.year + "-" + (item.month > 9 ? item.month : "0" + item.month) + "-" + (item.date > 9 ? item.date : "0" + item.date);
			return ymd == this.dateValue;
		},
		/**
		 * 画月历图
		 * 所需数据
		 * 		year  	年
		 * 		month 	月 (1~12)
		 * 		date		日  (1~31)
		 * 		day		星期几  (0-6)
		 *
		 * 输出数据
		 * 		fortmatMonthData 用于渲染日历的数据
		 */
		formatDate() {
			let result = [];
			let year = this.year;
			let month = this.month;

			// 获取当月天数
			let dayNum = Date.getDaysInMonth(year, month - 1);

			/**
			 * 排布出当月号码
			 * 组装数据
			 */
			for (let index = 0; index < dayNum; index++) {
				let curData = {}
				curData.standards = 0;
				let date = index + 1;
				let buildDate = Date.parse(`${year}.${month}.${date}`);
				let nowDate = new Date();
				let day = buildDate.getDay();
				let isWeekend = day == 0 || day == 6;
				let disable = false; // 判断是否不可选
				if (this.filtModel.indexOf("weekend") >= 0) {
					disable = isWeekend;
				}
				if (!disable && this.filtModel.indexOf("future") >= 0) {
					buildDate.setHours(23, 59, 59, 999);
					disable = buildDate.getTime() > nowDate.getTime()
				}
				if (!disable && this.filtModel.indexOf("history") >= 0) {
					buildDate.setHours(0, 0, 0, 0);
					disable = buildDate.getTime() < nowDate.getTime()
				}
				if (!disable && this.filtModel.indexOf("today") >= 0) {
					buildDate.setHours(0, 0, 0, 0);
					nowDate.setHours(0, 0, 0, 0);
					disable = buildDate.getTime() == nowDate.getTime();
				}
				// disable = isWeekend && !this.weekend; // 判断周末不可选
				let filterDates = this.filterDates;
				filterDates.forEach(element => {
					let curDate = `${year}-${month}-${date}`;
					if (element == curDate) {
						disable = true;
					}
				});
				curData = Object.assign({
					year: year,
					month: month,
					date: date, // (1~31)
					day: day,
					isWeekend: isWeekend,
					disable: disable,
				}, curData);
				result.push(curData);
			}

			// 当月1号星期几 0-6 往前塞值
			let dateStr = month + '.' + '1' + '.' + year;
			let firstDay = Date.parse(dateStr).getDay();

			// 根据星期几在前面补空格 星期日0格子,星期一1格子,星期六6格子
			for (let index = 0; index < firstDay; index++) {
				result.unshift(null);
			}
			this.fortmatMonthData = result;
		},
		// 加/减 月份
		addMonths(value) {
			let dateStr = this.month + '.' + this.date + '.' + this.year;
			let targetDate = Date.parse(dateStr).addMonths(value);
			this.year = targetDate.getFullYear();
			this.month = targetDate.getMonth() + 1;
			this.date = targetDate.getDate();
			this.formatDate();
		},
		// 上一个月
		prevMonth() {
			this.addMonths(-1);
		},
		// 下一个月
		nextMonth() {
			this.addMonths(1);
		},
		// 加/减 年份
		addYear(value) {
			let dateStr = this.month + '.' + this.date + '.' + this.year;
			let targetDate = Date.parse(dateStr).addYears(value);
			this.year = targetDate.getFullYear();
			this.month = targetDate.getMonth() + 1;
			this.formatDate();
		},
		// 上一年
		prevYear() {
			this.addYear(-1);
		},
		// 下一年
		nextYear() {
			this.addYear(1);
		},
		// 选择日期
		selectDay(item) {
			if (!item) return;
			let curData = item;
			let curDate = curData.date;
			this.date = curDate;
			let {
				year,
				month,
				date
			} = this;
			// this.dateValue = `${year}-${month}-${date}`;
			this.formatDateValue(year, month, date);
			this.showCalendar();
		},
		// 选择月份
		selectMonth(item) {
			if (!item) return;
			this.dateType = 1;
			this.month = item;
			this.formatDate();
			let {
				year,
				month,
				date
			} = this;
			// this.dateValue = `${year}-${month}-${date}`;
			this.formatDateValue(year, month, date);
		},
		// 选择年份
		selectYear(item) {
			if (!item) return;
			this.dateType = 2;
			let curData = item;
			let curYear = curData.year;
			this.year = curYear;
			let {
				year,
				month,
				date,
			} = this;
			// this.dateValue = `${year}-${month}-${date}`;
			this.formatDateValue(year, month, date);
		},

		// 计算year渲染列表
		refreshYearList() {
			let yearPage = this.yearPage;
			if (yearPage <= 0) return;
			let yearList = [];
			for (let index = 0; index < 12; index++) {
				yearList.push({
					year: yearPage * 10 + index - 1,
					disable: index == 0 || index == 11
				});
			}
			this.yearRange = `${yearPage * 10 + 0}-${yearPage * 10 + 9}`
			this.yearList = yearList;
		},
		// 显示日历
		showCalendar(boo) {
			this.visible = boo;
			this.dateType = 1;
			let year = this.year;
			let yearPage = Math.floor(year / 10);
			this.yearPage = yearPage;
			this.refreshYearList();
		},
		getMonthByIndex(index) {
			let monthList = this.getMonthList;
			return monthList[index - 1];
		},
		// 选择类型 年/月/日
		onTypeHandler() {
			let dateType = this.dateType;
			if (dateType == 1) {
				this.dateType = 2;
				return;
			}
			if (dateType == 2) {
				this.dateType = 3;
				this.refreshYearList();
				return;
			}
		},
		// 上下按钮
		onPrevHandler() {
			if (this.dateType == 1) {
				this.prevMonth();
				return;
			}
			if (this.dateType == 2) {
				this.prevYear();
				return;
			}
			if (this.dateType == 3) {
				this.yearPage--;
				this.refreshYearList();
				return;
			}
		},
		onNextHandler() {
			if (this.dateType == 1) {
				this.nextMonth();
				return;
			}
			if (this.dateType == 2) {
				this.nextYear();
				return;
			}
			if (this.dateType == 3) {
				this.yearPage++;
				this.refreshYearList();
				return;
			}
		},
		formatDateValue(year, month, date) {
			if (month < 10) {
				month = "0" + month
			}
			if (date < 10) {
				date = "0" + date
			}
			this.dateValue = `${year}-${month}-${date}`;
		},
		/**
		 * 校验并返回日期
		 * {
		 *	dateValue:yyyy-MM-dd,
		 *  disable:boolean  true:不可用 , false,当前日期可用
		 * }
		 */
		checkDateValue() {
			if (this.check) {
				let disable = false;
				let dateValue = this.dateValue;
				let fortmatMonthData = this.fortmatMonthData;
				fortmatMonthData.forEach((element, idx) => {
					let curDate = `${element.year}-${element.month}-${element.date}`;
					if (curDate == dateValue && element.disable == true) {
						disable = true;
					}
				});
				let result = {
					dateValue: this.dateValue,
					disable: disable
				}
				this.check(result);
			}
		},
		initData() {
			// 设置今天日期
			let isInit = false;
			if (this.value) {
				let ymd = this.value.split("-");
				if (ymd.length == 3) {
					try {
						this.year = Number(ymd[0]);
						this.month = Number(ymd[1]);
						this.date = Number(ymd[2]);
						isInit = true;
					} catch (e) {}
				}
			}
			if (!isInit) {
				let today = Date.today();
				try {
					if (this.filtModel.indexOf("today") >= 0) {
						if (this.filtModel.indexOf("future") >= 0) {
							today.setDate(today.getDate() - 1);
							if (this.filtModel.indexOf("weekend") >= 0) {
								let day = today.getDay();
								let m = day == 0 ? -2 : day == 6 ? -1 : 0;
								today.setDate(today.getDate() + m);
							}
						} else if (this.filtModel.indexOf("history") >= 0) {
							today.setDate(today.getDate() + 1);
							if (this.filtModel.indexOf("weekend") >= 0) {
								let day = today.getDay();
								let m = day == 0 ? 1 : day == 6 ? 2 : 0;
								today.setDate(today.getDate() + m);
							}
						}
					}
				} catch (e) {
					console.error(e);
				}
				this.year = today.getFullYear();
				this.month = today.getMonth() + 1;
				this.date = today.getDate();
			}
			this.formatDate();
		},
		judgeLastDateValidate(val) {

		}
	},
	mounted() {},
	created() {
		this.initData();
	},
	watch: {
		value(val) {
			this.dateValue = val;
			this.initData();
		},
		dateValue(val, oldVal) {
			this.checkDateValue();
			this.$emit('input', val);
		}
	}
}