通用文件上传

到店订单分页
This commit is contained in:
2024-05-15 14:53:06 +08:00
parent 0ebfa2795d
commit 34e06b6b3f
8 changed files with 88 additions and 15 deletions

View File

@@ -58,6 +58,11 @@
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@@ -42,7 +42,7 @@ public class LoginFilter implements Filter {
"cashierService/location/**",//高德 获取行政区域
"cashierService/home/homePageUp",//首页上半
"cashierService/home",//首页
"cashierService/common/**",//通用接口
"cashierService/distirict/**",//首页其它接口
"cashierService/login/**",//登录部分接口不校验
"cashierService/notify/**",//登录部分接口不校验

View File

@@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.controller;
import com.chaozhanggui.system.cashierservice.dao.TbPlatformDictMapper;
import com.chaozhanggui.system.cashierservice.entity.TbPlatformDict;
import com.chaozhanggui.system.cashierservice.exception.MsgException;
import com.chaozhanggui.system.cashierservice.service.FileService;
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.util.LocationUtils;
@@ -12,15 +13,12 @@ import com.chaozhanggui.system.cashierservice.util.ValidateCodeUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
@@ -39,6 +37,8 @@ public class CommonController {
private RedisUtils redisUtils;
@Resource
private TbPlatformDictMapper platformDictMapper;
@Resource
private FileService fileService;
/**
* 一分钟
*/
@@ -100,4 +100,9 @@ public class CommonController {
JsonNode jsonNode = mapper.readTree(gpsInfo);
return Result.success(CodeEnum.SUCCESS, jsonNode);
}
@RequestMapping("common/upload")
public Result upload(MultipartFile file) throws Exception {
return new Result(CodeEnum.SUCCESS, fileService.uploadFile(file));
}
}

View File

@@ -25,6 +25,7 @@ public class ProductVo {
private Integer id;
private String name; // 商品名称
private BigDecimal lowPrice; // 售价
private String groupNum; // 数量
private String unitName; // 单位
/**
* 商品标签

View File

@@ -0,0 +1,66 @@
package com.chaozhanggui.system.cashierservice.service;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.chaozhanggui.system.cashierservice.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.util.UUID;
@Service
@Slf4j
public class FileService {
@Value("${aliyun.keyid}")
private String accessKeyId;
@Value("${aliyun.keysecret}")
private String accessKeySecret;
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.bucketname}")
private String bucketName;
// 创建阿里云登录凭证
public CredentialsProvider credentialsProvider;
@PostConstruct
public void init() {
credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
}
public String uploadFile(MultipartFile file) throws Exception {
String suffix = FilenameUtils.getExtension(file.getResource().getFilename());
String path = getPath("upload", suffix);
OSS client = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
client.putObject(bucketName, path, file.getInputStream());
client.shutdown();
} catch (Exception e) {
throw new Exception("上传异常");
}
return "https://" + bucketName + "." + endpoint + "/" + path;
}
public String getPath(String prefix, String suffix) {
//生成uuid
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//文件路径
String path = DateUtils.getDays() + "/" + uuid;
if (StringUtils.isNotBlank(prefix)) {
path = prefix + "/" + path;
}
return path + "." + suffix;
}
}

View File

@@ -229,7 +229,7 @@ public class OrderService {
// date.setTotalNumber(number);
// }
// }
return Result.success(CodeEnum.ENCRYPT, tbOrderInfos);
return Result.success(CodeEnum.ENCRYPT, new PageInfo<>(tbOrderInfos));
}

View File

@@ -1,8 +1,5 @@
package com.chaozhanggui.system.cashierservice.util;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.chaozhanggui.system.cashierservice.sign.Result;
@@ -10,11 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static com.chaozhanggui.system.cashierservice.sign.CodeEnum.SUCCESS;

View File

@@ -51,3 +51,7 @@ logging:
aliyun:
keyid: LTAI5tPdEfYSZcqHbjCrtPRD
keysecret: DZjyHBq3nTujF0NMLxnZgsecU8ZCvy
oss:
bucketname: cashier-oss
endpoint: oss-cn-beijing.aliyuncs.com