|
@@ -0,0 +1,61 @@
|
|
|
+package com.example.controller.user;
|
|
|
+
|
|
|
+import com.example.constant.JwtClaimsConstant;
|
|
|
+import com.example.dto.UserLoginDTO;
|
|
|
+import com.example.entity.User;
|
|
|
+import com.example.properties.JwtProperties;
|
|
|
+import com.example.result.Result;
|
|
|
+import com.example.service.UserService;
|
|
|
+import com.example.utils.JwtUtil;
|
|
|
+import com.example.vo.UserLoginVO;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 账户管理
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@Slf4j
|
|
|
+@Api(tags = "账户相关接口")
|
|
|
+@RequestMapping("/user")
|
|
|
+public class UserController {
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private JwtProperties jwtProperties;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 老师登录
|
|
|
+ *
|
|
|
+ * @param userLoginDTO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("/login")
|
|
|
+ @ApiOperation(value = "老师登录")
|
|
|
+ public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
|
|
+ log.info("老师登录:{}", userLoginDTO);
|
|
|
+ User user = userService.login(userLoginDTO);
|
|
|
+ //登录成功后,生成jwt令牌
|
|
|
+ Map<String, Object> claims = new HashMap<>();
|
|
|
+ claims.put(JwtClaimsConstant.USER_ID, user.getId());
|
|
|
+ String token = JwtUtil.createJWT(
|
|
|
+ jwtProperties.getAdminSecretKey(),
|
|
|
+ jwtProperties.getAdminTtl(),
|
|
|
+ claims);
|
|
|
+ UserLoginVO userLoginVO = UserLoginVO.builder()
|
|
|
+ .id(user.getId())
|
|
|
+ .userName(user.getUserName())
|
|
|
+ .name(user.getName())
|
|
|
+ .token(token).build();
|
|
|
+ return Result.success(userLoginVO);
|
|
|
+ }
|
|
|
+}
|