Procházet zdrojové kódy

上传列表添加

maker před 3 roky
rodič
revize
57dc9e63cc

+ 71 - 10
api/v1/task/upload_file.go

@@ -1,12 +1,14 @@
 package task
 
 import (
-	"fmt"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"log-server/global"
 	"log-server/model/common/response"
+	"log-server/model/task"
+	"log-server/model/task/request"
 	taskRes "log-server/model/task/response"
+	"log-server/utils"
 	"strconv"
 )
 
@@ -22,10 +24,19 @@ func (b *UploadFileApi) UploadFile(c *gin.Context) {
 		response.FailWithMessage("接收文件失败", c)
 		return
 	}
-	gameID := c.PostForm("game_id")
+	taskID := c.PostForm("task_id")
+	user   := c.PostForm("responsible_person")
+	if taskID == "" {
+		response.FailWithMessage("taskId不能为空", c)
+		return
+	}
+	if user == "" {
+		response.FailWithMessage("负责人不能为空", c)
+		return
+	}
 	var fileRes taskRes.UploadFileResponse
-	gameId, _ := strconv.Atoi(gameID)
-	fileRes, err = uploadFileService.UploadFile(gameId,  header) // 文件上传后拿到文件路径
+	taskId, _ := strconv.Atoi(taskID)
+	fileRes, err = uploadFileService.UploadFile(taskId, user,  header) // 文件上传后拿到文件路径
 	if err != nil {
 		global.GVA_LOG.Error("修改数据库链接失败!", zap.Error(err))
 		response.FailWithMessage("修改数据库链接失败", c)
@@ -35,17 +46,67 @@ func (b *UploadFileApi) UploadFile(c *gin.Context) {
 }
 
 func (b *UploadFileApi) DownloadFile(c *gin.Context) {
-	gameIdStr := c.Query("gameId")
+	taskIdStr := c.Query("taskId")
 	md5String := c.Query("md5String")
-
-	fmt.Println(gameIdStr)
-	gameId, _ := strconv.Atoi(gameIdStr)
-	downloadFile, err := uploadFileService.DownloadFile(gameId, md5String)
+	if taskIdStr == "" {
+		response.FailWithMessage("taskId不能为空", c)
+		return
+	}
+	if md5String == "" {
+		response.FailWithMessage("md5值不能为空", c)
+		return
+	}
+	taskId, _ := strconv.Atoi(taskIdStr)
+	downloadFile, err := uploadFileService.DownloadFile(taskId, md5String)
 	if err != nil {
 		global.GVA_LOG.Error("查询失败!", zap.Error(err))
-		response.FailWithMessage("查询失败", c)
+		response.FailWithMessage("查询失败,无对应脚本", c)
 	} else {
 		response.OkWithDetailed(taskRes.DownloadInfoResponse{downloadFile}, "查询成功", c)
 	}
 
 }
+
+
+//获取所有游戏脚本列表
+func (b *UploadFileApi) GetScriptList(c *gin.Context)  {
+	//获取前端传值并校验
+	var pageInfo task.SearchAccountTypeParams
+	_ = c.ShouldBindJSON(&pageInfo)
+	//页面信息校验
+	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil{
+		response.FailWithMessage(err.Error(), c)
+		return
+	}
+	if list, total, err := uploadFileService.GetScriptList(pageInfo.GameScriptResponse, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); err != nil{
+		global.GVA_LOG.Error("获取失败!", zap.Error(err))
+		response.FailWithMessage("获取失败", c)
+	} else {
+		response.OkWithDetailed(response.PageResult{
+			List:     list,
+			Total:    total,
+			Page:     pageInfo.Page,
+			PageSize: pageInfo.PageSize,
+		}, "获取成功", c)
+	}
+}
+
+//更改脚本状态
+func (b *UploadFileApi) StatusChange(c *gin.Context) {
+	var paramsInfo request.UpdateScriptStatusRequest
+	_ = c.ShouldBindJSON(&paramsInfo)
+	if paramsInfo.State == 0 {
+		response.FailWithMessage("状态值不能为空", c)
+		return
+	}
+	if paramsInfo.Id == 0 {
+		response.FailWithMessage("Id不能为空", c)
+		return
+	}
+	if err := uploadFileService.UpdateScriptStatus(paramsInfo); err != nil {
+		global.GVA_LOG.Error("修改失败!", zap.Error(err))
+		response.FailWithMessage(err.Error(), c)
+	} else {
+		response.OkWithMessage("修改成功", c)
+	}
+}

+ 1 - 0
config.yaml

@@ -87,6 +87,7 @@ jwt:
 local:
   path: uploads/file
   store-path: uploads/file
+  script-path: uploads/file/scriptFile
 mysql:
   path: 120.92.140.20
   port: "3306"

+ 1 - 0
config/oss_local.go

@@ -3,4 +3,5 @@ package config
 type Local struct {
 	Path      string `mapstructure:"path" json:"path" yaml:"path"`                   // 本地文件访问路径
 	StorePath string `mapstructure:"store-path" json:"store-path" yaml:"store-path"` // 本地文件存储路径
+	ScriptPath string `mapstructure:"script-path" json:"script-path" yaml:"script-path"` //本地脚本存储路径
 }

+ 32 - 0
model/task/game_script.go

@@ -0,0 +1,32 @@
+package task
+
+import "log-server/model/typeManage"
+
+type GameScript struct {
+	TaskID int `json:"task_id"`	//游戏Id
+	ResponsiblePerson string `json:"responsible_person"`	//负责人名称
+	Name string `json:"name" gorm:"comment:文件名"` // 文件名
+	Url  string `json:"url" gorm:"comment:文件地址"` // 文件地址
+	Tag  string `json:"tag" gorm:"comment:文件标签"` // 文件标签
+	Key  string `json:"key" gorm:"comment:编号"`   // 编号
+	Version int `json:"version"`	//版本号
+	Md5String string `json:"md5_string"`	//md5值
+	State int8 `json:"state"`	//脚本启用状态
+	CreateTime typeManage.LocalTime `json:"create_time"`
+	UpdateTime typeManage.LocalTime `json:"update_time"`
+}
+
+type GameScriptResponse struct {
+	Id int `json:"id"`	//id
+	TaskID int `json:"task_id"`	//游戏Id
+	ResponsiblePerson string `json:"responsible_person"`	//负责人名称
+	Name string `json:"name" gorm:"comment:文件名"` // 文件名
+	Url  string `json:"url" gorm:"comment:文件地址"` // 文件地址
+	Tag  string `json:"tag" gorm:"comment:文件标签"` // 文件标签
+	Key  string `json:"key" gorm:"comment:编号"`   // 编号
+	Version int `json:"version"`	//版本号
+	Md5String string `json:"md5_string"`	//md5值
+	State int8 `json:"state"`	//脚本启用状态
+	CreateTime typeManage.LocalTime `json:"create_time"`
+	UpdateTime typeManage.LocalTime `json:"update_time"`
+}

+ 5 - 0
model/task/request/download_file.go

@@ -4,3 +4,8 @@ type DownloadRequest struct {
 	GameId int `json:"game_id"`
 	Md5String string `json:"md5_string"`
 }
+
+type UpdateScriptStatusRequest struct {
+	State int `json:"state"` // 状态-1关闭,1开启
+	Id int `json:"id"`
+}

+ 3 - 1
model/task/response/upload_file.go

@@ -1,13 +1,15 @@
 package response
 
 type UploadFileResponse struct {
-	GameID int `json:"game_id"`	//游戏Id
+	TaskID int `json:"task_id"`	//游戏Id
+	ResponsiblePerson string `json:"responsible_person"`	//负责人名称
 	Name string `json:"name" gorm:"comment:文件名"` // 文件名
 	Url  string `json:"url" gorm:"comment:文件地址"` // 文件地址
 	Tag  string `json:"tag" gorm:"comment:文件标签"` // 文件标签
 	Key  string `json:"key" gorm:"comment:编号"`   // 编号
 	Version int `json:"version"`	//版本号
 	Md5String string `json:"md5_string"`	//md5值
+	State int8 `json:"state"`	//脚本启用状态
 }
 
 type InfoResponse struct {

+ 11 - 0
model/task/search_script_params.go

@@ -0,0 +1,11 @@
+package task
+
+import "log-server/model/common/request"
+
+// script分页条件查询及排序结构体
+type SearchAccountTypeParams struct {
+	GameScriptResponse
+	request.PageInfo
+	OrderKey string `json:"orderKey"` // 排序
+	Desc     bool   `json:"desc"`     // 排序方式:升序false(默认)|降序true
+}

+ 2 - 0
router/task/upload_file.go

@@ -15,6 +15,8 @@ func (e *UploadFileRouter) InitUploadFileRouter(Router *gin.RouterGroup) {
 	{
 		GameTaskRouter.POST("addFileUpdate", UploadFileApi.UploadFile)
 		GameTaskRouter.GET("downloadFile", UploadFileApi.DownloadFile)
+		GameTaskRouter.POST("getScriptList", UploadFileApi.GetScriptList)
+		GameTaskRouter.POST("statusChange", UploadFileApi.StatusChange)
 
 	}
 }

+ 84 - 13
service/task/upload_file.go

@@ -4,10 +4,14 @@ import (
 	"crypto/md5"
 	"encoding/hex"
 	"errors"
+	"fmt"
 	"go.uber.org/zap"
 	"gorm.io/gorm"
 	"io"
 	"log-server/global"
+	"log-server/model/common/request"
+	"log-server/model/task"
+	taskReq "log-server/model/task/request"
 	"log-server/model/task/response"
 	"log-server/utils"
 	"mime/multipart"
@@ -35,14 +39,15 @@ func (e *UploadFileService) UploadFileFunc(file *multipart.FileHeader) (string,
 	// 拼接新文件名
 	filename := name + "_" + time.Now().Format("20060102150405") + ext
 	// 尝试创建此路径
-	mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.StorePath, os.ModePerm)
+	mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.ScriptPath, os.ModePerm)
 	if mkdirErr != nil {
 		global.GVA_LOG.Error("function os.MkdirAll() Filed", zap.Any("err", mkdirErr.Error()))
 		return "", "", "", errors.New("function os.MkdirAll() Filed, err:" + mkdirErr.Error())
 	}
 	// 拼接路径和文件名
-	p := global.GVA_CONFIG.Local.StorePath + "/" + filename
-	filepath := global.GVA_CONFIG.Local.Path + "/" + filename
+	p := global.GVA_CONFIG.Local.ScriptPath + "/" + filename
+	filepath := global.GVA_CONFIG.Local.ScriptPath + "/" + filename
+
 
 	f, openError := file.Open() // 读取文件
 	f1, openErr := file.Open()	//	读取文件生成MD5值
@@ -67,6 +72,7 @@ func (e *UploadFileService) UploadFileFunc(file *multipart.FileHeader) (string,
 	defer out.Close() // 创建文件 defer 关闭
 
 	_, copyErr := io.Copy(out, f) // 传输(拷贝)文件
+	fmt.Println("拷贝文件")
 	if copyErr != nil {
 		global.GVA_LOG.Error("function io.Copy() Filed", zap.Any("err", copyErr.Error()))
 		return "", "", "",  errors.New("function io.Copy() Filed, err:" + copyErr.Error())
@@ -77,51 +83,56 @@ func (e *UploadFileService) UploadFileFunc(file *multipart.FileHeader) (string,
 
 
 //上传文件
-func (e *UploadFileService) UploadFile(gameId int, header *multipart.FileHeader) (file response.UploadFileResponse, err error) {
+func (e *UploadFileService) UploadFile(taskId int, user string, header *multipart.FileHeader) (file response.UploadFileResponse, err error) {
 	//查询数据库记录数
 	var count int64
 	//获取最后一条记录
 	var lastRecord response.UploadFileResponse
 	var version int
+	//定义下载前缀
+	var prefix string = global.GVA_CONFIG.PrefixUrl.PrefixLocal
 	filePath, key, md5String, uploadErr := e.UploadFileFunc(header)
 	if uploadErr != nil {
 		panic(err)
 	}
 
-	global.GVA_DB.Table("upload_file").Where("game_id = ?", gameId).Count(&count)
+	global.GVA_DB.Table("upload_file").Where("task_id = ?", taskId).Count(&count)
 	if count == 0 {
 		version = 1
 	} else {
-		global.GVA_DB.Table("upload_file").Where("game_id = ?", gameId).Last(&lastRecord)
+		global.GVA_DB.Table("upload_file").Where("task_id = ?", taskId).Last(&lastRecord)
 		version = lastRecord.Version + 1
 	}
 
 	s := strings.Split(header.Filename, ".")
 	f := response.UploadFileResponse{
-		GameID: gameId,
-		Url:  filePath,
+		TaskID: taskId,
+		ResponsiblePerson: user,
+		Url:  prefix + filePath,
 		Name: header.Filename,
 		Tag:  s[len(s)-1],
 		Key:  key,
 		Version: version,
 		Md5String: md5String,
+		State : 1,
 	}
 	return f, e.Upload(f)
 }
 
 //下载文件
-func (e *UploadFileService) DownloadFile(gameId int, md5String string) (info response.DownloadFile, err error) {
+func (e *UploadFileService) DownloadFile(taskId int, md5String string) (info response.DownloadFile, err error) {
 	var file response.UploadFileResponse
-	var prefix string = global.GVA_CONFIG.PrefixUrl.PrefixLocal
+	//改由存储至数据库的时候拼接前缀
+	//var prefix string = global.GVA_CONFIG.PrefixUrl.PrefixLocal
 	//数据库中无记录,即不需要更新
-	err = global.GVA_DB.Table("upload_file").Where("game_id = ?", gameId).Last(&file).Error
+	err = global.GVA_DB.Table("upload_file").Where("task_id = ? and state = 1", taskId).Last(&file).Error
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		info = response.DownloadFile{
 			Url: "",
 			Md5String: "",
 			Flag: false,
 		}
-		return info, errors.New("无对应game_id")
+		return info, errors.New("无对应脚本")
 	}
 
 	if file.Md5String == md5String {
@@ -131,7 +142,7 @@ func (e *UploadFileService) DownloadFile(gameId int, md5String string) (info res
 			Flag: false,
 		}
 	} else {
-		fullUrl := prefix + file.Url
+		fullUrl := file.Url
 		info = response.DownloadFile{
 			Url: fullUrl,
 			Md5String: file.Md5String,
@@ -141,5 +152,65 @@ func (e *UploadFileService) DownloadFile(gameId int, md5String string) (info res
 
 	return info, err
 
+}
+
+//条件查询脚本列表
+func (e *UploadFileService) GetScriptList(script task.GameScriptResponse, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
+	//获取分页数据
+	limit := info.PageSize
+	offset := (info.Page - 1) * info.PageSize
+	//绑定操作结构体
+	db := global.GVA_DB.Table("upload_file").Model(&task.GameScriptResponse{})
+	var scriptList []task.GameScriptResponse
+	//条件查询
+	if script.ResponsiblePerson != "" {
+		db = db.Where("responsible_person = ?", script.ResponsiblePerson)
+	}
+	if script.TaskID != 0 {
+		db = db.Where("task_id = ?", script.TaskID)
+	}
 
+	//先过滤在排序
+	if err = db.Count(&total).Error; err != nil {
+		return scriptList, total, err
+	} else {
+		//分页
+		db.Limit(limit).Offset(offset)
+		//判断是否有排序字段
+		if order != "" {
+			var orderStr string
+			orderMap := make(map[string]bool, 1)
+			orderMap["create_time"] = true
+			orderMap["task_id"] = true
+			orderMap["name"] = true
+			orderMap["responsible_person"] = true
+			if orderMap[order] {
+				//合法排序字符
+				if desc {
+					orderStr = order + " desc"
+				} else {
+					orderStr = order
+				}
+			} else {
+				//传入排序字段非法
+				err = fmt.Errorf("传入非法字段 %v", order)
+				return scriptList, total, err
+			}
+			err = db.Order(orderStr).Find(&scriptList).Error
+		} else {
+			//默认按照名字升序排序
+			err = db.Order("name").Find(&scriptList).Error
+		}
+	}
+	return scriptList, total, err
 }
+
+//更改脚本状态
+func (e *UploadFileService) UpdateScriptStatus(c taskReq.UpdateScriptStatusRequest) (err error) {
+	err = global.GVA_DB.Table("upload_file").Where("id = ?", c.Id).Error
+	if errors.Is(err, gorm.ErrRecordNotFound) {
+		return errors.New("请传入正确的Id")
+	}
+	err = global.GVA_DB.Table("upload_file").Where("id = ?", c.Id).Update("state", c.State).Error
+	return err
+}

binární
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230228152241.dll