mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-28 08:17:30 +08:00
登录信息修改的优化
This commit is contained in:
parent
9a9226baa3
commit
635783dd49
@ -62,7 +62,7 @@ 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()));
|
||||
applicationContext.publishEvent(new AdminUserLoginEvent(this, adminUser.getId(), new Date(), token.getToken(), IpUtil.getIpAddress(), adminUser.getLoginTimes()));
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
@ -5,8 +5,10 @@ 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.domain.AdminUser;
|
||||
import xyz.playedu.api.event.AdminUserLoginEvent;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.util.IpUtil;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@ -17,14 +19,23 @@ public class AdminUserLoginListener {
|
||||
|
||||
@Order(1)
|
||||
@EventListener
|
||||
public void updateLoginAtAndTimes(AdminUserLoginEvent event) {
|
||||
adminUserService.updateLoginTimesAndLoginAt(event.getAdminId(), event.getLoginAt(), event.getLoginTimes() + 1);
|
||||
public void updateLoginInfo(AdminUserLoginEvent event) {
|
||||
AdminUser adminUser = new AdminUser();
|
||||
|
||||
adminUser.setId(event.getAdminId());
|
||||
adminUser.setLoginAt(event.getLoginAt());
|
||||
adminUser.setLoginTimes(event.getLoginTimes() + 1);
|
||||
adminUser.setLoginIp(event.getIp());
|
||||
|
||||
adminUserService.updateById(adminUser);
|
||||
}
|
||||
|
||||
@Order(10)
|
||||
@EventListener
|
||||
public void recordLoginIp(AdminUserLoginEvent event) {
|
||||
log.info("我执行了:recordLoginIp");
|
||||
String area = IpUtil.getRealAddressByIP(event.getIp());
|
||||
log.info("地区:" + area);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -4,8 +4,6 @@ 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
|
||||
@ -13,7 +11,6 @@ import java.util.Date;
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -18,6 +18,4 @@ public interface AdminUserService extends IService<AdminUser> {
|
||||
AdminUser findByEmail(String email);
|
||||
|
||||
AdminUser findById(Integer id);
|
||||
|
||||
Integer updateLoginTimesAndLoginAt(Integer id, Date loginAt, Integer loginTimes);
|
||||
}
|
||||
|
@ -39,11 +39,6 @@ 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,6 +5,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@ -48,16 +49,15 @@ public class IpUtil {
|
||||
* 根据IP获取所在地址
|
||||
*
|
||||
* @param ip Ip地址
|
||||
* @return String (广州省 广州市)
|
||||
* @return String (广州省-广州市)
|
||||
* @author fzr
|
||||
*/
|
||||
public static String getRealAddressByIP(String ip) {
|
||||
String IP_URL = "https://whois.pconline.com.cn/ipJson.jsp";
|
||||
String UNKNOWN = "XX XX";
|
||||
String UNKNOWN = "未知";
|
||||
|
||||
// 内网不查询
|
||||
if (IpUtil.internalIp(ip)) {
|
||||
return "内网IP";
|
||||
return "内网";
|
||||
}
|
||||
|
||||
try {
|
||||
@ -69,7 +69,7 @@ public class IpUtil {
|
||||
JSONObject obj = JSONObject.parseObject(rspStr);
|
||||
String region = obj.getString("pro");
|
||||
String city = obj.getString("city");
|
||||
return String.format("%s %s", region, city);
|
||||
return String.format("%s-%s", region, city);
|
||||
} catch (Exception e) {
|
||||
log.error("获取地理位置异常 {}", ip);
|
||||
}
|
||||
|
@ -25,12 +25,4 @@
|
||||
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