| /** | 
|  * projectName: zq-erp | 
|  * fileName: UserCacheManager.java | 
|  * packageName: com.matrix.system.common.init | 
|  * date: 2021-10-25 16:58 | 
|  * copyright(c) 2021 http://www.hydee.cn/ Inc. All rights reserved. | 
|  */ | 
| package com.matrix.system.common.init; | 
|   | 
| import com.matrix.core.constance.SystemErrorCode; | 
| import com.matrix.core.exception.GlobleException; | 
| import com.matrix.core.tools.StringUtils; | 
| import com.matrix.core.tools.UUIDUtil; | 
| import com.matrix.core.tools.WebUtil; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * @version: V1.0 | 
|  * @author: JiangYouYao | 
|  * @className: UserCacheManager | 
|  * @packageName: com.matrix.system.common.init | 
|  * @description: 用户缓存管理 | 
|  * @data: 2021-10-25 16:58 | 
|  **/ | 
| @Component | 
| public class UserCacheManager { | 
|   | 
|     @Value("${debug}") | 
|     private String isDebug; | 
|   | 
|   | 
|     /** | 
|      * 判断用户是否已经登录过 | 
|      * | 
|      * @return | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年1月19日 | 
|      * 会从http请求投中获取token进行验证 | 
|      */ | 
|     public boolean isUserLogin() { | 
|         String token = getUserToken(); | 
|         if (StringUtils.isNotBlank(token)) { | 
|             return LocalCache.get(token) != null; | 
|         } else { | 
|             return false; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 在本地缓存中保存用户信息,并且返回保存的token | 
|      * | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年1月19日 | 
|      * @return 返回保存用户信息的token | 
|      */ | 
|     public String saveUserInfo(Object obj) { | 
|         String token = UUIDUtil.getRandomID(); | 
|         LocalCache.save(token, obj); | 
|        return token; | 
|     } | 
|   | 
|     /** | 
|      * 用户退出 | 
|      * | 
|      * @author:吕敬瑛 | 
|      * @date:2018年1月19日下午3:17:14 | 
|      */ | 
|     public void loginOut() { | 
|         LocalCache.remove(getUserToken()); | 
|     } | 
|   | 
|     /** | 
|      * 更新用户缓存 | 
|      */ | 
|     public void updateUserInfo(Object user) { | 
|         LocalCache.save(getUserToken(),user); | 
|     } | 
|   | 
|     /** | 
|      * 获取登录用户信息,如果 获取失败将会抛出异常 | 
|      * | 
|      * @return | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年1月19日 | 
|      */ | 
|     public <T> T getLoginUser() { | 
|         String userToken = getUserToken(); | 
|         if (userToken != null) { | 
|             T user = LocalCache.get(userToken); | 
|             if (Objects.nonNull(user)) { | 
|                 //重新设置key过期时间 | 
|                 LocalCache.resetExpire(userToken); | 
|                 return user; | 
|             } else { | 
|                 throw new GlobleException(SystemErrorCode.REQUEST_INVALID); | 
|             } | 
|         } else { | 
|             throw new GlobleException(SystemErrorCode.REQUEST_INVALID); | 
|         } | 
|     } | 
|   | 
|     public String getUserToken() { | 
|         String token = WebUtil.getRequest().getHeader("token"); | 
|         // debug模式可以从url参数中获取token | 
|         if (StringUtils.isBlank(token) && "true".equals(isDebug)) { | 
|             if (token == null) { | 
|                 token = WebUtil.getRequest().getParameter("token"); | 
|             } | 
|         } | 
|         return token; | 
|     } | 
|   | 
|   | 
| } |