处理时间hook函数

125 阅读1分钟
import { computed } from 'vue';
export const useCurrentTime = () => {
  const startDate = computed(() => getDate('start'));
  const endDate = computed(() => getDate('end'));
  const lastMonthDate = computed(() => getDate('lastMonth'));
  const currentDate = computed(() => getDate({ format: true }));
  const getClock = computed(() => getDateClock());
  function getDate(type) {
    const date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    if (type === 'start') {
      year = year - 1000;
    } else if (type === 'end') {
      year = year + 1000;
    }else if(type === 'lastMonth'){
      month = month+1
    }
    month = month > 9 ? month : '0' + month;
    day = day > 9 ? day : '0' + day;
    return `${year}-${month}-${day}`;
  }
  function getDateDetails(time) {
    let date;
    if (!time) {
      date = new Date();
    } else {
      date = new Date(time);
    }
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hh = date.getHours(); // 获取当前小时数(0-23)
    let mm = date.getMinutes(); // 获取当前分钟数(0-59)
    let ss = date.getSeconds(); // 获取当前秒数(0-59)
    month = month > 9 ? month : '0' + month;
    day = day > 9 ? day : '0' + day;
    mm = mm > 9 ? mm : '0' + mm;
    hh = hh > 9 ? hh : '0' + hh;
    ss = ss > 9 ? ss : '0' + ss;
    return `${year}-${month}-${day} ${hh}:${mm}:${ss}`;
  }
  function getDateClock() {
    const date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hh = date.getHours(); // 获取当前小时数(0-23) 
   let mm = date.getMinutes(); // 获取当前分钟数(0-59)
    let ss = date.getSeconds(); // 获取当前秒数(0-59)
    month = month > 9 ? month : '0' + month;
    day = day > 9 ? day : '0' + day;
    mm = mm > 9 ? mm : '0' + mm;
    hh = hh > 9 ? hh : '0' + hh;
    ss = ss > 9 ? ss : '0' + ss;
    return `${hh}:${mm}`;
  }
function getLongTime(start: string, end: string) {   // 数字版
  // console.log(start, end);
  const date1 = new Date(start.substring(0, 10).replace(/\-/g, "-"));
  const date2 = new Date(end.substring(0, 10).replace(/\-/g, "-"));
  let day: number = date2.getTime() - date1.getTime();
  // if (day < 0) { 
 //   ElMessage.warning("不能晚于上一阶段时间");
  //   return 0;
  // }
  let longTime: number = Math.ceil(day / (1000 * 60 * 60 * 24));
  return longTime;
}
  function getLongTime(start, end, today) {  //汉字版
    const date1 = new Date(start.substring(0, 10).replace(/\-/g, '/'));
    const date2 = new Date(end.substring(0, 10).replace(/\-/g, '/'));
    let day = date2.getTime() - date1.getTime();
    let longTime = parseInt(day / (1000 * 60 * 60 * 24));
    let narr = longTime.toString();
    let str = ''; 
   var arr = new Array(      '十',      '一',      '二',      '三',      '四',      '五',      '六',      '七',      '八',      '九'    );
    if (narr.length == 2) {
      if (narr[1] == 0) {
        str = arr[narr[0]] + '' + arr[narr[1]];
      } else {
        str = arr[narr[0]] + '十' + arr[narr[1]];
      }
    } else {
      str = arr[narr];
    }
    return longTime + 1;
  }  
function statusTime(start, end, today) {
    const date0 = new Date(end.substring(0, 10).replace(/\-/g, '/')).getTime();
    const date1 = new Date(start.substring(0, 10).replace(/\-/g, '/')).getTime();
    const date2 = new Date(today.substring(0, 10).replace(/\-/g, '/')).getTime();
    if (date2 < date1) {
      return '未开始';
    } else if (date2 > date0) {
      return '已结束';
    } else {
      let day = date2 - date1;
      let longTime = parseInt(day / (1000 * 60 * 60 * 24));
      return `第${longTime + 1}天`;
    }
  }
 

// date.js
import dayjs from "@/plugin/dayjs/dayjs.min.js";

/**
 * 获得当前周的开始和结束时间
 */
export function getWeekTimes() {
  const today = new Date();
  const dayOfWeek = today.getDay();
  return [
    new Date(today.getFullYear(), today.getMonth(), today.getDate() - dayOfWeek, 0, 0, 0),
    new Date(today.getFullYear(), today.getMonth(), today.getDate() + (6 - dayOfWeek), 23, 59, 59)
  ]
}

/**
 * 获得当前月的开始和结束时间
 */
export function getMonthTimes() {
  const today = new Date();
  const year = today.getFullYear();
  const month = today.getMonth();
  const startDate = new Date(year, month, 1, 0, 0, 0);
  const nextMonth = new Date(year, month + 1, 1);
  const endDate = new Date(nextMonth.getTime() - 1);
  return [startDate, endDate]
} 

//index.vue
import * as DateUtil from '@/utils/date.js';
import dayjs from "@/plugin/dayjs/dayjs.min.js";let times;if (this.type === 'week') {          times = DateUtil.getWeekTimes();} else {          times = DateUtil.getMonthTimes();}this.times = [ this.formatDate(times[0]), this.formatDate(times[1]) ]

formatDate: function(date) {        return dayjs(date).format("YYYY-MM-DD HH:mm:ss");},

微信小程序音频播放

export const useAutoVideo = () => {

function eggMusic() {

const innerAudioContext = wx.createInnerAudioContext()/**微信小程序开发文档自带的音频方法 */

innerAudioContext.autoplay = true/**true是开启自动播放,false则关闭*/

// innerAudioContext.src = 'bjetxgzv.cdn.bspapp.com/VKCEYUGU-he… 可以放在线的也可以放本地的 */

// 微信小程序音频不支持本地路径

innerAudioContext.src = 'http://114.116.36.61:6911/product/product\_1688184907043.mp3'/\*\*你要播放的音频文件的地址 可以放在线的也可以放本地的 */

innerAudioContext.onPlay(() => {/**开始播放是触发 */

// console.log('Start playback')

})

// console.log(innerAudioContext.src);

innerAudioContext.onError((res) => {/**播放是有错误时触发 */

// console.log(res.errMsg)

// console.log(res.errCode)

})

}

return {eggMusic}

}

滑动事件

@touchstart='touchStart' @touchmove='touchMove' @touchend='touchEnd'

function touchStart(e) {

//开启滑动事件

slipFlag.value = true

//记录触摸点的坐标信息

startPoint.value = e.touches[0]

}

async function touchMove(e) {

ResultList.value = []

// console.log(e.touches);

if (startPoint.value.clientX - e.touches[e.touches.length - 1]?.clientX > 80 && slipFlag.value) {

slipFlag.value = false

// console.log('左滑')

let date1 = new Date(new Date(current.value).getTime() + 24 * 60 * 60 * 1000)

current.value = date2Str(date1)

getResList()

getTodoThing()

eggMusic()

// getPassJiNianriList()

return

} else if (

startPoint.value.clientX - e.touches[e.touches.length - 1]?.clientX < -80 &&

slipFlag.value

) {

slipFlag.value = false

let date1 = new Date(new Date(current.value).getTime() - 24 * 60 * 60 * 1000)

current.value = date2Str(date1)

// getResList()

// getTodoThing()

// eggMusic()

// getPassJiNianriList()

// console.log('右滑')

return

}

}