290 lines
8.4 KiB
JavaScript
290 lines
8.4 KiB
JavaScript
//登录存储信息
|
||
const setLoginData=function(obj){
|
||
uni.setStorageSync("demu_mes_user_name",obj.mes_user_name);
|
||
uni.setStorageSync("mes_token",obj.mes_token);
|
||
}
|
||
|
||
const removeLoginData=function(){
|
||
uni.removeStorageSync("demu_mes_user_name");
|
||
uni.removeStorageSync("mes_token");
|
||
}
|
||
// 重定义 弹窗
|
||
const toast = function(params){
|
||
uni.showToast({
|
||
title:params.text,
|
||
icon:params.type,
|
||
duration:1000
|
||
})
|
||
}
|
||
|
||
// 0~9 对应小数转换
|
||
const numberToChineseLower = function(num) {
|
||
var chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
|
||
return num.toString().split('').map(function(digit) {
|
||
return chineseNums[parseInt(digit)];
|
||
}).join('');
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
|
||
export default {
|
||
setLoginData,
|
||
removeLoginData,
|
||
toast,
|
||
numberToChineseLower,
|
||
getDatesOfWeek,
|
||
getPreviousWeekDatesWithDays,
|
||
getNextWeekDatesWithDays,
|
||
getWeeksOfMonth,
|
||
getPreviousMonthGroupedByWeek,
|
||
getNextMonthGroupedByWeek,
|
||
getPreviousMonthWeekCount,
|
||
getNextMonthWeekCount,
|
||
}; |