| | |
| | | package com.terra.common.service; |
| | | |
| | | import com.terra.common.entity.all.RedisCacheKey; |
| | | import com.terra.common.entity.all.SettingData; |
| | | import com.terra.common.entity.lf.TokenPo; |
| | | import com.terra.common.entity.lf.UserPo; |
| | | import com.terra.common.helper.StringHelper; |
| | |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 令牌帮助类 |
| | | * 通用服务类 |
| | | * @author WWW |
| | | */ |
| | | @Component |
| | |
| | | } |
| | | |
| | | public UserPo getUserByToken(String token) { |
| | | if (StringHelper.isNull(token)) { |
| | | return null; |
| | | } |
| | | if (StringHelper.isNull(token)) return null; |
| | | |
| | | // redis |
| | | String userKey = RedisCacheKey.signUserKey(token); |
| | | Object obj = redisService.get(userKey); |
| | | if (obj instanceof UserPo) { |
| | | return (UserPo) obj; |
| | | } |
| | | TokenPo tokenPo = getTokenPo(token); |
| | | if (null == tokenPo) return null; |
| | | |
| | | // db |
| | | UserPo po = mapper.selectByToken(token); |
| | | if (null != po) { |
| | | getEntityByToken(token); |
| | | } |
| | | |
| | | return po; |
| | | return getUser(token); |
| | | } |
| | | |
| | | public TokenPo getEntityByToken(String token) { |
| | | public TokenPo getTokenPo(String token) { |
| | | String tokenKey = RedisCacheKey.signTokenKey(token); |
| | | |
| | | // redis |
| | |
| | | } |
| | | |
| | | // db |
| | | TokenPo te = mapper.selectOneByToken(token); |
| | | if (null != te) { |
| | | long min = StringHelper.getMinuteDifference(te.getExpire()); |
| | | if (min > 0) { |
| | | redisService.put(tokenKey, te, min, TimeUnit.MINUTES); |
| | | } |
| | | TokenPo po = mapper.selectOneByToken(token); |
| | | if (null != po) { |
| | | long min = StringHelper.getMinuteDifference(po.getExpire()); |
| | | redisService.put(tokenKey, po, min, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return te; |
| | | return po; |
| | | } |
| | | |
| | | public static String getCurrentUserName() { |
| | | return null; |
| | | public UserPo getUser(String token) { |
| | | String userKey = RedisCacheKey.signUserKey(token); |
| | | |
| | | // redis |
| | | Object obj = redisService.get(userKey); |
| | | if (obj instanceof UserPo) { |
| | | return (UserPo) obj; |
| | | } |
| | | |
| | | // db |
| | | UserPo po = mapper.selectByToken(token); |
| | | if (null != po) { |
| | | redisService.put(userKey, po, SettingData.CACHE_EXPIRE, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | return po; |
| | | } |
| | | } |