当前位置:首页 > 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()
}

完整示例

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

<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
分享给朋友:

相关文章

订单 vue实现

订单 vue实现

Vue 实现订单功能 数据模型设计 订单功能通常需要设计以下数据结构: // 订单数据结构 const order = { id: '', // 订单ID userId…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Token…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…