|
@@ -1,14 +1,22 @@
|
|
|
package cn.org.spring.wechar.controller;
|
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.org.spring.common.util.StringUtil;
|
|
|
import cn.org.spring.wechar.bean.Result;
|
|
|
+import cn.org.spring.wechar.utils.CacheUtil;
|
|
|
import cn.org.spring.wechar.utils.PrizeRandomUtil;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.CookieValue;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author: 谢辉
|
|
@@ -22,6 +30,9 @@ import java.util.Map;
|
|
|
@RequestMapping("/v1/prize")
|
|
|
public class PrizeController {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ CacheUtil cacheUtil;
|
|
|
+
|
|
|
// String 可以为任意类型 也可以自定义类型
|
|
|
static Map<String, Integer> keyChanceMap = new HashMap<String, Integer>();
|
|
|
|
|
@@ -39,11 +50,37 @@ public class PrizeController {
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/getTelCharge")
|
|
|
- public Result getTelCharge() {
|
|
|
- String result = PrizeRandomUtil.chanceSelect(keyChanceMap);
|
|
|
- if (StringUtil.isEmpty(result)) {
|
|
|
- result = "0";
|
|
|
+ public Result getTelCharge(@CookieValue(value = "wechat-token") String userId) {
|
|
|
+ if (StringUtils.isEmpty(userId)) {
|
|
|
+ return Result.fail("获取用户信息失败");
|
|
|
+ }
|
|
|
+ Boolean exists = cacheUtil.exists("lottery:" + userId);
|
|
|
+ if (exists) {
|
|
|
+ return Result.fail("一天只能抽取一次,请您明天再来~~~");
|
|
|
+ } else {
|
|
|
+ String result = PrizeRandomUtil.chanceSelect(keyChanceMap);
|
|
|
+ if (StringUtil.isEmpty(result)) {
|
|
|
+ result = "0";
|
|
|
+ }
|
|
|
+ long currentTime = System.currentTimeMillis();
|
|
|
+ long diff = timeDiff(currentTime);
|
|
|
+ if (diff > 0) {
|
|
|
+ cacheUtil.set("lottery:" + userId, result + ":" + currentTime, diff, TimeUnit.MILLISECONDS);
|
|
|
+ }
|
|
|
+ return Result.ok(result);
|
|
|
}
|
|
|
- return Result.ok(result);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间和当天结束的时间差
|
|
|
+ *
|
|
|
+ * @param currentTimeMillis 单位毫秒
|
|
|
+ * @return 时间差
|
|
|
+ */
|
|
|
+ private long timeDiff(long currentTimeMillis) {
|
|
|
+ DateTime dateTime = DateUtil.endOfDay(new Date());
|
|
|
+ long endDayOfMillis = dateTime.getTime();
|
|
|
+ return (endDayOfMillis - currentTimeMillis);
|
|
|
}
|
|
|
}
|