当前位置:首页 > VUE

vue实现打卡提醒

2026-01-18 19:03:04VUE

实现思路

使用Vue开发打卡提醒功能,主要涉及前端界面交互、本地存储或后端数据同步、定时提醒逻辑。以下是具体实现方法:

界面设计

创建打卡按钮和状态显示组件,使用Vue的响应式数据绑定功能。示例代码:

<template>
  <div>
    <button @click="handleCheckIn" :disabled="isCheckedIn">打卡</button>
    <p v-if="isCheckedIn">今日已打卡</p>
    <p v-else>今日未打卡</p>
  </div>
</template>

数据存储

采用浏览器本地存储保存打卡记录,适合纯前端实现:

data() {
  return {
    isCheckedIn: localStorage.getItem('checkedIn') === 'true'
  }
},
methods: {
  handleCheckIn() {
    localStorage.setItem('checkedIn', 'true')
    this.isCheckedIn = true
  }
}

定时提醒

利用浏览器的Notification API实现系统通知,需要先请求用户授权:

methods: {
  requestNotificationPermission() {
    Notification.requestPermission().then(permission => {
      if (permission === 'granted') {
        this.setReminder()
      }
    })
  },
  setReminder() {
    const now = new Date()
    const targetTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 0) // 下午6点提醒

    if (now < targetTime && !this.isCheckedIn) {
      setTimeout(() => {
        new Notification('打卡提醒', { body: '记得完成今日打卡!' })
      }, targetTime - now)
    }
  }
},
mounted() {
  this.requestNotificationPermission()
}

每日重置

添加逻辑在次日自动重置打卡状态:

methods: {
  checkDateChange() {
    const lastCheckDate = localStorage.getItem('lastCheckDate')
    const today = new Date().toDateString()

    if (lastCheckDate !== today) {
      localStorage.setItem('checkedIn', 'false')
      localStorage.setItem('lastCheckDate', today)
      this.isCheckedIn = false
    }
  }
},
mounted() {
  this.checkDateChange()
}

完整示例

结合上述功能的完整组件示例:

vue实现打卡提醒

<template>
  <div>
    <button @click="handleCheckIn" :disabled="isCheckedIn">打卡</button>
    <p v-if="isCheckedIn">今日已打卡</p>
    <p v-else>今日未打卡</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCheckedIn: localStorage.getItem('checkedIn') === 'true'
    }
  },
  methods: {
    handleCheckIn() {
      localStorage.setItem('checkedIn', 'true')
      localStorage.setItem('lastCheckDate', new Date().toDateString())
      this.isCheckedIn = true
    },
    requestNotificationPermission() {
      Notification.requestPermission().then(permission => {
        if (permission === 'granted') {
          this.setReminder()
        }
      })
    },
    setReminder() {
      const now = new Date()
      const targetTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 0)

      if (now < targetTime && !this.isCheckedIn) {
        setTimeout(() => {
          new Notification('打卡提醒', { body: '记得完成今日打卡!' })
        }, targetTime - now)
      }
    },
    checkDateChange() {
      const lastCheckDate = localStorage.getItem('lastCheckDate')
      const today = new Date().toDateString()

      if (lastCheckDate !== today) {
        localStorage.setItem('checkedIn', 'false')
        localStorage.setItem('lastCheckDate', today)
        this.isCheckedIn = false
      }
    }
  },
  mounted() {
    this.checkDateChange()
    this.requestNotificationPermission()
  }
}
</script>

进阶优化

  1. 添加后端API集成,使用axios发送打卡数据到服务器
  2. 实现历史打卡记录查看功能
  3. 添加多设备同步支持
  4. 使用Vuex管理全局打卡状态
  5. 添加打卡统计图表展示

以上实现可根据实际需求进行调整和扩展。

标签: vue
分享给朋友:

相关文章

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…