通用文件上传

到店订单分页
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

@@ -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));
}