Browse Source

用户登录登出

master
sige 2 years ago
parent
commit
426da35ee3
  1. 52
      src/main/java/com/dreamworks/boditech/controller/UserController.java
  2. 23
      src/main/java/com/dreamworks/boditech/entity/ActionLogEntry.java
  3. 15
      src/main/java/com/dreamworks/boditech/mapper/ActionLogMapper.java
  4. 38
      src/main/java/com/dreamworks/boditech/service/ActionLogService.java
  5. 14
      src/main/java/com/dreamworks/boditech/service/UserService.java
  6. 17
      src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java

52
src/main/java/com/dreamworks/boditech/controller/UserController.java

@ -16,25 +16,53 @@ public class UserController extends BaseController {
@ResponseBody
@PostMapping("/api/user/login")
public ApiResponse login(@RequestBody ParamUserLogin param ) {
try {
User user = this.userService.login(param);
return this.success(user);
} catch ( RuntimeException e ) {
return this.error(e.getMessage());
}
User user = this.userService.login(param);
return this.success(user);
}
@ResponseBody
@PostMapping("/api/user/logout")
public ApiResponse logout() {
try {
this.userService.logout();
return this.success();
} catch ( RuntimeException e ) {
return this.error(e.getMessage());
}
this.userService.logout();
return this.success();
}
// @TODO : 下面这些接口还没测试
@ResponseBody
@PostMapping("/api/user/create")
public ApiResponse create( String name, String pin ) {

23
src/main/java/com/dreamworks/boditech/entity/ActionLogEntry.java

@ -0,0 +1,23 @@
package com.dreamworks.boditech.entity;
import com.dreamworks.boditech.mapper.ActionLogMapper;
public class ActionLogEntry {
public Integer id;
public Integer userId;
public String action;
public String status;
public String params;
public String result;
public String startedAt;
public String finishedAt;
// action log mapper
private ActionLogMapper mapper;
// constructor
public ActionLogEntry(ActionLogMapper mapper) {
this.mapper = mapper;
}
public void done() {}
public void error() {}
}

15
src/main/java/com/dreamworks/boditech/mapper/ActionLogMapper.java

@ -0,0 +1,15 @@
package com.dreamworks.boditech.mapper;
import com.dreamworks.boditech.entity.ActionLogEntry;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
@Mapper
public interface ActionLogMapper {
@Insert(
"INSERT INTO bdt_action_log (userId, action, params, startedAt) VALUES " +
"(#{userId}, #{action}, #{params}, #{startedAt})"
)
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
void insert(ActionLogEntry entry);
}

38
src/main/java/com/dreamworks/boditech/service/ActionLogService.java

@ -0,0 +1,38 @@
package com.dreamworks.boditech.service;
import com.dreamworks.boditech.entity.ActionLogEntry;
import com.dreamworks.boditech.mapper.ActionLogMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@Service
public class ActionLogService {
@Resource
private ActionLogMapper actionLogMapper;
// user id
private Integer userId = 0;
// set user id
public void setUserId( Integer userId ) {
this.userId = userId;
}
// log action start
public void log( String name, Object ... params ) {
ActionLogEntry entry = new ActionLogEntry(this.actionLogMapper);
entry.userId = this.userId;
entry.action = name;
// join params to string
List<String> paramList = new ArrayList<>();
for ( Object param : params ) {
paramList.add(param.toString());
}
entry.params = String.join(",", paramList);
entry.startedAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
this.actionLogMapper.insert(entry);
}
}

14
src/main/java/com/dreamworks/boditech/service/UserService.java

@ -6,30 +6,36 @@ import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
@Service
public class UserService {
private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
@Resource
private UserMapper userMapper;
@Resource
private ActionLogService actionLog;
// current user
private User curUser = null;
// login
public User login(ParamUserLogin param) {
User user = userMapper.findByAccount(param.account);
if ( null == user || !user.pin.equals(param.pin) ) {
String hashPin = DigestUtils.md5DigestAsHex(param.pin.getBytes());
if ( null == user || !user.pin.equals(hashPin) ) {
throw new RuntimeException("USER_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE");
}
this.curUser = user;
this.curUser.pin = "****";
LOG.info("user login success, user id : {}", this.curUser.id);
this.actionLog.setUserId(user.id);
this.actionLog.log("user.login", param.account);
return user;
}
// logout
public void logout() {
this.actionLog.log("user.logout");
this.actionLog.setUserId(0);
this.curUser = null;
}

17
src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java

@ -0,0 +1,17 @@
package com.dreamworks.boditech.utils;
import com.dreamworks.boditech.controller.entity.ApiResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(value = RuntimeException.class)
@ResponseBody
public ApiResponse handleRuntimeException(RuntimeException e ) {
ApiResponse response = new ApiResponse();
response.success = false;
response.message = e.getMessage();
response.data = null;
return response;
}
}
Loading…
Cancel
Save