mes_mobile/utils/public.js

421 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2024-03-13 16:07:11 +08:00
//登录存储信息
const setLoginData=function(obj){
uni.setStorageSync("demu_mes_user_name",obj.mes_user_name);
uni.setStorageSync("mes_token",obj.mes_token);
2024-04-23 11:48:55 +08:00
}
const removeLoginData=function(){
uni.removeStorageSync("demu_mes_user_name");
uni.removeStorageSync("mes_token");
2024-04-30 16:08:07 +08:00
}
// 重定义 弹窗
const toast = function(params){
uni.showToast({
title:params.text,
icon:params.type,
duration:1000
})
}
// 0~9 对应小数转换
const numberToChineseLower = function(num) {
2024-05-07 19:37:20 +08:00
const chineseMonths = {
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六',
'7': '七',
'8': '八',
'9': '九',
'10': '十',
'11': '十一',
'12': '十二'
};
return chineseMonths[num] || '';
}
const getToday = function() {
let today = new Date();
let year = today.getFullYear();
let month = today.getMonth() + 1;
let day = today.getDate();
let formattedMonth = month < 10 ? '0' + month : month;
let formattedDay = day < 10 ? '0' + day : day;
// 返回格式化后的日期字符串,格式为 yyyy-mm-dd
return year + '-' + formattedMonth + '-' + formattedDay;
2024-04-30 16:08:07 +08:00
}
const getDatesOfWeek = function() {
var now = new Date();
// 获取当前日期是周几0表示周日1表示周一以此类推
var dayOfWeek = now.getDay();
var monday = new Date(now.setDate(now.getDate() - dayOfWeek + (dayOfWeek === 0 ? 0 : 1)));
var datesOfWeek = [];
for (var i = 0; i < 7; i++) {
var currentDate = new Date(monday.getTime() + i * 24 * 60 * 60 * 1000);
var weekdays = ['日', '一', '二', '三', '四', '五', '六'];
var weekday = weekdays[currentDate.getDay()];
var dateInfo = {
date: currentDate.toISOString().split('T')[0],
name: weekday
};
datesOfWeek.push(dateInfo);
}
return datesOfWeek;
}
// 往前一周
const getPreviousWeekDatesWithDays = function(date=null) {
var now = new Date(date);
var lastMonday = new Date(now);
lastMonday.setDate(lastMonday.getDate() - 7);
var datesAndDays = [];
for (var i = 0; i < 7; i++) {
var currentDate = new Date(lastMonday.getTime() + i * 24 * 60 * 60 * 1000);
var weekdays = ['日', '一', '二', '三', '四', '五', '六'];
datesAndDays.push({
date: currentDate.toISOString().split('T')[0],
name: weekdays[currentDate.getDay()]
});
}
return datesAndDays;
}
// 往后一周
const getNextWeekDatesWithDays = function(date = null) {
var now = new Date(date);
var nextMonday = new Date(now);
nextMonday.setDate(nextMonday.getDate() + (8 - now.getDay()) % 7);
var datesAndDays = [];
for (var i = 0; i < 7; i++) {
var currentDate = new Date(nextMonday.getTime() + i * 24 * 60 * 60 * 1000);
var weekdays = ['日', '一', '二', '三', '四', '五', '六'];
datesAndDays.push({
date: currentDate.toISOString().split('T')[0],
name: weekdays[currentDate.getDay()]
});
}
return datesAndDays;
}
// 获取本月日期
// year, month
const getWeeksOfMonth = function() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
const startDate = new Date(year, month - 1, 1);
const endDate = new Date(year, month, 0);
const startDateDayOfWeek = startDate.getDay(); // 获取本月第一天是周几
const totalDays = endDate.getDate(); // 获取本月总天数
let weeks = [];
let currentWeek = [];
let currentDate = new Date(startDate);
// 填充到本周开始
for (let i = 0; i < startDateDayOfWeek; i++) {
currentWeek.push(null);
}
while (currentDate <= endDate) {
let textMonth = month<10?'0'+month:month;
let textDate = currentDate.getDate()<10?'0'+currentDate.getDate():currentDate.getDate();
currentWeek.push({
date: year +'-'+ textMonth +'-'+ textDate,
name: ['一', '二', '三', '四', '五', '六','日'][currentDate.getDay()]
});
currentDate.setDate(currentDate.getDate() + 1);
// 当前周已满,开始新周
if (currentWeek.length === 7) {
weeks.push(currentWeek);
currentWeek = [];
}
}
// 处理最后不满一周的情况
if (currentWeek.length > 0) {
while (currentWeek.length < 7) {
currentWeek.push(null);
}
weeks.push(currentWeek);
}
return weeks;
}
// 往前推一个月
const getPreviousMonthGroupedByWeek = function(date = null) {
let now = new Date(date);
var year = now.getFullYear();
var month = now.getMonth();
// 如果当前月份是1月则年份减1月份变成12月
if (month === 0) {
year--;
month = 11;
} else {
month--;
}
let prevMonthFirstDay = new Date(year, month, 1); // 获取上一个月的第一天
let prevMonthLastDay = new Date(year, month + 1, 0); // 获取上一个月的最后一天
let currentDate = new Date(prevMonthFirstDay); // 从上一个月的第一天开始
let weeksData = [];
let currentWeek = [];
const weekdays = ['一', '二', '三', '四', '五', '六', '日'];
while (currentDate <= prevMonthLastDay) {
// 获取当前日期和星期几
let date = currentDate.getDate();
let weekdayIndex = currentDate.getDay();
let textMonth = month<9?'0'+(month+1):(month+1);
let textDate = date<10?'0'+date:date;
currentWeek.push({
date: year + '-' + textMonth + '-' + textDate,
name: weekdays[weekdayIndex]
});
// 如果到达了周日或者已经是最后一日将当前周添加到weeksData并重置currentWeek
if (weekdayIndex === 6 || currentDate.getTime() === prevMonthLastDay.getTime()) {
weeksData.push(currentWeek);
currentWeek = [];
}
// 前往下一天
currentDate.setDate(currentDate.getDate() + 1);
}
return weeksData;
}
const getNextMonthGroupedByWeek = function(date = null) {
let now = new Date(date);
var year = now.getFullYear();
var month = now.getMonth();
if (month === 11) {
year++;
month = 0;
} else {
month++;
}
let nextMonthFirstDay = new Date(year, month, 1); // 获取下一个月的第一天
let nextMonthLastDay = new Date(year, month + 1, 0); // 获取下一个月的最后一天
let currentDate = new Date(nextMonthFirstDay); // 从下一个月的第一天开始
let weeksData = [];
let currentWeek = [];
const weekdays = ['一', '二', '三', '四', '五', '六', '日'];
while (currentDate <= nextMonthLastDay) {
let date = currentDate.getDate();
let weekdayIndex = currentDate.getDay();
let textMonth = month<9?'0'+(month+1):(month+1);
let textDate = date<10?'0'+date:date;
currentWeek.push({
date: year + '-' + textMonth + '-' + textDate,
name: weekdays[weekdayIndex]
});
if (weekdayIndex === 6 || currentDate.getTime() === nextMonthLastDay.getTime()) {
weeksData.push(currentWeek);
currentWeek = [];
}
// 前往下一天
currentDate.setDate(currentDate.getDate() + 1);
}
return weeksData;
}
// 上一个月有几周
const getPreviousMonthWeekCount = function(date = null) {
let now = new Date(date);
var year = now.getFullYear();
var month = now.getMonth();
if (month === 0) {
year--;
month = 11;
} else {
month--;
}
let prevMonthFirstDay = new Date(year, month, 1); // 获取上一个月的第一天
let prevMonthLastDay = new Date(year, month + 1, 0); // 获取上一个月的最后一天
let totalWeeks = 0;
// 计算上一个月有几周
let currentDate = new Date(prevMonthFirstDay);
while (currentDate <= prevMonthLastDay) {
let weekdayIndex = currentDate.getDay();
if (weekdayIndex === 6 || currentDate.getTime() === prevMonthLastDay.getTime()) {
totalWeeks++;
}
currentDate.setDate(currentDate.getDate() + 1);
}
return totalWeeks;
}
// 下个月有几周
const getNextMonthWeekCount = function(date = null) {
let now = new Date(date);
var year = now.getFullYear();
var month = now.getMonth();
if (month === 11) {
year++;
month = 0;
} else {
month++;
}
let nextMonthFirstDay = new Date(year, month, 1); // 获取下一个月的第一天
let nextMonthLastDay = new Date(year, month + 1, 0); // 获取下一个月的最后一天
let totalWeeks = 0;
let currentDate = new Date(nextMonthFirstDay);
while (currentDate <= nextMonthLastDay) {
let weekdayIndex = currentDate.getDay();
if (weekdayIndex === 6 || currentDate.getTime() === nextMonthLastDay.getTime()) {
totalWeeks++;
}
// 前往下一天
currentDate.setDate(currentDate.getDate() + 1);
}
return totalWeeks;
}
2024-05-07 19:37:20 +08:00
// 获取当前的日期
const getCurrentYearData = function() {
let today = new Date(); // 获取当前日期
let year = today.getFullYear(); // 获取年份
let yearData = []; // 存储当前年份的数据
// 循环遍历每个月份
for (let month = 0; month < 12; month++) {
let list = getCurrentMonthData(year,month+1);
let monthData = {
month: month + 1, // 月份从 1 开始
weeks: list, // 存储该月份的所有周数据
};
// 将该月份的数据添加到当前年份的数据中
yearData.push(monthData);
}
return yearData;
}
// 获取上一年的日期
const getPreviousYearData = function(date = null) {
let currentDate = new Date(date); // 获取指定日期或者当前日期
currentDate.setFullYear(currentDate.getFullYear() - 1); // 获取指定日期的上一年日期
let year = currentDate.getFullYear(); // 获取年份
let yearData = []; // 存储指定日期的上一年的数据
for (let month = 0; month < 12; month++) {
let list = getCurrentMonthData(year, month + 1);
let monthData = {
month: month + 1,
weeks: list,
};
yearData.push(monthData);
}
return yearData;
}
// 获取下一年的日期
const getNextYearData = function(date = null) {
let currentDate = new Date(date); // 获取指定日期或者当前日期
currentDate.setFullYear(currentDate.getFullYear() + 1); // 获取指定日期的上一年日期
let year = currentDate.getFullYear(); // 获取年份
let yearData = []; // 存储指定日期的上一年的数据
for (let month = 0; month < 12; month++) {
let list = getCurrentMonthData(year, month + 1);
let monthData = {
month: month + 1,
weeks: list,
};
yearData.push(monthData);
}
return yearData;
}
// 获取当前月的所有周
const getCurrentMonthData = function(year,month) {
const startDate = new Date(year, month - 1, 1);
const endDate = new Date(year, month, 0);
const startDateDayOfWeek = startDate.getDay(); // 获取本月第一天是周几
const totalDays = endDate.getDate(); // 获取本月总天数
let weeks = [];
let currentWeek = [];
let currentDate = new Date(startDate);
// 填充到本周开始
for (let i = 0; i < startDateDayOfWeek; i++) {
currentWeek.push(null);
}
while (currentDate <= endDate) {
let textMonth = month<10?'0'+month:month;
let textDate = currentDate.getDate()<10?'0'+currentDate.getDate():currentDate.getDate();
currentWeek.push({
date: year +'-'+ textMonth +'-'+ textDate,
name: ['一', '二', '三', '四', '五', '六','日'][currentDate.getDay()]
});
currentDate.setDate(currentDate.getDate() + 1);
// 当前周已满,开始新周
if (currentWeek.length === 7) {
weeks.push(currentWeek);
currentWeek = [];
}
}
// 处理最后不满一周的情况
if (currentWeek.length > 0) {
while (currentWeek.length < 7) {
currentWeek.push(null);
}
weeks.push(currentWeek);
}
return weeks;
}
2024-03-13 16:07:11 +08:00
export default {
2024-04-23 11:48:55 +08:00
setLoginData,
2024-04-30 16:08:07 +08:00
removeLoginData,
toast,
numberToChineseLower,
2024-05-07 19:37:20 +08:00
getToday,
2024-04-30 16:08:07 +08:00
getDatesOfWeek,
getPreviousWeekDatesWithDays,
getNextWeekDatesWithDays,
getWeeksOfMonth,
getPreviousMonthGroupedByWeek,
getNextMonthGroupedByWeek,
getPreviousMonthWeekCount,
2024-05-07 19:37:20 +08:00
getNextMonthWeekCount,
getCurrentYearData,
getPreviousYearData,
getNextYearData,
2024-03-13 16:07:11 +08:00
};