基础架构变更 增加模块化
This commit is contained in:
15
server/api/v1/example/enter.go
Normal file
15
server/api/v1/example/enter.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package example
|
||||
|
||||
import "gin-vue-admin/service"
|
||||
|
||||
type ApiGroup struct {
|
||||
CustomerApi
|
||||
ExcelApi
|
||||
FileUploadAndDownloadApi
|
||||
SimpleUploaderApi
|
||||
}
|
||||
|
||||
var fileUploadAndDownloadService = service.ServiceGroupApp.ExampleServiceGroup.FileUploadAndDownloadService
|
||||
var customerService = service.ServiceGroupApp.ExampleServiceGroup.CustomerService
|
||||
var excelService = service.ServiceGroupApp.ExampleServiceGroup.ExcelService
|
||||
var simpleUploaderService = service.ServiceGroupApp.ExampleServiceGroup.SimpleUploaderService
|
129
server/api/v1/example/exa_breakpoint_continue.go
Normal file
129
server/api/v1/example/exa_breakpoint_continue.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model/common/response"
|
||||
exampleRes "gin-vue-admin/model/example/response"
|
||||
"gin-vue-admin/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 断点续传到服务器
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "an example for breakpoint resume, 断点续传示例"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"切片创建成功"}"
|
||||
// @Router /fileUploadAndDownload/breakpointContinue [post]
|
||||
func (u *FileUploadAndDownloadApi) BreakpointContinue(c *gin.Context) {
|
||||
fileMd5 := c.Request.FormValue("fileMd5")
|
||||
fileName := c.Request.FormValue("fileName")
|
||||
chunkMd5 := c.Request.FormValue("chunkMd5")
|
||||
chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber"))
|
||||
chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal"))
|
||||
_, FileHeader, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("接收文件失败", c)
|
||||
return
|
||||
}
|
||||
f, err := FileHeader.Open()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("文件读取失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("文件读取失败", c)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
cen, _ := ioutil.ReadAll(f)
|
||||
if !utils.CheckMd5(cen, chunkMd5) {
|
||||
global.GVA_LOG.Error("检查md5失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("检查md5失败", c)
|
||||
return
|
||||
}
|
||||
err, file := fileUploadAndDownloadService.FindOrCreateFile(fileMd5, fileName, chunkTotal)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("查找或创建记录失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("查找或创建记录失败", c)
|
||||
return
|
||||
}
|
||||
err, pathc := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("断点续传失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("断点续传失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
if err = fileUploadAndDownloadService.CreateFileChunk(file.ID, pathc, chunkNumber); err != nil {
|
||||
global.GVA_LOG.Error("创建文件记录失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("创建文件记录失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("切片创建成功", c)
|
||||
}
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 查找文件
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "Find the file, 查找文件"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查找成功"}"
|
||||
// @Router /fileUploadAndDownload/findFile [post]
|
||||
func (u *FileUploadAndDownloadApi) FindFile(c *gin.Context) {
|
||||
fileMd5 := c.Query("fileMd5")
|
||||
fileName := c.Query("fileName")
|
||||
chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal"))
|
||||
err, file := fileUploadAndDownloadService.FindOrCreateFile(fileMd5, fileName, chunkTotal)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("查找失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("查找失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(exampleRes.FileResponse{File: file}, "查找成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 创建文件
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "上传文件完成"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"file uploaded, 文件创建成功"}"
|
||||
// @Router /fileUploadAndDownload/findFile [post]
|
||||
func (b *FileUploadAndDownloadApi) BreakpointContinueFinish(c *gin.Context) {
|
||||
fileMd5 := c.Query("fileMd5")
|
||||
fileName := c.Query("fileName")
|
||||
err, filePath := utils.MakeFile(fileName, fileMd5)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("文件创建失败!", zap.Any("err", err))
|
||||
response.FailWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "文件创建失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "文件创建成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 删除切片
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "删除缓存切片"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"缓存切片删除成功"}"
|
||||
// @Router /fileUploadAndDownload/removeChunk [post]
|
||||
func (u *FileUploadAndDownloadApi) RemoveChunk(c *gin.Context) {
|
||||
fileMd5 := c.Query("fileMd5")
|
||||
fileName := c.Query("fileName")
|
||||
filePath := c.Query("filePath")
|
||||
err := utils.RemoveChunk(fileMd5)
|
||||
err = fileUploadAndDownloadService.DeleteFileChunk(fileMd5, fileName, filePath)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("缓存切片删除失败!", zap.Any("err", err))
|
||||
response.FailWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "缓存切片删除失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "缓存切片删除成功", c)
|
||||
}
|
||||
}
|
143
server/api/v1/example/exa_customer.go
Normal file
143
server/api/v1/example/exa_customer.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model/common/request"
|
||||
"gin-vue-admin/model/common/response"
|
||||
"gin-vue-admin/model/example"
|
||||
exampleRes "gin-vue-admin/model/example/response"
|
||||
"gin-vue-admin/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type CustomerApi struct {
|
||||
}
|
||||
|
||||
// @Tags ExaCustomer
|
||||
// @Summary 创建客户
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.ExaCustomer true "客户用户名, 客户手机号码"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
|
||||
// @Router /customer/customer [post]
|
||||
func (e *CustomerApi) CreateExaCustomer(c *gin.Context) {
|
||||
var customer example.ExaCustomer
|
||||
_ = c.ShouldBindJSON(&customer)
|
||||
if err := utils.Verify(customer, utils.CustomerVerify); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
customer.SysUserID = utils.GetUserID(c)
|
||||
customer.SysUserAuthorityID = utils.GetUserAuthorityId(c)
|
||||
if err := customerService.CreateExaCustomer(customer); err != nil {
|
||||
global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("创建失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags ExaCustomer
|
||||
// @Summary 删除客户
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.ExaCustomer true "客户ID"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
||||
// @Router /customer/customer [delete]
|
||||
func (e *CustomerApi) DeleteExaCustomer(c *gin.Context) {
|
||||
var customer example.ExaCustomer
|
||||
_ = c.ShouldBindJSON(&customer)
|
||||
if err := utils.Verify(customer.GVA_MODEL, utils.IdVerify); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
if err := customerService.DeleteExaCustomer(customer); err != nil {
|
||||
global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("删除失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags ExaCustomer
|
||||
// @Summary 更新客户信息
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.ExaCustomer true "客户ID, 客户信息"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
|
||||
// @Router /customer/customer [put]
|
||||
func (e *CustomerApi) UpdateExaCustomer(c *gin.Context) {
|
||||
var customer example.ExaCustomer
|
||||
_ = c.ShouldBindJSON(&customer)
|
||||
if err := utils.Verify(customer.GVA_MODEL, utils.IdVerify); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
if err := utils.Verify(customer, utils.CustomerVerify); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
if err := customerService.UpdateExaCustomer(&customer); err != nil {
|
||||
global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("更新失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags ExaCustomer
|
||||
// @Summary 获取单一客户信息
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.ExaCustomer true "客户ID"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /customer/customer [get]
|
||||
func (e *CustomerApi) GetExaCustomer(c *gin.Context) {
|
||||
var customer example.ExaCustomer
|
||||
_ = c.ShouldBindQuery(&customer)
|
||||
if err := utils.Verify(customer.GVA_MODEL, utils.IdVerify); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
err, data := customerService.GetExaCustomer(customer.ID)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("获取失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(exampleRes.ExaCustomerResponse{Customer: data}, "获取成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags ExaCustomer
|
||||
// @Summary 分页获取权限客户列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.PageInfo true "页码, 每页大小"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /customer/customerList [get]
|
||||
func (e *CustomerApi) GetExaCustomerList(c *gin.Context) {
|
||||
var pageInfo request.PageInfo
|
||||
_ = c.ShouldBindQuery(&pageInfo)
|
||||
if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
err, customerList, total := customerService.GetCustomerInfoList(utils.GetUserAuthorityId(c), pageInfo)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("获取失败"+err.Error(), c)
|
||||
} else {
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: customerList,
|
||||
Total: total,
|
||||
Page: pageInfo.Page,
|
||||
PageSize: pageInfo.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
}
|
101
server/api/v1/example/exa_excel.go
Normal file
101
server/api/v1/example/exa_excel.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model/common/response"
|
||||
"gin-vue-admin/model/example"
|
||||
"gin-vue-admin/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ExcelApi struct {
|
||||
}
|
||||
|
||||
// /excel/importExcel 接口,与upload接口作用类似,只是把文件存到resource/excel目录下,用于导入Excel时存放Excel文件(ExcelImport.xlsx)
|
||||
// /excel/loadExcel接口,用于读取resource/excel目录下的文件((ExcelImport.xlsx)并加载为[]model.SysBaseMenu类型的示例数据
|
||||
// /excel/exportExcel 接口,用于读取前端传来的tableData,生成Excel文件并返回
|
||||
// /excel/downloadTemplate 接口,用于下载resource/excel目录下的 ExcelTemplate.xlsx 文件,作为导入的模板
|
||||
|
||||
// @Tags excel
|
||||
// @Summary 导出Excel
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/octet-stream
|
||||
// @Param data body model.ExcelInfo true "导出Excel文件信息"
|
||||
// @Success 200
|
||||
// @Router /excel/exportExcel [post]
|
||||
func (e *ExcelApi) ExportExcel(c *gin.Context) {
|
||||
var excelInfo example.ExcelInfo
|
||||
_ = c.ShouldBindJSON(&excelInfo)
|
||||
filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
|
||||
err := excelService.ParseInfoList2Excel(excelInfo.InfoList, filePath)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("转换Excel失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("转换Excel失败", c)
|
||||
return
|
||||
}
|
||||
c.Writer.Header().Add("success", "true")
|
||||
c.File(filePath)
|
||||
}
|
||||
|
||||
// @Tags excel
|
||||
// @Summary 导入Excel文件
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "导入Excel文件"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
|
||||
// @Router /excel/importExcel [post]
|
||||
func (e *ExcelApi) ImportExcel(c *gin.Context) {
|
||||
_, header, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("接收文件失败", c)
|
||||
return
|
||||
}
|
||||
_ = c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
|
||||
response.OkWithMessage("导入成功", c)
|
||||
}
|
||||
|
||||
// @Tags excel
|
||||
// @Summary 加载Excel数据
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"加载数据成功"}"
|
||||
// @Router /excel/loadExcel [get]
|
||||
func (e *ExcelApi) LoadExcel(c *gin.Context) {
|
||||
menus, err := excelService.ParseExcel2InfoList()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("加载数据失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("加载数据失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: menus,
|
||||
Total: int64(len(menus)),
|
||||
Page: 1,
|
||||
PageSize: 999,
|
||||
}, "加载数据成功", c)
|
||||
}
|
||||
|
||||
// @Tags excel
|
||||
// @Summary 下载模板
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param fileName query string true "模板名称"
|
||||
// @Success 200
|
||||
// @Router /excel/downloadTemplate [get]
|
||||
func (e *ExcelApi) DownloadTemplate(c *gin.Context) {
|
||||
fileName := c.Query("fileName")
|
||||
filePath := global.GVA_CONFIG.Excel.Dir + fileName
|
||||
ok, err := utils.PathExists(filePath)
|
||||
if !ok || err != nil {
|
||||
global.GVA_LOG.Error("文件不存在!", zap.Any("err", err))
|
||||
response.FailWithMessage("文件不存在", c)
|
||||
return
|
||||
}
|
||||
c.Writer.Header().Add("success", "true")
|
||||
c.File(filePath)
|
||||
}
|
83
server/api/v1/example/exa_file_upload_download.go
Normal file
83
server/api/v1/example/exa_file_upload_download.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model/common/request"
|
||||
"gin-vue-admin/model/common/response"
|
||||
"gin-vue-admin/model/example"
|
||||
exampleRes "gin-vue-admin/model/example/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type FileUploadAndDownloadApi struct {
|
||||
}
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 上传文件示例
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "上传文件示例"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
|
||||
// @Router /fileUploadAndDownload/upload [post]
|
||||
func (u *FileUploadAndDownloadApi) UploadFile(c *gin.Context) {
|
||||
var file example.ExaFileUploadAndDownload
|
||||
noSave := c.DefaultQuery("noSave", "0")
|
||||
_, header, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("接收文件失败", c)
|
||||
return
|
||||
}
|
||||
err, file = fileUploadAndDownloadService.UploadFile(header, noSave) // 文件上传后拿到文件路径
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("修改数据库链接失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("修改数据库链接失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithDetailed(exampleRes.ExaFileResponse{File: file}, "上传成功", c)
|
||||
}
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 删除文件
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Param data body model.ExaFileUploadAndDownload true "传入文件里面id即可"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
||||
// @Router /fileUploadAndDownload/deleteFile [post]
|
||||
func (u *FileUploadAndDownloadApi) DeleteFile(c *gin.Context) {
|
||||
var file example.ExaFileUploadAndDownload
|
||||
_ = c.ShouldBindJSON(&file)
|
||||
if err := fileUploadAndDownloadService.DeleteFile(file); err != nil {
|
||||
global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("删除失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// @Tags ExaFileUploadAndDownload
|
||||
// @Summary 分页文件列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.PageInfo true "页码, 每页大小"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /fileUploadAndDownload/getFileList [post]
|
||||
func (u *FileUploadAndDownloadApi) GetFileList(c *gin.Context) {
|
||||
var pageInfo request.PageInfo
|
||||
_ = c.ShouldBindJSON(&pageInfo)
|
||||
err, list, total := fileUploadAndDownloadService.GetFileRecordInfoList(pageInfo)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("获取失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: pageInfo.Page,
|
||||
PageSize: pageInfo.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
}
|
95
server/api/v1/example/exa_simple_uploader.go
Normal file
95
server/api/v1/example/exa_simple_uploader.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model/common/response"
|
||||
"gin-vue-admin/model/example"
|
||||
"gin-vue-admin/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type SimpleUploaderApi struct {
|
||||
}
|
||||
|
||||
// @Tags SimpleUploader
|
||||
// @Summary 断点续传插件版示例
|
||||
// @Security ApiKeyAuth
|
||||
// @accept multipart/form-data
|
||||
// @Produce application/json
|
||||
// @Param file formData file true "断点续传插件版示例"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"切片创建成功"}"
|
||||
// @Router /SimpleUploaderApi/upload [post]
|
||||
func (s *SimpleUploaderApi) SimpleUploaderUpload(c *gin.Context) {
|
||||
var chunk example.ExaSimpleUploader
|
||||
_, header, err := c.Request.FormFile("file")
|
||||
chunk.Filename = c.PostForm("filename")
|
||||
chunk.ChunkNumber = c.PostForm("chunkNumber")
|
||||
chunk.CurrentChunkSize = c.PostForm("currentChunkSize")
|
||||
chunk.Identifier = c.PostForm("identifier")
|
||||
chunk.TotalSize = c.PostForm("totalSize")
|
||||
chunk.TotalChunks = c.PostForm("totalChunks")
|
||||
var chunkDir = "./chunk/" + chunk.Identifier + "/"
|
||||
hasDir, _ := utils.PathExists(chunkDir)
|
||||
if !hasDir {
|
||||
if err := utils.CreateDir(chunkDir); err != nil {
|
||||
global.GVA_LOG.Error("创建目录失败!", zap.Any("err", err))
|
||||
}
|
||||
}
|
||||
chunkPath := chunkDir + chunk.Filename + chunk.ChunkNumber
|
||||
err = c.SaveUploadedFile(header, chunkPath)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("切片创建失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("切片创建失败", c)
|
||||
return
|
||||
}
|
||||
chunk.CurrentChunkPath = chunkPath
|
||||
err = simpleUploaderService.SaveChunk(chunk)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("切片创建失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("切片创建失败", c)
|
||||
return
|
||||
} else {
|
||||
response.OkWithMessage("切片创建成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags SimpleUploader
|
||||
// @Summary 断点续传插件版示例
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Param md5 query string true "md5"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
|
||||
// @Router /SimpleUploaderApi/checkFileMd5 [get]
|
||||
func (s *SimpleUploaderApi) CheckFileMd5(c *gin.Context) {
|
||||
md5 := c.Query("md5")
|
||||
err, chunks, isDone := simpleUploaderService.CheckFileMd5(md5)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("md5读取失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("md5读取失败", c)
|
||||
} else {
|
||||
response.OkWithDetailed(gin.H{
|
||||
"chunks": chunks,
|
||||
"isDone": isDone,
|
||||
}, "查询成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags SimpleUploader
|
||||
// @Summary 合并文件
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Param md5 query string true "md5"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"合并成功"}"
|
||||
// @Router /SimpleUploaderApi/mergeFileMd5 [get]
|
||||
func (s *SimpleUploaderApi) MergeFileMd5(c *gin.Context) {
|
||||
md5 := c.Query("md5")
|
||||
fileName := c.Query("fileName")
|
||||
err := simpleUploaderService.MergeFileMd5(md5, fileName)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("md5读取失败!", zap.Any("err", err))
|
||||
response.FailWithMessage("md5读取失败", c)
|
||||
} else {
|
||||
response.OkWithMessage("合并成功", c)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user