前端实现每日签到功能,如果今天签了今天就不用在签到了

export default{
    data{
        return{
            reg:false,		//是否已经签到
			regArr:[],		//签到数组
        }
    },
    methods:{
        regMethods(){
    	    var nowdate = this.getToday();   //获取到今天日期
            //先取出缓存,分为三种情况:(没有缓存),(有缓存,今天没签到),(有缓存今天已签到)
            if(Object.keys(this.getCache("videoregdata")).length === 0){ //判断今天是否签到了,            
              等于0那么 证明没有签到,或者之前的签到被清楚了
		        this.regArr.push(nowdate)
		        this.setCache("videoregdata",this.regArr)
		        this.reg = true;
	        }else{
	 	        if(this.getCache("videoregdata").indexOf(nowdate) == -1){  //判断今天是否已经签到,等于-1证明今天没有签到
	 	            this.regArr = getCache("videoregdata")
	 		        this.regArr.push(nowdate)
		 	        this.setCache("videoregdata",this.regArr)
		 		  	this.reg = true;
	 		    }else{												//证明今天签过到了
	 				this.reg = false;
	 			}
	 	}
	},
    getToday(){
		    var date = new Date;
		    var Year = date.getFullYear();
		    var month=date.getMonth()+1 <         
            10?"0"+String(date.getMonth()+1):date.getMonth()+1;
		    var strDate = date.getDate() < 10?"0"+String(date.getDate()):date.getDate();
		    var setDate =Year + String(month) + String(strDate);
		    return setDate
    },
    // 设置缓存
setCache (key, val, day) {
  day = day || 1
  // 获取当前缓存
  let cache = getItem(key)
  // 获取当前时间
  let date = getDate()
  // 今天日期
  let toDayDate = dateToNumber(`${date.year}-${date.month}-${date.day}`)

  // 缓存存在
  if (cache) {
    // 缓存过期了
    if (pastDue(cache.t, day)) {
      val.t = toDayDate
      setItem(key, val)
    } else {
      setItem(key, Object.assign(cache, val))
    }
  } else {
    // 缓存不存在
    val.t = toDayDate
    setItem(key, val)
  }
},

// 获取缓存
getCache (key,  day) {
  // 获取当前缓存
  let cache = getItem(key)
  // 缓存存在
  if (cache) {
    // 缓存过期了
    if (pastDue(cache.t, day)) {
      return {}
    } else {
      return cache
    }
  } else {
    // 缓存不存在
    return {}
  }
}

        
 }

}


 

你可能感兴趣的:(原生js,签到,vue实现签到)