|
@ -5,10 +5,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
import com.qyft.gd.system.common.base.BasePageQuery; |
|
|
import com.qyft.gd.system.common.base.BasePageQuery; |
|
|
import com.qyft.gd.system.common.result.PageResult; |
|
|
import com.qyft.gd.system.common.result.PageResult; |
|
|
import com.qyft.gd.system.common.result.Result; |
|
|
import com.qyft.gd.system.common.result.Result; |
|
|
|
|
|
import com.qyft.gd.system.common.result.ResultCode; |
|
|
|
|
|
import com.qyft.gd.system.common.utils.JwtUtil; |
|
|
import com.qyft.gd.system.model.entity.User; |
|
|
import com.qyft.gd.system.model.entity.User; |
|
|
import com.qyft.gd.system.service.UserService; |
|
|
import com.qyft.gd.system.service.UserService; |
|
|
|
|
|
import io.jsonwebtoken.Claims; |
|
|
import io.swagger.v3.oas.annotations.Operation; |
|
|
import io.swagger.v3.oas.annotations.Operation; |
|
|
import io.swagger.v3.oas.annotations.tags.Tag; |
|
|
import io.swagger.v3.oas.annotations.tags.Tag; |
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.web.bind.annotation.*; |
|
|
import org.springframework.web.bind.annotation.*; |
|
@ -31,13 +35,38 @@ public class UserController { |
|
|
@Operation(summary = "添加新用户") |
|
|
@Operation(summary = "添加新用户") |
|
|
@PostMapping("/") |
|
|
@PostMapping("/") |
|
|
public Result<String> addUser(@RequestBody User user) { |
|
|
public Result<String> addUser(@RequestBody User user) { |
|
|
boolean isSuccess = userService.addUser(user); |
|
|
|
|
|
if (isSuccess) { |
|
|
|
|
|
return Result.success(); |
|
|
|
|
|
|
|
|
User existingUser = userService.findByUsername(user.getUsername()); |
|
|
|
|
|
if(existingUser == null) { |
|
|
|
|
|
boolean isSuccess = userService.addUser(user); |
|
|
|
|
|
if (isSuccess) { |
|
|
|
|
|
return Result.success(); |
|
|
|
|
|
} |
|
|
|
|
|
}else{ |
|
|
|
|
|
return Result.failed(ResultCode.USERNAME_ALREADY_EXISTS); |
|
|
} |
|
|
} |
|
|
return Result.failed(); |
|
|
return Result.failed(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "当前用户信息") |
|
|
|
|
|
@GetMapping("/current") |
|
|
|
|
|
public Result<User> currentUser(HttpServletRequest request) { |
|
|
|
|
|
String token = (String) request.getAttribute("token"); |
|
|
|
|
|
if (token == null || token.isEmpty()) { |
|
|
|
|
|
return Result.failed(); |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
Claims claims = JwtUtil.parseJWE(token); |
|
|
|
|
|
String username = claims.getSubject(); |
|
|
|
|
|
User user = userService.findByUsername(username); |
|
|
|
|
|
if (user == null) { |
|
|
|
|
|
return Result.failed(); |
|
|
|
|
|
} |
|
|
|
|
|
return Result.success(user); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
return Result.failed(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Operation(summary = "更新用户信息") |
|
|
@Operation(summary = "更新用户信息") |
|
|
@PutMapping("/{id}") |
|
|
@PutMapping("/{id}") |
|
|
public Result<String> updateUser(@PathVariable Long id, @RequestBody User user) { |
|
|
public Result<String> updateUser(@PathVariable Long id, @RequestBody User user) { |
|
|