format.js 17.5 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
// 获取最近n个月,默认6个月
export function getCurrentSixMonth(n = 5) {
	return [new Date(new Date().setMonth(new Date().getMonth() - n)), new Date()];
}

export function formatThousand(val) {
	if (val === null || val === undefined || val === "") return "-";
	const str = String(val);
	const parts = str.split(".");
	parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	return parts.join(".");
}

// 新值,旧值,整数部分位数,小数部分位数,最小值,最大值,规则
export function limitPrice(
	newValue,
	oldValue,
	intergerDigit,
	pointDigit,
	minValue,
	maxValue,
	regRule
) {
	// console.log(
	//   newValue,
	//   minValue,
	//   newValue &&
	//     ((minValue && newValue < minValue) || (maxValue && newValue > maxValue)),
	//   "limitPrice"
	// )

	let reg = {
		priceReg: /^\d[\d\.]*$/, // eslint-disable-line
		numReg: /^[1-9]\d*$/,
		priceReg_negative: /^-?\d[\d.]*$/, // 允许负数的正则表达式,
		priceReg_negative_input: /^-?(\d+(?:\.\d*)?|\.\d+)?$/, // input时,允许负数的正则表达式
	};
	regRule = regRule || "priceReg";
	if (
		newValue &&
		((minValue && newValue < minValue) || (maxValue && newValue > maxValue))
	) {
		return oldValue || "";
	}
	if (newValue && reg[regRule].test(newValue + "")) {
		if (/\./.test(newValue)) {
			newValue = newValue + "";
			let interger = newValue.split(".")[0];
			let point = newValue.split(".")[1];
			let intergerNum =
				interger.length > intergerDigit ? oldValue.split(".")[0] : interger;
			let pointNum =
				point.length > pointDigit ? point.slice(0, pointDigit) : point;
			newValue = pointDigit
				? intergerNum + "." + pointNum
				: newValue.split(".")[0];
		} else {
			newValue =
				newValue.length > intergerDigit
					? oldValue.slice(0, intergerDigit)
					: newValue;
		}
		return newValue;
	} else {
		return newValue ? oldValue : "";
	}
}

// 允许输入负数的输入框限制
export function limitPriceForNegative(newValue) {
	// 用来检索整数且不超过七位数
	const limitDigit = /\d{1,7}/;
	// 用来检索小数且小数点后不超过两位数
	const limitDigitdot = /\d{1,7}.\d{0,2}/;
	if (newValue) {
		newValue = newValue + "";
		let nn = "";
		// 正负判断
		if (newValue.match("-")) {
			nn = newValue.match("-")[0];
		}
		// 小数判断
		if (newValue.includes(".")) {
			if (newValue.match(limitDigitdot)) {
				nn = nn + newValue.match(limitDigitdot)[0] || "";
			}
		} else {
			if (newValue.match(limitDigit)) {
				nn = nn + newValue.match(limitDigit)[0] || "";
			}
		}
		// 限制数字最大为100
		// if (parseFloat(nn) > 100) nn = 9999999;
		return nn;
	}
}

/**
 * 时间控件-限制只能选当前及之前的时间
 */
export const rangePickerOptions = {
	disabledDate(date) {
		if (date > new Date()) {
			return true;
		}
		return false;
	},
};

/** 时间空间-限制只能选昨天之前的时间 */
export const yesterdayPicker = {
	disabledDate(date) {
		return date.getTime() > Date.now() - 8.64e7;
	},
};
/**
 * 时间控件-限制只能选当前及之后的日期
 */
export const afterPickerOptions = {
	disabledDate(date) {
		return date.getTime() < Date.now() - 8.64e7; // 8.64e7 毫秒数代表一天
	},
};

/**
 * 时间控件-限制只能选当前及之后的日期
 */
export const afterPickerOptions_timeChoose = {
	// 设置禁用过去日期不能选
	disabledDate(date) {
		return date.getTime() < Date.now() - 8.64e7; // 8.64e7 毫秒数代表一天
	},
	// 控制时分秒不可选
	selectableRange: (() => {
		const data = new Date();
		const hour = data.getHours();
		const minute = data.getMinutes();
		// const second = data.getSeconds()
		const second = "00";
		return [`${hour}:${minute}:${second} - 23:59:59`];
	})(),
};

