| package com.xcong.excoin.system.controller; | 
|   | 
| import com.xcong.excoin.common.annotation.Limit; | 
| import com.xcong.excoin.common.controller.BaseController; | 
| import com.xcong.excoin.common.entity.FebsResponse; | 
| import com.xcong.excoin.common.exception.FebsException; | 
| import com.xcong.excoin.common.service.ValidateCodeService; | 
| import com.xcong.excoin.common.utils.Md5Util; | 
| import com.xcong.excoin.monitor.entity.LoginLog; | 
| import com.xcong.excoin.monitor.service.ILoginLogService; | 
| import com.xcong.excoin.system.entity.User; | 
| import com.xcong.excoin.system.service.IUserService; | 
| import lombok.RequiredArgsConstructor; | 
| import org.apache.shiro.authc.UsernamePasswordToken; | 
| import org.springframework.validation.annotation.Validated; | 
| import org.springframework.web.bind.annotation.GetMapping; | 
| import org.springframework.web.bind.annotation.PathVariable; | 
| import org.springframework.web.bind.annotation.PostMapping; | 
| import org.springframework.web.bind.annotation.RestController; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import javax.servlet.http.HttpServletResponse; | 
| import javax.servlet.http.HttpSession; | 
| import javax.validation.constraints.NotBlank; | 
| import java.io.IOException; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * @author MrBird | 
|  */ | 
| @Validated | 
| @RestController | 
| @RequiredArgsConstructor | 
| public class LoginController extends BaseController { | 
|   | 
|     private final IUserService userService; | 
|     private final ValidateCodeService validateCodeService; | 
|     private final ILoginLogService loginLogService; | 
|   | 
|     @PostMapping("login") | 
|     @Limit(key = "login", period = 60, count = 10, name = "登录接口", prefix = "limit") | 
|     public FebsResponse login( | 
|             @NotBlank(message = "{required}") String username, | 
|             @NotBlank(message = "{required}") String password, | 
|             @NotBlank(message = "{required}") String verifyCode, | 
|             boolean rememberMe, HttpServletRequest request) throws FebsException { | 
|         HttpSession session = request.getSession(); | 
|         validateCodeService.check(session.getId(), verifyCode); | 
|         password = Md5Util.encrypt(username.toLowerCase(), password); | 
|         UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe); | 
|         super.login(token); | 
|         // 保存登录日志 | 
|         LoginLog loginLog = new LoginLog(); | 
|         loginLog.setUsername(username); | 
|         loginLog.setSystemBrowserInfo(); | 
|         this.loginLogService.saveLoginLog(loginLog); | 
|   | 
|         return new FebsResponse().success(); | 
|     } | 
|   | 
|     @PostMapping("regist") | 
|     public FebsResponse regist( | 
|             @NotBlank(message = "{required}") String username, | 
|             @NotBlank(message = "{required}") String password) throws FebsException { | 
|         User user = userService.findByName(username); | 
|         if (user != null) { | 
|             throw new FebsException("该用户名已存在"); | 
|         } | 
|         this.userService.regist(username, password); | 
|         return new FebsResponse().success(); | 
|     } | 
|   | 
|     @GetMapping("index/{username}") | 
|     public FebsResponse index(@NotBlank(message = "{required}") @PathVariable String username) { | 
|         // 更新登录时间 | 
|         this.userService.updateLoginTime(username); | 
|         Map<String, Object> data = new HashMap<>(5); | 
|         // 获取系统访问记录 | 
|         Long totalVisitCount = this.loginLogService.findTotalVisitCount(); | 
|         data.put("totalVisitCount", totalVisitCount); | 
|         Long todayVisitCount = this.loginLogService.findTodayVisitCount(); | 
|         data.put("todayVisitCount", todayVisitCount); | 
|         Long todayIp = this.loginLogService.findTodayIp(); | 
|         data.put("todayIp", todayIp); | 
|         // 获取近期系统访问记录 | 
|         List<Map<String, Object>> lastSevenVisitCount = this.loginLogService.findLastSevenDaysVisitCount(null); | 
|         data.put("lastSevenVisitCount", lastSevenVisitCount); | 
|         User param = new User(); | 
|         param.setUsername(username); | 
|         List<Map<String, Object>> lastSevenUserVisitCount = this.loginLogService.findLastSevenDaysVisitCount(param); | 
|         data.put("lastSevenUserVisitCount", lastSevenUserVisitCount); | 
|         return new FebsResponse().success().data(data); | 
|     } | 
|   | 
|     @GetMapping("images/captcha") | 
|     @Limit(key = "get_captcha", period = 60, count = 10, name = "获取验证码", prefix = "limit") | 
|     public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException, FebsException { | 
|         validateCodeService.create(request, response); | 
|     } | 
| } |