完成了文件上传

This commit is contained in:
chen-xin-zhi 2024-10-31 19:18:10 +08:00
parent 935cc4b0bd
commit 8dcf105e05
30 changed files with 1017 additions and 67 deletions

View File

@ -142,6 +142,7 @@
<version>1.6.2</version> <version>1.6.2</version>
</dependency> </dependency>
<!-- 工具类-->
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>

View File

@ -6,7 +6,7 @@ import com.cultural.heritage.constant.UserConstant;
import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.enums.UserRoleEnum; import com.cultural.heritage.model.enums.UserRoleEnum;
import com.cultural.heritage.service.userinfo.UserService; import com.cultural.heritage.service.user.UserService;
import io.micrometer.common.util.StringUtils; import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;

View File

@ -0,0 +1,70 @@
package com.cultural.heritage.config;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import java.text.SimpleDateFormat;
import java.util.Date;
@Data
@Configuration
public class HweiOBSConfig {
/**
* 访问密钥AK
*/
@Value("${hwyun.obs.accessKey}")
private String accessKey;
/**
* 访问密钥SK
*/
@Value("${hwyun.obs.securityKey}")
private String securityKey;
/**
* 终端节点
*/
@Value("${hwyun.obs.endPoint}")
private String endPoint;
/**
*
*/
@Value("${hwyun.obs.bucketName}")
private String bucketName;
/**
* @Description 获取OBS客户端实例
* @return: com.obs.services.ObsClient
*/
public ObsClient getInstance() {
return new ObsClient(accessKey, securityKey, endPoint);
}
/**
* @Description 销毁OBS客户端实例
* @param: obsClient
*/
public void destroy(ObsClient obsClient){
try {
obsClient.close();
} catch (ObsException e) {
} catch (Exception e) {
}
}
/**
* @Description 微服务文件存放路径
* @return: java.lang.String
*/
public static String getObjectKey() {
// 项目或者服务名称 + 日期存储方式
return "Hwei" + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date() )+ "/";
}
}

View File

@ -0,0 +1,15 @@
package com.cultural.heritage.constant;
public interface FileConstant {
/**
* 服务器访问地址
*/
String SERVER_HOST = "/static/img";
/**
* 服务器上传路径
*/
String SERVER_UPLOAD_DIR = "D:\\匠承非遗文件上传\\img";
}

View File