/**
 * 时间控件-(0-30天内的日期)
 */
export const appointDays = 35;
export const afterTodayOptions = {
	disabledDate(date) {
		if (
			date < new Date(new Date().toLocaleDateString()) ||
			date >
				new Date(new Date().setDate(new Date().getDate() + (appointDays - 1)))
		) {
			return true;
		}
		return false;
	},
};

/**
 * 时间控件-限制时间跨度不超过93天,且结束时间不能小于开始时间
 */
export var selectDate1 = "";
export const rangePickertwoMonthOptions = {
	onPick({ maxDate, minDate }) {
		selectDate1 = minDate ? minDate.getTime() : "";
		if (maxDate) {
			selectDate1 = "";
		}
	},
	disabledDate(time) {
		if (selectDate1 !== "") {
			const ninetyThreeDays = 93 * 24 * 3600 * 1000;
			const minTime = selectDate1; // 开始时间即为最小时间
			const maxTime = selectDate1 + ninetyThreeDays; // 开始时间+93天为最大时间
			return time.getTime() < minTime || time.getTime() > maxTime;
		} else {
			return false;
		}
	},
};

/**
 * 时间控件-限制时间跨度不超过93天,且结束时间不能小于开始时间,且结束时间不能大于当前时间
 */
export var selectDate2 = "";
export const rangePickertwoMonthOptionsWithCurrentLimit = {
	onPick({ maxDate, minDate }) {
		selectDate2 = minDate ? minDate.getTime() : "";
		if (maxDate) {
			selectDate2 = "";
		}
	},
	disabledDate(time) {
		const currentTime = Date.now(); // 当前时间

		// 限制所有大于当前时间的日期不可选
		if (time.getTime() > currentTime) {
			return true;
		}

		if (selectDate2 !== "") {
			const ninetyThreeDays = 93 * 24 * 3600 * 1000;
			const minTime = selectDate2; // 开始时间即为最小时间
			const maxTime = Math.min(selectDate2 + ninetyThreeDays, currentTime); // 开始时间+93天与当前时间取较小值作为最大时间
			return time.getTime() < minTime || time.getTime() > maxTime;
		} else {
			return false;
		}
	},
};

/**
 * 时间控件-限制选择30天时间范围,且不大于当前时间
 */
export var pickerMinDate = ""; // 获取开始选择时间
export var pickerMaxDate = ""; // 获取结束选择时间
export const rangePickerOptions_30day = {
	// 时间范围选择控制
	onPick({ maxDate, minDate }) {
		// console.log("minDate", minDate)
		// console.log("maxDate", maxDate)
		if (minDate) {
			pickerMinDate = minDate.getTime();
		}
		if (maxDate) {
			pickerMinDate = "";
			pickerMaxDate = maxDate.getTime();
		}
	},
	disabledDate(time) {
		// console.log(time)

		const day31 = 31 * 24 * 3600 * 1000;
		if (pickerMinDate !== "") {
			let maxTime = pickerMinDate + day31;
			// console.log("maxTime", maxTime)

			if (maxTime > new Date()) {
				maxTime = new Date();
			}
			return time.getTime() > maxTime || time.getTime() > Date.now() - 86400000;
		}
		if (pickerMaxDate !== "" && !pickerMinDate) {
			const minTime = pickerMaxDate - day31;
			return time.getTime() < minTime || time.getTime() > Date.now() - 86400000;
		}
		return time.getTime() > Date.now() - 86400000;
	},
};

/**
 * 时间控件-限制选择30天时间范围,且不大于当前时间
 */
