🎨 优化app端文件上传接口
This commit is contained in:
@@ -32,6 +32,7 @@ func (b *FileUploadAndDownloadApi) UploadFile(c *gin.Context) {
|
|||||||
response.FailWithMessage("接收文件失败", c)
|
response.FailWithMessage("接收文件失败", c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err = fileUploadAndDownloadService.UploadFile(header, noSave, classId) // 文件上传后拿到文件路径
|
file, err = fileUploadAndDownloadService.UploadFile(header, noSave, classId) // 文件上传后拿到文件路径
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("上传文件失败!", zap.Error(err))
|
global.GVA_LOG.Error("上传文件失败!", zap.Error(err))
|
||||||
|
@@ -95,24 +95,24 @@ func Routers() *gin.Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
systemRouter.InitApiRouter(PrivateGroup, PublicGroup) // 注册功能api路由
|
systemRouter.InitApiRouter(PrivateGroup, PublicGroup) // 注册功能api路由
|
||||||
systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由
|
systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由
|
||||||
systemRouter.InitUserRouter(PrivateGroup) // 注册用户路由
|
systemRouter.InitUserRouter(PrivateGroup) // 注册用户路由
|
||||||
systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由
|
systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由
|
||||||
systemRouter.InitSystemRouter(PrivateGroup) // system相关路由
|
systemRouter.InitSystemRouter(PrivateGroup) // system相关路由
|
||||||
systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由
|
systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由
|
||||||
systemRouter.InitAutoCodeRouter(PrivateGroup, PublicGroup) // 创建自动化代码
|
systemRouter.InitAutoCodeRouter(PrivateGroup, PublicGroup) // 创建自动化代码
|
||||||
systemRouter.InitAuthorityRouter(PrivateGroup) // 注册角色路由
|
systemRouter.InitAuthorityRouter(PrivateGroup) // 注册角色路由
|
||||||
systemRouter.InitSysDictionaryRouter(PrivateGroup) // 字典管理
|
systemRouter.InitSysDictionaryRouter(PrivateGroup) // 字典管理
|
||||||
systemRouter.InitAutoCodeHistoryRouter(PrivateGroup) // 自动化代码历史
|
systemRouter.InitAutoCodeHistoryRouter(PrivateGroup) // 自动化代码历史
|
||||||
systemRouter.InitSysOperationRecordRouter(PrivateGroup) // 操作记录
|
systemRouter.InitSysOperationRecordRouter(PrivateGroup) // 操作记录
|
||||||
systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理
|
systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理
|
||||||
systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 按钮权限管理
|
systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 按钮权限管理
|
||||||
systemRouter.InitSysExportTemplateRouter(PrivateGroup, PublicGroup) // 导出模板
|
systemRouter.InitSysExportTemplateRouter(PrivateGroup, PublicGroup) // 导出模板
|
||||||
systemRouter.InitSysParamsRouter(PrivateGroup, PublicGroup) // 参数管理
|
systemRouter.InitSysParamsRouter(PrivateGroup, PublicGroup) // 参数管理
|
||||||
exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由
|
exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由
|
||||||
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
|
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup, AppAuthGroup) // 文件上传下载功能路由
|
||||||
exampleRouter.InitAttachmentCategoryRouterRouter(PrivateGroup) // 文件上传下载分类
|
exampleRouter.InitAttachmentCategoryRouterRouter(PrivateGroup) // 文件上传下载分类
|
||||||
|
|
||||||
}
|
}
|
||||||
//APP相关路由
|
//APP相关路由
|
||||||
@@ -126,7 +126,7 @@ func Routers() *gin.Engine {
|
|||||||
InstallPlugin(PrivateGroup, PublicGroup, Router)
|
InstallPlugin(PrivateGroup, PublicGroup, Router)
|
||||||
|
|
||||||
// 注册业务路由
|
// 注册业务路由
|
||||||
initBizRouter(PrivateGroup, PublicGroup)
|
initBizRouter(PrivateGroup, PublicGroup, AppAuthGroup)
|
||||||
|
|
||||||
PluginInit(PublicGroup, customerservice.CreateCustomerServicePlug())
|
PluginInit(PublicGroup, customerservice.CreateCustomerServicePlug())
|
||||||
PluginInit(PrivateGroup, picturelibrary.CreatePictureLibraryPlug())
|
PluginInit(PrivateGroup, picturelibrary.CreatePictureLibraryPlug())
|
||||||
|
@@ -12,6 +12,7 @@ func holder(routers ...*gin.RouterGroup) {
|
|||||||
func initBizRouter(routers ...*gin.RouterGroup) {
|
func initBizRouter(routers ...*gin.RouterGroup) {
|
||||||
privateGroup := routers[0]
|
privateGroup := routers[0]
|
||||||
publicGroup := routers[1]
|
publicGroup := routers[1]
|
||||||
|
appGroup := routers[2]
|
||||||
holder(publicGroup, privateGroup)
|
holder(publicGroup, privateGroup)
|
||||||
{
|
{
|
||||||
categoryRouter := router.RouterGroupApp.Category
|
categoryRouter := router.RouterGroupApp.Category
|
||||||
@@ -23,7 +24,7 @@ func initBizRouter(routers ...*gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
articleRouter := router.RouterGroupApp.Article
|
articleRouter := router.RouterGroupApp.Article
|
||||||
articleRouter.InitBotRouter(privateGroup, publicGroup)
|
articleRouter.InitBotRouter(privateGroup, publicGroup, appGroup)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
userRouter := router.RouterGroupApp.User
|
userRouter := router.RouterGroupApp.User
|
||||||
|
@@ -6,8 +6,9 @@ import (
|
|||||||
|
|
||||||
type FileUploadAndDownloadRouter struct{}
|
type FileUploadAndDownloadRouter struct{}
|
||||||
|
|
||||||
func (e *FileUploadAndDownloadRouter) InitFileUploadAndDownloadRouter(Router *gin.RouterGroup) {
|
func (e *FileUploadAndDownloadRouter) InitFileUploadAndDownloadRouter(Router *gin.RouterGroup, APPRouter *gin.RouterGroup) {
|
||||||
fileUploadAndDownloadRouter := Router.Group("fileUploadAndDownload")
|
fileUploadAndDownloadRouter := Router.Group("fileUploadAndDownload")
|
||||||
|
appFileRouter := APPRouter.Group("appFile")
|
||||||
{
|
{
|
||||||
fileUploadAndDownloadRouter.POST("upload", exaFileUploadAndDownloadApi.UploadFile) // 上传文件
|
fileUploadAndDownloadRouter.POST("upload", exaFileUploadAndDownloadApi.UploadFile) // 上传文件
|
||||||
fileUploadAndDownloadRouter.POST("getFileList", exaFileUploadAndDownloadApi.GetFileList) // 获取上传文件列表
|
fileUploadAndDownloadRouter.POST("getFileList", exaFileUploadAndDownloadApi.GetFileList) // 获取上传文件列表
|
||||||
@@ -19,4 +20,7 @@ func (e *FileUploadAndDownloadRouter) InitFileUploadAndDownloadRouter(Router *gi
|
|||||||
fileUploadAndDownloadRouter.POST("removeChunk", exaFileUploadAndDownloadApi.RemoveChunk) // 删除切片
|
fileUploadAndDownloadRouter.POST("removeChunk", exaFileUploadAndDownloadApi.RemoveChunk) // 删除切片
|
||||||
fileUploadAndDownloadRouter.POST("importURL", exaFileUploadAndDownloadApi.ImportURL) // 导入URL
|
fileUploadAndDownloadRouter.POST("importURL", exaFileUploadAndDownloadApi.ImportURL) // 导入URL
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
appFileRouter.POST("upload", exaFileUploadAndDownloadApi.UploadFile) // APP上传文件
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,8 +2,11 @@ package example
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"git.echol.cn/loser/lckt/utils"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.echol.cn/loser/lckt/global"
|
"git.echol.cn/loser/lckt/global"
|
||||||
"git.echol.cn/loser/lckt/model/example"
|
"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) {
|
func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader, noSave string, classId int) (file example.ExaFileUploadAndDownload, err error) {
|
||||||
oss := upload.NewOss()
|
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)
|
filePath, key, uploadErr := oss.UploadFile(header)
|
||||||
if uploadErr != nil {
|
if uploadErr != nil {
|
||||||
return file, uploadErr
|
return file, uploadErr
|
||||||
}
|
}
|
||||||
s := strings.Split(header.Filename, ".")
|
|
||||||
f := example.ExaFileUploadAndDownload{
|
f := example.ExaFileUploadAndDownload{
|
||||||
Url: filePath,
|
Url: filePath,
|
||||||
Name: header.Filename,
|
Name: header.Filename,
|
||||||
|
@@ -21,3 +21,16 @@ func GenerateInviteCode(userID uint) string {
|
|||||||
}
|
}
|
||||||
return code
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user