diff --git a/community-security-common/src/main/java/com/zyh/common/domain/Area.java b/community-security-common/src/main/java/com/zyh/common/domain/Area.java new file mode 100644 index 0000000..648839c --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/domain/Area.java @@ -0,0 +1,14 @@ +package com.zyh.common.domain; + +import lombok.Data; + +@Data +public class Area { + private Integer id; + private String name; + private Integer areaId; + private Integer parentId; + + + +} diff --git a/community-security-common/src/main/java/com/zyh/common/domain/House.java b/community-security-common/src/main/java/com/zyh/common/domain/House.java new file mode 100644 index 0000000..768a9f1 --- /dev/null +++ b/community-security-common/src/main/java/com/zyh/common/domain/House.java @@ -0,0 +1,17 @@ +package com.zyh.common.domain; + +import lombok.Data; + +@Data +public class House { + private Integer id; + private String name; + private Integer jurisdiction; + private Double lng; + private Double lat; + private String pic; + private String plotId; + private String areaname; + + +} diff --git a/community-security-modules/community-security-modules-shop/pom.xml b/community-security-modules/community-security-modules-shop/pom.xml index 23dfb51..c2a2dc3 100644 --- a/community-security-modules/community-security-modules-shop/pom.xml +++ b/community-security-modules/community-security-modules-shop/pom.xml @@ -61,7 +61,18 @@ test + + com.github.tobato + fastdfs-client + 1.26.5 + + + + com.aliyun.oss + aliyun-sdk-oss + 3.10.2 + diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/controller/HouseController.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/controller/HouseController.java new file mode 100644 index 0000000..262ffbd --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/controller/HouseController.java @@ -0,0 +1,49 @@ +package com.zyh.system.controller; + +import com.zyh.common.domain.Area; +import com.zyh.common.domain.House; +import com.zyh.common.result.Result; +import com.zyh.system.service.HouseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@RestController +public class HouseController { + + @Autowired + private HouseService houseService; + + //新增小区 + @PostMapping("addHouse") + public Result addHouse(@RequestBody House house) { + return houseService.addHouse(house); + } + + //查询小区 + @PostMapping("getHouse") + public Result> getHouse() { + return houseService.getHouse(); + } + + //查询行政区树 + @PostMapping("getJurisdictionTree") + public Result> getJurisdictionTree(@RequestParam Integer parentId) { + return houseService.getJurisdictionTree(parentId); + } + + //上传图片 + @PostMapping("sendPic") + public Result sendPic(@RequestParam("file") MultipartFile multipartFile) { + + Result result = houseService.sendPic(multipartFile); + + return result; + } + +} diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/mapper/HouseMapper.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/mapper/HouseMapper.java new file mode 100644 index 0000000..e9c7aca --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/mapper/HouseMapper.java @@ -0,0 +1,18 @@ +package com.zyh.system.mapper; + +import com.zyh.common.domain.Area; +import com.zyh.common.domain.House; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +@Mapper +public interface HouseMapper { + Integer addHouse(House house); + + List getJurisdictionTree(@Param("parentId") Integer parentId); + + List getHouse(); + +} diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/service/HouseService.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/service/HouseService.java new file mode 100644 index 0000000..0ef4a77 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/service/HouseService.java @@ -0,0 +1,18 @@ +package com.zyh.system.service; + +import com.zyh.common.domain.Area; +import com.zyh.common.domain.House; +import com.zyh.common.result.Result; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +public interface HouseService { + Result addHouse(House house); + + Result> getJurisdictionTree(Integer parentId); + + Result> getHouse(); + + Result sendPic(MultipartFile pic) ; +} diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/service/impl/HouseServicempl.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/service/impl/HouseServicempl.java new file mode 100644 index 0000000..5e92224 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/service/impl/HouseServicempl.java @@ -0,0 +1,76 @@ +package com.zyh.system.service.impl; + +import com.zyh.common.domain.Area; +import com.zyh.common.domain.House; +import com.zyh.common.result.Result; +import com.zyh.common.utils.StringUtils; +import com.zyh.system.mapper.HouseMapper; +import com.zyh.system.service.HouseService; +import com.zyh.system.util.FastUtil; +import com.zyh.system.util.OssUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@Service +public class HouseServicempl implements HouseService { + @Autowired + FastUtil fastUtil; + @Autowired + private HouseMapper houseMapper; + + @Override + public Result addHouse(House house) { + //校验参数 + + checkHouse(house); + + + + Integer add = houseMapper.addHouse(house); + if (add > 0) { + return Result.success("添加成功"); + } else { + return Result.error("添加失败"); + } + } + + private void checkHouse(House house) { + + if (StringUtils.isEmpty(house.getName())) { + throw new RuntimeException("名称不能为空"); + } + if (StringUtils.isEmpty(house.getPlotId())) { + throw new RuntimeException("所属小区编号不能为空"); + } + if (house.getJurisdiction() == null) { + throw new RuntimeException("所属辖区不能为空"); + } + } + + @Override + public Result> getJurisdictionTree(Integer parentId) { + List list=houseMapper.getJurisdictionTree(parentId); + + return Result.success(list); + } + + @Override + public Result> getHouse() { + List list=houseMapper.getHouse(); + return Result.success(list); + } + + @Override + public Result sendPic(MultipartFile pic) { + + + String sendPic = ""; + sendPic= OssUtil.uploadMultipartFile(pic); + + return Result.success(sendPic); + + } +} diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/util/FastUtil.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/util/FastUtil.java new file mode 100644 index 0000000..ff24836 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/util/FastUtil.java @@ -0,0 +1,55 @@ +package com.zyh.system.util; + +import com.github.tobato.fastdfs.domain.fdfs.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; + +/** + * @BelongsProject: 0107day02 + * @BelongsPackage: com.bw.config + * @Author: zhupengfei + * @CreateTime: 2023-02-01 08:52 + */ +@Component +public class FastUtil { + private static final Logger log = LoggerFactory.getLogger(FastUtil.class); + + @Resource + private FastFileStorageClient storageClient ; + + /** + * 上传文件 + */ + public String upload(MultipartFile multipartFile) throws Exception{ + String originalFilename = multipartFile.getOriginalFilename(). + substring(multipartFile.getOriginalFilename(). + lastIndexOf(".") + 1); + StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( + multipartFile.getInputStream(), + multipartFile.getSize(),originalFilename , null); + return storePath.getFullPath() ; + } + /** + * 删除文件 + */ + public String deleteFile(String fileUrl) { + if (StringUtils.isEmpty(fileUrl)) { + log.info("fileUrl == >>文件路径为空..."); + return "文件路径不能为空"; + } + try { + StorePath storePath = StorePath.parseFromUrl(fileUrl); + storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); + } catch (Exception e) { + log.error(e.getMessage()); + } + return "删除成功"; + } + +} diff --git a/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/util/OssUtil.java b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/util/OssUtil.java new file mode 100644 index 0000000..6a70102 --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/java/com/zyh/system/util/OssUtil.java @@ -0,0 +1,188 @@ +package com.zyh.system.util; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.model.GetObjectRequest; +import com.aliyun.oss.model.ObjectMetadata; +import com.aliyun.oss.model.PutObjectRequest; +import lombok.extern.log4j.Log4j2; +import org.springframework.web.multipart.MultipartFile; + +import java.io.*; +import java.time.LocalDateTime; +import java.util.UUID; + +/** + * Oss服务调用 + */ +@Log4j2 +public class OssUtil { + + /** + * Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述 + */ + private static String endPoint = "oss-cn-shanghai.aliyuncs.com"; + private static String accessKeyId = "LTAI5tN8mAfrDAwgKQwyAapY"; + private static String accessKeySecret = "AliAqC58cSClsFp5esdKfk48K1bXeD"; + private static String accessPre = "https://sirzpengbucket.oss-cn-shanghai.aliyuncs.com/"; + + + /** + * bucket名称 + * @return + */ + private static String bucketName = "sirzpengbucket"; + + private static OSS ossClient ; + + static { + ossClient = new OSSClientBuilder().build( + endPoint, + accessKeyId, + accessKeySecret); + log.info("oss服务连接成功!"); + } + + + /** + * 企业级阿里云 OSS 上传指定文件。 + * @param filePath 待上传的文件路径。 + * @return 如果上传成功,返回该文件在阿里云 OSS 存储上的访问路径;否则返回 null。 + **/ + public static String uploadFileFilePath(String filePath) { +// 创建OSSClient实例 + OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret); + + try { +// 生成唯一的文件名,可以根据需要进行调整 + String key = System.currentTimeMillis() + "_" + new File(filePath).getName(); + +// 设置上传文件的元信息 + ObjectMetadata metadata = new ObjectMetadata(); +// 可以根据需要设置其他元信息,例如Content-Type + +// 执行文件上传 + ossClient.putObject(bucketName, key, new File(filePath), metadata); + +// 返回访问路径 + return accessPre+key; + } catch (Exception e) { + e.printStackTrace(); + } finally { +// 关闭OSSClient + ossClient.shutdown(); + } + + return "上传失败"; + } + + /** + * 默认路径上传本地文件 + * @param filePath + */ + public static String uploadFile(String filePath){ + return uploadFileForBucket(bucketName,getOssFilePath(filePath) ,filePath); + } + + /** + * 默认路径上传multipartFile文件 + * @param multipartFile + */ + public static String uploadMultipartFile(MultipartFile multipartFile) { + return uploadMultipartFile(bucketName,getOssFilePath(multipartFile.getOriginalFilename()),multipartFile); + } + /** + * 上传 multipartFile 类型文件 + * @param bucketName + * @param ossPath + * @param multipartFile + */ + public static String uploadMultipartFile(String bucketName , String ossPath , MultipartFile multipartFile){ + InputStream inputStream = null; + try { + inputStream = multipartFile.getInputStream(); + } catch (IOException e) { + e.printStackTrace(); + } + uploadFileInputStreamForBucket(bucketName, ossPath, inputStream); + return accessPre+ossPath; + } + + /** + * 使用File上传PutObject上传文件 ** 程序默认使用次方法上传 + * @param bucketName 实例名称 + * @param ossPath oss存储路径 + * @param filePath 本地文件路径 + */ + public static String uploadFileForBucket(String bucketName , String ossPath , String filePath) { + // 创建PutObjectRequest对象。 + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath)); + + // 上传 + ossClient.putObject(putObjectRequest); + return accessPre+ossPath; + } + + /** + * 使用文件流上传到指定的bucket实例 + * @param bucketName 实例名称 + * @param ossPath oss存储路径 + * @param filePath 本地文件路径 + */ + public static String uploadFileInputStreamForBucket(String bucketName , String ossPath , String filePath){ + + // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。 + InputStream inputStream = null; + try { + inputStream = new FileInputStream(filePath); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。 + uploadFileInputStreamForBucket(bucketName, ossPath, inputStream); + return accessPre+ossPath; + } + + public static void uploadFileInputStreamForBucket(String bucketName , String ossPath , InputStream inputStream ){ + ossClient.putObject(bucketName, ossPath, inputStream); + } + + /** + * 下载 + * @param ossFilePath + * @param filePath + */ + public static void downloadFile(String ossFilePath , String filePath ){ + downloadFileForBucket(bucketName , ossFilePath , filePath); + } + /** + * 下载 + * @param bucketName 实例名称 + * @param ossFilePath oss存储路径 + * @param filePath 本地文件路径 + */ + public static void downloadFileForBucket(String bucketName , String ossFilePath , String filePath ){ + ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath)); + } + + /** + * + * @return + */ + public static String getOssDefaultPath(){ + LocalDateTime now = LocalDateTime.now(); + String url = + now.getYear()+"/"+ + now.getMonth()+"/"+ + now.getDayOfMonth()+"/"+ + now.getHour()+"/"+ + now.getMinute()+"/"; + return url; + } + + public static String getOssFilePath(String filePath){ + String fileSuf = filePath.substring(filePath.indexOf(".") + 1); + return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf; + } + +} diff --git a/community-security-modules/community-security-modules-shop/src/main/resources/mapper/SysUserMapper.xml b/community-security-modules/community-security-modules-shop/src/main/resources/mapper/SysUserMapper.xml new file mode 100644 index 0000000..1e6993d --- /dev/null +++ b/community-security-modules/community-security-modules-shop/src/main/resources/mapper/SysUserMapper.xml @@ -0,0 +1,20 @@ + + + + + + + insert into house values(0,#{plotId},#{name},#{jurisdiction},#{lng},#{lat},#{pic}) + + + +