export var selectDate = "";
export const rangePickerOptions_30day2 = {
	// 时间范围选择控制
	onPick({ maxDate, minDate }) {
		selectDate = minDate.getTime();
		if (maxDate) {
			selectDate = "";
		}
	},
	disabledDate(time) {
		if (selectDate !== "") {
			const one = 30 * 24 * 3600 * 1000;
			const minTime = selectDate - one;
			let maxTime = selectDate + one;
			if (maxTime > new Date()) {
				maxTime = new Date();
			}
			return time.getTime() < minTime || time.getTime() > maxTime;
		} else {
			return time.getTime() > new Date();
		}
	},
};

/**
 * 时间控件-单年份选择时限制年份
 */
export const rangePickerOptions_year_2023 = {
	disabledDate(date) {
		// console.log(date)
		// console.log(date.getFullYear())
		// console.log(new Date())

		if (
			date.getFullYear() > new Date().getFullYear() ||
			date.getFullYear() < 2023
		) {
			return true;
		}
		return false;
	},
};

/**
 * 时间控件-单年份选择时限制年份
 */
export const rangePickerOptions_year = {
	disabledDate(date) {
		// console.log(date)
		// console.log(date.getFullYear())
		// console.log(new Date())

		if (date.getFullYear() > new Date().getFullYear()) {
			return true;
		}
		return false;
	},
};
export const todayEndPickerOptions = {
	disabledDate(time) {
		// 获取当天结束时间(23:59:59)
		const today = new Date();
		const todayEnd = new Date(
			today.getFullYear(),
			today.getMonth(),
			today.getDate(),
			23,
			59,
			59
		);

		return time.getTime() > todayEnd.getTime();
	},
};

export const stockQueryDatePickerOptions = {
	disabledDate(time) {
		const minDate = new Date(2026, 1, 1); // 2026年2月1日 (月份从0开始,1代表2月)
		minDate.setHours(0, 0, 0, 0);

		const today = new Date();
		const todayEnd = new Date(
			today.getFullYear(),
			today.getMonth(),
			today.getDate(),
			23,
			59,
			59
		);

		return (
			time.getTime() < minDate.getTime() || time.getTime() > todayEnd.getTime()
		);
	},
};

/**
 * 店面配置-门店产值
 * 限制:‌当前日期在1-10号时‌:允许选择当月和下个月;‌当前日期超过10号时‌:仅允许选择下个月;其他情况‌:禁用所有其他月份‌
 */
export const rangePickerOptions_outputValue = {
	disabledDate: (time) => {
		const current = new Date();
		const currentYear = current.getFullYear();
		const currentMonth = current.getMonth();
		const currentDay = current.getDate();

		// 计算允许的月份范围
		const allowedMonths = [];
		if (currentDay <= 10) {
			// 允许当前月和下个月
			allowedMonths.push({ year: currentYear, month: currentMonth });
			allowedMonths.push({
				year: currentMonth === 11 ? currentYear + 1 : currentYear,
				month: currentMonth === 11 ? 0 : currentMonth + 1,
			});
		} else {
			// 仅允许下个月
			allowedMonths.push({
				year: currentMonth === 11 ? currentYear + 1 : currentYear,
				month: currentMonth === 11 ? 0 : currentMonth + 1,
			});
		}

		// 检查当前时间是否在允许的月份范围内
		const year = time.getFullYear();
		const month = time.getMonth();
		const isAllowed = allowedMonths.some(
			(m) => m.year === year && m.month === month
		);
		return !isAllowed; // 不在允许范围则禁用
	},
};

/**
 * 时间控件-限制只能选当前年份的上一年1月1日至今
 */
export const rangePickerOptions_lastOneYear = {
	disabledDate(date) {
		const now = new Date();
		const lastYearStart = new Date(now.getFullYear() - 1, 0, 1);
		lastYearStart.setHours(0, 0, 0, 0);
		// 禁用:大于今天 OR 小于去年1月1日
		return date > now || date < lastYearStart;
	},
};

/**
 * 时间控件-不可选未来时间,最大筛选范围为一年
 */
