---
url: /notes/项目/用户中心/index.md
---
## 项目地址

知识星球：https://articles.zsxq.com/id\_5f36h45rq0io.html
编程导航：https://www.code-nav.cn/course/1790943469757837313

代码地址：https://github.com/731016/usercenter/tree/master

### 主要技术栈

全局异常处理

统一返回工具

## 安装umi插件出错

![image-20230609223451463](https://note-1259190304.cos.ap-chengdu.myqcloud.com/note202306092234932.png)

[解决 npm install 时出现的.git can‘t be found (see https://git.io/Jc3F9)的问题 run `npm fund` for details问题解决\_.git can't be found\_互联网全栈开发实战的博客-CSDN博客](https://blog.csdn.net/lvoelife/article/details/126172368)

可能是"prepare": "cd .. && husky install superjsonweb/.husky" 替换为这个

#### 项目启动报错

![image-20240919205513590](https://note-1259190304.cos.ap-chengdu.myqcloud.com/noteimage-20240919205513590.png)

\[node:internal/crypto/hash:71 this[kHandle\] = new \_Hash(algorithm, xofLen)-CSDN博客](https://blog.csdn.net/weixin_47818125/article/details/131975219)

![image-20240919205558683](https://note-1259190304.cos.ap-chengdu.myqcloud.com/noteimage-20240919205558683.png)

## springboot初始化模板

https://start.springboot.io/

## 逻辑删除

[逻辑删除 | MyBatis-Plus (baomidou.com)](https://baomidou.com/pages/6b03c5/#使用方法)

## 前台代理不能通过配置,匹配请求前缀

```tsx
export const request = {
  prefix : 'http://127.0.0.1:8090/api'
};
```

## 异常处理

### 全局异常处理

```java
@RestControllerAdvice
@Slf4j
public class GlobalExecptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        log.error("businessException: " + e.getMessage() + ",描述:" + e.getDescription(), e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return ResultUtils.error(CodeEnum.SYSTEM_ERROR, e.getMessage(), "");
    }

}
```

### 局部异常处理

```java
@ExceptionHandler(RuntimeException.class)
指定在某个controller类中
```

### 统一返回

#### 业务异常

```java
public class BusinessException extends RuntimeException {
    private final int code;
    private final String description;

    public BusinessException(String message, int code, String description) {
        super(message);
        this.code = code;
        this.description = description;
    }

    public BusinessException(CodeEnum errorCode) {
        super(errorCode.getMessage() + "^" + errorCode.getDescription());
        this.code = errorCode.getCode();
        this.description = errorCode.getDescription();
    }

    public BusinessException(CodeEnum errorCode, String description) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }
}
```

#### 返回结果

```java
@Data
public class BaseResponse<T> implements Serializable {

    private int code;
    private T data;
    private String message;
    private String description;

    public BaseResponse(int code, T data, String message, String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }

    public BaseResponse(int code, T data) {
        this(code,data,"","");
    }

    public BaseResponse(CodeEnum codeEnum) {
        this(codeEnum.getCode(),null, codeEnum.getMessage(), codeEnum.getDescription());
    }
}
```

#### 统一返回工具类

```java
public class ResultUtils {

    public static <T> BaseResponse success(T data) {
        return new BaseResponse(CodeEnum.SUCCESS_STATUS.getCode(), data, CodeEnum.SUCCESS_STATUS.getMessage(), CodeEnum.SUCCESS_STATUS.getDescription());
    }

    public static <T> BaseResponse error(T data) {
        return new BaseResponse(CodeEnum.ERROR_STATUS.getCode(), data, CodeEnum.ERROR_STATUS.getMessage(), CodeEnum.ERROR_STATUS.getDescription());
    }
    
    public static BaseResponse error(int code, String message, String description) {
        return new BaseResponse(code, null, message, description);
    }
    
    public static <T> BaseResponse error(T data,String description) {
        return new BaseResponse(CodeEnum.ERROR_STATUS.getCode(), data, CodeEnum.ERROR_STATUS.getMessage(), description);
    }

    //使用枚举
    public static <T> BaseResponse error(CodeEnum codeEnum) {
        return new BaseResponse(codeEnum);
    }

    public static <T> BaseResponse error(CodeEnum codeEnum, String message, String description) {
        return new BaseResponse(codeEnum.getCode(), null, message, description);
    }
}
```
