diff --git a/pom.xml b/pom.xml index 38fd691..f85457e 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,11 @@ commons-collections 3.2.2 + + + + + @@ -80,6 +85,7 @@ spring-boot-maven-plugin io.github.leaf.MyApplication + diff --git a/src/main/java/io/github/leaf/controllers/BaseController.java b/src/main/java/io/github/leaf/controllers/BaseController.java index 0159b75..a28b5fa 100644 --- a/src/main/java/io/github/leaf/controllers/BaseController.java +++ b/src/main/java/io/github/leaf/controllers/BaseController.java @@ -6,6 +6,10 @@ public class BaseController { + public static final String SUCCESS = "{\"state\":\"success\",\"msg\":\"\"}"; + + public static final String ERROR = "{\"state\":\"error\",\"reason\":\"\"}"; + protected Map jsonError(String reason) { Map map = new HashMap<>(); map.put("state", "error"); diff --git a/src/main/java/io/github/leaf/controllers/IndexController.java b/src/main/java/io/github/leaf/controllers/IndexController.java index 3030a27..4872c0d 100644 --- a/src/main/java/io/github/leaf/controllers/IndexController.java +++ b/src/main/java/io/github/leaf/controllers/IndexController.java @@ -20,6 +20,7 @@ import javax.servlet.http.HttpSession; import java.util.Collections; import java.util.Date; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -128,6 +129,20 @@ public Map delay(@PathVariable String id) { return jsonSuccess(format); } + @RequestMapping("delete/{id}") + public String delete(HttpServletRequest request, @PathVariable String id) throws Exception { + leafService.delete(id); + + String userId = getUserId(request); + LeafBag leafBag = userService.getLeafBag(userId); + LinkedList idList = Lists.newLinkedList(leafBag.getLeafIds()); + idList.remove(id); + leafBag.setLeafIds(idList); + userService.modify(leafBag); + + return SUCCESS; + } + private String format(Date date) { return DateUtil.getFormat("MM月dd日 HH:mm").format(date); } diff --git a/src/main/java/io/github/leaf/impl/LeafServiceImpl.java b/src/main/java/io/github/leaf/impl/LeafServiceImpl.java index b474dbe..24c0a51 100644 --- a/src/main/java/io/github/leaf/impl/LeafServiceImpl.java +++ b/src/main/java/io/github/leaf/impl/LeafServiceImpl.java @@ -18,14 +18,14 @@ public LeafServiceImpl(StorageService storageService) { this.storageService = storageService; } - private static final char[] BASE_LITERAL = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); + private static final char[] BASE_LITERAL = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray(); private String createId() { Long id = storageService.getIncrementId(); - return to64BasedNumber(id); + return to36BasedNumber(id); } - String to64BasedNumber(long id) { + String to36BasedNumber(long id) { int base = BASE_LITERAL.length; StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/io/github/leaf/impl/RedisServiceImpl.java b/src/main/java/io/github/leaf/impl/RedisServiceImpl.java index 5df221d..a55620c 100644 --- a/src/main/java/io/github/leaf/impl/RedisServiceImpl.java +++ b/src/main/java/io/github/leaf/impl/RedisServiceImpl.java @@ -8,18 +8,18 @@ import io.lettuce.core.RedisClient; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.sync.RedisCommands; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import javax.annotation.PostConstruct; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; -import java.util.ResourceBundle; @Service public class RedisServiceImpl implements StorageService { @@ -32,23 +32,24 @@ public class RedisServiceImpl implements StorageService { private static final String CURRENT_ID = "leaf:CURRENT_ID"; - private static RedisCommands redis; + private RedisCommands redis; - static { - redisInit(); - } + @Value("${redis.host}") + private String host; - private static void redisInit() { - String uri = "redis://:${password}@${host}:${port}/${database}"; + @Value("${redis.port}") + private String port; - ResourceBundle bundle = ResourceBundle.getBundle("redis"); - for (String key : bundle.keySet()) { - String value = bundle.getString(key); - uri = uri.replace("${" + key + "}", value); - } + @Value("${redis.password}") + private String password; - logger.info("redis uri:" + uri); + @Value("${redis.database}") + private String database; + @PostConstruct + public void init() { + String uri = "redis://:" + password + "@" + host + ":" + port + "/" + database; +// logger.info("redis uri:" + uri.replaceAll(password)); RedisClient client = RedisClient.create(uri); StatefulRedisConnection connect = client.connect(); redis = connect.sync(); @@ -156,8 +157,7 @@ private String getLeafBagKey(String id) { @Override public void modify(LeafBag leafBag) { if (null == leafBag - || StringUtils.isBlank(leafBag.getUserId()) - || CollectionUtils.isEmpty(leafBag.getLeafIds())) { + || StringUtils.isBlank(leafBag.getUserId())) { return; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 22952b9..887c37b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,7 @@ -server.port=8081 -logging.level.org.springframework.web=info \ No newline at end of file +server.port=8666 +logging.level.org.springframework.web=info +# redis +redis.host=localhost +redis.port=6379 +redis.password=72ca90980ca7c3a0 +redis.database=4 \ No newline at end of file diff --git a/src/main/resources/redis.properties b/src/main/resources/redis.properties deleted file mode 100644 index ea509b8..0000000 --- a/src/main/resources/redis.properties +++ /dev/null @@ -1,4 +0,0 @@ -host=127.0.0.1 -port=6350 -password=abcde -database=0 \ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index 04c3094..2394648 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -14,6 +14,9 @@ border-top: 0; border-bottom: 0; } + .dropdown-menu { + min-width: 80px; + } diff --git a/src/main/resources/static/show.html b/src/main/resources/static/show.html index 6bd0ed0..10d75ab 100644 --- a/src/main/resources/static/show.html +++ b/src/main/resources/static/show.html @@ -15,19 +15,23 @@ min-height: 545px; max-height: 545px; } + .rotate180 { + transform: rotate(180deg); + }
@@ -69,7 +73,8 @@ }, success: function (data) { if (isEmpty(data)) { - alert("empty!"); + alert("Note已过期!"); + window.location.href = "/"; return; } else { $("#delay").attr("leaf-id", data.leaf.id); @@ -79,6 +84,7 @@ showLineNumber(); hljs.initHighlighting.called = false; hljs.initHighlighting(); + document.title = showId } } }) @@ -93,6 +99,17 @@ } }) }); + + $('#delete').on('click', function () { + if (confirm('确定要销毁吗?')) { + $.ajax({ + url: "/delete/" + $('#delay').attr('leaf-id'), + success: function (data) { + window.location.href = "/"; + } + }); + } + });