export var selectDate_oneYearNoFuture = "";
export const rangePickerOptions_oneYearNoFuture = {
	onPick({ maxDate, minDate }) {
		selectDate_oneYearNoFuture = minDate ? minDate.getTime() : "";
		if (maxDate) {
			selectDate_oneYearNoFuture = "";
		}
	},
	disabledDate(time) {
		const today = new Date();
		const todayStart = new Date(
			today.getFullYear(),
			today.getMonth(),
			today.getDate()
		).getTime();
		// 不可选未来时间
		if (time.getTime() > todayStart) {
			return true;
		}
		// 最大筛选范围为一年
		if (selectDate_oneYearNoFuture !== "") {
			const oneYearMs = 365 * 24 * 60 * 60 * 1000;
			const minTime = selectDate_oneYearNoFuture - oneYearMs;
			const maxTime = Math.min(
				selectDate_oneYearNoFuture + oneYearMs,
				todayStart
			);
			return time.getTime() < minTime || time.getTime() > maxTime;
		}
		return false;
	},
};

/**
 * 店面配置-门店主机厂交付数/个人交付成交率
 * 限制:当前日期>10号‌:仅允许选择当前月份;当前日期≤10号‌:允许选择当前月份和上个月;其他情况‌:禁用所有其他月份‌
 */
export const rangePickerOptions_deliveryQuantity = {
	disabledDate: (time) => {
		const current = new Date();
		const currentYear = current.getFullYear();
		const currentMonth = current.getMonth();
		const currentDay = current.getDate();

		// 计算允许的月份范围
		const allowedMonths = [];
		if (currentDay > 10) {
			// 仅允许当前月
			allowedMonths.push({ year: currentYear, month: currentMonth });
		} else {
			// 允许当前月和上个月
			allowedMonths.push(
				{ year: currentYear, month: currentMonth },
				// 处理跨年情况(如当前为1月时,上个月是去年12月)
				currentMonth === 0
					? { year: currentYear - 1, month: 11 }
					: { year: currentYear, month: currentMonth - 1 }
			);
		}

		// 检查当前时间是否在允许的月份范围内
		const year = time.getFullYear();
		const month = time.getMonth();
		const isAllowed = allowedMonths.some(
			(m) => m.year === year && m.month === month
		);
		return !isAllowed; // 不在允许范围则禁用
	},
};

/**
 * 根据 yyyy-MM 获取当月首尾日期
 * @param {string} targetMonth
 * @returns {{ startDate: string, endDate: string } | null}
 */
export function getMonthDateRange(targetMonth) {
	if (!targetMonth) return null;
	const [yearStr, monthStr] = targetMonth.split("-");
	const year = parseInt(yearStr, 10);
	const month = parseInt(monthStr, 10);
	if (!year || !month) return null;
	const startDate = `${yearStr}-${monthStr.padStart(2, "0")}-01`;
	const lastDay = new Date(year, month, 0).getDate();
	const endDate = `${yearStr}-${monthStr.padStart(2, "0")}-${String(lastDay).padStart(2, "0")}`;
	return { startDate, endDate };
}

export function getFallbackSettlementOptions() {
	const options = [{ label: "每月最后1天", value: 0 }];
	for (let day = 1; day <= 27; day += 1) {
		options.push({ label: `每月${day}号`, value: day });
	}
	return options;
}

export function getSettlementPeriodRange(targetMonth, settlementDate = 0) {
	const monthRange = getMonthDateRange(targetMonth);
	if (!monthRange) return null;

	const day = Number(settlementDate) || 0;
	if (day <= 0) {
		return {
			startDate: monthRange.startDate,
			fullEndDate: monthRange.endDate,
			maxEndDate: monthRange.endDate,
		};
	}

	const [yearStr, monthStr] = targetMonth.split("-");
	const year = parseInt(yearStr, 10);
	const month = parseInt(monthStr, 10);
	const start = new Date(year, month - 2, day + 1);
	const startDate = `${start.getFullYear()}-${String(start.getMonth() + 1).padStart(2, "0")}-${String(start.getDate()).padStart(2, "0")}`;
	const fullEndDate = `${yearStr}-${monthStr.padStart(2, "0")}-${String(day).padStart(2, "0")}`;

	return {
		startDate,
		fullEndDate,
		maxEndDate: fullEndDate,
	};
}

