parent
0dd89d35b1
commit
636f64326a
@ -1,10 +1,35 @@
|
||||
package org.example.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.example.mapper.UserMapper;
|
||||
import org.example.pojo.User;
|
||||
import org.example.service.UserService;
|
||||
import org.example.utils.Result;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Tag(name="用户",description = "用户相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@Operation(summary = "注册", description = "用户注册接口")
|
||||
@PostMapping("/regist")
|
||||
public Result regist(@RequestBody User user){
|
||||
System.out.println(user);
|
||||
return userService.regist(user);
|
||||
}
|
||||
|
||||
@Operation(summary = "登录", description = "用户登录接口")
|
||||
@GetMapping("/login")
|
||||
public Result login(@Parameter(description = "用户名") @Param("username") String username,@Parameter(description = "用户密码") @Param("password") String password){
|
||||
return userService.login(username, password);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
package org.example.service;
|
||||
|
||||
import org.example.pojo.User;
|
||||
import org.example.utils.Result;
|
||||
|
||||
public interface UserService {
|
||||
public Result regist(User user);
|
||||
public Result login(String username, String password);
|
||||
}
|
||||
|
@ -1,7 +1,43 @@
|
||||
package org.example.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.example.mapper.UserMapper;
|
||||
import org.example.pojo.User;
|
||||
import org.example.service.UserService;
|
||||
import org.example.utils.MD5Util;
|
||||
import org.example.utils.Result;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl {
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
|
||||
public Result regist(User user){
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("username",user.getUsername());
|
||||
boolean isExist = userMapper.exists(queryWrapper);
|
||||
if(isExist){
|
||||
return Result.error("-1","用户名已被使用");
|
||||
}
|
||||
user.setPassword(MD5Util.encrypt(user.getPassword()));
|
||||
Integer rows = userMapper.insert(user);
|
||||
if(rows>0){
|
||||
return Result.success();
|
||||
}
|
||||
return Result.error("-1","failure");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result login(String username, String password) {
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("username",username).eq("password",MD5Util.encrypt(password));
|
||||
User user = userMapper.selectOne(queryWrapper);
|
||||
user.setPassword(null);
|
||||
if(user != null){
|
||||
return Result.success(user);
|
||||
}
|
||||
return Result.error("-1","账号或者密码错误");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,66 @@
|
||||
package org.example.utils;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
@ApiModel(value = "响应结果实体", description = "响应结果实体")
|
||||
public class Result<T> {
|
||||
@ApiModelProperty(value = "响应提示码")
|
||||
private String code;
|
||||
@ApiModelProperty(value = "提示信息")
|
||||
private String msg;
|
||||
@ApiModelProperty(value = "响应数据")
|
||||
private T data;
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Result() {
|
||||
}
|
||||
|
||||
public Result(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static Result success() {
|
||||
Result result = new Result<>();
|
||||
result.setCode("0");
|
||||
result.setMsg("成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> Result<T> success(T data) {
|
||||
Result<T> result = new Result<>(data);
|
||||
result.setCode("0");
|
||||
result.setMsg("成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Result error(String code, String msg) {
|
||||
Result result = new Result();
|
||||
result.setCode(code);
|
||||
result.setMsg(msg);
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue