18 changed files with 771 additions and 17 deletions
@ -0,0 +1,28 @@ |
|||||||
|
package com.water.watersys.components; |
||||||
|
|
||||||
|
import cn.dev33.satoken.exception.NotLoginException; |
||||||
|
import cn.dev33.satoken.util.SaResult; |
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice; |
||||||
|
|
||||||
|
@RestControllerAdvice |
||||||
|
public class GlobalExceptionHandler { |
||||||
|
|
||||||
|
/** |
||||||
|
* 全局异常拦截,鉴权失败不会报错,会返回给前端报错原因 |
||||||
|
* @param e |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@ExceptionHandler |
||||||
|
public SaResult handlerException(Exception e) { |
||||||
|
e.printStackTrace(); |
||||||
|
return SaResult.error(e.getMessage()); |
||||||
|
} |
||||||
|
|
||||||
|
@ExceptionHandler |
||||||
|
public SaResult notLoginException(NotLoginException e){ |
||||||
|
System.out.println(e.getMessage()); |
||||||
|
e.printStackTrace(); |
||||||
|
return SaResult.error(e.getMessage()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,167 @@ |
|||||||
|
package com.water.watersys.components; |
||||||
|
|
||||||
|
import lombok.Getter; |
||||||
|
|
||||||
|
@Getter |
||||||
|
public class Result<T> { |
||||||
|
|
||||||
|
private Integer code; // 状态码
|
||||||
|
|
||||||
|
private String msg; // 返回消息
|
||||||
|
|
||||||
|
private T data; // 返回数据
|
||||||
|
|
||||||
|
public Result() { |
||||||
|
} |
||||||
|
|
||||||
|
public Result(Integer code, String msg, T data) { |
||||||
|
this.code = code; |
||||||
|
this.msg = msg; |
||||||
|
this.data = data; |
||||||
|
} |
||||||
|
|
||||||
|
private static <T> Result<T> build(T data) { |
||||||
|
Result<T> result = new Result<>(); |
||||||
|
|
||||||
|
if (data != null) { |
||||||
|
result.data = data; |
||||||
|
} |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
private static <T> Result<T> build(T data, Integer code, String msg) { |
||||||
|
|
||||||
|
return new Result<>(code, msg, data); |
||||||
|
} |
||||||
|
|
||||||
|
private static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) { |
||||||
|
|
||||||
|
return new Result<>(resultCodeEnum.getCode(), resultCodeEnum.getMsg(), data); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 200 无参数成功 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> success() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.SUCCESS); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 200 有参数成功 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> success(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.SUCCESS); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 201 无参创建资源成功 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> createSuccess() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.CREATE_SUCCESS); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 201 有参创建资源成功 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> createSuccess(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.CREATE_SUCCESS); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 204 删除资源成功,无参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> removeSuccess() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.REMOVE_SUCCESS); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 204 删除资源成功,有参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> removeSuccess(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.REMOVE_SUCCESS); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 400 失败,无参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> fail() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 400 失败,有参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> fail(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 401 验证失败,无参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> verificationFail() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.VERIFICATION_FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 401 验证失败,有参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> verificationFail(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.VERIFICATION_FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 402 创建资源失败,无参 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> createFail() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.CREATE_FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
public static <T> Result<T> deleteFail() { |
||||||
|
return build(null, ResultCodeEnum.REMOVE_FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 402 创建资源失败,有参数 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> createFail(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.CREATE_FAIL); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 404 资源不存在 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> notFoundFail() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.NOT_FOUND); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 500 服务端错误 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> error() { |
||||||
|
|
||||||
|
return build(null, ResultCodeEnum.ERROR); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 500 服务端错误,带参 |
||||||
|
*/ |
||||||
|
public static <T> Result<T> error(T data) { |
||||||
|
|
||||||
|
return build(data, ResultCodeEnum.ERROR); |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,27 @@ |
|||||||
|
package com.water.watersys.components; |
||||||
|
|
||||||
|
|
||||||
|
import lombok.Getter; |
||||||
|
|
||||||
|
@Getter |
||||||
|
public enum ResultCodeEnum { |
||||||
|
SUCCESS(200, "操作成功"), |
||||||
|
CREATE_SUCCESS(201, "创建成功"), |
||||||
|
REMOVE_SUCCESS(204, "删除成功"), |
||||||
|
REMOVE_FAIL(403, "删除失败"), |
||||||
|
FAIL(400, "操作失败"), |
||||||
|
VERIFICATION_FAIL(401, "验证失败"), |
||||||
|
CREATE_FAIL(402, "创建失败"), |
||||||
|
NOT_FOUND(404, "资源不存在"), |
||||||
|
ERROR(500, "服务器错误"); |
||||||
|
|
||||||
|
|
||||||
|
private final Integer code; |
||||||
|
private final String msg; |
||||||
|
|
||||||
|
ResultCodeEnum(Integer code, String msg) { |
||||||
|
this.code = code; |
||||||
|
this.msg = msg; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,55 @@ |
|||||||
|
package com.water.watersys.controller; |
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil; |
||||||
|
import com.water.watersys.components.Result; |
||||||
|
import com.water.watersys.model.domain.UserSys; |
||||||
|
import com.water.watersys.model.dto.LoginDto; |
||||||
|
import com.water.watersys.service.UserSysService; |
||||||
|
import io.swagger.v3.oas.annotations.Operation; |
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag; |
||||||
|
import jakarta.annotation.Resource; |
||||||
|
import org.springframework.web.bind.annotation.*; |
||||||
|
|
||||||
|
@RestController |
||||||
|
@CrossOrigin |
||||||
|
@Tag(name = "登录注册", description = "用户登录注册") |
||||||
|
public class LoginController { |
||||||
|
@Resource |
||||||
|
private UserSysService userSysService; |
||||||
|
|
||||||
|
@Operation(summary = "登录", description = "登录") |
||||||
|
@PostMapping("/login") |
||||||
|
public Result<String> login(@RequestBody LoginDto loginDto) { |
||||||
|
Integer id = userSysService.login(loginDto); |
||||||
|
if (id != 0) { |
||||||
|
StpUtil.login(id); |
||||||
|
return Result.success(StpUtil.getTokenValue()); |
||||||
|
}else { |
||||||
|
return Result.verificationFail(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Operation(summary = "退出登录", description = "退出登录") |
||||||
|
@PostMapping("/logout") |
||||||
|
public Result logout(){ |
||||||
|
StpUtil.logout(StpUtil.getLoginId()); |
||||||
|
return Result.success(StpUtil.getTokenValue()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Operation(summary = "注册", description = "注册") |
||||||
|
@PostMapping("/register") |
||||||
|
public Result register(@RequestBody UserSys userSys) { |
||||||
|
int index = userSysService.register(userSys); |
||||||
|
if (index > 0) { |
||||||
|
return Result.success(index); |
||||||
|
}else { |
||||||
|
return Result.createFail(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,58 @@ |
|||||||
|
package com.water.watersys.controller; |
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil; |
||||||
|
import com.water.watersys.components.Result; |
||||||
|
import com.water.watersys.model.dto.ChangePassword; |
||||||
|
import com.water.watersys.model.vo.UserOv; |
||||||
|
import com.water.watersys.service.UserSysService; |
||||||
|
import io.swagger.v3.oas.annotations.Operation; |
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag; |
||||||
|
import jakarta.annotation.Resource; |
||||||
|
import org.springframework.web.bind.annotation.*; |
||||||
|
|
||||||
|
@RestController |
||||||
|
@CrossOrigin |
||||||
|
@Tag(name = "用户", description = "用户") |
||||||
|
public class UserController { |
||||||
|
@Resource |
||||||
|
private UserSysService userSysService; |
||||||
|
|
||||||
|
|
||||||
|
@Operation(summary = "获取用户信息", description = "获取用户信息") |
||||||
|
@GetMapping("/user") |
||||||
|
public Result<UserOv> user() { |
||||||
|
Object loginId = StpUtil.getLoginId(); |
||||||
|
UserOv userOv = userSysService.getUser((String) loginId); |
||||||
|
if (userOv != null) { |
||||||
|
return Result.success(userOv); |
||||||
|
}else { |
||||||
|
return Result.notFoundFail(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Operation(summary = "修改用户密码", description = "修改用户密码") |
||||||
|
@PutMapping("/changePassword") |
||||||
|
public Result<String> changePassword(@RequestBody ChangePassword changePassword) { |
||||||
|
String loginId = (String) StpUtil.getLoginId(); |
||||||
|
Integer id = userSysService.changePassword(changePassword,loginId); |
||||||
|
if (id != 0) { |
||||||
|
return Result.success(); |
||||||
|
}else { |
||||||
|
return Result.createFail("密码验证失败"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Operation(summary = "修改用户状态", description = "修改用户状态") |
||||||
|
@PutMapping("/changeUserStatus") |
||||||
|
public Result<String> changeUserStatus(Integer status) { |
||||||
|
String loginId = (String) StpUtil.getLoginId(); |
||||||
|
Integer id = userSysService.changeUserStatus(status,loginId); |
||||||
|
if (id != 0) { |
||||||
|
return Result.success(); |
||||||
|
}else { |
||||||
|
return Result.createFail(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -1,14 +0,0 @@ |
|||||||
package com.water.watersys.controller; |
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin; |
|
||||||
import org.springframework.web.bind.annotation.GetMapping; |
|
||||||
import org.springframework.web.bind.annotation.RestController; |
|
||||||
|
|
||||||
@RestController |
|
||||||
@CrossOrigin |
|
||||||
public class textController { |
|
||||||
@GetMapping("/index") |
|
||||||
public String index() { |
|
||||||
return "Hello World"; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,27 @@ |
|||||||
|
package com.water.watersys.mapper; |
||||||
|
|
||||||
|
import com.water.watersys.model.domain.UserSys; |
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||||
|
import com.water.watersys.model.vo.UserOv; |
||||||
|
import org.apache.ibatis.annotations.Mapper; |
||||||
|
import org.apache.ibatis.annotations.Select; |
||||||
|
import org.apache.ibatis.annotations.Update; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author mac |
||||||
|
* @description 针对表【user_sys】的数据库操作Mapper |
||||||
|
* @createDate 2025-04-05 23:50:03 |
||||||
|
* @Entity generator.domain.UserSys |
||||||
|
*/ |
||||||
|
@Mapper |
||||||
|
public interface UserSysMapper extends BaseMapper<UserSys> { |
||||||
|
@Select("select * from user_sys where id = #{loginId}") |
||||||
|
UserOv getUser(String loginId); |
||||||
|
|
||||||
|
@Update("UPDATE user_sys SET password = #{newPassword} WHERE id = #{id}") |
||||||
|
Integer changePassword(Integer id, String newPassword); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,110 @@ |
|||||||
|
package com.water.watersys.model.domain; |
||||||
|
|
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType; |
||||||
|
import com.baomidou.mybatisplus.annotation.TableId; |
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @TableName user_sys |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class UserSys { |
||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
@TableId(value="id",type = IdType.AUTO ) |
||||||
|
private Integer id; |
||||||
|
|
||||||
|
/** |
||||||
|
* 用户名 |
||||||
|
*/ |
||||||
|
private String username; |
||||||
|
|
||||||
|
/** |
||||||
|
* 密码 |
||||||
|
*/ |
||||||
|
private String password; |
||||||
|
|
||||||
|
/** |
||||||
|
* 手机号 |
||||||
|
*/ |
||||||
|
private String phone; |
||||||
|
|
||||||
|
/** |
||||||
|
* 地址 |
||||||
|
*/ |
||||||
|
private String address; |
||||||
|
|
||||||
|
/** |
||||||
|
* 状态 0禁止 1启用 |
||||||
|
*/ |
||||||
|
private Integer status; |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建时间 |
||||||
|
*/ |
||||||
|
private Date createdTime; |
||||||
|
|
||||||
|
/** |
||||||
|
* 修改时间 |
||||||
|
*/ |
||||||
|
private Date updatedTime; |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object that) { |
||||||
|
if (this == that) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
if (that == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (getClass() != that.getClass()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
UserSys other = (UserSys) that; |
||||||
|
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) |
||||||
|
&& (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername())) |
||||||
|
&& (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())) |
||||||
|
&& (this.getPhone() == null ? other.getPhone() == null : this.getPhone().equals(other.getPhone())) |
||||||
|
&& (this.getAddress() == null ? other.getAddress() == null : this.getAddress().equals(other.getAddress())) |
||||||
|
&& (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus())) |
||||||
|
&& (this.getCreatedTime() == null ? other.getCreatedTime() == null : this.getCreatedTime().equals(other.getCreatedTime())) |
||||||
|
&& (this.getUpdatedTime() == null ? other.getUpdatedTime() == null : this.getUpdatedTime().equals(other.getUpdatedTime())); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int hashCode() { |
||||||
|
final int prime = 31; |
||||||
|
int result = 1; |
||||||
|
result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); |
||||||
|
result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode()); |
||||||
|
result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode()); |
||||||
|
result = prime * result + ((getPhone() == null) ? 0 : getPhone().hashCode()); |
||||||
|
result = prime * result + ((getAddress() == null) ? 0 : getAddress().hashCode()); |
||||||
|
result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode()); |
||||||
|
result = prime * result + ((getCreatedTime() == null) ? 0 : getCreatedTime().hashCode()); |
||||||
|
result = prime * result + ((getUpdatedTime() == null) ? 0 : getUpdatedTime().hashCode()); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
sb.append(getClass().getSimpleName()); |
||||||
|
sb.append(" ["); |
||||||
|
sb.append("Hash = ").append(hashCode()); |
||||||
|
sb.append(", id=").append(id); |
||||||
|
sb.append(", username=").append(username); |
||||||
|
sb.append(", password=").append(password); |
||||||
|
sb.append(", phone=").append(phone); |
||||||
|
sb.append(", address=").append(address); |
||||||
|
sb.append(", status=").append(status); |
||||||
|
sb.append(", createdTime=").append(createdTime); |
||||||
|
sb.append(", updatedTime=").append(updatedTime); |
||||||
|
sb.append("]"); |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package com.water.watersys.model.dto; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
@Data |
||||||
|
public class ChangePassword { |
||||||
|
private String oldPassword; |
||||||
|
private String newPassword; |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package com.water.watersys.model.dto; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
@Data |
||||||
|
public class LoginDto { |
||||||
|
private String username; |
||||||
|
private String password; |
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
package com.water.watersys.model.vo; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
@Data |
||||||
|
public class UserOv { |
||||||
|
private Integer id; |
||||||
|
private String username; |
||||||
|
private String phone; |
||||||
|
private String address; |
||||||
|
private Integer status; |
||||||
|
private Date createdTime; |
||||||
|
private Date updatedTime; |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
package com.water.watersys.service; |
||||||
|
|
||||||
|
import com.water.watersys.model.domain.UserSys; |
||||||
|
import com.baomidou.mybatisplus.extension.service.IService; |
||||||
|
import com.water.watersys.model.dto.ChangePassword; |
||||||
|
import com.water.watersys.model.dto.LoginDto; |
||||||
|
import com.water.watersys.model.vo.UserOv; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author mac |
||||||
|
* @description 针对表【user_sys】的数据库操作Service |
||||||
|
* @createDate 2025-04-05 23:50:03 |
||||||
|
*/ |
||||||
|
@Service |
||||||
|
public interface UserSysService extends IService<UserSys> { |
||||||
|
|
||||||
|
int register(UserSys userSys); |
||||||
|
|
||||||
|
Integer login(LoginDto loginDto); |
||||||
|
|
||||||
|
UserOv getUser(String loginId); |
||||||
|
|
||||||
|
Integer changePassword(ChangePassword changePassword, String loginId); |
||||||
|
|
||||||
|
Integer changeUserStatus(Integer status, String loginId); |
||||||
|
} |
@ -0,0 +1,85 @@ |
|||||||
|
package com.water.watersys.service.impl; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||||
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||||
|
import com.water.watersys.model.domain.UserSys; |
||||||
|
import com.water.watersys.mapper.UserSysMapper; |
||||||
|
import com.water.watersys.model.dto.ChangePassword; |
||||||
|
import com.water.watersys.model.dto.LoginDto; |
||||||
|
import com.water.watersys.model.vo.UserOv; |
||||||
|
import com.water.watersys.service.UserSysService; |
||||||
|
import jakarta.annotation.Resource; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
import org.springframework.util.DigestUtils; |
||||||
|
|
||||||
|
import java.util.Objects; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author mac |
||||||
|
* @description 针对表【user_sys】的数据库操作Service实现 |
||||||
|
* @createDate 2025-04-05 23:50:03 |
||||||
|
*/ |
||||||
|
@Service |
||||||
|
public class UserSysServiceImpl extends ServiceImpl<UserSysMapper, UserSys> |
||||||
|
implements UserSysService { |
||||||
|
@Resource |
||||||
|
private UserSysMapper userSysMapper; |
||||||
|
|
||||||
|
@Override |
||||||
|
public int register(UserSys userSys) { |
||||||
|
String password = DigestUtils.md5DigestAsHex(userSys.getPassword().getBytes()); |
||||||
|
userSys.setPassword(password); |
||||||
|
return userSysMapper.insert(userSys); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Integer login(LoginDto loginDto) { |
||||||
|
QueryWrapper<UserSys> userSysQueryWrapper = new QueryWrapper<>(); |
||||||
|
String password = DigestUtils.md5DigestAsHex(loginDto.getPassword().getBytes()); |
||||||
|
userSysQueryWrapper.eq("password", password); |
||||||
|
userSysQueryWrapper.eq("username", loginDto.getUsername()); |
||||||
|
UserSys userSys = userSysMapper.selectOne(userSysQueryWrapper); |
||||||
|
if (userSys == null) { |
||||||
|
return 0; |
||||||
|
}else if (userSys.getStatus() == 0){ |
||||||
|
return 0; |
||||||
|
}else { |
||||||
|
return userSys.getId(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public UserOv getUser(String loginId) { |
||||||
|
return userSysMapper.getUser(loginId); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Integer changePassword(ChangePassword changePassword, String loginId) { |
||||||
|
UserSys userSys = userSysMapper.selectById(loginId); |
||||||
|
String password = userSys.getPassword(); |
||||||
|
if (Objects.equals(password, DigestUtils.md5DigestAsHex(changePassword.getOldPassword().getBytes()))) { |
||||||
|
String newPassword = changePassword.getNewPassword(); |
||||||
|
changePassword.setNewPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes())); |
||||||
|
return userSysMapper.changePassword(userSys.getId(),changePassword.getNewPassword()); |
||||||
|
}else { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Integer changeUserStatus(Integer status, String loginId) { |
||||||
|
LambdaUpdateWrapper<UserSys> lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); |
||||||
|
lambdaUpdateWrapper |
||||||
|
.eq(UserSys::getId, loginId) // 条件:id = loginId
|
||||||
|
.set(UserSys::getStatus, status); // 设置新的 status 值
|
||||||
|
return userSysMapper.update(null, lambdaUpdateWrapper); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,29 @@ |
|||||||
|
package com.water.watersys.utils; |
||||||
|
|
||||||
|
import io.swagger.v3.oas.models.OpenAPI; |
||||||
|
import io.swagger.v3.oas.models.info.Info; |
||||||
|
import io.swagger.v3.oas.models.info.Contact; |
||||||
|
import io.swagger.v3.oas.models.info.License; |
||||||
|
import io.swagger.v3.oas.models.servers.Server; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class OpenApiConfig { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public OpenAPI customOpenAPI() { |
||||||
|
return new OpenAPI() |
||||||
|
.addServersItem(new Server().url("http://localhost:8080").description("Local Development Server")) |
||||||
|
.info(new Info() |
||||||
|
.title("API文档") |
||||||
|
.version("1.0") |
||||||
|
.description("Spring Boot 3.4.4 应用API文档") |
||||||
|
.contact(new Contact() |
||||||
|
.name("开发者") |
||||||
|
.email("319906174@qq.com")) |
||||||
|
.license(new License() |
||||||
|
.name("Apache 2.0") |
||||||
|
.url("http://localhost:8080"))); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
package com.water.watersys.utils; |
||||||
|
|
||||||
|
import cn.dev33.satoken.interceptor.SaInterceptor; |
||||||
|
import cn.dev33.satoken.stp.StpUtil; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class SaTokenConfigure implements WebMvcConfigurer { |
||||||
|
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||||
|
@Override |
||||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||||
|
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验。
|
||||||
|
registry.addInterceptor(new SaInterceptor(handler -> StpUtil.checkLogin())) |
||||||
|
.addPathPatterns("/**") |
||||||
|
.excludePathPatterns( |
||||||
|
"/login/**", |
||||||
|
"/swagger-ui/**", |
||||||
|
"/swagger-resources/**", |
||||||
|
"/v2/api-docs", |
||||||
|
"/v3/api-docs/**", |
||||||
|
"/webjars/**", |
||||||
|
"/doc.html" // 如果是Knife4j
|
||||||
|
); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<!DOCTYPE mapper |
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||||
|
<mapper namespace="com.water.watersys.mapper.UserSysMapper"> |
||||||
|
|
||||||
|
<resultMap id="BaseResultMap" type="com.water.watersys.model.domain.UserSys"> |
||||||
|
<id property="id" column="id" /> |
||||||
|
<result property="username" column="username" /> |
||||||
|
<result property="password" column="password" /> |
||||||
|
<result property="phone" column="phone" /> |
||||||
|
<result property="address" column="address" /> |
||||||
|
<result property="status" column="status" /> |
||||||
|
<result property="createdTime" column="created_time" /> |
||||||
|
<result property="updatedTime" column="updated_time" /> |
||||||
|
</resultMap> |
||||||
|
|
||||||
|
<sql id="Base_Column_List"> |
||||||
|
id,username,password,phone,address,status, |
||||||
|
created_time,updated_time |
||||||
|
</sql> |
||||||
|
</mapper> |
Loading…
Reference in new issue