@ -0,0 +1,121 @@
package com.cultural.heritage.controller.address;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.address.AddressAddRequest;
import com.cultural.heritage.model.dto.address.AddressUpdateRequest;
import com.cultural.heritage.model.entity.Address;
import com.cultural.heritage.service.address.AddressService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/address")
@Slf4j
@Tag(name = "地址接口")
public class AddressController {
@Resource
private AddressService addressService;
/**
* 添加地址信息
* @param addressAddRequest 地址添加请求体
* @return 是否添加成功
*/
@PostMapping("/add")
public BaseResponse<Boolean> addAddress(@RequestBody AddressAddRequest addressAddRequest) {
if (addressAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Address address = new Address();
BeanUtils.copyProperties(addressAddRequest, address);
boolean save = addressService.save(address);
if (!save) {
throw new BusinessException(ErrorCode.OPERATION_ERROR);
}
return ResultUtils.success(true);
}
/**
* 删除地址信息
* @param deleteRequest 地址删除请求体
* @return 是否成功删除
*/
@PostMapping("/delete")
public BaseResponse<Boolean> delAddress(@RequestBody CommonRequest deleteRequest) {
if (deleteRequest == null || deleteRequest.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = deleteRequest.getId();
boolean result = addressService.removeById(id);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
/**
* 更新地址信息
* @param addressUpdateRequest 地址更新请求体
* @return 是否更新成功
*/
@PostMapping("/update")
public BaseResponse<Boolean> updateAddress(@RequestBody AddressUpdateRequest addressUpdateRequest) {
if (addressUpdateRequest == null || addressUpdateRequest.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Address address = new Address();
BeanUtils.copyProperties(addressUpdateRequest, address);
System.out.println(address);
boolean result = addressService.updateById(address);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
/**
* 查询用户地址信息
* @param addressQueryRequest 地址查询请求体
* @return 用户地址列表
*/
@PostMapping("/list")
public BaseResponse<List<Address>> listAddress(@RequestBody CommonRequest addressQueryRequest) {
if (addressQueryRequest == null || addressQueryRequest.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = addressQueryRequest.getId();
QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
addressQueryWrapper.eq("userId", id);
List<Address> list = addressService.list(addressQueryWrapper);
if (CollectionUtils.isEmpty(list)) {
list = new ArrayList<>();
}
return ResultUtils.success(list);
}
}

View File

@ -0,0 +1,122 @@
package com.cultural.heritage.controller.file;
import cn.hutool.core.io.FileUtil;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.constant.FileConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.model.dto.file.UploadFileRequest;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.enums.FileUploadBizEnum;
import com.cultural.heritage.service.file.IHweiYunOBSService;
import com.cultural.heritage.service.user.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
@RestController
@RequestMapping("/file")
@Slf4j
@Tag(name = "文件上传接口")
public class FileController {
@Resource
private UserService userService;
@Resource
private IHweiYunOBSService iHweiYunOBSService;
/**
* 未登录的情况下的文件上传
* @param multipartFile 文件上传参数
* @param uploadFileRequest 文件业务类型请求体
* @return 图片可访问地址
*/
@PostMapping("/upload/server/not_login")
private BaseResponse<String> uploadServerFileNotLogin(@RequestPart("file")MultipartFile multipartFile, UploadFileRequest uploadFileRequest) {
//获取业务名称
String biz = uploadFileRequest.getBiz();
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
if (fileUploadBizEnum == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "业务名称错误");
}
// 校验文件
validFile(multipartFile, fileUploadBizEnum);
//文件目录根据业务用户来划分
String uuid = RandomStringUtils.randomAlphabetic(8);
String filename = uuid + "-" + multipartFile.getOriginalFilename();
String filepath = String.format("/%s/%s", fileUploadBizEnum.getValue(), filename);
// 判断目录是否存在
File file = new File(FileConstant.SERVER_UPLOAD_DIR, filepath);
if (!file.exists()) {
// 创建目录
boolean mkdirs = file.mkdirs();
if (!mkdirs) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "创建目录失败");
}
}
//返回可访问地址
String test = iHweiYunOBSService.fileUpload(multipartFile, "feiyi" + filepath);
return ResultUtils.success(test);
}
@PostMapping("/upload/server")
public BaseResponse<String> uploadServerFile(@RequestPart MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) {
//获取业务名称
String biz = uploadFileRequest.getBiz();
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
if (fileUploadBizEnum == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "业务名称错误");
}
// 校验文件
validFile(multipartFile, fileUploadBizEnum);
// 校验用户是否登录
User loginUser = userService.getLoginUser(request);
if (loginUser == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR, "未登录");
}
// 文件目录根据业务用户来划分
String uuid = RandomStringUtils.randomAlphabetic(8);
String s = uuid + "-" + multipartFile.getOriginalFilename();
return null;
}
/**
* 校验文件
*
* @param multipartFile 文件
* @param fileUploadBizEnum 业务类型
*/
private void validFile(MultipartFile multipartFile, FileUploadBizEnum fileUploadBizEnum) {
//文件大小
long fileSize = multipartFile.getSize();
//文件后缀
String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
final long LIMIT = 512 * 1024L;
if (fileSize > LIMIT) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件大小不能超过512K");
}
if (!Arrays.asList("jpeg", "jpg", "svg", "png", "webp").contains(fileSuffix)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型错误");
}
}
}

View File

