|
@@ -21,6 +21,8 @@ import (
|
|
|
"github.com/gin-gonic/gin"
|
|
|
excelize "github.com/xuri/excelize/v2"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
+
|
|
|
+ "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
)
|
|
|
|
|
|
// 固定Excel模板表头
|
|
@@ -73,12 +75,12 @@ func ExcelImportWithImages(c *gin.Context, apictx *ApiSession, file io.Reader, g
|
|
|
|
|
|
// 获取分类配置
|
|
|
cat := []*model.Category{}
|
|
|
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ err = repo.RepoSeachDocs(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
CollectName: repo.CollectionCategory,
|
|
|
Query: repo.Map{},
|
|
|
Project: []string{"name", "type", "children", "createTime"},
|
|
|
- }, cat)
|
|
|
- if err != nil || !found {
|
|
|
+ }, &cat)
|
|
|
+ if err != nil {
|
|
|
return nil, NewError("获取分类配置失败")
|
|
|
}
|
|
|
cates := cat[0].Children
|
|
@@ -508,7 +510,7 @@ func ZipImport(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
defer file.Close()
|
|
|
|
|
|
// 检查文件大小
|
|
|
- if header.Size > 100*1024*1024 { // 限制100MB
|
|
|
+ if header.Size > 500*1024*1024 { // 限制100MB
|
|
|
return nil, NewError("上传文件过大,请控制在100MB以内")
|
|
|
}
|
|
|
|
|
@@ -690,17 +692,52 @@ func findImageFile(dir, originalName string) string {
|
|
|
|
|
|
// 上传本地图片到OSS
|
|
|
func uploadLocalImage(filePath string, prefix string, apictx *ApiSession) (*model.OssType, error) {
|
|
|
- // 获取ObsClient
|
|
|
- obsClient, err := CreateObsClient()
|
|
|
+ // 获取Alibaba Cloud OSS Client
|
|
|
+ accessKeyID := apictx.Svc.Conf.Obs.AccessKeyId
|
|
|
+ secretKey := apictx.Svc.Conf.Obs.SecrateKey
|
|
|
+ endpoint := apictx.Svc.Conf.Obs.Endpoint
|
|
|
+
|
|
|
+ // 创建Alibaba Cloud OSS Client
|
|
|
+ client, err := oss.New(endpoint, accessKeyID, secretKey)
|
|
|
if err != nil {
|
|
|
- return nil, fmt.Errorf("创建ObsClient失败: %v", err)
|
|
|
+ return nil, fmt.Errorf("创建OSS Client失败: %v", err)
|
|
|
}
|
|
|
- defer obsClient.Close()
|
|
|
|
|
|
- // 上传图片到OSS
|
|
|
+ // 获取Bucket
|
|
|
bucketName := apictx.Svc.Conf.Obs.Bucket
|
|
|
+ bucket, err := client.Bucket(bucketName)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("获取Bucket失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 上传图片到OSS
|
|
|
ossPath := fmt.Sprintf("u/%s/%s", apictx.User.ID, prefix)
|
|
|
- return UploadFile(obsClient, bucketName, filePath, ossPath), nil
|
|
|
+ ossPath = strings.TrimRight(strings.ReplaceAll(ossPath, "\\", "/"), "/")
|
|
|
+ _, fileName := filepath.Split(filePath)
|
|
|
+ objectKey := ossPath + "/" + fileName
|
|
|
+
|
|
|
+ // 上传文件
|
|
|
+ err = bucket.PutObjectFromFile(objectKey, filePath)
|
|
|
+ if err != nil {
|
|
|
+ // 重试一次
|
|
|
+ err = bucket.PutObjectFromFile(objectKey, filePath)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("上传文件到OSS失败: %v", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取文件大小
|
|
|
+ fi, err := os.Stat(filePath)
|
|
|
+ size := int64(1)
|
|
|
+ if err == nil {
|
|
|
+ size = fi.Size()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回OSS文件信息
|
|
|
+ return &model.OssType{
|
|
|
+ Url: fmt.Sprintf("https://%s.%s/%s", bucketName, endpoint, objectKey),
|
|
|
+ Size: size,
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
// 工具函数 - 获取图片URL
|
|
@@ -948,6 +985,7 @@ func ZipExport(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
staffId := ""
|
|
|
if img.From != "" {
|
|
|
staff := model.StaffUser{}
|
|
|
+ // 验证样品收集人是否预设
|
|
|
found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
CollectName: repo.CollectionStaffUser,
|
|
|
Query: repo.Map{"name": img.From},
|