From 81358e6d7c460ac3124a6cf4b73d3b0a9bf453ba Mon Sep 17 00:00:00 2001 From: ah-max150 Date: Tue, 25 Aug 2026 00:00:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=AD=98=E5=82=A8=E4=B8=8E=E4=B8=8A=E4=BC=A0=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=EF=BC=8C=E8=A7=A3=E8=80=A6=20OSS=20=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E3=80=81=E5=AD=98=E5=82=A8=E7=B1=BB=E5=9E=8B=E4=B8=8E?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E4=B8=8A=E4=BC=A0=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ecom-dmb-backend/.gitignore | 1 + .../sql/design/org/org_company_design.sql | 7 +- .../V20260917__org_company_logo_image_id.sql | 5 + .../ec/shop/controller/ShopController.java | 15 - .../modules/ec/shop/service/ShopService.java | 10 - .../ec/shop/service/impl/ShopServiceImpl.java | 18 -- .../company/controller/CompanyController.java | 14 - .../company/model/dto/CreateCompanyDTO.java | 2 + .../company/model/dto/UpdateCompanyDTO.java | 2 + .../org/company/model/po/CompanyPO.java | 8 +- .../org/company/model/vo/CompanyVO.java | 7 +- .../org/company/service/CompanyService.java | 9 - .../service/impl/CompanyServiceImpl.java | 53 ++- .../controller/SysConfigController.java | 28 ++ .../sys/config/model/vo/SysConfigVO.java | 25 ++ .../sys/config/service/SysConfigService.java | 12 + .../service/impl/SysConfigServiceImpl.java | 30 ++ .../modules/sys/file/config/OssConfig.java | 15 +- .../sys/file/config/SysFileProperties.java | 28 ++ .../file/controller/SysFileController.java | 8 +- .../modules/sys/file/service/OssService.java | 3 + .../sys/file/service/impl/OssServiceImpl.java | 7 +- .../file/service/impl/SysFileServiceImpl.java | 94 +++--- .../sys/file/support/FileStorageSupport.java | 128 ++++++++ .../sys/user/controller/UserController.java | 14 - .../modules/sys/user/service/UserService.java | 9 - .../user/service/impl/UserServiceImpl.java | 36 --- .../ecomdmbbackend/utils/SysFileUtils.java | 22 +- .../src/main/resources/application-dev.yml | 11 +- .../src/main/resources/application-pre.yml | 7 +- .../src/main/resources/application-prod.yml | 7 +- .../src/main/resources/application-test.yml | 7 +- .../src/main/resources/application.yml | 10 +- .../apps/web-antd/src/api/company/apis.ts | 13 - .../src/api/company/types/types.vo.ts | 2 + .../apps/web-antd/src/api/ec/shop/apis.ts | 13 - .../apps/web-antd/src/api/sys/config/apis.ts | 11 + .../apps/web-antd/src/api/sys/config/index.ts | 2 + .../apps/web-antd/src/api/sys/config/types.ts | 17 + .../apps/web-antd/src/api/sys/file/apis.ts | 8 +- .../apps/web-antd/src/api/sys/user/apis.ts | 13 - .../src/components/ImportTaskDropdown.vue | 5 +- .../apps/web-antd/src/layouts/basic.vue | 5 +- .../apps/web-antd/src/router/guard.ts | 8 + .../apps/web-antd/src/store/app-config.ts | 66 ++++ .../apps/web-antd/src/store/auth.ts | 16 + .../apps/web-antd/src/store/import-task.ts | 5 + .../apps/web-antd/src/store/index.ts | 1 + .../apps/web-antd/src/utils/file-upload.ts | 72 ----- .../apps/web-antd/src/utils/oss-upload.ts | 301 +----------------- .../apps/web-antd/src/utils/upload-file.ts | 206 ++++++++++++ .../ad-fee/components/ImportTaskWizard.vue | 4 +- .../order/components/ImportTaskWizard.vue | 4 +- .../list/components/ProductFormModal.vue | 6 +- .../ec/shop/list/components/ShopFormModal.vue | 4 +- .../company/components/CompanyFormModal.vue | 10 +- .../web-antd/src/views/sys/profile/index.vue | 4 +- 57 files changed, 776 insertions(+), 672 deletions(-) create mode 100644 ecom-dmb-backend/sql/migration/V20260917__org_company_logo_image_id.sql create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/controller/SysConfigController.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/model/vo/SysConfigVO.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/SysConfigService.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/impl/SysConfigServiceImpl.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/SysFileProperties.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/support/FileStorageSupport.java create mode 100644 ecom-dmb-frontend/apps/web-antd/src/api/sys/config/apis.ts create mode 100644 ecom-dmb-frontend/apps/web-antd/src/api/sys/config/index.ts create mode 100644 ecom-dmb-frontend/apps/web-antd/src/api/sys/config/types.ts create mode 100644 ecom-dmb-frontend/apps/web-antd/src/store/app-config.ts delete mode 100644 ecom-dmb-frontend/apps/web-antd/src/utils/file-upload.ts create mode 100644 ecom-dmb-frontend/apps/web-antd/src/utils/upload-file.ts diff --git a/ecom-dmb-backend/.gitignore b/ecom-dmb-backend/.gitignore index 87317fbd..023729df 100644 --- a/ecom-dmb-backend/.gitignore +++ b/ecom-dmb-backend/.gitignore @@ -1,6 +1,7 @@ HELP.md target/ temp/ +uploads/ .mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/ diff --git a/ecom-dmb-backend/sql/design/org/org_company_design.sql b/ecom-dmb-backend/sql/design/org/org_company_design.sql index f7183f21..1eee7bfc 100644 --- a/ecom-dmb-backend/sql/design/org/org_company_design.sql +++ b/ecom-dmb-backend/sql/design/org/org_company_design.sql @@ -15,7 +15,8 @@ CREATE TABLE `org_company` ( `owner_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司拥有者姓名', `owner_phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司拥有者电话', `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱', - `logo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'logo URL(冗余字段)', + `logo_image_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT 'logo图片ID', + `logo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'logo URL(冗余字段;有 logo_image_id 时查询覆盖)', `status` int NOT NULL DEFAULT 1 COMMENT '状态 (0-禁用 1-正常)', `expire_time` timestamp NULL DEFAULT NULL COMMENT '订阅到期时间', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', @@ -25,8 +26,10 @@ CREATE TABLE `org_company` ( PRIMARY KEY (`id`) USING BTREE, INDEX `fk_org_company_creator`(`create_by` ASC) USING BTREE, INDEX `fk_org_company_updater`(`update_by` ASC) USING BTREE, + INDEX `fk_org_company_logo_image`(`logo_image_id` ASC) USING BTREE, CONSTRAINT `fk_org_company_creator` FOREIGN KEY (`create_by`) REFERENCES `sys_user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, - CONSTRAINT `fk_org_company_updater` FOREIGN KEY (`update_by`) REFERENCES `sys_user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE + CONSTRAINT `fk_org_company_updater` FOREIGN KEY (`update_by`) REFERENCES `sys_user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `fk_org_company_logo_image` FOREIGN KEY (`logo_image_id`) REFERENCES `sys_file` (`id`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '系统模块-公司表' -- ======================================== diff --git a/ecom-dmb-backend/sql/migration/V20260917__org_company_logo_image_id.sql b/ecom-dmb-backend/sql/migration/V20260917__org_company_logo_image_id.sql new file mode 100644 index 00000000..6077d986 --- /dev/null +++ b/ecom-dmb-backend/sql/migration/V20260917__org_company_logo_image_id.sql @@ -0,0 +1,5 @@ +-- 公司 Logo 对齐店铺:增加 logo_image_id,保留 logo 为访问 URL 冗余 +ALTER TABLE `org_company` + ADD COLUMN `logo_image_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT 'logo图片ID' AFTER `email`, + ADD INDEX `fk_org_company_logo_image` (`logo_image_id` ASC), + ADD CONSTRAINT `fk_org_company_logo_image` FOREIGN KEY (`logo_image_id`) REFERENCES `sys_file` (`id`) ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/controller/ShopController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/controller/ShopController.java index 8814f47a..6be03357 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/controller/ShopController.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/controller/ShopController.java @@ -2,7 +2,6 @@ package top.xyquan.ecomdmbbackend.modules.ec.shop.controller; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission; import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO; @@ -15,8 +14,6 @@ import top.xyquan.ecomdmbbackend.modules.ec.shop.model.dto.UpdateShopDTO; import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductVO; import top.xyquan.ecomdmbbackend.modules.ec.shop.model.vo.ShopVO; import top.xyquan.ecomdmbbackend.modules.ec.shop.service.ShopService; -import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO; -import top.xyquan.ecomdmbbackend.utils.FileUploadUtils; /** * 店铺管理控制器 @@ -69,18 +66,6 @@ public class ShopController { return ApiResponse.success("获取成功", vo); } - @PostMapping("/upload-logo") - @RequirePermission(value = "ec:shop:create", description = "上传店铺Logo", allowSuper = true) - public ApiResponse uploadLogo(@RequestParam("file") MultipartFile file) { - // 检查是否为图片文件 - if (!FileUploadUtils.isImageFile(file.getOriginalFilename())) { - return ApiResponse.fail("只能上传图片文件"); - } - - SysFileUploadVO fileVO = shopService.uploadLogo(file); - return ApiResponse.success("Logo上传成功", fileVO); - } - @PostMapping("/products/list") @RequirePermission(value = "ec:shop:read", description = "查询店铺商品列表", allowSuper = true) public ApiResponse> getShopProductPage( diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/ShopService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/ShopService.java index 6ec1e619..0068bf31 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/ShopService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/ShopService.java @@ -1,7 +1,6 @@ package top.xyquan.ecomdmbbackend.modules.ec.shop.service; import com.baomidou.mybatisplus.extension.service.IService; -import org.springframework.web.multipart.MultipartFile; import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO; import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO; import top.xyquan.ecomdmbbackend.modules.ec.shop.model.dto.CreateShopDTO; @@ -11,7 +10,6 @@ import top.xyquan.ecomdmbbackend.modules.ec.shop.model.dto.UpdateShopDTO; import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO; import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductVO; import top.xyquan.ecomdmbbackend.modules.ec.shop.model.vo.ShopVO; -import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO; /** * 店铺服务接口 @@ -58,14 +56,6 @@ public interface ShopService extends IService { */ ShopVO getShopDetail(Long id); - /** - * 上传店铺Logo - * - * @param file Logo文件 - * @return 上传后的图片信息 - */ - SysFileUploadVO uploadLogo(MultipartFile file); - /** * 分页获取店铺商品信息 * diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/impl/ShopServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/impl/ShopServiceImpl.java index 9ba3b883..d34e8ffe 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/impl/ShopServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ec/shop/service/impl/ShopServiceImpl.java @@ -31,12 +31,9 @@ import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductVO; import top.xyquan.ecomdmbbackend.modules.ec.shop.model.vo.ShopVO; import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService; import top.xyquan.ecomdmbbackend.modules.ec.shop.service.ShopService; -import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO; import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService; import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; -import org.springframework.web.multipart.MultipartFile; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -223,21 +220,6 @@ public class ShopServiceImpl extends ServiceImpl implements return PageResultVO.create(voList, pageResult.getTotal(), pageQueryDTO.getPage(), pageQueryDTO.getPageSize()); } - /** - * 上传Logo - * @param file 文件 - * @return Logo图片VO - */ - @Override - public SysFileUploadVO uploadLogo(MultipartFile file) { - // 上传Logo到系统文件模块(支持哈希校验和跨公司共享),设置文件分类为logo - List files = sysFileService.batchUploadFiles(Collections.singletonList(file), "logo"); - if (files.isEmpty()) { - throw new BusinessException("Logo上传失败"); - } - return files.get(0); - } - /** * 转换为VO(单条;创建/更新等场景) */ diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/controller/CompanyController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/controller/CompanyController.java index 3e2269f3..6d4b772f 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/controller/CompanyController.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/controller/CompanyController.java @@ -2,7 +2,6 @@ package top.xyquan.ecomdmbbackend.modules.org.company.controller; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission; import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO; import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO; @@ -12,7 +11,6 @@ import top.xyquan.ecomdmbbackend.modules.org.company.model.dto.UpdateCompanyDTO; import top.xyquan.ecomdmbbackend.modules.org.company.model.dto.CompanyFilterDTO; import top.xyquan.ecomdmbbackend.modules.org.company.model.vo.CompanyVO; import top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService; -import top.xyquan.ecomdmbbackend.utils.FileUploadUtils; /** * 公司管理控制器 @@ -57,16 +55,4 @@ public class CompanyController { PageResultVO pageResult = companyService.getCompanyPage(pageQuery); return ApiResponse.success("获取公司列表成功", pageResult); } - - @PostMapping("/upload-logo") - @RequirePermission(value = "org:company:update", description = "上传公司Logo", allowSuper = true) - public ApiResponse uploadLogo(@RequestParam("file") MultipartFile file) { - // 检查是否为图片文件 - if (!FileUploadUtils.isImageFile(file.getOriginalFilename())) { - return ApiResponse.fail("只能上传图片文件"); - } - - CompanyVO companyVO = companyService.uploadLogo(file); - return ApiResponse.success("公司Logo上传成功", companyVO); - } } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/CreateCompanyDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/CreateCompanyDTO.java index df4a126c..ba8c09ea 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/CreateCompanyDTO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/CreateCompanyDTO.java @@ -42,6 +42,8 @@ public class CreateCompanyDTO { /** * logo图片ID */ + private Long logoImageId; + /** * 状态 (0-禁用 1-正常) */ diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/UpdateCompanyDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/UpdateCompanyDTO.java index cf0389d9..abc84336 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/UpdateCompanyDTO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/dto/UpdateCompanyDTO.java @@ -30,6 +30,8 @@ public class UpdateCompanyDTO { /** * logo图片ID */ + private Long logoImageId; + /** * 状态 (0-禁用 1-正常) */ diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/po/CompanyPO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/po/CompanyPO.java index 94ea75b9..001cc2b2 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/po/CompanyPO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/po/CompanyPO.java @@ -57,7 +57,13 @@ public class CompanyPO { private String email; /** - * logo URL(冗余字段) + * logo 图片 ID(sys_file) + */ + @TableField("logo_image_id") + private Long logoImageId; + + /** + * logo URL(冗余字段;有 logo_image_id 时查询可覆盖) */ @TableField("logo") private String logo; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/vo/CompanyVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/vo/CompanyVO.java index 8976e5a2..fe081c1c 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/vo/CompanyVO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/model/vo/CompanyVO.java @@ -46,7 +46,12 @@ public class CompanyVO { private String email; /** - * logo + * logo 图片 ID + */ + private Long logoImageId; + + /** + * logo 访问 URL */ private String logo; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/CompanyService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/CompanyService.java index 40925335..44a6ea51 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/CompanyService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/CompanyService.java @@ -1,7 +1,6 @@ package top.xyquan.ecomdmbbackend.modules.org.company.service; import com.baomidou.mybatisplus.extension.service.IService; -import org.springframework.web.multipart.MultipartFile; import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO; import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO; import top.xyquan.ecomdmbbackend.modules.org.company.model.dto.CreateCompanyDTO; @@ -55,14 +54,6 @@ public interface CompanyService extends IService { */ PageResultVO getCompanyPage(PageQueryDTO pageQuery); - /** - * 上传公司Logo - * - * @param file Logo文件 - * @return 更新后的公司信息 - */ - CompanyVO uploadLogo(MultipartFile file); - /** * 初始化公司菜单 * diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/impl/CompanyServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/impl/CompanyServiceImpl.java index 76d2c028..1e1b4e3f 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/impl/CompanyServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/company/service/impl/CompanyServiceImpl.java @@ -18,7 +18,6 @@ import top.xyquan.ecomdmbbackend.modules.org.company.model.po.CompanyPO; import top.xyquan.ecomdmbbackend.modules.org.company.model.vo.CompanyVO; import top.xyquan.ecomdmbbackend.modules.org.company.service.CompanyService; import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService; -import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO; import top.xyquan.ecomdmbbackend.modules.org.company.menu.service.CompanyMenuService; import top.xyquan.ecomdmbbackend.modules.org.department.service.DepartmentService; import top.xyquan.ecomdmbbackend.modules.org.position.service.PositionService; @@ -37,7 +36,6 @@ import top.xyquan.ecomdmbbackend.modules.sys.license.LicenseInfo; import top.xyquan.ecomdmbbackend.modules.sys.license.LicenseValidator; import org.springframework.context.annotation.Lazy; import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; -import org.springframework.web.multipart.MultipartFile; import org.springframework.transaction.annotation.Transactional; import org.springframework.security.crypto.password.PasswordEncoder; @@ -119,6 +117,12 @@ public class CompanyServiceImpl extends ServiceImpl im CompanyPO company = new CompanyPO(); BeanCopyUtils.copyProperties(createCompanyDTO, company); + // logo:有 fileId 时同步冗余 URL + if (createCompanyDTO.getLogoImageId() != null) { + company.setLogoImageId(createCompanyDTO.getLogoImageId()); + company.setLogo(sysFileService.getFileAccessUrl(createCompanyDTO.getLogoImageId())); + } + // 设置默认值 if (company.getStatus() == null) { company.setStatus(1); @@ -209,6 +213,12 @@ public class CompanyServiceImpl extends ServiceImpl im // 更新公司 CompanyPO company = new CompanyPO(); BeanCopyUtils.copyProperties(updateCompanyDTO, company); + + // logo:有 fileId 时同步冗余 URL + if (updateCompanyDTO.getLogoImageId() != null) { + company.setLogoImageId(updateCompanyDTO.getLogoImageId()); + company.setLogo(sysFileService.getFileAccessUrl(updateCompanyDTO.getLogoImageId())); + } // 设置公司ID(必须在updateById之前设置) company.setId(id); @@ -278,37 +288,6 @@ public class CompanyServiceImpl extends ServiceImpl im return PageResultVO.create(records, pageResult.getTotal(), pageQuery.getPage(), pageQuery.getPageSize()); } - /** - * 上传公司Logo - * @param file 文件 - * @return 公司VO - */ - @Override - public CompanyVO uploadLogo(MultipartFile file) { - // 获取当前用户所属公司ID - Long companyId = currentUserContext.getCurrentCompanyId(); - if (companyId == null) { - throw new BusinessException("用户未关联公司"); - } - - // 查询公司信息 - CompanyPO company = this.getById(companyId); - if (company == null) { - throw new BusinessException("公司不存在"); - } - - // 上传Logo到媒体模块(支持哈希校验和共享) - List uploaded = sysFileService.batchUploadFiles(java.util.List.of(file), "logo", company.getId()); - String logoUrl = uploaded.isEmpty() ? null : sysFileService.getFileAccessUrl(uploaded.get(0).getId()); - - // 更新公司Logo信息 - company.setLogo(logoUrl); - this.updateById(company); - - // 转换为VO并返回 - return convertToVO(company); - } - /** * 初始化公司菜单 * @param companyId 公司ID @@ -437,6 +416,14 @@ public class CompanyServiceImpl extends ServiceImpl im CompanyVO companyVO = new CompanyVO(); BeanCopyUtils.copyProperties(company, companyVO); + // 有系统文件 id 则覆盖 logo URL + if (company.getLogoImageId() != null) { + String accessUrl = sysFileService.getFileAccessUrl(company.getLogoImageId()); + if (accessUrl != null && !accessUrl.isBlank()) { + companyVO.setLogo(accessUrl); + } + } + // 计算是否过期 if (company.getExpireTime() != null) { companyVO.setExpired(company.getExpireTime().isBefore(LocalDateTime.now())); diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/controller/SysConfigController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/controller/SysConfigController.java new file mode 100644 index 00000000..5deac063 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/controller/SysConfigController.java @@ -0,0 +1,28 @@ +package top.xyquan.ecomdmbbackend.modules.sys.config.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import top.xyquan.ecomdmbbackend.common.response.ApiResponse; +import top.xyquan.ecomdmbbackend.modules.sys.config.model.vo.SysConfigVO; +import top.xyquan.ecomdmbbackend.modules.sys.config.service.SysConfigService; + +/** + * 系统配置接口(需登录)。 + */ +@RestController +@RequestMapping("/api/sys/config") +public class SysConfigController { + + private final SysConfigService sysConfigService; + + public SysConfigController(SysConfigService sysConfigService) { + this.sysConfigService = sysConfigService; + } + + /** 获取前端可用的系统配置(文件存储/上传模式等) */ + @GetMapping + public ApiResponse getConfig() { + return ApiResponse.success("获取系统配置成功", sysConfigService.getConfig()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/model/vo/SysConfigVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/model/vo/SysConfigVO.java new file mode 100644 index 00000000..6df56feb --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/model/vo/SysConfigVO.java @@ -0,0 +1,25 @@ +package top.xyquan.ecomdmbbackend.modules.sys.config.model.vo; + +import lombok.Builder; +import lombok.Data; + +/** + * 系统配置(仅下发前端所需、不含敏感凭证)。 + */ +@Data +@Builder +public class SysConfigVO { + + private FileConfig file; + + @Data + @Builder + public static class FileConfig { + /** 存储方式:local / oss */ + private String storageType; + /** 上传方式:multipart(传后端)/ direct(直传对象存储) */ + private String uploadMode; + /** 单文件大小上限(MB) */ + private Integer maxFileSizeMb; + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/SysConfigService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/SysConfigService.java new file mode 100644 index 00000000..1ef89c54 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/SysConfigService.java @@ -0,0 +1,12 @@ +package top.xyquan.ecomdmbbackend.modules.sys.config.service; + +import top.xyquan.ecomdmbbackend.modules.sys.config.model.vo.SysConfigVO; + +/** + * 系统配置查询(登录后下发前端)。 + */ +public interface SysConfigService { + + /** 获取可公开给前端的系统配置 */ + SysConfigVO getConfig(); +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/impl/SysConfigServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/impl/SysConfigServiceImpl.java new file mode 100644 index 00000000..8b75486b --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/config/service/impl/SysConfigServiceImpl.java @@ -0,0 +1,30 @@ +package top.xyquan.ecomdmbbackend.modules.sys.config.service.impl; + +import org.springframework.stereotype.Service; +import top.xyquan.ecomdmbbackend.modules.sys.config.model.vo.SysConfigVO; +import top.xyquan.ecomdmbbackend.modules.sys.config.service.SysConfigService; +import top.xyquan.ecomdmbbackend.modules.sys.file.support.FileStorageSupport; + +/** + * 系统配置聚合:文件存储/上传模式等。 + */ +@Service +public class SysConfigServiceImpl implements SysConfigService { + + private final FileStorageSupport fileStorageSupport; + + public SysConfigServiceImpl(FileStorageSupport fileStorageSupport) { + this.fileStorageSupport = fileStorageSupport; + } + + @Override + public SysConfigVO getConfig() { + return SysConfigVO.builder() + .file(SysConfigVO.FileConfig.builder() + .storageType(fileStorageSupport.storageType()) + .uploadMode(fileStorageSupport.uploadMode()) + .maxFileSizeMb(fileStorageSupport.maxSizeMb()) + .build()) + .build(); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/OssConfig.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/OssConfig.java index 0c2d6ca6..70f55826 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/OssConfig.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/OssConfig.java @@ -10,11 +10,17 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** - * 阿里云OSS配置 + * 阿里云 OSS 客户端配置。 + *

+ * enabled 仅控制是否创建 SDK 客户端;文件落盘位置与前端上传方式见 sys.file。 */ @Configuration public class OssConfig { + /** 是否创建 OSS SDK 客户端(需配合有效凭证) */ + @Value("${oss.enabled:false}") + private boolean enabled; + @Value("${oss.endpoint:}") private String endpoint; @@ -32,6 +38,9 @@ public class OssConfig { @Bean public OSS ossClient() { + if (!enabled) { + return null; + } if (endpoint == null || endpoint.isEmpty() || accessKeyId == null || accessKeyId.isEmpty() || accessKeySecret == null || accessKeySecret.isEmpty()) { @@ -62,6 +71,10 @@ public class OssConfig { .build(); } + public boolean isEnabled() { + return enabled; + } + public String getEndpoint() { return endpoint; } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/SysFileProperties.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/SysFileProperties.java new file mode 100644 index 00000000..8022aae3 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/config/SysFileProperties.java @@ -0,0 +1,28 @@ +package top.xyquan.ecomdmbbackend.modules.sys.file.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * 系统文件模块配置(sys.file)。 + *

+ * storage-type 决定物理存储位置;upload-mode 决定前端上传路径,二者独立配置。 + */ +@Data +@Component +@ConfigurationProperties(prefix = "sys.file") +public class SysFileProperties { + + /** 存储方式:local | oss */ + private String storageType = "local"; + + /** 前端上传方式:multipart(经后端)| direct(直传对象存储) */ + private String uploadMode = "multipart"; + + /** 本地文件访问域名(storage-type=local 时使用) */ + private String accessDomain = ""; + + /** 单文件大小上限(MB) */ + private int maxSizeMb = 50; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/controller/SysFileController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/controller/SysFileController.java index 897db9a3..255a424c 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/controller/SysFileController.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/controller/SysFileController.java @@ -32,8 +32,12 @@ public class SysFileController { } @PostMapping("/batch-upload") - public ApiResponse> batchUploadFiles(@RequestParam("files") List files) { - List result = sysFileService.batchUploadFiles(files); + public ApiResponse> batchUploadFiles( + @RequestParam("files") List files, + @RequestParam(value = "category", required = false) String category) { + List result = category == null || category.isBlank() + ? sysFileService.batchUploadFiles(files) + : sysFileService.batchUploadFiles(files, category); return ApiResponse.success("文件批量上传成功", result); } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/OssService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/OssService.java index ecfe28f8..e6a3d2d5 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/OssService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/OssService.java @@ -9,6 +9,9 @@ import java.io.InputStream; */ public interface OssService { + /** OSS 是否可用(开关打开且客户端已创建) */ + boolean isAvailable(); + /** * 生成上传Header签名(V1签名方式,HMAC-SHA1) * diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/OssServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/OssServiceImpl.java index 985077a2..095bb1ab 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/OssServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/OssServiceImpl.java @@ -25,7 +25,12 @@ public class OssServiceImpl implements OssService { public OssServiceImpl(OssConfig ossConfig) { this.ossConfig = ossConfig; - this.ossClient = ossConfig.ossClient(); + this.ossClient = ossConfig.isEnabled() ? ossConfig.ossClient() : null; + } + + @Override + public boolean isAvailable() { + return ossConfig.isEnabled() && ossClient != null; } @Override diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/SysFileServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/SysFileServiceImpl.java index d9e72c5f..7b4bc240 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/SysFileServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/service/impl/SysFileServiceImpl.java @@ -23,6 +23,7 @@ import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO; import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileVO; import top.xyquan.ecomdmbbackend.modules.sys.file.service.OssService; import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService; +import top.xyquan.ecomdmbbackend.modules.sys.file.support.FileStorageSupport; import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; import top.xyquan.ecomdmbbackend.utils.FileHashUtils; import top.xyquan.ecomdmbbackend.utils.SysFileUtils; @@ -41,10 +42,14 @@ public class SysFileServiceImpl extends ServiceImpl im private final CurrentUserContext currentUserContext; private final OssService ossService; + private final FileStorageSupport fileStorageSupport; - public SysFileServiceImpl(CurrentUserContext currentUserContext, OssService ossService) { + public SysFileServiceImpl(CurrentUserContext currentUserContext, + OssService ossService, + FileStorageSupport fileStorageSupport) { this.currentUserContext = currentUserContext; this.ossService = ossService; + this.fileStorageSupport = fileStorageSupport; } @Override @@ -57,16 +62,15 @@ public class SysFileServiceImpl extends ServiceImpl im Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null; Long userId = currentUserContext.getCurrentUserId(); List result = new ArrayList<>(); + String storageType = fileStorageSupport.storageType(); for (MultipartFile file : files) { if (file.isEmpty()) { continue; } - // 计算文件哈希 String fileHash = FileHashUtils.calculateFileHash(file); - - // 检查当前公司是否已有相同哈希的文件 + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SysFilePO::getCompanyId, companyId) .eq(SysFilePO::getFileHash, fileHash) @@ -75,17 +79,13 @@ public class SysFileServiceImpl extends ServiceImpl im SysFilePO existingFile = this.getOne(wrapper); if (existingFile != null) { - // 当前公司已有相同文件,先验证文件是否真的存在可访问 if (verifyFileExists(existingFile)) { result.add(convertToUploadVO(existingFile)); continue; - } else { - // 文件不存在,删除无效记录,继续处理 - this.removeById(existingFile.getId()); } + this.removeById(existingFile.getId()); } - // 检查是否有其他公司已有相同哈希的文件 LambdaQueryWrapper otherCompanyWrapper = new LambdaQueryWrapper<>(); otherCompanyWrapper.eq(SysFilePO::getFileHash, fileHash) .isNull(SysFilePO::getDeleteTime) @@ -93,30 +93,28 @@ public class SysFileServiceImpl extends ServiceImpl im SysFilePO otherCompanyFile = this.getOne(otherCompanyWrapper); String storagePath = null; - String storageType = "local"; // 上传接口始终使用本地存储 + String resolvedStorageType = storageType; if (otherCompanyFile != null) { - // 其他公司已有相同文件,先验证文件是否真的存在可访问 if (verifyFileExists(otherCompanyFile)) { - // 文件存在,复制存储路径和类型 storagePath = otherCompanyFile.getStoragePath(); - storageType = otherCompanyFile.getStorageType(); - - // 如果是本地存储,需要重新上传到当前公司的存储路径 - if ("local".equals(storageType)) { + resolvedStorageType = otherCompanyFile.getStorageType(); + + // 本地存储不可跨公司共享磁盘路径,需按当前模式重新落盘 + if (FileStorageSupport.STORAGE_LOCAL.equals(resolvedStorageType) + || (FileStorageSupport.STORAGE_LOCAL.equals(storageType) + && !FileStorageSupport.STORAGE_OSS.equals(resolvedStorageType))) { String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash); - storagePath = "./" + relativePath; - saveFileToLocal(storagePath, file); + storagePath = fileStorageSupport.persist(relativePath, file); + resolvedStorageType = storageType; } - // 如果是OSS存储,直接使用相同的存储路径(OSS文件可以跨公司共享) - - // 创建当前公司的文件记录 + SysFilePO sysFile = new SysFilePO(); BeanCopyUtils.copyProperties(otherCompanyFile, sysFile); sysFile.setId(null); sysFile.setCompanyId(companyId); sysFile.setStoragePath(storagePath); - sysFile.setStorageType(storageType); + sysFile.setStorageType(resolvedStorageType); sysFile.setFilename(file.getOriginalFilename()); sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase()); sysFile.setMimeType(file.getContentType()); @@ -128,22 +126,16 @@ public class SysFileServiceImpl extends ServiceImpl im sysFile.setUpdateBy(null); sysFile.setDeleteTime(null); sysFile.setDeleteBy(null); - + this.save(sysFile); result.add(convertToUploadVO(sysFile)); - } else { - // 文件不存在,删除无效记录,继续处理(当作新文件处理) } } - - // 如果没有找到有效的其他公司文件,或者文件不存在,则作为新文件处理 + if (storagePath == null) { - // 没有任何公司有相同文件,生成新的存储路径并上传(仅本地存储) String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash); - storagePath = "./" + relativePath; - saveFileToLocal(storagePath, file); - - // 创建新记录 + storagePath = fileStorageSupport.persist(relativePath, file); + SysFilePO sysFile = new SysFilePO(); sysFile.setCompanyId(companyId); sysFile.setStorageType(storageType); @@ -152,8 +144,8 @@ public class SysFileServiceImpl extends ServiceImpl im sysFile.setFileFormat(getFileExtension(file.getOriginalFilename()).toLowerCase()); sysFile.setMimeType(file.getContentType()); sysFile.setFileSize(file.getSize()); - sysFile.setFileHash(fileHash); - sysFile.setUploadBy(userId); + sysFile.setFileHash(fileHash); + sysFile.setUploadBy(userId); this.save(sysFile); result.add(convertToUploadVO(sysFile)); @@ -178,10 +170,10 @@ public class SysFileServiceImpl extends ServiceImpl im Long companyId = overrideCompanyId != null ? overrideCompanyId : (currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null); Long userId = currentUserContext.getCurrentUserId(); List result = new ArrayList<>(); + String storageType = fileStorageSupport.storageType(); for (MultipartFile file : files) { if (file.isEmpty()) { continue; } String fileHash = FileHashUtils.calculateFileHash(file); - // 查询当前公司是否已有相同哈希的文件 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (companyId != null) { wrapper.eq(SysFilePO::getCompanyId, companyId); @@ -196,9 +188,8 @@ public class SysFileServiceImpl extends ServiceImpl im result.add(convertToUploadVO(existingFile)); continue; } - String storageType = "local"; - String storagePath = "./" + generateStoragePath(file.getOriginalFilename(), fileHash); - saveFileToLocal(storagePath, file); + String relativePath = generateStoragePath(file.getOriginalFilename(), fileHash); + String storagePath = fileStorageSupport.persist(relativePath, file); SysFilePO sysFile = new SysFilePO(); sysFile.setCompanyId(companyId); sysFile.setStorageType(storageType); @@ -220,6 +211,10 @@ public class SysFileServiceImpl extends ServiceImpl im @Override public BatchSignUrlVO batchSignUrl(BatchSignUrlDTO dto) { + if (!fileStorageSupport.isDirectUpload()) { + throw new BusinessException("当前 upload-mode 非 direct,请使用 multipart 上传接口"); + } + fileStorageSupport.ensureOssAvailable(); Long companyId = currentUserContext.getCurrentCompanyId() != null ? currentUserContext.getCurrentCompanyId().longValue() : null; List fileSignInfos = new ArrayList<>(); @@ -328,6 +323,10 @@ public class SysFileServiceImpl extends ServiceImpl im @Override @Transactional public List batchCreateOssFiles(BatchCreateOssFileDTO dto) { + if (!fileStorageSupport.isOssStorage()) { + throw new BusinessException("当前 storage-type 非 oss,请使用 multipart 上传接口"); + } + fileStorageSupport.ensureOssAvailable(); if (dto.getFiles() == null || dto.getFiles().isEmpty()) { throw new BusinessException("文件列表不能为空"); } @@ -431,19 +430,6 @@ public class SysFileServiceImpl extends ServiceImpl im } } - /** - * 保存文件到本地存储 - */ - private void saveFileToLocal(String storagePath, MultipartFile file) { - try { - java.nio.file.Path path = java.nio.file.Paths.get(storagePath); - java.nio.file.Files.createDirectories(path.getParent()); - java.nio.file.Files.copy(file.getInputStream(), path); - } catch (Exception e) { - throw new BusinessException("文件保存到本地失败: " + e.getMessage()); - } - } - /** * 获取文件扩展名 */ @@ -470,11 +456,7 @@ public class SysFileServiceImpl extends ServiceImpl im vo.setCategory(sysFile.getCategory()); vo.setTags(sysFile.getTags()); vo.setRemark(sysFile.getRemark()); - - // 如果是OSS存储,生成访问URL - if ("oss".equals(sysFile.getStorageType())) { - vo.setAccessUrl(ossService.generateAccessUrl(sysFile.getStoragePath(), 60)); - } + vo.setAccessUrl(SysFileUtils.getFileAccessUrl(sysFile.getStoragePath(), sysFile.getStorageType())); return vo; } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/support/FileStorageSupport.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/support/FileStorageSupport.java new file mode 100644 index 00000000..f658c9f7 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/file/support/FileStorageSupport.java @@ -0,0 +1,128 @@ +package top.xyquan.ecomdmbbackend.modules.sys.file.support; + +import jakarta.annotation.PostConstruct; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; +import top.xyquan.ecomdmbbackend.common.exception.BusinessException; +import top.xyquan.ecomdmbbackend.modules.sys.file.config.SysFileProperties; +import top.xyquan.ecomdmbbackend.modules.sys.file.service.OssService; + +/** + * 文件存储/上传模式:读取 sys.file 配置,与 oss.enabled(仅控制 SDK 客户端)解耦。 + */ +@Component +public class FileStorageSupport { + + public static final String STORAGE_LOCAL = "local"; + public static final String STORAGE_OSS = "oss"; + /** 前端 multipart 传到后端 */ + public static final String UPLOAD_MULTIPART = "multipart"; + /** 前端直传对象存储 */ + public static final String UPLOAD_DIRECT = "direct"; + + private final SysFileProperties sysFileProperties; + private final OssService ossService; + + public FileStorageSupport(SysFileProperties sysFileProperties, OssService ossService) { + this.sysFileProperties = sysFileProperties; + this.ossService = ossService; + } + + @PostConstruct + void validateConfig() { + if (isDirectUpload() && !isOssStorage()) { + throw new IllegalStateException( + "sys.file.upload-mode=direct 时 storage-type 必须为 oss"); + } + if (isOssStorage() && !ossService.isAvailable()) { + throw new IllegalStateException( + "sys.file.storage-type=oss 但 OSS 客户端不可用,请配置 oss.enabled 与凭证"); + } + if (isDirectUpload() && !ossService.isAvailable()) { + throw new IllegalStateException( + "sys.file.upload-mode=direct 但 OSS 客户端不可用,请配置 oss.enabled 与凭证"); + } + } + + /** 落库 storage_type */ + public String storageType() { + return normalizeStorageType(sysFileProperties.getStorageType()); + } + + /** 下发给前端的上传方式 */ + public String uploadMode() { + return normalizeUploadMode(sysFileProperties.getUploadMode()); + } + + public boolean isOssStorage() { + return STORAGE_OSS.equals(storageType()); + } + + public boolean isDirectUpload() { + return UPLOAD_DIRECT.equals(uploadMode()); + } + + public int maxSizeMb() { + return sysFileProperties.getMaxSizeMb(); + } + + /** + * 写入物理存储并返回应写入 sys_file.storage_path 的值。 + * + * @param relativePath 统一相对路径,如 uploads/2026/03/xxx.png + */ + public String persist(String relativePath, MultipartFile file) { + if (isOssStorage()) { + ensureOssAvailable(); + try { + boolean ok = ossService.uploadFile(relativePath, file.getInputStream()); + if (!ok) { + throw new BusinessException("文件上传到对象存储失败"); + } + } catch (BusinessException e) { + throw e; + } catch (Exception e) { + throw new BusinessException("文件上传到对象存储失败: " + e.getMessage()); + } + return relativePath; + } + String localPath = "./" + relativePath; + try { + java.nio.file.Path path = java.nio.file.Paths.get(localPath); + java.nio.file.Files.createDirectories(path.getParent()); + java.nio.file.Files.copy(file.getInputStream(), path); + } catch (Exception e) { + throw new BusinessException("文件保存到本地失败: " + e.getMessage()); + } + return localPath; + } + + public void ensureOssAvailable() { + if (!ossService.isAvailable()) { + throw new BusinessException("对象存储未配置或不可用"); + } + } + + private static String normalizeStorageType(String raw) { + if (!StringUtils.hasText(raw)) { + return STORAGE_LOCAL; + } + String v = raw.trim().toLowerCase(); + if (STORAGE_OSS.equals(v)) { + return STORAGE_OSS; + } + return STORAGE_LOCAL; + } + + private static String normalizeUploadMode(String raw) { + if (!StringUtils.hasText(raw)) { + return UPLOAD_MULTIPART; + } + String v = raw.trim().toLowerCase(); + if (UPLOAD_DIRECT.equals(v)) { + return UPLOAD_DIRECT; + } + return UPLOAD_MULTIPART; + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/controller/UserController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/controller/UserController.java index 1741aa55..6be0e600 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/controller/UserController.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/controller/UserController.java @@ -2,7 +2,6 @@ package top.xyquan.ecomdmbbackend.modules.sys.user.controller; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission; import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO; import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO; @@ -15,7 +14,6 @@ import top.xyquan.ecomdmbbackend.modules.sys.user.model.vo.UserCompanySummaryVO; import top.xyquan.ecomdmbbackend.modules.sys.user.model.vo.UserVO; import top.xyquan.ecomdmbbackend.modules.sys.menu.service.UserMenuService; import top.xyquan.ecomdmbbackend.modules.sys.user.service.UserService; -import top.xyquan.ecomdmbbackend.utils.FileUploadUtils; import java.util.List; @@ -59,18 +57,6 @@ public class UserController { return ApiResponse.success("密码修改成功"); } - @PostMapping("/upload-avatar") - @RequirePermission(value = "sys:user:update", description = "上传用户头像", allowSuper = true) - public ApiResponse uploadAvatar(@RequestParam("file") MultipartFile file) { - // 检查是否为图片文件 - if (!FileUploadUtils.isImageFile(file.getOriginalFilename())) { - return ApiResponse.fail("只能上传图片文件"); - } - - UserVO userVO = userService.uploadAvatar(file); - return ApiResponse.success("头像上传成功", userVO); - } - @GetMapping("/menu") public ApiResponse> getUserMenus() { List userMenuVOS = userMenuService.getUserMenus(); diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/UserService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/UserService.java index 48ec7e8e..2490fad6 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/UserService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/UserService.java @@ -1,7 +1,6 @@ package top.xyquan.ecomdmbbackend.modules.sys.user.service; import com.baomidou.mybatisplus.extension.service.IService; -import org.springframework.web.multipart.MultipartFile; import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO; import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO; import top.xyquan.ecomdmbbackend.modules.sys.auth.model.dto.ChangePasswordDTO; @@ -68,14 +67,6 @@ public interface UserService extends IService { */ void changePassword(ChangePasswordDTO changePasswordDTO); - /** - * 上传用户头像 - * - * @param file 头像文件 - * @return 更新后的用户信息 - */ - UserVO uploadAvatar(MultipartFile file); - /** * 刷新访问令牌 * diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/impl/UserServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/impl/UserServiceImpl.java index 81279bf3..f546f878 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/impl/UserServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/sys/user/service/impl/UserServiceImpl.java @@ -26,12 +26,10 @@ import top.xyquan.ecomdmbbackend.modules.sys.user.model.vo.UserVO; import top.xyquan.ecomdmbbackend.modules.sys.user.service.UserService; import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; import top.xyquan.ecomdmbbackend.modules.sys.file.service.SysFileService; -import top.xyquan.ecomdmbbackend.modules.sys.file.model.vo.SysFileUploadVO; import top.xyquan.ecomdmbbackend.utils.JwtUtils; import top.xyquan.ecomdmbbackend.utils.SecurityUtils; import top.xyquan.ecomdmbbackend.utils.AccessTokenPayload; import top.xyquan.ecomdmbbackend.utils.RefreshTokenPayload; -import org.springframework.web.multipart.MultipartFile; import java.time.LocalDateTime; import java.util.List; @@ -292,40 +290,6 @@ public class UserServiceImpl extends ServiceImpl implements this.updateById(user); } - /** - * 上传头像 - * @param file 文件 - * @return 用户VO - */ - @Override - public UserVO uploadAvatar(MultipartFile file) { - // 从Security上下文获取用户ID - Long userId = SecurityUtils.getCurrentUserId(); - if (userId == null) { - throw new BusinessException("用户未登录"); - } - - // 查询用户信息 - UserPO user = this.getById(userId); - if (user == null) { - throw new BusinessException("用户不存在"); - } - - // 上传头像到媒体模块(支持哈希校验和共享) - List uploaded = sysFileService.batchUploadFiles(java.util.List.of(file), "avatar"); - String avatarUrl = uploaded.isEmpty() ? null : sysFileService.getFileAccessUrl(uploaded.get(0).getId()); - - // 更新用户头像信息 - user.setAvatar(avatarUrl); - this.updateById(user); - - // 转换为VO并返回 - UserVO userVO = new UserVO(); - BeanCopyUtils.copyProperties(user, userVO); - fillCompanyFromCurrentToken(userVO); - return userVO; - } - /** * 刷新令牌 * @param refreshTokenDTO 刷新令牌DTO diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/utils/SysFileUtils.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/utils/SysFileUtils.java index b2de2c81..15d8aee0 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/utils/SysFileUtils.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/utils/SysFileUtils.java @@ -1,9 +1,9 @@ package top.xyquan.ecomdmbbackend.utils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import top.xyquan.ecomdmbbackend.modules.sys.file.config.OssConfig; +import top.xyquan.ecomdmbbackend.modules.sys.file.config.SysFileProperties; import jakarta.annotation.PostConstruct; @@ -19,13 +19,13 @@ public class SysFileUtils { @Autowired private OssConfig ossConfig; - @Value("${file.access-domain:}") - private String fileAccessDomain; + @Autowired + private SysFileProperties sysFileProperties; @PostConstruct public void init() { staticOssConfig = ossConfig; - staticFileAccessDomain = fileAccessDomain; + staticFileAccessDomain = sysFileProperties.getAccessDomain(); } /** @@ -46,26 +46,22 @@ public class SysFileUtils { return null; } String domain = staticOssConfig.getDomain(); - // 确保域名不以/结尾,路径不以/开头 domain = domain.endsWith("/") ? domain.substring(0, domain.length() - 1) : domain; String path = storagePath.startsWith("/") ? storagePath : "/" + storagePath; return domain + path; } else { - // 本地存储:拼接文件访问域名和存储路径 + // 本地存储:拼接 sys.file.access-domain 与路径 if (staticFileAccessDomain == null || staticFileAccessDomain.isEmpty()) { return null; } - // 确保域名不以/结尾 - String domain = staticFileAccessDomain.endsWith("/") - ? staticFileAccessDomain.substring(0, staticFileAccessDomain.length() - 1) + String domain = staticFileAccessDomain.endsWith("/") + ? staticFileAccessDomain.substring(0, staticFileAccessDomain.length() - 1) : staticFileAccessDomain; - // 本地存储路径去掉 ./ 前缀 - String path = storagePath.startsWith("./") - ? storagePath.substring(2) + String path = storagePath.startsWith("./") + ? storagePath.substring(2) : storagePath; path = path.startsWith("/") ? path : "/" + path; return domain + path; } } } - diff --git a/ecom-dmb-backend/src/main/resources/application-dev.yml b/ecom-dmb-backend/src/main/resources/application-dev.yml index d939d67f..4032c5ef 100644 --- a/ecom-dmb-backend/src/main/resources/application-dev.yml +++ b/ecom-dmb-backend/src/main/resources/application-dev.yml @@ -31,13 +31,18 @@ mybatis-plus: # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl oss: - enabled: false + enabled: true access-key-id: LTAI5t8oyyor6wLrQasDUQA3 access-key-secret: U0d3BeKADTJUteSsT5gBtxzlbHoJFi domain: https://xyquan.oss-cn-guangzhou.aliyuncs.com -file: - access-domain: http://localhost:5666 +sys: + file: + # local 本地存储;oss 阿里云 OSS + # multipart 通过后端上传;direct 直接上传 + storage-type: oss + upload-mode: direct + access-domain: http://localhost:5666 ecom: license: diff --git a/ecom-dmb-backend/src/main/resources/application-pre.yml b/ecom-dmb-backend/src/main/resources/application-pre.yml index 103ecbd0..1b9cd467 100644 --- a/ecom-dmb-backend/src/main/resources/application-pre.yml +++ b/ecom-dmb-backend/src/main/resources/application-pre.yml @@ -32,8 +32,11 @@ oss: access-key-secret: U0d3BeKADTJUteSsT5gBtxzlbHoJFi domain: https://xyquan.oss-cn-guangzhou.aliyuncs.com -file: - access-domain: https://staging.example.com +sys: + file: + storage-type: oss + upload-mode: direct + access-domain: https://staging.example.com ecom: license: diff --git a/ecom-dmb-backend/src/main/resources/application-prod.yml b/ecom-dmb-backend/src/main/resources/application-prod.yml index f4535cc1..aab99d53 100644 --- a/ecom-dmb-backend/src/main/resources/application-prod.yml +++ b/ecom-dmb-backend/src/main/resources/application-prod.yml @@ -33,8 +33,11 @@ oss: access-key-secret: U0d3BeKADTJUteSsT5gBtxzlbHoJFi domain: https://xyquan.oss-cn-guangzhou.aliyuncs.com -file: - access-domain: https://api.example.com +sys: + file: + storage-type: oss + upload-mode: direct + access-domain: https://api.example.com ecom: license: diff --git a/ecom-dmb-backend/src/main/resources/application-test.yml b/ecom-dmb-backend/src/main/resources/application-test.yml index c48a503e..c581e6ed 100644 --- a/ecom-dmb-backend/src/main/resources/application-test.yml +++ b/ecom-dmb-backend/src/main/resources/application-test.yml @@ -31,8 +31,11 @@ oss: access-key-secret: U0d3BeKADTJUteSsT5gBtxzlbHoJFi domain: https://xyquan.oss-cn-guangzhou.aliyuncs.com -file: - access-domain: http://localhost:5666 +sys: + file: + storage-type: oss + upload-mode: direct + access-domain: http://localhost:5666 ecom: license: diff --git a/ecom-dmb-backend/src/main/resources/application.yml b/ecom-dmb-backend/src/main/resources/application.yml index a2152fa1..041137b9 100644 --- a/ecom-dmb-backend/src/main/resources/application.yml +++ b/ecom-dmb-backend/src/main/resources/application.yml @@ -124,9 +124,15 @@ mybatis-plus: configuration: map-underscore-to-camel-case: true -# OSS(开关与凭证见各环境配置) +# OSS 客户端(凭证与开关;是否用于存文件由 sys.file.storage-type 决定) oss: endpoint: https://oss-cn-guangzhou.aliyuncs.com bucket-name: xyquan + enabled: false -# 拼多多渠道配置见 config/channel-pdd.yml +# 系统文件:storage-type 决定落盘位置,upload-mode 决定前端上传路径 +sys: + file: + storage-type: local + upload-mode: multipart + max-size-mb: 50 diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/company/apis.ts b/ecom-dmb-frontend/apps/web-antd/src/api/company/apis.ts index 4b2f9172..42dfa1df 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/company/apis.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/company/apis.ts @@ -34,16 +34,3 @@ export async function updateCompanyApi(id: number, data: CompanyDTO.UpdateCompan export async function deleteCompanyApi(id: number) { return requestClient.delete(`/company/delete/${id}`); } - -/** - * 上传公司Logo - */ -export async function uploadCompanyLogoApi(file: File) { - const formData = new FormData(); - formData.append('file', file); - return requestClient.post('/company/upload-logo', formData, { - headers: { - 'Content-Type': 'multipart/form-data', - }, - }); -} diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/company/types/types.vo.ts b/ecom-dmb-frontend/apps/web-antd/src/api/company/types/types.vo.ts index 4ad65dc0..32fef51b 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/company/types/types.vo.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/company/types/types.vo.ts @@ -21,6 +21,8 @@ export namespace CompanyVO { email?: string; /** logo */ logo?: string; + /** logo 图片 ID */ + logoImageId?: number; /** 状态 (0-禁用 1-正常) */ status: number; /** 订阅到期时间 */ diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/ec/shop/apis.ts b/ecom-dmb-frontend/apps/web-antd/src/api/ec/shop/apis.ts index da1b2d94..899b7ade 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/ec/shop/apis.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/ec/shop/apis.ts @@ -44,19 +44,6 @@ export async function getShopDetailApi(id: number) { return requestClient.get(`/ec/shop/detail/${id}`); } -/** - * 上传店铺Logo - */ -export async function uploadShopLogoApi(file: File) { - const formData = new FormData(); - formData.append('file', file); - return requestClient.post('/ec/shop/upload-logo', formData, { - headers: { - 'Content-Type': 'multipart/form-data', - }, - }); -} - /** * 分页获取店铺商品信息 */ diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/apis.ts b/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/apis.ts new file mode 100644 index 00000000..9c42f154 --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/apis.ts @@ -0,0 +1,11 @@ +/** + * 系统配置 API(需登录) + */ +import type { SysConfigVO } from './types'; + +import { requestClient } from '#/api/request'; + +/** 获取系统配置(文件存储/上传模式等) */ +export async function getSysConfigApi() { + return requestClient.get('/sys/config'); +} diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/index.ts b/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/index.ts new file mode 100644 index 00000000..daf76d51 --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/index.ts @@ -0,0 +1,2 @@ +export * from './apis'; +export * from './types'; diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/types.ts b/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/types.ts new file mode 100644 index 00000000..b9c59950 --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/api/sys/config/types.ts @@ -0,0 +1,17 @@ +/** + * 系统配置 VO + */ +export namespace SysConfigVO { + export interface Config { + file: FileConfig; + } + + export interface FileConfig { + /** 存储方式:local / oss */ + storageType: 'local' | 'oss' | string; + /** 上传方式:multipart(传后端)/ direct(直传对象存储) */ + uploadMode: 'multipart' | 'direct' | string; + /** 单文件大小上限(MB) */ + maxFileSizeMb: number; + } +} diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/sys/file/apis.ts b/ecom-dmb-frontend/apps/web-antd/src/api/sys/file/apis.ts index 5b114ba8..c2fe25d0 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/sys/file/apis.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/sys/file/apis.ts @@ -9,14 +9,18 @@ import { requestClient } from '#/api/request'; */ /** - * 批量上传文件 + * 批量上传文件(multipart,由后端按 storageType 落盘) * @param files 文件列表 + * @param category 可选分类 */ -export async function batchUploadFilesApi(files: File[]) { +export async function batchUploadFilesApi(files: File[], category?: string) { const formData = new FormData(); files.forEach((file) => { formData.append('files', file); }); + if (category) { + formData.append('category', category); + } return requestClient.post( '/sys/file/batch-upload', formData, diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/sys/user/apis.ts b/ecom-dmb-frontend/apps/web-antd/src/api/sys/user/apis.ts index f99dfa81..621f0ec7 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/sys/user/apis.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/sys/user/apis.ts @@ -35,19 +35,6 @@ export async function changePasswordApi(data: UserDTO.ChangePasswordDTO) { return requestClient.post('/user/change-password', data); } -/** - * 上传用户头像 - */ -export async function uploadAvatarApi(file: File) { - const formData = new FormData(); - formData.append('file', file); - return requestClient.post('/user/upload-avatar', formData, { - headers: { - 'Content-Type': 'multipart/form-data', - }, - }); -} - /** * 获取用户菜单 */ diff --git a/ecom-dmb-frontend/apps/web-antd/src/components/ImportTaskDropdown.vue b/ecom-dmb-frontend/apps/web-antd/src/components/ImportTaskDropdown.vue index b084e6c2..40a3f52b 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/components/ImportTaskDropdown.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/components/ImportTaskDropdown.vue @@ -1,7 +1,7 @@