@ -1,7 +1,6 @@
package com.cultural.heritage.controller.operategood; package com.cultural.heritage.controller.good;
import com.cultural.heritage.annotation.AuthCheck;
import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.common.ResultUtils;
@ -10,17 +9,22 @@ import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.category.CategoryAddRequest; import com.cultural.heritage.model.dto.category.CategoryAddRequest;
import com.cultural.heritage.model.entity.Category; import com.cultural.heritage.model.entity.Category;
import com.cultural.heritage.service.operategood.CategoryService; import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.service.good.CategoryService;
import com.cultural.heritage.service.good.GoodService;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/category") @RequestMapping("/category")
@ -32,6 +36,12 @@ public class CategoryController {
private CategoryService categoryService; private CategoryService categoryService;
@Resource
private GoodService goodService;
/** /**
* 添加商品类别 * 添加商品类别
* @param categoryAddRequest 类别添加请求体 * @param categoryAddRequest 类别添加请求体
@ -42,6 +52,11 @@ public class CategoryController {
if (categoryAddRequest == null) { if (categoryAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
String typeName = categoryAddRequest.getTypeName();
List<String> typeNameList = categoryService.getTypeNameList();
if (typeNameList.contains(typeName)) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "已存在该类别");
}
Category category = new Category(); Category category = new Category();
BeanUtils.copyProperties(categoryAddRequest, category); BeanUtils.copyProperties(categoryAddRequest, category);
boolean save = categoryService.save(category); boolean save = categoryService.save(category);
@ -59,7 +74,7 @@ public class CategoryController {
*/ */
@PostMapping("/delete") @PostMapping("/delete")
public BaseResponse<Boolean> deleteCategory(@RequestBody CommonRequest deleteCategoryRequest) { public BaseResponse<Boolean> deleteCategory(@RequestBody CommonRequest deleteCategoryRequest) {
if (deleteCategoryRequest == null) { if (deleteCategoryRequest == null || deleteCategoryRequest.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Long id = deleteCategoryRequest.getId(); Long id = deleteCategoryRequest.getId();
@ -77,9 +92,14 @@ public class CategoryController {
*/ */
@PostMapping("/update") @PostMapping("/update")
public BaseResponse<Boolean> updateCategory(@RequestBody Category category) { public BaseResponse<Boolean> updateCategory(@RequestBody Category category) {
if (category == null) { if (category == null || category.getId() == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
String typeName = category.getTypeName();
List<String> typeNameList = categoryService.getTypeNameList();
if (typeNameList.contains(typeName)) {
throw new BusinessException(ErrorCode.OPERATION_ERROR, "已存在该类别");
}
boolean result = categoryService.updateById(category); boolean result = categoryService.updateById(category);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "类别更新成功"); return ResultUtils.success(true, "类别更新成功");
@ -87,14 +107,27 @@ public class CategoryController {
/** /**
* 查询商品类别 * 根据类别查询商品列表
* @return 类别列表 * @param categoryQueryRequest 类别查询请求体
* @return 当前类别的商品列表
*/ */
@PostMapping("/list") @PostMapping("/list/type")
public BaseResponse<List<Category>> listCategory() { public BaseResponse<Map<Category, List<Good>>> listGoodByCategory(@RequestBody CommonRequest categoryQueryRequest) {
List<Category> list = categoryService.list(); if (categoryQueryRequest == null || categoryQueryRequest.getId() == null) {
return ResultUtils.success(list); throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = categoryQueryRequest.getId();
Category category = categoryService.getCategoryById(id);
String typeName = category.getTypeName();
if (StringUtils.isBlank(typeName)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
List<Good> goodList = goodService.getGoodListByTypeName(typeName);
Map<Category, List<Good>> map = new HashMap<>();
map.put(category, goodList);
return ResultUtils.success(map);
} }
} }

View File

@ -1,13 +1,11 @@
package com.cultural.heritage.controller.operategood; package com.cultural.heritage.controller.good;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cultural.heritage.annotation.AuthCheck;
import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.constant.UserConstant;
import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonDelBatchRequest; import com.cultural.heritage.model.dto.CommonDelBatchRequest;
@ -16,15 +14,13 @@ import com.cultural.heritage.model.dto.good.GoodAddRequest;
import com.cultural.heritage.model.dto.good.GoodQueryRequest; import com.cultural.heritage.model.dto.good.GoodQueryRequest;
import com.cultural.heritage.model.dto.good.GoodUpdateRequest; import com.cultural.heritage.model.dto.good.GoodUpdateRequest;
import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.service.operategood.GoodService; import com.cultural.heritage.service.good.CategoryService;
import com.cultural.heritage.service.good.GoodService;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@ -42,6 +38,10 @@ public class GoodController {
private GoodService goodService; private GoodService goodService;
@Resource
private CategoryService categoryService;
/** /**
* 添加商品 * 添加商品
* @param goodAddRequest 商品添加请求体 * @param goodAddRequest 商品添加请求体
@ -118,6 +118,7 @@ public class GoodController {
/** /**
* 批量删除商品 * 批量删除商品
* @param commonDelBatchRequest 批量删除请求体 * @param commonDelBatchRequest 批量删除请求体
@ -134,5 +135,4 @@ public class GoodController {
} }

View File

@ -1,4 +1,4 @@
package com.cultural.heritage.controller.userinfo; package com.cultural.heritage.controller.user;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -12,14 +12,13 @@ import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.user.*; import com.cultural.heritage.model.dto.user.*;
import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.UserVO; import com.cultural.heritage.model.vo.UserVO;
import com.cultural.heritage.service.userinfo.UserService; import com.cultural.heritage.service.user.UserService;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -42,19 +41,6 @@ public class UserController {
private UserService userService; private UserService userService;
// @GetMapping("/test")
// public String test(){
// return "匠承非遗";
// }
//
// @GetMapping("/list")
// public BaseResponse<List<User>> listUser(){
// return ResultUtils.success(userService.list());
// }
/** /**
* 用户登录 * 用户登录
* @param userLoginRequest 用户登录请求体 * @param userLoginRequest 用户登录请求体

View File

@ -0,0 +1,8 @@
package com.cultural.heritage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.Address;
public interface AddressMapper extends BaseMapper<Address> {
}

View File

@ -0,0 +1,48 @@
package com.cultural.heritage.model.dto.address;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class AddressAddRequest implements Serializable {
/**
* 联系人
*/
private String name;
/**
* 手机号
*/
private String phone;
/**
* 地区
*/
private String region;
/**
* 详细地址
*/
private String detailAddress;
/**
* 用户id
*/
private Long userId;
/**
* 是否为默认地址
*/
private Integer isDefault;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,53 @@
package com.cultural.heritage.model.dto.address;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class AddressUpdateRequest implements Serializable {
/**
* id
*/
private Long id;
/**
* 联系人
*/
private String name;
/**
* 手机号
*/
private String phone;
/**
* 地区
*/
private String region;
/**
* 详细地址
*/
private String detailAddress;
/**
* 用户id
*/
private Long userId;
/**
* 是否为默认地址
*/
private Integer isDefault;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,19 @@
package com.cultural.heritage.model.dto.file;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class UploadFileRequest implements Serializable {
/**
* 业务
*/
private String biz;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -63,6 +63,13 @@ public class GoodUpdateRequest implements Serializable {
*/ */
private Integer festivalOrder; private Integer festivalOrder;
/**
* 是否上架
*/
private Integer isShelves;
@TableField(exist = false) @TableField(exist = false)
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
} }

View File

@ -0,0 +1,63 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class Address implements Serializable {
/**
* id
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 联系人
*/
private String name;
/**
* 手机号
*/
private String phone;
/**
* 地区
*/
private String region;
/**
* 详细地址
*/
private String detailAddress;
/**
* 用户id
*/
private Long userId;
/**
* 是否删除
*/
private Integer isDelete;
/**
* 是否为默认地址
*/
private Integer isDefault;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,58 @@
package com.cultural.heritage.model.enums;
import lombok.Getter;
import org.springframework.util.ObjectUtils;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 文件上传业务类型枚举
*/
@Getter
public enum FileUploadBizEnum {
USER_AVATAR("头像", "user_avatar"),
GOOD_IMAGE("商品", "good"),
SYSTEM_IMAGE("系统", "system"),
TEST_IMAGE("测试", "test");
private final String text;
private final String value;
FileUploadBizEnum(String text, String value) {
this.text = text;
this.value = value;
}
/**
* 获取值列表
*/
public static List<String> getValues() {
return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
}
/**
* 根据 value 获取枚举
*/
public static FileUploadBizEnum getEnumByValue(String value) {
if (ObjectUtils.isEmpty(value)) {
return null;
}
for (FileUploadBizEnum anEnum : FileUploadBizEnum.values()) {
if (anEnum.value.equals(value)) {
return anEnum;
}
}
return null;
}
}

View File

@ -0,0 +1,8 @@
package com.cultural.heritage.service.address;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.Address;
public interface AddressService extends IService<Address> {
}

View File

@ -0,0 +1,12 @@
package com.cultural.heritage.service.address.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.mapper.AddressMapper;
import com.cultural.heritage.model.entity.Address;
import com.cultural.heritage.service.address.AddressService;
import org.springframework.stereotype.Service;
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
}

View File

@ -0,0 +1,42 @@
package com.cultural.heritage.service.file;
import org.springframework.web.multipart.MultipartFile;
/**
* 华为云OBS服务接口
*/
public interface IHweiYunOBSService {
/**
* @Description 删除文件
* @param: objectKey 文件名
* @return: boolean 执行结果
*/
// boolean delete(String objectKey);
// boolean deleteFileByPath(String objectKey);
/**
* @Description 批量删除文件
* @param: objectKeys 文件名集合
* @return: boolean 执行结果
*/
// boolean delete(List<String> objectKeys);
/**
* @Description 上传文件
* @param: uploadFile 上传文件
* @param: objectKey 文件名称
* @return: java.lang.String url访问路径
*/
String fileUpload(MultipartFile uploadFile, String objectKey);
// String uploadFileByte(byte data[], String objectKey);
// String uploadFile(File file);
//
// /**
// * @Description 文件下载
// * @param: objectKey
// * @return: java.io.InputStream
// */
// InputStream fileDownload(String objectKey);
}

View File

@ -0,0 +1,190 @@
package com.cultural.heritage.service.file.impl;
import com.cultural.heritage.config.HweiOBSConfig;
import com.cultural.heritage.service.file.IHweiYunOBSService;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* 华为云OBS服务业务层
*/
@Service
@Slf4j
public class HweiYunOBSServiceImpl implements IHweiYunOBSService {
@Autowired
private HweiOBSConfig hweiOBSConfig;
// @Override
// public boolean delete(String objectKey) {
// ObsClient obsClient = null;
// try {
// // 创建ObsClient实例
// obsClient = hweiOBSConfig.getInstance();
// // obs删除
// obsClient.deleteObject(hweiOBSConfig.getBucketName(), objectKey);
// } catch (ObsException e) {
// } finally {
// hweiOBSConfig.destroy(obsClient);
// }
// return true;
// }
// @Override
// public boolean deleteFileByPath(String path) {
// //pathhttps://wwwbak.obs.ap-southeast-1.myhuaweicloud.com/uploadFiles/xiehui/yongjiu/fujian/wode.jpg
// ObsClient obsClient = null;
// try {
// // 创建ObsClient实例
// obsClient = hweiOBSConfig.getInstance();
//
// String file_http_url = PropertiesUtil.readValue("file_http_url");
//
// File file = new File(path);
// String key = file.getName();//wode.jpg
//
// //realPath : uploadFiles/xiehui/yongjiu/fujian
// String realPath = path.replace(file_http_url, "");
// realPath = realPath.substring(0, realPath.lastIndexOf('/'));
//
// // obs删除
// obsClient.deleteObject(hweiOBSConfig.getBucketName(), realPath + "/" + key);
// } catch (ObsException e) {
// } finally {
// hweiOBSConfig.destroy(obsClient);
// }
// return false;
// }
// @Override
// public boolean delete(List<String> objectKeys) {
// ObsClient obsClient = null;
// try {
// obsClient = hweiOBSConfig.getInstance();
// DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(hweiOBSConfig.getBucketName());
// objectKeys.forEach(x -> deleteObjectsRequest.addKeyAndVersion(x));
// // 批量删除请求
// obsClient.deleteObjects(deleteObjectsRequest);
// return true;
// } catch (ObsException e) {
// } finally {
// hweiOBSConfig.destroy(obsClient);
// }
// return false;
// }
@Override
public String fileUpload(MultipartFile uploadFile, String objectKey) {
ObsClient obsClient = null;
try {
String bucketName = hweiOBSConfig.getBucketName();
obsClient = hweiOBSConfig.getInstance();
// 判断桶是否存在
boolean exists = obsClient.headBucket(bucketName);
if (!exists) {
// 若不存在则创建桶
HeaderResponse response = obsClient.createBucket(bucketName);
}
InputStream inputStream = uploadFile.getInputStream();
long available = inputStream.available();
PutObjectRequest request = new PutObjectRequest(bucketName, objectKey, inputStream);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(available);
request.setMetadata(objectMetadata);
// 设置对象访问权限为公共读
request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
PutObjectResult result = obsClient.putObject(request);
// 读取该已上传对象的URL
return result.getObjectUrl();
} catch (ObsException e) {
} catch (IOException e) {
} finally {
hweiOBSConfig.destroy(obsClient);
}
return null;
}
// @Override
// public String uploadFileByte(byte[] data, String fileName) {
// try {
// ObsClient obsClient = null;
// String bucketName = hweiOBSConfig.getBucketName();
// obsClient = hweiOBSConfig.getInstance();
// // 判断桶是否存在
// boolean exists = obsClient.headBucket(bucketName);
// if (!exists) {
// // 若不存在则创建桶
// HeaderResponse response = obsClient.createBucket(bucketName);
// }
//
// String file_http_url = PropertiesUtil.readValue("file_http_url");
//
// //转为File
// InputStream inputStream = new ByteArrayInputStream(data);
// String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);//后缀
// String uuid = UUID.randomUUID().toString().replace("-", "");//customer_YYYYMMDDHHMM
// fileName = uuid + "." + prefix; //文件全路径
// obsClient.putObject(bucketName, fileName.substring(0, fileName.lastIndexOf("/")) + "/" + fileName, inputStream);
// return file_http_url + fileName;
// } catch (ObsException e) {
// }
// return null;
// }
// @Override
// public String uploadFile(File file) {
// ObsClient obsClient = null;
// try {
// String bucketName = hweiOBSConfig.getBucketName();
// obsClient = hweiOBSConfig.getInstance();
// // 判断桶是否存在
// boolean exists = obsClient.headBucket(bucketName);
// if (!exists) {
// // 若不存在则创建桶
// HeaderResponse response = obsClient.createBucket(bucketName);
// }
//
// String file_http_url = PropertiesUtil.readValue("file_http_url");
//
// //转为File
// String filename = file.getName();
// String prefix = filename.substring(filename.lastIndexOf(".") + 1);//后缀
// String uuid = UUID.randomUUID().toString().replace("-", "");//customer_YYYYMMDDHHMM
// String fileName = uuid + "." + prefix; //文件全路径
// obsClient.putObject(bucketName, fileName.substring(0, fileName.lastIndexOf("/")) + "/" + fileName, file);
// return file_http_url + fileName;
// } catch (Exception e) {
// } finally {
// hweiOBSConfig.destroy(obsClient);
// }
// return null;
// }
// @Override
// public InputStream fileDownload(String objectKey) {
// ObsClient obsClient = null;
// try {
// String bucketName = hweiOBSConfig.getBucketName();
// obsClient = hweiOBSConfig.getInstance();
// ObsObject obsObject = obsClient.getObject(bucketName, objectKey);
// return obsObject.getObjectContent();
// } catch (ObsException e) {
// } finally {
// hweiOBSConfig.destroy(obsClient);
// }
// return null;
// }
}

View File

@ -0,0 +1,22 @@
package com.cultural.heritage.service.good;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.Category;
import java.util.List;
public interface CategoryService extends IService<Category> {
/**
* 获取类别名列表
*/
List<String> getTypeNameList();
/**
* 根据id获取类别
*/
Category getCategoryById(Long id);
}

View File

@ -1,14 +1,22 @@
package com.cultural.heritage.service.operategood; package com.cultural.heritage.service.good;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.dto.good.GoodQueryRequest; import com.cultural.heritage.model.dto.good.GoodQueryRequest;
import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.Good;
import java.util.List;
public interface GoodService extends IService<Good> { public interface GoodService extends IService<Good> {
/** /**
* 获取查询条件 * 获取查询条件
*/ */
QueryWrapper<Good> getGoodQueryWrapper(GoodQueryRequest goodQueryRequest); QueryWrapper<Good> getGoodQueryWrapper(GoodQueryRequest goodQueryRequest);
/**
* 根据商品类名查询商品列表
*/
List<Good> getGoodListByTypeName(String typeName);
} }

View File

@ -0,0 +1,43 @@
package com.cultural.heritage.service.good.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.mapper.CategoryMapper;
import com.cultural.heritage.model.entity.Category;
import com.cultural.heritage.service.good.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
/**
* 获取类别名列表
*/
@Override
public List<String> getTypeNameList() {
List<Category> list = this.list();
List<String> collect = list.stream().map(Category::getTypeName).collect(Collectors.toList());
if (CollectionUtils.isEmpty(collect)) {
return new ArrayList<>();
}
return collect;
}
@Override
public Category getCategoryById(Long id) {
QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", id);
Category category = this.baseMapper.selectOne(queryWrapper);
if (category == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
return category;
}
}

View File

@ -1,4 +1,4 @@
package com.cultural.heritage.service.operategood.impl; package com.cultural.heritage.service.good.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -8,15 +8,21 @@ import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.mapper.GoodMapper; import com.cultural.heritage.mapper.GoodMapper;
import com.cultural.heritage.model.dto.good.GoodQueryRequest; import com.cultural.heritage.model.dto.good.GoodQueryRequest;
import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.entity.User; import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.operategood.GoodService;
import com.cultural.heritage.utils.SqlUtils; import com.cultural.heritage.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
@Service @Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService { public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {
/**
* 商品查询条件
*/
@Override @Override
public QueryWrapper<Good> getGoodQueryWrapper(GoodQueryRequest goodQueryRequest) { public QueryWrapper<Good> getGoodQueryWrapper(GoodQueryRequest goodQueryRequest) {
if (goodQueryRequest == null) { if (goodQueryRequest == null) {
@ -41,4 +47,19 @@ public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements Go
return queryWrapper; return queryWrapper;
} }
/**
* 根据商品类名查询商品列表
*/
@Override
public List<Good> getGoodListByTypeName(String typeName) {
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", typeName);
List<Good> list = this.list(queryWrapper);
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
return list;
}
} }

View File

@ -1,9 +0,0 @@
package com.cultural.heritage.service.operategood;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.Category;
public interface CategoryService extends IService<Category> {
}

View File

@ -1,11 +0,0 @@
package com.cultural.heritage.service.operategood.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.mapper.CategoryMapper;
import com.cultural.heritage.model.entity.Category;
import com.cultural.heritage.service.operategood.CategoryService;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}

View File

@ -1,4 +1,4 @@
package com.cultural.heritage.service.userinfo; package com.cultural.heritage.service.user;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;

View File

@ -1,4 +1,4 @@
package com.cultural.heritage.service.userinfo.impl; package com.cultural.heritage.service.user.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -10,7 +10,7 @@ import com.cultural.heritage.model.dto.user.UserQueryRequest;
import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.enums.UserRoleEnum; import com.cultural.heritage.model.enums.UserRoleEnum;
import com.cultural.heritage.model.vo.UserVO; import com.cultural.heritage.model.vo.UserVO;
import com.cultural.heritage.service.userinfo.UserService; import com.cultural.heritage.service.user.UserService;
import com.cultural.heritage.utils.SqlUtils; import com.cultural.heritage.utils.SqlUtils;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
@ -97,7 +97,6 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
if (currentUser == null || currentUser.getId() == null) { if (currentUser == null || currentUser.getId() == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR); throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
} }
//根据id进行查询 //根据id进行查询
Long userId = currentUser.getId(); Long userId = currentUser.getId();
currentUser = this.getById(userId); currentUser = this.getById(userId);

View File

@ -9,9 +9,13 @@ spring:
port: 6379 port: 6379
host: localhost host: localhost
database: 0 database: 0
servlet:
multipart:
max-file-size: 10MB
springdoc:
default-flat-param-object: true
server: server:
@ -27,6 +31,16 @@ mybatis-plus:
mapper-locations: classpath:mapper/*.xml mapper-locations: classpath:mapper/*.xml
configuration: configuration:
map-underscore-to-camel-case: false map-underscore-to-camel-case: false
global-config:
db-config:
logic-delete-field: isDelete #全局逻辑删除的实体字段名
logic-delete-value: 1 #逻辑已删除值(默认为1)
logic-not-delete-value: 0 #逻辑未删除值(默认为0)
hwyun:
obs:
accessKey: TEA5FAYCZDUSCEJP8NKX
securityKey: djX3WNrYjRDmp5v7rOXfa25e9CHj8OXKRzQJp6Ec
endPoint: obs.cn-north-4.myhuaweicloud.com
bucketName: carbon2

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cultural.heritage.mapper.AddressMapper">
</mapper>