diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/tool/QiniuController.java b/ghy-admin/src/main/java/com/ghy/web/controller/tool/QiniuController.java new file mode 100644 index 00000000..a7dc03a0 --- /dev/null +++ b/ghy-admin/src/main/java/com/ghy/web/controller/tool/QiniuController.java @@ -0,0 +1,44 @@ +package com.ghy.web.controller.tool; + +import com.ghy.common.core.domain.AjaxResult; +import com.ghy.common.utils.QiniuUtils; +import io.swagger.annotations.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; + +/** + * 七牛云 + * + * @author clunt + */ +@Api +@Controller +@RequestMapping("/tool/qiniu") +public class QiniuController { + + private static final Logger logger = LoggerFactory.getLogger(QiniuController.class); + + /** + * 七牛云上传(单个) + */ + @PostMapping(value = "/upload") + public AjaxResult uploadFile(@RequestParam(value = "uploadFile", required = false) MultipartFile file) { + try { + // 上传后返回的文件路径 + String fileUrl = QiniuUtils.upload(file.getBytes()); + logger.info("upload return url is : " + fileUrl); + AjaxResult ajax = AjaxResult.success(); + ajax.put("url", fileUrl); + return ajax; + } catch (Exception e) { + return AjaxResult.error(e.getMessage()); + } + } + +} diff --git a/ghy-admin/src/main/java/com/ghy/web/core/config/SwaggerConfig.java b/ghy-admin/src/main/java/com/ghy/web/core/config/SwaggerConfig.java index 928868e1..64b2bc09 100644 --- a/ghy-admin/src/main/java/com/ghy/web/core/config/SwaggerConfig.java +++ b/ghy-admin/src/main/java/com/ghy/web/core/config/SwaggerConfig.java @@ -41,7 +41,7 @@ public class SwaggerConfig // 扫描所有有注解的api,用这种方式更灵活 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // 扫描指定包中的swagger注解 - .apis(RequestHandlerSelectors.basePackage("com.ghy.web.controller.goods")) + .apis(RequestHandlerSelectors.basePackage("com.ghy.web.controller.tool")) // 扫描所有 .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); diff --git a/ghy-admin/src/main/resources/application.yaml b/ghy-admin/src/main/resources/application.yaml index bf5e94d3..23aee072 100644 --- a/ghy-admin/src/main/resources/application.yaml +++ b/ghy-admin/src/main/resources/application.yaml @@ -139,4 +139,10 @@ xss: # Swagger配置 swagger: # 是否开启swagger - enabled: false + enabled: true + +qiniu: + accessKey: 'QTNOppkvtufxTxLjt1V7YZwvzV2Rc6WLD5yXLBVY' + secretKey: 'V8SM9nkbO-dft4JmG7UaCH6RYxXdqzrvQ0zWO2W3' + bucketName: 'gqz' + mediaUrl: 'http://gqz.opsoul.com/' diff --git a/ghy-common/pom.xml b/ghy-common/pom.xml index 1d828563..968f3cdb 100644 --- a/ghy-common/pom.xml +++ b/ghy-common/pom.xml @@ -17,6 +17,16 @@ + + com.google.code.gson + gson + + + + com.qiniu + qiniu-java-sdk + + org.projectlombok lombok diff --git a/ghy-common/src/main/java/com/ghy/common/config/QiniuConfig.java b/ghy-common/src/main/java/com/ghy/common/config/QiniuConfig.java new file mode 100644 index 00000000..abf4defe --- /dev/null +++ b/ghy-common/src/main/java/com/ghy/common/config/QiniuConfig.java @@ -0,0 +1,54 @@ +package com.ghy.common.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * 七牛云配置 + * @author clunt + */ +@Component +@ConfigurationProperties(prefix = "qiniu") +public class QiniuConfig { + + private static String accessKey; + + private static String secretKey; + + private static String bucketName; + + private static String mediaUrl; + + public static String getAccessKey() { + return accessKey; + } + + public static void setAccessKey(String accessKey) { + QiniuConfig.accessKey = accessKey; + } + + public static String getSecretKey() { + return secretKey; + } + + public static void setSecretKey(String secretKey) { + QiniuConfig.secretKey = secretKey; + } + + public static String getBucketName() { + return bucketName; + } + + public static void setBucketName(String bucketName) { + QiniuConfig.bucketName = bucketName; + } + + public static String getMediaUrl() { + return mediaUrl; + } + + public static void setMediaUrl(String mediaUrl) { + QiniuConfig.mediaUrl = mediaUrl; + } +} diff --git a/ghy-common/src/main/java/com/ghy/common/utils/QiniuUtils.java b/ghy-common/src/main/java/com/ghy/common/utils/QiniuUtils.java new file mode 100644 index 00000000..8ed1fdde --- /dev/null +++ b/ghy-common/src/main/java/com/ghy/common/utils/QiniuUtils.java @@ -0,0 +1,37 @@ +package com.ghy.common.utils; + +import com.google.gson.Gson; +import com.qiniu.common.Zone; +import com.qiniu.http.Response; +import com.qiniu.storage.Configuration; +import com.ghy.common.config.QiniuConfig; +import com.ghy.common.utils.uuid.UUID; +import com.qiniu.common.QiniuException; +import com.qiniu.storage.Region; +import com.qiniu.storage.UploadManager; +import com.qiniu.storage.model.DefaultPutRet; +import com.qiniu.util.Auth; + +public class QiniuUtils { + + public static String getUpToken() { + Auth auth = Auth.create(QiniuConfig.getAccessKey(), QiniuConfig.getSecretKey()); + return auth.uploadToken(QiniuConfig.getBucketName()); + } + + public static String upload(byte[] uploadBytes) throws QiniuException { + + String key = UUID.randomUUID().toString().replaceAll("-", ""); + + Configuration cfg = new Configuration(Region.region0());//设置华南的服务器 + UploadManager uploadManager = new UploadManager(cfg); + + String upToken = getUpToken(); + Response response = uploadManager.put(uploadBytes, key, upToken); + //解析上传成功的结果 + DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); + + return QiniuConfig.getMediaUrl() + putRet.key; + } + +} diff --git a/ghy-goods/src/main/java/com/ghy/goods/domain/Goods.java b/ghy-goods/src/main/java/com/ghy/goods/domain/Goods.java index 63fda724..567968b2 100644 --- a/ghy-goods/src/main/java/com/ghy/goods/domain/Goods.java +++ b/ghy-goods/src/main/java/com/ghy/goods/domain/Goods.java @@ -45,6 +45,9 @@ public class Goods extends BaseEntity { @Excel(name = "商品图片", cellType = Excel.ColumnType.IMAGE) private String goodsImgUrl; + @Excel(name = "商品视频", cellType = Excel.ColumnType.STRING) + private String goodsVideoUrl; + @Excel(name = "商品库存,-1则表示无限制", cellType = Excel.ColumnType.NUMERIC) private Integer goodsNumber; diff --git a/ghy-goods/src/main/resources/mapper.goods/GoodsMapper.xml b/ghy-goods/src/main/resources/mapper.goods/GoodsMapper.xml index 7ec02aa1..943f3f76 100644 --- a/ghy-goods/src/main/resources/mapper.goods/GoodsMapper.xml +++ b/ghy-goods/src/main/resources/mapper.goods/GoodsMapper.xml @@ -12,6 +12,7 @@ + diff --git a/pom.xml b/pom.xml index 89ab87db..13112d3b 100644 --- a/pom.xml +++ b/pom.xml @@ -34,11 +34,26 @@ 2.3 2.17.1 1.18.12 + 7.4.0 + 2.8.5 + + + com.google.code.gson + gson + ${gson.version} + + + + com.qiniu + qiniu-java-sdk + ${qiniu.version} + + org.projectlombok lombok