diff --git a/README.md b/README.md index d4fe32e8..07a007d7 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,8 @@ continew-admin # 全局通用项目配置及依赖版本管理 │ │ └─ cnadmin │ │ ├─ webapi │ │ │ └─ controller - │ │ │ └─ auth # 认证相关 API + │ │ │ ├─ auth # 认证相关 API + │ │ │ └─ system # 系统管理相关 API │ │ └─ ContinewAdminApplication.java # 启动入口 │ └─ resources # 工程配置目录 │ └─ db.changelog.v0.0.1 # 数据库脚本文件 @@ -153,7 +154,8 @@ continew-admin # 全局通用项目配置及依赖版本管理 │ │ └─ system # 系统管理相关业务及配置 │ │ ├─ mapper # 系统管理相关 Mapper │ │ ├─ model # 系统管理相关模型 - │ │ │ └─ entity # 系统管理相关实体对象 + │ │ │ ├─ entity # 系统管理相关实体对象 + │ │ │ └─ vo # 系统管理相关 VO(View Object) │ │ └─ service # 系统管理相关业务接口及实现类 │ │ └─ impl # 系统管理相关业务实现类 │ └─ resources # 工程配置目录 @@ -181,7 +183,8 @@ continew-admin # 全局通用项目配置及依赖版本管理 │ │ └─ vo # 公共 VO(View Object) │ └─ util # 公共工具类 │ ├─ helper # 公共 Helper(助手) - │ └─ holder # 公共 Holder(持有者) + │ ├─ holder # 公共 Holder(持有者) + │ └─ validate # 公共校验器(参数校验,业务校验) ``` ### 前端 @@ -189,12 +192,14 @@ continew-admin # 全局通用项目配置及依赖版本管理 ```bash continew-admin └─ continew-admin-ui # 前端项目 + ├─ public # 公共静态资源(favicon.ico、logo.svg) ├─ src │ ├─ api # 请求接口 - │ │ └─ auth # 认证模块 - │ ├─ assets # 静态资源 - │ │ └─ style # 全局样式 + │ │ ├─ auth # 认证模块 + │ │ └─ system # 系统管理模块 │ ├─ assets # 静态资源 + │ │ ├─ images # 图片资源 + │ │ └─ style # 样式资源 │ ├─ components # 通用业务组件 │ ├─ config # 全局配置(包含 echarts 主题) │ │ └─ settings.json # 配置文件 @@ -205,11 +210,12 @@ continew-admin │ ├─ mock # 模拟数据 │ ├─ router # 路由配置 │ ├─ store # 状态管理中心 - │ ├─ types # Typescript 类型 + │ ├─ types # TypeScript 类型 │ ├─ utils # 工具库 │ ├─ views # 页面模板 │ │ ├─ login # 登录模块 - │ │ └─ user # 用户模块(用户设置、用户中心) + │ │ └─ system # 系统管理模块 + │ │ └─ user # 用户模块(用户中心) │ ├─ App.vue # 视图入口 │ └─ main.ts # 入口文件 ├─ .env.development diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/WebMvcConfiguration.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/WebMvcConfiguration.java index 102c5746..8167804b 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/WebMvcConfiguration.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/WebMvcConfiguration.java @@ -35,6 +35,7 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import top.charles7c.cnadmin.common.config.properties.CorsProperties; +import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties; /** * Web MVC 配置 @@ -48,6 +49,7 @@ import top.charles7c.cnadmin.common.config.properties.CorsProperties; public class WebMvcConfiguration implements WebMvcConfigurer { private final CorsProperties corsProperties; + private final LocalStorageProperties localStorageProperties; private final MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter; /** @@ -55,6 +57,13 @@ public class WebMvcConfiguration implements WebMvcConfigurer { */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + LocalStorageProperties.LocalStoragePath path = localStorageProperties.getPath(); + String avatarUtl = "file:" + path.getAvatar().replace("\\", "/"); + String fileUrl = "file:" + path.getFile().replace("\\", "/"); + registry.addResourceHandler(localStorageProperties.getFilePattern()).addResourceLocations(fileUrl) + .setCachePeriod(0); + registry.addResourceHandler(localStorageProperties.getAvatarPattern()).addResourceLocations(avatarUtl) + .setCachePeriod(0); registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/") .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic()); diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/properties/LocalStorageProperties.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/properties/LocalStorageProperties.java new file mode 100644 index 00000000..588f1e19 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/config/properties/LocalStorageProperties.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.common.config.properties; + +import lombok.Data; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import cn.hutool.system.OsInfo; +import cn.hutool.system.SystemUtil; + +/** + * 本地存储配置属性 + * + * @author Charles7c + * @since 2023/1/2 19:43 + */ +@Data +@Component +@ConfigurationProperties(prefix = "local-storage") +public class LocalStorageProperties { + + /** 文件模式 */ + private String filePattern; + + /** 头像模式 */ + private String avatarPattern; + + /** 文件大小限制 */ + private Long maxSizeInMb; + + /** 头像大小限制 */ + private Long avatarMaxSizeInMb; + + /** Windows 系统本地存储路径 */ + private LocalStoragePath windows; + + /** Linux 系统本地存储路径 */ + private LocalStoragePath linux; + + /** MAC 系统本地存储路径 */ + private LocalStoragePath mac; + + /** + * 获取存储路径 + * + * @return / + */ + public LocalStoragePath getPath() { + OsInfo osInfo = SystemUtil.getOsInfo(); + if (osInfo.isWindows()) { + return windows; + } + if (osInfo.isMac()) { + return mac; + } + return linux; + } + + /** + * 本地存储路径 + */ + @Data + public static class LocalStoragePath { + + /** 文件存储路径 */ + private String file; + + /** 头像存储路径 */ + private String avatar; + } +} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/consts/FileConstants.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/consts/FileConstants.java new file mode 100644 index 00000000..13ed0f69 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/consts/FileConstants.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.common.consts; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * 文件常量 + * + * @author Charles7c + * @since 2023/1/2 21:19 + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class FileConstants { + + /** + * 头像支持的图片类型 + */ + public static final String[] AVATAR_SUPPORTED_IMG_TYPES = {"jpg", "png", "gif", "jpeg"}; + +} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java index db218a87..ac647e5d 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java @@ -33,8 +33,10 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import cn.dev33.satoken.exception.NotLoginException; +import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import top.charles7c.cnadmin.common.exception.BadRequestException; @@ -164,6 +166,18 @@ public class GlobalExceptionHandler { return R.fail(HttpStatus.UNAUTHORIZED.value(), "认证失败,无法访问系统资源"); } + /** + * 拦截文件上传异常-超过上传大小限制 + */ + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MaxUploadSizeExceededException.class) + public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) { + String sizeLimit = StrUtil.subBetween(e.getMessage(), "The maximum size ", " for"); + log.error("请求地址'{}',上传文件失败,文件大小超过限制", request.getRequestURI(), e); + return R.fail(HttpStatus.BAD_REQUEST.value(), + String.format("请上传小于 %s MB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024)); + } + /** * 操作日志保存异常信息 * diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/FileUtils.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/FileUtils.java new file mode 100644 index 00000000..88cb90d5 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/FileUtils.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.common.util; + +import java.io.File; +import java.time.LocalDateTime; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import org.springframework.web.multipart.MultipartFile; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.file.FileNameUtil; +import cn.hutool.core.util.IdUtil; + +/** + * 文件工具类 + * + * @author Charles7c + * @since 2023/1/2 21:34 + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class FileUtils { + + /** + * 上传文件 + * + * @param multipartFile + * 源文件对象 + * @param filePath + * 文件路径 + * @param isKeepOriginalFilename + * 是否保留原文件名 + * @return 目标文件对象 + */ + public static File upload(MultipartFile multipartFile, String filePath, boolean isKeepOriginalFilename) { + String originalFilename = multipartFile.getOriginalFilename(); + String extensionName = FileNameUtil.extName(originalFilename); + + String filename; + if (isKeepOriginalFilename) { + filename = String.format("%s-%s.%s", FileNameUtil.getPrefix(originalFilename), + DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmssS"), extensionName); + } else { + filename = String.format("%s.%s", IdUtil.fastSimpleUUID(), extensionName); + } + + try { + String pathname = filePath + filename; + File dest = new File(pathname).getCanonicalFile(); + // 如果父路径不存在,自动创建 + if (!dest.getParentFile().exists()) { + if (!dest.getParentFile().mkdirs()) { + log.error("Create upload file parent path failed."); + } + } + // 文件写入 + multipartFile.transferTo(dest); + return dest; + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return null; + } + +} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/helper/LoginHelper.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/helper/LoginHelper.java index b2441a40..62655f93 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/helper/LoginHelper.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/helper/LoginHelper.java @@ -65,6 +65,17 @@ public class LoginHelper { return loginUser; } + /** + * 更新登录用户信息 + * + * @param loginUser + * 登录用户信息 + */ + public static void updateLoginUser(LoginUser loginUser) { + SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser); + StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser); + } + /** * 获取登录用户 ID * diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/CheckUtils.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/CheckUtils.java new file mode 100644 index 00000000..a94b6ad9 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/CheckUtils.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.common.util.validate; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import top.charles7c.cnadmin.common.exception.ServiceException; + +/** + * 业务检查工具类(抛出 500 ServiceException) + * + * @author Charles7c + * @see ServiceException + * @since 2023/1/2 22:12 + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class CheckUtils extends Validator { + + private static final Class EXCEPTION_TYPE = ServiceException.class; + + /** + * 如果为空,抛出异常 + * + * @param obj + * 被检测的对象 + * @param message + * 错误信息 + */ + public static void exIfNull(Object obj, String message) { + exIfNull(obj, message, EXCEPTION_TYPE); + } + + /** + * 如果为空,抛出异常 + * + * @param str + * 被检测的字符串 + * @param message + * 错误信息 + */ + public static void exIfBlank(CharSequence str, String message) { + exIfBlank(str, message, EXCEPTION_TYPE); + } + + /** + * 如果相同,抛出异常 + * + * @param obj1 + * 要比较的对象1 + * @param obj2 + * 要比较的对象2 + * @param message + * 错误信息 + */ + public static void exIfEqual(Object obj1, Object obj2, String message) { + exIfEqual(obj1, obj2, message, EXCEPTION_TYPE); + } + + /** + * 如果不相同,抛出异常 + * + * @param obj1 + * 要比较的对象1 + * @param obj2 + * 要比较的对象2 + * @param message + * 错误信息 + */ + public static void exIfNotEqual(Object obj1, Object obj2, String message) { + exIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE); + } + + /** + * 如果条件成立,抛出异常 + * + * @param conditionSupplier + * 条件 + * @param message + * 错误信息 + */ + public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message) { + exIfCondition(conditionSupplier, message, EXCEPTION_TYPE); + } +} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/CheckUtils.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/ValidationUtils.java similarity index 72% rename from continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/CheckUtils.java rename to continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/ValidationUtils.java index 1f4fe40f..9451405c 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/CheckUtils.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/ValidationUtils.java @@ -14,26 +14,26 @@ * limitations under the License. */ -package top.charles7c.cnadmin.common.util; +package top.charles7c.cnadmin.common.util.validate; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; - import top.charles7c.cnadmin.common.exception.BadRequestException; /** - * 检查工具类 + * 校验工具类(抛出 400 BadRequestException) * * @author Charles7c * @since 2022/12/21 20:56 + * @see BadRequestException */ @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class CheckUtils { +public class ValidationUtils extends Validator { + + private static final Class EXCEPTION_TYPE = BadRequestException.class; /** * 如果为空,抛出异常 @@ -44,10 +44,7 @@ public class CheckUtils { * 错误信息 */ public static void exIfNull(Object obj, String message) { - if (obj == null) { - log.error(message); - throw new BadRequestException(message); - } + exIfNull(obj, message, EXCEPTION_TYPE); } /** @@ -59,10 +56,7 @@ public class CheckUtils { * 错误信息 */ public static void exIfBlank(CharSequence str, String message) { - if (StrUtil.isBlank(str)) { - log.error(message); - throw new BadRequestException(message); - } + exIfBlank(str, message, EXCEPTION_TYPE); } /** @@ -76,10 +70,7 @@ public class CheckUtils { * 错误信息 */ public static void exIfEqual(Object obj1, Object obj2, String message) { - if (ObjectUtil.equals(obj1, obj2)) { - log.error(message); - throw new BadRequestException(message); - } + exIfEqual(obj1, obj2, message, EXCEPTION_TYPE); } /** @@ -93,10 +84,7 @@ public class CheckUtils { * 错误信息 */ public static void exIfNotEqual(Object obj1, Object obj2, String message) { - if (ObjectUtil.notEqual(obj1, obj2)) { - log.error(message); - throw new BadRequestException(message); - } + exIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE); } /** @@ -108,9 +96,6 @@ public class CheckUtils { * 错误信息 */ public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message) { - if (conditionSupplier != null && conditionSupplier.getAsBoolean()) { - log.error(message); - throw new BadRequestException(message); - } + exIfCondition(conditionSupplier, message, EXCEPTION_TYPE); } } diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/Validator.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/Validator.java new file mode 100644 index 00000000..613cec33 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/validate/Validator.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.common.util.validate; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; + +/** + * @author Charles7c + * @since 2023/1/2 22:12 + */ +@Slf4j +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Validator { + + /** + * 如果为空,抛出异常 + * + * @param obj + * 被检测的对象 + * @param message + * 错误信息 + * @param exceptionType + * 异常类型 + */ + protected static void exIfNull(Object obj, String message, Class exceptionType) { + if (obj == null) { + log.error(message); + throw ReflectUtil.newInstance(exceptionType, message); + } + } + + /** + * 如果为空,抛出异常 + * + * @param str + * 被检测的字符串 + * @param message + * 错误信息 + * @param exceptionType + * 异常类型 + */ + public static void exIfBlank(CharSequence str, String message, Class exceptionType) { + if (StrUtil.isBlank(str)) { + log.error(message); + throw ReflectUtil.newInstance(exceptionType, message); + } + } + + /** + * 如果相同,抛出异常 + * + * @param obj1 + * 要比较的对象1 + * @param obj2 + * 要比较的对象2 + * @param message + * 错误信息 + * @param exceptionType + * 异常类型 + */ + public static void exIfEqual(Object obj1, Object obj2, String message, + Class exceptionType) { + if (ObjectUtil.equals(obj1, obj2)) { + log.error(message); + throw ReflectUtil.newInstance(exceptionType, message); + } + } + + /** + * 如果不相同,抛出异常 + * + * @param obj1 + * 要比较的对象1 + * @param obj2 + * 要比较的对象2 + * @param message + * 错误信息 + * @param exceptionType + * 异常类型 + */ + public static void exIfNotEqual(Object obj1, Object obj2, String message, + Class exceptionType) { + if (ObjectUtil.notEqual(obj1, obj2)) { + log.error(message); + throw ReflectUtil.newInstance(exceptionType, message); + } + } + + /** + * 如果条件成立,抛出异常 + * + * @param conditionSupplier + * 条件 + * @param message + * 错误信息 + * @param exceptionType + * 异常类型 + */ + public static void exIfCondition(java.util.function.BooleanSupplier conditionSupplier, String message, + Class exceptionType) { + if (conditionSupplier != null && conditionSupplier.getAsBoolean()) { + log.error(message); + throw ReflectUtil.newInstance(exceptionType, message); + } + } +} diff --git a/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/service/impl/LoginServiceImpl.java b/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/service/impl/LoginServiceImpl.java index 2d4771e5..21d14d40 100644 --- a/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/service/impl/LoginServiceImpl.java +++ b/continew-admin-system/src/main/java/top/charles7c/cnadmin/auth/service/impl/LoginServiceImpl.java @@ -26,9 +26,9 @@ import cn.hutool.core.bean.BeanUtil; import top.charles7c.cnadmin.auth.service.LoginService; import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum; import top.charles7c.cnadmin.common.model.dto.LoginUser; -import top.charles7c.cnadmin.common.util.CheckUtils; import top.charles7c.cnadmin.common.util.SecureUtils; import top.charles7c.cnadmin.common.util.helper.LoginHelper; +import top.charles7c.cnadmin.common.util.validate.ValidationUtils; import top.charles7c.cnadmin.system.model.entity.SysUser; import top.charles7c.cnadmin.system.service.UserService; @@ -50,10 +50,11 @@ public class LoginServiceImpl implements LoginService { SysUser sysUser = userService.getByUsername(username); // 校验 - CheckUtils.exIfNull(sysUser, "用户名或密码错误"); + ValidationUtils.exIfNull(sysUser, "用户名或密码错误"); Long userId = sysUser.getUserId(); - CheckUtils.exIfNotEqual(sysUser.getPassword(), SecureUtils.md5Salt(password, userId.toString()), "用户名或密码错误"); - CheckUtils.exIfEqual(DisEnableStatusEnum.DISABLE, sysUser.getStatus(), "此账号已被禁用,如有疑问,请联系管理员"); + ValidationUtils.exIfNotEqual(sysUser.getPassword(), SecureUtils.md5Salt(password, userId.toString()), + "用户名或密码错误"); + ValidationUtils.exIfEqual(DisEnableStatusEnum.DISABLE, sysUser.getStatus(), "此账号已被禁用,如有疑问,请联系管理员"); // 登录 LoginUser loginUser = BeanUtil.copyProperties(sysUser, LoginUser.class); diff --git a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/model/vo/AvatarVO.java b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/model/vo/AvatarVO.java new file mode 100644 index 00000000..bd3a9abc --- /dev/null +++ b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/model/vo/AvatarVO.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.cnadmin.system.model.vo; + +import java.io.Serializable; + +import lombok.Data; +import lombok.experimental.Accessors; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * 头像信息 + * + * @author Charles7c + * @since 2023/1/2 16:29 + */ +@Data +@Accessors(chain = true) +@Schema(description = "头像信息") +public class AvatarVO implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 头像地址 + */ + @Schema(description = "头像地址") + private String avatar; +} diff --git a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/UserService.java b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/UserService.java index a0e42f1c..9ed6ffdc 100644 --- a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/UserService.java +++ b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/UserService.java @@ -34,4 +34,14 @@ public interface UserService { * @return 用户信息 */ SysUser getByUsername(String username); + + /** + * 修改头像 + * + * @param avatar + * 头像路径 + * @param userId + * 用户ID + */ + void updateAvatar(String avatar, Long userId); } diff --git a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/UserServiceImpl.java b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/UserServiceImpl.java index c20de6a7..baaaa339 100644 --- a/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/UserServiceImpl.java +++ b/continew-admin-system/src/main/java/top/charles7c/cnadmin/system/service/impl/UserServiceImpl.java @@ -20,6 +20,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import top.charles7c.cnadmin.system.mapper.UserMapper; @@ -42,4 +43,10 @@ public class UserServiceImpl implements UserService { public SysUser getByUsername(String username) { return userMapper.selectOne(Wrappers.lambdaQuery().eq(SysUser::getUsername, username)); } + + @Override + public void updateAvatar(String avatar, Long userId) { + userMapper.update(null, + new LambdaUpdateWrapper().set(SysUser::getAvatar, avatar).eq(SysUser::getUserId, userId)); + } } diff --git a/continew-admin-ui/src/api/system/user-center.ts b/continew-admin-ui/src/api/system/user-center.ts new file mode 100644 index 00000000..1a798fc1 --- /dev/null +++ b/continew-admin-ui/src/api/system/user-center.ts @@ -0,0 +1,18 @@ +import axios from 'axios'; + +export interface BasicInfoModel { + username: string; + nickname: string; + gender: number; +} + +export interface AvatarRes { + avatar: string; +} +export function uploadAvatar(data: FormData) { + return axios.post('/system/user/center/avatar', data); +} + +export function saveUserInfo() { + return axios.get('/api/user/save-info'); +} \ No newline at end of file diff --git a/continew-admin-ui/src/api/user-center.ts b/continew-admin-ui/src/api/user-center.ts deleted file mode 100644 index 5816f48e..00000000 --- a/continew-admin-ui/src/api/user-center.ts +++ /dev/null @@ -1,57 +0,0 @@ -import axios from 'axios'; - -export interface MyProjectRecord { - id: number; - name: string; - description: string; - peopleNumber: number; - contributors: { - name: string; - email: string; - avatar: string; - }[]; -} -export function queryMyProjectList() { - return axios.get('/api/user/my-project/list'); -} - -export interface MyTeamRecord { - id: number; - avatar: string; - name: string; - peopleNumber: number; -} -export function queryMyTeamList() { - return axios.get('/api/user/my-team/list'); -} - -export interface LatestActivity { - id: number; - title: string; - description: string; - avatar: string; -} -export function queryLatestActivity() { - return axios.get('/api/user/latest-activity'); -} - -export function saveUserInfo() { - return axios.get('/api/user/save-info'); -} - -export interface BasicInfoModel { - username: string; - nickname: string; - gender: number; -} - -export function userUploadApi( - data: FormData, - config: { - controller: AbortController; - onUploadProgress?: (progressEvent: any) => void; - } -) { - // const controller = new AbortController(); - return axios.post('/api/user/upload', data, config); -} diff --git a/continew-admin-ui/src/components/message-box/locale/en-US.ts b/continew-admin-ui/src/components/message-box/locale/en-US.ts index 8a0f68e0..7bfcdff0 100644 --- a/continew-admin-ui/src/components/message-box/locale/en-US.ts +++ b/continew-admin-ui/src/components/message-box/locale/en-US.ts @@ -8,6 +8,5 @@ export default { 'messageBox.noContent': 'No Content', 'messageBox.switchRoles': 'Switch Roles', 'messageBox.userCenter': 'User Center', - 'messageBox.userSettings': 'User Settings', 'messageBox.logout': 'Logout', }; diff --git a/continew-admin-ui/src/components/message-box/locale/zh-CN.ts b/continew-admin-ui/src/components/message-box/locale/zh-CN.ts index 9ffcb6ba..cedf8164 100644 --- a/continew-admin-ui/src/components/message-box/locale/zh-CN.ts +++ b/continew-admin-ui/src/components/message-box/locale/zh-CN.ts @@ -7,7 +7,6 @@ export default { 'messageBox.viewMore': '查看更多', 'messageBox.noContent': '暂无内容', 'messageBox.switchRoles': '切换角色', - 'messageBox.userCenter': '用户中心', - 'messageBox.userSettings': '用户设置', + 'messageBox.userCenter': '个人中心', 'messageBox.logout': '退出登录', }; diff --git a/continew-admin-ui/src/components/navbar/index.vue b/continew-admin-ui/src/components/navbar/index.vue index 92b5103b..9d2d2db1 100644 --- a/continew-admin-ui/src/components/navbar/index.vue +++ b/continew-admin-ui/src/components/navbar/index.vue @@ -144,7 +144,7 @@ :size="32" :style="{ marginRight: '8px', cursor: 'pointer' }" > - avatar + avatar