123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package com.emato.ich.local;
- import com.emato.ich.utils.StringUtils;
- import java.util.concurrent.TimeUnit;
- public class LocalStorage {
- private final Session SESSION = new Session();
- private LocalStorage(){}
- public static LocalStorage getInstance(){
- return LocalStorageInnerClass.INSTANCE;
- }
- public Session getSession(){
- return SESSION;
- }
- public Session setSession(Session session) {
- if (StringUtils.isNullOrEmpty(SESSION.phone) || StringUtils.isNullOrEmpty(SESSION.token)) {
- SESSION.token = session.getToken();
- SESSION.phone = session.getPhone();
- if (session.getExpiryTime() > 0) {
- SESSION.expiryTime = session.getExpiryTime();
- }
- if (session.getTime() > 0) {
- SESSION.time = session.getTime();
- }
- }
- return SESSION;
- }
- public void cleanSession(){
- SESSION.phone = "";
- SESSION.token = "";
- }
- /**
- * 过期返回true, 否则返回false
- * @param session session
- * @return 是否
- */
- public boolean isExpired(Session session) {
- long sessionTime = session.getTime();
- long currentTime = System.currentTimeMillis();
- long diff = currentTime - sessionTime;
- return diff > session.getExpiryTime() * 60 * 1000;
- }
- public static final class LocalStorageInnerClass {
- private final static LocalStorage INSTANCE = new LocalStorage();
- }
- public static class Session {
- private String phone;
- private String token;
- private long time;
- private int expiryTime = 15;
- /**
- * 新增字段 只能做一次异常处理, 点击异常处理的 异常事件按钮 后该值为false
- */
- private boolean exception = true;
- private TimeUnit timeUnit = TimeUnit.MINUTES;
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public String getToken() {
- return token;
- }
- public void setToken(String token) {
- this.token = token.replace("\n", "").trim();
- }
- public long getTime() {
- return time;
- }
- public void setTime(long time) {
- this.time = time;
- }
- public int getExpiryTime() {
- return expiryTime;
- }
- public void setExpiryTime(int expiryTime) {
- this.expiryTime = expiryTime;
- }
- public TimeUnit getTimeUnit() {
- return timeUnit;
- }
- public void setTimeUnit(TimeUnit timeUnit) {
- this.timeUnit = timeUnit;
- }
- public boolean getException() {
- return exception;
- }
- public void setException(boolean exception) {
- this.exception = exception;
- }
- }
- }
|