LocalStorage.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.emato.ich.local;
  2. import com.emato.ich.utils.StringUtils;
  3. import java.util.concurrent.TimeUnit;
  4. public class LocalStorage {
  5. private final Session SESSION = new Session();
  6. private LocalStorage(){}
  7. public static LocalStorage getInstance(){
  8. return LocalStorageInnerClass.INSTANCE;
  9. }
  10. public Session getSession(){
  11. return SESSION;
  12. }
  13. public Session setSession(Session session) {
  14. if (StringUtils.isNullOrEmpty(SESSION.phone) || StringUtils.isNullOrEmpty(SESSION.token)) {
  15. SESSION.token = session.getToken();
  16. SESSION.phone = session.getPhone();
  17. if (session.getExpiryTime() > 0) {
  18. SESSION.expiryTime = session.getExpiryTime();
  19. }
  20. if (session.getTime() > 0) {
  21. SESSION.time = session.getTime();
  22. }
  23. }
  24. return SESSION;
  25. }
  26. public void cleanSession(){
  27. SESSION.phone = "";
  28. SESSION.token = "";
  29. }
  30. /**
  31. * 过期返回true, 否则返回false
  32. * @param session session
  33. * @return 是否
  34. */
  35. public boolean isExpired(Session session) {
  36. long sessionTime = session.getTime();
  37. long currentTime = System.currentTimeMillis();
  38. long diff = currentTime - sessionTime;
  39. return diff > session.getExpiryTime() * 60 * 1000;
  40. }
  41. public static final class LocalStorageInnerClass {
  42. private final static LocalStorage INSTANCE = new LocalStorage();
  43. }
  44. public static class Session {
  45. private String phone;
  46. private String token;
  47. private long time;
  48. private int expiryTime = 15;
  49. /**
  50. * 新增字段 只能做一次异常处理, 点击异常处理的 异常事件按钮 后该值为false
  51. */
  52. private boolean exception = true;
  53. private TimeUnit timeUnit = TimeUnit.MINUTES;
  54. public String getPhone() {
  55. return phone;
  56. }
  57. public void setPhone(String phone) {
  58. this.phone = phone;
  59. }
  60. public String getToken() {
  61. return token;
  62. }
  63. public void setToken(String token) {
  64. this.token = token.replace("\n", "").trim();
  65. }
  66. public long getTime() {
  67. return time;
  68. }
  69. public void setTime(long time) {
  70. this.time = time;
  71. }
  72. public int getExpiryTime() {
  73. return expiryTime;
  74. }
  75. public void setExpiryTime(int expiryTime) {
  76. this.expiryTime = expiryTime;
  77. }
  78. public TimeUnit getTimeUnit() {
  79. return timeUnit;
  80. }
  81. public void setTimeUnit(TimeUnit timeUnit) {
  82. this.timeUnit = timeUnit;
  83. }
  84. public boolean getException() {
  85. return exception;
  86. }
  87. public void setException(boolean exception) {
  88. this.exception = exception;
  89. }
  90. }
  91. }