From 82c6003b4afb6465caf7809d7513b9fd00aaf3fa Mon Sep 17 00:00:00 2001 From: Echo <1711788888@qq.com> Date: Thu, 31 Jul 2025 02:41:39 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96app=E7=AB=AF=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/example/exa_file_upload_download.go | 1 + initialize/router.go | 38 +++++++++---------- initialize/router_biz.go | 3 +- .../example/exa_file_upload_and_download.go | 6 ++- service/example/exa_file_upload_download.go | 9 ++++- utils/rand_code.go | 13 +++++++ 6 files changed, 48 insertions(+), 22 deletions(-) diff --git a/api/v1/example/exa_file_upload_download.go b/api/v1/example/exa_file_upload_download.go index dbced61..c1ac5f8 100644 --- a/api/v1/example/exa_file_upload_download.go +++ b/api/v1/example/exa_file_upload_download.go @@ -32,6 +32,7 @@ func (b *FileUploadAndDownloadApi) UploadFile(c *gin.Context) { response.FailWithMessage("接收文件失败", c) return } + file, err = fileUploadAndDownloadService.UploadFile(header, noSave, classId) // 文件上传后拿到文件路径 if err != nil { global.GVA_LOG.Error("上传文件失败!", zap.Error(err)) diff --git a/initialize/router.go b/initialize/router.go index 305a29e..8d40c4c 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -95,24 +95,24 @@ func Routers() *gin.Engine { } { - systemRouter.InitApiRouter(PrivateGroup, PublicGroup) // 注册功能api路由 - systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由 - systemRouter.InitUserRouter(PrivateGroup) // 注册用户路由 - systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由 - systemRouter.InitSystemRouter(PrivateGroup) // system相关路由 - systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由 - systemRouter.InitAutoCodeRouter(PrivateGroup, PublicGroup) // 创建自动化代码 - systemRouter.InitAuthorityRouter(PrivateGroup) // 注册角色路由 - systemRouter.InitSysDictionaryRouter(PrivateGroup) // 字典管理 - systemRouter.InitAutoCodeHistoryRouter(PrivateGroup) // 自动化代码历史 - systemRouter.InitSysOperationRecordRouter(PrivateGroup) // 操作记录 - systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理 - systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 按钮权限管理 - systemRouter.InitSysExportTemplateRouter(PrivateGroup, PublicGroup) // 导出模板 - systemRouter.InitSysParamsRouter(PrivateGroup, PublicGroup) // 参数管理 - exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由 - exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由 - exampleRouter.InitAttachmentCategoryRouterRouter(PrivateGroup) // 文件上传下载分类 + systemRouter.InitApiRouter(PrivateGroup, PublicGroup) // 注册功能api路由 + systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由 + systemRouter.InitUserRouter(PrivateGroup) // 注册用户路由 + systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由 + systemRouter.InitSystemRouter(PrivateGroup) // system相关路由 + systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由 + systemRouter.InitAutoCodeRouter(PrivateGroup, PublicGroup) // 创建自动化代码 + systemRouter.InitAuthorityRouter(PrivateGroup) // 注册角色路由 + systemRouter.InitSysDictionaryRouter(PrivateGroup) // 字典管理 + systemRouter.InitAutoCodeHistoryRouter(PrivateGroup) // 自动化代码历史 + systemRouter.InitSysOperationRecordRouter(PrivateGroup) // 操作记录 + systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理 + systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 按钮权限管理 + systemRouter.InitSysExportTemplateRouter(PrivateGroup, PublicGroup) // 导出模板 + systemRouter.InitSysParamsRouter(PrivateGroup, PublicGroup) // 参数管理 + exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由 + exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup, AppAuthGroup) // 文件上传下载功能路由 + exampleRouter.InitAttachmentCategoryRouterRouter(PrivateGroup) // 文件上传下载分类 } //APP相关路由 @@ -126,7 +126,7 @@ func Routers() *gin.Engine { InstallPlugin(PrivateGroup, PublicGroup, Router) // 注册业务路由 - initBizRouter(PrivateGroup, PublicGroup) + initBizRouter(PrivateGroup, PublicGroup, AppAuthGroup) PluginInit(PublicGroup, customerservice.CreateCustomerServicePlug()) PluginInit(PrivateGroup, picturelibrary.CreatePictureLibraryPlug()) diff --git a/initialize/router_biz.go b/initialize/router_biz.go index 9e838ab..8c1dc6f 100644 --- a/initialize/router_biz.go +++ b/initialize/router_biz.go @@ -12,6 +12,7 @@ func holder(routers ...*gin.RouterGroup) { func initBizRouter(routers ...*gin.RouterGroup) { privateGroup := routers[0] publicGroup := routers[1] + appGroup := routers[2] holder(publicGroup, privateGroup) { categoryRouter := router.RouterGroupApp.Category @@ -23,7 +24,7 @@ func initBizRouter(routers ...*gin.RouterGroup) { } { articleRouter := router.RouterGroupApp.Article - articleRouter.InitBotRouter(privateGroup, publicGroup) + articleRouter.InitBotRouter(privateGroup, publicGroup, appGroup) } { userRouter := router.RouterGroupApp.User diff --git a/router/example/exa_file_upload_and_download.go b/router/example/exa_file_upload_and_download.go index 84f6ecd..e905792 100644 --- a/router/example/exa_file_upload_and_download.go +++ b/router/example/exa_file_upload_and_download.go @@ -6,8 +6,9 @@ import ( type FileUploadAndDownloadRouter struct{} -func (e *FileUploadAndDownloadRouter) InitFileUploadAndDownloadRouter(Router *gin.RouterGroup) { +func (e *FileUploadAndDownloadRouter) InitFileUploadAndDownloadRouter(Router *gin.RouterGroup, APPRouter *gin.RouterGroup) { fileUploadAndDownloadRouter := Router.Group("fileUploadAndDownload") + appFileRouter := APPRouter.Group("appFile") { fileUploadAndDownloadRouter.POST("upload", exaFileUploadAndDownloadApi.UploadFile) // 上传文件 fileUploadAndDownloadRouter.POST("getFileList", exaFileUploadAndDownloadApi.GetFileList) // 获取上传文件列表 @@ -19,4 +20,7 @@ func (e *FileUploadAndDownloadRouter) InitFileUploadAndDownloadRouter(Router *gi fileUploadAndDownloadRouter.POST("removeChunk", exaFileUploadAndDownloadApi.RemoveChunk) // 删除切片 fileUploadAndDownloadRouter.POST("importURL", exaFileUploadAndDownloadApi.ImportURL) // 导入URL } + { + appFileRouter.POST("upload", exaFileUploadAndDownloadApi.UploadFile) // APP上传文件 + } } diff --git a/service/example/exa_file_upload_download.go b/service/example/exa_file_upload_download.go index ebacb1e..405e253 100644 --- a/service/example/exa_file_upload_download.go +++ b/service/example/exa_file_upload_download.go @@ -2,8 +2,11 @@ package example import ( "errors" + "git.echol.cn/loser/lckt/utils" "mime/multipart" + "strconv" "strings" + "time" "git.echol.cn/loser/lckt/global" "git.echol.cn/loser/lckt/model/example" @@ -94,11 +97,15 @@ func (e *FileUploadAndDownloadService) GetFileRecordInfoList(info request.ExaAtt func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string, classId int) (file example.ExaFileUploadAndDownload, err error) { oss := upload.NewOss() + s := strings.Split(header.Filename, ".") + if classId == 2 { + header.Filename = utils.GenerateRandomString(12) + "_" + strconv.FormatInt(time.Now().Unix(), 10) + "." + s[len(s)-1] + } filePath, key, uploadErr := oss.UploadFile(header) if uploadErr != nil { return file, uploadErr } - s := strings.Split(header.Filename, ".") + f := example.ExaFileUploadAndDownload{ Url: filePath, Name: header.Filename, diff --git a/utils/rand_code.go b/utils/rand_code.go index 4ca7184..aa32634 100644 --- a/utils/rand_code.go +++ b/utils/rand_code.go @@ -21,3 +21,16 @@ func GenerateInviteCode(userID uint) string { } return code } + +func GenerateRandomString(length int) string { + rand.Seed(time.Now().UnixNano()) + // 拼接用户ID和随机数 + data := fmt.Sprintf("%d%d", 6, rand.Intn(1000000)) + hash := md5.Sum([]byte(data)) + code := "" + for i := 0; i < 12; i++ { + // 取哈希的前6位,每位映射到字符集 + code += string(charset[int(hash[i])%len(charset)]) + } + return code +}