controller
//用户签到实现
@PostMapping("/check")
public AjaxResult check(@PathVariable String id) {
//首先拼接key
String day = DateFormatUtils.format(new Date(), "yyyyMMdd");
String key = id + ":" +day;
//redis中是否存在该key
Boolean flag = redisTemplate.hasKey(key);
if(flag){
return AjaxResult.error(500,"今日用户已签到");
}else{
//设置redis中的过期时间,凌晨0点清空;
redisTemplate.opsForValue().set(key, day, getRefreshTime(), TimeUnit.SECONDS);
//将未签到用户记录在mysql中
int i = iPlatUserService.insert(id);
if(i > 0){
//签到成功
return AjaxResult.success("用户成功签到");
}else {
return AjaxResult.error(500,"由于不正常原因,用户签到失败!");
}
}
}
/*
* 获取当前时间离明天凌晨还有多少时间
* */
public static int getRefreshTime(){
Calendar calendar = Calendar.getInstance();
int now = (int) (calendar.getTimeInMillis()/1000);
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY , 0);
return (int) (calendar.getTimeInMillis()/1000-now);
}
|