From c15739adf58aa87002516f151d6314b8caecbb29 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 30 九月 2022 12:39:47 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/data/TokenService.java |  206 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 195 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/lf/server/service/data/TokenService.java b/src/main/java/com/lf/server/service/data/TokenService.java
index 067fcb7..66436ca 100644
--- a/src/main/java/com/lf/server/service/data/TokenService.java
+++ b/src/main/java/com/lf/server/service/data/TokenService.java
@@ -1,14 +1,21 @@
 package com.lf.server.service.data;
 
+import com.lf.server.entity.all.StaticData;
 import com.lf.server.entity.data.LoginEntity;
 import com.lf.server.entity.data.TokenEntity;
+import com.lf.server.entity.data.UsersEntity;
+import com.lf.server.entity.sys.RedisCacheKey;
+import com.lf.server.helper.StringHelper;
 import com.lf.server.helper.WebHelper;
 import com.lf.server.mapper.data.TokenMapper;
+import com.lf.server.service.sys.RedisService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 浠ょ墝琛�
@@ -18,9 +25,17 @@
 
 @Service
 public class TokenService implements TokenMapper {
+    @Autowired
+    private RedisService redisService;
 
     @Autowired
     TokenMapper tokenMapper;
+
+    @Autowired
+    UsersService usersService;
+
+    @Autowired
+    LoginService loginService;
 
     @Override
     public Integer selectCount(String token) {
@@ -29,7 +44,22 @@
 
     @Override
     public List<TokenEntity> selectByPage(String token, Integer limit, Integer offset) {
-        return tokenMapper.selectByPage(token,limit,offset);
+        return tokenMapper.selectByPage(token, limit, offset);
+    }
+
+    @Override
+    public TokenEntity selectToken(int id) {
+        return tokenMapper.selectToken(id);
+    }
+
+    @Override
+    public TokenEntity selectOneByToken(String token) {
+        return tokenMapper.selectOneByToken(token);
+    }
+
+    @Override
+    public List<TokenEntity> selectTokenAll() {
+        return tokenMapper.selectTokenAll();
     }
 
     @Override
@@ -58,20 +88,24 @@
     }
 
     @Override
-    public TokenEntity selectToken(int id) {
-        return tokenMapper.selectToken(id);
+    public Integer updateTokenExpire(TokenEntity tokenEntity) {
+        return tokenMapper.updateTokenExpire(tokenEntity);
     }
 
-    @Override
-    public List<TokenEntity> selectTokenAll() {
-        return tokenMapper.selectTokenAll();
-    }
-
+    /**
+     * 鑾峰彇鏂扮殑浠ょ墝瀹炰綋绫�
+     *
+     * @param userid
+     * @param req
+     * @return
+     */
     public TokenEntity getNewToken(int userid, HttpServletRequest req) {
-        TokenEntity te=new TokenEntity();
+        int duration = 240;
+
+        TokenEntity te = new TokenEntity();
         te.setToken(WebHelper.getGuid());
-        te.setDuration(240);
-        te.setExpire(WebHelper.getTimestamp(240));
+        te.setDuration(duration);
+        te.setExpire(WebHelper.getTimestamp(duration));
         te.setType(0);
         te.setIp(WebHelper.getIpAddress(req));
         te.setCreateUser(userid);
@@ -79,4 +113,154 @@
 
         return te;
     }
+
+    /**
+     * 鏄�/鍚︾櫥褰�
+     *
+     * @param req
+     * @param res
+     * @return
+     */
+    public Boolean isLogin(HttpServletRequest req, HttpServletResponse res) {
+        String token = WebHelper.getToken(req);
+        if (StringHelper.isNull(token)) {
+            return false;
+        }
+
+        // redis
+        String tokenKey = RedisCacheKey.signTokenKey(token);
+        if (redisService.hasKey(tokenKey)) {
+            return true;
+        }
+
+        // db
+        TokenEntity te = selectOneByToken(token);
+        if (te != null) {
+            redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES);
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * 鐧诲嚭
+     *
+     * @param token
+     * @param req
+     * @return
+     */
+    public Boolean logout(String token, HttpServletRequest req) {
+        TokenEntity te = getEntityByToken(token);
+        if (te == null) {
+            return false;
+        }
+
+        // 娓呴櫎Cookie
+        WebHelper.deleteCookie(StaticData.TOKEN_COOKIE_KEY, req);
+
+        // 娓呴櫎缂撳瓨
+        String tokenKey = RedisCacheKey.signTokenKey(token);
+        redisService.delete(tokenKey);
+
+        // 鑾峰彇褰撳墠鐢ㄦ埛
+        UsersEntity ue = getCurrentUser(req);
+        if (te == null) {
+            return false;
+        }
+
+        // db锛岃缃护鐗岃繃鏈�
+        te.setUpdateUser(ue.getId());
+        Integer rows = updateTokenExpire(te);
+        if (rows == 0) {
+            return false;
+        }
+
+        // 鍐欐棩蹇�
+        LoginEntity le = loginService.getNewLogin(ue.getId(), req);
+        le.setType(3);
+
+        rows = loginService.insertLogin(le);
+
+        return rows > 0;
+    }
+
+    /**
+     * 鏍规嵁浠ょ墝鑾峰彇瀹炰綋
+     *
+     * @param token
+     * @return
+     */
+    public TokenEntity getEntityByToken(String token) {
+        if (StringHelper.isNull(token)) {
+            return null;
+        }
+
+        String tokenKey = RedisCacheKey.signTokenKey(token);
+
+        // redis
+        Object obj = redisService.get(tokenKey);
+        if (obj != null && obj instanceof TokenEntity) {
+            return (TokenEntity) obj;
+        }
+
+        // db
+        TokenEntity te = selectOneByToken(token);
+        if (te != null) {
+            redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES);
+        }
+
+        return te;
+    }
+
+    /**
+     * 淇濆瓨token
+     *
+     * @param te
+     * @param req
+     * @param res
+     */
+    public void saveToken(UsersEntity ue, TokenEntity te, HttpServletRequest req, HttpServletResponse res) {
+        // 淇濆瓨鑷矯ookie
+        WebHelper.saveToken2Cookie(te.getToken(), req, res);
+
+        // 浠ょ墝淇濆瓨鑷砇edis
+        String tokenKey = RedisCacheKey.signTokenKey(te.getToken());
+        redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES);
+
+        String userKey = RedisCacheKey.signUserKey(te.getToken());
+        redisService.put(userKey, ue, te.getDuration(), TimeUnit.MINUTES);
+    }
+
+    /**
+     * 鑾峰彇褰撳墠鐢ㄦ埛
+     *
+     * @param req
+     * @return
+     */
+    public UsersEntity getCurrentUser(HttpServletRequest req) {
+        String token = WebHelper.getToken(req);
+        if (StringHelper.isEmpty(token)) {
+            return null;
+        }
+
+        String userKey = RedisCacheKey.signUserKey(token);
+
+        // redis
+        Object obj = redisService.get(userKey);
+        if (obj != null && obj instanceof UsersEntity) {
+            return (UsersEntity) obj;
+        }
+
+        // db
+        UsersEntity ue = usersService.selectByToken(token);
+        if (ue != null) {
+            TokenEntity te = getEntityByToken(token);
+            if (te != null) {
+                redisService.put(userKey, ue, te.getDuration(), TimeUnit.MINUTES);
+            }
+        }
+
+        return ue;
+    }
 }

--
Gitblit v1.9.3