diff --git a/muyu-modules/muyu-product/pom.xml b/muyu-modules/muyu-product/pom.xml index 7243f3d..2c51567 100644 --- a/muyu-modules/muyu-product/pom.xml +++ b/muyu-modules/muyu-product/pom.xml @@ -82,7 +82,12 @@ com.muyu muyu-common-swagger - + + + com.aliyun.oss + aliyun-sdk-oss + 3.10.2 + diff --git a/muyu-modules/muyu-product/src/main/java/com/muyu/product/controller/BrandInfoController.java b/muyu-modules/muyu-product/src/main/java/com/muyu/product/controller/BrandInfoController.java new file mode 100644 index 0000000..e31a301 --- /dev/null +++ b/muyu-modules/muyu-product/src/main/java/com/muyu/product/controller/BrandInfoController.java @@ -0,0 +1,62 @@ +package com.muyu.product.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.muyu.common.core.domain.Result; +import com.muyu.common.core.utils.StringUtils; +import com.muyu.product.domain.BrandInfo; +import com.muyu.product.service.BrandInfoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @ClassName BrandInfoController + * @Description 描述 + * @Author ZHIHAO.DAI + * @Date 2024/3/6 21:15 + */ +@RestController +@RequestMapping("/brandInfo") +public class BrandInfoController { + @Autowired + private BrandInfoService brandInfoService; + + //新增品牌 + @PostMapping("/insertBrand") + public Result insertBrand(@RequestBody BrandInfo brandInfo){ + if (brandInfo.getId()!=null && !"".equals(brandInfo.getId())){ + BrandInfo updateBrandInfo = BrandInfo.updateBuildBrandInfo(brandInfo); + boolean update = brandInfoService.updateById(updateBrandInfo); + if (update){ + return Result.success("","修改成功!"); + } + return Result.error("修改失败"); + } + BrandInfo insertBrandInfo = BrandInfo.buildBrandInfo(brandInfo); + boolean save = brandInfoService.save(insertBrandInfo); + if (save){ + return Result.success(null,"添加成功"); + } + return Result.error("添加失败"); + } + + @GetMapping("/getBrandList") + public Result> getBrandList(@RequestParam("likeName") String likeName){ + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + if (StringUtils.isNotEmpty(likeName)){ + wrapper.like(BrandInfo::getName,likeName); + } + List list = brandInfoService.list(wrapper); + return Result.success(list); + } + @GetMapping("deleteBrandInfo") + public Result deleteBrandInfo(Integer brandId){ + boolean b = brandInfoService.removeById(brandId); + if (b){ + return Result.success("","删除成功!"); + } + return Result.error("删除失败"); + } +} diff --git a/muyu-modules/muyu-product/src/main/java/com/muyu/product/controller/UploadPicController.java b/muyu-modules/muyu-product/src/main/java/com/muyu/product/controller/UploadPicController.java new file mode 100644 index 0000000..64c44a8 --- /dev/null +++ b/muyu-modules/muyu-product/src/main/java/com/muyu/product/controller/UploadPicController.java @@ -0,0 +1,158 @@ +package com.muyu.product.controller; + +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.ObjectMetadata; +import com.aliyun.oss.model.PutObjectResult; +import com.muyu.common.core.domain.Result; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Date; +import java.util.Random; + +/** + * @ClassName UploadPicController + * @Description 描述 + * @Author ZHIHAO.DAI + * @Date 2024/3/6 21:16 + */ +@RestController +@RequestMapping("/upload") +@CrossOrigin +public class UploadPicController { + @Value("${aliyun.endpoint}") + private String endpoint; + //阿里云OSS账号 + @Value("${aliyun.accessKeyId}") + private String accessKeyId; + //阿里云OSS密钥 + @Value("${aliyun.accessKeySecret}") + private String accessKeySecret; + //阿里云OSS上的存储块bucket名字 + @Value("${aliyun.bucketName}") + private String bucketName; + //阿里云图片文件存储目录 + @Value("${aliyun.filedir}") + private String filedir; + + + @PostMapping("/uploadPic") + public Result uploadImg(@RequestParam("file")MultipartFile file){ + if (file.getSize()>1024*1024*20){ + return Result.error("图片太大"); + } + //获取图片后缀 :.jpg .png等 + String originalFilename = file.getOriginalFilename(); + String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase(); + //设定图片前缀 + Random random = new Random(); + String name = random.nextInt(10000) + substring; + //尝试将文件化作流 + try { + InputStream inputStream = file.getInputStream(); + //调用方法,将文件流和名字传过去 + String s = this.realUploadFile(inputStream, name); + System.out.println("他的返回值是:"+s); + String imgUrl = getImgUrl(name); + return Result.success(imgUrl); + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public String realUploadFile(InputStream inputStream, String fileName){ + String ret = ""; + + try { + //该对象表示元数据信息 + ObjectMetadata objectMetadata = new ObjectMetadata(); + //将文件正确的长度传递给Oss,以便使用正确的字节流处理 + objectMetadata.setContentLength(inputStream.available()); + objectMetadata.setCacheControl("no-cache"); + objectMetadata.setHeader("pragma", "no-cache"); + objectMetadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf(".")))); + objectMetadata.setContentDisposition("inline;filename=" +fileName); + + //开始上传 + OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); + PutObjectResult putObjectResult = ossClient.putObject(bucketName, filedir + fileName, inputStream, objectMetadata); + ret = putObjectResult.getETag(); + } catch (IOException e) { + throw new RuntimeException(e); + }finally { + + try { + if (inputStream !=null){ + inputStream.close(); + } + }catch (IOException e) { + throw new RuntimeException(e); + } + } + System.out.println("ret:"+ret); + return ret; + } + public static String getContentType(String FilenameExtension) { + if (FilenameExtension.equalsIgnoreCase(".bmp")) { + return "image/bmp"; + } + if (FilenameExtension.equalsIgnoreCase(".gif")) { + return "image/gif"; + } + if (FilenameExtension.equalsIgnoreCase(".jpeg") || + FilenameExtension.equalsIgnoreCase(".jpg") || + FilenameExtension.equalsIgnoreCase(".png")) { + return "image/jpg"; + } + if (FilenameExtension.equalsIgnoreCase(".html")) { + return "text/html"; + } + if (FilenameExtension.equalsIgnoreCase(".txt")) { + return "text/plain"; + } + if (FilenameExtension.equalsIgnoreCase(".vsd")) { + return "application/vnd.visio"; + } + if (FilenameExtension.equalsIgnoreCase(".pptx") || + FilenameExtension.equalsIgnoreCase(".ppt")) { + return "application/vnd.ms-powerpoint"; + } + if (FilenameExtension.equalsIgnoreCase(".docx") || + FilenameExtension.equalsIgnoreCase(".doc")) { + return "application/msword"; + } + if (FilenameExtension.equalsIgnoreCase(".xml")) { + return "text/xml"; + } + return "image/jpg"; + } + public String getImgUrl(String fileUrl) { + System.out.println(fileUrl); + if (!StringUtils.isEmpty(fileUrl)) { + String[] split = fileUrl.split("/"); + String url = this.getUrl(this.filedir + split[split.length - 1]); + String[] spilt1 = url.split("\\?"); + return spilt1[0]; + } + return null; + } + public String getUrl(String key) { + // 设置URL过期时间为10年 3600l* 1000*24*365*10 + Date expiration = new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 10); + // 生成URL + OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); + URL url = ossClient.generatePresignedUrl(bucketName, key, expiration); + System.out.println("生成的url是:"+url.toString()); + if (url != null) { + return url.toString(); + } + return null; + } + +} diff --git a/muyu-modules/muyu-product/src/main/java/com/muyu/product/domain/BrandInfo.java b/muyu-modules/muyu-product/src/main/java/com/muyu/product/domain/BrandInfo.java new file mode 100644 index 0000000..0892958 --- /dev/null +++ b/muyu-modules/muyu-product/src/main/java/com/muyu/product/domain/BrandInfo.java @@ -0,0 +1,66 @@ +package com.muyu.product.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.muyu.common.core.annotation.Excel; +import com.muyu.common.core.web.domain.BaseEntity; +import com.muyu.common.security.utils.SecurityUtils; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; + +import java.util.Date; + +/** + * @ClassName BrandInfo + * @Description 描述 + * @Author ZHIHAO.DAI + * @Date 2024/3/6 21:08 + */ +@Data +@SuperBuilder +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +@TableName("brand_info") +public class BrandInfo extends BaseEntity { + private static final long serialVersionUID = 1L; + @Excel(name = "参数主键", cellType = Excel.ColumnType.NUMERIC) + @TableId( type = IdType.AUTO) + private Integer id; + @Excel(name = "品牌名称") + private String name; + @Excel(name = "品牌logo") + private String logo; + + @Excel(name = "品牌介绍") + private String introduction; + + public static BrandInfo buildBrandInfo(BrandInfo brandInfo){ + return BrandInfo.builder() + .id(brandInfo.id) + .name(brandInfo.name) + .logo(brandInfo.logo) + .introduction(brandInfo.introduction) + .createBy(SecurityUtils.getUsername()) + .createTime(new Date()) + .remark("") + .build(); + } + + public static BrandInfo updateBuildBrandInfo(BrandInfo brandInfo){ + return BrandInfo.builder() + .id(brandInfo.id) + .name(brandInfo.name) + .logo(brandInfo.logo) + .introduction(brandInfo.introduction) + .updateBy(SecurityUtils.getUsername()) + .updateTime(new Date()) + .remark("") + .build(); + } + +} diff --git a/muyu-modules/muyu-product/src/main/java/com/muyu/product/mapper/BrandInfoMapper.java b/muyu-modules/muyu-product/src/main/java/com/muyu/product/mapper/BrandInfoMapper.java new file mode 100644 index 0000000..d91e4ff --- /dev/null +++ b/muyu-modules/muyu-product/src/main/java/com/muyu/product/mapper/BrandInfoMapper.java @@ -0,0 +1,13 @@ +package com.muyu.product.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.muyu.product.domain.BrandInfo; + +/** + * @ClassName BrandInfoMapper + * @Description 描述 + * @Author ZHIHAO.DAI + * @Date 2024/3/6 21:11 + */ +public interface BrandInfoMapper extends BaseMapper { +} diff --git a/muyu-modules/muyu-product/src/main/java/com/muyu/product/service/BrandInfoService.java b/muyu-modules/muyu-product/src/main/java/com/muyu/product/service/BrandInfoService.java new file mode 100644 index 0000000..fd68fa7 --- /dev/null +++ b/muyu-modules/muyu-product/src/main/java/com/muyu/product/service/BrandInfoService.java @@ -0,0 +1,13 @@ +package com.muyu.product.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.muyu.product.domain.BrandInfo; + +/** + * @ClassName BrandInfoService + * @Description 描述 + * @Author ZHIHAO.DAI + * @Date 2024/3/6 21:13 + */ +public interface BrandInfoService extends IService { +} diff --git a/muyu-modules/muyu-product/src/main/java/com/muyu/product/service/impl/BrandInfoServiceImpl.java b/muyu-modules/muyu-product/src/main/java/com/muyu/product/service/impl/BrandInfoServiceImpl.java new file mode 100644 index 0000000..c4f6c8f --- /dev/null +++ b/muyu-modules/muyu-product/src/main/java/com/muyu/product/service/impl/BrandInfoServiceImpl.java @@ -0,0 +1,18 @@ +package com.muyu.product.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.muyu.product.domain.BrandInfo; +import com.muyu.product.mapper.BrandInfoMapper; +import com.muyu.product.service.BrandInfoService; +import org.springframework.stereotype.Service; + +/** + * @ClassName BrandInfoServiceImpl + * @Description 描述 + * @Author ZHIHAO.DAI + * @Date 2024/3/6 21:13 + */ +@Service +public class BrandInfoServiceImpl extends ServiceImpl + implements BrandInfoService { +} diff --git a/muyu-modules/muyu-product/src/main/resources/bootstrap.yml b/muyu-modules/muyu-product/src/main/resources/bootstrap.yml index 4c6564b..329ca0f 100644 --- a/muyu-modules/muyu-product/src/main/resources/bootstrap.yml +++ b/muyu-modules/muyu-product/src/main/resources/bootstrap.yml @@ -23,6 +23,13 @@ spring: # 共享配置 shared-configs: - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} +aliyun: + endpoint: oss-cn-shanghai.aliyuncs.com + accessKeyId: LTAI5t7tFq3epGTDBkLtdmdd + accessKeySecret: ZI1Wpe8VFRLbsUE6dmyzZAf0n47H8o + filedir: test/hyc/dzh + accessPre: http://oss.zywjjj.vip + bucketName: hycdzh logging: level: com.muyu.system.mapper: DEBUG diff --git a/muyu-modules/muyu-product/src/main/resources/mapper/product/BrandInfoMapper.xml b/muyu-modules/muyu-product/src/main/resources/mapper/product/BrandInfoMapper.xml new file mode 100644 index 0000000..a9c9db9 --- /dev/null +++ b/muyu-modules/muyu-product/src/main/resources/mapper/product/BrandInfoMapper.xml @@ -0,0 +1,7 @@ + + + + +