1.自定义事件
a.创建包event
b.创建类LoginSuccessEvent
c.编写类的内容
package com.huinongtx.power.consumer.event;
import com.huinongtx.power.consumer.request.account.LoginForm;
import com.huinongtx.power.consumer.vo.result.ResultVO;
import com.huinongtx.power.provider.dto.account.AccountDTO;
import lombok.Data;
import org.springframework.context.ApplicationEvent;
/**
* @ClassName LoginSuccessEvent
* @Description 登录成功事件
* @Author yunshuodeng
* @Date 2019-04-25 10:43
* @Version 1.0
**/
@Data
public class LoginSuccessEvent extends ApplicationEvent {
private ResultVO resultVO;
private AccountDTO accountDTO;
private LoginForm loginForm;
public LoginSuccessEvent(Object source) {
super(source);
}
public LoginSuccessEvent(Object source,ResultVO resultVO,AccountDTO accountDTO,LoginForm loginForm) {
super(source);
this.resultVO = resultVO;
this.accountDTO = accountDTO;
this.loginForm = loginForm;
}
}
2.自定义监听
a.创建包listener
b.创建类LoginSuccessListener
c.编写类的实现
package com.huinongtx.power.consumer.listener;
import com.huinongtx.power.consumer.event.LoginSuccessEvent;
import com.huinongtx.power.consumer.repository.AccountRepository;
import com.huinongtx.power.consumer.request.account.LoginForm;
import com.huinongtx.power.consumer.util.IpUtil;
import com.huinongtx.power.provider.dto.account.AccountDTO;
import com.huinongtx.power.provider.dto.account.ModifyAccountLoginInfoDTO;
import com.huinongtx.power.provider.dto.result.ResultDTO;
import com.huinongtx.power.provider.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
/**
* @ClassName LoginSuccessListener
* @Description TODO
* @Author yunshuodeng
* @Date 2019-04-25 10:49
* @Version 1.0
**/
@Async
@Component
public class LoginSuccessListener implements ApplicationListener<LoginSuccessEvent> {
@Autowired
private HttpServletRequest request;
// 注入账户持久层
@Autowired
private AccountRepository accountRepository;
public void onApplicationEvent(LoginSuccessEvent loginSuccessEvent) {
if (loginSuccessEvent.getResultVO().getCode() == 0){
// System.out.println("执行修改登录信息操作");
// 修改登录相关信息
try {
modifyLoginInfo(request,loginSuccessEvent.getAccountDTO(),loginSuccessEvent.getLoginForm());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 修改登录信息
* @param request
* @param accountDTO
* @param loginForm
* @throws Exception
*/
private void modifyLoginInfo(HttpServletRequest request, AccountDTO accountDTO, LoginForm loginForm) throws Exception {
// 获取客户端登录IP
String ip = IpUtil.getIpAddr(request);
// 修改登录相关信息
ModifyAccountLoginInfoDTO modifyAccountLoginInfoDTO = new ModifyAccountLoginInfoDTO();
modifyAccountLoginInfoDTO.setId(accountDTO.getId());
modifyAccountLoginInfoDTO.setDeviceNo(loginForm.getDeviceNo());
modifyAccountLoginInfoDTO.setPlatform(loginForm.getPlatform());
modifyAccountLoginInfoDTO.setDevicePlatform(loginForm.getDevicePlatform());
modifyAccountLoginInfoDTO.setLoginTime(TimeUtil.getCurrentTimeMillis());
modifyAccountLoginInfoDTO.setLastLoginTime(TimeUtil.getCurrentTimeMillis());
modifyAccountLoginInfoDTO.setIp(ip);
Long count = StringUtils.isEmpty(accountDTO.getCount()) ? 0 : accountDTO.getCount();
modifyAccountLoginInfoDTO.setCount(count+1);
ResultDTO resultDTO = accountRepository.modifyAccountInfoByCondition(modifyAccountLoginInfoDTO);
System.out.println(resultDTO.getState());
}
}
3.发布事件
a.在service中注入spring 上下文
@Autowired
private ApplicationContext applicationContext;
b.在相应的业务方法中发布事件
// 代码省略
// 发布事件
applicationContext.publishEvent(new LoginSuccessEvent(this,resultVO,accountDTO,loginForm));
// 代码省略
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。