export function getSettlementPeriodPickerDefaultDate(targetMonth, settlementDate = 0) {
	const { maxEndDate } = getSettlementPeriodRange(targetMonth, settlementDate) || {};
	if (!maxEndDate) return null;

	const [year, month, day] = maxEndDate.split("-").map(item => parseInt(item, 10));
	return new Date(year, month - 1, day);
}

export function getEditablePeriodStartDateRange(targetMonth) {
	if (!targetMonth) return null;
	const [yearStr, monthStr] = targetMonth.split("-");
	const year = parseInt(yearStr, 10);
	const month = parseInt(monthStr, 10);
	if (!year || !month) return null;

	const minStart = new Date(year, month - 2, 2);
	const minStartDate = `${minStart.getFullYear()}-${String(minStart.getMonth() + 1).padStart(2, "0")}-02`;
	const maxStartDate = `${yearStr}-${monthStr.padStart(2, "0")}-01`;
	return { minStartDate, maxStartDate };
}

export function getEditablePeriodStartPickerDefaultDate(targetMonth) {
	const { minStartDate } = getEditablePeriodStartDateRange(targetMonth) || {};
	if (!minStartDate) return null;

	const [year, month, day] = minStartDate.split("-").map(item => parseInt(item, 10));
	return new Date(year, month - 1, day);
}

export function getEditableSettlementPeriodRange(targetMonth, startDate) {
	const monthRange = getMonthDateRange(targetMonth);
	const startRange = getEditablePeriodStartDateRange(targetMonth);
	if (!monthRange || !startRange || !startDate) return null;
	if (startDate < startRange.minStartDate || startDate > startRange.maxStartDate) return null;

	const [, , startDayStr] = startDate.split("-");
	const startDay = parseInt(startDayStr, 10);
	const monthLastDay = parseInt(monthRange.endDate.split("-")[2], 10);
	const endDay = Math.min(startDay - 1, monthLastDay);
	const fullEndDate = startDate === monthRange.startDate
		? monthRange.endDate
		: `${targetMonth}-${String(endDay).padStart(2, "0")}`;

	return {
		startDate,
		fullEndDate,
		maxEndDate: fullEndDate,
	};
}

/**
 * 解析归属周期展示字符串为 [startDate, endDate]
 * 支持 2026/05/01-2026/05/15 与 2026-05-01-2026-05-15
 * @param {string} periodDateStr
 * @returns {[string, string] | null}
 */
export function parsePeriodDateStr(periodDateStr) {
	if (!periodDateStr) return null;
	const slashMatch = periodDateStr.match(/^(\d{4}\/\d{2}\/\d{2})-(\d{4}\/\d{2}\/\d{2})$/);
	if (slashMatch) {
		return [slashMatch[1].replace(/\//g, "-"), slashMatch[2].replace(/\//g, "-")];
	}
	const dashMatch = periodDateStr.match(/^(\d{4}-\d{2}-\d{2})-(\d{4}-\d{2}-\d{2})$/);
	if (dashMatch) {
		return [dashMatch[1], dashMatch[2]];
	}
	return null;
}

/**
 * 店面配置-门店主机厂交付数导入
 * 限制:当前日期>20号:仅允许选择当前月份;当前日期≤20号:允许选择当前月份和上个月
 */
export const rangePickerOptions_deliveryQuantity_import = {
	disabledDate: (time) => {
		const current = new Date();
		const currentYear = current.getFullYear();
		const currentMonth = current.getMonth();
		const currentDay = current.getDate();

		const allowedMonths = [];
		if (currentDay > 20) {
			allowedMonths.push({ year: currentYear, month: currentMonth });
		} else {
			allowedMonths.push(
				{ year: currentYear, month: currentMonth },
				currentMonth === 0
					? { year: currentYear - 1, month: 11 }
					: { year: currentYear, month: currentMonth - 1 },
			);
		}

		const year = time.getFullYear();
		const month = time.getMonth();
		const isAllowed = allowedMonths.some(
			m => m.year === year && m.month === month,
		);
		return !isAllowed;
	},
};