Browse Source

增加Redis记录当前登录用户信息功能

loovi 1 year ago
parent
commit
1d13109854

+ 1 - 0
sp-service/sp-admin/src/main/java/com/pj/project4sp/admin/SpAdmin.java

@@ -3,6 +3,7 @@ package com.pj.project4sp.admin;
 import java.io.Serializable;
 import java.util.Date;
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonIgnore;
 
 import lombok.Data;

+ 39 - 0
sp-service/sp-admin/src/main/java/com/pj/project4sp/admin/SpLoginInfo.java

@@ -0,0 +1,39 @@
+package com.pj.project4sp.admin;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class SpLoginInfo {
+    /** id,--主键、自增 */
+    private String id;
+    /** admin名称 */
+    private String name;
+    /** 手机号 */
+    private String phone;
+
+    /** 所属角色id */
+    private Integer roleId;
+
+    /** 账号状态(1=正常, 2=禁用) */
+    private Integer status;
+
+    /** 上次登陆时间 */
+
+    private Date loginTime;
+
+    /** 上次登陆IP */
+    private String loginIp;
+
+    /** 登陆次数 */
+    private Integer loginCount;
+
+}

+ 4 - 0
sp-service/sp-admin/src/main/java/com/pj/project4sp/admin4login/SpAccAdminController.java

@@ -3,7 +3,9 @@ package com.pj.project4sp.admin4login;
 import javax.servlet.http.HttpServletRequest;
 
 import cn.dev33.satoken.annotation.SaCheckPermission;
+import cn.hutool.json.JSONUtil;
 import com.pj.current.satoken.AuthConst;
+import com.pj.utils.cache.RedisUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -49,6 +51,8 @@ public class SpAccAdminController {
 	/** 退出登录  */
 	@RequestMapping("doExit")
 	AjaxJson doExit() {
+		// 退出时删除登录信息
+		RedisUtil.del("loginInfo:");
 		StpUtil.logout();
 		return AjaxJson.getSuccess();
 	}

+ 9 - 1
sp-service/sp-admin/src/main/java/com/pj/project4sp/admin4login/SpAccAdminService.java

@@ -4,7 +4,11 @@ package com.pj.project4sp.admin4login;
 import java.util.Date;
 import java.util.List;
 
+import cn.hutool.json.JSONUtil;
 import com.pj.current.satoken.StpUserUtil;
+import com.pj.project4sp.admin.SpLoginInfo;
+import com.pj.utils.cache.RedisUtil;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
@@ -90,16 +94,20 @@ public class SpAccAdminService {
             return AjaxJson.getError("此账号已被禁用,如有疑问,请联系管理员");
         }
 
-        // =========== 至此, 已登录成功 ============ 
+        // =========== 至此, 已登录成功 ============
         successLogin(admin);
         StpUtil.login(admin.getId());
         // 组织返回参数
+        SpLoginInfo spLoginInfo = new SpLoginInfo();
+        BeanUtils.copyProperties(admin,spLoginInfo);
         SoMap map = new SoMap();
         map.put("admin", admin);
         List<String> per_list = spRolePermissionService.getPcodeByRid2(admin.getRoleId());
         map.put("per_list", per_list);
         map.put("tokenInfo", StpUtil.getTokenInfo());
         StpUserUtil.cachePerList(per_list);
+        // 记录登录信息至redis
+        RedisUtil.set("loginInfo:", JSONUtil.toJsonStr(spLoginInfo));
         return AjaxJson.getSuccessData(map);
     }