mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-08 02:04:04 +08:00
管理登录次数+登录时间的记录
This commit is contained in:
parent
aed4c60296
commit
9a9226baa3
@ -2,6 +2,7 @@ package xyz.playedu.api.controller.backend;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -9,16 +10,20 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import xyz.playedu.api.constant.SystemConstant;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.event.AdminUserLoginEvent;
|
||||
import xyz.playedu.api.exception.JwtLogoutException;
|
||||
import xyz.playedu.api.middleware.ImageCaptchaCheckMiddleware;
|
||||
import xyz.playedu.api.request.LoginRequest;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.service.JWTService;
|
||||
import xyz.playedu.api.types.JWTPayload;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.types.JwtToken;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
import xyz.playedu.api.util.IpUtil;
|
||||
import xyz.playedu.api.util.RequestUtil;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Slf4j
|
||||
@ -32,6 +37,9 @@ public class LoginController {
|
||||
@Autowired
|
||||
private JWTService jwtService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@PostMapping("/login")
|
||||
@ImageCaptchaCheckMiddleware
|
||||
public JsonResponse login(@RequestBody @Validated LoginRequest loginRequest) {
|
||||
@ -54,6 +62,8 @@ public class LoginController {
|
||||
data.put("token", token.getToken());
|
||||
data.put("expire", token.getExpire());
|
||||
|
||||
applicationContext.publishEvent(new AdminUserLoginEvent(this, adminUser.getId(), new Date(), token.getToken(), IpUtil.getHostIp(), adminUser.getLoginTimes()));
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
|
31
src/main/java/xyz/playedu/api/event/AdminUserLoginEvent.java
Normal file
31
src/main/java/xyz/playedu/api/event/AdminUserLoginEvent.java
Normal file
@ -0,0 +1,31 @@
|
||||
package xyz.playedu.api.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class AdminUserLoginEvent extends ApplicationEvent {
|
||||
|
||||
private Integer adminId;
|
||||
|
||||
private Date loginAt;
|
||||
|
||||
private String token;
|
||||
|
||||
private String ip;
|
||||
|
||||
private Integer loginTimes;
|
||||
|
||||
public AdminUserLoginEvent(Object source, Integer adminId, Date loginAt, String token, String ip, Integer loginTimes) {
|
||||
super(source);
|
||||
this.adminId = adminId;
|
||||
this.loginAt = loginAt;
|
||||
this.token = token;
|
||||
this.ip = ip;
|
||||
this.loginTimes = loginTimes;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package xyz.playedu.api.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xyz.playedu.api.event.AdminUserLoginEvent;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AdminUserLoginListener {
|
||||
|
||||
@Autowired
|
||||
private AdminUserService adminUserService;
|
||||
|
||||
@Order(1)
|
||||
@EventListener
|
||||
public void updateLoginAtAndTimes(AdminUserLoginEvent event) {
|
||||
adminUserService.updateLoginTimesAndLoginAt(event.getAdminId(), event.getLoginAt(), event.getLoginTimes() + 1);
|
||||
}
|
||||
|
||||
@Order(10)
|
||||
@EventListener
|
||||
public void recordLoginIp(AdminUserLoginEvent event) {
|
||||
log.info("我执行了:recordLoginIp");
|
||||
}
|
||||
|
||||
}
|
@ -4,15 +4,16 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Mapper
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Mapper
|
||||
* @createDate 2023-02-11 10:58:52
|
||||
* @Entity xyz.playedu.api.domain.AdminUser
|
||||
*/
|
||||
public interface AdminUserMapper extends BaseMapper<AdminUser> {
|
||||
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Mapper
|
||||
* @createDate 2023-02-11 10:58:52
|
||||
* @Entity xyz.playedu.api.domain.AdminUser
|
||||
*/ public interface AdminUserMapper extends BaseMapper<AdminUser> {
|
||||
Integer updateLoginAtAndLoginTimes(Integer id, Date loginAt, Integer loginTimes);
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,6 +5,8 @@ import xyz.playedu.api.domain.AdminUser;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Service
|
||||
@ -16,4 +18,6 @@ public interface AdminUserService extends IService<AdminUser> {
|
||||
AdminUser findByEmail(String email);
|
||||
|
||||
AdminUser findById(Integer id);
|
||||
|
||||
Integer updateLoginTimesAndLoginAt(Integer id, Date loginAt, Integer loginTimes);
|
||||
}
|
||||
|
@ -11,14 +11,10 @@ import xyz.playedu.api.mapper.AdminUserMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Service实现
|
||||
* @createDate 2023-02-11 10:58:52
|
||||
*/
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser>
|
||||
implements AdminUserService {
|
||||
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {
|
||||
|
||||
public PaginationResult<AdminUser> paginate(int page, int size, Wrapper<AdminUser> queryWrapper) {
|
||||
IPage<AdminUser> userPage = new Page<>(page, size);
|
||||
@ -43,6 +39,11 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
|
||||
queryWrapper.eq("id", id);
|
||||
return this.getBaseMapper().selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer updateLoginTimesAndLoginAt(Integer id, Date loginAt, Integer loginTimes) {
|
||||
return this.getBaseMapper().updateLoginAtAndLoginTimes(id, loginAt, loginTimes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,23 +5,32 @@
|
||||
<mapper namespace="xyz.playedu.api.mapper.AdminUserMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.AdminUser">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="name" column="name" jdbcType="VARCHAR"/>
|
||||
<result property="email" column="email" jdbcType="VARCHAR"/>
|
||||
<result property="password" column="password" jdbcType="VARCHAR"/>
|
||||
<result property="salt" column="salt" jdbcType="VARCHAR"/>
|
||||
<result property="loginIp" column="login_ip" jdbcType="VARCHAR"/>
|
||||
<result property="loginAt" column="login_at" jdbcType="TIMESTAMP"/>
|
||||
<result property="isBanLogin" column="is_ban_login" jdbcType="TINYINT"/>
|
||||
<result property="loginTimes" column="login_times" jdbcType="INTEGER"/>
|
||||
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
|
||||
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="name" column="name" jdbcType="VARCHAR"/>
|
||||
<result property="email" column="email" jdbcType="VARCHAR"/>
|
||||
<result property="password" column="password" jdbcType="VARCHAR"/>
|
||||
<result property="salt" column="salt" jdbcType="VARCHAR"/>
|
||||
<result property="loginIp" column="login_ip" jdbcType="VARCHAR"/>
|
||||
<result property="loginAt" column="login_at" jdbcType="TIMESTAMP"/>
|
||||
<result property="isBanLogin" column="is_ban_login" jdbcType="TINYINT"/>
|
||||
<result property="loginTimes" column="login_times" jdbcType="INTEGER"/>
|
||||
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
|
||||
<result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,name,email,
|
||||
password,salt,login_ip,
|
||||
login_at,is_ban_login,login_times,
|
||||
id
|
||||
,name,email,
|
||||
password,salt,login_ip,
|
||||
login_at,is_ban_login,login_times,
|
||||
created_at,updated_at
|
||||
</sql>
|
||||
|
||||
<update id="updateLoginAtAndLoginTimes">
|
||||
UPDATE `admin_users`
|
||||
SET `login_at` = #{loginAt},
|
||||
`login_times`= #{loginTimes}
|
||||
where id = #{id}
|
||||
and `login_times` = ${loginTimes - 1} limit 1
|
||||
</update>
|
||||
</mapper>
|
||||
|
Loading…
x
Reference in New Issue
Block a user